-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecompressor.cpp
More file actions
395 lines (337 loc) · 16.6 KB
/
decompressor.cpp
File metadata and controls
395 lines (337 loc) · 16.6 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdint>
#include <algorithm>
#include <cctype>
// --- Lookup Tables (Values from original code) ---
const uint32_t dword_4B3B80[] = { 0x40, 0x5E };
const uint32_t dword_4B3B88[] = { 0, 0x20 };
const uint32_t dword_4B3B90[] = { 0, 0x20 };
// ===============================================================================================
// UTILITY FUNCTIONS
// ===============================================================================================
// Helper to safely read from the compressed data buffer
inline uint32_t get_bit(const std::vector<uint32_t>& data, int bit_index) {
if ((bit_index >> 5) >= data.size()) return 0;
return (data[bit_index >> 5] >> (bit_index & 0x1F)) & 1;
}
// ===============================================================================================
// PORTED DECOMPRESSION FUNCTIONS
// ===============================================================================================
// Forward declarations
void sub_45B6D0(int width, int x, int y, uint32_t* image_buffer, const std::vector<uint32_t>& pixel_data, uint32_t a8);
void sub_45B930(int width, int x, int y, uint32_t* image_buffer, const std::vector<uint32_t>& pixel_data);
void sub_45BB00(int width, int x, int y, uint32_t* image_buffer, const std::vector<uint32_t>& pixel_data);
void sub_45BCE0(int width, int x, int y, uint32_t* image_buffer, const std::vector<uint32_t>& pixel_data, uint32_t a8);
// Corresponds to sub_45B530
// Generates a palette based on the exact reverse-order interpolation logic from the original game.
void generate_palette_from_ida(uint32_t* palette, const uint32_t* c1, const uint32_t* c2, bool is_3_color_mode) {
if (!is_3_color_mode) {
// --- 4-Color Opaque Mode ---
// CRITICAL: The interpolation is from c2 to c1, not the other way around.
// Palette[0] = c2
// Palette[1] = 1/3 c1 + 2/3 c2
// Palette[2] = 2/3 c1 + 1/3 c2
// Palette[3] = c1
for (int i = 0; i < 3; ++i) { // RGB components
palette[0 * 4 + i] = c2[i];
palette[1 * 4 + i] = (1 * c1[i] + 2 * c2[i] + 1) / 3;
palette[2 * 4 + i] = (2 * c1[i] + 1 * c2[i] + 1) / 3;
palette[3 * 4 + i] = c1[i];
}
for (int i = 0; i < 4; ++i) {
palette[i * 4 + 3] = 255; // Set full alpha for all colors
}
}
else {
// --- 3-Color + 1-Bit Alpha Mode ---
// CRITICAL: The interpolation is also from c2 to c1 here.
// Palette[0] = c2
// Palette[1] = 1/2 c1 + 1/2 c2
// Palette[2] = c1
// Palette[3] = Transparent
for (int i = 0; i < 3; ++i) { // RGB components
palette[0 * 4 + i] = c2[i];
palette[1 * 4 + i] = (c1[i] + c2[i]) / 2; // Note: Original uses integer division, no +1 rounding
palette[2 * 4 + i] = c1[i];
}
palette[0 * 4 + 3] = 255;
palette[1 * 4 + 3] = 255;
palette[2 * 4 + 3] = 255;
// Set the fourth color to transparent black
palette[3 * 4 + 0] = 0;
palette[3 * 4 + 1] = 0;
palette[3 * 4 + 2] = 0;
palette[3 * 4 + 3] = 0;
}
}
// Corresponds to sub_45B630
// Generates an 8-color palette by interpolating between two RGB colors.
void generate_palette_mode1(uint32_t* palette, const uint32_t* colors1, const uint32_t* colors2)
{
// This function generates 7 interpolated colors and one transparent color.
// The loop iterates 7 times to generate the main part of the palette.
for (int i = 0; i < 7; ++i) {
uint32_t row = i; // Corresponds to the weight of colors1
uint32_t weight = 6 - i; // Corresponds to the weight of colors2
uint32_t* output_ptr = palette + (i * 4); // Point to the start of the current color in the palette
// Interpolate the R, G, B components
for (int comp = 0; comp < 3; ++comp) {
uint32_t term1 = colors1[comp] * weight;
uint32_t term2 = colors2[comp] * row;
output_ptr[comp] = (term1 + term2 + 2) / 6;
}
output_ptr[3] = 0xFF; // Set alpha channel to opaque
}
// Set the 8th and final color to transparent black
palette[28] = 0;
palette[29] = 0;
palette[30] = 0;
palette[31] = 0;
}
// Corresponds to sub_45B6D0
void sub_45B6D0(int width, int x, int y, uint32_t* image_buffer, const std::vector<uint32_t>& pixel_data, uint32_t a8) {
bool v27 = (a8 >> 28) & 1; // This is the 'a1' flag for the palette function
for (int block_idx = 0; block_idx < 2; ++block_idx) {
int block_offset_x = block_idx * 4;
uint32_t c1[4] = { 0 }, c2[4] = { 0 }; // c1 is a4, c2 is a3
uint32_t color_base_offset = dword_4B3B80[block_idx];
bool v11 = (block_idx == 0) ? ((a8 >> 29) & 1) : ((a8 >> 30) & 1);
for (int i = 0; i < 3; ++i) {
uint32_t c1_comp_offset = color_base_offset;
uint32_t c2_comp_offset = color_base_offset + 15;
for (int bit_idx = 0; bit_idx < 5; ++bit_idx) {
c1[i] |= get_bit(pixel_data, c1_comp_offset + i * 5 + bit_idx) << (3 + bit_idx);
c2[i] |= get_bit(pixel_data, c2_comp_offset + i * 5 + bit_idx) << (3 + bit_idx);
}
c1[i] |= (c1[i] >> 5);
c2[i] |= (c2[i] >> 5);
}
c1[1] = (c1[1] & 0xF8) | (c1[1] >> 6) | (v11 ? 4 : 0);
if (!v27) {
bool xor_bit = get_bit(pixel_data, dword_4B3B88[block_idx] + 1);
c2[1] = (c2[1] & 0xF8) | (c2[1] >> 6) | ((v11 ^ xor_bit) ? 4 : 0);
}
uint32_t palette[4 * 4];
generate_palette_from_ida(palette, c1, c2, v27);
uint32_t* block_ptr = image_buffer + (y * width) + x + block_offset_x;
int bit_stream_offset = dword_4B3B88[block_idx];
for (int row = 0; row < 4; ++row) {
for (int col = 0; col < 4; ++col) {
int bit_offset = row * 8 + col * 2;
uint32_t index_bit0 = get_bit(pixel_data, bit_stream_offset + bit_offset);
uint32_t index_bit1 = get_bit(pixel_data, bit_stream_offset + bit_offset + 1);
uint32_t palette_index = index_bit0 | (index_bit1 << 1);
uint32_t r = palette[palette_index * 4 + 0];
uint32_t g = palette[palette_index * 4 + 1];
uint32_t b = palette[palette_index * 4 + 2];
uint32_t a = palette[palette_index * 4 + 3];
if ((y + row < width) && (x + block_offset_x + col < width)) {
block_ptr[row * width + col] = (a << 24) | (b << 16) | (g << 8) | r;
}
}
}
}
}
// Corresponds to sub_45B930
void sub_45B930(int width, int x, int y, uint32_t* image_buffer, const std::vector<uint32_t>& pixel_data) {
uint32_t c1[3] = { 0 }, c2[3] = { 0 };
for (int i = 0; i < 3; ++i) { // RGB
int component_offset = i * 5;
for (int bit_idx = 0; bit_idx < 5; ++bit_idx) {
c1[i] |= get_bit(pixel_data, 96 + component_offset + bit_idx) << (3 + bit_idx);
c2[i] |= get_bit(pixel_data, 111 + component_offset + bit_idx) << (3 + bit_idx);
}
c1[i] |= c1[i] >> 5;
c2[i] |= c2[i] >> 5;
}
uint32_t palette[8 * 4]; // 8 colors, 4 channels
generate_palette_mode1(palette, c1, c2);
uint32_t* block_ptr = image_buffer + y * width + x;
for (int row = 0; row < 4; ++row) {
for (int col = 0; col < 8; ++col) {
// Correct bit offset calculation based on column position
int bit_offset = (col < 4 ? 0 : 0x30) + 3 * ((row * 4) + (col & 3));
uint32_t idx0 = get_bit(pixel_data, bit_offset);
uint32_t idx1 = get_bit(pixel_data, bit_offset + 1);
uint32_t idx2 = get_bit(pixel_data, bit_offset + 2);
uint32_t palette_index = idx0 | (idx1 << 1) | (idx2 << 2);
uint32_t r = palette[palette_index * 4 + 0];
uint32_t g = palette[palette_index * 4 + 1];
uint32_t b = palette[palette_index * 4 + 2];
uint32_t a = palette[palette_index * 4 + 3];
if (y + row < width && x + col < width) {
block_ptr[row * width + col] = (a << 24) | (b << 16) | (g << 8) | r;
}
}
}
}
// Corresponds to sub_45BB00
void sub_45BB00(int width, int x, int y, uint32_t* image_buffer, const std::vector<uint32_t>& pixel_data) {
uint32_t colors[4][4] = { {0} };
int base_bit_index = 79;
// Outer loop iterates through components (R, G, B)
for (int comp_idx = 0; comp_idx < 3; ++comp_idx) {
// Inner loop builds the 5 bits for the current component for each color.
for (int bit_pos = 0; bit_pos < 5; ++bit_pos) {
int current_bit_index = base_bit_index + bit_pos;
int dest_bit = 3 + bit_pos;
// Read the bit for the current component (comp_idx) for each of the 4 colors.
// The source bit locations are interleaved in the data stream.
colors[0][comp_idx] |= get_bit(pixel_data, current_bit_index - 15) << dest_bit;
colors[1][comp_idx] |= get_bit(pixel_data, current_bit_index) << dest_bit;
colors[2][comp_idx] |= get_bit(pixel_data, current_bit_index + 15) << dest_bit;
colors[3][comp_idx] |= get_bit(pixel_data, current_bit_index + 30) << dest_bit;
}
base_bit_index += 5; // Move base index to the start of the next component block.
}
// Finalize the colors: apply the ">> 5" replication and set alpha.
for (int c_idx = 0; c_idx < 4; ++c_idx) {
for (int comp_idx = 0; comp_idx < 3; ++comp_idx) {
colors[c_idx][comp_idx] |= colors[c_idx][comp_idx] >> 5;
}
colors[c_idx][3] = 255; // Alpha is always opaque.
}
uint32_t* block_ptr = image_buffer + y * width + x;
for (int row = 0; row < 4; ++row) {
for (int col = 0; col < 8; ++col) {
int bit_offset = (col < 4 ? 0 : 0x20) + 2 * ((row * 4) + (col & 3));
uint32_t idx0 = get_bit(pixel_data, bit_offset);
uint32_t idx1 = get_bit(pixel_data, bit_offset + 1);
uint32_t palette_index = idx0 | (idx1 << 1);
uint32_t r = colors[palette_index][0];
uint32_t g = colors[palette_index][1];
uint32_t b = colors[palette_index][2];
uint32_t a = colors[palette_index][3];
if (y + row < width && x + col < width) {
block_ptr[row * width + col] = (a << 24) | (b << 16) | (g << 8) | r;
}
}
}
}
// Corresponds to sub_45BCE0
void sub_45BCE0(int width, int x, int y, uint32_t* image_buffer, const std::vector<uint32_t>& pixel_data, uint32_t a8) {
bool v58 = (a8 >> 28) & 1;
uint32_t c[3][4] = { {0} };
// This function reads color data component-first.
// It reads the R for all 3 colors, then G for all 3, then B for all 3.
int base_bit_index = 79;
// Outer loop iterates through components (R, G, B)
for (int comp_idx = 0; comp_idx < 3; ++comp_idx) {
// Inner loop builds the 5 bits for the current component for each of the 3 colors.
for (int bit_pos = 0; bit_pos < 5; ++bit_pos) {
int current_bit_index = base_bit_index + bit_pos;
int dest_bit = 3 + bit_pos;
// Read the bit for the current component (comp_idx) for each of the 3 colors.
c[0][comp_idx] |= get_bit(pixel_data, current_bit_index - 15) << dest_bit;
c[1][comp_idx] |= get_bit(pixel_data, current_bit_index) << dest_bit;
c[2][comp_idx] |= get_bit(pixel_data, current_bit_index + 15) << dest_bit;
}
base_bit_index += 5; // Move to the start of the next component's data.
}
// Read the 5-bit Alpha for all 3 colors, matching the decompiled version's logic.
for (int bit_pos = 0; bit_pos < 5; ++bit_pos) {
int dest_bit = 3 + bit_pos;
int base_idx = 13 + bit_pos;
c[0][3] |= get_bit(pixel_data, base_idx + 96) << dest_bit;
c[1][3] |= get_bit(pixel_data, base_idx + 101) << dest_bit;
c[2][3] |= get_bit(pixel_data, base_idx + 106) << dest_bit;
}
// Finalize the colors: apply the ">> 5" replication to all RGBA components.
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 4; ++j) { // Loop to 4 to include Alpha
c[i][j] |= c[i][j] >> 5;
}
}
// Interpolated path
if (v58) {
for (int block_idx = 0; block_idx < 2; ++block_idx) {
uint32_t palette[4][4];
if (block_idx == 0) {
// First block interpolates from c[0] to c[1]
for (int j = 0; j < 4; ++j) {
for (int k = 0; k < 4; ++k) {
palette[j][k] = (c[0][k] * (3 - j) + c[1][k] * j + 1) / 3;
}
}
}
else {
// Second block interpolates from c[2] to c[1]
for (int j = 0; j < 4; ++j) {
for (int k = 0; k < 4; ++k) {
palette[j][k] = (c[2][k] * (3 - j) + c[1][k] * j + 1) / 3;
}
}
}
uint32_t* block_ptr = image_buffer + (y * width) + x + (block_idx * 4);
int bit_stream_offset = dword_4B3B90[block_idx];
for (int row = 0; row < 4; ++row) {
for (int col = 0; col < 4; ++col) {
int bit_offset = row * 8 + col * 2;
uint32_t idx0 = get_bit(pixel_data, bit_stream_offset + bit_offset);
uint32_t idx1 = get_bit(pixel_data, bit_stream_offset + bit_offset + 1);
uint32_t pal_idx = idx0 | (idx1 << 1);
uint32_t r = palette[pal_idx][0];
uint32_t g = palette[pal_idx][1];
uint32_t b = palette[pal_idx][2];
uint32_t a = palette[pal_idx][3];
if (y + row < width && x + col + block_idx * 4 < width) {
block_ptr[row * width + col] = (a << 24) | (b << 16) | (g << 8) | r;
}
}
}
}
}
else {
// Non-interpolated path
uint32_t* block_ptr = image_buffer + y * width + x;
for (int row = 0; row < 4; ++row) {
for (int col = 0; col < 8; ++col) {
int bit_offset = (col < 4 ? 0 : 0x20) + 2 * ((row * 4) + (col & 3));
uint32_t idx0 = get_bit(pixel_data, bit_offset);
uint32_t idx1 = get_bit(pixel_data, bit_offset + 1);
uint32_t pal_idx = idx0 | (idx1 << 1);
uint32_t r = (pal_idx < 3) ? c[pal_idx][0] : 0;
uint32_t g = (pal_idx < 3) ? c[pal_idx][1] : 0;
uint32_t b = (pal_idx < 3) ? c[pal_idx][2] : 0;
uint32_t a = (pal_idx < 3) ? c[pal_idx][3] : 0;
if (y + row < width && x + col < width) {
block_ptr[row * width + col] = (a << 24) | (b << 16) | (g << 8) | r;
}
}
}
}
}
// Corresponds to sub_45C130, the main dispatcher
void decode_block(int width, int x, int y, uint32_t* image_buffer, const std::vector<uint32_t>& pixel_data) {
uint32_t a8 = pixel_data[3];
if ((a8 & 0x80000000) != 0) {
sub_45B6D0(width, x, y, image_buffer, pixel_data, a8);
}
else if (((a8 >> 30) & 1) == 0) {
sub_45B930(width, x, y, image_buffer, pixel_data);
}
else if (((a8 >> 29) & 1) != 0) {
sub_45BCE0(width, x, y, image_buffer, pixel_data, a8);
}
else {
sub_45BB00(width, x, y, image_buffer, pixel_data);
}
}
// Corresponds to ProcessTextureBlocks
void decompress_image(uint32_t* decompressed_output, const char* compressed_input, int width, int height) {
const uint32_t* data_ptr = reinterpret_cast<const uint32_t*>(compressed_input);
for (int y = 0; y < height; y += 4) {
for (int x = 0; x < width; x += 8) {
std::vector<uint32_t> block_data(4);
// The original code passes the whole block as arguments, so we load it here.
block_data[0] = data_ptr[0]; // Corresponds to a5
block_data[1] = data_ptr[1]; // Corresponds to a6
block_data[2] = data_ptr[2]; // Corresponds to a7
block_data[3] = data_ptr[3]; // Corresponds to a8
data_ptr += 4;
decode_block(width, x, y, decompressed_output, block_data);
}
}
}