2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
30 int fgetLittleShort( FILE *f ){
36 return (short)( b1 + b2 * 256 );
39 int fgetLittleLong( FILE *f ){
47 return b1 + ( b2 << 8 ) + ( b3 << 16 ) + ( b4 << 24 );
50 int bufLittleShort( byte *buf, int len, int *pos ){
53 if ( ( len - *pos ) < 2 ) {
54 Error( "Unexpected buffer end" );
57 b1 = buf[*pos]; *pos += 1;
58 b2 = buf[*pos]; *pos += 1;
60 return (short)( b1 + b2 * 256 );
63 int bufLittleLong( byte *buf, int len, int *pos ){
66 if ( ( len - *pos ) < 4 ) {
67 Error( "Unexpected buffer end" );
70 b1 = buf[*pos]; *pos += 1;
71 b2 = buf[*pos]; *pos += 1;
72 b3 = buf[*pos]; *pos += 1;
73 b4 = buf[*pos]; *pos += 1;
75 return b1 + ( b2 << 8 ) + ( b3 << 16 ) + ( b4 << 24 );
80 ============================================================================
84 ============================================================================
88 typedef unsigned char UBYTE;
89 //conflicts with windows typedef short WORD;
90 typedef unsigned short UWORD;
115 UWORD transparentColor;
116 UBYTE xAspect,yAspect;
117 short pageWidth,pageHeight;
120 extern bmhd_t bmhd; // will be in native byte order
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 ) )
147 Source must be evenly aligned!
150 byte *LBMRLEDecompress( byte *source,byte *unpacked, int bpwidth ){
161 rept = ( rept ^ 0xff ) + 2;
163 memset( unpacked,b,rept );
166 else if ( rept < 0x80 ) {
168 memcpy( unpacked,source,rept );
173 rept = 0; // rept of 0x80 is NOP
178 } while ( count < bpwidth );
180 if ( count > bpwidth ) {
181 Error( "Decompression exceeded width!\n" );
194 void LoadLBM( const char *filename, byte **picture, byte **palette ){
195 byte *LBMbuffer, *picbuffer, *cmapbuffer;
197 byte *LBM_P, *LBMEND_P;
201 int formtype,formlength;
202 int chunktype,chunklength;
204 // qiet compiler warnings
211 LoadFile( filename, (void **)&LBMbuffer );
214 // parse the LBM header
217 if ( *(int *)LBMbuffer != LittleLong( FORMID ) ) {
218 Error( "No FORM ID at start of file!\n" );
222 formlength = BigLong( *(int *)LBM_P );
224 LBMEND_P = LBM_P + Align( formlength );
226 formtype = LittleLong( *(int *)LBM_P );
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 );
239 while ( LBM_P < LBMEND_P )
241 chunktype = LBM_P[0] + ( LBM_P[1] << 8 ) + ( LBM_P[2] << 16 ) + ( LBM_P[3] << 24 );
243 chunklength = LBM_P[3] + ( LBM_P[2] << 8 ) + ( LBM_P[1] << 16 ) + ( LBM_P[0] << 24 );
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 );
259 cmapbuffer = safe_malloc0( 768 );
260 memcpy( cmapbuffer, LBM_P, chunklength );
266 pic_p = picbuffer = safe_malloc( bmhd.w * bmhd.h );
267 if ( formtype == PBMID ) {
271 for ( y = 0 ; y < bmhd.h ; y++, pic_p += bmhd.w )
273 if ( bmhd.compression == cm_rle1 ) {
274 body_p = LBMRLEDecompress( (byte *)body_p
277 else if ( bmhd.compression == cm_none ) {
278 memcpy( pic_p,body_p,bmhd.w );
279 body_p += Align( bmhd.w );
289 Error( "%s is an interlaced LBM, not packed", filename );
294 LBM_P += Align( chunklength );
299 *picture = picbuffer;
302 *palette = cmapbuffer;
308 ============================================================================
312 ============================================================================
320 void WriteLBMfile( const char *filename, byte *data,
321 int width, int height, byte *palette ){
323 int *formlength, *bmhdlength, *cmaplength, *bodylength;
327 lbm = lbmptr = safe_malloc( width * height + 1000 );
337 formlength = (int*)lbmptr;
338 lbmptr += 4; // leave space for length
353 bmhdlength = (int *)lbmptr;
354 lbmptr += 4; // leave space for length
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 );
365 memcpy( lbmptr,&basebmhd,sizeof( basebmhd ) );
366 lbmptr += sizeof( basebmhd );
368 length = lbmptr - (byte *)bmhdlength - 4;
369 *bmhdlength = BigLong( length );
371 *lbmptr++ = 0; // pad chunk to even offset
382 cmaplength = (int *)lbmptr;
383 lbmptr += 4; // leave space for length
385 memcpy( lbmptr,palette,768 );
388 length = lbmptr - (byte *)cmaplength - 4;
389 *cmaplength = BigLong( length );
391 *lbmptr++ = 0; // pad chunk to even offset
402 bodylength = (int *)lbmptr;
403 lbmptr += 4; // leave space for length
405 memcpy( lbmptr,data,width * height );
406 lbmptr += width * height;
408 length = lbmptr - (byte *)bodylength - 4;
409 *bodylength = BigLong( length );
411 *lbmptr++ = 0; // pad chunk to even offset
417 length = lbmptr - (byte *)formlength - 4;
418 *formlength = BigLong( length );
420 *lbmptr++ = 0; // pad chunk to even offset
426 SaveFile( filename, lbm, lbmptr - lbm );
432 ============================================================================
436 ============================================================================
445 unsigned short xmin,ymin,xmax,ymax;
446 unsigned short hres,vres;
447 unsigned char palette[48];
450 unsigned short bytes_per_line;
451 unsigned short palette_type;
453 unsigned char data; // unbounded
464 #define DECODEPCX( b, d, r ) d = *b++; if ( ( d & 0xC0 ) == 0xC0 ) {r = d & 0x3F; d = *b++; }else{r = 1; }
466 void LoadPCX( const char *filename, byte **pic, byte **palette, int *width, int *height ){
471 int dataByte, runLength;
476 len = vfsLoadFile( filename, (void **)&raw, 0 );
478 Error( "LoadPCX: Couldn't read %s", filename );
482 /* parse the PCX file */
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 );
495 if ( pcx->manufacturer != 0x0a
497 || pcx->encoding != 1
498 || pcx->bits_per_pixel != 8
500 || pcx->ymax >= 480 ) {
501 Error( "Bad pcx file %s", filename );
505 *palette = safe_malloc( 768 );
506 memcpy( *palette, (byte *)pcx + len - 768, 768 );
510 *width = pcx->xmax + 1;
513 *height = pcx->ymax + 1;
520 out = safe_malloc( ( pcx->ymax + 1 ) * ( pcx->xmax + 1 ) );
522 Error( "LoadPCX: couldn't allocate" );
528 /* RR2DO2: pcx fix */
529 lsize = pcx->color_planes * pcx->bytes_per_line;
531 /* go scanline by scanline */
532 for ( y = 0; y <= pcx->ymax; y++, pix += pcx->xmax + 1 )
536 for ( x = 0; x <= pcx->xmax; )
539 DECODEPCX( raw, dataByte, runLength );
540 while ( runLength-- > 0 )
541 pix[ x++ ] = dataByte;
544 /* RR2DO2: discard any other data */
547 DECODEPCX( raw, dataByte, runLength );
550 while ( runLength-- > 0 )
555 if ( raw - (byte *) pcx > len ) {
556 Error( "PCX file %s was malformed", filename );
568 void WritePCXfile( const char *filename, byte *data,
569 int width, int height, byte *palette ){
574 pcx = safe_malloc0( width * height * 2 + 1000 );
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
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
593 for ( i = 0 ; i < height ; i++ )
595 for ( j = 0 ; j < width ; j++ )
597 if ( ( *data & 0xc0 ) != 0xc0 ) {
609 *pack++ = 0x0c; // palette ID byte
610 for ( i = 0 ; i < 768 ; i++ )
611 *pack++ = *palette++;
614 length = pack - (byte *)pcx;
615 SaveFile( filename, pcx, length );
621 ============================================================================
625 ============================================================================
631 // we can't just use these structures, because
632 // compiler structure alignment will not be portable
633 // on this unaligned stuff
635 typedef struct tagBITMAPFILEHEADER { // bmfh
643 typedef struct tagBITMAPINFOHEADER{ // bmih
651 LONG biXPelsPerMeter;
652 LONG biYPelsPerMeter;
654 DWORD biClrImportant;
657 typedef struct tagBITMAPINFO { // bmi
658 BITMAPINFOHEADER bmiHeader;
659 RGBQUAD bmiColors[1];
662 typedef struct tagBITMAPCOREHEADER { // bmch
670 typedef struct _BITMAPCOREINFO { // bmci
671 BITMAPCOREHEADER bmciHeader;
672 RGBTRIPLE bmciColors[1];
682 void LoadBMP( const char *filename, byte **pic, byte **palette, int *width, int *height ){
691 byte bcPalette[1024];
696 len = vfsLoadFile( filename, (void **)&in, 0 );
698 Error( "Couldn't read %s", filename );
701 i = bufLittleShort( in, len, &pos );
702 if ( i != 'B' + ( 'M' << 8 ) ) {
703 Error( "%s is not a bmp file", filename );
706 /* bfSize = */ bufLittleLong( in, len, &pos );
707 bufLittleShort( in, len, &pos );
708 bufLittleShort( in, len, &pos );
709 bfOffBits = bufLittleLong( in, len, &pos );
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 ) {
716 bcWidth = bufLittleLong( in, len, &pos );
717 bcHeight = bufLittleLong( in, len, &pos );
718 bcPlanes = bufLittleShort( in, len, &pos );
719 bcBitCount = bufLittleShort( in, len, &pos );
724 memcpy( bcPalette, in + pos, 1024 );
726 *palette = safe_malloc( 768 );
728 for ( i = 0 ; i < 256 ; i++ )
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];
736 else if ( structSize == 12 ) {
738 bcWidth = bufLittleShort( in, len, &pos );
739 bcHeight = bufLittleShort( in, len, &pos );
740 bcPlanes = bufLittleShort( in, len, &pos );
741 bcBitCount = bufLittleShort( in, len, &pos );
744 memcpy( bcPalette, in + pos, 768 );
746 *palette = safe_malloc( 768 );
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];
756 Error( "%s had strange struct size", filename );
759 if ( bcPlanes != 1 ) {
760 Error( "%s was not a single plane image", filename );
763 if ( bcBitCount != 8 ) {
764 Error( "%s was not an 8 bit image", filename );
767 if ( bcHeight < 0 ) {
768 bcHeight = -bcHeight;
787 out = safe_malloc( bcWidth * bcHeight );
792 for ( i = 0 ; i < bcHeight ; i++ ) {
793 memcpy( out + bcWidth * ( bcHeight - 1 - i ), in + pos, bcWidth );
798 memcpy( out, in + pos, bcWidth * bcHeight );
799 pos += bcWidth * bcHeight;
807 ============================================================================
811 ============================================================================
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.
822 void Load256Image( const char *name, byte **pixels, byte **palette, int *width, int *height ){
825 ExtractFileExtension( name, ext );
826 if ( !Q_stricmp( ext, "lbm" ) ) {
827 LoadLBM( name, pixels, palette );
835 else if ( !Q_stricmp( ext, "pcx" ) ) {
836 LoadPCX( name, pixels, palette, width, height );
838 else if ( !Q_stricmp( ext, "bmp" ) ) {
839 LoadBMP( name, pixels, palette, width, height );
842 Error( "%s doesn't have a known image extension", name );
851 Will save either an lbm or pcx, depending on extension.
854 void Save256Image( const char *name, byte *pixels, byte *palette,
855 int width, int height ){
858 ExtractFileExtension( name, ext );
859 if ( !Q_stricmp( ext, "lbm" ) ) {
860 WriteLBMfile( name, pixels, width, height, palette );
862 else if ( !Q_stricmp( ext, "pcx" ) ) {
863 WritePCXfile( name, pixels, width, height, palette );
866 Error( "%s doesn't have a known image extension", name );
874 ============================================================================
878 ============================================================================
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;
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 );
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;
903 TargaHeader targa_header;
904 unsigned char palette[256 * 4];
908 // abort if it is too small to parse
909 if ( enddata - f < 19 ) {
913 targa_header.id_length = f[0];
914 targa_header.colormap_type = f[1];
915 targa_header.image_type = f[2];
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;
925 targa_header.pixel_size = f[16];
926 targa_header.attributes = f[17];
928 // advance to end of header
931 // skip TARGA image comment (usually 0 bytes)
932 fin += targa_header.id_length;
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
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" );
942 if ( targa_header.colormap_index ) {
943 TargaError( &targa_header, "LoadTGA: colormap_index not supported\n" );
946 if ( targa_header.colormap_size == 24 ) {
947 for ( x = 0; x < targa_header.colormap_length; x++ )
949 palette[x * 4 + 2] = *fin++;
950 palette[x * 4 + 1] = *fin++;
951 palette[x * 4 + 0] = *fin++;
952 palette[x * 4 + 3] = 255;
955 else if ( targa_header.colormap_size == 32 ) {
956 for ( x = 0; x < targa_header.colormap_length; x++ )
958 palette[x * 4 + 2] = *fin++;
959 palette[x * 4 + 1] = *fin++;
960 palette[x * 4 + 0] = *fin++;
961 palette[x * 4 + 3] = *fin++;
966 TargaError( &targa_header, "LoadTGA: Only 32 and 24 bit colormap_size supported\n" );
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" );
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" );
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" );
992 TargaError( &targa_header, "LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported" );
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" );
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" );
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 );
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;
1021 pixbuf = image_rgba;
1025 compressed = targa_header.image_type == 9 || targa_header.image_type == 10 || targa_header.image_type == 11;
1028 red = green = blue = alpha = 255;
1029 while ( y < image_height )
1031 // decoder is mostly the same whether it's compressed or not
1032 readpixelcount = 1000000;
1034 if ( compressed && fin < enddata ) {
1036 // high bit indicates this is an RLE compressed run
1037 if ( runlen & 0x80 ) {
1040 runlen = 1 + ( runlen & 0x7f );
1043 while ( ( runlen-- ) && y < image_height )
1045 if ( readpixelcount > 0 ) {
1047 red = green = blue = alpha = 255;
1048 if ( fin < enddata ) {
1049 switch ( targa_header.image_type )
1055 if ( pindex >= targa_header.colormap_length ) {
1056 pindex = 0; // error
1058 p = palette + pindex * 4;
1068 if ( fin < enddata ) {
1071 if ( fin < enddata ) {
1074 if ( targa_header.pixel_size == 32 && fin < enddata ) {
1081 red = green = blue = *fin++;
1094 if ( x == image_width ) {
1095 // end of line, advance to next
1105 *width = image_width;
1108 *height = image_height;
1118 void LoadTGA( const char *name, byte **pixels, int *width, int *height ){
1124 nLen = vfsLoadFile( name, (void **)&buffer, 0 );
1126 Error( "Couldn't read %s", name );
1129 LoadTGABuffer( buffer, buffer + nLen, pixels, width, height );
1139 void WriteTGA( const char *filename, byte *data, int width, int height ) {
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
1157 c = 18 + width * height * 4;
1158 for ( i = 18 ; i < c ; i += 4 )
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
1166 f = fopen( filename, "wb" );
1167 fwrite( buffer, 1, c, f );
1173 void WriteTGAGray( const char *filename, byte *data, int width, int height ) {
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
1185 f = fopen( filename, "wb" );
1186 fwrite( buffer, 1, 18, f );
1187 fwrite( data, 1, width * height, f );
1192 ============================================================================
1196 ============================================================================
1203 Any of the return pointers can be NULL if you don't want them.
1206 void Load32BitImage( const char *name, unsigned **pixels, int *width, int *height ){
1215 ExtractFileExtension( name, ext );
1216 if ( !Q_stricmp( ext, "tga" ) ) {
1217 LoadTGA( name, (byte **)pixels, width, height );
1220 Load256Image( name, &pixels8, &palette, width, height );
1224 size = *width * *height;
1225 pixels32 = safe_malloc( size * 4 );
1226 *pixels = (unsigned *)pixels32;
1227 for ( i = 0 ; i < size ; 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;
1239 ============================================================================
1243 ============================================================================
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 ) ) )
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
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
1263 #define KTX_FORMAT_ETC1_RGB8 0x8D64
1265 static void KTX_DecodeA8( const byte *in, qboolean bigEndian, byte *out ){
1266 out[0] = out[1] = out[2] = 0;
1270 static void KTX_DecodeRGB8( const byte *in, qboolean bigEndian, byte *out ){
1277 static void KTX_DecodeRGBA8( const byte *in, qboolean bigEndian, byte *out ){
1284 static void KTX_DecodeL8( const byte *in, qboolean bigEndian, byte *out ){
1285 out[0] = out[1] = out[2] = in[0];
1289 static void KTX_DecodeLA8( const byte *in, qboolean bigEndian, byte *out ){
1290 out[0] = out[1] = out[2] = in[0];
1294 static void KTX_DecodeBGR8( const byte *in, qboolean bigEndian, byte *out ){
1301 static void KTX_DecodeBGRA8( const byte *in, qboolean bigEndian, byte *out ){
1308 static void KTX_DecodeRGBA4( const byte *in, qboolean bigEndian, byte *out ){
1309 unsigned short rgba;
1313 rgba = ( in[0] << 8 ) | in[1];
1316 rgba = ( in[1] << 8 ) | in[0];
1319 r = ( rgba >> 12 ) & 0xf;
1320 g = ( rgba >> 8 ) & 0xf;
1321 b = ( rgba >> 4 ) & 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;
1329 static void KTX_DecodeRGBA5( const byte *in, qboolean bigEndian, byte *out ){
1330 unsigned short rgba;
1334 rgba = ( in[0] << 8 ) | in[1];
1337 rgba = ( in[1] << 8 ) | in[0];
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;
1349 static void KTX_DecodeRGB5( const byte *in, qboolean bigEndian, byte *out ){
1350 unsigned short rgba;
1354 rgba = ( in[0] << 8 ) | in[1];
1357 rgba = ( in[1] << 8 ) | in[0];
1360 r = ( rgba >> 11 ) & 0x1f;
1361 g = ( rgba >> 5 ) & 0x3f;
1363 out[0] = ( r << 3 ) | ( r >> 2 );
1364 out[1] = ( g << 2 ) | ( g >> 4 );
1365 out[2] = ( b << 3 ) | ( b >> 2 );
1372 unsigned int format;
1373 unsigned int pixelSize;
1374 void ( *decode )( const byte *in, qboolean bigEndian, byte *out );
1375 } KTX_UncompressedFormat_t;
1377 static const KTX_UncompressedFormat_t KTX_UncompressedFormats[] =
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 },
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;
1396 if ( inSize < ( ( ( ( width + 3 ) & ~3 ) * ( ( height + 3 ) & ~3 ) ) >> 1 ) ) {
1400 for ( y = 0; y < height; y += 4, out += stride * 4 )
1403 unsigned int x, blockrows;
1405 blockrows = height - y;
1406 if ( blockrows > 4 ) {
1411 for ( x = 0; x < width; x += 4, p += 16 )
1413 unsigned int blockrowsize, blockrow;
1415 ETC_DecodeETC1Block( in, rgba, qtrue );
1418 blockrowsize = width - x;
1419 if ( blockrowsize > 4 ) {
1423 for ( blockrow = 0; blockrow < blockrows; blockrow++ )
1425 memcpy( p + blockrow * stride, rgba + blockrow * 16, blockrowsize );
1433 #define KTX_HEADER_UINT32( buf ) ( bigEndian ? KTX_UINT32_BE( buf ) : KTX_UINT32_LE( buf ) )
1435 void LoadKTXBufferFirstImage( const byte *buffer, size_t bufSize, byte **pic, int *picWidth, int *picHeight ){
1436 unsigned int type, format, width, height, imageOffset;
1439 if ( bufSize < 64 ) {
1440 Error( "LoadKTX: Image doesn't have a header" );
1443 if ( memcmp( buffer, "\xABKTX 11\xBB\r\n\x1A\n", 12 ) ) {
1444 Error( "LoadKTX: Image has the wrong identifier" );
1447 qboolean bigEndian = ( buffer[4] == 4 );
1449 type = KTX_HEADER_UINT32( buffer + 16 );
1451 format = KTX_HEADER_UINT32( buffer + 32 );
1454 format = KTX_HEADER_UINT32( buffer + 28 );
1457 width = KTX_HEADER_UINT32( buffer + 36 );
1458 height = KTX_HEADER_UINT32( buffer + 40 );
1460 Error( "LoadKTX: Image has zero width" );
1469 *picHeight = height;
1472 imageOffset = 64 + KTX_HEADER_UINT32( buffer + 60 ) + 4;
1473 if ( bufSize < imageOffset ) {
1474 Error( "LoadKTX: No image in the file" );
1476 buffer += imageOffset;
1477 bufSize -= imageOffset;
1479 pixels = safe_malloc( width * height * 4 );
1483 const KTX_UncompressedFormat_t *ktxFormat = KTX_UncompressedFormats;
1484 unsigned int pixelSize;
1485 unsigned int inRowLength, inPadding;
1488 while ( ktxFormat->type )
1490 if ( ktxFormat->type == type && ktxFormat->format == format ) {
1495 if ( !ktxFormat->type ) {
1496 Error( "LoadKTX: Image has an unsupported pixel type 0x%X or format 0x%X", type, format );
1499 pixelSize = ktxFormat->pixelSize;
1500 inRowLength = width * pixelSize;
1501 inPadding = ( ( inRowLength + 3 ) & ~3 ) - inRowLength;
1503 if ( bufSize < height * ( inRowLength + inPadding ) ) {
1504 Error( "LoadKTX: Image is truncated" );
1507 for ( y = 0; y < height; y++ )
1510 for ( x = 0; x < width; x++, buffer += pixelSize, pixels += 4 )
1512 ktxFormat->decode( buffer, bigEndian, pixels );
1514 buffer += inPadding;
1518 qboolean decoded = qfalse;
1522 case KTX_FORMAT_ETC1_RGB8:
1523 decoded = KTX_DecodeETC1( buffer, bufSize, width, height, pixels );
1526 Error( "LoadKTX: Image has an unsupported compressed format format 0x%X", format );
1531 Error( "LoadKTX: Image is truncated" );