-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstbi_pvr_c.h
More file actions
1001 lines (843 loc) · 22.8 KB
/
stbi_pvr_c.h
File metadata and controls
1001 lines (843 loc) · 22.8 KB
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
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "pvr_helper.h"
static int stbi__pvr_test(stbi__context *s)
{
// check header size
if (stbi__get32le(s) != sizeof(PVR_Texture_Header)) {
stbi__rewind(s);
return 0;
}
// stbi__skip until the magic number
stbi__skip(s, 10*4);
// check the magic number
if ( stbi__get32le(s) != PVRTEX_IDENTIFIER ) {
stbi__rewind(s);
return 0;
}
// Also rewind because the loader needs to read the header
stbi__rewind(s);
return 1;
}
#ifndef STBI_NO_STDIO
int stbi__pvr_test_filename (char const *filename)
{
int r;
FILE *f = fopen(filename, "rb");
if (!f) return 0;
r = stbi__pvr_test_file(f);
fclose(f);
return r;
}
int stbi__pvr_test_file (FILE *f)
{
stbi__context s;
int r,n = ftell(f);
stbi__start_file(&s,f);
r = stbi__pvr_test(&s);
fseek(f,n,SEEK_SET);
return r;
}
#endif
int stbi__pvr_test_memory (stbi_uc const *buffer, int len)
{
stbi__context s;
stbi__start_mem(&s,buffer, len);
return stbi__pvr_test(&s);
}
int stbi__pvr_test_callbacks (stbi_io_callbacks const *clbk, void *user)
{
stbi__context s;
stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
return stbi__pvr_test(&s);
}
static int stbi__pvr_info(stbi__context *s, int *x, int *y, int *comp, int * iscompressed )
{
PVR_Texture_Header header={0};
stbi__getn( s, (stbi_uc*)(&header), sizeof(PVR_Texture_Header) );
// Check the header size
if ( header.dwHeaderSize != sizeof(PVR_Texture_Header) ) {
stbi__rewind( s );
return 0;
}
// Check the magic identifier
if ( header.dwPVR != PVRTEX_IDENTIFIER ) {
stbi__rewind(s);
return 0;
}
*x = s->img_x = header.dwWidth;
*y = s->img_y = header.dwHeight;
*comp = s->img_n = ( header.dwBitCount + 7 ) / 8;
if ( iscompressed )
*iscompressed = 0;
switch ( header.dwpfFlags & PVRTEX_PIXELTYPE )
{
case OGL_RGBA_4444:
s->img_n = 2;
break;
case OGL_RGBA_5551:
s->img_n = 2;
break;
case OGL_RGBA_8888:
s->img_n = 4;
break;
case OGL_RGB_565:
s->img_n = 2;
break;
case OGL_RGB_888:
s->img_n = 3;
break;
case OGL_I_8:
s->img_n = 1;
break;
case OGL_AI_88:
s->img_n = 2;
break;
case OGL_PVRTC2:
s->img_n = 4;
if ( iscompressed )
*iscompressed = 1;
break;
case OGL_PVRTC4:
s->img_n = 4;
if ( iscompressed )
*iscompressed = 1;
break;
case OGL_RGB_555:
default:
stbi__rewind(s);
return 0;
}
*comp = s->img_n;
return 1;
}
int stbi__pvr_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int * iscompressed )
{
stbi__context s;
stbi__start_mem(&s,buffer, len);
return stbi__pvr_info( &s, x, y, comp, iscompressed );
}
int stbi__pvr_info_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int * iscompressed)
{
stbi__context s;
stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
return stbi__pvr_info( &s, x, y, comp, iscompressed );
}
#ifndef STBI_NO_STDIO
int stbi__pvr_info_from_path(char const *filename, int *x, int *y, int *comp, int * iscompressed)
{
int res;
FILE *f = fopen(filename, "rb");
if (!f) return 0;
res = stbi__pvr_info_from_file( f, x, y, comp, iscompressed );
fclose(f);
return res;
}
int stbi__pvr_info_from_file(FILE *f, int *x, int *y, int *comp, int * iscompressed)
{
stbi__context s;
int res;
long n = ftell(f);
stbi__start_file(&s, f);
res = stbi__pvr_info(&s, x, y, comp, iscompressed);
fseek(f, n, SEEK_SET);
return res;
}
#endif
/******************************************************************************
Taken from:
@File PVRTDecompress.cpp
@Title PVRTDecompress
@Copyright Copyright (C) Imagination Technologies Limited.
@Platform ANSI compatible
@Description PVRTC Texture Decompression.
******************************************************************************/
typedef unsigned char PVRTuint8;
typedef unsigned short PVRTuint16;
typedef unsigned int PVRTuint32;
/*****************************************************************************
* defines and consts
*****************************************************************************/
#define PT_INDEX (2) // The Punch-through index
#define BLK_Y_SIZE (4) // always 4 for all 2D block types
#define BLK_X_MAX (8) // Max X dimension for blocks
#define BLK_X_2BPP (8) // dimensions for the two formats
#define BLK_X_4BPP (4)
#define WRAP_COORD(Val, Size) ((Val) & ((Size)-1))
#define POWER_OF_2(X) util_number_is_power_2(X)
/*
Define an expression to either wrap or clamp large or small vals to the
legal coordinate range
*/
#define PVRT_MIN(a,b) (((a) < (b)) ? (a) : (b))
#define PVRT_MAX(a,b) (((a) > (b)) ? (a) : (b))
#define PVRT_CLAMP(x, l, h) (PVRT_MIN((h), PVRT_MAX((x), (l))))
#define LIMIT_COORD(Val, Size, AssumeImageTiles) \
((AssumeImageTiles)? WRAP_COORD((Val), (Size)): PVRT_CLAMP((Val), 0, (Size)-1))
/*****************************************************************************
* Useful typedefs
*****************************************************************************/
typedef PVRTuint32 U32;
typedef PVRTuint8 U8;
/***********************************************************
DECOMPRESSION ROUTINES
************************************************************/
/*!***********************************************************************
@Struct AMTC_BLOCK_STRUCT
@Brief
*************************************************************************/
typedef struct
{
// Uses 64 bits pre block
U32 PackedData[2];
}AMTC_BLOCK_STRUCT;
/*!***********************************************************************
@Function util_number_is_power_2
@Input input A number
@Returns TRUE if the number is an integer power of two, else FALSE.
@Description Check that a number is an integer power of two, i.e.
1, 2, 4, 8, ... etc.
Returns FALSE for zero.
*************************************************************************/
int util_number_is_power_2( unsigned input )
{
unsigned minus1;
if( !input ) return 0;
minus1 = input - 1;
return ( (input | minus1) == (input ^ minus1) ) ? 1 : 0;
}
/*!***********************************************************************
@Function Unpack5554Colour
@Input pBlock
@Input ABColours
@Description Given a block, extract the colour information and convert
to 5554 formats
*************************************************************************/
static void Unpack5554Colour(const AMTC_BLOCK_STRUCT *pBlock,
int ABColours[2][4])
{
U32 RawBits[2];
int i;
// Extract A and B
RawBits[0] = pBlock->PackedData[1] & (0xFFFE); // 15 bits (shifted up by one)
RawBits[1] = pBlock->PackedData[1] >> 16; // 16 bits
// step through both colours
for(i = 0; i < 2; i++)
{
// If completely opaque
if(RawBits[i] & (1<<15))
{
// Extract R and G (both 5 bit)
ABColours[i][0] = (RawBits[i] >> 10) & 0x1F;
ABColours[i][1] = (RawBits[i] >> 5) & 0x1F;
/*
The precision of Blue depends on A or B. If A then we need to
replicate the top bit to get 5 bits in total
*/
ABColours[i][2] = RawBits[i] & 0x1F;
if(i==0)
{
ABColours[0][2] |= ABColours[0][2] >> 4;
}
// set 4bit alpha fully on...
ABColours[i][3] = 0xF;
}
else // Else if colour has variable translucency
{
/*
Extract R and G (both 4 bit).
(Leave a space on the end for the replication of bits
*/
ABColours[i][0] = (RawBits[i] >> (8-1)) & 0x1E;
ABColours[i][1] = (RawBits[i] >> (4-1)) & 0x1E;
// replicate bits to truly expand to 5 bits
ABColours[i][0] |= ABColours[i][0] >> 4;
ABColours[i][1] |= ABColours[i][1] >> 4;
// grab the 3(+padding) or 4 bits of blue and add an extra padding bit
ABColours[i][2] = (RawBits[i] & 0xF) << 1;
/*
expand from 3 to 5 bits if this is from colour A, or 4 to 5 bits if from
colour B
*/
if(i==0)
{
ABColours[0][2] |= ABColours[0][2] >> 3;
}
else
{
ABColours[0][2] |= ABColours[0][2] >> 4;
}
// Set the alpha bits to be 3 + a zero on the end
ABColours[i][3] = (RawBits[i] >> 11) & 0xE;
}
}
}
/*!***********************************************************************
@Function UnpackModulations
@Input pBlock
@Input Do2bitMode
@Input ModulationVals
@Input ModulationModes
@Input StartX
@Input StartY
@Description Given the block and the texture type and it's relative
position in the 2x2 group of blocks, extract the bit
patterns for the fully defined pixels.
*************************************************************************/
static void UnpackModulations(const AMTC_BLOCK_STRUCT *pBlock,
const int Do2bitMode,
int ModulationVals[8][16],
int ModulationModes[8][16],
int StartX,
int StartY)
{
int BlockModMode;
U32 ModulationBits;
int x, y;
BlockModMode= pBlock->PackedData[1] & 1;
ModulationBits = pBlock->PackedData[0];
// if it's in an interpolated mode
if(Do2bitMode && BlockModMode)
{
/*
run through all the pixels in the block. Note we can now treat all the
"stored" values as if they have 2bits (even when they didn't!)
*/
for(y = 0; y < BLK_Y_SIZE; y++)
{
for(x = 0; x < BLK_X_2BPP; x++)
{
ModulationModes[y+StartY][x+StartX] = BlockModMode;
// if this is a stored value...
if(((x^y)&1) == 0)
{
ModulationVals[y+StartY][x+StartX] = ModulationBits & 3;
ModulationBits >>= 2;
}
}
}
}
else if(Do2bitMode) // else if direct encoded 2bit mode - i.e. 1 mode bit per pixel
{
for(y = 0; y < BLK_Y_SIZE; y++)
{
for(x = 0; x < BLK_X_2BPP; x++)
{
ModulationModes[y+StartY][x+StartX] = BlockModMode;
// double the bits so 0=> 00, and 1=>11
if(ModulationBits & 1)
{
ModulationVals[y+StartY][x+StartX] = 0x3;
}
else
{
ModulationVals[y+StartY][x+StartX] = 0x0;
}
ModulationBits >>= 1;
}
}
}
else // else its the 4bpp mode so each value has 2 bits
{
for(y = 0; y < BLK_Y_SIZE; y++)
{
for(x = 0; x < BLK_X_4BPP; x++)
{
ModulationModes[y+StartY][x+StartX] = BlockModMode;
ModulationVals[y+StartY][x+StartX] = ModulationBits & 3;
ModulationBits >>= 2;
}
}
}
// make sure nothing is left over
assert(ModulationBits==0);
}
/*!***********************************************************************
@Function InterpolateColours
@Input ColourP
@Input ColourQ
@Input ColourR
@Input ColourS
@Input Do2bitMode
@Input x
@Input y
@Modified Result
@Description This performs a HW bit accurate interpolation of either the
A or B colours for a particular pixel.
NOTE: It is assumed that the source colours are in ARGB 5554
format - This means that some "preparation" of the values will
be necessary.
*************************************************************************/
static void InterpolateColours(const int ColourP[4],
const int ColourQ[4],
const int ColourR[4],
const int ColourS[4],
const int Do2bitMode,
const int x,
const int y,
int Result[4])
{
int u, v, uscale;
int k;
int tmp1, tmp2;
int P[4], Q[4], R[4], S[4];
// Copy the colours
for(k = 0; k < 4; k++)
{
P[k] = ColourP[k];
Q[k] = ColourQ[k];
R[k] = ColourR[k];
S[k] = ColourS[k];
}
// put the x and y values into the right range
v = (y & 0x3) | ((~y & 0x2) << 1);
if(Do2bitMode)
u = (x & 0x7) | ((~x & 0x4) << 1);
else
u = (x & 0x3) | ((~x & 0x2) << 1);
// get the u and v scale amounts
v = v - BLK_Y_SIZE/2;
if(Do2bitMode)
{
u = u - BLK_X_2BPP/2;
uscale = 8;
}
else
{
u = u - BLK_X_4BPP/2;
uscale = 4;
}
for(k = 0; k < 4; k++)
{
tmp1 = P[k] * uscale + u * (Q[k] - P[k]);
tmp2 = R[k] * uscale + u * (S[k] - R[k]);
tmp1 = tmp1 * 4 + v * (tmp2 - tmp1);
Result[k] = tmp1;
}
// Lop off the appropriate number of bits to get us to 8 bit precision
if(Do2bitMode)
{
// do RGB
for(k = 0; k < 3; k++)
{
Result[k] >>= 2;
}
Result[3] >>= 1;
}
else
{
// do RGB (A is ok)
for(k = 0; k < 3; k++)
{
Result[k] >>= 1;
}
}
// sanity check
for(k = 0; k < 4; k++)
{
assert(Result[k] < 256);
}
/*
Convert from 5554 to 8888
do RGB 5.3 => 8
*/
for(k = 0; k < 3; k++)
{
Result[k] += Result[k] >> 5;
}
Result[3] += Result[3] >> 4;
// 2nd sanity check
for(k = 0; k < 4; k++)
{
assert(Result[k] < 256);
}
}
/*!***********************************************************************
@Function GetModulationValue
@Input x
@Input y
@Input Do2bitMode
@Input ModulationVals
@Input ModulationModes
@Input Mod
@Input DoPT
@Description Get the modulation value as a numerator of a fraction of 8ths
*************************************************************************/
static void GetModulationValue(int x,
int y,
const int Do2bitMode,
const int ModulationVals[8][16],
const int ModulationModes[8][16],
int *Mod,
int *DoPT)
{
static const int RepVals0[4] = {0, 3, 5, 8};
static const int RepVals1[4] = {0, 4, 4, 8};
int ModVal;
// Map X and Y into the local 2x2 block
y = (y & 0x3) | ((~y & 0x2) << 1);
if(Do2bitMode)
x = (x & 0x7) | ((~x & 0x4) << 1);
else
x = (x & 0x3) | ((~x & 0x2) << 1);
// assume no PT for now
*DoPT = 0;
// extract the modulation value. If a simple encoding
if(ModulationModes[y][x]==0)
{
ModVal = RepVals0[ModulationVals[y][x]];
}
else if(Do2bitMode)
{
// if this is a stored value
if(((x^y)&1)==0)
ModVal = RepVals0[ModulationVals[y][x]];
else if(ModulationModes[y][x] == 1) // else average from the neighbours if H&V interpolation..
{
ModVal = (RepVals0[ModulationVals[y-1][x]] +
RepVals0[ModulationVals[y+1][x]] +
RepVals0[ModulationVals[y][x-1]] +
RepVals0[ModulationVals[y][x+1]] + 2) / 4;
}
else if(ModulationModes[y][x] == 2) // else if H-Only
{
ModVal = (RepVals0[ModulationVals[y][x-1]] +
RepVals0[ModulationVals[y][x+1]] + 1) / 2;
}
else // else it's V-Only
{
ModVal = (RepVals0[ModulationVals[y-1][x]] +
RepVals0[ModulationVals[y+1][x]] + 1) / 2;
}
}
else // else it's 4BPP and PT encoding
{
ModVal = RepVals1[ModulationVals[y][x]];
*DoPT = ModulationVals[y][x] == PT_INDEX;
}
*Mod =ModVal;
}
/*!***********************************************************************
@Function TwiddleUV
@Input YSize Y dimension of the texture in pixels
@Input XSize X dimension of the texture in pixels
@Input YPos Pixel Y position
@Input XPos Pixel X position
@Returns The twiddled offset of the pixel
@Description Given the Block (or pixel) coordinates and the dimension of
the texture in blocks (or pixels) this returns the twiddled
offset of the block (or pixel) from the start of the map.
NOTE the dimensions of the texture must be a power of 2
*************************************************************************/
static int DisableTwiddlingRoutine = 0;
static U32 TwiddleUV(U32 YSize, U32 XSize, U32 YPos, U32 XPos)
{
U32 Twiddled;
U32 MinDimension;
U32 MaxValue;
U32 SrcBitPos;
U32 DstBitPos;
int ShiftCount;
assert(YPos < YSize);
assert(XPos < XSize);
assert(POWER_OF_2(YSize));
assert(POWER_OF_2(XSize));
if(YSize < XSize)
{
MinDimension = YSize;
MaxValue = XPos;
}
else
{
MinDimension = XSize;
MaxValue = YPos;
}
// Nasty hack to disable twiddling
if(DisableTwiddlingRoutine)
return (YPos* XSize + XPos);
// Step through all the bits in the "minimum" dimension
SrcBitPos = 1;
DstBitPos = 1;
Twiddled = 0;
ShiftCount = 0;
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;
}
/***********************************************************/
/*
// Decompress
//
// Takes the compressed input data and outputs the equivalent decompressed
// image.
*/
/***********************************************************/
static void Decompress(AMTC_BLOCK_STRUCT *pCompressedData,
const int Do2bitMode,
const int XDim,
const int YDim,
const int AssumeImageTiles,
unsigned char* pResultImage)
{
int x, y;
int i, j;
int BlkX, BlkY;
int BlkXp1, BlkYp1;
int XBlockSize;
int BlkXDim, BlkYDim;
int StartX, StartY;
int ModulationVals[8][16];
int ModulationModes[8][16];
int Mod, DoPT;
unsigned int uPosition;
/*
// local neighbourhood of blocks
*/
AMTC_BLOCK_STRUCT *pBlocks[2][2];
AMTC_BLOCK_STRUCT *pPrevious[2][2] = {{NULL, NULL}, {NULL, NULL}};
/*
// Low precision colours extracted from the blocks
*/
struct
{
int Reps[2][4];
}Colours5554[2][2];
/*
// Interpolated A and B colours for the pixel
*/
int ASig[4], BSig[4];
int Result[4];
if(Do2bitMode)
{
XBlockSize = BLK_X_2BPP;
}
else
{
XBlockSize = BLK_X_4BPP;
}
/*
// For MBX don't allow the sizes to get too small
*/
BlkXDim = PVRT_MAX(2, XDim / XBlockSize);
BlkYDim = PVRT_MAX(2, YDim / BLK_Y_SIZE);
/*
// Step through the pixels of the image decompressing each one in turn
//
// Note that this is a hideously inefficient way to do this!
*/
for(y = 0; y < YDim; y++)
{
for(x = 0; x < XDim; x++)
{
/*
// map this pixel to the top left neighbourhood of blocks
*/
BlkX = (x - XBlockSize/2);
BlkY = (y - BLK_Y_SIZE/2);
BlkX = LIMIT_COORD(BlkX, XDim, AssumeImageTiles);
BlkY = LIMIT_COORD(BlkY, YDim, AssumeImageTiles);
BlkX /= XBlockSize;
BlkY /= BLK_Y_SIZE;
//BlkX = LIMIT_COORD(BlkX, BlkXDim, AssumeImageTiles);
//BlkY = LIMIT_COORD(BlkY, BlkYDim, AssumeImageTiles);
/*
// compute the positions of the other 3 blocks
*/
BlkXp1 = LIMIT_COORD(BlkX+1, BlkXDim, AssumeImageTiles);
BlkYp1 = LIMIT_COORD(BlkY+1, BlkYDim, AssumeImageTiles);
/*
// Map to block memory locations
*/
pBlocks[0][0] = pCompressedData +TwiddleUV(BlkYDim, BlkXDim, BlkY, BlkX);
pBlocks[0][1] = pCompressedData +TwiddleUV(BlkYDim, BlkXDim, BlkY, BlkXp1);
pBlocks[1][0] = pCompressedData +TwiddleUV(BlkYDim, BlkXDim, BlkYp1, BlkX);
pBlocks[1][1] = pCompressedData +TwiddleUV(BlkYDim, BlkXDim, BlkYp1, BlkXp1);
/*
// extract the colours and the modulation information IF the previous values
// have changed.
*/
if(memcmp(pPrevious, pBlocks, 4*sizeof(void*)) != 0)
{
StartY = 0;
for(i = 0; i < 2; i++)
{
StartX = 0;
for(j = 0; j < 2; j++)
{
Unpack5554Colour(pBlocks[i][j], Colours5554[i][j].Reps);
UnpackModulations(pBlocks[i][j],
Do2bitMode,
ModulationVals,
ModulationModes,
StartX, StartY);
StartX += XBlockSize;
}/*end for j*/
StartY += BLK_Y_SIZE;
}/*end for i*/
/*
// make a copy of the new pointers
*/
memcpy(pPrevious, pBlocks, 4*sizeof(void*));
}/*end if the blocks have changed*/
/*
// decompress the pixel. First compute the interpolated A and B signals
*/
InterpolateColours(Colours5554[0][0].Reps[0],
Colours5554[0][1].Reps[0],
Colours5554[1][0].Reps[0],
Colours5554[1][1].Reps[0],
Do2bitMode, x, y,
ASig);
InterpolateColours(Colours5554[0][0].Reps[1],
Colours5554[0][1].Reps[1],
Colours5554[1][0].Reps[1],
Colours5554[1][1].Reps[1],
Do2bitMode, x, y,
BSig);
GetModulationValue(x,y, Do2bitMode, (const int (*)[16])ModulationVals, (const int (*)[16])ModulationModes,
&Mod, &DoPT);
/*
// compute the modulated colour
*/
for(i = 0; i < 4; i++)
{
Result[i] = ASig[i] * 8 + Mod * (BSig[i] - ASig[i]);
Result[i] >>= 3;
}
if(DoPT)
{
Result[3] = 0;
}
/*
// Store the result in the output image
*/
uPosition = (x+y*XDim)<<2;
pResultImage[uPosition+0] = (unsigned char)Result[0];
pResultImage[uPosition+1] = (unsigned char)Result[1];
pResultImage[uPosition+2] = (unsigned char)Result[2];
pResultImage[uPosition+3] = (unsigned char)Result[3];
}/*end for x*/
}/*end for y*/
}
static void * stbi__pvr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp)
{
stbi_uc *pvr_data = NULL;
stbi_uc *pvr_res_data = NULL;
PVR_Texture_Header header={0};
int iscompressed = 0;
int bitmode = 0;
unsigned int levelSize = 0;
stbi__getn( s, (stbi_uc*)(&header), sizeof(PVR_Texture_Header) );
// Check the header size
if ( header.dwHeaderSize != sizeof(PVR_Texture_Header) ) {
return NULL;
}
// Check the magic identifier
if ( header.dwPVR != PVRTEX_IDENTIFIER ) {
return NULL;
}
*x = s->img_x = header.dwWidth;
*y = s->img_y = header.dwHeight;
/* Get if the texture is compressed and the texture mode ( 2bpp or 4bpp ) */
switch ( header.dwpfFlags & PVRTEX_PIXELTYPE )
{
case OGL_RGBA_4444:
s->img_n = 2;
break;
case OGL_RGBA_5551:
s->img_n = 2;
break;
case OGL_RGBA_8888:
s->img_n = 4;
break;
case OGL_RGB_565:
s->img_n = 2;
break;
case OGL_RGB_888:
s->img_n = 3;
break;
case OGL_I_8:
s->img_n = 1;
break;
case OGL_AI_88:
s->img_n = 2;
break;
case OGL_PVRTC2:
bitmode = 1;
s->img_n = 4;
iscompressed = 1;
break;
case OGL_PVRTC4:
s->img_n = 4;
iscompressed = 1;
break;
case OGL_RGB_555:
default:
return NULL;
}
*comp = s->img_n;
// Load only the first mip map level
levelSize = (s->img_x * s->img_y * header.dwBitCount + 7) / 8;
// get the raw data
pvr_data = (stbi_uc *)malloc( levelSize );
stbi__getn( s, pvr_data, levelSize );
// if compressed decompress as RGBA
if ( iscompressed ) {
pvr_res_data = (stbi_uc *)malloc( s->img_x * s->img_y * 4 );
Decompress( (AMTC_BLOCK_STRUCT*)pvr_data, bitmode, s->img_x, s->img_y, 1, (unsigned char*)pvr_res_data );
free( pvr_data );
} else {
// otherwise use the raw data
pvr_res_data = pvr_data;
}
if( (req_comp <= 4) && (req_comp >= 1) ) {
// user has some requirements, meet them
if( req_comp != s->img_n ) {
pvr_res_data = stbi__convert_format( pvr_res_data, s->img_n, req_comp, s->img_x, s->img_y );
*comp = req_comp;
}
}
return pvr_res_data;
}
#ifndef STBI_NO_STDIO
void *stbi__pvr_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp)
{
stbi__context s;
stbi__start_file(&s,f);
return stbi__pvr_load(&s,x,y,comp,req_comp);
}
void *stbi__pvr_load_from_path (char const*filename, int *x, int *y, int *comp, int req_comp)
{
void *data;
FILE *f = fopen(filename, "rb");
if (!f) return NULL;
data = stbi__pvr_load_from_file(f,x,y,comp,req_comp);
fclose(f);
return data;
}
#endif
void *stbi__pvr_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
{
stbi__context s;
stbi__start_mem(&s,buffer, len);
return stbi__pvr_load(&s,x,y,comp,req_comp);
}
void *stbi__pvr_load_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
{
stbi__context s;
stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
return stbi__pvr_load(&s,x,y,comp,req_comp);