blob: 917ee7e44187368c1f93e83f60cfddf316222cae [file] [log] [blame]
José Fonseca97becb32013-05-25 13:29:25 +01001#!/usr/bin/env python
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
31import json
32import optparse
33import base64
34import sys
35
36
José Fonseca98340702014-11-07 11:33:00 +000037pngSignature = "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"
38
39
José Fonseca97becb32013-05-25 13:29:25 +010040def dumpSurfaces(state, memberName):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010041 for name, imageObj in state[memberName].items():
José Fonseca97becb32013-05-25 13:29:25 +010042 data = imageObj['__data__']
43 data = base64.b64decode(data)
44
José Fonseca98340702014-11-07 11:33:00 +000045 if data.startswith(pngSignature):
46 extName = 'png'
47 else:
48 magic = data[:2]
49 if magic in ('P1', 'P4'):
50 extName = 'pbm'
51 elif magic in ('P2', 'P5'):
52 extName = 'pgm'
53 elif magic in ('P3', 'P6'):
54 extName = 'ppm'
55 elif magic in ('Pf', 'PF'):
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é Fonseca97becb32013-05-25 13:29:25 +010062 open(imageName, 'wb').write(data)
63 sys.stderr.write('Wrote %s\n' % imageName)
64
65
66def 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
80if __name__ == '__main__':
81 main()