GME  13
extract_icons.py
Go to the documentation of this file.
00001 import sys
00002 import struct
00003 
00004 
00005 #with open(r'C:\Users\ksmyth\git\GMESRC\GME\Gme\res\GME.ico', 'rb') as input:
00006 with open(sys.argv[1], 'rb') as input:
00007     input.read(4)
00008     n = struct.unpack('<h', input.read(2))[0]
00009     data = []
00010     for i in range(n):
00011         datum = {}
00012         datum['width'] = struct.unpack('<B', input.read(1))[0]
00013         datum['height'] = struct.unpack('<B', input.read(1))[0]
00014         datum['colors'] = struct.unpack('<B', input.read(1))[0]
00015         input.read(1)
00016         datum['planes'] = struct.unpack('<h', input.read(2))[0]
00017         datum['bpp'] = struct.unpack('<h', input.read(2))[0]
00018         datum['size'] = struct.unpack('<l', input.read(4))[0]
00019         datum['offset'] = struct.unpack('<l', input.read(4))[0]
00020         print datum['size'],datum['offset']
00021         print datum
00022         data.append(type('Data', (object,), datum))
00023 
00024     for datum in data:
00025         input.seek(datum.offset)
00026         imagedata = input.read(datum.size)
00027         info = str(datum.offset) + "_" + str(datum.width) + "x" + str(datum.height) + "_" + str(datum.bpp) + "bpp"
00028 #        with open('output_' + info, 'wb') as output:
00029 #            output.write(imagedata)
00030         if imagedata[1:4] == 'PNG':
00031             with open('output_' + info + ".png", 'wb') as output:
00032                 output.write(imagedata)
00033         else:
00034             with open('output_' + info + ".bmp", 'wb') as output:
00035 #typedef struct tagBITMAPFILEHEADER {
00036 #  WORD bfType; 
00037                 output.write('BM')
00038 #  DWORD bfSize; 
00039                 output.write(struct.pack('<L', len(imagedata) + 14))
00040 #  WORD bfReserved1; 
00041                 output.write(struct.pack('<H', 0))
00042 #  WORD bfReserved2; 
00043                 output.write(struct.pack('<H', 0))
00044 #  DWORD bfOffBits; 
00045 #} BITMAPFILEHEADER; 
00046                 #output.write(struct.pack('<L', 140))
00047                 output.write(struct.pack('<L', 54))
00048 #  LONG  biHeight;
00049 #} BITMAPINFOHEADER, *PBITMAPINFOHEADER;
00050                 output.write(imagedata)
00051                 # AND/XOR transparency business (this kills the alpha mask)
00052                 output.seek(0x16) # sizeof(BITMAPFILEHEADER) + offsetof(BITMAPINFOHEADER, biHeight)
00053                 output.write(struct.pack('<L', datum.height))
00054 
00055             with open('output_' + info + "_mask.bmp", 'wb') as output:
00056 #typedef struct tagBITMAPFILEHEADER {
00057 #  WORD bfType; 
00058                 output.write('BM')
00059 #  DWORD bfSize; 
00060                 output.write(struct.pack('<L', 14 + 40 + 8 + datum.width*datum.height/8 + datum.height*2))
00061 #  WORD bfReserved1; 
00062                 output.write(struct.pack('<H', 0))
00063 #  WORD bfReserved2; 
00064                 output.write(struct.pack('<H', 0))
00065 #  DWORD bfOffBits; 
00066 #} BITMAPFILEHEADER; 
00067                 #output.write(struct.pack('<L', 140))
00068                 output.write(struct.pack('<L', 54))
00069                 #output.write(struct.pack('<L', 62))
00070 #  LONG  biHeight;
00071 #} BITMAPINFOHEADER, *PBITMAPINFOHEADER;
00072 #typedef struct tagBITMAPINFOHEADER {
00073 #  DWORD biSize;
00074                 output.write(struct.pack('<L', 40))
00075 #  LONG  biWidth;
00076                 output.write(struct.pack('<L', datum.width))
00077 #  LONG  biHeight;
00078                 output.write(struct.pack('<L', datum.height))
00079 #  WORD  biPlanes;
00080                 output.write(struct.pack('<H', 1))
00081 #  WORD  biBitCount;
00082                 output.write(struct.pack('<H', 1))
00083 #  DWORD biCompression;
00084                 output.write(struct.pack('<L', 0))
00085 #  DWORD biSizeImage;
00086                 output.write(struct.pack('<L', 0))
00087 #  LONG  biXPelsPerMeter;
00088                 output.write(struct.pack('<L', 0))
00089 #  LONG  biYPelsPerMeter;
00090                 output.write(struct.pack('<L', 0))
00091 #  DWORD biClrUsed;
00092                 output.write(struct.pack('<L', 2))
00093 #  DWORD biClrImportant;
00094                 output.write(struct.pack('<L', 2))
00095 #} BITMAPINFOHEADER, *PBITMAPINFOHEADER;
00096                 output.write(struct.pack('<L', 0x00000000))
00097                 output.write(struct.pack('<L', 0x00FFFFFF))
00098                 #output.write(imagedata[-(datum.width*datum.height/8):])
00099                 #output.write(imagedata[-(datum.width*datum.height/8):])
00100                 for i in range(datum.height, 0, -1):
00101                     pad = 0
00102                     #  The bits in the array are packed together, but each scan line must be padded with zeros to end on a LONG data-type boundary
00103                     if datum.width % 32 != 0: pad = 16
00104                     start, end = -((datum.width+pad)*i/8), -((datum.width+pad)*(i-1)/8)
00105                     if end == 0: end = len(imagedata)
00106                     print "%d %d %d" % (datum.width, start, end)
00107                     output.write(imagedata[start:end])
00108                     #if datum.width % 32 != 0:
00109                     #    output.write(struct.pack('H', 0))
00110                 # AND/XOR transparency business (this kills the alpha mask)
00111                 #output.seek(0x16) # sizeof(BITMAPFILEHEADER) + offsetof(BITMAPINFOHEADER, biHeight)
00112                 #output.write(struct.pack('<L', datum.height))
00113 
00114 
00115