1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tools/bitmap.py Sun Nov 24 19:01:08 2013 +0000
1.3 @@ -0,0 +1,89 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Produce program files for image data converted to 16bpp values suitable for
1.8 +writing directly to the display.
1.9 +
1.10 +Copyright (C) 2013 Paul Boddie
1.11 +
1.12 +This program is free software; you can redistribute it and/or modify
1.13 +it under the terms of the GNU General Public License as published by
1.14 +the Free Software Foundation; either version 2 of the License, or
1.15 +(at your option) any later version.
1.16 +"""
1.17 +
1.18 +import PIL.Image
1.19 +import math
1.20 +import sys
1.21 +
1.22 +def get_image(input_filename, width, height):
1.23 +
1.24 + i = PIL.Image.open(input_filename)
1.25 + i.thumbnail((width, height), PIL.Image.ANTIALIAS)
1.26 +
1.27 + all_values = []
1.28 + values = []
1.29 + last = None
1.30 + x = 0
1.31 + for (r, g, b) in i.getdata():
1.32 + rgb = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3)
1.33 + values.append(rgb)
1.34 + x += 1
1.35 +
1.36 + if x == width:
1.37 + x = 0
1.38 + all_values.append(values)
1.39 + values = []
1.40 +
1.41 + return all_values
1.42 +
1.43 +def get_image_size(width, height):
1.44 + return width * height
1.45 +
1.46 +if __name__ == "__main__":
1.47 + import sys, os
1.48 +
1.49 + if len(sys.argv) < 3:
1.50 + print sys.argv[0], "<output basename> <image filename>..."
1.51 + sys.exit(1)
1.52 +
1.53 + output_basename = sys.argv[1]
1.54 + input_filenames = sys.argv[2:]
1.55 + width = 132
1.56 + height = 132
1.57 + size = get_image_size(width, height)
1.58 + number_of_images = len(input_filenames)
1.59 +
1.60 + f = open("%s.c" % output_basename, "wb")
1.61 +
1.62 + try:
1.63 + # Write the image data.
1.64 +
1.65 + f.write("#include <stdint.h>\n")
1.66 + f.write("const uint16_t image_data[%d][%d] = {\n" % (number_of_images, size))
1.67 + for input_filename in input_filenames:
1.68 + f.write("{\n")
1.69 + for values in get_image(input_filename, width, height):
1.70 + f.write(", ".join([("0x%04x" % v) for v in values]))
1.71 + f.write(",\n")
1.72 + f.write("},\n")
1.73 + f.write("};\n")
1.74 +
1.75 + finally:
1.76 + f.close()
1.77 +
1.78 + f = open("%s.h" % output_basename, "wb")
1.79 +
1.80 + try:
1.81 + f.write("#include <stdint.h>\n")
1.82 + f.write("extern const uint16_t image_data[%d][%d];\n" % (number_of_images, size))
1.83 + f.write("#define MAX_IMAGE %d\n" % (number_of_images - 1))
1.84 + f.write("#define NUMBER_OF_IMAGES %d\n" % number_of_images)
1.85 + f.write("#define IMAGE_SIZE %d\n" % size)
1.86 + f.write("#define IMAGE_WIDTH %d\n" % width)
1.87 + f.write("#define IMAGE_HEIGHT %d\n" % height)
1.88 +
1.89 + finally:
1.90 + f.close()
1.91 +
1.92 +# vim: tabstop=4 expandtab shiftwidth=4