# Unswizzle 24-bit color w/ Alpha

a guest
Apr 30th, 2017
184
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 = 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):
13. ##    print(segwidth, segheight)
14.     if segwidth == 2 and segheight == 2:
15.         if xy:
18.         else:
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)
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)
RAW Paste Data