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 ============================================================================
34 ============================================================================
38 typedef unsigned char UBYTE;
39 //conflicts with windows typedef short WORD;
40 typedef unsigned short UWORD;
65 UWORD transparentColor;
66 UBYTE xAspect,yAspect;
67 short pageWidth,pageHeight;
70 extern bmhd_t bmhd; // will be in native byte order
74 #define FORMID ('F'+('O'<<8)+((int)'R'<<16)+((int)'M'<<24))
75 #define ILBMID ('I'+('L'<<8)+((int)'B'<<16)+((int)'M'<<24))
76 #define PBMID ('P'+('B'<<8)+((int)'M'<<16)+((int)' '<<24))
77 #define BMHDID ('B'+('M'<<8)+((int)'H'<<16)+((int)'D'<<24))
78 #define BODYID ('B'+('O'<<8)+((int)'D'<<16)+((int)'Y'<<24))
79 #define CMAPID ('C'+('M'<<8)+((int)'A'<<16)+((int)'P'<<24))
97 Source must be evenly aligned!
100 byte *LBMRLEDecompress (byte *source,byte *unpacked, int bpwidth)
113 rept = (rept^0xff)+2;
115 memset(unpacked,b,rept);
118 else if (rept < 0x80)
121 memcpy(unpacked,source,rept);
126 rept = 0; // rept of 0x80 is NOP
130 } while (count<bpwidth);
133 Error ("Decompression exceeded width!\n");
145 void LoadLBM (char *filename, byte **picture, byte **palette)
147 byte *LBMbuffer, *picbuffer, *cmapbuffer;
149 byte *LBM_P, *LBMEND_P;
153 int formtype,formlength;
154 int chunktype,chunklength;
156 // qiet compiler warnings
163 LoadFile (filename, (void **)&LBMbuffer);
166 // parse the LBM header
169 if ( *(int *)LBMbuffer != LittleLong(FORMID) )
170 Error ("No FORM ID at start of file!\n");
173 formlength = BigLong( *(int *)LBM_P );
175 LBMEND_P = LBM_P + Align(formlength);
177 formtype = LittleLong(*(int *)LBM_P);
179 if (formtype != ILBMID && formtype != PBMID)
180 Error ("Unrecognized form type: %c%c%c%c\n", formtype&0xff
181 ,(formtype>>8)&0xff,(formtype>>16)&0xff,(formtype>>24)&0xff);
189 while (LBM_P < LBMEND_P)
191 chunktype = LBM_P[0] + (LBM_P[1]<<8) + (LBM_P[2]<<16) + (LBM_P[3]<<24);
193 chunklength = LBM_P[3] + (LBM_P[2]<<8) + (LBM_P[1]<<16) + (LBM_P[0]<<24);
199 memcpy (&bmhd,LBM_P,sizeof(bmhd));
200 bmhd.w = BigShort(bmhd.w);
201 bmhd.h = BigShort(bmhd.h);
202 bmhd.x = BigShort(bmhd.x);
203 bmhd.y = BigShort(bmhd.y);
204 bmhd.pageWidth = BigShort(bmhd.pageWidth);
205 bmhd.pageHeight = BigShort(bmhd.pageHeight);
209 cmapbuffer = malloc (768);
210 memset (cmapbuffer, 0, 768);
211 memcpy (cmapbuffer, LBM_P, chunklength);
217 pic_p = picbuffer = malloc (bmhd.w*bmhd.h);
218 if (formtype == PBMID)
223 for (y=0 ; y<bmhd.h ; y++, pic_p += bmhd.w)
225 if (bmhd.compression == cm_rle1)
226 body_p = LBMRLEDecompress ((byte *)body_p
228 else if (bmhd.compression == cm_none)
230 memcpy (pic_p,body_p,bmhd.w);
231 body_p += Align(bmhd.w);
241 Error ("%s is an interlaced LBM, not packed", filename);
246 LBM_P += Align(chunklength);
251 *picture = picbuffer;
254 *palette = cmapbuffer;
259 ============================================================================
263 ============================================================================
271 void WriteLBMfile (char *filename, byte *data,
272 int width, int height, byte *palette)
275 int *formlength, *bmhdlength, *cmaplength, *bodylength;
279 lbm = lbmptr = malloc (width*height+1000);
289 formlength = (int*)lbmptr;
290 lbmptr+=4; // leave space for length
305 bmhdlength = (int *)lbmptr;
306 lbmptr+=4; // leave space for length
308 memset (&basebmhd,0,sizeof(basebmhd));
309 basebmhd.w = BigShort((short)width);
310 basebmhd.h = BigShort((short)height);
311 basebmhd.nPlanes = BigShort(8);
312 basebmhd.xAspect = BigShort(5);
313 basebmhd.yAspect = BigShort(6);
314 basebmhd.pageWidth = BigShort((short)width);
315 basebmhd.pageHeight = BigShort((short)height);
317 memcpy (lbmptr,&basebmhd,sizeof(basebmhd));
318 lbmptr += sizeof(basebmhd);
320 length = lbmptr-(byte *)bmhdlength-4;
321 *bmhdlength = BigLong(length);
323 *lbmptr++ = 0; // pad chunk to even offset
333 cmaplength = (int *)lbmptr;
334 lbmptr+=4; // leave space for length
336 memcpy (lbmptr,palette,768);
339 length = lbmptr-(byte *)cmaplength-4;
340 *cmaplength = BigLong(length);
342 *lbmptr++ = 0; // pad chunk to even offset
352 bodylength = (int *)lbmptr;
353 lbmptr+=4; // leave space for length
355 memcpy (lbmptr,data,width*height);
356 lbmptr += width*height;
358 length = lbmptr-(byte *)bodylength-4;
359 *bodylength = BigLong(length);
361 *lbmptr++ = 0; // pad chunk to even offset
366 length = lbmptr-(byte *)formlength-4;
367 *formlength = BigLong(length);
369 *lbmptr++ = 0; // pad chunk to even offset
374 SaveFile (filename, lbm, lbmptr-lbm);
380 ============================================================================
384 ============================================================================
393 unsigned short xmin,ymin,xmax,ymax;
394 unsigned short hres,vres;
395 unsigned char palette[48];
398 unsigned short bytes_per_line;
399 unsigned short palette_type;
401 unsigned char data; // unbounded
410 void LoadPCX (char *filename, byte **pic, byte **palette, int *width, int *height)
416 int dataByte, runLength;
422 len = LoadFile (filename, (void **)&raw);
425 // parse the PCX file
430 pcx->xmin = LittleShort(pcx->xmin);
431 pcx->ymin = LittleShort(pcx->ymin);
432 pcx->xmax = LittleShort(pcx->xmax);
433 pcx->ymax = LittleShort(pcx->ymax);
434 pcx->hres = LittleShort(pcx->hres);
435 pcx->vres = LittleShort(pcx->vres);
436 pcx->bytes_per_line = LittleShort(pcx->bytes_per_line);
437 pcx->palette_type = LittleShort(pcx->palette_type);
439 if (pcx->manufacturer != 0x0a
441 || pcx->encoding != 1
442 || pcx->bits_per_pixel != 8
445 Error ("Bad pcx file %s", filename);
449 *palette = malloc(768);
450 memcpy (*palette, (byte *)pcx + len - 768, 768);
454 *width = pcx->xmax+1;
456 *height = pcx->ymax+1;
461 out = malloc ( (pcx->ymax+1) * (pcx->xmax+1) );
463 Error ("Skin_Cache: couldn't allocate");
469 for (y=0 ; y<=pcx->ymax ; y++, pix += pcx->xmax+1)
471 for (x=0 ; x<=pcx->xmax ; )
475 if((dataByte & 0xC0) == 0xC0)
477 runLength = dataByte & 0x3F;
483 while(runLength-- > 0)
489 if ( raw - (byte *)pcx > len)
490 Error ("PCX file %s was malformed", filename);
500 void WritePCXfile (char *filename, byte *data,
501 int width, int height, byte *palette)
507 pcx = malloc (width*height*2+1000);
508 memset (pcx, 0, sizeof(*pcx));
510 pcx->manufacturer = 0x0a; // PCX id
511 pcx->version = 5; // 256 color
512 pcx->encoding = 1; // uncompressed
513 pcx->bits_per_pixel = 8; // 256 color
516 pcx->xmax = LittleShort((short)(width-1));
517 pcx->ymax = LittleShort((short)(height-1));
518 pcx->hres = LittleShort((short)width);
519 pcx->vres = LittleShort((short)height);
520 pcx->color_planes = 1; // chunky image
521 pcx->bytes_per_line = LittleShort((short)width);
522 pcx->palette_type = LittleShort(2); // not a grey scale
527 for (i=0 ; i<height ; i++)
529 for (j=0 ; j<width ; j++)
531 if ( (*data & 0xc0) != 0xc0)
542 *pack++ = 0x0c; // palette ID byte
543 for (i=0 ; i<768 ; i++)
544 *pack++ = *palette++;
547 length = pack - (byte *)pcx;
548 SaveFile (filename, pcx, length);
555 ============================================================================
559 ============================================================================
566 Will load either an lbm or pcx, depending on extension.
567 Any of the return pointers can be NULL if you don't want them.
570 void Load256Image (char *name, byte **pixels, byte **palette,
571 int *width, int *height)
575 ExtractFileExtension (name, ext);
576 if (!Q_strncasecmp (ext, "lbm", strlen(ext) ))
578 LoadLBM (name, pixels, palette);
584 else if (!Q_strncasecmp (ext, "pcx",strlen(ext)))
586 LoadPCX (name, pixels, palette, width, height);
589 Error ("%s doesn't have a known image extension", name);
597 Will save either an lbm or pcx, depending on extension.
600 void Save256Image (char *name, byte *pixels, byte *palette,
601 int width, int height)
605 ExtractFileExtension (name, ext);
606 if (!Q_strncasecmp (ext, "lbm",strlen(ext)))
608 WriteLBMfile (name, pixels, width, height, palette);
610 else if (!Q_strncasecmp (ext, "pcx",strlen(ext)))
612 WritePCXfile (name, pixels, width, height, palette);
615 Error ("%s doesn't have a known image extension", name);
622 ============================================================================
626 ============================================================================
629 typedef struct _TargaHeader {
630 unsigned char id_length, colormap_type, image_type;
631 unsigned short colormap_index, colormap_length;
632 unsigned char colormap_size;
633 unsigned short x_origin, y_origin, width, height;
634 unsigned char pixel_size, attributes;
637 int fgetLittleShort (FILE *f)
644 return (short)(b1 + b2*256);
647 int fgetLittleLong (FILE *f)
656 return b1 + (b2<<8) + (b3<<16) + (b4<<24);
665 void LoadTGA (char *name, byte **pixels, int *width, int *height)
667 int columns, rows, numPixels;
672 TargaHeader targa_header;
674 fin = fopen (name, "rb");
676 Error ("Couldn't read %s", name);
678 targa_header.id_length = fgetc(fin);
679 targa_header.colormap_type = fgetc(fin);
680 targa_header.image_type = fgetc(fin);
682 targa_header.colormap_index = fgetLittleShort(fin);
683 targa_header.colormap_length = fgetLittleShort(fin);
684 targa_header.colormap_size = fgetc(fin);
685 targa_header.x_origin = fgetLittleShort(fin);
686 targa_header.y_origin = fgetLittleShort(fin);
687 targa_header.width = fgetLittleShort(fin);
688 targa_header.height = fgetLittleShort(fin);
689 targa_header.pixel_size = fgetc(fin);
690 targa_header.attributes = fgetc(fin);
692 if (targa_header.image_type!=2
693 && targa_header.image_type!=10)
694 Error ("LoadTGA: Only type 2 and 10 targa RGB images supported\n");
696 if (targa_header.colormap_type !=0
697 || (targa_header.pixel_size!=32 && targa_header.pixel_size!=24))
698 Error ("Texture_LoadTGA: Only 32 or 24 bit images supported (no colormaps)\n");
700 columns = targa_header.width;
701 rows = targa_header.height;
702 numPixels = columns * rows;
708 targa_rgba = malloc(numPixels*4);
709 *pixels = targa_rgba;
711 if (targa_header.id_length != 0)
712 fseek(fin, targa_header.id_length, SEEK_CUR); // skip TARGA image comment
714 if (targa_header.image_type==2) { // Uncompressed, RGB images
715 for(row=rows-1; row>=0; row--) {
716 pixbuf = targa_rgba + row*columns*4;
717 for(column=0; column<columns; column++) {
718 unsigned char red,green,blue,alphabyte;
719 switch (targa_header.pixel_size) {
734 alphabyte = getc(fin);
738 *pixbuf++ = alphabyte;
744 else if (targa_header.image_type==10) { // Runlength encoded RGB images
745 unsigned char red,green,blue,alphabyte,packetHeader,packetSize,j;
746 for(row=rows-1; row>=0; row--) {
747 pixbuf = targa_rgba + row*columns*4;
748 for(column=0; column<columns; ) {
749 packetHeader=getc(fin);
750 packetSize = 1 + (packetHeader & 0x7f);
751 if (packetHeader & 0x80) { // run-length packet
752 switch (targa_header.pixel_size) {
763 alphabyte = getc(fin);
767 for(j=0;j<packetSize;j++) {
773 if (column==columns) { // run spans across rows
779 pixbuf = targa_rgba + row*columns*4;
783 else { // non run-length packet
784 for(j=0;j<packetSize;j++) {
785 switch (targa_header.pixel_size) {
799 alphabyte = getc(fin);
803 *pixbuf++ = alphabyte;
807 if (column==columns) { // pixel packet run spans across rows
813 pixbuf = targa_rgba + row*columns*4;
822 // vertically flipped
823 if ( (targa_header.attributes & (1<<5)) ) {
825 for (row = 0; row < .5f * rows; row++)
827 for (column = 0; column < columns; column++)
829 flip = *( (int*)targa_rgba + row * columns + column);
830 *( (int*)targa_rgba + row * columns + column) = *( (int*)targa_rgba + ( ( rows - 1 ) - row ) * columns + column );
831 *( (int*)targa_rgba + ( ( rows - 1 ) - row ) * columns + column ) = flip;