blob: ac20c23d70272e07eab835b5945f9cfa5360f632 [file] [log] [blame]
Jose Fonseca247e1fa2019-04-28 14:14:44 +01001#!/usr/bin/env python3
José Fonseca97becb32013-05-25 13:29:25 +01002##########################################################################
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
Jose Fonsecaf5279242019-04-29 06:41:49 +010037pngSignature = b"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"
José Fonseca98340702014-11-07 11:33:00 +000038
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]
Jose Fonsecaf5279242019-04-29 06:41:49 +010049 if magic in (b'P1', b'P4'):
José Fonseca98340702014-11-07 11:33:00 +000050 extName = 'pbm'
Jose Fonsecaf5279242019-04-29 06:41:49 +010051 elif magic in (b'P2', b'P5'):
José Fonseca98340702014-11-07 11:33:00 +000052 extName = 'pgm'
Jose Fonsecaf5279242019-04-29 06:41:49 +010053 elif magic in (b'P3', b'P6'):
José Fonseca98340702014-11-07 11:33:00 +000054 extName = 'ppm'
Jose Fonsecaf5279242019-04-29 06:41:49 +010055 elif magic in (b'Pf', b'PF'):
José Fonseca98340702014-11-07 11:33:00 +000056 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()