-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinPNG.py
257 lines (237 loc) · 7.4 KB
/
BinPNG.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import struct
import png
import math
from bitstring import *
from PIL import Image #for skyboxes
import zlib
from functools import lru_cache
#convert bin to png
def InitSkybox(name):
return Image.new("RGB",(248,248),None)
def TileSkybox(FullBox,x,y,tilepath):
t = Image.open(tilepath)
FullBox.paste(t,(x,y))
def MakeImage(name):
return open(name+'.png','wb')
def GetCHKSM(filename):
f = open(filename,'rb')
c = zlib.crc32(f.read())
f.close()
return c
#file is bin, image is png
#Alpha changed to true because N64 graphics does not like PNGS with no alpha YES!!!
def I(width,height,depth,file,image):
if depth>4:
w = png.Writer(width,height,greyscale=True,bitdepth=depth,alpha=True)
rows = CreateIRows(width,height,depth//2,2,file)
else:
w = png.Writer(width,height,greyscale=True,bitdepth=8,alpha=True)
rows = EditIFile(file,width,height,[4,4],2,1)
w.write(image,rows)
def IA(width,height,depth,file,image):
if depth>4:
w = png.Writer(width,height,greyscale=True,bitdepth=depth//2,alpha=True)
rows = CreateRows(width,height,depth//2,2,file)
else:
w = png.Writer(width,height,greyscale=True,bitdepth=8,alpha=True)
rows = EditFile(file,width,height,[5,8,5,8],2,1)
w.write(image,rows)
def RGBA(width,height,depth,file,image):
if depth==16:
RGBA16(width,height,file,image)
else:
RGBA32(width,height,file,image)
def RGBA32(width,height,file,image):
w = png.Writer(width,height,greyscale=False,bitdepth=8,alpha=True)
rows = CreateRows(width,height,8,4,file)
w.write(image,rows)
def RGBA16(width,height,file,image):
w = png.Writer(width,height,greyscale=False,bitdepth=8,alpha=True)
rows = EditFile(file,width,height,[3,3,3,8],1,2)
w.write(image,rows)
def CI(width,height,depth,p,file,image):
p = GetPalette(p,depth,2)
w = png.Writer(width,height,palette=p,bitdepth=depth)
rows = CreateRows(width,height,depth,1,file)
w.write(image,rows)
def CreateIRows(width,height,depth,Channels,file):
rows=[]
for r in range(height):
L=int((depth/8)*Channels*width)#bytes per row
bin = BitArray(file[L*r:L*r+L])
a=bin.unpack('%d*uint:%d'%(width*Channels,depth))
a = [b&0xFF for b in a]
AlphaAdd = [0xFF]*(len(a)*2)
AlphaAdd[0::2] = a
rows.append(AlphaAdd)
return rows
def CreateRows(width,height,depth,Channels,file):
rows=[]
for r in range(height):
L=int((depth/8)*Channels*width)#bytes per row
bin = BitArray(file[L*r:L*r+L])
a=bin.unpack('%d*uint:%d'%(width*Channels,depth))
a = [b&0xFF for b in a]
rows.append(a)
return rows
#change I4 to IA88
def EditIFile(file,width,height,shifts,PpB,b):
newfile=[]
for x in range(height):
row=[]
for pixel in range(0,width//PpB):
pixel=pixel+x*width//PpB
bin=file[b*pixel:b*pixel+b]
bin=pack('>%dB'%b,*bin)
upack = ['uint:%d'%(8-a) if a!=8 else 'uint:1' for a in shifts]
channels=bin.unpack(','.join(upack))
channels=[CB(c,8-s) if s<8 else OBA(c) for c,s in zip(channels,shifts)]
AlphaAdd = [0xFF]*(len(channels)*2)
AlphaAdd[0::2] = channels
p = struct.pack('>%dB'%len(AlphaAdd),*AlphaAdd)
row.extend(p)
newfile.append(row)
return newfile
#change IA31 to IA88 or rgba5551 to rgba8888
def EditFile(file,width,height,shifts,PpB,b):
newfile=[]
for x in range(height):
row=[]
for pixel in range(0,width//PpB):
pixel=pixel+x*width//PpB
bin=file[b*pixel:b*pixel+b]
bin=pack('>%dB'%b,*bin)
upack = ['uint:%d'%(8-a) if a!=8 else 'uint:1' for a in shifts]
channels=bin.unpack(','.join(upack))
channels=[CB(c,8-s) if s<8 else OBA(c) for c,s in zip(channels,shifts)]
p = struct.pack('>%dB'%len(channels),*channels)
row.extend(p)
newfile.append(row)
return newfile
#convert bits
@lru_cache(maxsize=255)
def CB(val,bits):
return int(((val*255)+(2**(bits-1))-1)/(2**(bits)-1))
#one bit alpha
def OBA(val):
if val:
return 255
else:
return 0
#Palette is [Binary Region,format],every palette uses either IA16 or RGBA16
def GetPalette(palette,depth,bpp):
bin = palette[0]
shifts = [3,3,3,8]
o=[]
for p in range(2**depth):
b = BitArray(bin[p*2:p*2+2])
a=b.unpack('3*uint:5,uint:1')
a = *(CB(c,8-s) if s<8 else OBA(c) for c,s in zip(a,shifts)),
o.append(a)
return o
def MakeRGBA(file,Bpp,Alpha):
r = png.Reader(file)
re = r.read()
depth = re[3]['bitdepth']
channels = re[3]['planes']
if channels==3:
shifts = [3,3,3]
else:
shifts = [3,3,3,7]
if depth==8 and Bpp==16:
#two bytes per pixel, need to convert to rgba5551
rows = []
for r in re[2]:
for W in range(0,len(r),channels):
b = r[W:W+channels]
a = [c>>s for c,s in zip(b,shifts)]
if len(shifts)==3:
a.append(1)
b = pack('3*uint:5,uint:1',*a)
rows.append(b.bytes)
return rows
else:
if channels==3:
solid = bytes([0xff])
else:
solid = bytes()
bin = []
for r in re[2]:
for W in range(0,len(r),channels):
bin.append(r[W:W+channels]+solid)
return bin
def MakeCI(file,Bpp,Alpha):
r = png.Reader(file)
re = r.read()
Pal = re[3]['palette']
Pbin = []
shifts = [3,3,3,7]
for p in Pal:
b = [a>>s for a,s in zip(p,shifts)]
if len(p)==4:
b = pack('3*uint:5,uint:1',*b)
else:
b = pack('3*uint:5,uint:1',*b,1)
Pbin.append(b.bytes)
bin = []
for p in re[2]:
for w in range(0,re[0],(8//Bpp)):
b = p[w:w+(8//Bpp)]
b = pack('%d*uint:%d'%((8//Bpp),Bpp),*b)
bin.append(b.bytes)
return [bin,Pbin]
def MakeIntensity(file,Bpp,Alpha):
r = png.Reader(file)
re = r.read()
depth = re[3]['bitdepth']
channels = re[3]['planes']
if channels-Alpha>1:
#This means it has an alpha channel we must ignore
bin = []
for r in re[2]:
last = 0
for W in range(0,len(r),channels*depth//8):
b = r[W:W+(max(1,Bpp//8))]
b = [a for a in b]
if last==0 and Bpp==4:
last = ((b[0]>>(depth-4))&0xF)<<(depth-4)
continue
elif (8-Bpp)>0:
a = b[0]>>(depth-4)
else:
a = b[0]>>(depth-Bpp)
bin.append(bytes([a+last]))
last = 0
return bin
else:
#This should mean its all ready to go
if Bpp==4:
shifts = [4,7]
else:
shifts = [0,0]
bin = []
for r in re[2]:
last = 0
for W in range(0,len(r),channels*depth//8):
b = r[W:W+(max(1,Bpp//8))*channels]
b = [a for a in b]
if last==0 and Bpp==4:
if Alpha:
last = ((b[0]>>(depth-4))&0xE)<<(depth-4)
last += ((b[1]>>(depth-1))&0xF)<<(depth-4)
else:
last = ((b[0]>>(depth-4))&0xF)<<(depth-4)
continue
elif Bpp==4:
if Alpha:
a = (b[0]>>(depth-4))&0xE
a += b[1]>>(depth-1)
else:
a = b[0]>>(depth-4)
else:
a = b[0]>>(depth-Bpp)
if Alpha:
a = ((b[0]>>(depth-Bpp//2))<<(Bpp//2))+(b[1]>>(depth-Bpp//2))
bin.append(bytes([a+last]))
last = 0
return bin