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
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)
43 p = (paletteRGB_t *)pal;
44 // Color 0 always transparent
51 ============================================================================
55 ============================================================================
59 typedef unsigned char UBYTE;
60 //conflicts with windows typedef short WORD;
61 typedef unsigned short UWORD;
86 UWORD transparentColor;
87 UBYTE xAspect,yAspect;
88 short pageWidth,pageHeight;
91 extern bmhd_t bmhd; // will be in native byte order
95 #define FORMID ('F'+('O'<<8)+((int)'R'<<16)+((int)'M'<<24))
96 #define ILBMID ('I'+('L'<<8)+((int)'B'<<16)+((int)'M'<<24))
97 #define PBMID ('P'+('B'<<8)+((int)'M'<<16)+((int)' '<<24))
98 #define BMHDID ('B'+('M'<<8)+((int)'H'<<16)+((int)'D'<<24))
99 #define BODYID ('B'+('O'<<8)+((int)'D'<<16)+((int)'Y'<<24))
100 #define CMAPID ('C'+('M'<<8)+((int)'A'<<16)+((int)'P'<<24))
118 Source must be evenly aligned!
121 byte *LBMRLEDecompress (byte *source,byte *unpacked, int bpwidth)
134 rept = (rept^0xff)+2;
136 memset(unpacked,b,rept);
139 else if (rept < 0x80)
142 memcpy(unpacked,source,rept);
147 rept = 0; // rept of 0x80 is NOP
151 } while (count<bpwidth);
154 Error ("Decompression exceeded width!\n");
166 void LoadLBM (char *filename, byte **picture, byte **palette)
168 byte *LBMbuffer, *picbuffer, *cmapbuffer;
170 byte *LBM_P, *LBMEND_P;
174 int formtype,formlength;
175 int chunktype,chunklength;
177 // qiet compiler warnings
184 LoadFile (filename, (void **)&LBMbuffer);
187 // parse the LBM header
190 if ( *(int *)LBMbuffer != LittleLong(FORMID) )
191 Error ("No FORM ID at start of file!\n");
194 formlength = BigLong( *(int *)LBM_P );
196 LBMEND_P = LBM_P + Align(formlength);
198 formtype = LittleLong(*(int *)LBM_P);
200 if (formtype != ILBMID && formtype != PBMID)
201 Error ("Unrecognized form type: %c%c%c%c\n", formtype&0xff
202 ,(formtype>>8)&0xff,(formtype>>16)&0xff,(formtype>>24)&0xff);
210 while (LBM_P < LBMEND_P)
212 chunktype = LBM_P[0] + (LBM_P[1]<<8) + (LBM_P[2]<<16) + (LBM_P[3]<<24);
214 chunklength = LBM_P[3] + (LBM_P[2]<<8) + (LBM_P[1]<<16) + (LBM_P[0]<<24);
220 memcpy (&bmhd,LBM_P,sizeof(bmhd));
221 bmhd.w = BigShort(bmhd.w);
222 bmhd.h = BigShort(bmhd.h);
223 bmhd.x = BigShort(bmhd.x);
224 bmhd.y = BigShort(bmhd.y);
225 bmhd.pageWidth = BigShort(bmhd.pageWidth);
226 bmhd.pageHeight = BigShort(bmhd.pageHeight);
230 cmapbuffer = malloc (768);
231 memset (cmapbuffer, 0, 768);
232 memcpy (cmapbuffer, LBM_P, chunklength);
233 CorrectPalette(cmapbuffer);
239 pic_p = picbuffer = malloc (bmhd.w*bmhd.h);
240 if (formtype == PBMID)
245 for (y=0 ; y<bmhd.h ; y++, pic_p += bmhd.w)
247 if (bmhd.compression == cm_rle1)
248 body_p = LBMRLEDecompress ((byte *)body_p
250 else if (bmhd.compression == cm_none)
252 memcpy (pic_p,body_p,bmhd.w);
253 body_p += Align(bmhd.w);
263 Error ("%s is an interlaced LBM, not packed", filename);
268 LBM_P += Align(chunklength);
273 *picture = picbuffer;
276 *palette = cmapbuffer;
281 ============================================================================
285 ============================================================================
293 void WriteLBMfile (char *filename, byte *data,
294 int width, int height, byte *palette)
297 int *formlength, *bmhdlength, *cmaplength, *bodylength;
301 lbm = lbmptr = malloc (width*height+1000);
311 formlength = (int*)lbmptr;
312 lbmptr+=4; // leave space for length
327 bmhdlength = (int *)lbmptr;
328 lbmptr+=4; // leave space for length
330 memset (&basebmhd,0,sizeof(basebmhd));
331 basebmhd.w = BigShort((short)width);
332 basebmhd.h = BigShort((short)height);
333 basebmhd.nPlanes = BigShort(8);
334 basebmhd.xAspect = BigShort(5);
335 basebmhd.yAspect = BigShort(6);
336 basebmhd.pageWidth = BigShort((short)width);
337 basebmhd.pageHeight = BigShort((short)height);
339 memcpy (lbmptr,&basebmhd,sizeof(basebmhd));
340 lbmptr += sizeof(basebmhd);
342 length = lbmptr-(byte *)bmhdlength-4;
343 *bmhdlength = BigLong(length);
345 *lbmptr++ = 0; // pad chunk to even offset
355 cmaplength = (int *)lbmptr;
356 lbmptr+=4; // leave space for length
358 memcpy (lbmptr,palette,768);
361 length = lbmptr-(byte *)cmaplength-4;
362 *cmaplength = BigLong(length);
364 *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
388 length = lbmptr-(byte *)formlength-4;
389 *formlength = BigLong(length);
391 *lbmptr++ = 0; // pad chunk to even offset
396 SaveFile (filename, lbm, lbmptr-lbm);
402 ============================================================================
406 ============================================================================
415 unsigned short xmin,ymin,xmax,ymax;
416 unsigned short hres,vres;
417 unsigned char palette[48];
420 unsigned short bytes_per_line;
421 unsigned short palette_type;
423 unsigned char data; // unbounded
431 void LoadPCX (char *filename, byte **pic, byte **palette, int *width, int *height)
437 int dataByte, runLength;
443 len = LoadFile (filename, (void **)&raw);
446 // parse the PCX file
451 pcx->xmin = LittleShort(pcx->xmin);
452 pcx->ymin = LittleShort(pcx->ymin);
453 pcx->xmax = LittleShort(pcx->xmax);
454 pcx->ymax = LittleShort(pcx->ymax);
455 pcx->hres = LittleShort(pcx->hres);
456 pcx->vres = LittleShort(pcx->vres);
457 pcx->bytes_per_line = LittleShort(pcx->bytes_per_line);
458 pcx->palette_type = LittleShort(pcx->palette_type);
460 if (pcx->manufacturer != 0x0a
462 || pcx->encoding != 1
463 || pcx->bits_per_pixel != 8
466 Error ("Bad pcx file %s", filename);
470 *palette = malloc(768);
471 memcpy (*palette, (byte *)pcx + len - 768, 768);
472 CorrectPalette(*palette);
476 *width = pcx->xmax+1;
478 *height = pcx->ymax+1;
483 out = malloc ( (pcx->ymax+1) * (pcx->xmax+1) );
485 Error ("Skin_Cache: couldn't allocate");
491 for (y=0 ; y<=pcx->ymax ; y++, pix += pcx->xmax+1)
493 for (x=0 ; x<=pcx->xmax ; )
497 if((dataByte & 0xC0) == 0xC0)
499 runLength = dataByte & 0x3F;
505 while(runLength-- > 0)
511 if ( raw - (byte *)pcx > len)
512 Error ("PCX file %s was malformed", filename);
523 void StuffPackedByte(int curRepCount, byte curByte, byte** packPtr)
529 while(curRepCount > 0)
531 if (curRepCount == 1)
533 if ((curByte & 0xc0) != 0xc0)
544 if (curRepCount < 0x0040)
546 *pack++ = (0x00c0 | curRepCount);
552 curRepCount -= 0x003f;
559 void WritePCXfile (char *filename, byte *data,
560 int width, int height, byte *palette)
568 pcx = malloc (width*height*2+1000);
569 memset (pcx, 0, sizeof(*pcx));
571 pcx->manufacturer = 0x0a; // PCX id
572 pcx->version = 5; // 256 color
573 pcx->encoding = 1; // RLE
574 pcx->bits_per_pixel = 8; // 256 color
577 pcx->xmax = LittleShort((short)(width-1));
578 pcx->ymax = LittleShort((short)(height-1));
579 pcx->hres = LittleShort((short)width);
580 pcx->vres = LittleShort((short)height);
581 pcx->color_planes = 1; // chunky image
582 pcx->bytes_per_line = LittleShort((short)width);
583 pcx->palette_type = LittleShort(1); // not a grey scale
588 /* for (i=0 ; i<height ; i++)
590 for (j=0 ; j<width ; j++)
592 if ( (*data & 0xc0) != 0xc0)
602 for (i=0 ; i<height ; i++)
606 for (j=0 ; j<width ; j++)
608 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)
654 ExtractFileExtension (name, ext);
655 if (!Q_strcasecmp (ext, "lbm"))
657 LoadLBM (name, pixels, palette);
663 else if (!Q_strcasecmp (ext, "pcx"))
665 LoadPCX (name, pixels, palette, width, height);
668 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)
684 ExtractFileExtension (name, ext);
685 if (!Q_strcasecmp (ext, "lbm"))
687 WriteLBMfile (name, pixels, width, height, palette);
689 else if (!Q_strcasecmp (ext, "pcx"))
691 WritePCXfile (name, pixels, width, height, palette);
694 Error ("%s doesn't have a known image extension", name);
701 ============================================================================
705 ============================================================================
708 typedef struct _TargaHeader
710 unsigned char id_length, colormap_type, image_type;
711 unsigned short colormap_index, colormap_length;
712 unsigned char colormap_size;
713 unsigned short x_origin, y_origin, width, height;
714 unsigned char pixel_size, attributes;
717 int fgetLittleShort (FILE *f)
724 return((short)(b1 + (b2 << 8)));
727 int fgetLittleLong (FILE *f)
736 return(b1 + (b2 << 8) + (b3 << 16) + (b4 << 24));
745 void LoadTGA(char *name, byte **pixels, int *width, int *height)
747 int columns, rows, numPixels;
753 TargaHeader targa_header;
754 unsigned char red, green, blue, alphabyte;
755 unsigned char packetHeader, packetSize, j;
761 fin = fopen(name, "rb");
763 Error ("Couldn't read %s", name);
765 targa_header.id_length = fgetc(fin);
766 targa_header.colormap_type = fgetc(fin);
767 targa_header.image_type = fgetc(fin);
769 targa_header.colormap_index = fgetLittleShort(fin);
770 targa_header.colormap_length = fgetLittleShort(fin);
771 targa_header.colormap_size = fgetc(fin);
772 targa_header.x_origin = fgetLittleShort(fin);
773 targa_header.y_origin = fgetLittleShort(fin);
774 targa_header.width = fgetLittleShort(fin);
775 targa_header.height = fgetLittleShort(fin);
776 targa_header.pixel_size = fgetc(fin);
777 targa_header.attributes = fgetc(fin);
778 flip = (targa_header.attributes & 0x020) == 0;
779 mirror = (targa_header.attributes & 0x010) != 0;
781 if ((targa_header.image_type != 2) && (targa_header.image_type != 10))
782 Error ("LoadTGA: Only type 2 and 10 targa RGB images supported\n");
784 if (targa_header.colormap_type || ((targa_header.pixel_size != 32) && (targa_header.pixel_size != 24)))
785 Error ("Texture_LoadTGA: Only 32 or 24 bit images supported (no colormaps)\n");
787 columns = targa_header.width;
788 rows = targa_header.height;
789 numPixels = columns * rows;
799 targa_rgba = malloc(numPixels * 4);
800 *pixels = targa_rgba;
804 pixbuf = targa_rgba + ((rows - 1) * columns * 4);
805 rowOffset = -columns * 4;
810 rowOffset = columns * 4;
815 pixbuf += ((columns - 1) * 4);
822 if (targa_header.id_length)
823 fseek(fin, targa_header.id_length, SEEK_CUR); // skip TARGA image comment
825 if (targa_header.image_type == 2)
826 { // Uncompressed, RGB images
827 for(row = 0; row < rows; row++)
830 for(column = 0; column < columns; column++)
832 switch (targa_header.pixel_size)
842 rowBuf += pixDirection;
848 alphabyte = getc(fin);
852 rowBuf[3] = alphabyte;
853 rowBuf += pixDirection;
860 else if(targa_header.image_type == 10)
861 { // Runlength encoded RGB images
862 for(row = 0; row < rows; row++)
865 for(column = 0; column < columns; )
867 packetHeader = getc(fin);
868 packetSize = 1 + (packetHeader & 0x7f);
869 if (packetHeader & 0x80)
870 { // run-length packet
871 switch (targa_header.pixel_size)
883 alphabyte = getc(fin);
887 for(j = 0; j < packetSize; j++)
892 rowBuf[3] = alphabyte;
893 rowBuf += pixDirection;
895 if(column == columns)
896 { // run spans across rows
907 { // non run-length packet
908 for(j = 0; j < packetSize; j++)
910 switch (targa_header.pixel_size)
920 rowBuf += pixDirection;
926 alphabyte = getc(fin);
930 rowBuf[3] = alphabyte;
931 rowBuf += pixDirection;
935 if (column == columns)
936 { // pixel packet run spans across rows
954 void MergeAlpha(byte *pix, byte *alpha, byte *pal, byte **out, int width, int height)
957 byte *data, *src, *srca;
959 size = width * height;
960 data = malloc(size * 4);
962 Error("Could not allocate memory for true color image");
968 for(i = 0; i < size; i++, src++, srca++)
970 *data++ = pal[*src * 3 + 0]; // r
971 *data++ = pal[*src * 3 + 1]; // g
972 *data++ = pal[*src * 3 + 2]; // b
973 *data++ = *srca; // a
985 false: paletted texture
986 true: true color RGBA image (no palette)
989 qboolean LoadAnyImage (char *name, byte **pixels, byte **palette, int *width, int *height)
993 int alpha_width, alpha_height;
994 char alpha_name[128];
997 ExtractFileExtension (name, ext);
1004 if (!Q_strcasecmp (ext, "lbm"))
1006 LoadLBM (name, pixels, palette);
1013 else if (!Q_strcasecmp (ext, "pcx"))
1016 strcpy(alpha_name, name);
1017 strcpy(&alpha_name[len - 4], "_a.pcx"); // Alpha map name (may not exist)
1019 if(FileExists(alpha_name))
1021 LoadPCX (name, pixels, palette, width, height); // Load in image
1022 LoadPCX (alpha_name, &alpha_pixels, NULL, &alpha_width, &alpha_height); // Load in alpha map
1023 if((*width != alpha_width) || (*height != alpha_height))
1025 Error("Alpha image dimensions not equal to graphic image dimensions");
1027 MergeAlpha(*pixels, alpha_pixels, *palette, pixels, *width, *height);
1028 *palette = NULL;//Merge Frees pal
1033 LoadPCX (name, pixels, palette, width, height); // Load in image
1037 else if (!Q_strcasecmp (ext, "tga"))
1039 LoadTGA(name, pixels, width, height);
1048 Error ("%s doesn't have a known image extension", name);