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
28 // Ups the palette values so no pixels except 0 appear transparent
29 // Need a value of 8 to cater for 16bit renderers
39 void CorrectPalette( byte *pal ){
42 p = (paletteRGB_t *)pal;
43 // Color 0 always transparent
50 ============================================================================
54 ============================================================================
58 typedef unsigned char UBYTE;
59 //conflicts with windows typedef short WORD;
60 typedef unsigned short UWORD;
85 UWORD transparentColor;
86 UBYTE xAspect,yAspect;
87 short pageWidth,pageHeight;
90 extern bmhd_t bmhd; // will be in native byte order
94 #define FORMID ( 'F' + ( 'O' << 8 ) + ( (int)'R' << 16 ) + ( (int)'M' << 24 ) )
95 #define ILBMID ( 'I' + ( 'L' << 8 ) + ( (int)'B' << 16 ) + ( (int)'M' << 24 ) )
96 #define PBMID ( 'P' + ( 'B' << 8 ) + ( (int)'M' << 16 ) + ( (int)' ' << 24 ) )
97 #define BMHDID ( 'B' + ( 'M' << 8 ) + ( (int)'H' << 16 ) + ( (int)'D' << 24 ) )
98 #define BODYID ( 'B' + ( 'O' << 8 ) + ( (int)'D' << 16 ) + ( (int)'Y' << 24 ) )
99 #define CMAPID ( 'C' + ( 'M' << 8 ) + ( (int)'A' << 16 ) + ( (int)'P' << 24 ) )
117 Source must be evenly aligned!
120 byte *LBMRLEDecompress( byte *source,byte *unpacked, int bpwidth ){
131 rept = ( rept ^ 0xff ) + 2;
133 memset( unpacked,b,rept );
136 else if ( rept < 0x80 ) {
138 memcpy( unpacked,source,rept );
143 rept = 0; // rept of 0x80 is NOP
148 } while ( count < bpwidth );
150 if ( count > bpwidth ) {
151 Error( "Decompression exceeded width!\n" );
164 void LoadLBM( char *filename, byte **picture, byte **palette ){
165 byte *LBMbuffer, *picbuffer, *cmapbuffer;
167 byte *LBM_P, *LBMEND_P;
171 int formtype,formlength;
172 int chunktype,chunklength;
174 // qiet compiler warnings
181 LoadFile( filename, (void **)&LBMbuffer );
184 // parse the LBM header
187 if ( *(int *)LBMbuffer != LittleLong( FORMID ) ) {
188 Error( "No FORM ID at start of file!\n" );
192 formlength = BigLong( *(int *)LBM_P );
194 LBMEND_P = LBM_P + Align( formlength );
196 formtype = LittleLong( *(int *)LBM_P );
198 if ( formtype != ILBMID && formtype != PBMID ) {
199 Error( "Unrecognized form type: %c%c%c%c\n", formtype & 0xff
200 ,( formtype >> 8 ) & 0xff,( formtype >> 16 ) & 0xff,( formtype >> 24 ) & 0xff );
209 while ( LBM_P < LBMEND_P )
211 chunktype = LBM_P[0] + ( LBM_P[1] << 8 ) + ( LBM_P[2] << 16 ) + ( LBM_P[3] << 24 );
213 chunklength = LBM_P[3] + ( LBM_P[2] << 8 ) + ( LBM_P[1] << 16 ) + ( LBM_P[0] << 24 );
219 memcpy( &bmhd,LBM_P,sizeof( bmhd ) );
220 bmhd.w = BigShort( bmhd.w );
221 bmhd.h = BigShort( bmhd.h );
222 bmhd.x = BigShort( bmhd.x );
223 bmhd.y = BigShort( bmhd.y );
224 bmhd.pageWidth = BigShort( bmhd.pageWidth );
225 bmhd.pageHeight = BigShort( bmhd.pageHeight );
229 cmapbuffer = malloc( 768 );
230 memset( cmapbuffer, 0, 768 );
231 memcpy( cmapbuffer, LBM_P, chunklength );
232 CorrectPalette( cmapbuffer );
238 pic_p = picbuffer = malloc( bmhd.w * bmhd.h );
239 if ( formtype == PBMID ) {
243 for ( y = 0 ; y < bmhd.h ; y++, pic_p += bmhd.w )
245 if ( bmhd.compression == cm_rle1 ) {
246 body_p = LBMRLEDecompress( (byte *)body_p
249 else if ( bmhd.compression == cm_none ) {
250 memcpy( pic_p,body_p,bmhd.w );
251 body_p += Align( bmhd.w );
261 Error( "%s is an interlaced LBM, not packed", filename );
266 LBM_P += Align( chunklength );
271 *picture = picbuffer;
274 *palette = cmapbuffer;
280 ============================================================================
284 ============================================================================
292 void WriteLBMfile( char *filename, byte *data,
293 int width, int height, byte *palette ){
295 int *formlength, *bmhdlength, *cmaplength, *bodylength;
299 lbm = lbmptr = malloc( width * height + 1000 );
309 formlength = (int*)lbmptr;
310 lbmptr += 4; // leave space for length
325 bmhdlength = (int *)lbmptr;
326 lbmptr += 4; // leave space for length
328 memset( &basebmhd,0,sizeof( basebmhd ) );
329 basebmhd.w = BigShort( (short)width );
330 basebmhd.h = BigShort( (short)height );
331 basebmhd.nPlanes = BigShort( 8 );
332 basebmhd.xAspect = BigShort( 5 );
333 basebmhd.yAspect = BigShort( 6 );
334 basebmhd.pageWidth = BigShort( (short)width );
335 basebmhd.pageHeight = BigShort( (short)height );
337 memcpy( lbmptr,&basebmhd,sizeof( basebmhd ) );
338 lbmptr += sizeof( basebmhd );
340 length = lbmptr - (byte *)bmhdlength - 4;
341 *bmhdlength = BigLong( length );
343 *lbmptr++ = 0; // pad chunk to even offset
354 cmaplength = (int *)lbmptr;
355 lbmptr += 4; // leave space for length
357 memcpy( lbmptr,palette,768 );
360 length = lbmptr - (byte *)cmaplength - 4;
361 *cmaplength = BigLong( length );
363 *lbmptr++ = 0; // pad chunk to even offset
374 bodylength = (int *)lbmptr;
375 lbmptr += 4; // leave space for length
377 memcpy( lbmptr,data,width * height );
378 lbmptr += width * height;
380 length = lbmptr - (byte *)bodylength - 4;
381 *bodylength = BigLong( length );
383 *lbmptr++ = 0; // pad chunk to even offset
389 length = lbmptr - (byte *)formlength - 4;
390 *formlength = BigLong( length );
392 *lbmptr++ = 0; // pad chunk to even offset
398 SaveFile( filename, lbm, lbmptr - lbm );
404 ============================================================================
408 ============================================================================
417 unsigned short xmin,ymin,xmax,ymax;
418 unsigned short hres,vres;
419 unsigned char palette[48];
422 unsigned short bytes_per_line;
423 unsigned short palette_type;
425 unsigned char data; // unbounded
433 void LoadPCX( char *filename, byte **pic, byte **palette, int *width, int *height ){
438 int dataByte, runLength;
444 len = LoadFile( filename, (void **)&raw );
447 // parse the PCX file
452 pcx->xmin = LittleShort( pcx->xmin );
453 pcx->ymin = LittleShort( pcx->ymin );
454 pcx->xmax = LittleShort( pcx->xmax );
455 pcx->ymax = LittleShort( pcx->ymax );
456 pcx->hres = LittleShort( pcx->hres );
457 pcx->vres = LittleShort( pcx->vres );
458 pcx->bytes_per_line = LittleShort( pcx->bytes_per_line );
459 pcx->palette_type = LittleShort( pcx->palette_type );
461 if ( pcx->manufacturer != 0x0a
463 || pcx->encoding != 1
464 || pcx->bits_per_pixel != 8
466 || pcx->ymax >= 480 ) {
467 Error( "Bad pcx file %s", filename );
471 *palette = malloc( 768 );
472 memcpy( *palette, (byte *)pcx + len - 768, 768 );
473 CorrectPalette( *palette );
477 *width = pcx->xmax + 1;
480 *height = pcx->ymax + 1;
487 out = malloc( ( pcx->ymax + 1 ) * ( pcx->xmax + 1 ) );
489 Error( "Skin_Cache: couldn't allocate" );
496 for ( y = 0 ; y <= pcx->ymax ; y++, pix += pcx->xmax + 1 )
498 for ( x = 0 ; x <= pcx->xmax ; )
502 if ( ( dataByte & 0xC0 ) == 0xC0 ) {
503 runLength = dataByte & 0x3F;
510 while ( runLength-- > 0 )
516 if ( raw - (byte *)pcx > len ) {
517 Error( "PCX file %s was malformed", filename );
529 void StuffPackedByte( int curRepCount, byte curByte, byte** packPtr ){
534 while ( curRepCount > 0 )
536 if ( curRepCount == 1 ) {
537 if ( ( curByte & 0xc0 ) != 0xc0 ) {
547 if ( curRepCount < 0x0040 ) {
548 *pack++ = ( 0x00c0 | curRepCount );
554 curRepCount -= 0x003f;
561 void WritePCXfile( char *filename, byte *data,
562 int width, int height, byte *palette ){
569 pcx = malloc( width * height * 2 + 1000 );
570 memset( pcx, 0, sizeof( *pcx ) );
572 pcx->manufacturer = 0x0a; // PCX id
573 pcx->version = 5; // 256 color
574 pcx->encoding = 1; // RLE
575 pcx->bits_per_pixel = 8; // 256 color
578 pcx->xmax = LittleShort( (short)( width - 1 ) );
579 pcx->ymax = LittleShort( (short)( height - 1 ) );
580 pcx->hres = LittleShort( (short)width );
581 pcx->vres = LittleShort( (short)height );
582 pcx->color_planes = 1; // chunky image
583 pcx->bytes_per_line = LittleShort( (short)width );
584 pcx->palette_type = LittleShort( 1 ); // not a grey scale
589 /* for (i=0 ; i<height ; i++)
591 for (j=0 ; j<width ; j++)
593 if ( (*data & 0xc0) != 0xc0)
603 for ( i = 0 ; i < height ; i++ )
607 for ( j = 0 ; j < width ; j++ )
609 if ( *data == curByte ) {
614 StuffPackedByte( curRepCount, curByte, &pack );
618 StuffPackedByte( curRepCount, curByte, &pack );
621 *pack++ = 0x0c; // palette ID byte
622 for ( i = 0 ; i < 768 ; i++ )
623 *pack++ = *palette++;
626 length = pack - (byte *)pcx;
627 SaveFile( filename, pcx, length );
634 ============================================================================
638 ============================================================================
645 Will load either an lbm or pcx, depending on extension.
646 Any of the return pointers can be NULL if you don't want them.
649 void Load256Image( char *name, byte **pixels, byte **palette,
650 int *width, int *height ){
653 ExtractFileExtension( name, ext );
654 if ( !Q_strcasecmp( ext, "lbm" ) ) {
655 LoadLBM( name, pixels, palette );
663 else if ( !Q_strcasecmp( ext, "pcx" ) ) {
664 LoadPCX( name, pixels, palette, width, height );
667 Error( "%s doesn't have a known image extension", name );
676 Will save either an lbm or pcx, depending on extension.
679 void Save256Image( char *name, byte *pixels, byte *palette,
680 int width, int height ){
683 ExtractFileExtension( name, ext );
684 if ( !Q_strcasecmp( ext, "lbm" ) ) {
685 WriteLBMfile( name, pixels, width, height, palette );
687 else if ( !Q_strcasecmp( ext, "pcx" ) ) {
688 WritePCXfile( name, pixels, width, height, palette );
691 Error( "%s doesn't have a known image extension", name );
699 ============================================================================
703 ============================================================================
706 typedef struct _TargaHeader
708 unsigned char id_length, colormap_type, image_type;
709 unsigned short colormap_index, colormap_length;
710 unsigned char colormap_size;
711 unsigned short x_origin, y_origin, width, height;
712 unsigned char pixel_size, attributes;
715 int fgetLittleShort( FILE *f ){
721 return( (short)( b1 + ( b2 << 8 ) ) );
724 int fgetLittleLong( FILE *f ){
732 return( b1 + ( b2 << 8 ) + ( b3 << 16 ) + ( b4 << 24 ) );
741 void LoadTGA( char *name, byte **pixels, int *width, int *height ){
742 int columns, rows, numPixels;
748 TargaHeader targa_header;
749 unsigned char red, green, blue, alphabyte;
750 unsigned char packetHeader, packetSize, j;
756 fin = fopen( name, "rb" );
758 Error( "Couldn't read %s", name );
761 targa_header.id_length = fgetc( fin );
762 targa_header.colormap_type = fgetc( fin );
763 targa_header.image_type = fgetc( fin );
765 targa_header.colormap_index = fgetLittleShort( fin );
766 targa_header.colormap_length = fgetLittleShort( fin );
767 targa_header.colormap_size = fgetc( fin );
768 targa_header.x_origin = fgetLittleShort( fin );
769 targa_header.y_origin = fgetLittleShort( fin );
770 targa_header.width = fgetLittleShort( fin );
771 targa_header.height = fgetLittleShort( fin );
772 targa_header.pixel_size = fgetc( fin );
773 targa_header.attributes = fgetc( fin );
774 flip = ( targa_header.attributes & 0x020 ) == 0;
775 mirror = ( targa_header.attributes & 0x010 ) != 0;
777 if ( ( targa_header.image_type != 2 ) && ( targa_header.image_type != 10 ) ) {
778 Error( "LoadTGA: Only type 2 and 10 targa RGB images supported\n" );
781 if ( targa_header.colormap_type || ( ( targa_header.pixel_size != 32 ) && ( targa_header.pixel_size != 24 ) ) ) {
782 Error( "Texture_LoadTGA: Only 32 or 24 bit images supported (no colormaps)\n" );
785 columns = targa_header.width;
786 rows = targa_header.height;
787 numPixels = columns * rows;
800 targa_rgba = malloc( numPixels * 4 );
801 *pixels = targa_rgba;
804 pixbuf = targa_rgba + ( ( rows - 1 ) * columns * 4 );
805 rowOffset = -columns * 4;
810 rowOffset = columns * 4;
814 pixbuf += ( ( columns - 1 ) * 4 );
821 if ( targa_header.id_length ) {
822 fseek( fin, targa_header.id_length, SEEK_CUR ); // skip TARGA image comment
825 if ( targa_header.image_type == 2 ) { // Uncompressed, RGB images
826 for ( row = 0; row < rows; row++ )
829 for ( column = 0; column < columns; column++ )
831 switch ( targa_header.pixel_size )
841 rowBuf += pixDirection;
847 alphabyte = getc( fin );
851 rowBuf[3] = alphabyte;
852 rowBuf += pixDirection;
859 else if ( targa_header.image_type == 10 ) { // Runlength encoded RGB images
860 for ( row = 0; row < rows; row++ )
863 for ( column = 0; column < columns; )
865 packetHeader = getc( fin );
866 packetSize = 1 + ( packetHeader & 0x7f );
867 if ( packetHeader & 0x80 ) { // run-length packet
868 switch ( targa_header.pixel_size )
880 alphabyte = getc( fin );
884 for ( j = 0; j < packetSize; j++ )
889 rowBuf[3] = alphabyte;
890 rowBuf += pixDirection;
892 if ( column == columns ) { // run spans across rows
904 { // non run-length packet
905 for ( j = 0; j < packetSize; j++ )
907 switch ( targa_header.pixel_size )
917 rowBuf += pixDirection;
923 alphabyte = getc( fin );
927 rowBuf[3] = alphabyte;
928 rowBuf += pixDirection;
932 if ( column == columns ) { // pixel packet run spans across rows
951 void MergeAlpha( byte *pix, byte *alpha, byte *pal, byte **out, int width, int height ){
953 byte *data, *src, *srca;
955 size = width * height;
956 data = malloc( size * 4 );
958 Error( "Could not allocate memory for true color image" );
965 for ( i = 0; i < size; i++, src++, srca++ )
967 *data++ = pal[*src * 3 + 0]; // r
968 *data++ = pal[*src * 3 + 1]; // g
969 *data++ = pal[*src * 3 + 2]; // b
970 *data++ = *srca; // a
982 false: paletted texture
983 true: true color RGBA image (no palette)
986 qboolean LoadAnyImage( char *name, byte **pixels, byte **palette, int *width, int *height ){
989 int alpha_width, alpha_height;
990 char alpha_name[128];
993 ExtractFileExtension( name, ext );
999 if ( !Q_strcasecmp( ext, "lbm" ) ) {
1000 LoadLBM( name, pixels, palette );
1009 else if ( !Q_strcasecmp( ext, "pcx" ) ) {
1010 len = strlen( name );
1011 strcpy( alpha_name, name );
1012 strcpy( &alpha_name[len - 4], "_a.pcx" ); // Alpha map name (may not exist)
1014 if ( FileExists( alpha_name ) ) {
1015 LoadPCX( name, pixels, palette, width, height ); // Load in image
1016 LoadPCX( alpha_name, &alpha_pixels, NULL, &alpha_width, &alpha_height ); // Load in alpha map
1017 if ( ( *width != alpha_width ) || ( *height != alpha_height ) ) {
1018 Error( "Alpha image dimensions not equal to graphic image dimensions" );
1020 MergeAlpha( *pixels, alpha_pixels, *palette, pixels, *width, *height );
1021 *palette = NULL; //Merge Frees pal
1026 LoadPCX( name, pixels, palette, width, height ); // Load in image
1030 else if ( !Q_strcasecmp( ext, "tga" ) ) {
1031 LoadTGA( name, pixels, width, height );
1039 Error( "%s doesn't have a known image extension", name );