2 Copyright (C) 1999-2006 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
29 int fgetLittleShort (FILE *f)
36 return (short)(b1 + b2*256);
39 int fgetLittleLong (FILE *f)
48 return b1 + (b2<<8) + (b3<<16) + (b4<<24);
51 int bufLittleShort (byte *buf, int len, int *pos)
56 Error ("Unexpected buffer end");
58 b1 = buf[*pos]; *pos += 1;
59 b2 = buf[*pos]; *pos += 1;
61 return (short)(b1 + b2*256);
64 int bufLittleLong (byte *buf, int len, int *pos)
69 Error ("Unexpected buffer end");
71 b1 = buf[*pos]; *pos += 1;
72 b2 = buf[*pos]; *pos += 1;
73 b3 = buf[*pos]; *pos += 1;
74 b4 = buf[*pos]; *pos += 1;
76 return b1 + (b2<<8) + (b3<<16) + (b4<<24);
81 ============================================================================
85 ============================================================================
89 typedef unsigned char UBYTE;
90 //conflicts with windows typedef short WORD;
91 typedef unsigned short UWORD;
116 UWORD transparentColor;
117 UBYTE xAspect,yAspect;
118 short pageWidth,pageHeight;
121 extern bmhd_t bmhd; // will be in native byte order
125 #define FORMID ('F'+('O'<<8)+((int)'R'<<16)+((int)'M'<<24))
126 #define ILBMID ('I'+('L'<<8)+((int)'B'<<16)+((int)'M'<<24))
127 #define PBMID ('P'+('B'<<8)+((int)'M'<<16)+((int)' '<<24))
128 #define BMHDID ('B'+('M'<<8)+((int)'H'<<16)+((int)'D'<<24))
129 #define BODYID ('B'+('O'<<8)+((int)'D'<<16)+((int)'Y'<<24))
130 #define CMAPID ('C'+('M'<<8)+((int)'A'<<16)+((int)'P'<<24))
148 Source must be evenly aligned!
151 byte *LBMRLEDecompress (byte *source,byte *unpacked, int bpwidth)
164 rept = (rept^0xff)+2;
166 memset(unpacked,b,rept);
169 else if (rept < 0x80)
172 memcpy(unpacked,source,rept);
177 rept = 0; // rept of 0x80 is NOP
181 } while (count<bpwidth);
184 Error ("Decompression exceeded width!\n");
196 void LoadLBM (const char *filename, byte **picture, byte **palette)
198 byte *LBMbuffer, *picbuffer, *cmapbuffer;
200 byte *LBM_P, *LBMEND_P;
204 int formtype,formlength;
205 int chunktype,chunklength;
207 // qiet compiler warnings
214 LoadFile (filename, (void **)&LBMbuffer);
217 // parse the LBM header
220 if ( *(int *)LBMbuffer != LittleLong(FORMID) )
221 Error ("No FORM ID at start of file!\n");
224 formlength = BigLong( *(int *)LBM_P );
226 LBMEND_P = LBM_P + Align(formlength);
228 formtype = LittleLong(*(int *)LBM_P);
230 if (formtype != ILBMID && formtype != PBMID)
231 Error ("Unrecognized form type: %c%c%c%c\n", formtype&0xff
232 ,(formtype>>8)&0xff,(formtype>>16)&0xff,(formtype>>24)&0xff);
240 while (LBM_P < LBMEND_P)
242 chunktype = LBM_P[0] + (LBM_P[1]<<8) + (LBM_P[2]<<16) + (LBM_P[3]<<24);
244 chunklength = LBM_P[3] + (LBM_P[2]<<8) + (LBM_P[1]<<16) + (LBM_P[0]<<24);
250 memcpy (&bmhd,LBM_P,sizeof(bmhd));
251 bmhd.w = BigShort(bmhd.w);
252 bmhd.h = BigShort(bmhd.h);
253 bmhd.x = BigShort(bmhd.x);
254 bmhd.y = BigShort(bmhd.y);
255 bmhd.pageWidth = BigShort(bmhd.pageWidth);
256 bmhd.pageHeight = BigShort(bmhd.pageHeight);
260 cmapbuffer = safe_malloc (768);
261 memset (cmapbuffer, 0, 768);
262 memcpy (cmapbuffer, LBM_P, chunklength);
268 pic_p = picbuffer = safe_malloc (bmhd.w*bmhd.h);
269 if (formtype == PBMID)
274 for (y=0 ; y<bmhd.h ; y++, pic_p += bmhd.w)
276 if (bmhd.compression == cm_rle1)
277 body_p = LBMRLEDecompress ((byte *)body_p
279 else if (bmhd.compression == cm_none)
281 memcpy (pic_p,body_p,bmhd.w);
282 body_p += Align(bmhd.w);
292 Error ("%s is an interlaced LBM, not packed", filename);
297 LBM_P += Align(chunklength);
302 *picture = picbuffer;
305 *palette = cmapbuffer;
310 ============================================================================
314 ============================================================================
322 void WriteLBMfile (const char *filename, byte *data,
323 int width, int height, byte *palette)
326 int *formlength, *bmhdlength, *cmaplength, *bodylength;
330 lbm = lbmptr = safe_malloc (width*height+1000);
340 formlength = (int*)lbmptr;
341 lbmptr+=4; // leave space for length
356 bmhdlength = (int *)lbmptr;
357 lbmptr+=4; // leave space for length
359 memset (&basebmhd,0,sizeof(basebmhd));
360 basebmhd.w = BigShort((short)width);
361 basebmhd.h = BigShort((short)height);
362 basebmhd.nPlanes = BigShort(8);
363 basebmhd.xAspect = BigShort(5);
364 basebmhd.yAspect = BigShort(6);
365 basebmhd.pageWidth = BigShort((short)width);
366 basebmhd.pageHeight = BigShort((short)height);
368 memcpy (lbmptr,&basebmhd,sizeof(basebmhd));
369 lbmptr += sizeof(basebmhd);
371 length = lbmptr-(byte *)bmhdlength-4;
372 *bmhdlength = BigLong(length);
374 *lbmptr++ = 0; // pad chunk to even offset
384 cmaplength = (int *)lbmptr;
385 lbmptr+=4; // leave space for length
387 memcpy (lbmptr,palette,768);
390 length = lbmptr-(byte *)cmaplength-4;
391 *cmaplength = BigLong(length);
393 *lbmptr++ = 0; // pad chunk to even offset
403 bodylength = (int *)lbmptr;
404 lbmptr+=4; // leave space for length
406 memcpy (lbmptr,data,width*height);
407 lbmptr += width*height;
409 length = lbmptr-(byte *)bodylength-4;
410 *bodylength = BigLong(length);
412 *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
425 SaveFile (filename, lbm, lbmptr-lbm);
431 ============================================================================
435 ============================================================================
444 unsigned short xmin,ymin,xmax,ymax;
445 unsigned short hres,vres;
446 unsigned char palette[48];
449 unsigned short bytes_per_line;
450 unsigned short palette_type;
452 unsigned char data; // unbounded
463 #define DECODEPCX( b, d, r ) d=*b++;if((d&0xC0)==0xC0){r=d&0x3F;d=*b++;}else{r=1;}
465 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 );
481 /* parse the PCX file */
485 pcx->xmin = LittleShort(pcx->xmin);
486 pcx->ymin = LittleShort(pcx->ymin);
487 pcx->xmax = LittleShort(pcx->xmax);
488 pcx->ymax = LittleShort(pcx->ymax);
489 pcx->hres = LittleShort(pcx->hres);
490 pcx->vres = LittleShort(pcx->vres);
491 pcx->bytes_per_line = LittleShort(pcx->bytes_per_line);
492 pcx->palette_type = LittleShort(pcx->palette_type);
494 if (pcx->manufacturer != 0x0a
496 || pcx->encoding != 1
497 || pcx->bits_per_pixel != 8
500 Error ("Bad pcx file %s", filename);
504 *palette = safe_malloc(768);
505 memcpy (*palette, (byte *)pcx + len - 768, 768);
509 *width = pcx->xmax+1;
511 *height = pcx->ymax+1;
516 out = safe_malloc ( (pcx->ymax+1) * (pcx->xmax+1) );
518 Error( "LoadPCX: couldn't allocate");
523 /* RR2DO2: pcx fix */
524 lsize = pcx->color_planes * pcx->bytes_per_line;
526 /* go scanline by scanline */
527 for( y = 0; y <= pcx->ymax; y++, pix += pcx->xmax + 1 )
531 for( x=0; x <= pcx->xmax; )
534 DECODEPCX( raw, dataByte, runLength );
535 while( runLength-- > 0 )
536 pix[ x++ ] = dataByte;
539 /* RR2DO2: discard any other data */
542 DECODEPCX( raw, dataByte, runLength );
545 while( runLength-- > 0 )
550 if( raw - (byte *) pcx > len)
551 Error( "PCX file %s was malformed", filename );
562 void WritePCXfile (const char *filename, byte *data,
563 int width, int height, byte *palette)
569 pcx = safe_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; // uncompressed
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)
604 *pack++ = 0x0c; // palette ID byte
605 for (i=0 ; i<768 ; i++)
606 *pack++ = *palette++;
609 length = pack - (byte *)pcx;
610 SaveFile (filename, pcx, length);
616 ============================================================================
620 ============================================================================
626 // we can't just use these structures, because
627 // compiler structure alignment will not be portable
628 // on this unaligned stuff
630 typedef struct tagBITMAPFILEHEADER { // bmfh
638 typedef struct tagBITMAPINFOHEADER{ // bmih
646 LONG biXPelsPerMeter;
647 LONG biYPelsPerMeter;
649 DWORD biClrImportant;
652 typedef struct tagBITMAPINFO { // bmi
653 BITMAPINFOHEADER bmiHeader;
654 RGBQUAD bmiColors[1];
657 typedef struct tagBITMAPCOREHEADER { // bmch
665 typedef struct _BITMAPCOREINFO { // bmci
666 BITMAPCOREHEADER bmciHeader;
667 RGBTRIPLE bmciColors[1];
677 void LoadBMP (const char *filename, byte **pic, byte **palette, int *width, int *height)
688 byte bcPalette[1024];
693 len = vfsLoadFile (filename, (void **)&in, 0);
696 Error ("Couldn't read %s", filename);
699 i = bufLittleShort (in, len, &pos);
700 if (i != 'B' + ('M'<<8) ) {
701 Error ("%s is not a bmp file", filename);
704 bfSize = bufLittleLong (in, len, &pos);
705 bufLittleShort(in, len, &pos);
706 bufLittleShort(in, len, &pos);
707 bfOffBits = bufLittleLong (in, len, &pos);
709 // the size will tell us if it is a
710 // bitmapinfo or a bitmapcore
711 structSize = bufLittleLong (in, len, &pos);
712 if (structSize == 40)
715 bcWidth = bufLittleLong(in, len, &pos);
716 bcHeight= bufLittleLong(in, len, &pos);
717 bcPlanes = bufLittleShort(in, len, &pos);
718 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)
739 bcWidth = bufLittleShort(in, len, &pos);
740 bcHeight= bufLittleShort(in, len, &pos);
741 bcPlanes = bufLittleShort(in, len, &pos);
742 bcBitCount = bufLittleShort(in, len, &pos);
746 memcpy (bcPalette, in+pos, 768);
748 *palette = safe_malloc(768);
750 for (i = 0 ; i < 256 ; i++) {
751 (*palette)[i * 3 + 0] = bcPalette[i * 3 + 2];
752 (*palette)[i * 3 + 1] = bcPalette[i * 3 + 1];
753 (*palette)[i * 3 + 2] = bcPalette[i * 3 + 0];
757 Error ("%s had strange struct size", filename);
761 Error ("%s was not a single plane image", filename);
764 if (bcBitCount != 8) {
765 Error ("%s was not an 8 bit image", filename);
769 bcHeight = -bcHeight;
785 out = safe_malloc ( bcWidth * bcHeight );
790 for (i = 0 ; i < bcHeight ; i++) {
791 memcpy (out + bcWidth * (bcHeight - 1 - i), in+pos, bcWidth);
795 memcpy (out, in+pos, bcWidth*bcHeight);
796 pos += bcWidth*bcHeight;
804 ============================================================================
808 ============================================================================
815 Will load either an lbm or pcx, depending on extension.
816 Any of the return pointers can be NULL if you don't want them.
819 void Load256Image (const char *name, byte **pixels, byte **palette, int *width, int *height)
823 ExtractFileExtension (name, ext);
824 if (!Q_stricmp (ext, "lbm"))
826 LoadLBM (name, pixels, palette);
832 else if (!Q_stricmp (ext, "pcx"))
834 LoadPCX (name, pixels, palette, width, height);
836 else if (!Q_stricmp (ext, "bmp"))
838 LoadBMP (name, pixels, palette, width, height);
841 Error ("%s doesn't have a known image extension", name);
849 Will save either an lbm or pcx, depending on extension.
852 void Save256Image (const char *name, byte *pixels, byte *palette,
853 int width, int height)
857 ExtractFileExtension (name, ext);
858 if (!Q_stricmp (ext, "lbm"))
860 WriteLBMfile (name, pixels, width, height, palette);
862 else if (!Q_stricmp (ext, "pcx"))
864 WritePCXfile (name, pixels, width, height, palette);
867 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)
891 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);
899 void LoadTGABuffer (const byte *f, const byte *enddata, byte **pic, int *width, int *height)
901 int x, y, row_inc, compressed, readpixelcount, red, green, blue, alpha, runlen, pindex, alphabits, image_width, image_height;
902 byte *pixbuf, *image_rgba;
905 TargaHeader targa_header;
906 unsigned char palette[256*4];
910 // abort if it is too small to parse
911 if (enddata - f < 19)
914 targa_header.id_length = f[0];
915 targa_header.colormap_type = f[1];
916 targa_header.image_type = f[2];
918 targa_header.colormap_index = f[3] + f[4] * 256;
919 targa_header.colormap_length = f[5] + f[6] * 256;
920 targa_header.colormap_size = f[7];
921 targa_header.x_origin = f[8] + f[9] * 256;
922 targa_header.y_origin = f[10] + f[11] * 256;
923 targa_header.width = image_width = f[12] + f[13] * 256;
924 targa_header.height = image_height = f[14] + f[15] * 256;
926 targa_header.pixel_size = f[16];
927 targa_header.attributes = f[17];
929 // advance to end of header
932 // skip TARGA image comment (usually 0 bytes)
933 fin += targa_header.id_length;
935 // read/skip the colormap if present (note: according to the TARGA spec it
936 // can be present even on truecolor or greyscale images, just not used by
938 if (targa_header.colormap_type)
940 if (targa_header.colormap_length > 256)
942 TargaError(&targa_header, "LoadTGA: only up to 256 colormap_length supported\n");
945 if (targa_header.colormap_index)
947 TargaError(&targa_header, "LoadTGA: colormap_index not supported\n");
950 if (targa_header.colormap_size == 24)
952 for (x = 0;x < targa_header.colormap_length;x++)
954 palette[x*4+2] = *fin++;
955 palette[x*4+1] = *fin++;
956 palette[x*4+0] = *fin++;
957 palette[x*4+3] = 255;
960 else if (targa_header.colormap_size == 32)
962 for (x = 0;x < targa_header.colormap_length;x++)
964 palette[x*4+2] = *fin++;
965 palette[x*4+1] = *fin++;
966 palette[x*4+0] = *fin++;
967 palette[x*4+3] = *fin++;
972 TargaError(&targa_header, "LoadTGA: Only 32 and 24 bit colormap_size supported\n");
977 // check our pixel_size restrictions according to image_type
978 if (targa_header.image_type == 2 || targa_header.image_type == 10)
980 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
982 TargaError(&targa_header, "LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
986 else if (targa_header.image_type == 1 || targa_header.image_type == 9)
988 if (targa_header.pixel_size != 8)
990 TargaError(&targa_header, "LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
994 else if (targa_header.image_type == 3 || targa_header.image_type == 11)
996 if (targa_header.pixel_size != 8)
998 TargaError(&targa_header, "LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
1004 TargaError(&targa_header, "LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported");
1008 if (targa_header.attributes & 0x10)
1010 TargaError(&targa_header, "LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
1014 // number of attribute bits per pixel, we only support 0 or 8
1015 alphabits = targa_header.attributes & 0x0F;
1016 if (alphabits != 8 && alphabits != 0)
1018 TargaError(&targa_header, "LoadTGA: only 0 or 8 attribute (alpha) bits supported\n");
1022 image_rgba = safe_malloc(image_width * image_height * 4);
1025 Sys_Printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
1029 // If bit 5 of attributes isn't set, the image has been stored from bottom to top
1030 if ((targa_header.attributes & 0x20) == 0)
1032 pixbuf = image_rgba + (image_height - 1)*image_width*4;
1033 row_inc = -image_width*4*2;
1037 pixbuf = image_rgba;
1041 compressed = targa_header.image_type == 9 || targa_header.image_type == 10 || targa_header.image_type == 11;
1044 red = green = blue = alpha = 255;
1045 while (y < image_height)
1047 // decoder is mostly the same whether it's compressed or not
1048 readpixelcount = 1000000;
1050 if (compressed && fin < enddata)
1053 // high bit indicates this is an RLE compressed run
1056 runlen = 1 + (runlen & 0x7f);
1059 while((runlen--) && y < image_height)
1061 if (readpixelcount > 0)
1064 red = green = blue = alpha = 255;
1067 switch(targa_header.image_type)
1073 if (pindex >= targa_header.colormap_length)
1074 pindex = 0; // error
1075 p = palette + pindex * 4;
1089 if (targa_header.pixel_size == 32 && fin < enddata)
1095 red = green = blue = *fin++;
1107 if (x == image_width)
1109 // end of line, advance to next
1119 *width = image_width;
1121 *height = image_height;
1130 void LoadTGA (const char *name, byte **pixels, int *width, int *height)
1137 nLen = vfsLoadFile ( name, (void **)&buffer, 0);
1140 Error ("Couldn't read %s", name);
1143 LoadTGABuffer(buffer, buffer + nLen, pixels, width, height);
1153 void WriteTGA (const char *filename, byte *data, int width, int height) {
1159 buffer = safe_malloc(width*height*4 + 18);
1160 memset (buffer, 0, 18);
1161 buffer[2] = 2; // uncompressed type
1162 buffer[12] = width&255;
1163 buffer[13] = width>>8;
1164 buffer[14] = height&255;
1165 buffer[15] = height>>8;
1166 buffer[16] = 32; // pixel size
1169 c = 18 + width * height * 4;
1170 for (i=18 ; i<c ; i+=4)
1172 buffer[i] = data[i-18+2]; // blue
1173 buffer[i+1] = data[i-18+1]; // green
1174 buffer[i+2] = data[i-18+0]; // red
1175 buffer[i+3] = data[i-18+3]; // alpha
1178 f = fopen (filename, "wb");
1179 fwrite (buffer, 1, c, f);
1185 void WriteTGAGray (const char *filename, byte *data, int width, int height) {
1189 memset (buffer, 0, 18);
1190 buffer[2] = 3; // uncompressed type
1191 buffer[12] = width&255;
1192 buffer[13] = width>>8;
1193 buffer[14] = height&255;
1194 buffer[15] = height>>8;
1195 buffer[16] = 8; // pixel size
1197 f = fopen (filename, "wb");
1198 fwrite (buffer, 1, 18, f);
1199 fwrite (data, 1, width * height, f);
1204 ============================================================================
1208 ============================================================================
1215 Any of the return pointers can be NULL if you don't want them.
1218 void Load32BitImage (const char *name, unsigned **pixels, int *width, int *height)
1228 ExtractFileExtension (name, ext);
1229 if (!Q_stricmp (ext, "tga")) {
1230 LoadTGA (name, (byte **)pixels, width, height);
1232 Load256Image (name, &pixels8, &palette, width, height);
1236 size = *width * *height;
1237 pixels32 = safe_malloc(size * 4);
1238 *pixels = (unsigned *)pixels32;
1239 for (i = 0 ; i < size ; i++) {
1241 pixels32[i*4 + 0] = palette[ v * 3 + 0 ];
1242 pixels32[i*4 + 1] = palette[ v * 3 + 1 ];
1243 pixels32[i*4 + 2] = palette[ v * 3 + 2 ];
1244 pixels32[i*4 + 3] = 0xff;