• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2
3'''
4Copyright 2015 Google Inc.
5
6Use of this source code is governed by a BSD-style license that can be
7found in the LICENSE file.
8'''
9
10import argparse
11
12
13def bytes_from_file(f, chunksize=8192):
14  while True:
15    chunk = f.read(chunksize)
16    if chunk:
17      for b in chunk:
18        if isinstance(b, str):
19          # python 2
20          yield ord(b)
21        else:
22          # python 3
23          yield b
24    else:
25      break
26
27
28def main():
29  parser = argparse.ArgumentParser(
30      formatter_class=argparse.RawDescriptionHelpFormatter,
31      description='Convert resource files to embedded read only data.',
32      epilog='''The output (when compiled and linked) can be used as:
33struct SkEmbeddedResource {const uint8_t* data; const size_t size;};
34struct SkEmbeddedHeader {const SkEmbeddedResource* entries; const int count;};
35extern "C" SkEmbeddedHeader const NAME;''')
36  parser.add_argument('--align', default=1, type=int,
37                      help='minimum alignment (in bytes) of resource data')
38  parser.add_argument('--name', default='_resource', type=str,
39                      help='the name of the c identifier to export')
40  parser.add_argument('--input', required=True, type=argparse.FileType('rb'),
41                      nargs='+', help='list of resource files to embed')
42  parser.add_argument('--output', required=True, type=argparse.FileType('w'),
43                      help='the name of the cpp file to output')
44  args = parser.parse_args()
45
46  out = args.output.write;
47  out('#include <stddef.h>\n')
48  out('#include <stdint.h>\n')
49
50  # Write the resources.
51  index = 0
52  for f in args.input:
53    out('alignas({1:d}) static const uint8_t resource{0:d}[] = {{\n'
54        .format(index, args.align))
55    bytes_written = 0
56    bytes_on_line = 0
57    for b in bytes_from_file(f):
58      out(hex(b) + ',')
59      bytes_written += 1
60      bytes_on_line += 1
61      if bytes_on_line >= 32:
62        out('\n')
63        bytes_on_line = 0
64    out('};\n')
65    out('static const size_t resource{0:d}_size = {1:d};\n'
66        .format(index, bytes_written))
67    index += 1
68
69  # Write the resource entries.
70  out('struct SkEmbeddedResource { const uint8_t* d; const size_t s; };\n')
71  out('static const SkEmbeddedResource header[] = {\n')
72  index = 0
73  for f in args.input:
74    out('  {{ resource{0:d}, resource{0:d}_size }},\n'.format(index))
75    index += 1
76  out('};\n')
77  out('static const int header_count = {0:d};\n'.format(index))
78
79  # Export the resource header.
80  out('struct SkEmbeddedHeader {const SkEmbeddedResource* e; const int c;};\n')
81  out('extern "C" const SkEmbeddedHeader {0:s} = {{ header, header_count }};\n'
82      .format(args.name))
83
84
85if __name__ == "__main__":
86  main()
87