-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpvrspi.cpp
414 lines (361 loc) · 9.82 KB
/
pvrspi.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pvrspi.h"
typedef struct {
unsigned char id[4];
unsigned long length;
unsigned char type[4];
unsigned short width;
unsigned short height;
} PVRHDR;
#define SKIP_GBIX(data) \
if (memcmp(data,"GBIX",4)==0) {\
long size = ((long*)data)[1]; \
data+=size+8; \
}
enum {ARGB1555,RGB565,ARGB4444,YUV422,BUMP,PAL4,PAL8};
int pvr2binhdr(int &width, int &height, int &img_size,int &bits, PVRHDR *hdr);
int pvr2bindata(unsigned char *pbin,const PVRHDR *hdr);
int pvr2bin(const unsigned char *buf,unsigned char *&res, int &width, int &height, int &img_size, int &bits)
{
const unsigned char *data;
PVRHDR *hdr;
data = buf;
SKIP_GBIX(data);
if (memcmp(data,"PVRT",4)) return -1;
hdr = (PVRHDR*)data;
pvr2binhdr(width, height, img_size, bits, hdr);
res = (unsigned char *)malloc(img_size);
pvr2bindata(res,hdr);
return 0;
}
static int calc_n(int n)
{
int sum = 1;
while(n) {
n>>=1;
sum +=n*n;
}
return sum;
}
typedef struct {
int rmask, rmult, radd, rshift;
int gmask, gmult, gadd, gshift;
int bmask, bmult, badd, bshift;
int amask, amult, aadd, ashift;
} SHIFTTBL;
const static SHIFTTBL shifttbl[]= {
{ /* ARGB1555 */
0x7c00, 527, 23<<10, 16,
0x03e0, 527, 23<<5, 11,
0x001f, 527, 23, 6,
0x8000, 0x1ff, 0, 16
},{
/* RGB565 */
0xf800, 527, 23<<11, 17,
0x07e0, 259, 33<<5, 11,
0x001f, 527, 23, 6,
0x0000, 0, 0xff, 0
},{
/* ARGB4444 */
0x0f00, 1088, 32<<8, 14,
0x00f0, 1088, 32<<4, 10,
0x000f, 1088, 32, 6,
0xf000, 1088, 32<<12, 18
}
};
static unsigned long twiddle(unsigned long xSize, unsigned long ySize, unsigned long xPos, unsigned long yPos)
{
//Initially assume X is the larger size.
unsigned long minDimension=xSize;
unsigned long maxValue=yPos;
unsigned long twiddled=0;
unsigned long srcBitPos=1;
unsigned long dstBitPos=1;
int shiftCount=0;
//If Y is the larger dimension - switch the min/max values.
if(xSize > ySize) {
minDimension = ySize;
maxValue = xPos;
}
// Step through all the bits in the "minimum" dimension
while(srcBitPos < minDimension) {
if(yPos & srcBitPos) twiddled |= dstBitPos;
if(xPos & srcBitPos) twiddled |= (dstBitPos << 1);
srcBitPos <<= 1;
dstBitPos <<= 2;
shiftCount += 1;
}
// Prepend any unused bits
maxValue >>= shiftCount;
twiddled |= (maxValue << (2*shiftCount));
return twiddled;
}
enum {R,G,B,A};
int decode_small_vq(unsigned char *out,const unsigned char *in0,const unsigned char *in1,int width,int height,int mode)
{
unsigned char vqtab[4*4*16];
const unsigned char *in;
int x,y,wbyte;
const SHIFTTBL *s;
const unsigned short *in_w = (unsigned short *) in1;
unsigned char *p = vqtab;
s = &shifttbl[mode];
for(x=0;x<16*4;x++) {
int c = in_w[x];
p[R] = ((c & s->rmask) * s->rmult + s->radd) >> s->rshift;
p[G] = ((c & s->gmask) * s->gmult + s->gadd) >> s->gshift;
p[B] = ((c & s->bmask) * s->bmult + s->badd) >> s->bshift;
p[A] = ((c & s->amask) * s->amult + s->aadd) >> s->ashift;
p+=4;
}
in = in0;
wbyte = width*4;
for(y=0;y<height;y+=4) {
unsigned char *p = out+(height-y-1)*wbyte;
for(x=0;x<width;x+=2) {
int c = in[twiddle(width/2,height/4,x/2,y/4)];
unsigned char *vq;
vq = &vqtab[(c&15)*16];
p[0] = vq[0];
p[1] = vq[1];
p[2] = vq[2];
p[3] = vq[3];
p[4] = vq[8];
p[5] = vq[9];
p[6] = vq[10];
p[7] = vq[11];
p[0-wbyte] = vq[4];
p[1-wbyte] = vq[5];
p[2-wbyte] = vq[6];
p[3-wbyte] = vq[7];
p[4-wbyte] = vq[12];
p[5-wbyte] = vq[13];
p[6-wbyte] = vq[14];
p[7-wbyte] = vq[15];
vq = &vqtab[(c>>4)*16];
p-=wbyte*2;
p[0] = vq[0];
p[1] = vq[1];
p[2] = vq[2];
p[3] = vq[3];
p[4] = vq[8];
p[5] = vq[9];
p[6] = vq[10];
p[7] = vq[11];
p[0-wbyte] = vq[4];
p[1-wbyte] = vq[5];
p[2-wbyte] = vq[6];
p[3-wbyte] = vq[7];
p[4-wbyte] = vq[12];
p[5-wbyte] = vq[13];
p[6-wbyte] = vq[14];
p[7-wbyte] = vq[15];
p+=wbyte*2;
p+=8;
}
}
return 0;
}
int decode_twiddled_vq(unsigned char *out,const unsigned char *in0,const unsigned char *in1,int width,int height,int mode)
{
unsigned char vqtab[4*4*256];
const unsigned char *in;
int x,y,wbyte;
const SHIFTTBL *s;
const unsigned short *in_w = (const unsigned short *)in1;
unsigned char *p = vqtab;
s = &shifttbl[mode];
for(x=0;x<256*4;x++) {
int c = in_w[x];
p[R] = ((c & s->rmask) * s->rmult + s->radd) >> s->rshift;
p[G] = ((c & s->gmask) * s->gmult + s->gadd) >> s->gshift;
p[B] = ((c & s->bmask) * s->bmult + s->badd) >> s->bshift;
p[A] = ((c & s->amask) * s->amult + s->aadd) >> s->ashift;
p+=4;
}
in = in0;
wbyte = width*4;
for(y=0;y<height;y+=2) {
unsigned char *p = out+(height-y-1)*wbyte;
for(x=0;x<width;x+=2) {
int c = in[twiddle(width/2, height/2, x/2, y/2)];
unsigned char *vq = &vqtab[c*16];
p[0] = vq[0];
p[1] = vq[1];
p[2] = vq[2];
p[3] = vq[3];
p[4] = vq[8];
p[5] = vq[9];
p[6] = vq[10];
p[7] = vq[11];
p[0-wbyte] = vq[4];
p[1-wbyte] = vq[5];
p[2-wbyte] = vq[6];
p[3-wbyte] = vq[7];
p[4-wbyte] = vq[12];
p[5-wbyte] = vq[13];
p[6-wbyte] = vq[14];
p[7-wbyte] = vq[15];
p+=8;
}
}
return 0;
}
int decode_twiddled(unsigned char *out,const unsigned char *in0,int width,int height,int mode)
{
int x,y,wbyte;
const SHIFTTBL *s;
switch(mode){
case PAL4:
wbyte = (width/2+3)&~3;
for(y=0;y<height;y+=2) {
unsigned char *p = out+(height-y-1)*wbyte;
const unsigned char *in=in0;
for(x=0;x<width;x+=2) {
int c0 = in[twiddle(width, height/2, x, y/2)];
int c1 = in[twiddle(width, height/2, x+1, y/2)];
p[x/2 ] = (c0&0x0f) | (c1<<4);
p[x/2-wbyte] = (c0>>4) | (c1&0xf0);
}
}
break;
case PAL8:
wbyte = (width+3)&~3;
for(y=0;y<height;y++) {
unsigned char *p = out+(height-y-1)*wbyte;
const unsigned char *in=in0;
for(x=0;x<width;x++) {
p[x] = in[twiddle(width, height, x, y)];
}
}
break;
default:
s = &shifttbl[mode];
wbyte = width*4;
for(y=0;y<height;y++) {
unsigned char *p = out+(height-y-1)*wbyte;
const unsigned short *in=(const unsigned short *)in0;
for(x=0;x<width;x++) {
int c = in[twiddle(width, height, x, y)];
p[R] = ((c & s->rmask) * s->rmult + s->radd) >> s->rshift;
p[G] = ((c & s->gmask) * s->gmult + s->gadd) >> s->gshift;
p[B] = ((c & s->bmask) * s->bmult + s->badd) >> s->bshift;
p[A] = ((c & s->amask) * s->amult + s->aadd) >> s->ashift;
p+=4;
}
}
}
return 0;
}
int decode_rectangle(unsigned char *out,const unsigned char *in0,int width,int height,int mode)
{
int x,y,wbyte;
const SHIFTTBL *s;
if (mode<=2)
s = &shifttbl[mode];
wbyte = width*4;
for(y=0;y<height;y++) {
unsigned char *p = out+(height-y-1)*wbyte;
const unsigned short *in = (const unsigned short *)in0;
for(x=0;x<width;x++) {
int c = in[x];
p[R] = ((c & s->rmask) * s->rmult + s->radd) >> s->rshift;
p[G] = ((c & s->gmask) * s->gmult + s->gadd) >> s->gshift;
p[B] = ((c & s->bmask) * s->bmult + s->badd) >> s->bshift;
p[A] = ((c & s->amask) * s->amult + s->aadd) >> s->ashift;
p+=4;
}
}
return 0;
}
int decode_rectangle_twiddled(unsigned char *out,const unsigned char *in0,int width,int height,int mode)
{
int x,y,wbyte;
const SHIFTTBL *s;
if (mode<=2)
s = &shifttbl[mode];
wbyte = width*4;
for(y=0;y<height;y++) {
unsigned char *p = out+(height-y-1)*wbyte;
const unsigned short *in = (const unsigned short *)in0;
for(x=0;x<width;x++) {
int c = in[twiddle(width, height, x, y)];
p[R] = ((c & s->rmask) * s->rmult + s->radd) >> s->rshift;
p[G] = ((c & s->gmask) * s->gmult + s->gadd) >> s->gshift;
p[B] = ((c & s->bmask) * s->bmult + s->badd) >> s->bshift;
p[A] = ((c & s->amask) * s->amult + s->aadd) >> s->ashift;
p+=4;
}
}
return 0;
}
int pvr2binhdr(int &width, int &height, int &img_size,int &bits, PVRHDR *hdr)
{
// !!MH!! change handling to include both 24 and 32 bit color depth.
// Right now the alpha channel is always used.
switch(hdr->type[0]){
case 0x00: /* ARGB155 */
case 0x01: /* RGB565 */
case 0x02: /* ARGB4444 */
case 0x03: /* YUV422 */
bits = 32; break;
case 0x04: /* BUMP */
case 0x05: /* 4BPP */
bits = 4; break;
case 0x06: /* 8BPP */
bits = 8; break;
default:
break;
}
width = hdr->width;
height = hdr->height;
img_size = ((hdr->width*bits/8+3)&~3) * hdr->height;
return 0;
}
int pvr2bindata(unsigned char *pbin,const PVRHDR *hdr)
{
const unsigned char *data = (const unsigned char*)(hdr+1);
switch(hdr->type[1]){
case 0x01: /* twiddled */
decode_twiddled(pbin,data,hdr->width,hdr->height,hdr->type[0]);
break;
case 0x02: /* twiddled & mipmap */
decode_twiddled(pbin,data+calc_n(hdr->width)*2,hdr->width,hdr->height,hdr->type[0]);
break;
case 0x03: /* VQ */
decode_twiddled_vq(pbin,data+2048,data,hdr->width,hdr->height,hdr->type[0]);
break;
case 0x04: /* VQ & MIPMAP */
decode_twiddled_vq(pbin,data+2048+calc_n(hdr->width/2),data,hdr->width,hdr->height,hdr->type[0]);
break;
case 0x05: /* twiddled 4bit? */
decode_twiddled(pbin,data,hdr->width,hdr->height,hdr->type[0]);
break;
case 0x06: /* twiddled & mipmap 4bit? */
decode_twiddled(pbin,data+calc_n(hdr->width)/2,hdr->width,hdr->height,hdr->type[0]);
break;
case 0x07: /* twiddled 8bit? */
decode_twiddled(pbin,data,hdr->width,hdr->height,hdr->type[0]);
break;
case 0x08: /* twiddled & mipmap 8bit? */
decode_twiddled(pbin,data+calc_n(hdr->width),hdr->width,hdr->height,hdr->type[0]);
break;
case 0x09: /* RECTANGLE */
decode_rectangle(pbin,data,hdr->width,hdr->height,hdr->type[0]);
break;
case 0x0d: /* RECTANGLE twiddled */
decode_rectangle_twiddled(pbin,data,hdr->width,hdr->height,hdr->type[0]);
break;
case 0x10: /* SMALL_VQ */
decode_small_vq(pbin,data+128,data,hdr->width,hdr->height,hdr->type[0]);
break;
case 0x11: /* SMALL_VQ & MIPMAP */
decode_small_vq(pbin,data+128+calc_n(hdr->width/2)/2,data,hdr->width,hdr->height,hdr->type[0]);
break;
default:
break;
}
return 0;
}