forked from aboood40091/Puzzle-NSMBU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompressBC3.py
316 lines (232 loc) · 9.15 KB
/
compressBC3.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Miyamoto! Level Editor - New Super Mario Bros. U Level Editor
# Copyright (C) 2009-2017 Treeki, Tempus, angelsl, JasonP27, Kinnay,
# MalStar1000, RoadrunnerWMC, MrRean, Grop, AboodXD, Gota7
# compressBC3.py
# A Python port of Wexos's Toolbox BC3 compressor.
################################################################
################################################################
def CompressAlphaBlock(AlphaBlock, Palette, Width, Height, x, y):
Indices = [0] * 16
for y2 in range(4):
for x2 in range(4):
if (y + y2) < Height and (x + x2) < Width:
Index = 0
Delta = 2147483647
A = AlphaBlock[y2 * 4 + x2]
for i in range(len(Palette)):
if A == Palette[i]:
Index = i
break
AlphaDelta = A - Palette[i]
AlphaDelta = max(AlphaDelta, -AlphaDelta)
if AlphaDelta < Delta:
Delta = AlphaDelta
Index = i
Indices[y2 * 4 + x2] = Index
return Indices
def GetAlphaPalette(A0, A1):
Palette = bytearray(8)
Palette[0] = A0
Palette[1] = A1
if A0 > A1:
Palette[2] = (6 * A0 + 1 * A1) // 7
Palette[3] = (5 * A0 + 2 * A1) // 7
Palette[4] = (4 * A0 + 3 * A1) // 7
Palette[5] = (3 * A0 + 4 * A1) // 7
Palette[6] = (2 * A0 + 5 * A1) // 7
Palette[7] = (1 * A0 + 6 * A1) // 7
else:
Palette[2] = (4 * A0 + 1 * A1) // 5
Palette[3] = (3 * A0 + 2 * A1) // 5
Palette[4] = (2 * A0 + 3 * A1) // 5
Palette[5] = (1 * A0 + 4 * A1) // 5
Palette[6] = 0
Palette[7] = 0xFF
return Palette
def CompressBlock(ColorBlock, Palette, Width, Height, x, y, ScaleR, ScaleG, ScaleB):
Indices = [0] * 16
for y2 in range(4):
for x2 in range(4):
if y + y2 < Height and x + x2 < Width:
Index = 0
Delta = 2147483647
Color = ColorBlock[y2 * 4 + x2]
R = (Color >> 16) & 0xFF
G = (Color >> 8) & 0xFF
B = Color & 0xFF
A = (Color >> 24) & 0xFF
for i in range(len(Palette)):
if Color == Palette[i]:
Index = i
break
RedDelta = R - ((Palette[i] >> 16) & 0xFF)
GreenDelta = G - ((Palette[i] >> 8) & 0xFF)
BlueDelta = B - (Palette[i] & 0xFF)
RedDelta = max(RedDelta, -RedDelta)
GreenDelta = max(GreenDelta, -GreenDelta)
BlueDelta = max(BlueDelta, -BlueDelta)
NewDelta = RedDelta * ScaleR + GreenDelta * ScaleG + BlueDelta * ScaleB
if NewDelta < Delta:
Delta = NewDelta
Index = i
Indices[y2 * 4 + x2] = Index
return Indices
def FindMinMax(Colors):
MaxR = 0
MaxG = 0
MaxB = 0
MinR = 255
MinG = 255
MinB = 255
TransparentBlock = True
for Color in Colors:
R = (Color >> 16) & 0xFF
G = (Color >> 8) & 0xFF
B = Color & 0xFF
A = (Color >> 24) & 0xFF
if not A:
continue
TransparentBlock = False
# Max color
if R > MaxR: MaxR = R
if G > MaxG: MaxG = G
if B > MaxB: MaxB = B
# Min color
if R < MinR: MinR = R
if G < MinG: MinG = G
if B < MinB: MinB = B
if TransparentBlock:
MinR = 0
MinG = 0
MinB = 0
MaxR = 0
MaxG = 0
MaxB = 0
return MinR, MinG, MinB, MaxR, MaxG, MaxB, TransparentBlock
def ToARGB8(Red, Green, Blue, Alpha):
R = int(Red * 255)
G = int(Green * 255)
B = int(Blue * 255)
A = int(Alpha * 255)
return (A << 24) | (R << 16) | (G << 8) | B
def GetPalette(Color0, Color1):
Palette = [0] * 4
Palette[0] = ToARGB8(((Color0 >> 11) & 0b11111) / 31, ((Color0 >> 5) & 0b111111) / 63, (Color0 & 0b11111) / 31, 0)
Palette[1] = ToARGB8(((Color1 >> 11) & 0b11111) / 31, ((Color1 >> 5) & 0b111111) / 63, (Color1 & 0b11111) / 31, 0)
R0 = (Palette[0] >> 16) & 0xFF
G0 = (Palette[0] >> 8) & 0xFF
B0 = Palette[0] & 0xFF
R1 = (Palette[1] >> 16) & 0xFF
G1 = (Palette[1] >> 8) & 0xFF
B1 = Palette[1] & 0xFF
Palette[2] = ((2 * R0 // 3 + 1 * R1 // 3) << 16) | ((2 * G0 // 3 + 1 * G1 // 3) << 8) | (2 * B0 // 3 + 1 * B1 // 3)
Palette[3] = ((1 * R0 // 3 + 2 * R1 // 3) << 16) | ((1 * G0 // 3 + 2 * G1 // 3) << 8) | (1 * B0 // 3 + 2 * B1 // 3)
return Palette
def ToRGB565(R, G, B):
return ((R >> 3) << 11) | ((G >> 2) << 5) | (B >> 3)
def FindColors(Colors):
ThresholdMin = 0x02
ThresholdMax = 0xFD
PartMin = 255
PartMax = 0
Min = 255
Max = 0
UseLessThanAlgorithm = False # Used when colors are close to both 0 and 0xFF
for Color in Colors:
if Color <= ThresholdMin:
UseLessThanAlgorithm = True
elif Color >= ThresholdMax:
UseLessThanAlgorithm = True
if not UseLessThanAlgorithm and Color < PartMin:
PartMin = Color
if not UseLessThanAlgorithm and Color > PartMax:
PartMax = Color
if Color < Min:
Min = Color
if Color > Max:
Max = Color
if Max <= 0x15 or (Min <= 0x05 and Max <= 0x30) or Min >= 0xEA or (Max >= 0xFA and Min >= 0xCF): # What is good here?
UseLessThanAlgorithm = False
else:
Max = PartMax
Min = PartMin
if not UseLessThanAlgorithm and Min == Max:
Max -= 1
if Max < 0:
Max = 256 + Max
Color0 = Min if UseLessThanAlgorithm else Max
Color1 = Max if UseLessThanAlgorithm else Min
return Color0, Color1
def CompressBC3(SrcPtr, Stride, Width, Height):
Stride //= 4
DstPtr = bytearray()
for y in range(0, Height, 4):
for x in range(0, Width, 4):
Colors = []
Alphas = []
ActualColors = [0] * 16
ActualAlphas = bytearray(16)
for y2 in range(4):
for x2 in range(4):
if y + y2 < Height and x + x2 < Width:
# Read RGBA data and convert it to ARGB
pos = (y + y2) * Stride + (x + x2)
pos *= 4
A = SrcPtr[pos + 3]
R = SrcPtr[pos]
G = SrcPtr[pos + 1]
B = SrcPtr[pos + 2]
Color = (A << 24) | (R << 16) | (G << 8) | B
Colors.append(Color)
Alphas.append(A)
ActualColors[y2 * 4 + x2] = Color
ActualAlphas[y2 * 4 + x2] = A
MinR, MinG, MinB, MaxR, MaxG, MaxB, TransparentBlock = FindMinMax(Colors)
Alpha0, Alpha1 = FindColors(Alphas)
Color0 = ToRGB565(MaxR, MaxG, MaxB)
Color1 = ToRGB565(MinR, MinG, MinB)
if Color0 == Color1:
Color0 += 1
Palette = GetPalette(Color0, Color1)
AlphaPalette = GetAlphaPalette(Alpha0, Alpha1)
Indices = 0
AlphaIndices = 0
if not TransparentBlock:
IndexBlock = CompressBlock(ActualColors, Palette, Width, Height, x, y, 256 - (MaxR - MinR), 256 - (MaxG - MinG), 256 - (MaxB - MinB))
AlphaIndexBlock = CompressAlphaBlock(ActualAlphas, AlphaPalette, Width, Height, x, y)
for y2 in range(4):
for x2 in range(4):
i = y2 * 4 + x2
Indices |= IndexBlock[i] << (i * 2)
AlphaIndices |= AlphaIndexBlock[i] << (i * 3)
else:
Indices = 0xFFFFFFFF
TransparentIndex = 0
for i in range(len(AlphaPalette)):
if not AlphaPalette[i]:
TransparentIndex = i
break
if AlphaPalette[TransparentIndex]:
raise RuntimeError
for y2 in range(4):
for x2 in range(4):
AlphaIndices |= TransparentIndex << ((y2 * 4 + x2) * 3)
DstPtr += bytes([Alpha0])
DstPtr += bytes([Alpha1])
DstPtr += bytes([AlphaIndices & 0xFF])
DstPtr += bytes([(AlphaIndices >> 8) & 0xFF])
DstPtr += bytes([(AlphaIndices >> 16) & 0xFF])
DstPtr += bytes([(AlphaIndices >> 24) & 0xFF])
DstPtr += bytes([(AlphaIndices >> 32) & 0xFF])
DstPtr += bytes([(AlphaIndices >> 40) & 0xFF])
DstPtr += bytes([Color0 & 0xFF])
DstPtr += bytes([(Color0 >> 8) & 0xFF])
DstPtr += bytes([Color1 & 0xFF])
DstPtr += bytes([(Color1 >> 8) & 0xFF])
DstPtr += bytes([Indices & 0xFF])
DstPtr += bytes([(Indices >> 8) & 0xFF])
DstPtr += bytes([(Indices >> 16) & 0xFF])
DstPtr += bytes([(Indices >> 24) & 0xFF])
return DstPtr