GME  13
make_ico.py
Go to the documentation of this file.
00001 # python make_ico.py  GME_icon.png  output_10198  output_17534  output_20774
00002 
00003 import struct
00004 import os
00005 import sys
00006 
00007 class Out(object):
00008     def __init__(self, output):
00009         self.output = output
00010         self.count = 0
00011     
00012     def write1(self, v):
00013         output.write(struct.pack('<B', v))
00014 
00015     def write2(self, v):
00016         output.write(struct.pack('<H', v))
00017 
00018     def write4(self, v):
00019         output.write(struct.pack('<L', v))
00020 
00021     def write(self, v):
00022         output.write(v)
00023 
00024 #sizes = ( (0,0), (48,48), (32,32), (16,16) )
00025 sizes = ( (48,48), (32,32), (16,16) )
00026 palettesize = ( 0, 0, 0, 0 )
00027 bpp = ( 32, 32, 32, 24)
00028 
00029 def roundup(x):
00030     import math
00031     return int(math.ceil(x / 32.0) * 32)
00032 
00033 files = []
00034 for (i, f) in enumerate(sys.argv[1:]):
00035     with open(f, 'rb') as datafile:
00036         data = datafile.read()
00037         if data[0:2] == 'BM': # magic number for BMP
00038             data = data[14:] # sizeof(BITMAPFILEHEADER)
00039             # TODO: read mask bmp
00040             # TODO: fix size
00041             data += struct.pack('b', 0) * roundup(sizes[i][0]) * sizes[i][1]
00042             data = list(data)
00043             data[8:12] = struct.pack('<L', struct.unpack('<L', ''.join(data[8:12]))[0] * 2) # offsetof(BITMAPINFOHEADER, biHeight) == 8
00044             print struct.unpack('<L', ''.join(data[8:12]))
00045             data = ''.join(data)
00046         files.append(data)
00047 
00048 with open('output.ico', 'wb') as output:
00049     out = Out(output)
00050     out.write2(0)
00051     out.write2(1)
00052     out.write2(len(files))
00053     for i in range(len(files)):
00054         out.write1(sizes[i][0])
00055         out.write1(sizes[i][1])
00056         print 'height ' + str(sizes[i][1])
00057         out.write1(palettesize[i])
00058         out.write1(0)
00059         out.write2(1) # FIXME: what is color plane?
00060         out.write2(bpp[i])
00061         out.write4(len(files[i]))
00062         print(len(files[i]))
00063         print sum((len(file) for file in files[:i]))
00064         out.write4(6 + 16 * len(files) + sum((len(file) for file in files[:i])))
00065         
00066     for file in files:
00067         out.write(file)
00068 
00069