Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import struct
- import os
- from functools import partial
- #0x2: Size of file (4 bytes)
- #0x6: Unused (4 bytes, all zero)
- #0xA: Offset to bitmap data (4 bytes)
- #0xE: DIB header bytes (always 40, 4 bytes)
- #0x12: Bitmap width (4 bytes)
- #0x16: Bitmap height (4 bytes)
- #0x1A: Color planes used (2 bytes)
- #0x1C: Bits per pixel (2 bytes)
- #0x1E: Always zero (4 bytes)
- #0x22: Size of raw bitmap data (4 bytes)
- #0x26: Print resolution horizontal (4 bytes)
- #0x2A: Print resolution vertical (4 bytes)
- #0x2E: Colors in pallette
- #0x32: Important colors (always zero, 4 bytes)
- filename = 'logo.ptx'
- with open(filename,'rb') as f:
- f.seek(4)
- unknown_1 = ord(f.read(1)) #0x04
- unknown_2 = ord(f.read(1)) #0x05
- width = struct.unpack('<H', f.read(2))[0] #0x06
- actual_width = struct.unpack('<H', f.read(2))[0] #0x08
- height = struct.unpack('<H', f.read(2))[0] #0x0A
- #Bits flag: 4 for 4bpp, 5 for 8bpp
- bits_flag = ord(f.read(1)) #0x0C
- #Unknown 3 and 4 are the same for every file
- unknown_3 = ord(f.read(1)) #0x0D
- unknown_4 = ord(f.read(1)) #0x0E
- #Colors flag: 0x2 for 16 colors, 0x4 for 32 colors,
- #0x8 for 64 colors, 0x10 for 128 colors, 0x20 for 256 colors
- colors_flag = ord(f.read(1)) #0x0F
- colors = struct.unpack('<I', f.read(4))[0] #0x10
- unknown_5 = ord(f.read(1)) #0x14
- unknown_6 = ord(f.read(1)) #0x15
- unknown_7 = struct.unpack('<H', f.read(2))[0] #0x16
- color_table_addr = struct.unpack('<I', f.read(4))[0]#0x18
- bitmap_addr = struct.unpack('<I', f.read(4))[0] #0x1C
- color_table = f.read(colors * 4) #32 bits per color
- bitmap = f.read()
- if bits_flag == 4:
- BITS_PER_PIXEL = 4
- elif bits_flag == 5:
- BITS_PER_PIXEL = 8
- else:
- print('Unknown number of bits per pixel.')
- quit()
- BMP_WIDTH = width
- BMP_HEIGHT = len(bitmap) // width * 8 // BITS_PER_PIXEL
- PALLETTE_COLORS = colors
- write_int = partial(struct.pack, '<I')
- write_hint = partial(struct.pack, '<H')
- with open(os.path.splitext(filename)[0] + '.bmp','wb') as f:
- f.write(b'BM') #0x0 Signature
- f.write(write_int(0x36 + colors * 4 + len(bitmap))) #0x2 Size of file
- f.write(b'\x00'*4) #0x6 Unused
- f.write(write_int(0x36 + 4 * colors)) #0xA Offset to bitmap data (4 bytes)
- f.write(write_int(40)) #0xE Size of DIB header
- f.write(write_int(BMP_WIDTH)) #0x12
- f.write(write_int(BMP_HEIGHT)) #0x16
- f.write(write_hint(1)) #0x1A Numer of color planes
- f.write(write_hint(BITS_PER_PIXEL)) #0x1C
- f.write(b'\x00'*4) #0x1E Always zero (no compression used)
- f.write(write_int(len(bitmap))) #0x22 Size of bitmap data (minus header)
- f.write(write_int(0xB13)*2) #0x26 72 DPI - Vert. + Horz. resolution
- f.write(write_int(PALLETTE_COLORS)) #0x2E
- f.write(b'\x00'*4) #0x32 Important colors
- #End of header, start of color table
- f.write(color_table)
- #Bitmap data
- f.write(bitmap)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement