Jose Fonseca | 247e1fa | 2019-04-28 14:14:44 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
José Fonseca | 97becb3 | 2013-05-25 13:29:25 +0100 | [diff] [blame] | 2 | ########################################################################## |
| 3 | # |
| 4 | # Copyright 2012 Jose Fonseca |
| 5 | # All Rights Reserved. |
| 6 | # |
| 7 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | # of this software and associated documentation files (the "Software"), to deal |
| 9 | # in the Software without restriction, including without limitation the rights |
| 10 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | # copies of the Software, and to permit persons to whom the Software is |
| 12 | # furnished to do so, subject to the following conditions: |
| 13 | # |
| 14 | # The above copyright notice and this permission notice shall be included in |
| 15 | # all copies or substantial portions of the Software. |
| 16 | # |
| 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | # THE SOFTWARE. |
| 24 | # |
| 25 | ##########################################################################/ |
| 26 | |
| 27 | |
| 28 | '''Simple script to extract PNG files from the JSON state dumps.''' |
| 29 | |
| 30 | |
| 31 | import json |
| 32 | import optparse |
| 33 | import base64 |
| 34 | import sys |
| 35 | |
| 36 | |
Jose Fonseca | f527924 | 2019-04-29 06:41:49 +0100 | [diff] [blame] | 37 | pngSignature = b"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A" |
José Fonseca | 9834070 | 2014-11-07 11:33:00 +0000 | [diff] [blame] | 38 | |
| 39 | |
José Fonseca | 97becb3 | 2013-05-25 13:29:25 +0100 | [diff] [blame] | 40 | def dumpSurfaces(state, memberName): |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 41 | for name, imageObj in state[memberName].items(): |
José Fonseca | 97becb3 | 2013-05-25 13:29:25 +0100 | [diff] [blame] | 42 | data = imageObj['__data__'] |
| 43 | data = base64.b64decode(data) |
| 44 | |
José Fonseca | 9834070 | 2014-11-07 11:33:00 +0000 | [diff] [blame] | 45 | if data.startswith(pngSignature): |
| 46 | extName = 'png' |
| 47 | else: |
| 48 | magic = data[:2] |
Jose Fonseca | f527924 | 2019-04-29 06:41:49 +0100 | [diff] [blame] | 49 | if magic in (b'P1', b'P4'): |
José Fonseca | 9834070 | 2014-11-07 11:33:00 +0000 | [diff] [blame] | 50 | extName = 'pbm' |
Jose Fonseca | f527924 | 2019-04-29 06:41:49 +0100 | [diff] [blame] | 51 | elif magic in (b'P2', b'P5'): |
José Fonseca | 9834070 | 2014-11-07 11:33:00 +0000 | [diff] [blame] | 52 | extName = 'pgm' |
Jose Fonseca | f527924 | 2019-04-29 06:41:49 +0100 | [diff] [blame] | 53 | elif magic in (b'P3', b'P6'): |
José Fonseca | 9834070 | 2014-11-07 11:33:00 +0000 | [diff] [blame] | 54 | extName = 'ppm' |
Jose Fonseca | f527924 | 2019-04-29 06:41:49 +0100 | [diff] [blame] | 55 | elif magic in (b'Pf', b'PF'): |
José Fonseca | 9834070 | 2014-11-07 11:33:00 +0000 | [diff] [blame] | 56 | extName = 'pfm' |
| 57 | else: |
| 58 | sys.stderr.write('warning: unsupport Netpbm format %s\n' % magic) |
| 59 | continue |
| 60 | |
| 61 | imageName = '%s.%s' % (name, extName) |
José Fonseca | 97becb3 | 2013-05-25 13:29:25 +0100 | [diff] [blame] | 62 | open(imageName, 'wb').write(data) |
| 63 | sys.stderr.write('Wrote %s\n' % imageName) |
| 64 | |
| 65 | |
| 66 | def main(): |
| 67 | optparser = optparse.OptionParser( |
| 68 | usage="\n\t%prog [options] <json>") |
| 69 | |
| 70 | (options, args) = optparser.parse_args(sys.argv[1:]) |
| 71 | |
| 72 | for arg in args: |
| 73 | state = json.load(open(arg, 'rt'), strict=False) |
| 74 | |
| 75 | dumpSurfaces(state, 'textures') |
| 76 | dumpSurfaces(state, 'framebuffer') |
| 77 | |
| 78 | |
| 79 | |
| 80 | if __name__ == '__main__': |
| 81 | main() |