﻿

Test Image Bitmap Unswizzle and Output

a guest
Apr 9th, 2017
104
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):
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 // 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:
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