Advertisement
Guest User

Unswizzle 24-bit color w/ Alpha

a guest
Apr 30th, 2017
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. import png, struct, os, math, pdb
  2. import sys
  3.  
  4. xy = True  #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+8] = readbuf[readoffset:readoffset+8]
  17.             writebuf[writeoffset + datawidth:writeoffset + datawidth + 8] = readbuf[readoffset + 8:readoffset + 16]
  18.         else:
  19.             writebuf[writeoffset:writeoffset+4] = readbuf[readoffset:readoffset+4]
  20.             writebuf[writeoffset + datawidth:writeoffset + datawidth+4] = readbuf[readoffset+4:readoffset+8]
  21.             writebuf[writeoffset+4:writeoffset+8] = readbuf[readoffset+8:readoffset+12]
  22.             writebuf[writeoffset + datawidth + 4:writeoffset + datawidth + 8] = readbuf[readoffset+12:readoffset+16]
  23.         readoffset += 16
  24.     else:
  25.         if xy:
  26.             unswizzle(writebuf, readbuf, writeoffset, segwidth // 2, segheight // 2, datawidth)
  27.             unswizzle(writebuf, readbuf, writeoffset + segwidth * 4 // 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 * 4 // 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 * 4 // 2, segwidth // 2, segheight // 2, datawidth)
  34.             unswizzle(writebuf, readbuf, writeoffset + datawidth * (segheight // 2) + segwidth * 4 // 2, segwidth // 2, segheight // 2, datawidth)
  35.  
  36. with open('ch01.png.phyre', 'rb') as f:
  37.     f.seek(0xF8F)
  38.     bitmap = f.read()
  39. width = 1024
  40. height = 1024
  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 * 4)
  45. elif width > height:
  46.     for w in range(0, width, height):
  47.         unswizzle(buffer, bitmap, w, height, height, width * 4)
  48. elif height > width:
  49.     for h in range(0, height * width, width ** 2):
  50.         unswizzle(buffer, bitmap, h, width, width, width * 4)
  51. else:
  52.     print('error')
  53. p = list(chunks(buffer, width * 4))
  54. with open('output1.png', 'wb') as f:
  55.     w = png.Writer(width, height, alpha=True)
  56.     w.write(f, p)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement