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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
|
#include <stdio.h>
#include <stdlib.h>
#if defined(_WIN32) || defined(_WIN64)
#include <string.h>
#else
#include <strings.h>
#endif
#define STBI_NO_LINEAR
#define STBI_NO_HDR
#define STBI_NO_TGA
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>
#include "n64graphics.h"
#include "libmio0/utils.h"
// SCALE_M_N: upscale/downscale M-bit integer to N-bit
#define SCALE_5_8(VAL_) (((VAL_) * 0xFF) / 0x1F)
#define SCALE_8_5(VAL_) ((((VAL_) + 4) * 0x1F) / 0xFF)
#define SCALE_4_8(VAL_) ((VAL_) * 0x11)
#define SCALE_8_4(VAL_) ((VAL_) / 0x11)
#define SCALE_3_8(VAL_) ((VAL_) * 0x24)
#define SCALE_8_3(VAL_) ((VAL_) / 0x24)
typedef struct {
enum {
IMG_FORMAT_RGBA,
IMG_FORMAT_IA,
IMG_FORMAT_I,
IMG_FORMAT_CI,
} format;
int depth;
} img_format;
//---------------------------------------------------------
// N64 RGBA/IA/I/CI -> internal RGBA/IA
//---------------------------------------------------------
rgba* raw2rgba(const uint8_t* raw, int width, int height, int depth) {
rgba* img;
int img_size;
img_size = width * height * sizeof(*img);
img = malloc(img_size);
if (!img) {
ERROR("Error allocating %d bytes\n", img_size);
return NULL;
}
if (depth == 16) {
for (int i = 0; i < width * height; i++) {
img[i].red = SCALE_5_8((raw[i * 2] & 0xF8) >> 3);
img[i].green = SCALE_5_8(((raw[i * 2] & 0x07) << 2) | ((raw[i * 2 + 1] & 0xC0) >> 6));
img[i].blue = SCALE_5_8((raw[i * 2 + 1] & 0x3E) >> 1);
img[i].alpha = (raw[i * 2 + 1] & 0x01) ? 0xFF : 0x00;
}
} else if (depth == 32) {
for (int i = 0; i < width * height; i++) {
img[i].red = raw[i * 4];
img[i].green = raw[i * 4 + 1];
img[i].blue = raw[i * 4 + 2];
img[i].alpha = raw[i * 4 + 3];
}
}
return img;
}
ia* raw2ia(const uint8_t* raw, int width, int height, int depth) {
ia* img;
int img_size;
img_size = width * height * sizeof(*img);
img = malloc(img_size);
if (!img) {
ERROR("Error allocating %u bytes\n", img_size);
return NULL;
}
switch (depth) {
case 16:
for (int i = 0; i < width * height; i++) {
img[i].intensity = raw[i * 2];
img[i].alpha = raw[i * 2 + 1];
}
break;
case 8:
for (int i = 0; i < width * height; i++) {
img[i].intensity = SCALE_4_8((raw[i] & 0xF0) >> 4);
img[i].alpha = SCALE_4_8(raw[i] & 0x0F);
}
break;
case 4:
for (int i = 0; i < width * height; i++) {
uint8_t bits;
bits = raw[i / 2];
if (i % 2) {
bits &= 0xF;
} else {
bits >>= 4;
}
img[i].intensity = SCALE_3_8((bits >> 1) & 0x07);
img[i].alpha = (bits & 0x01) ? 0xFF : 0x00;
}
break;
case 1:
for (int i = 0; i < width * height; i++) {
uint8_t bits;
uint8_t mask;
bits = raw[i / 8];
mask = 1 << (7 - (i % 8)); // MSb->LSb
bits = (bits & mask) ? 0xFF : 0x00;
img[i].intensity = bits;
img[i].alpha = bits;
}
break;
default:
ERROR("Error invalid depth %d\n", depth);
break;
}
return img;
}
ci* raw2ci_torch(const uint8_t* raw, int width, int height, int depth) {
ci* img = NULL;
int img_size;
img_size = width * height * sizeof(*img);
img = malloc(img_size);
if (!img) {
ERROR("Error allocating %u bytes\n", img_size);
return NULL;
}
switch (depth) {
case 8:
for (int i = 0; i < width * height; i++) {
img[i].index = raw[i];
}
break;
case 4:
for (int i = 0; i < width * height; i++) {
int pos = i / 2;
img[i].index = i % 2 ? raw[pos] & 0xF : raw[pos] >> 4;
}
break;
default:
ERROR("Error invalid depth %d\n", depth);
break;
}
return img;
}
ia* raw2i(const uint8_t* raw, int width, int height, int depth) {
ia* img = NULL;
int img_size;
img_size = width * height * sizeof(*img);
img = malloc(img_size);
if (!img) {
ERROR("Error allocating %u bytes\n", img_size);
return NULL;
}
switch (depth) {
case 8:
for (int i = 0; i < width * height; i++) {
img[i].intensity = raw[i];
img[i].alpha = 0xFF;
}
break;
case 4:
for (int i = 0; i < width * height; i++) {
uint8_t bits;
bits = raw[i / 2];
if (i % 2) {
bits &= 0xF;
} else {
bits >>= 4;
}
img[i].intensity = SCALE_4_8(bits);
img[i].alpha = 0xFF;
}
break;
default:
ERROR("Error invalid depth %d\n", depth);
break;
}
return img;
}
// convert CI raw data and palette to raw data (either RGBA16 or IA16)
uint8_t* ci2raw(const uint8_t* rawci, const uint8_t* palette, int width, int height, int ci_depth) {
uint8_t* raw;
int raw_size;
// first convert to raw RGBA
raw_size = sizeof(uint16_t) * width * height;
raw = malloc(raw_size);
if (!raw) {
ERROR("Error allocating %u bytes\n", raw_size);
return NULL;
}
for (int i = 0; i < width * height; i++) {
int pal_idx = rawci[i];
if (ci_depth == 4) {
int byte_idx = i / 2;
int nibble = 1 - (i % 2);
int shift = 4 * nibble;
pal_idx = (rawci[byte_idx] >> shift) & 0xF;
}
raw[2 * i] = palette[2 * pal_idx];
raw[2 * i + 1] = palette[2 * pal_idx + 1];
}
return raw;
}
//---------------------------------------------------------
// internal RGBA/IA -> N64 RGBA/IA/I/CI
// returns length written to 'raw' used or -1 on error
//---------------------------------------------------------
int rgba2raw(uint8_t* raw, const rgba* img, int width, int height, int depth) {
int size = width * height * depth / 8;
INFO("Converting RGBA%d %dx%d to raw\n", depth, width, height);
if (depth == 16) {
for (int i = 0; i < width * height; i++) {
uint8_t r, g, b, a;
r = SCALE_8_5(img[i].red);
g = SCALE_8_5(img[i].green);
b = SCALE_8_5(img[i].blue);
a = img[i].alpha ? 0x1 : 0x0;
raw[i * 2] = (r << 3) | (g >> 2);
raw[i * 2 + 1] = ((g & 0x3) << 6) | (b << 1) | a;
}
} else if (depth == 32) {
for (int i = 0; i < width * height; i++) {
raw[i * 4] = img[i].red;
raw[i * 4 + 1] = img[i].green;
raw[i * 4 + 2] = img[i].blue;
raw[i * 4 + 3] = img[i].alpha;
}
} else {
ERROR("Error invalid depth %d\n", depth);
size = -1;
}
return size;
}
int ia2raw(uint8_t* raw, const ia* img, int width, int height, int depth) {
int size = width * height * depth / 8;
INFO("Converting IA%d %dx%d to raw\n", depth, width, height);
switch (depth) {
case 16:
for (int i = 0; i < width * height; i++) {
raw[i * 2] = img[i].intensity;
raw[i * 2 + 1] = img[i].alpha;
}
break;
case 8:
for (int i = 0; i < width * height; i++) {
uint8_t val = SCALE_8_4(img[i].intensity);
uint8_t alpha = SCALE_8_4(img[i].alpha);
raw[i] = (val << 4) | alpha;
}
break;
case 4:
for (int i = 0; i < width * height; i++) {
uint8_t val = SCALE_8_3(img[i].intensity);
uint8_t alpha = img[i].alpha ? 0x01 : 0x00;
uint8_t old = raw[i / 2];
if (i % 2) {
raw[i / 2] = (old & 0xF0) | (val << 1) | alpha;
} else {
raw[i / 2] = (old & 0x0F) | (((val << 1) | alpha) << 4);
}
}
break;
case 1:
for (int i = 0; i < width * height; i++) {
uint8_t val = img[i].intensity;
uint8_t old = raw[i / 8];
uint8_t bit = 1 << (7 - (i % 8));
if (val) {
raw[i / 8] = old | bit;
} else {
raw[i / 8] = old & (~bit);
}
}
break;
default:
ERROR("Error invalid depth %d\n", depth);
size = -1;
break;
}
return size;
}
int i2raw(uint8_t* raw, const ia* img, int width, int height, int depth) {
int size = width * height * depth / 8;
INFO("Converting I%d %dx%d to raw\n", depth, width, height);
switch (depth) {
case 8:
for (int i = 0; i < width * height; i++) {
raw[i] = img[i].intensity;
}
break;
case 4:
for (int i = 0; i < width * height; i++) {
uint8_t val = SCALE_8_4(img[i].intensity);
uint8_t old = raw[i / 2];
if (i % 2) {
raw[i / 2] = (old & 0xF0) | val;
} else {
raw[i / 2] = (old & 0x0F) | (val << 4);
}
}
break;
default:
ERROR("Error invalid depth %d\n", depth);
size = -1;
break;
}
return size;
}
int ci2raw_torch(uint8_t* raw, const ci* img, int width, int height, int depth) {
int size = width * height * depth / 8;
INFO("Converting I%d %dx%d to raw\n", depth, width, height);
switch (depth) {
case 8:
for (int i = 0; i < width * height; i++) {
raw[i] = img[i].index;
}
break;
case 4:
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x += 2) {
const size_t pos = (y * width + x) / 2;
const uint8_t cR1 = img[y * width + x].index;
const uint8_t cR2 = img[y * width + x + 1].index;
raw[pos] = cR1 << 4 | cR2;
}
}
break;
default:
ERROR("Error invalid depth %d\n", depth);
size = -1;
break;
}
return size;
}
//---------------------------------------------------------
// internal RGBA/IA -> PNG
//---------------------------------------------------------
int rgba2png(unsigned char** png_output, int* size_output, const rgba* img, int width, int height) {
int ret = 0;
// convert to format stb_image_write expects
uint8_t* data = malloc(4 * width * height);
if (data) {
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
int idx = j * width + i;
data[4 * idx] = img[idx].red;
data[4 * idx + 1] = img[idx].green;
data[4 * idx + 2] = img[idx].blue;
data[4 * idx + 3] = img[idx].alpha;
}
}
*png_output = stbi_write_png_to_mem(data, 0, width, height, 4, size_output);
free(data);
}
return ret;
}
int ia2png(unsigned char** png_output, int* size_output, const ia* img, int width, int height) {
int ret = 0;
// convert to format stb_image_write expects
uint8_t* data = malloc(2 * width * height);
if (data) {
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
int idx = j * width + i;
data[2 * idx] = img[idx].intensity;
data[2 * idx + 1] = img[idx].alpha;
}
}
(*png_output) = stbi_write_png_to_mem(data, 0, width, height, 2, size_output);
free(data);
}
return ret;
}
int ci2png(unsigned char** png_output, int* size_output, const ci* img, int width, int height) {
int ret = 0;
// convert to format stb_image_write expects
uint8_t* data = malloc(width * height);
if (data) {
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
int idx = j * width + i;
data[idx] = img[idx].index;
}
}
(*png_output) = stbi_write_plte_png_to_mem(data, 0, width, height, 1, NULL, 0, size_output);
free(data);
}
return ret;
}
//---------------------------------------------------------
// PNG -> internal RGBA/IA
//---------------------------------------------------------
rgba* png2rgba(unsigned char* png_input, int size_input, int* width, int* height) {
rgba* img = NULL;
int w = 0;
int h = 0;
int channels = 0;
int img_size;
stbi_uc* data = stbi_load_from_memory(png_input, size_input, &w, &h, &channels, STBI_default);
if (!data || w <= 0 || h <= 0) {
ERROR("Error loading file\n");
return NULL;
}
INFO("Read %dx%d channels: %d\n", w, h, channels);
img_size = w * h * sizeof(rgba);
img = malloc(img_size);
if (!img) {
ERROR("Error allocating %u bytes\n", img_size);
return NULL;
}
switch (channels) {
case 3: // red, green, blue
case 4: // red, green, blue, alpha
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
int idx = j * w + i;
img[idx].red = data[channels * idx];
img[idx].green = data[channels * idx + 1];
img[idx].blue = data[channels * idx + 2];
if (channels == 4) {
img[idx].alpha = data[channels * idx + 3];
} else {
img[idx].alpha = 0xFF;
}
}
}
break;
case 2: // grey, alpha
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
int idx = j * w + i;
img[idx].red = data[2 * idx];
img[idx].green = data[2 * idx];
img[idx].blue = data[2 * idx];
img[idx].alpha = data[2 * idx + 1];
}
}
break;
default:
ERROR("Don't know how to read channels: %d\n", channels);
free(img);
img = NULL;
}
// cleanup
stbi_image_free(data);
*width = w;
*height = h;
return img;
}
rgb* png2rgb(unsigned char* png_input, int size_input, int* width, int* height) {
rgba* img = NULL;
int w = 0;
int h = 0;
int channels = 0;
int img_size;
stbi_uc* data = stbi_load_from_memory(png_input, size_input, &w, &h, &channels, STBI_rgb);
if (!data || w <= 0 || h <= 0) {
ERROR("Error loading file\n");
return NULL;
}
INFO("Read %dx%d channels: %d\n", w, h, channels);
img_size = w * h * sizeof(*img);
img = malloc(img_size);
if (!img) {
ERROR("Error allocating %u bytes\n", img_size);
return NULL;
}
switch (channels) {
case 3: // red, green, blue
case 4: // red, green, blue
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
int idx = j * w + i;
img[idx].red = data[channels * idx];
img[idx].green = data[channels * idx + 1];
img[idx].blue = data[channels * idx + 2];
if (channels == 4) {
img[idx].alpha = data[channels * idx + 3];
} else {
img[idx].alpha = 0xFF;
}
}
}
break;
case 2: // grey, alpha
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
int idx = j * w + i;
img[idx].red = data[2 * idx];
img[idx].green = data[2 * idx];
img[idx].blue = data[2 * idx];
}
}
break;
default:
ERROR("Don't know how to read channels: %d\n", channels);
free(img);
img = NULL;
}
// cleanup
stbi_image_free(data);
*width = w;
*height = h;
return img;
}
ia* png2ia(unsigned char* png_input, int size_input, int* width, int* height) {
ia* img = NULL;
int w = 0, h = 0;
int channels = 0;
int img_size;
stbi_uc* data = stbi_load_from_memory(png_input, size_input, &w, &h, &channels, STBI_default);
if (!data || w <= 0 || h <= 0) {
ERROR("Error loading file\n");
return NULL;
}
INFO("Read %dx%d channels: %d\n", w, h, channels);
img_size = w * h * sizeof(*img);
img = malloc(img_size);
if (!img) {
ERROR("Error allocating %d bytes\n", img_size);
return NULL;
}
switch (channels) {
case 3: // red, green, blue
case 4: // red, green, blue, alpha
ERROR("Warning: averaging RGB PNG to create IA\n");
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
int idx = j * w + i;
int sum = data[channels * idx] + data[channels * idx + 1] + data[channels * idx + 2];
img[idx].intensity = (sum + 1) / 3; // add 1 to round up where appropriate
if (channels == 4) {
img[idx].alpha = data[channels * idx + 3];
} else {
img[idx].alpha = 0xFF;
}
}
}
break;
case 2: // grey, alpha
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
int idx = j * w + i;
img[idx].intensity = data[2 * idx];
img[idx].alpha = data[2 * idx + 1];
}
}
break;
default:
ERROR("Don't know how to read channels: %d\n", channels);
free(img);
img = NULL;
}
// cleanup
stbi_image_free(data);
*width = w;
*height = h;
return img;
}
ci* png2ci(unsigned char* png_input, int size_input, int* width, int* height) {
ci* img = NULL;
int w = 0, h = 0;
int channels = 0;
int img_size;
stbi_uc* data = stbi_load_from_memory(png_input, size_input, &w, &h, &channels, STBI_default);
if (!data || w <= 0 || h <= 0) {
ERROR("Error loading file\n");
return NULL;
}
INFO("Read %dx%d channels: %d\n", w, h, channels);
img_size = w * h * sizeof(*img);
img = malloc(img_size);
if (!img) {
ERROR("Error allocating %d bytes\n", img_size);
return NULL;
}
switch (channels) {
case 3: // red, green, blue
case 4: // red, green, blue, alpha
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
int idx = j * w + i;
img[idx].index = data[channels * idx];
}
}
break;
case 2: // grey, alpha
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
int idx = j * w + i;
img[idx].index = data[2 * idx];
}
}
break;
default:
ERROR("Don't know how to read channels: %d\n", channels);
free(img);
img = NULL;
}
// cleanup
stbi_image_free(data);
*width = w;
*height = h;
return img;
}
// find index of palette color
// return -1 if not found
static int pal_find_color(const palette_t* pal, uint16_t val) {
for (int i = 0; i < pal->used; i++) {
if (pal->data[i] == val) {
return i;
}
}
return -1;
}
// find value in palette, or add if not there
// returns palette index entered or -1 if palette full
static int pal_add_color(palette_t* pal, uint16_t val) {
int idx;
idx = pal_find_color(pal, val);
if (idx < 0) {
if (pal->used == pal->max) {
ERROR("Error: trying to use more than %d\n", pal->max);
} else {
idx = pal->used;
pal->data[pal->used] = val;
pal->used++;
}
}
return idx;
}
// convert from raw (RGBA16 or IA16) format to CI + palette
// returns 1 on success
int raw2ci(uint8_t* rawci, palette_t* pal, const uint8_t* raw, int raw_len, int ci_depth) {
// assign colors to palette
pal->used = 0;
memset(pal->data, 0, sizeof(pal->data));
int ci_idx = 0;
for (int i = 0; i < raw_len; i += sizeof(uint16_t)) {
uint16_t val = read_u16_be(&raw[i]);
int pal_idx = pal_add_color(pal, val);
if (pal_idx < 0) {
ERROR("Error adding color @ (%d): %d (used: %d/%d)\n", i, pal_idx, pal->used, pal->max);
return 0;
} else {
switch (ci_depth) {
case 8:
rawci[ci_idx] = (uint8_t) pal_idx;
break;
case 4: {
int byte_idx = ci_idx / 2;
int nibble = 1 - (ci_idx % 2);
uint8_t mask = 0xF << (4 * (1 - nibble));
rawci[byte_idx] = (rawci[byte_idx] & mask) | (pal_idx << (4 * nibble));
break;
}
}
ci_idx++;
}
}
return 1;
}
const char* n64graphics_get_read_version(void) {
return "stb_image 2.19";
}
const char* n64graphics_get_write_version(void) {
return "stb_image_write 1.09";
}
|