forked from Based-Skid/iLaunchELF
-
Notifications
You must be signed in to change notification settings - Fork 5
/
textures.c
302 lines (243 loc) · 7.58 KB
/
textures.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
#include <gsKit.h>
#include <malloc.h>
#include <png.h>
#include "include/textures.h"
extern void *background_png;
extern void *background2_png;
extern void *logo_png;
typedef struct
{
int id;
void **texture;
} texture_t;
static texture_t internalTexture[TEXTURES_COUNT] = {
{BACKGROUND, &background_png},
{BACKGROUND2, &background2_png},
{LOGO, &logo_png}
};
static int texSizeValidate(int width, int height, short psm)
{
if (width > 1024 || height > 1024)
return -1;
if (gsKit_texture_size(width, height, psm) > 720*512*4)
return -1;
return 0;
}
typedef struct
{
u8 red;
u8 green;
u8 blue;
u8 alpha;
} png_clut_t;
typedef struct
{
png_colorp palette;
int numPalette;
int numTrans;
png_bytep trans;
png_clut_t *clut;
} png_texture_t;
static png_texture_t pngTexture;
static int texPngEnd(png_structp pngPtr, png_infop infoPtr, int status)
{
if (infoPtr != NULL)
png_destroy_read_struct(&pngPtr, &infoPtr, (png_infopp)NULL);
return status;
}
static void texPngReadMemFunction(png_structp pngPtr, png_bytep data, png_size_t length)
{
void **PngBufferPtr = png_get_io_ptr(pngPtr);
memcpy(data, *PngBufferPtr, length);
*PngBufferPtr = (u8 *)(*PngBufferPtr) + length;
}
static void texPngReadPixels4(GSTEXTURE *texture, png_bytep *rowPointers)
{
unsigned char *pixel = (unsigned char *)texture->Mem;
png_clut_t *clut = (png_clut_t *)texture->Clut;
int i, j, k = 0;
for (i = pngTexture.numPalette; i < 16; i++) {
memset(&clut[i], 0, sizeof(clut[i]));
}
for (i = 0; i < pngTexture.numPalette; i++) {
clut[i].red = pngTexture.palette[i].red;
clut[i].green = pngTexture.palette[i].green;
clut[i].blue = pngTexture.palette[i].blue;
clut[i].alpha = 0x80;
}
for (i = 0; i < pngTexture.numTrans; i++)
clut[i].alpha = pngTexture.trans[i] >> 1;
for (i = 0; i < texture->Height; i++) {
for (j = 0; j < texture->Width / 2; j++) {
memcpy(&pixel[k], &rowPointers[i][1 * j], 1);
pixel[k] = (pixel[k] << 4) | (pixel[k] >> 4);
k++;
}
}
}
static void texPngReadPixels8(GSTEXTURE *texture, png_bytep *rowPointers)
{
unsigned char *pixel = (unsigned char *)texture->Mem;
png_clut_t *clut = (png_clut_t *)texture->Clut;
int i, j, k = 0;
for (i = pngTexture.numPalette; i < 256; i++) {
memset(&clut[i], 0, sizeof(clut[i]));
}
for (i = 0; i < pngTexture.numPalette; i++) {
clut[i].red = pngTexture.palette[i].red;
clut[i].green = pngTexture.palette[i].green;
clut[i].blue = pngTexture.palette[i].blue;
clut[i].alpha = 0x80;
}
for (i = 0; i < pngTexture.numTrans; i++)
clut[i].alpha = pngTexture.trans[i] >> 1;
// rotate clut
for (i = 0; i < pngTexture.numPalette; i++) {
if ((i&0x18) == 8) {
png_clut_t tmp = clut[i];
clut[i] = clut[i+8];
clut[i+8] = tmp;
}
}
for (i = 0; i < texture->Height; i++) {
for (j = 0; j < texture->Width; j++) {
memcpy(&pixel[k++], &rowPointers[i][1 * j], 1);
}
}
}
static void texPngReadPixels24(GSTEXTURE *texture, png_bytep *rowPointers)
{
struct pixel3
{
unsigned char r, g, b;
};
struct pixel3 *Pixels = (struct pixel3 *)texture->Mem;
int i, j, k = 0;
for (i = 0; i < texture->Height; i++) {
for (j = 0; j < texture->Width; j++) {
memcpy(&Pixels[k++], &rowPointers[i][4 * j], 3);
}
}
}
static void texPngReadPixels32(GSTEXTURE *texture, png_bytep *rowPointers)
{
struct pixel
{
unsigned char r, g, b, a;
};
struct pixel *Pixels = (struct pixel *)texture->Mem;
int i, j, k = 0;
for (i = 0; i < texture->Height; i++) {
for (j = 0; j < texture->Width; j++) {
memcpy(&Pixels[k], &rowPointers[i][4 * j], 3);
Pixels[k++].a = rowPointers[i][4 * j + 3] >> 1;
}
}
}
static void texPngReadData(GSTEXTURE *texture, png_structp pngPtr, png_infop infoPtr,
void (*texPngReadPixels)(GSTEXTURE *texture, png_bytep *rowPointers))
{
int row, rowBytes = png_get_rowbytes(pngPtr, infoPtr);
size_t size = gsKit_texture_size_ee(texture->Width, texture->Height, texture->PSM);
texture->Mem = memalign(128, size);
// failed allocation
if (!texture->Mem) {
printf("TEXTURES PngReadData: Failed to allocate %d bytes\n", size);
return;
}
png_bytep *rowPointers = calloc(texture->Height, sizeof(png_bytep));
for (row = 0; row < texture->Height; row++) {
rowPointers[row] = malloc(rowBytes);
}
png_read_image(pngPtr, rowPointers);
texPngReadPixels(texture, rowPointers);
for (row = 0; row < texture->Height; row++)
free(rowPointers[row]);
free(rowPointers);
png_read_end(pngPtr, NULL);
}
int texPngLoad(GSTEXTURE *texture, int texID)
{
png_structp pngPtr = NULL;
png_infop infoPtr = NULL;
png_voidp readData = NULL;
png_rw_ptr readFunction = NULL;
void **PngFileBufferPtr;
texture->ClutPSM = 0;
texture->Filter = GS_FILTER_LINEAR;
texture->Mem = NULL;
texture->Vram = 0;
texture->VramClut = 0;
texture->Clut = NULL;
if (!internalTexture[texID].texture)
return -1;
PngFileBufferPtr = internalTexture[texID].texture;
readData = &PngFileBufferPtr;
readFunction = &texPngReadMemFunction;
pngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, NULL, NULL);
if (!pngPtr)
return texPngEnd(pngPtr, infoPtr, -2);
infoPtr = png_create_info_struct(pngPtr);
if (!infoPtr)
return texPngEnd(pngPtr, infoPtr, -3);
if (setjmp(png_jmpbuf(pngPtr)))
return texPngEnd(pngPtr, infoPtr, -4);
png_set_read_fn(pngPtr, readData, readFunction);
unsigned int sigRead = 0;
png_set_sig_bytes(pngPtr, sigRead);
png_read_info(pngPtr, infoPtr);
png_uint_32 pngWidth, pngHeight;
int bitDepth, colorType, interlaceType;
png_get_IHDR(pngPtr, infoPtr, &pngWidth, &pngHeight, &bitDepth, &colorType, &interlaceType, NULL, NULL);
texture->Width = pngWidth;
texture->Height = pngHeight;
if (colorType == PNG_COLOR_TYPE_GRAY)
png_set_expand(pngPtr);
if (bitDepth == 16)
png_set_strip_16(pngPtr);
png_set_filler(pngPtr, 0x80, PNG_FILLER_AFTER);
png_read_update_info(pngPtr, infoPtr);
void (*texPngReadPixels)(GSTEXTURE * texture, png_bytep * rowPointers);
switch (png_get_color_type(pngPtr, infoPtr)) {
case PNG_COLOR_TYPE_RGB_ALPHA:
texture->PSM = GS_PSM_CT32;
texPngReadPixels = &texPngReadPixels32;
break;
case PNG_COLOR_TYPE_RGB:
texture->PSM = GS_PSM_CT24;
texPngReadPixels = &texPngReadPixels24;
break;
case PNG_COLOR_TYPE_PALETTE:
pngTexture.palette = NULL;
pngTexture.numPalette = 0;
pngTexture.trans = NULL;
pngTexture.numTrans = 0;
png_get_PLTE(pngPtr, infoPtr, &pngTexture.palette, &pngTexture.numPalette);
png_get_tRNS(pngPtr, infoPtr, &pngTexture.trans, &pngTexture.numTrans, NULL);
texture->ClutPSM = GS_PSM_CT32;
if (bitDepth == 4) {
texture->PSM = GS_PSM_T4;
texture->Clut = memalign(128, gsKit_texture_size_ee(8, 2, GS_PSM_CT32));
memset(texture->Clut, 0, gsKit_texture_size_ee(8, 2, GS_PSM_CT32));
texPngReadPixels = &texPngReadPixels4;
}
else {
texture->PSM = GS_PSM_T8;
texture->Clut = memalign(128, gsKit_texture_size_ee(16, 16, GS_PSM_CT32));
memset(texture->Clut, 0, gsKit_texture_size_ee(16, 16, GS_PSM_CT32));
texPngReadPixels = &texPngReadPixels8;
}
break;
default:
return texPngEnd(pngPtr, infoPtr, -6);
}
if (texSizeValidate(texture->Width, texture->Height, texture->PSM) < 0) {
if (texture->Clut) {
free(texture->Clut);
texture->Clut = NULL;
}
return texPngEnd(pngPtr, infoPtr, -7);
}
texPngReadData(texture, pngPtr, infoPtr, texPngReadPixels);
return texPngEnd(pngPtr, infoPtr, 0);
}