SHARE
TWEET

Test Image Bitmap Unswizzle and Output

a guest Apr 9th, 2017 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import png, struct, os, math, pdb
  2. import sys
  3.  
  4. xy = False  #Set to false for yx swizzle order
  5.  
  6. def chunks(l, n):
  7.     """Yield successive n-sized chunks from l."""
  8.     for i in range(0, len(l), n):
  9.         yield l[i:i + n]
  10.  
  11. def unswizzle(writebuf, readbuf, writeoffset, segwidth, segheight, datawidth):
  12.     global readoffset, buffer
  13. ##    print(segwidth, segheight)
  14.     if segwidth == 2 and segheight == 2:
  15.         if xy:
  16.             writebuf[writeoffset:writeoffset+2] = readbuf[readoffset:readoffset+2]
  17.             writebuf[writeoffset + datawidth:writeoffset + datawidth + 2] = readbuf[readoffset + 2:readoffset + 4]
  18.         else:
  19.             writebuf[writeoffset] = readbuf[readoffset]
  20.             writebuf[writeoffset + datawidth] = readbuf[readoffset + 1]
  21.             writebuf[writeoffset + 1] = readbuf[readoffset + 2]
  22.             writebuf[writeoffset + datawidth + 1] = readbuf[readoffset + 3]
  23.         readoffset += 4
  24.     else:
  25.         if xy:
  26.             unswizzle(writebuf, readbuf, writeoffset, segwidth // 2, segheight // 2, datawidth)
  27.             unswizzle(writebuf, readbuf, writeoffset + segwidth // 2, segwidth // 2, segheight // 2, datawidth)
  28.             unswizzle(writebuf, readbuf, writeoffset + datawidth * (segheight // 2), segwidth // 2, segheight // 2, datawidth)
  29.             unswizzle(writebuf, readbuf, writeoffset + datawidth * (segheight // 2) + segwidth // 2, segwidth // 2, segheight // 2, datawidth)
  30.         else:
  31.             unswizzle(writebuf, readbuf, writeoffset, segwidth // 2, segheight // 2, datawidth)
  32.             unswizzle(writebuf, readbuf, writeoffset + datawidth * (segheight // 2), segwidth // 2, segheight // 2, datawidth)
  33.             unswizzle(writebuf, readbuf, writeoffset + segwidth // 2, segwidth // 2, segheight // 2, datawidth)
  34.             unswizzle(writebuf, readbuf, writeoffset + datawidth * (segheight // 2) + segwidth // 2, segwidth // 2, segheight // 2, datawidth)
  35.  
  36. with open('1ch6030.ykcmp.dec', 'rb') as f:
  37.     palette = f.read(0x400)
  38.     bitmap = f.read(0x40000)
  39. width = 512
  40. height = 512
  41. buffer = bytearray(len(bitmap))         #Output buffer
  42. readoffset = 0                          #Input position counter
  43. if width == height:
  44.     unswizzle(buffer, bitmap, 0, width, height, width)
  45. elif width > height:
  46.     for w in range(0, width, height):
  47.         unswizzle(buffer, bitmap, w, height, height, width)
  48. elif height > width:
  49.     for h in range(0, height * width, width ** 2):
  50.         unswizzle(buffer, bitmap, h, width, width, width)
  51. else:
  52.     print('error')
  53. p = list(chunks(buffer, width))
  54. with open('output.png', 'wb') as f:
  55.     w = png.Writer(len(p[0]), len(p), palette=list(chunks(palette, 4)))
  56.     w.write(f, p)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top