From dcfb2161ad0fc8ff71ca32ed86e9d5a59dc5a427 Mon Sep 17 00:00:00 2001 From: Skylark13 Date: Mon, 12 Aug 2019 11:40:11 -0400 Subject: [PATCH] - Support images with 8, 24 or 32 bits per pixel. - Format output so the entire image is not all on one line. --- img2c/img2c.py | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/img2c/img2c.py b/img2c/img2c.py index 1115faa..f58f7c7 100755 --- a/img2c/img2c.py +++ b/img2c/img2c.py @@ -12,22 +12,47 @@ im = Image.open(sys.argv[1]) pix = im.load() + mode_to_bpp = {'L':8, 'RGB':24, 'RGBA':32} + bpp = mode_to_bpp[im.mode] + filename = os.path.splitext(sys.argv[1])[0] f = open(filename + ".h", "w+") f.write("#include \n\n"); - f.write("static const uint32_t %s_WIDTH=%d;\n" % (filename.upper(), im.size[0])) - f.write("static const uint32_t %s_HEIGHT=%d;\n" % (filename.upper(), im.size[1])) - f.write("static const uint8_t %s_DATA[]={" % (filename.upper())) + f.write("static const uint32_t %s_WIDTH = %d;\n" % (filename.upper(), im.size[0])) + f.write("static const uint32_t %s_HEIGHT = %d;\n" % (filename.upper(), im.size[1])) + f.write("static const uint32_t %s_BPP = %d;\n" % (filename.upper(), bpp)) + f.write("static const uint8_t %s_DATA[] = \n{\n" % (filename.upper())) + num = 0 for y in xrange(0, im.size[1]): for x in xrange(0, im.size[0]): - r, g, b, a = pix[x, y] - f.write("%s, " % hex(r)) - f.write("%s, " % hex(g)) - f.write("%s, " % hex(b)) - f.write("%s, " % hex(a)) + if num == 0: + f.write("\t"); + if bpp == 8: + r = pix[x, y] + f.write("%s, " % hex(r)) + num = num + 1 + elif bpp == 24: + r, g, b = pix[x, y] + f.write("%s, " % hex(r)) + f.write("%s, " % hex(g)) + f.write("%s, " % hex(b)) + num = num + 3 + elif bpp == 32: + r, g, b, a = pix[x, y] + f.write("%s, " % hex(r)) + f.write("%s, " % hex(g)) + f.write("%s, " % hex(b)) + f.write("%s, " % hex(a)) + num = num + 4 + if num >= 16: + f.write("\n"); + num = 0 + + if num == 0: + f.write("};\n") + else: + f.write("\n};\n") - f.write("};\n") f.close() -