Advertisement
Guest User

pspfont.dat convert back

a guest
Jan 1st, 2017
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.02 KB | None | 0 0
  1. import struct
  2. import png
  3.  
  4. def unpack_bmp_pixel(b):
  5.     p1 = b & 0xF
  6.     p2 = b >> 4
  7.     return [p2, p1]
  8.  
  9. def pack_pspfont_pixels(a, b, c, d):
  10.     return (d << 6) + (c << 4) + (b << 2) + a
  11.  
  12. def chunks(l, n):
  13.     """Yield successive n-sized chunks from l."""
  14.     for i in range(0, len(l), n):
  15.         yield l[i:i + n]
  16.  
  17. WIDTH = 0x24DB8
  18. REAL_WIDTH = 0x24DB1
  19. width_bytes = WIDTH // 2
  20. #Real width = 0x24DB1
  21. HEIGHT = 0x10
  22. with open('pspfont.bmp', 'rb') as f:
  23.     f.seek(0x8A)
  24.     filedata = f.read()
  25. bitmap = bytearray(len(filedata) * 2)
  26. for row_bmp, row_png in zip(range(HEIGHT), reversed(range(HEIGHT))):
  27.     row_bmp_data = filedata[row_bmp * width_bytes:(row_bmp + 1) * width_bytes]
  28.     l = []
  29.     for b in row_bmp_data:
  30.         l += unpack_bmp_pixel(b)
  31.     bitmap[row_png * WIDTH:(row_png + 1) * WIDTH] = l
  32. bitmap_new = bytearray(REAL_WIDTH * HEIGHT)
  33. for row in range(HEIGHT):
  34.     bitmap_new[row * REAL_WIDTH:(row + 1)* REAL_WIDTH] = \
  35.         bitmap[row * WIDTH:row * WIDTH + REAL_WIDTH]
  36.  
  37. ##writer = png.Writer(width = REAL_WIDTH, height = HEIGHT, greyscale = True,
  38. ##                    bitdepth = 2)
  39. ##with open('test.png', 'wb') as f:
  40. ##    writer.write(f, chunks(bitmap_new, REAL_WIDTH))
  41. pspfont_data = bytearray()
  42. for col in range(REAL_WIDTH):
  43.     l = []
  44.     for row in range(HEIGHT):
  45.         l.append(bitmap_new[col + REAL_WIDTH * row])
  46.     for a, b, c, d in chunks(l, 4):
  47.         pspfont_data.append(pack_pspfont_pixels(a, b, c, d))
  48.        
  49. with open('pspfont.orig', 'rb') as f:
  50.     filedata = bytearray(f.read(0xEEE8))
  51. header_update_data = (
  52.     (15193, 150616, 6),
  53.     (15194, 150624, 6),
  54.     (15195, 150632, 6),
  55.     (15196, 150640, 6),
  56.     (15197, 150648, 5),
  57.     (15198, 150656, 6),
  58.     (15199, 150664, 6),
  59.     (15200, 150672, 6),
  60.     (15201, 150680, 6))
  61. for index, x_pos, width in header_update_data:
  62.     num = struct.pack('<I', (width << 24) + x_pos)
  63.     filedata[0xC4 + 4 * index:0xC4 + 4 * index + 4] = num
  64. with open('pspfont.dat', 'wb') as f:
  65.     f.write(filedata)
  66.     f.write(pspfont_data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement