From 5eb8bffe42f04cfe2d7570b3f24eaf5dc48873d5 Mon Sep 17 00:00:00 2001 From: halcanary Date: Tue, 23 Aug 2016 10:32:53 -0700 Subject: [PATCH] experimental/tools/mskp_parser.py TBR= GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2268403003 Review-Url: https://codereview.chromium.org/2268403003 --- experimental/tools/mskp_parser.py | 57 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100755 experimental/tools/mskp_parser.py diff --git a/experimental/tools/mskp_parser.py b/experimental/tools/mskp_parser.py new file mode 100755 index 0000000..051e579 --- /dev/null +++ b/experimental/tools/mskp_parser.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python + +# Copyright 2016 Google Inc. +# +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +# Experimental Skia Multi-Picture Doc parser. + +from __future__ import print_function + +import fileinput +import sys +import struct + +if len(sys.argv) < 2: + sys.stderr.write('Usage:\n\tpython %s MSKP_FILE [OUTPUT_SKP]\n\n' + % sys.argv[0]) + exit(1) + +mskp_src = sys.argv[1] +src = open(mskp_src, 'rb') + +magic_constant = b'Skia Multi-Picture Doc\n\n' +magic = src.read(len(magic_constant)) +if magic != magic_constant: + sys.stderr.write('Not a mskp file: "%s"\n' % mskp_src) + exit(2) + +version, page_count = struct.unpack('II', src.read(8))[:2] +print('MSKP version: ', version) +print('page count: ', page_count) +if version > 2 or version < 1: + #TODO(halcanary): Remove support for version 1. + sys.stderr.write('unsupported mskp version\n') + exit(3) +offsets = [] +for page in range(page_count): + print('page %3d\t' % page, end='') + if version == 1: + offset, size_x, size_y =struct.unpack('Qff', src.read(16)) + print('offset = %-7d\t' % offset, end='') + offsets.append(offset) + elif version == 2: + size_x, size_y =struct.unpack('ff', src.read(8)) + print('size = (%r,%r)' % (size_x, size_y)) + +if len(sys.argv) >= 3: + with open(sys.argv[2], 'wb') as o: + if version == 2 or len(offsets) < 2: + while True: + file_buffer = src.read(8192) + if 0 == len(file_buffer): + break + o.write(file_buffer) + else: + o.write(src.read(offsets[1] - offsets[0])) -- 2.7.4