#!/usr/bin/python
import sys, os, struct

name = "tglogo"

f = open('%s.bmp' % (name))
out = sys.stdout

def read_data(fmt):
 size = struct.calcsize(fmt)
 data = f.read(size)
 return struct.unpack(fmt, data)

(sig, fsize, res1, res2, offset) = read_data("=HLHHL")
(hsize, w, h, planes, bpp, comp, imgsize) = read_data("=LllHHLL")
out.write("/* source image size: %dx%dx%d */\n" % (w,h,bpp))
pitch = ((w*bpp+31)/32)*4

# get a power-of-two size
aw = 1
while aw < w: aw *= 2
ah = 1
while ah < h: ah *= 2

out.write("/* stored image size: %dx%dx%d */\n" % (aw,ah,32))

out.write("const int %s_imgwidth = %d;\n" % (name, aw))
out.write("const int %s_imgheight = %d;\n" % (name, ah))
out.write("const int %s_width = %d;\n" % (name, w))
out.write("const int %s_height = %d;\n" % (name, h))
out.write("const float %s_x0 = %f;\n" % (name, 0))
out.write("const float %s_y0 = %f;\n" % (name, float(h)/ah))
out.write("const float %s_x1 = %f;\n" % (name, float(w)/aw))
out.write("const float %s_y1 = %f;\n" % (name, 0))

f.seek(offset)

out.write("const unsigned char %s_data[%d]={\n" % (name, aw*ah*4))
sep = " "

for y in xrange(0,h):
 data = f.read(pitch)
 for x in xrange(0,w):
  # assumes source=32bpp for now
  pixel = map(ord, data[x*4:x*4+4])
  out.write(sep + "0x%02x,0x%02x,0x%02x,0x%02x" %
            (pixel[0], pixel[1], pixel[2], 0xff-pixel[3]))
  sep = ", "
 for x in xrange(w,aw):
  out.write(sep + "0,0,0,0")
  sep = ", "
 sep = ",\n "

# the C compiler will pad out any missing Y data

out.write("\n};\n")
