2 ===========================================================================
3 Copyright (C) 1997-2006 Id Software, Inc.
5 This file is part of Quake 2 Tools source code.
7 Quake 2 Tools source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
12 Quake 2 Tools source code is distributed in the hope that it will be
13 useful, 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 Quake 2 Tools source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
31 ============================================================================
35 ============================================================================
39 typedef unsigned char UBYTE;
40 //conflicts with windows typedef short WORD;
41 typedef unsigned short UWORD;
66 UWORD transparentColor;
67 UBYTE xAspect,yAspect;
68 short pageWidth,pageHeight;
71 extern bmhd_t bmhd; // will be in native byte order
75 #define FORMID ('F'+('O'<<8)+((int)'R'<<16)+((int)'M'<<24))
76 #define ILBMID ('I'+('L'<<8)+((int)'B'<<16)+((int)'M'<<24))
77 #define PBMID ('P'+('B'<<8)+((int)'M'<<16)+((int)' '<<24))
78 #define BMHDID ('B'+('M'<<8)+((int)'H'<<16)+((int)'D'<<24))
79 #define BODYID ('B'+('O'<<8)+((int)'D'<<16)+((int)'Y'<<24))
80 #define CMAPID ('C'+('M'<<8)+((int)'A'<<16)+((int)'P'<<24))
98 Source must be evenly aligned!
101 byte *LBMRLEDecompress (byte *source,byte *unpacked, int bpwidth)
114 rept = (rept^0xff)+2;
116 memset(unpacked,b,rept);
119 else if (rept < 0x80)
122 memcpy(unpacked,source,rept);
127 rept = 0; // rept of 0x80 is NOP
131 } while (count<bpwidth);
134 Error ("Decompression exceeded width!\n");
146 void LoadLBM (char *filename, byte **picture, byte **palette)
148 byte *LBMbuffer, *picbuffer, *cmapbuffer;
150 byte *LBM_P, *LBMEND_P;
154 int formtype,formlength;
155 int chunktype,chunklength;
157 // qiet compiler warnings
164 LoadFile (filename, (void **)&LBMbuffer);
167 // parse the LBM header
170 if ( *(int *)LBMbuffer != LittleLong(FORMID) )
171 Error ("No FORM ID at start of file!\n");
174 formlength = BigLong( *(int *)LBM_P );
176 LBMEND_P = LBM_P + Align(formlength);
178 formtype = LittleLong(*(int *)LBM_P);
180 if (formtype != ILBMID && formtype != PBMID)
181 Error ("Unrecognized form type: %c%c%c%c\n", formtype&0xff
182 ,(formtype>>8)&0xff,(formtype>>16)&0xff,(formtype>>24)&0xff);
190 while (LBM_P < LBMEND_P)
192 chunktype = LBM_P[0] + (LBM_P[1]<<8) + (LBM_P[2]<<16) + (LBM_P[3]<<24);
194 chunklength = LBM_P[3] + (LBM_P[2]<<8) + (LBM_P[1]<<16) + (LBM_P[0]<<24);
200 memcpy (&bmhd,LBM_P,sizeof(bmhd));
201 bmhd.w = BigShort(bmhd.w);
202 bmhd.h = BigShort(bmhd.h);
203 bmhd.x = BigShort(bmhd.x);
204 bmhd.y = BigShort(bmhd.y);
205 bmhd.pageWidth = BigShort(bmhd.pageWidth);
206 bmhd.pageHeight = BigShort(bmhd.pageHeight);
210 cmapbuffer = malloc (768);
211 memset (cmapbuffer, 0, 768);
212 memcpy (cmapbuffer, LBM_P, chunklength);
218 pic_p = picbuffer = malloc (bmhd.w*bmhd.h);
219 if (formtype == PBMID)
224 for (y=0 ; y<bmhd.h ; y++, pic_p += bmhd.w)
226 if (bmhd.compression == cm_rle1)
227 body_p = LBMRLEDecompress ((byte *)body_p
229 else if (bmhd.compression == cm_none)
231 memcpy (pic_p,body_p,bmhd.w);
232 body_p += Align(bmhd.w);
242 Error ("%s is an interlaced LBM, not packed", filename);
247 LBM_P += Align(chunklength);
252 *picture = picbuffer;
255 *palette = cmapbuffer;
260 ============================================================================
264 ============================================================================
272 void WriteLBMfile (char *filename, byte *data,
273 int width, int height, byte *palette)
276 int *formlength, *bmhdlength, *cmaplength, *bodylength;
280 lbm = lbmptr = malloc (width*height+1000);
290 formlength = (int*)lbmptr;
291 lbmptr+=4; // leave space for length
306 bmhdlength = (int *)lbmptr;
307 lbmptr+=4; // leave space for length
309 memset (&basebmhd,0,sizeof(basebmhd));
310 basebmhd.w = BigShort((short)width);
311 basebmhd.h = BigShort((short)height);
312 basebmhd.nPlanes = 8;
313 basebmhd.xAspect = 5;
314 basebmhd.yAspect = 6;
315 basebmhd.pageWidth = BigShort((short)width);
316 basebmhd.pageHeight = BigShort((short)height);
318 memcpy (lbmptr,&basebmhd,sizeof(basebmhd));
319 lbmptr += sizeof(basebmhd);
321 length = lbmptr-(byte *)bmhdlength-4;
322 *bmhdlength = BigLong(length);
324 *lbmptr++ = 0; // pad chunk to even offset
334 cmaplength = (int *)lbmptr;
335 lbmptr+=4; // leave space for length
337 memcpy (lbmptr,palette,768);
340 length = lbmptr-(byte *)cmaplength-4;
341 *cmaplength = BigLong(length);
343 *lbmptr++ = 0; // pad chunk to even offset
353 bodylength = (int *)lbmptr;
354 lbmptr+=4; // leave space for length
356 memcpy (lbmptr,data,width*height);
357 lbmptr += width*height;
359 length = lbmptr-(byte *)bodylength-4;
360 *bodylength = BigLong(length);
362 *lbmptr++ = 0; // pad chunk to even offset
367 length = lbmptr-(byte *)formlength-4;
368 *formlength = BigLong(length);
370 *lbmptr++ = 0; // pad chunk to even offset
375 SaveFile (filename, lbm, lbmptr-lbm);
381 ============================================================================
385 ============================================================================
394 unsigned short xmin,ymin,xmax,ymax;
395 unsigned short hres,vres;
396 unsigned char palette[48];
399 unsigned short bytes_per_line;
400 unsigned short palette_type;
402 unsigned char data; // unbounded
411 void LoadPCX (char *filename, byte **pic, byte **palette, int *width, int *height)
417 int dataByte, runLength;
432 len = LoadFile (filename, (void **)&raw);
437 // parse the PCX file
442 pcx->xmin = LittleShort(pcx->xmin);
443 pcx->ymin = LittleShort(pcx->ymin);
444 pcx->xmax = LittleShort(pcx->xmax);
445 pcx->ymax = LittleShort(pcx->ymax);
446 pcx->hres = LittleShort(pcx->hres);
447 pcx->vres = LittleShort(pcx->vres);
448 pcx->bytes_per_line = LittleShort(pcx->bytes_per_line);
449 pcx->palette_type = LittleShort(pcx->palette_type);
451 if (pcx->manufacturer != 0x0a
453 || pcx->encoding != 1
454 || pcx->bits_per_pixel != 8
457 Error ("Bad pcx file %s", filename);
461 *palette = malloc(768);
462 memcpy (*palette, (byte *)pcx + len - 768, 768);
466 *width = pcx->xmax+1;
468 *height = pcx->ymax+1;
473 out = malloc ( (pcx->ymax+1) * (pcx->xmax+1) );
475 Error ("Skin_Cache: couldn't allocate");
481 for (y=0 ; y<=pcx->ymax ; y++, pix += pcx->xmax+1)
483 for (x=0 ; x<=pcx->xmax ; )
487 if((dataByte & 0xC0) == 0xC0)
489 runLength = dataByte & 0x3F;
495 while(runLength-- > 0)
501 if ( raw - (byte *)pcx > len)
502 Error ("PCX file %s was malformed", filename);
512 void WritePCXfile (char *filename, byte *data,
513 int width, int height, byte *palette)
519 pcx = malloc (width*height*2+1000);
520 memset (pcx, 0, sizeof(*pcx));
522 pcx->manufacturer = 0x0a; // PCX id
523 pcx->version = 5; // 256 color
524 pcx->encoding = 1; // uncompressed
525 pcx->bits_per_pixel = 8; // 256 color
528 pcx->xmax = LittleShort((short)(width-1));
529 pcx->ymax = LittleShort((short)(height-1));
530 pcx->hres = LittleShort((short)width);
531 pcx->vres = LittleShort((short)height);
532 pcx->color_planes = 1; // chunky image
533 pcx->bytes_per_line = LittleShort((short)width);
534 pcx->palette_type = LittleShort(2); // not a grey scale
539 for (i=0 ; i<height ; i++)
541 for (j=0 ; j<width ; j++)
543 if ( (*data & 0xc0) != 0xc0)
554 *pack++ = 0x0c; // palette ID byte
555 for (i=0 ; i<768 ; i++)
556 *pack++ = *palette++;
559 length = pack - (byte *)pcx;
560 SaveFile (filename, pcx, length);
567 ============================================================================
571 ============================================================================
578 Will load either an lbm or pcx, depending on extension.
579 Any of the return pointers can be NULL if you don't want them.
582 void Load256Image (char *name, byte **pixels, byte **palette,
583 int *width, int *height)
587 ExtractFileExtension (name, ext);
588 if (!Q_strcasecmp (ext, "lbm"))
590 LoadLBM (name, pixels, palette);
596 else if (!Q_strcasecmp (ext, "pcx"))
598 LoadPCX (name, pixels, palette, width, height);
601 Error ("%s doesn't have a known image extension", name);
609 Will save either an lbm or pcx, depending on extension.
612 void Save256Image (char *name, byte *pixels, byte *palette,
613 int width, int height)
617 ExtractFileExtension (name, ext);
618 if (!Q_strcasecmp (ext, "lbm"))
620 WriteLBMfile (name, pixels, width, height, palette);
622 else if (!Q_strcasecmp (ext, "pcx"))
624 WritePCXfile (name, pixels, width, height, palette);
627 Error ("%s doesn't have a known image extension", name);
634 ============================================================================
638 ============================================================================
641 typedef struct _TargaHeader {
642 unsigned char id_length, colormap_type, image_type;
643 unsigned short colormap_index, colormap_length;
644 unsigned char colormap_size;
645 unsigned short x_origin, y_origin, width, height;
646 unsigned char pixel_size, attributes;
649 int fgetLittleShort (FILE *f)
656 return (short)(b1 + b2*256);
659 int fgetLittleLong (FILE *f)
668 return b1 + (b2<<8) + (b3<<16) + (b4<<24);
677 void LoadTGA (char *name, byte **pixels, int *width, int *height)
679 int columns, rows, numPixels;
684 TargaHeader targa_header;
686 fin = fopen (name, "rb");
688 Error ("Couldn't read %s", name);
690 targa_header.id_length = fgetc(fin);
691 targa_header.colormap_type = fgetc(fin);
692 targa_header.image_type = fgetc(fin);
694 targa_header.colormap_index = fgetLittleShort(fin);
695 targa_header.colormap_length = fgetLittleShort(fin);
696 targa_header.colormap_size = fgetc(fin);
697 targa_header.x_origin = fgetLittleShort(fin);
698 targa_header.y_origin = fgetLittleShort(fin);
699 targa_header.width = fgetLittleShort(fin);
700 targa_header.height = fgetLittleShort(fin);
701 targa_header.pixel_size = fgetc(fin);
702 targa_header.attributes = fgetc(fin);
704 if (targa_header.image_type!=2
705 && targa_header.image_type!=10)
706 Error ("LoadTGA: Only type 2 and 10 targa RGB images supported\n");
708 if (targa_header.colormap_type !=0
709 || (targa_header.pixel_size!=32 && targa_header.pixel_size!=24))
710 Error ("Texture_LoadTGA: Only 32 or 24 bit images supported (no colormaps)\n");
712 columns = targa_header.width;
713 rows = targa_header.height;
714 numPixels = columns * rows;
720 targa_rgba = malloc(numPixels*4);
721 *pixels = targa_rgba;
723 if (targa_header.id_length != 0)
724 fseek(fin, targa_header.id_length, SEEK_CUR); // skip TARGA image comment
726 if (targa_header.image_type==2) { // Uncompressed, RGB images
727 for(row=rows-1; row>=0; row--) {
728 pixbuf = targa_rgba + row*columns*4;
729 for(column=0; column<columns; column++) {
730 unsigned char red,green,blue,alphabyte;
731 switch (targa_header.pixel_size) {
746 alphabyte = getc(fin);
750 *pixbuf++ = alphabyte;
756 else if (targa_header.image_type==10) { // Runlength encoded RGB images
757 unsigned char red,green,blue,alphabyte,packetHeader,packetSize,j;
758 for(row=rows-1; row>=0; row--) {
759 pixbuf = targa_rgba + row*columns*4;
760 for(column=0; column<columns; ) {
761 packetHeader=getc(fin);
762 packetSize = 1 + (packetHeader & 0x7f);
763 if (packetHeader & 0x80) { // run-length packet
764 switch (targa_header.pixel_size) {
775 alphabyte = getc(fin);
779 for(j=0;j<packetSize;j++) {
785 if (column==columns) { // run spans across rows
791 pixbuf = targa_rgba + row*columns*4;
795 else { // non run-length packet
796 for(j=0;j<packetSize;j++) {
797 switch (targa_header.pixel_size) {
811 alphabyte = getc(fin);
815 *pixbuf++ = alphabyte;
819 if (column==columns) { // pixel packet run spans across rows
825 pixbuf = targa_rgba + row*columns*4;