forked from mkst/sm64-port
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathskyconv.c
657 lines (566 loc) · 19.1 KB
/
skyconv.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/* skybox generator */
#define _GNU_SOURCE
#include <assert.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include "n64graphics.h"
#include "utils.h"
typedef struct {
rgba *px;
bool useless;
unsigned int pos;
} TextureTile;
typedef enum {
InvalidType = -1,
Skybox,
Cake,
CakeEU,
ImageType_MAX
} ImageType;
typedef enum {
InvalidMode = -1,
Combine,
Split
} OperationMode;
typedef struct {
int imageWidth, imageHeight;
int tileWidth, tileHeight;
int numCols, numRows;
bool wrapX;
bool optimizePositions;
} ImageProps;
static const ImageProps IMAGE_PROPERTIES[ImageType_MAX][2] = {
[Skybox] = {
{248, 248, 31, 31, 8, 8, true, true},
{256, 256, 32, 32, 8, 8, true, true},
},
[Cake] = {
{316, 228, 79, 19, 4, 12, false, false},
{320, 240, 80, 20, 4, 12, false, false},
},
[CakeEU] = {
{320, 224, 64, 32, 5, 7, false, false},
{320, 224, 64, 32, 5, 7, false, false},
},
};
typedef struct {
int cols, rows;
} TableDimension;
static const TableDimension TABLE_DIMENSIONS[ImageType_MAX] = {
[Skybox] = {8, 10},
[Cake] = {4, 12},
[CakeEU] = {5, 7},
};
TextureTile *tiles;
ImageType type = InvalidType;
OperationMode mode = InvalidMode;
char *programName;
char *input, *output;
char *writeDir;
char skyboxName[256];
bool expanded = false;
bool writeTiles;
static void allocate_tiles() {
const ImageProps props = IMAGE_PROPERTIES[type][true];
int tileWidth = props.tileWidth;
int tileHeight = props.tileHeight;
int numRows = props.numRows;
int numCols = props.numCols;
int tileSize = tileWidth * tileHeight * sizeof(rgba);
int totalSize = numRows * numCols * tileSize;
tiles = calloc(1, numRows * numCols * sizeof(TextureTile));
rgba *tileData = calloc(1, totalSize);
for (int row = 0; row < numRows; ++row) {
for (int col = 0; col < numCols; ++col) {
tiles[row * numCols + col].px = (tileData + (row * numCols + col) * (tileWidth * tileHeight));
}
}
}
static void free_tiles() {
free(tiles->px);
free(tiles);
}
static void split_tile(int col, int row, rgba *image, bool expanded) {
const ImageProps props = IMAGE_PROPERTIES[type][expanded];
int tileWidth = props.tileWidth;
int tileHeight = props.tileHeight;
int imageWidth = props.imageWidth;
int numCols = props.numCols;
int expandedWidth = IMAGE_PROPERTIES[type][true].tileWidth;
for (int y = 0; y < tileHeight; y++) {
for (int x = 0; x < tileWidth; x++) {
int ny = row * tileHeight + y;
int nx = col * tileWidth + x;
tiles[row * numCols + col].px[y * expandedWidth + x] = image[(ny * imageWidth + nx)];
}
}
}
static void expand_tiles(ImageType imageType) {
const ImageProps props = IMAGE_PROPERTIES[imageType][true];
int numRows = props.numRows;
int numCols = props.numCols;
int tileWidth = props.tileWidth;
int tileHeight = props.tileHeight;
// If the image type wraps,
// Copy each tile's left edge to the previous tile's right edge
// Each tile's height is still tileHeight - 1
if (props.wrapX) {
for (int row = 0; row < numRows; ++row) {
for (int col = 0; col < numCols; ++col) {
int nextCol = (col + 1) % numCols;
for (int y = 0; y < (tileHeight - 1); ++y) {
tiles[row * numCols + col].px[(tileWidth - 1) + y * tileWidth] = tiles[row * numCols + nextCol].px[y * tileWidth];
}
}
}
} else {
// Don't wrap, copy the second to last column instead.
for (int row = 0; row < numRows; ++row) {
for (int col = 0; col < numCols - 1; ++col) {
int nextCol = (col + 1) % numCols;
for (int y = 0; y < (tileHeight - 1); ++y) {
tiles[row * numCols + col].px[(tileWidth - 1) + y * tileWidth] = tiles[row * numCols + nextCol].px[y * tileWidth];
}
}
for (int y = 0; y < (tileHeight - 1); ++y) {
tiles[row * numCols + (numCols - 1)].px[(tileWidth - 1) + y * tileWidth] = tiles[row * numCols + (numCols - 1)].px[(tileWidth - 2) + y * tileWidth];
}
}
}
// Copy each tile's top edge to the previous tile's bottom edge, EXCEPT for the bottom row, which
// just duplicates its second-to-last row
for (int row = 0; row < numRows; ++row) {
if (row < numRows - 1) {
for (int col = 0; col < numCols; ++col) {
int nextRow = (row + 1) % numRows;
for (int x = 0; x < tileWidth; ++x) {
tiles[row * numCols + col].px[x + (tileHeight - 1) * tileWidth] = tiles[nextRow * numCols + col].px[x];
}
}
}
// For the last row of tiles, duplicate each one's second to last row
else {
for (int col = 0; col < numCols; ++col) {
for (int x = 0; x < tileWidth; ++x) {
tiles[row * numCols + col].px[x + (tileHeight - 1) * tileWidth] = tiles[row * numCols + col].px[x + (tileHeight - 2) * tileWidth];
}
}
}
}
}
static void init_tiles(rgba *image, bool expanded) {
const ImageProps props = IMAGE_PROPERTIES[type][expanded];
for (int row = 0; row < props.numRows; row++) {
for (int col = 0; col < props.numCols; col++) {
split_tile(col, row, image, expanded);
}
}
// Expand the tiles to their full size
if (!expanded) {
expand_tiles(type);
}
}
static void assign_tile_positions() {
const ImageProps props = IMAGE_PROPERTIES[type][true];
const size_t TILE_SIZE = props.tileWidth * props.tileHeight * sizeof(rgba);
unsigned int newPos = 0;
for (int i = 0; i < props.numRows * props.numCols; i++) {
if (props.optimizePositions) {
for (int j = 0; j < i; j++) {
if (!tiles[j].useless && memcmp(tiles[j].px, tiles[i].px, TILE_SIZE) == 0) {
tiles[i].useless = 1;
tiles[i].pos = j;
break;
}
}
}
if (!tiles[i].useless) {
tiles[i].pos = newPos;
newPos++;
}
}
}
// Provide a replacement for realpath on Windows
#ifdef _WIN32
#define realpath(path, resolved_path) _fullpath(resolved_path, path, PATH_MAX)
#endif
/* write pngs to disc */
void write_tiles() {
const ImageProps props = IMAGE_PROPERTIES[type][true];
char buffer[PATH_MAX];
char skyboxName[PATH_MAX];
if (realpath(writeDir, buffer) == NULL) {
fprintf(stderr, "err: Could not find find img dir %s", writeDir);
exit(EXIT_FAILURE);
}
strcat(buffer, "/");
switch(type) {
case Skybox:
strcat(buffer, skyboxName);
break;
case Cake:
strcat(buffer, "cake");
break;
case CakeEU:
strcat(buffer, "cake_eu");
break;
default:
exit(EXIT_FAILURE);
break;
}
int dirLength = strlen(buffer);
char *filename = buffer + dirLength;
for (int i = 0; i < props.numRows * props.numCols; i++) {
if (!tiles[i].useless) {
*filename = 0;
snprintf(filename, PATH_MAX, ".%d.rgba16.png", tiles[i].pos);
rgba2png(buffer, tiles[i].px, props.tileWidth, props.tileHeight);
}
}
}
static unsigned int get_index(TextureTile *t, unsigned int i) {
if (t[i].useless) {
i = t[i].pos;
}
return t[i].pos;
}
static void print_raw_data(FILE *cFile, TextureTile *tile) {
ImageProps props = IMAGE_PROPERTIES[type][true];
uint8_t *raw = malloc(props.tileWidth * props.tileHeight * 2);
int size = rgba2raw(raw, tile->px, props.tileWidth, props.tileHeight, 16);
for (int i = 0; i < size; ++i) {
fprintf(cFile, "0x%hhX,", raw[i]);
}
free(raw);
}
static void write_skybox_c() { /* write c data to disc */
const ImageProps props = IMAGE_PROPERTIES[type][true];
char fBuffer[PATH_MAX] = "";
FILE *cFile;
if (realpath(output, fBuffer) == NULL) {
fprintf(stderr, "err: Could not find find src dir %s", output);
exit(EXIT_FAILURE);
}
sprintf(fBuffer, "%s/%s_skybox.c", output, skyboxName);
cFile = fopen(fBuffer, "w"); /* reset file */
/* setup C file */
if (cFile == NULL) {
fprintf(stderr, "err: Could not open %s\n", fBuffer);
}
fprintf(cFile, "#include \"sm64.h\"\n\n#include \"make_const_nonconst.h\"\n\n");
for (int i = 0; i < props.numRows * props.numCols; i++) {
if (!tiles[i].useless) {
fprintf(cFile, "ALIGNED8 static const u8 %s_skybox_texture_%05X[] = {\n", skyboxName, tiles[i].pos);
print_raw_data(cFile, &tiles[i]);
fputs("};\n\n", cFile);
}
}
fprintf(cFile, "const u8 *const %s_skybox_ptrlist[] = {\n", skyboxName);
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 10; col++) {
fprintf(cFile, "%s_skybox_texture_%05X,\n", skyboxName, get_index(tiles, row * 8 + (col % 8)));
}
}
fputs("};\n\n", cFile);
fclose(cFile);
}
static void write_cake_c() {
char buffer[PATH_MAX] = "";
if (realpath(output, buffer) == NULL) {
fprintf(stderr, "err: Could not find find src dir %s", output);
exit(EXIT_FAILURE);
}
if (type == CakeEU) {
strcat(buffer, "/cake_eu.inc.c");
}
else {
strcat(buffer, "/cake.inc.c");
}
FILE *cFile = fopen(buffer, "w");
const char *euSuffx = "";
if (type == CakeEU) {
euSuffx = "eu_";
}
int numTiles = TABLE_DIMENSIONS[type].cols * TABLE_DIMENSIONS[type].rows;
for (int i = 0; i < numTiles; ++i) {
fprintf(cFile, "ALIGNED8 static const u8 cake_end_texture_%s%d[] = {\n", euSuffx, i);
print_raw_data(cFile, &tiles[i]);
fputs("};\n\n", cFile);
}
fclose(cFile);
}
// input: the skybox tiles + the table = up to 64 32x32 images (rgba16) + 80 pointers (u32)
// some pointers point to duplicate entries
void combine_skybox(const char *input, const char *output) {
enum { W = 10, H = 8, W2 = 8 };
FILE *file = fopen(input, "rb");
if (!file) goto fail;
if (fseek(file, 0, SEEK_END)) goto fail;
ssize_t fileSize = ftell(file);
if (fileSize < 8*10*4) goto fail;
rewind(file);
size_t tableIndex = fileSize - 8*10*4;
if (tableIndex % (32*32*2) != 0) goto fail;
// there are at most 64 tiles before the table
rgba *tiles[8*8];
size_t tileIndex = 0;
for (size_t pos = 0; pos < tableIndex; pos += 32*32*2) {
uint8_t buf[32*32*2];
if (fread(buf, sizeof(buf), 1, file) != 1) goto fail;
tiles[tileIndex] = raw2rgba(buf, 32, 32, 16);
tileIndex++;
}
uint32_t table[W*H];
if (fread(table, sizeof(table), 1, file) != 1) goto fail;
reverse_endian((unsigned char *) table, W*H*4);
uint32_t base = table[0];
for (int i = 0; i < W*H; i++) {
table[i] -= base;
}
// Convert the 256x256 skybox to an editable 248x248 image by skipping the duplicated rows and columns
// every 32nd column is a repeat of the 33rd, and
// every 32nd row is a repeat of the 33rd, EXCEPT for the last row, but that only matters when
// expanding the tiles
rgba combined[31*H * 31*W2];
for (int i = 0; i < H; i++) {
for (int j = 0; j < W2; j++) {
int index = table[i*W+j] / 0x800;
for (int y = 0; y < 31; y++) {
for (int x = 0; x < 31; x++) {
combined[(i*31 + y) * (31*W2) + (j*31 + x)] = tiles[index][y*32 + x];
}
}
}
}
if (!rgba2png(output, combined, 31*W2, 31*H)) {
fprintf(stderr, "Failed to write skybox image.\n");
exit(1);
}
return;
fail:
fprintf(stderr, "Failed to read skybox binary.\n");
exit(1);
}
void combine_cakeimg(const char *input, const char *output, bool eu) {
int W, H, SMALLH, SMALLW;
if (eu) {
W = 5;
H = 7;
SMALLH = 32;
SMALLW = 64;
} else {
W = 4;
H = 12;
SMALLH = 20;
SMALLW = 80;
}
FILE *file = fopen(input, "rb");
if (!file) goto fail;
rgba *combined;
if (!eu) {
combined = malloc((SMALLH-1)*H * (SMALLW-1)*W * sizeof(rgba));
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
//Read the full tile
uint8_t buf[SMALLH * SMALLW * 2];
if (fread(buf, sizeof(buf), 1, file) != 1) goto fail;
rgba *tile = raw2rgba(buf, SMALLH, SMALLW, 16);
//Only write the unique parts of each tile
for (int y = 0; y < SMALLH - 1; y++) {
for (int x = 0; x < SMALLW - 1; x++) {
combined[(i*(SMALLH-1) + y) * (SMALLW-1)*W + (j*(SMALLW-1) + x)] = tile[y*(SMALLW) + x];
}
}
}
}
if (!rgba2png(output, combined, (SMALLW-1)*W, (SMALLH-1)*H)) {
fprintf(stderr, "Failed to write cake image.\n");
exit(1);
}
}
else {
combined = malloc(SMALLH*H * SMALLW*W * sizeof(rgba));
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
uint8_t buf[SMALLH * SMALLW * 2];
if (fread(buf, sizeof(buf), 1, file) != 1) goto fail;
rgba *tile = raw2rgba(buf, SMALLH, SMALLW, 16);
for (int y = 0; y < SMALLH; y++) {
for (int x = 0; x < SMALLW; x++) {
combined[(i*SMALLH + y) * SMALLW*W + (j*SMALLW + x)] = tile[y*SMALLW + x];
}
}
}
}
if (!rgba2png(output, combined, SMALLW*W, SMALLH*H)) {
fprintf(stderr, "Failed to write cake image.\n");
exit(1);
}
}
return;
fail:
fprintf(stderr, "Failed to read cake binary.\n");
exit(1);
}
// Modified from n64split
static void usage() {
fprintf(stderr,
"Usage: %s --type sky|cake|cake_eu {--combine INPUT OUTPUT | --split INPUT OUTPUT}\n"
"\n"
"Optional arguments:\n"
" --write-tiles OUTDIR Also create the individual tiles' PNG files\n", programName);
}
// Modified from n64split
static int parse_arguments(int argc, char *argv[]) {
programName = argv[0];
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "--combine") == 0) {
if (++i >= argc || mode != InvalidMode) {
goto invalid;
}
mode = Combine;
input = argv[i];
if (++i >= argc) {
goto invalid;
}
output = argv[i];
}
if (strcmp(argv[i], "--split") == 0) {
if (++i >= argc || mode != InvalidMode) {
goto invalid;
}
mode = Split;
input = argv[i];
if (++i >= argc) {
goto invalid;
}
output = argv[i];
}
if (strcmp(argv[i], "--type") == 0) {
if (++i >= argc || type != InvalidType) {
goto invalid;
}
if (strcmp(argv[i], "sky") == 0) {
type = Skybox;
} else if(strcmp(argv[i], "cake-eu") == 0) {
type = CakeEU;
} else if(strcmp(argv[i], "cake") == 0) {
type = Cake;
}
}
if (strcmp(argv[i], "--write-tiles") == 0) {
if (++i >= argc || argv[i][0] == '-') {
goto invalid;
}
writeTiles = true;
writeDir = argv[i];
}
}
return 1;
invalid:
usage();
return 0;
}
bool imageMatchesDimensions(int width, int height) {
bool matchesDimensions = false;
for (int expand = false; expand <= true; ++expand) {
if (width == IMAGE_PROPERTIES[type][expand].imageWidth &&
height == IMAGE_PROPERTIES[type][expand].imageHeight) {
matchesDimensions = true;
expanded = expand;
break;
}
}
if (!matchesDimensions) {
if (type != CakeEU) {
fprintf(stderr, "err: That type of image must be either %d x %d or %d x %d. Yours is %d x %d.\n",
IMAGE_PROPERTIES[type][false].imageWidth, IMAGE_PROPERTIES[type][false].imageHeight,
IMAGE_PROPERTIES[type][true].imageWidth, IMAGE_PROPERTIES[type][true].imageHeight,
width, height);
}
else {
fprintf(stderr, "err: That type of image must be %d x %d. Yours is %d x %d.\n",
IMAGE_PROPERTIES[type][true].imageWidth, IMAGE_PROPERTIES[type][true].imageHeight,
width, height);
}
return false;
}
if (type == CakeEU) {
expanded = true;
}
return true;
}
int main(int argc, char *argv[]) {
if (parse_arguments(argc, argv) == false) {
return EXIT_FAILURE;
}
if (type == Skybox && mode == Split) {
// Extract the skybox's name (ie: bbh, bidw) from the input png
char *base = basename(input);
strcpy(skyboxName, base);
char *extension = strrchr(skyboxName, '.');
if (extension) *extension = '\0';
}
switch (mode) {
case Combine:
switch (type) {
case Skybox:
combine_skybox(input, output);
break;
case Cake:
combine_cakeimg(input, output, 0);
break;
case CakeEU:
combine_cakeimg(input, output, 1);
break;
default:
usage();
return EXIT_FAILURE;
break;
}
break;
case Split: {
int width, height;
rgba *image = png2rgba(input, &width, &height);
if (image == NULL) {
fprintf(stderr, "err: Could not load image %s\n", argv[1]);
return EXIT_FAILURE;
}
if (!imageMatchesDimensions(width, height)) {
return EXIT_FAILURE;
}
allocate_tiles();
init_tiles(image, expanded);
switch (type) {
case Skybox:
assign_tile_positions();
write_skybox_c();
break;
case Cake:
case CakeEU:
assign_tile_positions();
write_cake_c();
break;
default:
fprintf(stderr, "err: Unknown image type.\n");
return EXIT_FAILURE;
break;
}
if (writeTiles) {
write_tiles();
}
free_tiles();
free(image);
} break;
default:
usage();
return EXIT_FAILURE;
break;
}
return EXIT_SUCCESS;
}