Advertisement
Guest User

PTX to BMP (development version)

a guest
Dec 10th, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 KB | None | 0 0
  1. import struct
  2. import os
  3. from functools import partial
  4. #0x2: Size of file (4 bytes)
  5. #0x6: Unused (4 bytes, all zero)
  6. #0xA: Offset to bitmap data (4 bytes)
  7. #0xE: DIB header bytes (always 40, 4 bytes)
  8. #0x12: Bitmap width (4 bytes)
  9. #0x16: Bitmap height (4 bytes)
  10. #0x1A: Color planes used (2 bytes)
  11. #0x1C: Bits per pixel (2 bytes)
  12. #0x1E: Always zero (4 bytes)
  13. #0x22: Size of raw bitmap data (4 bytes)
  14. #0x26: Print resolution horizontal (4 bytes)
  15. #0x2A: Print resolution vertical (4 bytes)
  16. #0x2E: Colors in pallette
  17. #0x32: Important colors (always zero, 4 bytes)
  18.  
  19. filename = 'logo.ptx'
  20. with open(filename,'rb') as f:
  21.     f.seek(4)
  22.     unknown_1 = ord(f.read(1))                          #0x04
  23.     unknown_2 = ord(f.read(1))                          #0x05
  24.     width = struct.unpack('<H', f.read(2))[0]           #0x06
  25.     actual_width = struct.unpack('<H', f.read(2))[0]    #0x08
  26.     height = struct.unpack('<H', f.read(2))[0]          #0x0A
  27.     #Bits flag: 4 for 4bpp, 5 for 8bpp
  28.     bits_flag = ord(f.read(1))                          #0x0C
  29.     #Unknown 3 and 4 are the same for every file
  30.     unknown_3 = ord(f.read(1))                          #0x0D
  31.     unknown_4 = ord(f.read(1))                          #0x0E
  32.     #Colors flag: 0x2 for 16 colors, 0x4 for 32 colors,
  33.     #0x8 for 64 colors, 0x10 for 128 colors, 0x20 for 256 colors
  34.     colors_flag = ord(f.read(1))                        #0x0F
  35.     colors = struct.unpack('<I', f.read(4))[0]          #0x10
  36.     unknown_5 = ord(f.read(1))                          #0x14
  37.     unknown_6 = ord(f.read(1))                          #0x15
  38.     unknown_7 = struct.unpack('<H', f.read(2))[0]       #0x16
  39.     color_table_addr = struct.unpack('<I', f.read(4))[0]#0x18
  40.     bitmap_addr = struct.unpack('<I', f.read(4))[0]     #0x1C
  41.     color_table = f.read(colors * 4)                    #32 bits per color
  42.     bitmap = f.read()
  43.  
  44. if bits_flag == 4:
  45.     BITS_PER_PIXEL = 4
  46. elif bits_flag == 5:
  47.     BITS_PER_PIXEL = 8
  48. else:
  49.     print('Unknown number of bits per pixel.')
  50.     quit()
  51. BMP_WIDTH = width
  52. BMP_HEIGHT = len(bitmap) // width * 8 // BITS_PER_PIXEL
  53. PALLETTE_COLORS = colors
  54. write_int = partial(struct.pack, '<I')
  55. write_hint = partial(struct.pack, '<H')
  56. with open(os.path.splitext(filename)[0] + '.bmp','wb') as f:
  57.     f.write(b'BM')                  #0x0 Signature
  58.     f.write(write_int(0x36 + colors * 4 + len(bitmap))) #0x2 Size of file
  59.     f.write(b'\x00'*4)              #0x6 Unused
  60.     f.write(write_int(0x36 + 4 * colors))   #0xA Offset to bitmap data (4 bytes)
  61.     f.write(write_int(40))          #0xE Size of DIB header
  62.     f.write(write_int(BMP_WIDTH))   #0x12
  63.     f.write(write_int(BMP_HEIGHT))  #0x16
  64.     f.write(write_hint(1))          #0x1A Numer of color planes
  65.     f.write(write_hint(BITS_PER_PIXEL))     #0x1C
  66.     f.write(b'\x00'*4)              #0x1E Always zero (no compression used)
  67.     f.write(write_int(len(bitmap))) #0x22 Size of bitmap data (minus header)
  68.     f.write(write_int(0xB13)*2)     #0x26 72 DPI - Vert. + Horz. resolution
  69.     f.write(write_int(PALLETTE_COLORS))     #0x2E
  70.     f.write(b'\x00'*4)              #0x32 Important colors
  71.     #End of header, start of color table
  72.     f.write(color_table)
  73.     #Bitmap data
  74.     f.write(bitmap)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement