Debian Codegraph @

tools/quake3/common/imagelib.c

raw symbols SCIP
1 /*
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 // imagelib.c
23
24 #include "inout.h"
25 #include "cmdlib.h"
26 #include "etclib.h"
27 #include "imagelib.h"
28 #include "vfs.h"
29
30 int fgetLittleShort( FILE *f ){
31 byte b1, b2;
32
33 b1 = fgetc( f );
34 b2 = fgetc( f );
35
36 return (short)( b1 + b2 * 256 );
37 }
38
39 int fgetLittleLong( FILE *f ){
40 byte b1, b2, b3, b4;
41
42 b1 = fgetc( f );
43 b2 = fgetc( f );
44 b3 = fgetc( f );
45 b4 = fgetc( f );
46
47 return b1 + ( b2 << 8 ) + ( b3 << 16 ) + ( b4 << 24 );
48 }
49
50 int bufLittleShort( byte *buf, int len, int *pos ){
51 byte b1, b2;
52
53 if ( ( len - *pos ) < 2 ) {
54 Error( "Unexpected buffer end" );
55 }
56
57 b1 = buf[*pos]; *pos += 1;
58 b2 = buf[*pos]; *pos += 1;
59
60 return (short)( b1 + b2 * 256 );
61 }
62
63 int bufLittleLong( byte *buf, int len, int *pos ){
64 byte b1, b2, b3, b4;
65
66 if ( ( len - *pos ) < 4 ) {
67 Error( "Unexpected buffer end" );
68 }
69
70 b1 = buf[*pos]; *pos += 1;
71 b2 = buf[*pos]; *pos += 1;
72 b3 = buf[*pos]; *pos += 1;
73 b4 = buf[*pos]; *pos += 1;
74
75 return b1 + ( b2 << 8 ) + ( b3 << 16 ) + ( b4 << 24 );
76 }
77
78
79 /*
80 ============================================================================
81
82 LBM STUFF
83
84 ============================================================================
85 */
86
87
88 typedef unsigned char UBYTE;
89 //conflicts with windows typedef short WORD;
90 typedef unsigned short UWORD;
91 typedef long LONG;
92
93 typedef enum
94 {
95 ms_none,
96 ms_mask,
97 ms_transcolor,
98 ms_lasso
99 } mask_t;
100
101 typedef enum
102 {
103 cm_none,
104 cm_rle1
105 } compress_t;
106
107 typedef struct
108 {
109 UWORD w,h;
110 short x,y;
111 UBYTE nPlanes;
112 UBYTE masking;
113 UBYTE compression;
114 UBYTE pad1;
115 UWORD transparentColor;
116 UBYTE xAspect,yAspect;
117 short pageWidth,pageHeight;
118 } bmhd_t;
119
120 extern bmhd_t bmhd; // will be in native byte order
121
122
123
124 #define FORMID ( 'F' + ( 'O' << 8 ) + ( (int)'R' << 16 ) + ( (int)'M' << 24 ) )
125 #define ILBMID ( 'I' + ( 'L' << 8 ) + ( (int)'B' << 16 ) + ( (int)'M' << 24 ) )
126 #define PBMID ( 'P' + ( 'B' << 8 ) + ( (int)'M' << 16 ) + ( (int)' ' << 24 ) )
127 #define BMHDID ( 'B' + ( 'M' << 8 ) + ( (int)'H' << 16 ) + ( (int)'D' << 24 ) )
128 #define BODYID ( 'B' + ( 'O' << 8 ) + ( (int)'D' << 16 ) + ( (int)'Y' << 24 ) )
129 #define CMAPID ( 'C' + ( 'M' << 8 ) + ( (int)'A' << 16 ) + ( (int)'P' << 24 ) )
130
131
132 bmhd_t bmhd;
133
134 int Align( int l ){
135 if ( l & 1 ) {
136 return l + 1;
137 }
138 return l;
139 }
140
141
142
143 /*
144 ================
145 LBMRLEdecompress
146
147 Source must be evenly aligned!
148 ================
149 */
150 byte *LBMRLEDecompress( byte *source,byte *unpacked, int bpwidth ){
151 int count;
152 byte b,rept;
153
154 count = 0;
155
156 do
157 {
158 rept = *source++;
159
160 if ( rept > 0x80 ) {
161 rept = ( rept ^ 0xff ) + 2;
162 b = *source++;
163 memset( unpacked,b,rept );
164 unpacked += rept;
165 }
166 else if ( rept < 0x80 ) {
167 rept++;
168 memcpy( unpacked,source,rept );
169 unpacked += rept;
170 source += rept;
171 }
172 else{
173 rept = 0; // rept of 0x80 is NOP
174
175 }
176 count += rept;
177
178 } while ( count < bpwidth );
179
180 if ( count > bpwidth ) {
181 Error( "Decompression exceeded width!\n" );
182 }
183
184
185 return source;
186 }
187
188
189 /*
190 =================
191 LoadLBM
192 =================
193 */
194 void LoadLBM( const char *filename, byte **picture, byte **palette ){
195 byte *LBMbuffer, *picbuffer, *cmapbuffer;
196 int y;
197 byte *LBM_P, *LBMEND_P;
198 byte *pic_p;
199 byte *body_p;
200
201 int formtype,formlength;
202 int chunktype,chunklength;
203
204 // qiet compiler warnings
205 picbuffer = NULL;
206 cmapbuffer = NULL;
207
208 //
209 // load the LBM
210 //
211 LoadFile( filename, (void **)&LBMbuffer );
212
213 //
214 // parse the LBM header
215 //
216 LBM_P = LBMbuffer;
217 if ( *(int *)LBMbuffer != LittleLong( FORMID ) ) {
218 Error( "No FORM ID at start of file!\n" );
219 }
220
221 LBM_P += 4;
222 formlength = BigLong( *(int *)LBM_P );
223 LBM_P += 4;
224 LBMEND_P = LBM_P + Align( formlength );
225
226 formtype = LittleLong( *(int *)LBM_P );
227
228 if ( formtype != ILBMID && formtype != PBMID ) {
229 Error( "Unrecognized form type: %c%c%c%c\n", formtype & 0xff
230 ,( formtype >> 8 ) & 0xff,( formtype >> 16 ) & 0xff,( formtype >> 24 ) & 0xff );
231 }
232
233 LBM_P += 4;
234
235 //
236 // parse chunks
237 //
238
239 while ( LBM_P < LBMEND_P )
240 {
241 chunktype = LBM_P[0] + ( LBM_P[1] << 8 ) + ( LBM_P[2] << 16 ) + ( LBM_P[3] << 24 );
242 LBM_P += 4;
243 chunklength = LBM_P[3] + ( LBM_P[2] << 8 ) + ( LBM_P[1] << 16 ) + ( LBM_P[0] << 24 );
244 LBM_P += 4;
245
246 switch ( chunktype )
247 {
248 case BMHDID:
249 memcpy( &bmhd,LBM_P,sizeof( bmhd ) );
250 bmhd.w = BigShort( bmhd.w );
251 bmhd.h = BigShort( bmhd.h );
252 bmhd.x = BigShort( bmhd.x );
253 bmhd.y = BigShort( bmhd.y );
254 bmhd.pageWidth = BigShort( bmhd.pageWidth );
255 bmhd.pageHeight = BigShort( bmhd.pageHeight );
256 break;
257
258 case CMAPID:
259 cmapbuffer = safe_malloc0( 768 );
260 memcpy( cmapbuffer, LBM_P, chunklength );
261 break;
262
263 case BODYID:
264 body_p = LBM_P;
265
266 pic_p = picbuffer = safe_malloc( bmhd.w * bmhd.h );
267 if ( formtype == PBMID ) {
268 //
269 // unpack PBM
270 //
271 for ( y = 0 ; y < bmhd.h ; y++, pic_p += bmhd.w )
272 {
273 if ( bmhd.compression == cm_rle1 ) {
274 body_p = LBMRLEDecompress( (byte *)body_p
275 , pic_p, bmhd.w );
276 }
277 else if ( bmhd.compression == cm_none ) {
278 memcpy( pic_p,body_p,bmhd.w );
279 body_p += Align( bmhd.w );
280 }
281 }
282
283 }
284 else
285 {
286 //
287 // unpack ILBM
288 //
289 Error( "%s is an interlaced LBM, not packed", filename );
290 }
291 break;
292 }
293
294 LBM_P += Align( chunklength );
295 }
296
297 free( LBMbuffer );
298
299 *picture = picbuffer;
300
301 if ( palette ) {
302 *palette = cmapbuffer;
303 }
304 }
305
306
307 /*
308 ============================================================================
309
310 WRITE LBM
311
312 ============================================================================
313 */
314
315 /*
316 ==============
317 WriteLBMfile
318 ==============
319 */
320 void WriteLBMfile( const char *filename, byte *data,
321 int width, int height, byte *palette ){
322 byte *lbm, *lbmptr;
323 int *formlength, *bmhdlength, *cmaplength, *bodylength;
324 int length;
325 bmhd_t basebmhd;
326
327 lbm = lbmptr = safe_malloc( width * height + 1000 );
328
329 //
330 // start FORM
331 //
332 *lbmptr++ = 'F';
333 *lbmptr++ = 'O';
334 *lbmptr++ = 'R';
335 *lbmptr++ = 'M';
336
337 formlength = (int*)lbmptr;
338 lbmptr += 4; // leave space for length
339
340 *lbmptr++ = 'P';
341 *lbmptr++ = 'B';
342 *lbmptr++ = 'M';
343 *lbmptr++ = ' ';
344
345 //
346 // write BMHD
347 //
348 *lbmptr++ = 'B';
349 *lbmptr++ = 'M';
350 *lbmptr++ = 'H';
351 *lbmptr++ = 'D';
352
353 bmhdlength = (int *)lbmptr;
354 lbmptr += 4; // leave space for length
355
356 memset( &basebmhd,0,sizeof( basebmhd ) );
357 basebmhd.w = BigShort( (short)width );
358 basebmhd.h = BigShort( (short)height );
359 basebmhd.nPlanes = BigShort( 8 );
360 basebmhd.xAspect = BigShort( 5 );
361 basebmhd.yAspect = BigShort( 6 );
362 basebmhd.pageWidth = BigShort( (short)width );
363 basebmhd.pageHeight = BigShort( (short)height );
364
365 memcpy( lbmptr,&basebmhd,sizeof( basebmhd ) );
366 lbmptr += sizeof( basebmhd );
367
368 length = lbmptr - (byte *)bmhdlength - 4;
369 *bmhdlength = BigLong( length );
370 if ( length & 1 ) {
371 *lbmptr++ = 0; // pad chunk to even offset
372
373 }
374 //
375 // write CMAP
376 //
377 *lbmptr++ = 'C';
378 *lbmptr++ = 'M';
379 *lbmptr++ = 'A';
380 *lbmptr++ = 'P';
381
382 cmaplength = (int *)lbmptr;
383 lbmptr += 4; // leave space for length
384
385 memcpy( lbmptr,palette,768 );
386 lbmptr += 768;
387
388 length = lbmptr - (byte *)cmaplength - 4;
389 *cmaplength = BigLong( length );
390 if ( length & 1 ) {
391 *lbmptr++ = 0; // pad chunk to even offset
392
393 }
394 //
395 // write BODY
396 //
397 *lbmptr++ = 'B';
398 *lbmptr++ = 'O';
399 *lbmptr++ = 'D';
400 *lbmptr++ = 'Y';
401
402 bodylength = (int *)lbmptr;
403 lbmptr += 4; // leave space for length
404
405 memcpy( lbmptr,data,width * height );
406 lbmptr += width * height;
407
408 length = lbmptr - (byte *)bodylength - 4;
409 *bodylength = BigLong( length );
410 if ( length & 1 ) {
411 *lbmptr++ = 0; // pad chunk to even offset
412
413 }
414 //
415 // done
416 //
417 length = lbmptr - (byte *)formlength - 4;
418 *formlength = BigLong( length );
419 if ( length & 1 ) {
420 *lbmptr++ = 0; // pad chunk to even offset
421
422 }
423 //
424 // write output file
425 //
426 SaveFile( filename, lbm, lbmptr - lbm );
427 free( lbm );
428 }
429
430
431 /*
432 ============================================================================
433
434 LOAD PCX
435
436 ============================================================================
437 */
438
439 typedef struct
440 {
441 char manufacturer;
442 char version;
443 char encoding;
444 char bits_per_pixel;
445 unsigned short xmin,ymin,xmax,ymax;
446 unsigned short hres,vres;
447 unsigned char palette[48];
448 char reserved;
449 char color_planes;
450 unsigned short bytes_per_line;
451 unsigned short palette_type;
452 char filler[58];
453 unsigned char data; // unbounded
454 } pcx_t;
455
456
457 /*
458 ==============
459 LoadPCX
460 ==============
461 */
462
463 /* RR2DO2 */
464 #define DECODEPCX( b, d, r ) d = *b++; if ( ( d & 0xC0 ) == 0xC0 ) {r = d & 0x3F; d = *b++; }else{r = 1; }
465
466 void LoadPCX( const char *filename, byte **pic, byte **palette, int *width, int *height ){
467 byte *raw;
468 pcx_t *pcx;
469 int x, y, lsize;
470 int len;
471 int dataByte, runLength;
472 byte *out, *pix;
473
474
475 /* load the file */
476 len = vfsLoadFile( filename, (void **)&raw, 0 );
477 if ( len == -1 ) {
478 Error( "LoadPCX: Couldn't read %s", filename );
479 }
480
481
482 /* parse the PCX file */
483 pcx = (pcx_t *)raw;
484 raw = &pcx->data;
485
486 pcx->xmin = LittleShort( pcx->xmin );
487 pcx->ymin = LittleShort( pcx->ymin );
488 pcx->xmax = LittleShort( pcx->xmax );
489 pcx->ymax = LittleShort( pcx->ymax );
490 pcx->hres = LittleShort( pcx->hres );
491 pcx->vres = LittleShort( pcx->vres );
492 pcx->bytes_per_line = LittleShort( pcx->bytes_per_line );
493 pcx->palette_type = LittleShort( pcx->palette_type );
494
495 if ( pcx->manufacturer != 0x0a
496 || pcx->version != 5
497 || pcx->encoding != 1
498 || pcx->bits_per_pixel != 8
499 || pcx->xmax >= 640
500 || pcx->ymax >= 480 ) {
501 Error( "Bad pcx file %s", filename );
502 }
503
504 if ( palette ) {
505 *palette = safe_malloc( 768 );
506 memcpy( *palette, (byte *)pcx + len - 768, 768 );
507 }
508
509 if ( width ) {
510 *width = pcx->xmax + 1;
511 }
512 if ( height ) {
513 *height = pcx->ymax + 1;
514 }
515
516 if ( !pic ) {
517 return;
518 }
519
520 out = safe_malloc( ( pcx->ymax + 1 ) * ( pcx->xmax + 1 ) );
521 if ( !out ) {
522 Error( "LoadPCX: couldn't allocate" );
523 }
524
525 *pic = out;
526 pix = out;
527
528 /* RR2DO2: pcx fix */
529 lsize = pcx->color_planes * pcx->bytes_per_line;
530
531 /* go scanline by scanline */
532 for ( y = 0; y <= pcx->ymax; y++, pix += pcx->xmax + 1 )
533 {
534 /* do a scanline */
535 runLength = 0;
536 for ( x = 0; x <= pcx->xmax; )
537 {
538 /* RR2DO2 */
539 DECODEPCX( raw, dataByte, runLength );
540 while ( runLength-- > 0 )
541 pix[ x++ ] = dataByte;
542 }
543
544 /* RR2DO2: discard any other data */
545 while ( x < lsize )
546 {
547 DECODEPCX( raw, dataByte, runLength );
548 x++;
549 }
550 while ( runLength-- > 0 )
551 x++;
552 }
553
554 /* validity check */
555 if ( raw - (byte *) pcx > len ) {
556 Error( "PCX file %s was malformed", filename );
557 }
558 free( pcx );
559 }
560
561
562
563 /*
564 ==============
565 WritePCXfile
566 ==============
567 */
568 void WritePCXfile( const char *filename, byte *data,
569 int width, int height, byte *palette ){
570 int i, j, length;
571 pcx_t *pcx;
572 byte *pack;
573
574 pcx = safe_malloc0( width * height * 2 + 1000 );
575
576 pcx->manufacturer = 0x0a; // PCX id
577 pcx->version = 5; // 256 color
578 pcx->encoding = 1; // uncompressed
579 pcx->bits_per_pixel = 8; // 256 color
580 pcx->xmin = 0;
581 pcx->ymin = 0;
582 pcx->xmax = LittleShort( (short)( width - 1 ) );
583 pcx->ymax = LittleShort( (short)( height - 1 ) );
584 pcx->hres = LittleShort( (short)width );
585 pcx->vres = LittleShort( (short)height );
586 pcx->color_planes = 1; // chunky image
587 pcx->bytes_per_line = LittleShort( (short)width );
588 pcx->palette_type = LittleShort( 1 ); // not a grey scale
589
590 // pack the image
591 pack = &pcx->data;
592
593 for ( i = 0 ; i < height ; i++ )
594 {
595 for ( j = 0 ; j < width ; j++ )
596 {
597 if ( ( *data & 0xc0 ) != 0xc0 ) {
598 *pack++ = *data++;
599 }
600 else
601 {
602 *pack++ = 0xc1;
603 *pack++ = *data++;
604 }
605 }
606 }
607
608 // write the palette
609 *pack++ = 0x0c; // palette ID byte
610 for ( i = 0 ; i < 768 ; i++ )
611 *pack++ = *palette++;
612
613 // write output file
614 length = pack - (byte *)pcx;
615 SaveFile( filename, pcx, length );
616
617 free( pcx );
618 }
619
620 /*
621 ============================================================================
622
623 LOAD BMP
624
625 ============================================================================
626 */
627
628
629 /*
630
631 // we can't just use these structures, because
632 // compiler structure alignment will not be portable
633 // on this unaligned stuff
634
635 typedef struct tagBITMAPFILEHEADER { // bmfh
636 WORD bfType; // BM
637 DWORD bfSize;
638 WORD bfReserved1;
639 WORD bfReserved2;
640 DWORD bfOffBits;
641 } BITMAPFILEHEADER;
642
643 typedef struct tagBITMAPINFOHEADER{ // bmih
644 DWORD biSize;
645 LONG biWidth;
646 LONG biHeight;
647 WORD biPlanes;
648 WORD biBitCount
649 DWORD biCompression;
650 DWORD biSizeImage;
651 LONG biXPelsPerMeter;
652 LONG biYPelsPerMeter;
653 DWORD biClrUsed;
654 DWORD biClrImportant;
655 } BITMAPINFOHEADER;
656
657 typedef struct tagBITMAPINFO { // bmi
658 BITMAPINFOHEADER bmiHeader;
659 RGBQUAD bmiColors[1];
660 } BITMAPINFO;
661
662 typedef struct tagBITMAPCOREHEADER { // bmch
663 DWORD bcSize;
664 WORD bcWidth;
665 WORD bcHeight;
666 WORD bcPlanes;
667 WORD bcBitCount;
668 } BITMAPCOREHEADER;
669
670 typedef struct _BITMAPCOREINFO { // bmci
671 BITMAPCOREHEADER bmciHeader;
672 RGBTRIPLE bmciColors[1];
673 } BITMAPCOREINFO;
674
675 */
676
677 /*
678 ==============
679 LoadBMP
680 ==============
681 */
682 void LoadBMP( const char *filename, byte **pic, byte **palette, int *width, int *height ){
683 byte *out;
684 int i;
685 int bfOffBits;
686 int structSize;
687 int bcWidth;
688 int bcHeight;
689 int bcPlanes;
690 int bcBitCount;
691 byte bcPalette[1024];
692 qboolean flipped;
693 byte *in;
694 int len, pos = 0;
695
696 len = vfsLoadFile( filename, (void **)&in, 0 );
697 if ( len == -1 ) {
698 Error( "Couldn't read %s", filename );
699 }
700
701 i = bufLittleShort( in, len, &pos );
702 if ( i != 'B' + ( 'M' << 8 ) ) {
703 Error( "%s is not a bmp file", filename );
704 }
705
706 /* bfSize = */ bufLittleLong( in, len, &pos );
707 bufLittleShort( in, len, &pos );
708 bufLittleShort( in, len, &pos );
709 bfOffBits = bufLittleLong( in, len, &pos );
710
711 // the size will tell us if it is a
712 // bitmapinfo or a bitmapcore
713 structSize = bufLittleLong( in, len, &pos );
714 if ( structSize == 40 ) {
715 // bitmapinfo
716 bcWidth = bufLittleLong( in, len, &pos );
717 bcHeight = bufLittleLong( in, len, &pos );
718 bcPlanes = bufLittleShort( in, len, &pos );
719 bcBitCount = bufLittleShort( in, len, &pos );
720
721 pos += 24;
722
723 if ( palette ) {
724 memcpy( bcPalette, in + pos, 1024 );
725 pos += 1024;
726 *palette = safe_malloc( 768 );
727
728 for ( i = 0 ; i < 256 ; i++ )
729 {
730 ( *palette )[i * 3 + 0] = bcPalette[i * 4 + 2];
731 ( *palette )[i * 3 + 1] = bcPalette[i * 4 + 1];
732 ( *palette )[i * 3 + 2] = bcPalette[i * 4 + 0];
733 }
734 }
735 }
736 else if ( structSize == 12 ) {
737 // bitmapcore
738 bcWidth = bufLittleShort( in, len, &pos );
739 bcHeight = bufLittleShort( in, len, &pos );
740 bcPlanes = bufLittleShort( in, len, &pos );
741 bcBitCount = bufLittleShort( in, len, &pos );
742
743 if ( palette ) {
744 memcpy( bcPalette, in + pos, 768 );
745 pos += 768;
746 *palette = safe_malloc( 768 );
747
748 for ( i = 0 ; i < 256 ; i++ ) {
749 ( *palette )[i * 3 + 0] = bcPalette[i * 3 + 2];
750 ( *palette )[i * 3 + 1] = bcPalette[i * 3 + 1];
751 ( *palette )[i * 3 + 2] = bcPalette[i * 3 + 0];
752 }
753 }
754 }
755 else {
756 Error( "%s had strange struct size", filename );
757 }
758
759 if ( bcPlanes != 1 ) {
760 Error( "%s was not a single plane image", filename );
761 }
762
763 if ( bcBitCount != 8 ) {
764 Error( "%s was not an 8 bit image", filename );
765 }
766
767 if ( bcHeight < 0 ) {
768 bcHeight = -bcHeight;
769 flipped = qtrue;
770 }
771 else {
772 flipped = qfalse;
773 }
774
775 if ( width ) {
776 *width = bcWidth;
777 }
778 if ( height ) {
779 *height = bcHeight;
780 }
781
782 if ( !pic ) {
783 free( in );
784 return;
785 }
786
787 out = safe_malloc( bcWidth * bcHeight );
788 *pic = out;
789 pos = bfOffBits;
790
791 if ( flipped ) {
792 for ( i = 0 ; i < bcHeight ; i++ ) {
793 memcpy( out + bcWidth * ( bcHeight - 1 - i ), in + pos, bcWidth );
794 pos += bcWidth;
795 }
796 }
797 else {
798 memcpy( out, in + pos, bcWidth * bcHeight );
799 pos += bcWidth * bcHeight;
800 }
801
802 free( in );
803 }
804
805
806 /*
807 ============================================================================
808
809 LOAD IMAGE
810
811 ============================================================================
812 */
813
814 /*
815 ==============
816 Load256Image
817
818 Will load either an lbm or pcx, depending on extension.
819 Any of the return pointers can be NULL if you don't want them.
820 ==============
821 */
822 void Load256Image( const char *name, byte **pixels, byte **palette, int *width, int *height ){
823 char ext[128];
824
825 ExtractFileExtension( name, ext );
826 if ( !Q_stricmp( ext, "lbm" ) ) {
827 LoadLBM( name, pixels, palette );
828 if ( width ) {
829 *width = bmhd.w;
830 }
831 if ( height ) {
832 *height = bmhd.h;
833 }
834 }
835 else if ( !Q_stricmp( ext, "pcx" ) ) {
836 LoadPCX( name, pixels, palette, width, height );
837 }
838 else if ( !Q_stricmp( ext, "bmp" ) ) {
839 LoadBMP( name, pixels, palette, width, height );
840 }
841 else{
842 Error( "%s doesn't have a known image extension", name );
843 }
844 }
845
846
847 /*
848 ==============
849 Save256Image
850
851 Will save either an lbm or pcx, depending on extension.
852 ==============
853 */
854 void Save256Image( const char *name, byte *pixels, byte *palette,
855 int width, int height ){
856 char ext[128];
857
858 ExtractFileExtension( name, ext );
859 if ( !Q_stricmp( ext, "lbm" ) ) {
860 WriteLBMfile( name, pixels, width, height, palette );
861 }
862 else if ( !Q_stricmp( ext, "pcx" ) ) {
863 WritePCXfile( name, pixels, width, height, palette );
864 }
865 else{
866 Error( "%s doesn't have a known image extension", name );
867 }
868 }
869
870
871
872
873 /*
874 ============================================================================
875
876 TARGA IMAGE
877
878 ============================================================================
879 */
880
881 typedef struct _TargaHeader {
882 unsigned char id_length, colormap_type, image_type;
883 unsigned short colormap_index, colormap_length;
884 unsigned char colormap_size;
885 unsigned short x_origin, y_origin, width, height;
886 unsigned char pixel_size, attributes;
887 } TargaHeader;
888
889 void TargaError( TargaHeader *t, const char *message ){
890 Sys_Printf( "%s\n:TargaHeader:\nuint8 id_length = %i;\nuint8 colormap_type = %i;\nuint8 image_type = %i;\nuint16 colormap_index = %i;\nuint16 colormap_length = %i;\nuint8 colormap_size = %i;\nuint16 x_origin = %i;\nuint16 y_origin = %i;\nuint16 width = %i;\nuint16 height = %i;\nuint8 pixel_size = %i;\nuint8 attributes = %i;\n", message, t->id_length, t->colormap_type, t->image_type, t->colormap_index, t->colormap_length, t->colormap_size, t->x_origin, t->y_origin, t->width, t->height, t->pixel_size, t->attributes );
891 }
892
893 /*
894 =============
895 LoadTGABuffer
896 =============
897 */
898 void LoadTGABuffer( const byte *f, const byte *enddata, byte **pic, int *width, int *height ){
899 int x, y, row_inc, compressed, readpixelcount, red, green, blue, alpha, runlen, pindex, alphabits, image_width, image_height;
900 byte *pixbuf, *image_rgba;
901 const byte *fin;
902 unsigned char *p;
903 TargaHeader targa_header;
904 unsigned char palette[256 * 4];
905
906 *pic = NULL;
907
908 // abort if it is too small to parse
909 if ( enddata - f < 19 ) {
910 return;
911 }
912
913 targa_header.id_length = f[0];
914 targa_header.colormap_type = f[1];
915 targa_header.image_type = f[2];
916
917 targa_header.colormap_index = f[3] + f[4] * 256;
918 targa_header.colormap_length = f[5] + f[6] * 256;
919 targa_header.colormap_size = f[7];
920 targa_header.x_origin = f[8] + f[9] * 256;
921 targa_header.y_origin = f[10] + f[11] * 256;
922 targa_header.width = image_width = f[12] + f[13] * 256;
923 targa_header.height = image_height = f[14] + f[15] * 256;
924
925 targa_header.pixel_size = f[16];
926 targa_header.attributes = f[17];
927
928 // advance to end of header
929 fin = f + 18;
930
931 // skip TARGA image comment (usually 0 bytes)
932 fin += targa_header.id_length;
933
934 // read/skip the colormap if present (note: according to the TARGA spec it
935 // can be present even on truecolor or greyscale images, just not used by
936 // the image data)
937 if ( targa_header.colormap_type ) {
938 if ( targa_header.colormap_length > 256 ) {
939 TargaError( &targa_header, "LoadTGA: only up to 256 colormap_length supported\n" );
940 return;
941 }
942 if ( targa_header.colormap_index ) {
943 TargaError( &targa_header, "LoadTGA: colormap_index not supported\n" );
944 return;
945 }
946 if ( targa_header.colormap_size == 24 ) {
947 for ( x = 0; x < targa_header.colormap_length; x++ )
948 {
949 palette[x * 4 + 2] = *fin++;
950 palette[x * 4 + 1] = *fin++;
951 palette[x * 4 + 0] = *fin++;
952 palette[x * 4 + 3] = 255;
953 }
954 }
955 else if ( targa_header.colormap_size == 32 ) {
956 for ( x = 0; x < targa_header.colormap_length; x++ )
957 {
958 palette[x * 4 + 2] = *fin++;
959 palette[x * 4 + 1] = *fin++;
960 palette[x * 4 + 0] = *fin++;
961 palette[x * 4 + 3] = *fin++;
962 }
963 }
964 else
965 {
966 TargaError( &targa_header, "LoadTGA: Only 32 and 24 bit colormap_size supported\n" );
967 return;
968 }
969 }
970
971 // check our pixel_size restrictions according to image_type
972 if ( targa_header.image_type == 2 || targa_header.image_type == 10 ) {
973 if ( targa_header.pixel_size != 24 && targa_header.pixel_size != 32 ) {
974 TargaError( &targa_header, "LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n" );
975 return;
976 }
977 }
978 else if ( targa_header.image_type == 1 || targa_header.image_type == 9 ) {
979 if ( targa_header.pixel_size != 8 ) {
980 TargaError( &targa_header, "LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n" );
981 return;
982 }
983 }
984 else if ( targa_header.image_type == 3 || targa_header.image_type == 11 ) {
985 if ( targa_header.pixel_size != 8 ) {
986 TargaError( &targa_header, "LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n" );
987 return;
988 }
989 }
990 else
991 {
992 TargaError( &targa_header, "LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported" );
993 return;
994 }
995
996 if ( targa_header.attributes & 0x10 ) {
997 TargaError( &targa_header, "LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n" );
998 return;
999 }
1000
1001 // number of attribute bits per pixel, we only support 0 or 8
1002 alphabits = targa_header.attributes & 0x0F;
1003 if ( alphabits != 8 && alphabits != 0 ) {
1004 TargaError( &targa_header, "LoadTGA: only 0 or 8 attribute (alpha) bits supported\n" );
1005 return;
1006 }
1007
1008 image_rgba = safe_malloc( image_width * image_height * 4 );
1009 if ( !image_rgba ) {
1010 Sys_Printf( "LoadTGA: not enough memory for %i by %i image\n", image_width, image_height );
1011 return;
1012 }
1013
1014 // If bit 5 of attributes isn't set, the image has been stored from bottom to top
1015 if ( ( targa_header.attributes & 0x20 ) == 0 ) {
1016 pixbuf = image_rgba + ( image_height - 1 ) * image_width * 4;
1017 row_inc = -image_width * 4 * 2;
1018 }
1019 else
1020 {
1021 pixbuf = image_rgba;
1022 row_inc = 0;
1023 }
1024
1025 compressed = targa_header.image_type == 9 || targa_header.image_type == 10 || targa_header.image_type == 11;
1026 x = 0;
1027 y = 0;
1028 red = green = blue = alpha = 255;
1029 while ( y < image_height )
1030 {
1031 // decoder is mostly the same whether it's compressed or not
1032 readpixelcount = 1000000;
1033 runlen = 1000000;
1034 if ( compressed && fin < enddata ) {
1035 runlen = *fin++;
1036 // high bit indicates this is an RLE compressed run
1037 if ( runlen & 0x80 ) {
1038 readpixelcount = 1;
1039 }
1040 runlen = 1 + ( runlen & 0x7f );
1041 }
1042
1043 while ( ( runlen-- ) && y < image_height )
1044 {
1045 if ( readpixelcount > 0 ) {
1046 readpixelcount--;
1047 red = green = blue = alpha = 255;
1048 if ( fin < enddata ) {
1049 switch ( targa_header.image_type )
1050 {
1051 case 1:
1052 case 9:
1053 // colormapped
1054 pindex = *fin++;
1055 if ( pindex >= targa_header.colormap_length ) {
1056 pindex = 0; // error
1057 }
1058 p = palette + pindex * 4;
1059 red = p[0];
1060 green = p[1];
1061 blue = p[2];
1062 alpha = p[3];
1063 break;
1064 case 2:
1065 case 10:
1066 // BGR or BGRA
1067 blue = *fin++;
1068 if ( fin < enddata ) {
1069 green = *fin++;
1070 }
1071 if ( fin < enddata ) {
1072 red = *fin++;
1073 }
1074 if ( targa_header.pixel_size == 32 && fin < enddata ) {
1075 alpha = *fin++;
1076 }
1077 break;
1078 case 3:
1079 case 11:
1080 // greyscale
1081 red = green = blue = *fin++;
1082 break;
1083 }
1084 if ( !alphabits ) {
1085 alpha = 255;
1086 }
1087 }
1088 }
1089 *pixbuf++ = red;
1090 *pixbuf++ = green;
1091 *pixbuf++ = blue;
1092 *pixbuf++ = alpha;
1093 x++;
1094 if ( x == image_width ) {
1095 // end of line, advance to next
1096 x = 0;
1097 y++;
1098 pixbuf += row_inc;
1099 }
1100 }
1101 }
1102
1103 *pic = image_rgba;
1104 if ( width ) {
1105 *width = image_width;
1106 }
1107 if ( height ) {
1108 *height = image_height;
1109 }
1110 }
1111
1112
1113 /*
1114 =============
1115 LoadTGA
1116 =============
1117 */
1118 void LoadTGA( const char *name, byte **pixels, int *width, int *height ){
1119 byte *buffer;
1120 int nLen;
1121 //
1122 // load the file
1123 //
1124 nLen = vfsLoadFile( name, (void **)&buffer, 0 );
1125 if ( nLen == -1 ) {
1126 Error( "Couldn't read %s", name );
1127 }
1128
1129 LoadTGABuffer( buffer, buffer + nLen, pixels, width, height );
1130
1131 }
1132
1133
1134 /*
1135 ================
1136 WriteTGA
1137 ================
1138 */
1139 void WriteTGA( const char *filename, byte *data, int width, int height ) {
1140 byte *buffer;
1141 int i;
1142 int c;
1143 FILE *f;
1144
1145 buffer = safe_malloc( width * height * 4 + 18 );
1146 /* we may also use safe_malloc0 on the whole instead,
1147 * this would just be a bit slower */
1148 memset( buffer, 0, 18 );
1149 buffer[2] = 2; // uncompressed type
1150 buffer[12] = width & 255;
1151 buffer[13] = width >> 8;
1152 buffer[14] = height & 255;
1153 buffer[15] = height >> 8;
1154 buffer[16] = 32; // pixel size
1155
1156 // swap rgb to bgr
1157 c = 18 + width * height * 4;
1158 for ( i = 18 ; i < c ; i += 4 )
1159 {
1160 buffer[i] = data[i - 18 + 2]; // blue
1161 buffer[i + 1] = data[i - 18 + 1]; // green
1162 buffer[i + 2] = data[i - 18 + 0]; // red
1163 buffer[i + 3] = data[i - 18 + 3]; // alpha
1164 }
1165
1166 f = fopen( filename, "wb" );
1167 fwrite( buffer, 1, c, f );
1168 fclose( f );
1169
1170 free( buffer );
1171 }
1172
1173 void WriteTGAGray( const char *filename, byte *data, int width, int height ) {
1174 byte buffer[18];
1175 FILE *f;
1176
1177 memset( buffer, 0, 18 );
1178 buffer[2] = 3; // uncompressed type
1179 buffer[12] = width & 255;
1180 buffer[13] = width >> 8;
1181 buffer[14] = height & 255;
1182 buffer[15] = height >> 8;
1183 buffer[16] = 8; // pixel size
1184
1185 f = fopen( filename, "wb" );
1186 fwrite( buffer, 1, 18, f );
1187 fwrite( data, 1, width * height, f );
1188 fclose( f );
1189 }
1190
1191 /*
1192 ============================================================================
1193
1194 LOAD32BITIMAGE
1195
1196 ============================================================================
1197 */
1198
1199 /*
1200 ==============
1201 Load32BitImage
1202
1203 Any of the return pointers can be NULL if you don't want them.
1204 ==============
1205 */
1206 void Load32BitImage( const char *name, unsigned **pixels, int *width, int *height ){
1207 char ext[128];
1208 byte *palette;
1209 byte *pixels8;
1210 byte *pixels32;
1211 int size;
1212 int i;
1213 int v;
1214
1215 ExtractFileExtension( name, ext );
1216 if ( !Q_stricmp( ext, "tga" ) ) {
1217 LoadTGA( name, (byte **)pixels, width, height );
1218 }
1219 else {
1220 Load256Image( name, &pixels8, &palette, width, height );
1221 if ( !pixels ) {
1222 return;
1223 }
1224 size = *width * *height;
1225 pixels32 = safe_malloc( size * 4 );
1226 *pixels = (unsigned *)pixels32;
1227 for ( i = 0 ; i < size ; i++ ) {
1228 v = pixels8[i];
1229 pixels32[i * 4 + 0] = palette[ v * 3 + 0 ];
1230 pixels32[i * 4 + 1] = palette[ v * 3 + 1 ];
1231 pixels32[i * 4 + 2] = palette[ v * 3 + 2 ];
1232 pixels32[i * 4 + 3] = 0xff;
1233 }
1234 }
1235 }
1236
1237
1238 /*
1239 ============================================================================
1240
1241 KHRONOS TEXTURE
1242
1243 ============================================================================
1244 */
1245
1246
1247 #define KTX_UINT32_LE( buf ) ( ( unsigned int )( (buf)[0] | ( (buf)[1] << 8 ) | ( (buf)[2] << 16 ) | ( (buf)[3] << 24 ) ) )
1248 #define KTX_UINT32_BE( buf ) ( ( unsigned int )( (buf)[3] | ( (buf)[2] << 8 ) | ( (buf)[1] << 16 ) | ( (buf)[0] << 24 ) ) )
1249
1250 #define KTX_TYPE_UNSIGNED_BYTE 0x1401
1251 #define KTX_TYPE_UNSIGNED_SHORT_4_4_4_4 0x8033
1252 #define KTX_TYPE_UNSIGNED_SHORT_5_5_5_1 0x8034
1253 #define KTX_TYPE_UNSIGNED_SHORT_5_6_5 0x8363
1254
1255 #define KTX_FORMAT_ALPHA 0x1906
1256 #define KTX_FORMAT_RGB 0x1907
1257 #define KTX_FORMAT_RGBA 0x1908
1258 #define KTX_FORMAT_LUMINANCE 0x1909
1259 #define KTX_FORMAT_LUMINANCE_ALPHA 0x190A
1260 #define KTX_FORMAT_BGR 0x80E0
1261 #define KTX_FORMAT_BGRA 0x80E1
1262
1263 #define KTX_FORMAT_ETC1_RGB8 0x8D64
1264
1265 static void KTX_DecodeA8( const byte *in, qboolean bigEndian, byte *out ){
1266 out[0] = out[1] = out[2] = 0;
1267 out[3] = in[0];
1268 }
1269
1270 static void KTX_DecodeRGB8( const byte *in, qboolean bigEndian, byte *out ){
1271 out[0] = in[0];
1272 out[1] = in[1];
1273 out[2] = in[2];
1274 out[3] = 255;
1275 }
1276
1277 static void KTX_DecodeRGBA8( const byte *in, qboolean bigEndian, byte *out ){
1278 out[0] = in[0];
1279 out[1] = in[1];
1280 out[2] = in[2];
1281 out[3] = in[3];
1282 }
1283
1284 static void KTX_DecodeL8( const byte *in, qboolean bigEndian, byte *out ){
1285 out[0] = out[1] = out[2] = in[0];
1286 out[3] = 255;
1287 }
1288
1289 static void KTX_DecodeLA8( const byte *in, qboolean bigEndian, byte *out ){
1290 out[0] = out[1] = out[2] = in[0];
1291 out[3] = in[1];
1292 }
1293
1294 static void KTX_DecodeBGR8( const byte *in, qboolean bigEndian, byte *out ){
1295 out[0] = in[2];
1296 out[1] = in[1];
1297 out[2] = in[0];
1298 out[3] = 255;
1299 }
1300
1301 static void KTX_DecodeBGRA8( const byte *in, qboolean bigEndian, byte *out ){
1302 out[0] = in[2];
1303 out[1] = in[1];
1304 out[2] = in[0];
1305 out[3] = in[3];
1306 }
1307
1308 static void KTX_DecodeRGBA4( const byte *in, qboolean bigEndian, byte *out ){
1309 unsigned short rgba;
1310 int r, g, b, a;
1311
1312 if ( bigEndian ) {
1313 rgba = ( in[0] << 8 ) | in[1];
1314 }
1315 else {
1316 rgba = ( in[1] << 8 ) | in[0];
1317 }
1318
1319 r = ( rgba >> 12 ) & 0xf;
1320 g = ( rgba >> 8 ) & 0xf;
1321 b = ( rgba >> 4 ) & 0xf;
1322 a = rgba & 0xf;
1323 out[0] = ( r << 4 ) | r;
1324 out[1] = ( g << 4 ) | g;
1325 out[2] = ( b << 4 ) | b;
1326 out[3] = ( a << 4 ) | a;
1327 }
1328
1329 static void KTX_DecodeRGBA5( const byte *in, qboolean bigEndian, byte *out ){
1330 unsigned short rgba;
1331 int r, g, b;
1332
1333 if ( bigEndian ) {
1334 rgba = ( in[0] << 8 ) | in[1];
1335 }
1336 else {
1337 rgba = ( in[1] << 8 ) | in[0];
1338 }
1339
1340 r = ( rgba >> 11 ) & 0x1f;
1341 g = ( rgba >> 6 ) & 0x1f;
1342 b = ( rgba >> 1 ) & 0x1f;
1343 out[0] = ( r << 3 ) | ( r >> 2 );
1344 out[1] = ( g << 3 ) | ( g >> 2 );
1345 out[2] = ( b << 3 ) | ( b >> 2 );
1346 out[3] = ( rgba & 1 ) * 255;
1347 }
1348
1349 static void KTX_DecodeRGB5( const byte *in, qboolean bigEndian, byte *out ){
1350 unsigned short rgba;
1351 int r, g, b;
1352
1353 if ( bigEndian ) {
1354 rgba = ( in[0] << 8 ) | in[1];
1355 }
1356 else {
1357 rgba = ( in[1] << 8 ) | in[0];
1358 }
1359
1360 r = ( rgba >> 11 ) & 0x1f;
1361 g = ( rgba >> 5 ) & 0x3f;
1362 b = rgba & 0x1f;
1363 out[0] = ( r << 3 ) | ( r >> 2 );
1364 out[1] = ( g << 2 ) | ( g >> 4 );
1365 out[2] = ( b << 3 ) | ( b >> 2 );
1366 out[3] = 255;
1367 }
1368
1369 typedef struct
1370 {
1371 unsigned int type;
1372 unsigned int format;
1373 unsigned int pixelSize;
1374 void ( *decode )( const byte *in, qboolean bigEndian, byte *out );
1375 } KTX_UncompressedFormat_t;
1376
1377 static const KTX_UncompressedFormat_t KTX_UncompressedFormats[] =
1378 {
1379 { KTX_TYPE_UNSIGNED_BYTE, KTX_FORMAT_ALPHA, 1, KTX_DecodeA8 },
1380 { KTX_TYPE_UNSIGNED_BYTE, KTX_FORMAT_RGB, 3, KTX_DecodeRGB8 },
1381 { KTX_TYPE_UNSIGNED_BYTE, KTX_FORMAT_RGBA, 4, KTX_DecodeRGBA8 },
1382 { KTX_TYPE_UNSIGNED_BYTE, KTX_FORMAT_LUMINANCE, 1, KTX_DecodeL8 },
1383 { KTX_TYPE_UNSIGNED_BYTE, KTX_FORMAT_LUMINANCE_ALPHA, 2, KTX_DecodeLA8 },
1384 { KTX_TYPE_UNSIGNED_BYTE, KTX_FORMAT_BGR, 3, KTX_DecodeBGR8 },
1385 { KTX_TYPE_UNSIGNED_BYTE, KTX_FORMAT_BGRA, 4, KTX_DecodeBGRA8 },
1386 { KTX_TYPE_UNSIGNED_SHORT_4_4_4_4, KTX_FORMAT_RGBA, 2, KTX_DecodeRGBA4 },
1387 { KTX_TYPE_UNSIGNED_SHORT_5_5_5_1, KTX_FORMAT_RGBA, 2, KTX_DecodeRGBA5 },
1388 { KTX_TYPE_UNSIGNED_SHORT_5_6_5, KTX_FORMAT_RGB, 2, KTX_DecodeRGB5 },
1389 { 0, 0, 0, NULL }
1390 };
1391
1392 static qboolean KTX_DecodeETC1( const byte* in, size_t inSize, unsigned int width, unsigned int height, byte* out ){
1393 unsigned int y, stride = width * 4;
1394 byte rgba[64];
1395
1396 if ( inSize < ( ( ( ( width + 3 ) & ~3 ) * ( ( height + 3 ) & ~3 ) ) >> 1 ) ) {
1397 return qfalse;
1398 }
1399
1400 for ( y = 0; y < height; y += 4, out += stride * 4 )
1401 {
1402 byte *p;
1403 unsigned int x, blockrows;
1404
1405 blockrows = height - y;
1406 if ( blockrows > 4 ) {
1407 blockrows = 4;
1408 }
1409
1410 p = out;
1411 for ( x = 0; x < width; x += 4, p += 16 )
1412 {
1413 unsigned int blockrowsize, blockrow;
1414
1415 ETC_DecodeETC1Block( in, rgba, qtrue );
1416 in += 8;
1417
1418 blockrowsize = width - x;
1419 if ( blockrowsize > 4 ) {
1420 blockrowsize = 4;
1421 }
1422 blockrowsize *= 4;
1423 for ( blockrow = 0; blockrow < blockrows; blockrow++ )
1424 {
1425 memcpy( p + blockrow * stride, rgba + blockrow * 16, blockrowsize );
1426 }
1427 }
1428 }
1429
1430 return qtrue;
1431 }
1432
1433 #define KTX_HEADER_UINT32( buf ) ( bigEndian ? KTX_UINT32_BE( buf ) : KTX_UINT32_LE( buf ) )
1434
1435 void LoadKTXBufferFirstImage( const byte *buffer, size_t bufSize, byte **pic, int *picWidth, int *picHeight ){
1436 unsigned int type, format, width, height, imageOffset;
1437 byte *pixels;
1438
1439 if ( bufSize < 64 ) {
1440 Error( "LoadKTX: Image doesn't have a header" );
1441 }
1442
1443 if ( memcmp( buffer, "\xABKTX 11\xBB\r\n\x1A\n", 12 ) ) {
1444 Error( "LoadKTX: Image has the wrong identifier" );
1445 }
1446
1447 qboolean bigEndian = ( buffer[4] == 4 );
1448
1449 type = KTX_HEADER_UINT32( buffer + 16 );
1450 if ( type ) {
1451 format = KTX_HEADER_UINT32( buffer + 32 );
1452 }
1453 else {
1454 format = KTX_HEADER_UINT32( buffer + 28 );
1455 }
1456
1457 width = KTX_HEADER_UINT32( buffer + 36 );
1458 height = KTX_HEADER_UINT32( buffer + 40 );
1459 if ( !width ) {
1460 Error( "LoadKTX: Image has zero width" );
1461 }
1462 if ( !height ) {
1463 height = 1;
1464 }
1465 if ( picWidth ) {
1466 *picWidth = width;
1467 }
1468 if ( picHeight ) {
1469 *picHeight = height;
1470 }
1471
1472 imageOffset = 64 + KTX_HEADER_UINT32( buffer + 60 ) + 4;
1473 if ( bufSize < imageOffset ) {
1474 Error( "LoadKTX: No image in the file" );
1475 }
1476 buffer += imageOffset;
1477 bufSize -= imageOffset;
1478
1479 pixels = safe_malloc( width * height * 4 );
1480 *pic = pixels;
1481
1482 if ( type ) {
1483 const KTX_UncompressedFormat_t *ktxFormat = KTX_UncompressedFormats;
1484 unsigned int pixelSize;
1485 unsigned int inRowLength, inPadding;
1486 unsigned int y;
1487
1488 while ( ktxFormat->type )
1489 {
1490 if ( ktxFormat->type == type && ktxFormat->format == format ) {
1491 break;
1492 }
1493 ktxFormat++;
1494 }
1495 if ( !ktxFormat->type ) {
1496 Error( "LoadKTX: Image has an unsupported pixel type 0x%X or format 0x%X", type, format );
1497 }
1498
1499 pixelSize = ktxFormat->pixelSize;
1500 inRowLength = width * pixelSize;
1501 inPadding = ( ( inRowLength + 3 ) & ~3 ) - inRowLength;
1502
1503 if ( bufSize < height * ( inRowLength + inPadding ) ) {
1504 Error( "LoadKTX: Image is truncated" );
1505 }
1506
1507 for ( y = 0; y < height; y++ )
1508 {
1509 unsigned int x;
1510 for ( x = 0; x < width; x++, buffer += pixelSize, pixels += 4 )
1511 {
1512 ktxFormat->decode( buffer, bigEndian, pixels );
1513 }
1514 buffer += inPadding;
1515 }
1516 }
1517 else {
1518 qboolean decoded = qfalse;
1519
1520 switch ( format )
1521 {
1522 case KTX_FORMAT_ETC1_RGB8:
1523 decoded = KTX_DecodeETC1( buffer, bufSize, width, height, pixels );
1524 break;
1525 default:
1526 Error( "LoadKTX: Image has an unsupported compressed format format 0x%X", format );
1527 break;
1528 }
1529
1530 if ( !decoded ) {
1531 Error( "LoadKTX: Image is truncated" );
1532 }
1533 }
1534 }