-
Notifications
You must be signed in to change notification settings - Fork 4
/
tcb-convert.c
420 lines (356 loc) · 11.1 KB
/
tcb-convert.c
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include "lodepng/lodepng.h"
#include "pngquant/libimagequant.h"
// This is a modified TIM2 image data header
// Reference: http://wiki.xentax.com/index.php/TM2_TIM2
typedef struct tcbHeader {
// 0x00
char magic[4];
char padding[12];
// 0x10
uint32_t totalLength;
uint32_t paletteLength;
uint32_t imageDataLength;
uint16_t headerLength;
uint16_t numPaletteEntries;
// 0x20
uint8_t imageType;
uint8_t numMipmaps;
uint8_t paletteType;
uint8_t bitsPerPixel;
uint16_t width;
uint16_t height;
uint64_t gsTEX0;
// 0x30
uint64_t gsTEX1;
uint32_t gsRegs;
uint32_t gsTexClut;
// 0x40
uint8_t userData[16];
} tcbHeader_t;
typedef struct rgbaImageData {
uint32_t *rgba;
unsigned int width;
unsigned int height;
} rgbaImageData_t;
#define LONIBBLE(x) ((uint8_t) ((uint8_t) (x) & (uint8_t) 0x0F))
#define HINIBBLE(x) ((uint8_t) ((uint8_t) (x) >> (uint8_t) 4))
/**
* Filters palette colors to/from the format used by PS2 games.
*
* @param unfiltered pointer to array of unfiltered RGBA colors.
* @param filtered pointer to store array of filtered RGBA colors.
* @param length number of elements in unfiltered/filtered color array.
*
* @pre unfiltered and filtered point to allocated memory of length*4 bytes.
* @post filtered colors copied to filtered.
*/
void palette_filter(uint32_t *unfiltered, uint32_t *filtered, int length)
{
int parts = length / 32;
int stripes = 2;
int colors = 8;
int blocks = 2;
int i = 0;
for (int part = 0; part < parts; part++)
{
for (int block = 0; block < blocks; block++)
{
for (int stripe = 0; stripe < stripes; stripe++)
{
for (int color = 0; color < colors; color++)
{
filtered[i++] = unfiltered[ part * colors * stripes * blocks + block * colors + stripe * stripes * colors + color ];
}
}
}
}
}
int tcb_to_rgba(uint8_t *tcbData, int tcbLength, rgbaImageData_t *imageData);
int rgba_to_tcb(uint8_t *tcbData, int tcbLength, rgbaImageData_t *imageData);
int png_decode(char *pngPath, rgbaImageData_t *imageData);
int png_encode(char *pngPath, rgbaImageData_t *imageData);
int main(int argc, char *argv[])
{
FILE *tcbFile;
int tcbLength;
uint8_t *tcbData;
char *tcbPath;
char pngPath[260];
char mode;
rgbaImageData_t imageData;
if(argc != 3)
{
printf("TCB Converter\n");
printf("Usage:\n");
printf("Convert TCB to PNG: %s e <image.tcb>\n", argv[0]);
printf("Inject PNG into TCB: %s i <image.tcb>\n", argv[0]);
return EXIT_FAILURE;
}
mode = tolower(argv[1][0]);
tcbPath = argv[2];
strncpy(pngPath, tcbPath, sizeof(pngPath) - 1);
strncat(pngPath, ".png", sizeof(pngPath) - 1);
tcbFile = fopen(tcbPath, "rb");
if(!tcbFile)
{
printf("Error opening %s\n", tcbPath);
return EXIT_FAILURE;
}
fseek(tcbFile, 0, SEEK_END);
tcbLength = ftell(tcbFile);
fseek(tcbFile, 0, SEEK_SET);
tcbData = malloc(tcbLength);
if(!tcbData)
{
printf("malloc error\n");
fclose(tcbFile);
return EXIT_FAILURE;
}
fread(tcbData, 1, tcbLength, tcbFile);
fclose(tcbFile);
if(mode == 'i')
{
printf("Injecting image from %s into %s...\n", pngPath, tcbPath);
if(png_decode(pngPath, &imageData))
{
free(tcbData);
return EXIT_FAILURE;
}
if(rgba_to_tcb(tcbData, tcbLength, &imageData))
{
free(tcbData);
free(imageData.rgba);
return EXIT_FAILURE;
}
tcbFile = fopen(tcbPath, "wb");
if(!tcbFile)
{
printf("Error opening %s.\n", tcbPath);
free(tcbData);
free(imageData.rgba);
return EXIT_FAILURE;
}
fwrite(tcbData, 1, tcbLength, tcbFile);
fclose(tcbFile);
printf("Success!\n");
}
else if(mode == 'e')
{
printf("Extracting image from %s to %s...\n", tcbPath, pngPath);
if(tcb_to_rgba(tcbData, tcbLength, &imageData))
{
free(tcbData);
return EXIT_FAILURE;
}
if(png_encode(pngPath, &imageData))
{
free(tcbData);
free(imageData.rgba);
return EXIT_FAILURE;
}
printf("Success!\n");
}
else
{
printf("Error: Unsupported mode '%c'\n", mode);
free(tcbData);
return EXIT_FAILURE;
}
free(tcbData);
free(imageData.rgba);
return EXIT_SUCCESS;
}
/**
* Convert TCB data to RGBA data.
*
* @param tcbData pointer to data from a TCB image.
* @param tcbLength size of tcbData.
* @param imageData pointer to struct to store RGBA image data.
*
* @post Image data from TCB is converted to 32-bit RGBA and stored in imageData->rgba.
* @return 1 on success, 0 on failure
*/
int tcb_to_rgba(uint8_t *tcbData, int tcbLength, rgbaImageData_t *imageData)
{
tcbHeader_t *header;
uint8_t index, alpha;
uint8_t *image;
uint32_t *palette;
if (!tcbData || tcbLength <= 0 || !imageData)
return EXIT_FAILURE;
header = (tcbHeader_t*) tcbData;
image = (uint8_t*) (tcbData + 0x50);
palette = (uint32_t*) (tcbData + header->imageDataLength + 0x50);
if(strncmp(header->magic, "TCB\0", 4))
{
for(int i = 0; i < sizeof(header->padding); i++)
{
if(header->padding[i] != 0)
{
printf("ERROR: Invalid TCB file.\n");
return EXIT_FAILURE;
}
}
}
imageData->rgba = malloc(header->width * header->height * 4);
imageData->width = header->width;
imageData->height = header->height;
uint32_t *newPalette = NULL;
if(header->paletteLength > 0x50)
{
newPalette = calloc(256, 4);
palette_filter(palette, newPalette, (header->paletteLength - 0x10) / 4);
palette = newPalette;
}
if(header->paletteType == 0x05) // 8bpp
{
for(int i = 0; i < (header->width * header->height); i++)
{
index = image[i];
alpha = palette[index] >> 24;
if(alpha > 0)
alpha = (alpha << 1) - 1; // scale from 0-128 to 0-255 range
imageData->rgba[i] = palette[index] | (alpha << 24); // 255 alpha channel
}
}
else if(header->paletteType == 0x04) // 4bpp
{
for(int i = 0; i < (header->width * header->height); i++)
{
index = image[i/2];
if(i%2)
index = HINIBBLE(index);
else
index = LONIBBLE(index);
alpha = palette[index] >> 24;
if(alpha > 0)
alpha = (alpha << 1) - 1; // scale from 0-128 to 0-255 range
imageData->rgba[i] = palette[index] | (alpha << 24);
}
}
if(newPalette != NULL)
free(newPalette);
return EXIT_SUCCESS;
}
/**
* Inject RGBA data into existing TCB data.
*
* @param tcbData pointer to data from TCB image.
* @param tcbLength size of tcbData.
* @param imageData pointer to struct with RGBA image data.
*
* @post Image data from rgbaImageData is quantized and injected into tcbData.
* @return 0 on success, 1 on failure.
*/
int rgba_to_tcb(uint8_t *tcbData, int tcbLength, rgbaImageData_t *imageData)
{
tcbHeader_t *header;
uint8_t *image;
uint32_t *palette;
int len;
if(!tcbData || tcbLength <= 0 || !imageData)
return EXIT_FAILURE;
header = (tcbHeader_t*) tcbData;
image = (uint8_t*) (tcbData + 0x50);
palette = (uint32_t*) (tcbData + header->imageDataLength + 0x50);
if(header->width != imageData->width || header->height != imageData->height)
{
printf("Image size mismatch. PNG dimensions are %ux%u, but it needs to be %ux%u to be injected into the TCB.\n", imageData->width, imageData->height, header->width, header->height);
return EXIT_FAILURE;
}
liq_attr *attr = liq_attr_create();
if(header->paletteType == 0x04) // 4-bit color
{
liq_set_max_colors(attr, 16);
}
else if(header->paletteType != 0x05)
{
printf("Unsupported image type 0x%02X.\n", header->paletteType);
return EXIT_FAILURE;
}
/* Quantize image and create new image map and palette */
liq_image *imagel = liq_image_create_rgba(attr, imageData->rgba, imageData->width, imageData->height, 0);
liq_result *res = liq_quantize_image(attr, imagel);
if(header->paletteType == 0x05)
{
liq_write_remapped_image(res, imagel, image, imageData->height * imageData->width);
}
else if (header->paletteType == 0x04) // 4-bit color
{
uint8_t *fulldata = malloc(imageData->width * imageData->height);
liq_write_remapped_image(res, imagel, (uint32_t *)fulldata, imageData->width * imageData->height);
// Convert to 4-bit
for(int i = 0; i < (imageData->width * imageData->height)/2; i++)
{
uint8_t pixel1 = *((uint8_t *)fulldata + i*2);
uint8_t pixel2 = *((uint8_t *)fulldata + i*2 + 1);
image[i] = pixel1 | (pixel2 << 4);
}
free(fulldata);
}
/* Update palette in TCB */
const liq_palette *pal = liq_get_palette(res);
memcpy(palette, pal->entries, pal->count*4);
for(int i = 0; i < pal->count; i++)
{
uint8_t alpha = palette[i] >> 24;
if(alpha > 0)
alpha = (alpha >> 1) + 1; // scale down to 0-128 range
}
if(header->paletteType == 0x05) // Filter palette data
{
uint32_t *filtered = malloc(256 * 4);
palette_filter((uint32_t *) palette, filtered, 256);
memcpy((void *) palette, filtered, 256 * 4);
free(filtered);
}
liq_attr_destroy(attr);
liq_image_destroy(imagel);
liq_result_destroy(res);
return EXIT_SUCCESS;
}
/**
* Decode PNG file to RGBA data.
*
* @param pngPath Path to PNG file.
* @param imageData pointer to struct to store RGBA image data.
* @return 0 on success, 1 on failure.
*/
int png_decode(char *pngPath, rgbaImageData_t *imageData)
{
int ret;
if(!pngPath || !imageData)
return EXIT_FAILURE;
ret = lodepng_decode32_file((unsigned char **)&imageData->rgba, &imageData->width, &imageData->height, pngPath);
if(ret != 0)
{
printf("PNG decoding error %u: %s\n", ret, lodepng_error_text(ret));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/**
* Encode PNG file with RGBA data.
*
* @param pngPath Path to PNG file.
* @param imageData pointer to struct with RGBA image data.
* @return 0 on success, 1 on failure.
*/
int png_encode(char *pngPath, rgbaImageData_t *imageData)
{
int ret;
if(!pngPath || !imageData)
return EXIT_FAILURE;
ret = lodepng_encode32_file(pngPath, (const unsigned char *)imageData->rgba, imageData->width, imageData->height);
if(ret != 0)
{
printf("PNG encoding error %u: %s\n", ret, lodepng_error_text(ret));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}