-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconvert_file.c
820 lines (731 loc) · 21 KB
/
convert_file.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
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
// SPDX-License-Identifier: GPL-3.0-or-later
/* glide64_cache_extract, Glide64 TexCache Extraction tool for debugging
*
* SPDX-FileCopyrightText: Sven Eckelmann <[email protected]>
*/
#include "glide64_cache_extract.h"
#include <errno.h>
#include <inttypes.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#define DDSD_CAPS 0x00000001U
#define DDSD_HEIGHT 0x00000002U
#define DDSD_WIDTH 0x00000004U
#define DDSD_PITCH 0x00000008U
#define DDSD_PIXELFORMAT 0x00001000U
#define DDSD_LINEARSIZE 0x00080000U
#define DDPF_ALPHAPIXELS 0x00000001U
#define DDPF_FOURCC 0x00000004U
#define DDPF_RGB 0x00000040U
#define DDPF_LUMINANCE 0x00000040U
#define DDSCAPS_TEXTURE 0x00001000U
typedef struct {
uint32_t dwSize;
uint32_t dwFlags;
uint32_t dwFourCC;
uint32_t dwRGBBitCount;
uint32_t dwRBitMask;
uint32_t dwGBitMask;
uint32_t dwBBitMask;
uint32_t dwABitMask;
} DDS_PIXELFORMAT;
typedef struct {
uint32_t dwMagic;
uint32_t dwSize;
uint32_t dwFlags;
uint32_t dwHeight;
uint32_t dwWidth;
uint32_t dwPitchOrLinearSize;
uint32_t dwDepth;
uint32_t dwMipMapCount;
uint32_t dwReserved1[11];
DDS_PIXELFORMAT ddspf;
uint32_t dwCaps;
uint32_t dwCaps2;
uint32_t dwCaps3;
uint32_t dwCaps4;
uint32_t dwReserved2;
} DDS_HEADER;
#pragma pack(push, 1)
struct bmp_header {
int16_t identifier;
uint32_t filesize;
uint32_t reserved;
uint32_t dataofs;
uint32_t headersize;
uint32_t width;
uint32_t height;
int16_t planes;
int16_t bitperpixel;
uint32_t compression;
uint32_t datasize;
uint32_t hresolution;
uint32_t vresolution;
uint32_t colors;
uint32_t importantcolors;
};
struct bmp_header_v5 {
int16_t identifier;
uint32_t filesize;
uint32_t reserved;
uint32_t dataofs;
uint32_t headersize;
uint32_t width;
uint32_t height;
uint16_t planes;
uint16_t bitperpixel;
uint32_t compression;
uint32_t datasize;
uint32_t hresolution;
uint32_t vresolution;
uint32_t colors;
uint32_t importantcolors;
uint32_t redmask;
uint32_t greenmask;
uint32_t bluemask;
uint32_t alphamask;
uint32_t colorspace;
uint32_t ciexyz_red_x;
uint32_t ciexyz_red_y;
uint32_t ciexyz_red_z;
uint32_t ciexyz_green_x;
uint32_t ciexyz_green_y;
uint32_t ciexyz_green_z;
uint32_t ciexyz_blue_x;
uint32_t ciexyz_blue_y;
uint32_t ciexyz_blue_z;
uint32_t gamma_red;
uint32_t gamma_green;
uint32_t gamma_blue;
uint32_t intent;
uint32_t profiledata_offset;
uint32_t profiledata_size;
uint32_t reserved2;
};
#pragma pack(pop)
static size_t image_content_length(const struct glide64_file *file)
{
#define BALIGN(x, a) (((x) + (a)) & ~(a))
size_t size;
switch (file->format & ~GR_TEXFMT_GZ) {
case GR_TEXFMT_ALPHA_8:
case GR_TEXFMT_INTENSITY_8:
case GR_TEXFMT_ALPHA_INTENSITY_44:
case GR_TEXFMT_P_8:
size = file->width * file->height;
break;
case GR_TEXFMT_RGB_565:
case GR_TEXFMT_ARGB_1555:
case GR_TEXFMT_ARGB_4444:
case GR_TEXFMT_ALPHA_INTENSITY_88:
size = file->width * file->height * 2;
break;
case GR_TEXFMT_ARGB_CMP_FXT1:
size = (BALIGN(file->width, 7U) * BALIGN(file->height, 3U)) / 2;
break;
case GR_TEXFMT_ARGB_8888:
size = file->width * file->height * 4;
break;
case GR_TEXFMT_ARGB_CMP_DXT1:
size = BALIGN(file->width, 3U) * BALIGN(file->height, 3U);
break;
case GR_TEXFMT_ARGB_CMP_DXT3:
case GR_TEXFMT_ARGB_CMP_DXT5:
size = BALIGN(file->width, 3U) * BALIGN(file->height, 3U) * 2;
break;
default:
size = 0;
fprintf(stderr, "Unsupported format %#"PRIx16"\n", file->format);
break;
}
return size;
#undef BALIGN
}
static int resize_image_dds(struct glide64_file *file)
{
DDS_HEADER *header;
size_t header_size = 128;
void *buf;
buf = malloc(file->size + header_size);
if (!buf) {
fprintf(stderr, "Memory for DDS file couldn't be allocated\n");
return -ENOMEM;
}
header = (DDS_HEADER *)buf;
memset(header, 0, header_size);
header->dwMagic = htole32(0x20534444U);
header->dwSize = htole32(124);
header->dwFlags = htole32(DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT);
header->dwHeight = htole32(file->height);
header->dwWidth = htole32(file->width);
header->dwMipMapCount = htole32(1);
header->dwCaps = htole32(DDSCAPS_TEXTURE);
header->ddspf.dwSize = htole32(32);
switch (file->format) {
case GR_TEXFMT_ALPHA_8:
header->dwFlags |= DDSD_PITCH;
header->dwPitchOrLinearSize = htole32(file->width);
header->ddspf.dwFlags = htole32(DDPF_ALPHAPIXELS);
header->ddspf.dwRGBBitCount = htole32(8);
header->ddspf.dwABitMask = htole32(0xffU);
break;
case GR_TEXFMT_INTENSITY_8:
header->dwFlags |= DDSD_PITCH;
header->dwPitchOrLinearSize = htole32(file->width);
header->ddspf.dwFlags = htole32(DDPF_LUMINANCE);
header->ddspf.dwRGBBitCount = htole32(8);
header->ddspf.dwRBitMask = htole32(0xffU);
break;
case GR_TEXFMT_ALPHA_INTENSITY_44:
header->dwFlags |= DDSD_PITCH;
header->dwPitchOrLinearSize = htole32(file->width);
header->ddspf.dwFlags = htole32(DDPF_ALPHAPIXELS | DDPF_LUMINANCE);
header->ddspf.dwRGBBitCount = htole32(8);
header->ddspf.dwRBitMask = htole32(0x0fU);
header->ddspf.dwABitMask = htole32(0xf0U);
break;
case GR_TEXFMT_P_8:
fprintf(stderr, "Unsupported format GR_TEXFMT_P_8\n");
free(buf);
return -EPERM;
case GR_TEXFMT_RGB_565:
header->dwFlags |= DDSD_PITCH;
header->dwPitchOrLinearSize = htole32(file->width * 2);
header->ddspf.dwFlags = htole32(DDPF_RGB);
header->ddspf.dwRGBBitCount = htole32(16);
header->ddspf.dwRBitMask = htole32(0xf800U);
header->ddspf.dwGBitMask = htole32(0x07e0U);
header->ddspf.dwBBitMask = htole32(0x001fU);
break;
case GR_TEXFMT_ARGB_1555:
header->dwFlags |= DDSD_PITCH;
header->dwPitchOrLinearSize = htole32(file->width * 2);
header->ddspf.dwFlags = htole32(DDPF_ALPHAPIXELS | DDPF_RGB);
header->ddspf.dwRGBBitCount = htole32(16);
header->ddspf.dwRBitMask = htole32(0x7c00U);
header->ddspf.dwGBitMask = htole32(0x03e0U);
header->ddspf.dwBBitMask = htole32(0x001fU);
header->ddspf.dwABitMask = htole32(0x8000U);
break;
case GR_TEXFMT_ARGB_4444:
header->dwFlags |= DDSD_PITCH;
header->dwPitchOrLinearSize = htole32(file->width * 2);
header->ddspf.dwFlags = htole32(DDPF_ALPHAPIXELS | DDPF_RGB);
header->ddspf.dwRGBBitCount = htole32(16);
header->ddspf.dwRBitMask = htole32(0x7c00U);
header->ddspf.dwGBitMask = htole32(0x03e0U);
header->ddspf.dwBBitMask = htole32(0x001fU);
header->ddspf.dwABitMask = htole32(0x8000U);
break;
case GR_TEXFMT_ALPHA_INTENSITY_88:
header->dwFlags |= DDSD_PITCH;
header->dwPitchOrLinearSize = htole32(file->width * 2);
header->ddspf.dwFlags = htole32(DDPF_ALPHAPIXELS | DDPF_LUMINANCE);
header->ddspf.dwRGBBitCount = htole32(16);
header->ddspf.dwRBitMask = htole32(0x00ffU);
header->ddspf.dwABitMask = htole32(0xff00U);
break;
case GR_TEXFMT_ARGB_CMP_FXT1:
fprintf(stderr, "Unsupported format GR_TEXFMT_ARGB_CMP_FXT1\n");
free(buf);
return -EPERM;
case GR_TEXFMT_ARGB_8888:
header->dwFlags |= DDSD_PITCH;
header->dwPitchOrLinearSize = htole32(file->width * 4);
header->ddspf.dwFlags = htole32(DDPF_ALPHAPIXELS | DDPF_RGB);
header->ddspf.dwFourCC = htole32(0);
header->ddspf.dwRGBBitCount = htole32(32);
header->ddspf.dwRBitMask = htole32(0x00ff0000U);
header->ddspf.dwGBitMask = htole32(0x0000ff00U);
header->ddspf.dwBBitMask = htole32(0x000000ffU);
header->ddspf.dwABitMask = htole32(0xff000000U);
break;
case GR_TEXFMT_ARGB_CMP_DXT1:
header->dwFlags |= DDSD_LINEARSIZE;
header->dwPitchOrLinearSize = htole32(file->size);
header->ddspf.dwFlags = htole32(DDPF_FOURCC);
header->ddspf.dwFourCC = htole32(0x31545844U);
header->ddspf.dwRGBBitCount = htole32(24);
header->ddspf.dwRBitMask = htole32(0x00ff0000U);
header->ddspf.dwGBitMask = htole32(0x0000ff00U);
header->ddspf.dwBBitMask = htole32(0x000000ffU);
break;
case GR_TEXFMT_ARGB_CMP_DXT3:
header->dwFlags |= DDSD_LINEARSIZE;
header->dwPitchOrLinearSize = htole32(file->size);
header->ddspf.dwFlags = htole32(DDPF_FOURCC);
header->ddspf.dwFourCC = htole32(0x33545844U);
header->ddspf.dwRGBBitCount = htole32(24);
header->ddspf.dwRBitMask = htole32(0x00ff0000U);
header->ddspf.dwGBitMask = htole32(0x0000ff00U);
header->ddspf.dwBBitMask = htole32(0x000000ffU);
break;
case GR_TEXFMT_ARGB_CMP_DXT5:
header->dwFlags |= DDSD_LINEARSIZE;
header->dwPitchOrLinearSize = htole32(file->size);
header->ddspf.dwFlags = htole32(DDPF_FOURCC);
header->ddspf.dwFourCC = htole32(0x35545844U);
header->ddspf.dwRGBBitCount = htole32(24);
header->ddspf.dwRBitMask = htole32(0x00ff0000U);
header->ddspf.dwGBitMask = htole32(0x0000ff00U);
header->ddspf.dwBBitMask = htole32(0x000000ffU);
break;
default:
fprintf(stderr, "Unsupported format %x\n", file->format);
free(buf);
return -EPERM;
}
memcpy((uint8_t *)buf + header_size, file->data, file->size);
free(file->data);
file->data = buf;
file->size += header_size;
return 0;
}
static int resize_image_bmp(struct glide64_file *file)
{
struct bmp_header *header;
struct bmp_header_v5 *header_v5;
uint32_t header_size;
void *buf;
uint8_t *imagedata;
size_t line_size = file->width * 4;
uint32_t i;
if (file->format != GR_TEXFMT_ARGB_8888) {
fprintf(stderr, "Unsupported texture format %#x for bmp export\n", file->format);
return -EPERM;
}
if (globals.bitmapv5)
header_size = (uint32_t)sizeof(*header_v5);
else
header_size = (uint32_t)sizeof(*header);
if (file->size > (UINT32_MAX - header_size)) {
fprintf(stderr, "Too large texture for bmp export\n");
return -EPERM;
}
buf = malloc(file->size + header_size);
if (!buf) {
fprintf(stderr, "Memory for BMP file couldn't be allocated\n");
return -ENOMEM;
}
if (globals.bitmapv5) {
header_v5 = (struct bmp_header_v5 *)buf;
memset(header_v5, 0, header_size);
header_v5->identifier = htole16(0x4d42U);
header_v5->filesize = htole32(file->size + header_size);
header_v5->dataofs = htole32(header_size);
header_v5->headersize = htole32(header_size - 14);
header_v5->width = htole32(file->width);
header_v5->height = htole32(file->height);
header_v5->planes = htole16(1);
header_v5->bitperpixel = htole16(32);
header_v5->compression = htole32(3);
header_v5->datasize = htole32(file->size);
header_v5->hresolution = htole32(2835);
header_v5->vresolution = htole32(2835);
header_v5->colors = htole32(0);
header_v5->importantcolors = htole32(0);
header_v5->redmask = htole32(0x00ff0000U);
header_v5->greenmask = htole32(0x0000ff00U);
header_v5->bluemask = htole32(0x000000ffU);
header_v5->alphamask = htole32(0xff000000U);
header_v5->colorspace = htole32(0x73524742U);
header_v5->ciexyz_red_x = htole32(0x00000000U);
header_v5->ciexyz_red_y = htole32(0x00000000U);
header_v5->ciexyz_red_z = htole32(0xfc1eb854U);
header_v5->ciexyz_green_x = htole32(0x00000000U);
header_v5->ciexyz_green_y = htole32(0x00000000U);
header_v5->ciexyz_green_z = htole32(0xfc666666U);
header_v5->ciexyz_blue_x = htole32(0x00000000U);
header_v5->ciexyz_blue_y = htole32(0x00000000U);
header_v5->ciexyz_blue_z = htole32(0xff28f5c4U);
header_v5->gamma_red = htole32(0);
header_v5->gamma_green = htole32(0);
header_v5->intent = htole32(4);
header_v5->profiledata_offset = htole32(4);
header_v5->profiledata_size = htole32(4);
} else {
header = (struct bmp_header *)buf;
memset(header, 0, header_size);
header->identifier = htole16(0x4d42U);
header->filesize = htole32(file->size + header_size);
header->dataofs = htole32(header_size);
header->headersize = htole32(header_size - 14);
header->width = htole32(file->width);
header->height = htole32(file->height);
header->planes = htole16(1);
header->bitperpixel = htole16(32);
header->compression = htole32(0);
header->datasize = htole32(file->size);
header->hresolution = htole32(2835);
header->vresolution = htole32(2835);
header->colors = htole32(0);
header->importantcolors = htole32(0);
}
imagedata = (uint8_t *)buf + header_size;
for (i = 0; i < file->height; i++) {
uint32_t target_line = i;
uint32_t source_line = file->height - i - 1;
uint8_t *target_pos = imagedata + target_line * line_size;
uint8_t *source_pos = (uint8_t *)file->data;
source_pos += source_line * line_size;
memcpy(target_pos, source_pos, line_size);
}
free(file->data);
file->data = buf;
file->size += header_size;
return 0;
}
static int normalize_image_a8(struct glide64_file *file)
{
uint32_t *buf;
uint8_t *data, raw;
size_t newsize;
size_t pixels, pos;
uint32_t p, a;
pixels = file->width * file->height;
newsize = pixels * 4;
if (newsize > UINT32_MAX)
return -EINVAL;
buf = malloc(newsize);
if (!buf) {
fprintf(stderr, "Memory for A8 image content couldn't be allocated\n");
return -ENOMEM;
}
data = file->data;
for (pos = 0; pos < pixels; pos++) {
raw = data[pos];
a = raw;
p = (a << 24) | (a << 16) | (a << 8) | a;
buf[pos] = htole32(p);
}
free(file->data);
file->data = (uint8_t *)buf;
file->size = (uint32_t)newsize;
file->format = GR_TEXFMT_ARGB_8888;
return 0;
}
static int normalize_image_i8(struct glide64_file *file)
{
uint32_t *buf;
uint8_t *data, raw;
size_t newsize;
size_t pixels, pos;
uint32_t p, i;
pixels = file->width * file->height;
newsize = pixels * 4;
if (newsize > UINT32_MAX)
return -EINVAL;
buf = malloc(newsize);
if (!buf) {
fprintf(stderr, "Memory for I8 image content couldn't be allocated\n");
return -ENOMEM;
}
data = file->data;
for (pos = 0; pos < pixels; pos++) {
raw = data[pos];
i = raw;
p = (i << 24) | (i << 16) | (i << 8) | i;
buf[pos] = htole32(p);
}
free(file->data);
file->data = (uint8_t *)buf;
file->size = (uint32_t)newsize;
file->format = GR_TEXFMT_ARGB_8888;
return 0;
}
static int normalize_image_a4i4(struct glide64_file *file)
{
uint32_t *buf;
uint8_t *data, raw;
size_t newsize;
size_t pixels, pos;
uint32_t p, a, i;
pixels = file->width * file->height;
newsize = pixels * 4;
if (newsize > UINT32_MAX)
return -EINVAL;
buf = malloc(newsize);
if (!buf) {
fprintf(stderr, "Memory for A4I4 image content couldn't be allocated\n");
return -ENOMEM;
}
data = file->data;
for (pos = 0; pos < pixels; pos++) {
raw = data[pos];
i = (raw & 0x0fU) << 4;
i |= i >> 4;
a = (raw & 0xf0U);
a |= a >> 4;
p = (a << 24) | (i << 16) | (i << 8) | i;
buf[pos] = htole32(p);
}
free(file->data);
file->data = (uint8_t *)buf;
file->size = (uint32_t)newsize;
file->format = GR_TEXFMT_ARGB_8888;
return 0;
}
static int normalize_image_p8(struct glide64_file *file __attribute__((unused)))
{
fprintf(stderr, "Unsupported format GR_TEXFMT_P_8\n");
return -EPERM;
}
static int normalize_image_r5g6b5(struct glide64_file *file)
{
uint32_t *buf;
uint16_t *data, raw;
size_t newsize;
size_t pixels, pos;
uint32_t p, r, g, b;
pixels = file->width * file->height;
newsize = pixels * 4;
if (newsize > UINT32_MAX)
return -EINVAL;
buf = malloc(newsize);
if (!buf) {
fprintf(stderr, "Memory for R5G6B5 image content couldn't be allocated\n");
return -ENOMEM;
}
data = (uint16_t *)file->data;
for (pos = 0; pos < pixels; pos++) {
raw = le16toh(data[pos]);
r = (raw & 0xf800U) >> 8;
r |= r >> 5;
g = (raw & 0x07e0U) >> 3;
g |= g >> 6;
b = (raw & 0x001fU) << 3;
b |= b >> 5;
p = (0xffU << 24) | (r << 16) | (g << 8) | b;
buf[pos] = htole32(p);
}
free(file->data);
file->data = (uint8_t *)buf;
file->size = (uint32_t)newsize;
file->format = GR_TEXFMT_ARGB_8888;
return 0;
}
static int normalize_image_a1r5g5b5(struct glide64_file *file)
{
uint32_t *buf;
uint16_t *data, raw;
size_t newsize;
size_t pixels, pos;
uint32_t p, a, r, g, b;
pixels = file->width * file->height;
newsize = pixels * 4;
if (newsize > UINT32_MAX)
return -EINVAL;
buf = malloc(newsize);
if (!buf) {
fprintf(stderr, "Memory for A1R5G5B5 image content couldn't be allocated\n");
return -ENOMEM;
}
data = (uint16_t *)file->data;
for (pos = 0; pos < pixels; pos++) {
raw = le16toh(data[pos]);
a = (raw & 0x8000U) >> 15;
a *= 0xffU;
r = (raw & 0x7c00U) >> 7;
r |= r >> 5;
g = (raw & 0x03e0U) >> 2;
g |= g >> 5;
b = (raw & 0x001fU) << 3;
b |= b >> 5;
p = (a << 24) | (r << 16) | (g << 8) | b;
buf[pos] = htole32(p);
}
free(file->data);
file->data = (uint8_t *)buf;
file->size = (uint32_t)newsize;
file->format = GR_TEXFMT_ARGB_8888;
return 0;
}
static int normalize_image_a4r4g4b4(struct glide64_file *file)
{
uint32_t *buf;
uint16_t *data, raw;
size_t newsize;
size_t pixels, pos;
uint32_t p, a, r, g, b;
pixels = file->width * file->height;
newsize = pixels * 4;
if (newsize > UINT32_MAX)
return -EINVAL;
buf = malloc(newsize);
if (!buf) {
fprintf(stderr, "Memory for A4R4G4B4 image content couldn't be allocated\n");
return -ENOMEM;
}
data = (uint16_t *)file->data;
for (pos = 0; pos < pixels; pos++) {
raw = le16toh(data[pos]);
a = (raw & 0xf000U) >> 4;
a |= a >> 4;
r = (raw & 0x0f00U) >> 4;
r |= r >> 4;
g = (raw & 0x00f0U);
g |= g >> 4;
b = (raw & 0x000fU) << 4;
b |= b >> 4;
p = (a << 24) | (r << 16) | (g << 8) | b;
buf[pos] = htole32(p);
}
free(file->data);
file->data = (uint8_t *)buf;
file->size = (uint32_t)newsize;
file->format = GR_TEXFMT_ARGB_8888;
return 0;
}
static int normalize_image_a8i8(struct glide64_file *file)
{
uint32_t *buf;
uint16_t *data, raw;
size_t newsize;
size_t pixels, pos;
uint32_t p, a, i;
pixels = file->width * file->height;
newsize = pixels * 4;
if (newsize > UINT32_MAX)
return -EINVAL;
buf = malloc(newsize);
if (!buf) {
fprintf(stderr, "Memory for A4R4G4B4 image content couldn't be allocated\n");
return -ENOMEM;
}
data = (uint16_t *)file->data;
for (pos = 0; pos < pixels; pos++) {
raw = le16toh(data[pos]);
a = (raw & 0xff00U) >> 8;
i = (raw & 0x00ffU);
p = (a << 24) | (i << 16) | (i << 8) | i;
buf[pos] = htole32(p);
}
free(file->data);
file->data = (uint8_t *)buf;
file->size = (uint32_t)newsize;
file->format = GR_TEXFMT_ARGB_8888;
return 0;
}
static int resize_image_content(struct glide64_file *file)
{
int ret;
switch (file->format) {
case GR_TEXFMT_ALPHA_8:
ret = normalize_image_a8(file);
if (ret < 0) {
fprintf(stderr, "Error during conversion from ALPHA_8 to ARGB_8888\n");
return ret;
}
return resize_image_bmp(file);
case GR_TEXFMT_INTENSITY_8:
ret = normalize_image_i8(file);
if (ret < 0) {
fprintf(stderr, "Error during conversion from INTENSITY_8 to ARGB_8888\n");
return ret;
}
return resize_image_bmp(file);
case GR_TEXFMT_ALPHA_INTENSITY_44:
ret = normalize_image_a4i4(file);
if (ret < 0) {
fprintf(stderr, "Error during conversion from INTENSITY_44 to ARGB_8888\n");
return ret;
}
return resize_image_bmp(file);
case GR_TEXFMT_P_8:
ret = normalize_image_p8(file);
if (ret < 0) {
fprintf(stderr, "Error during conversion from P_8 to ARGB_8888\n");
return ret;
}
return resize_image_bmp(file);
case GR_TEXFMT_RGB_565:
ret = normalize_image_r5g6b5(file);
if (ret < 0) {
fprintf(stderr, "Error during conversion from RGB_565 to ARGB_8888\n");
return ret;
}
return resize_image_bmp(file);
case GR_TEXFMT_ARGB_1555:
ret = normalize_image_a1r5g5b5(file);
if (ret < 0) {
fprintf(stderr, "Error during conversion from ARGB_1555 to ARGB_8888\n");
return ret;
}
return resize_image_bmp(file);
case GR_TEXFMT_ARGB_4444:
ret = normalize_image_a4r4g4b4(file);
if (ret < 0) {
fprintf(stderr, "Error during conversion from ARGB_4444 to ARGB_8888\n");
return ret;
}
return resize_image_bmp(file);
case GR_TEXFMT_ALPHA_INTENSITY_88:
ret = normalize_image_a8i8(file);
if (ret < 0) {
fprintf(stderr, "Error during conversion from ALPHA_INTENSITY_88 to ARGB_8888\n");
return ret;
}
return resize_image_bmp(file);
case GR_TEXFMT_ARGB_CMP_FXT1:
fprintf(stderr, "Unsupported format GR_TEXFMT_ARGB_CMP_FXT1\n");
return -EPERM;
case GR_TEXFMT_ARGB_8888:
return resize_image_bmp(file);
case GR_TEXFMT_ARGB_CMP_DXT1:
case GR_TEXFMT_ARGB_CMP_DXT3:
case GR_TEXFMT_ARGB_CMP_DXT5:
return resize_image_dds(file);
default:
fprintf(stderr, "Unsupported format %x\n", file->format);
return -EPERM;
}
return 0;
}
int prepare_file(struct glide64_file *file)
{
size_t expected_size;
void *buf;
uLongf destLen;
int ret;
expected_size = image_content_length(file);
if (expected_size > UINT32_MAX)
return -EINVAL;
if (file->format & GR_TEXFMT_GZ) {
destLen = expected_size + 4096;
buf = malloc(destLen);
if (!buf) {
fprintf(stderr, "Memory for uncompressing the file couldn't be allocated\n");
return -ENOMEM;
}
ret = uncompress(buf, &destLen, file->data, file->size);
if (ret != Z_OK) {
free(buf);
fprintf(stderr, "Failure during decompressing\n");
return -EINVAL;
}
if (expected_size != destLen) {
free(buf);
fprintf(stderr, "Decompressed file has wrong filesize\n");
return -EINVAL;
}
file->format &= ~GR_TEXFMT_GZ;
free(file->data);
file->data = buf;
file->size = (uint32_t)expected_size;
} else {
if (expected_size != file->size) {
fprintf(stderr, "Expected size of file is not the actual file size\n");
return -EINVAL;
}
}
ret = resize_image_content(file);
if (ret < 0) {
fprintf(stderr, "Failed to prepare image content\n");
return ret;
}
return 0;
}