-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSegaPVRImage.c
332 lines (276 loc) · 9.19 KB
/
SegaPVRImage.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
//
// SegaPVRImage.c
// SEGAPVR
//
// Created by Yevgeniy Logachev on 4/13/14.
// Copyright (c) 2014 yev. All rights reserved.
//
#include "SegaPVRImage.h"
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <math.h>
// Twiddle
#define kTwiddleTableSize 1024
unsigned long int gTwiddledTable[kTwiddleTableSize];
unsigned long int GetUntwiddledTexelPosition(unsigned long int x, unsigned long int y);
int MipMapsCountFromWidth(unsigned long int width);
// RGBA Utils
void TexelToRGBA( unsigned short int srcTexel, enum TextureFormatMasks srcFormat, unsigned char *r, unsigned char *g, unsigned char *b, unsigned char *a);
unsigned int ToUint16(unsigned char* value)
{
return (value[0] | (value[1] << 8));
}
int LoadPVRFromFile(const char* filename, unsigned char** image, unsigned long int* imageSize, struct PVRTHeader* outPvrtHeader)
{
FILE* pFile = fopen(filename, "rb");
if (pFile == 0)
{
return 0;
}
fseek(pFile, 0, SEEK_END); // seek to end of file
size_t fsize = ftell(pFile); // get current file pointer
fseek(pFile, 0, SEEK_SET);
unsigned char* buff = (unsigned char*)malloc(fsize);
fread(buff, fsize, 1, pFile);
fclose(pFile);
unsigned char* srcPtr = buff;
struct PVRTHeader pvrtHeader;
unsigned int offset = ReadPVRHeader(srcPtr, &pvrtHeader);
if (offset == 0)
{
free(buff);
return 0;
}
if (outPvrtHeader)
{
*outPvrtHeader = pvrtHeader;
}
printf("Image size: (%d, %d)\n", pvrtHeader.width, pvrtHeader.height);
srcPtr += offset;
*imageSize = pvrtHeader.width * pvrtHeader.height * 4; // RGBA8888
*image = (unsigned char *)malloc(*imageSize);
memset(*image, 0, *imageSize);
DecodePVR(srcPtr, &pvrtHeader, *image);
free(buff);
return 1;
}
unsigned int ReadPVRHeader(unsigned char* srcData, struct PVRTHeader* pvrtHeader)
{
unsigned int offset = 0;
struct GBIXHeader gbixHeader;
if (strncmp((char*)srcData, "GBIX", 4) == 0)
{
memcpy(&gbixHeader, srcData, sizeof(struct GBIXHeader));
offset += sizeof(struct GBIXHeader);
}
if (strncmp((char*)srcData + offset, "PVRT", 4) == 0)
{
memcpy(pvrtHeader, srcData + offset, sizeof(struct PVRTHeader));
offset += sizeof(struct PVRTHeader);
}
return offset;
}
int DecodePVR(unsigned char* srcData, const struct PVRTHeader* pvrtHeader, unsigned char* dstData)
{
const unsigned int kSrcStride = sizeof(unsigned short int);
const unsigned int kDstStride = sizeof(unsigned int); // RGBA8888
enum TextureFormatMasks srcFormat = (enum TextureFormatMasks)(pvrtHeader->textureAttributes & 0xFF);
// Unpack data
bool isTwiddled = false;
bool isMipMaps = false;
bool isVQCompressed = false;
unsigned int codeBookSize = 0;
switch((pvrtHeader->textureAttributes >> 8) & 0xFF)
{
case TTM_TwiddledMipMaps:
isMipMaps = true;
case TTM_Twiddled:
case TTM_TwiddledNonSquare:
isTwiddled = true;
break;
case TTM_VectorQuantizedMipMaps:
isMipMaps = true;
case TTM_VectorQuantized:
isVQCompressed = true;
codeBookSize = 256;
break;
case TTM_VectorQuantizedCustomCodeBookMipMaps:
isMipMaps = true;
case TTM_VectorQuantizedCustomCodeBook:
isVQCompressed = true;
codeBookSize = pvrtHeader->width;
if(codeBookSize < 16)
{
codeBookSize = 16;
}
else if(codeBookSize == 64)
{
codeBookSize = 128;
}
else
{
codeBookSize = 256;
}
break;
case TTM_Raw:
case TTM_RawNonSquare:
break;
default:
return 0;
break;
}
const unsigned int numCodedComponents = 4;
unsigned char* srcVQ = 0;
if (isVQCompressed)
{
srcVQ = srcData;
srcData += numCodedComponents * kSrcStride * codeBookSize;
}
unsigned int mipWidth = 0;
unsigned int mipHeight = 0;
unsigned int mipSize = 0;
// skip mipmaps
int mipMapCount = (isMipMaps) ? MipMapsCountFromWidth(pvrtHeader->width) : 1;
while (mipMapCount)
{
mipWidth = (pvrtHeader->width >> (mipMapCount - 1));
mipHeight = (pvrtHeader->height >> (mipMapCount - 1));
mipSize = mipWidth * mipHeight;
if (--mipMapCount > 0)
{
if (isVQCompressed)
{
srcData += mipSize / 4;
}
else
{
srcData += kSrcStride * mipSize;
}
}
else if (isMipMaps)
{
srcData += (isVQCompressed) ? 1 : kSrcStride; // skip 1x1 mip
}
}
// Compressed textures processes only half-size
if (isVQCompressed)
{
mipWidth /= 2;
mipHeight /= 2;
mipSize = mipWidth * mipHeight;
}
//extract image data
unsigned long int x = 0;
unsigned long int y = 0;
unsigned int proccessed = 0;
while(proccessed < mipSize)
{
unsigned long int srcPos = 0;
unsigned long int dstPos = 0;
unsigned short int srcTexel = 0;
if (isVQCompressed)
{
unsigned long int vqIndex = srcData[GetUntwiddledTexelPosition(x, y)] * numCodedComponents; // Index of codebook * numbers of 2x2 block components
// Bypass elements in 2x2 block
for (unsigned int yoffset = 0; yoffset < 2; ++yoffset)
{
for (unsigned int xoffset = 0; xoffset < 2; ++xoffset)
{
srcPos = (vqIndex + (xoffset * 2 + yoffset)) * kSrcStride;
srcTexel = ToUint16(&srcVQ[srcPos]);
dstPos = ((y * 2 + yoffset) * 2 * mipWidth + (x * 2 + xoffset)) * kDstStride;
TexelToRGBA(srcTexel, srcFormat, &dstData[dstPos], &dstData[dstPos + 1], &dstData[dstPos + 2], &dstData[dstPos + 3]);
}
}
if (++x >= mipWidth)
{
x = 0;
++y;
}
}
else
{
x = proccessed % mipWidth;
y = proccessed / mipWidth;
srcPos = ((isTwiddled) ? GetUntwiddledTexelPosition(x, y) : proccessed) * kSrcStride;
srcTexel = ToUint16(&srcData[srcPos]);
dstPos = proccessed * kDstStride;
TexelToRGBA(srcTexel, srcFormat, &dstData[dstPos], &dstData[dstPos + 1], &dstData[dstPos + 2], &dstData[dstPos + 3]);
}
++proccessed;
}
return 1;
}
int MipMapsCountFromWidth(unsigned long int width)
{
unsigned int mipMapsCount = 0;
while( width )
{
++mipMapsCount;
width /= 2;
}
return mipMapsCount;
}
// Twiddle
unsigned long int UntwiddleValue(unsigned long int value)
{
unsigned long int untwiddled = 0;
for (size_t i = 0; i < 10; i++)
{
unsigned long int shift = pow(2, i);
if (value & shift) untwiddled |= (shift << i);
}
return untwiddled;
}
void BuildTwiddleTable()
{
for( unsigned long int i = 0; i < kTwiddleTableSize; i++ )
{
gTwiddledTable[i] = UntwiddleValue( i );
}
}
unsigned long int GetUntwiddledTexelPosition(unsigned long int x, unsigned long int y)
{
unsigned long int pos = 0;
if(x >= kTwiddleTableSize || y >= kTwiddleTableSize)
{
pos = UntwiddleValue(y) | UntwiddleValue(x) << 1;
}
else
{
pos = gTwiddledTable[y] | gTwiddledTable[x] << 1;
}
return pos;
}
void TexelToRGBA(unsigned short int srcTexel, enum TextureFormatMasks srcFormat, unsigned char *r, unsigned char *g, unsigned char *b, unsigned char *a)
{
switch( srcFormat )
{
case TFM_RGB565:
*a = 0xFF;
*r = (srcTexel & 0xF800) >> 8;
*g = (srcTexel & 0x07E0) >> 3;
*b = (srcTexel & 0x001F) << 3;
break;
case TFM_ARGB1555:
*a = (srcTexel & 0x8000) ? 0xFF : 0x00;
*r = (srcTexel & 0x7C00) >> 7;
*g = (srcTexel & 0x03E0) >> 2;
*b = (srcTexel & 0x001F) << 3;
break;
case TFM_ARGB4444:
*a = (srcTexel & 0xF000) >> 8;
*r = (srcTexel & 0x0F00) >> 4;
*g = (srcTexel & 0x00F0);
*b = (srcTexel & 0x000F) << 4;
break;
case TFM_YUV422: // wip
*a = 0xFF;
*r = 0xFF;
*g = 0xFF;
*b = 0xFF;
break;
}
}