1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330 |
- /*
- ===========================================================================
- Copyright (C) 1999-2005 Id Software, Inc.
- This file is part of Quake III Arena source code.
- Quake III Arena source code is free software; you can redistribute it
- and/or modify it under the terms of the GNU General Public License as
- published by the Free Software Foundation; either version 2 of the License,
- or (at your option) any later version.
- Quake III Arena source code is distributed in the hope that it will be
- useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with Foobar; if not, write to the Free Software
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- ===========================================================================
- */
- // lbmlib.c
- #include "stdafx.h"
- #include "cmdlib.h"
- #include "lbmlib.h"
- #include "bmp.h"
- #include "pakstuff.h"
- #include "jpeglib.h"
- /*
- ============================================================================
- LBM STUFF
- ============================================================================
- */
- typedef unsigned char UBYTE;
- //conflicts with windows typedef short WORD;
- typedef unsigned short UWORD;
- typedef long LONG;
- typedef enum
- {
- ms_none,
- ms_mask,
- ms_transcolor,
- ms_lasso
- } mask_t;
- typedef enum
- {
- cm_none,
- cm_rle1
- } compress_t;
- typedef struct
- {
- UWORD w,h;
- short x,y;
- UBYTE nPlanes;
- UBYTE masking;
- UBYTE compression;
- UBYTE pad1;
- UWORD transparentColor;
- UBYTE xAspect,yAspect;
- short pageWidth,pageHeight;
- } bmhd_t;
- extern bmhd_t bmhd; // will be in native byte order
- #define FORMID ('F'+('O'<<8)+((int)'R'<<16)+((int)'M'<<24))
- #define ILBMID ('I'+('L'<<8)+((int)'B'<<16)+((int)'M'<<24))
- #define PBMID ('P'+('B'<<8)+((int)'M'<<16)+((int)' '<<24))
- #define BMHDID ('B'+('M'<<8)+((int)'H'<<16)+((int)'D'<<24))
- #define BODYID ('B'+('O'<<8)+((int)'D'<<16)+((int)'Y'<<24))
- #define CMAPID ('C'+('M'<<8)+((int)'A'<<16)+((int)'P'<<24))
- bmhd_t bmhd;
- int Align (int l)
- {
- if (l&1)
- return l+1;
- return l;
- }
- /*
- ================
- LBMRLEdecompress
- Source must be evenly aligned!
- ================
- */
- byte *LBMRLEDecompress (byte *source,byte *unpacked, int bpwidth)
- {
- int count;
- byte b,rept;
- count = 0;
- do
- {
- rept = *source++;
- if (rept > 0x80)
- {
- rept = (rept^0xff)+2;
- b = *source++;
- memset(unpacked,b,rept);
- unpacked += rept;
- }
- else if (rept < 0x80)
- {
- rept++;
- memcpy(unpacked,source,rept);
- unpacked += rept;
- source += rept;
- }
- else
- rept = 0; // rept of 0x80 is NOP
- count += rept;
- } while (count<bpwidth);
- if (count>bpwidth)
- Error ("Decompression exceeded width!\n");
- return source;
- }
- /*
- =================
- LoadLBM
- =================
- */
- void LoadLBM (char *filename, byte **picture, byte **palette)
- {
- byte *LBMbuffer, *picbuffer, *cmapbuffer;
- int y;
- byte *LBM_P, *LBMEND_P;
- byte *pic_p;
- byte *body_p;
- int formtype,formlength;
- int chunktype,chunklength;
- // qiet compiler warnings
- picbuffer = NULL;
- cmapbuffer = NULL;
- //
- // load the LBM
- //
- LoadFile (filename, (void **)&LBMbuffer);
- if (LBMbuffer == NULL)
- {
- return;
- }
- //
- // parse the LBM header
- //
- LBM_P = LBMbuffer;
- if ( *(int *)LBMbuffer != LittleLong(FORMID) )
- Error ("No FORM ID at start of file!\n");
- LBM_P += 4;
- formlength = BigLong( *(int *)LBM_P );
- LBM_P += 4;
- LBMEND_P = LBM_P + Align(formlength);
- formtype = LittleLong(*(int *)LBM_P);
- if (formtype != ILBMID && formtype != PBMID)
- Error ("Unrecognized form type: %c%c%c%c\n", formtype&0xff
- ,(formtype>>8)&0xff,(formtype>>16)&0xff,(formtype>>24)&0xff);
- LBM_P += 4;
- //
- // parse chunks
- //
- while (LBM_P < LBMEND_P)
- {
- chunktype = LBM_P[0] + (LBM_P[1]<<8) + (LBM_P[2]<<16) + (LBM_P[3]<<24);
- LBM_P += 4;
- chunklength = LBM_P[3] + (LBM_P[2]<<8) + (LBM_P[1]<<16) + (LBM_P[0]<<24);
- LBM_P += 4;
- switch ( chunktype )
- {
- case BMHDID:
- memcpy (&bmhd,LBM_P,sizeof(bmhd));
- bmhd.w = BigShort(bmhd.w);
- bmhd.h = BigShort(bmhd.h);
- bmhd.x = BigShort(bmhd.x);
- bmhd.y = BigShort(bmhd.y);
- bmhd.pageWidth = BigShort(bmhd.pageWidth);
- bmhd.pageHeight = BigShort(bmhd.pageHeight);
- break;
- case CMAPID:
- cmapbuffer = (unsigned char*)malloc (768);
- memset (cmapbuffer, 0, 768);
- memcpy (cmapbuffer, LBM_P, chunklength);
- break;
- case BODYID:
- body_p = LBM_P;
- pic_p = picbuffer = (unsigned char*)malloc (bmhd.w*bmhd.h);
- if (formtype == PBMID)
- {
- //
- // unpack PBM
- //
- for (y=0 ; y<bmhd.h ; y++, pic_p += bmhd.w)
- {
- if (bmhd.compression == cm_rle1)
- body_p = LBMRLEDecompress ((byte *)body_p
- , pic_p , bmhd.w);
- else if (bmhd.compression == cm_none)
- {
- memcpy (pic_p,body_p,bmhd.w);
- body_p += Align(bmhd.w);
- }
- }
- }
- else
- {
- //
- // unpack ILBM
- //
- Error ("%s is an interlaced LBM, not packed", filename);
- }
- break;
- }
- LBM_P += Align(chunklength);
- }
- free (LBMbuffer);
- *picture = picbuffer;
- if (palette)
- *palette = cmapbuffer;
- }
- /*
- ============================================================================
- WRITE LBM
- ============================================================================
- */
- /*
- ==============
- WriteLBMfile
- ==============
- */
- void WriteLBMfile (char *filename, byte *data,
- int width, int height, byte *palette)
- {
- byte *lbm, *lbmptr;
- int *formlength, *bmhdlength, *cmaplength, *bodylength;
- int length;
- bmhd_t basebmhd;
- lbm = lbmptr = (unsigned char*)malloc (width*height+1000);
- //
- // start FORM
- //
- *lbmptr++ = 'F';
- *lbmptr++ = 'O';
- *lbmptr++ = 'R';
- *lbmptr++ = 'M';
- formlength = (int*)lbmptr;
- lbmptr+=4; // leave space for length
- *lbmptr++ = 'P';
- *lbmptr++ = 'B';
- *lbmptr++ = 'M';
- *lbmptr++ = ' ';
- //
- // write BMHD
- //
- *lbmptr++ = 'B';
- *lbmptr++ = 'M';
- *lbmptr++ = 'H';
- *lbmptr++ = 'D';
- bmhdlength = (int *)lbmptr;
- lbmptr+=4; // leave space for length
- memset (&basebmhd,0,sizeof(basebmhd));
- basebmhd.w = BigShort((short)width);
- basebmhd.h = BigShort((short)height);
- basebmhd.nPlanes = 8;
- basebmhd.xAspect = 5;
- basebmhd.yAspect = 6;
- basebmhd.pageWidth = BigShort((short)width);
- basebmhd.pageHeight = BigShort((short)height);
- memcpy (lbmptr,&basebmhd,sizeof(basebmhd));
- lbmptr += sizeof(basebmhd);
- length = lbmptr-(byte *)bmhdlength-4;
- *bmhdlength = BigLong(length);
- if (length&1)
- *lbmptr++ = 0; // pad chunk to even offset
- //
- // write CMAP
- //
- *lbmptr++ = 'C';
- *lbmptr++ = 'M';
- *lbmptr++ = 'A';
- *lbmptr++ = 'P';
- cmaplength = (int *)lbmptr;
- lbmptr+=4; // leave space for length
- memcpy (lbmptr,palette,768);
- lbmptr += 768;
- length = lbmptr-(byte *)cmaplength-4;
- *cmaplength = BigLong(length);
- if (length&1)
- *lbmptr++ = 0; // pad chunk to even offset
- //
- // write BODY
- //
- *lbmptr++ = 'B';
- *lbmptr++ = 'O';
- *lbmptr++ = 'D';
- *lbmptr++ = 'Y';
- bodylength = (int *)lbmptr;
- lbmptr+=4; // leave space for length
- memcpy (lbmptr,data,width*height);
- lbmptr += width*height;
- length = lbmptr-(byte *)bodylength-4;
- *bodylength = BigLong(length);
- if (length&1)
- *lbmptr++ = 0; // pad chunk to even offset
- //
- // done
- //
- length = lbmptr-(byte *)formlength-4;
- *formlength = BigLong(length);
- if (length&1)
- *lbmptr++ = 0; // pad chunk to even offset
- //
- // write output file
- //
- SaveFile (filename, lbm, lbmptr-lbm);
- free (lbm);
- }
- /*
- ============================================================================
- LOAD PCX
- ============================================================================
- */
- typedef struct
- {
- char manufacturer;
- char version;
- char encoding;
- char bits_per_pixel;
- unsigned short xmin,ymin,xmax,ymax;
- unsigned short hres,vres;
- unsigned char palette[48];
- char reserved;
- char color_planes;
- unsigned short bytes_per_line;
- unsigned short palette_type;
- char filler[58];
- unsigned char data; // unbounded
- } pcx_t;
- /*
- ==============
- LoadPCX
- ==============
- */
- void LoadPCX (char *filename, byte **pic, byte **palette, int *width, int *height)
- {
- byte *raw = 0;
- pcx_t *pcx;
- int x, y;
- int len;
- int dataByte, runLength;
- byte *out, *pix;
- if (pic)
- *pic = NULL;
- if (palette)
- *palette = NULL;
- if (width)
- *width = 0;
- if (height)
- *height = 0;
- //
- // load the file
- //
- len = LoadFile (filename, (void **)&raw);
- if (len == -1)
- return;
- //
- // parse the PCX file
- //
- pcx = (pcx_t *)raw;
- raw = &pcx->data;
- pcx->xmin = LittleShort(pcx->xmin);
- pcx->ymin = LittleShort(pcx->ymin);
- pcx->xmax = LittleShort(pcx->xmax);
- pcx->ymax = LittleShort(pcx->ymax);
- pcx->hres = LittleShort(pcx->hres);
- pcx->vres = LittleShort(pcx->vres);
- pcx->bytes_per_line = LittleShort(pcx->bytes_per_line);
- pcx->palette_type = LittleShort(pcx->palette_type);
- if (pcx->manufacturer != 0x0a
- || pcx->version != 5
- || pcx->encoding != 1
- || pcx->bits_per_pixel != 8
- || pcx->xmax >= 640
- || pcx->ymax >= 480)
- Error ("Bad pcx file %s", filename);
-
- if (palette)
- {
- *palette = (unsigned char*)malloc(768);
- memcpy (*palette, (byte *)pcx + len - 768, 768);
- }
- if (width)
- *width = pcx->xmax+1;
- if (height)
- *height = pcx->ymax+1;
- if (!pic)
- {
- free(pcx);
- return;
- }
- out = (unsigned char*)malloc ( (pcx->ymax+1) * (pcx->xmax+1) );
- if (!out)
- Error ("Skin_Cache: couldn't allocate");
- *pic = out;
- pix = out;
- for (y=0 ; y<=pcx->ymax ; y++, pix += pcx->xmax+1)
- {
- for (x=0 ; x<=pcx->xmax ; )
- {
- dataByte = *raw++;
- if((dataByte & 0xC0) == 0xC0)
- {
- runLength = dataByte & 0x3F;
- dataByte = *raw++;
- }
- else
- runLength = 1;
- while(runLength-- > 0)
- pix[x++] = dataByte;
- }
- }
- if ( raw - (byte *)pcx > len)
- Error ("PCX file %s was malformed", filename);
- free (pcx);
- }
- /*
- ==============
- WritePCXfile
- ==============
- */
- void WritePCXfile (char *filename, byte *data,
- int width, int height, byte *palette)
- {
- int i, j, length;
- pcx_t *pcx;
- byte *pack;
-
- pcx = (pcx_t*)malloc (width*height*2+1000);
- memset (pcx, 0, sizeof(*pcx));
- pcx->manufacturer = 0x0a; // PCX id
- pcx->version = 5; // 256 color
- pcx->encoding = 1; // uncompressed
- pcx->bits_per_pixel = 8; // 256 color
- pcx->xmin = 0;
- pcx->ymin = 0;
- pcx->xmax = LittleShort((short)(width-1));
- pcx->ymax = LittleShort((short)(height-1));
- pcx->hres = LittleShort((short)width);
- pcx->vres = LittleShort((short)height);
- pcx->color_planes = 1; // chunky image
- pcx->bytes_per_line = LittleShort((short)width);
- pcx->palette_type = LittleShort(2); // not a grey scale
- // pack the image
- pack = &pcx->data;
-
- for (i=0 ; i<height ; i++)
- {
- for (j=0 ; j<width ; j++)
- {
- if ( (*data & 0xc0) != 0xc0)
- *pack++ = *data++;
- else
- {
- *pack++ = 0xc1;
- *pack++ = *data++;
- }
- }
- }
-
- // write the palette
- *pack++ = 0x0c; // palette ID byte
- for (i=0 ; i<768 ; i++)
- *pack++ = *palette++;
-
- // write output file
- length = pack - (byte *)pcx;
- SaveFile (filename, pcx, length);
- free (pcx);
- }
- /*
- ============================================================================
- LOAD IMAGE
- ============================================================================
- */
- /*
- ==============
- Load256Image
- Will load either an lbm or pcx, depending on extension.
- Any of the return pointers can be NULL if you don't want them.
- ==============
- */
- void Load256Image (char *name, byte **pixels, byte **palette,
- int *width, int *height)
- {
- char ext[128];
- bitmap_t bmp;
- ExtractFileExtension (name, ext);
- if (stricmp(ext, "lbm"))
- {
- LoadLBM (name, pixels, palette);
- if (width)
- *width = bmhd.w;
- if (height)
- *height = bmhd.h;
- }
- else if (stricmp (ext, "pcx"))
- {
- LoadPCX (name, pixels, palette, width, height);
- }
- else if (stricmp (ext, "bmp"))
- {
- LoadBMP (name, &bmp);
- if (bmp.palette)
- {
- *palette = (unsigned char*)malloc (768);
- memcpy (*palette, bmp.palette, 768);
- }
- FreeBMP (&bmp);
- }
- else
- Error ("%s doesn't have a known image extension", name);
- }
- /*
- ==============
- Save256Image
- Will save either an lbm or pcx, depending on extension.
- ==============
- */
- void Save256Image (char *name, byte *pixels, byte *palette,
- int width, int height)
- {
- char ext[128];
- ExtractFileExtension (name, ext);
- if (!strcmp (ext, "lbm"))
- {
- WriteLBMfile (name, pixels, width, height, palette);
- }
- else if (!stricmp (ext, "pcx"))
- {
- WritePCXfile (name, pixels, width, height, palette);
- }
- else
- Error ("%s doesn't have a known image extension", name);
- }
- /*
- ============================================================================
- TARGA IMAGE
- ============================================================================
- */
- typedef struct _TargaHeader {
- unsigned char id_length, colormap_type, image_type;
- unsigned short colormap_index, colormap_length;
- unsigned char colormap_size;
- unsigned short x_origin, y_origin, width, height;
- unsigned char pixel_size, attributes;
- } TargaHeader;
- int fgetLittleShort (FILE *f)
- {
- byte b1, b2;
- b1 = fgetc(f);
- b2 = fgetc(f);
- return (short)(b1 + b2*256);
- }
- int getLittleShort (byte*& p)
- {
- byte b1, b2;
- b1 = *p++;
- b2 = *p++;
- return (short)(b1 + b2*256);
- }
- int getLittleLong (byte*& p)
- {
- byte b1, b2, b3, b4;
- b1 = *p++;
- b2 = *p++;
- b3 = *p++;
- b4 = *p++;
- return b1 + (b2<<8) + (b3<<16) + (b4<<24);
- }
- char getc(byte*& p)
- {
- return *p++;
- }
- /*
- =========================================================
- BMP LOADING
- =========================================================
- */
- typedef struct
- {
- char id[2];
- unsigned long fileSize;
- unsigned long reserved0;
- unsigned long bitmapDataOffset;
- unsigned long bitmapHeaderSize;
- unsigned long width;
- unsigned long height;
- unsigned short planes;
- unsigned short bitsPerPixel;
- unsigned long compression;
- unsigned long bitmapDataSize;
- unsigned long hRes;
- unsigned long vRes;
- unsigned long colors;
- unsigned long importantColors;
- unsigned char palette[256][4];
- } BMPHeader_t;
- static void LoadBMP( const char *name, byte **pic, int *width, int *height )
- {
- int columns, rows, numPixels;
- byte *pixbuf;
- int row, column;
- byte *buf_p;
- byte *buffer;
- unsigned int length;
- BMPHeader_t bmpHeader;
- byte *bmpRGBA;
- *pic = NULL;
- //
- // load the file
- //
- length = LoadFile( ( char * ) name, (void **)&buffer);
- if (length == -1)
- {
- length = PakLoadAnyFile( (char*)name, (void**)&buffer);
- if (length == -1)
- {
- return;
- }
- }
- buf_p = buffer;
- bmpHeader.id[0] = *buf_p++;
- bmpHeader.id[1] = *buf_p++;
- bmpHeader.fileSize = LittleLong( * ( long * ) buf_p );
- buf_p += 4;
- bmpHeader.reserved0 = LittleLong( * ( long * ) buf_p );
- buf_p += 4;
- bmpHeader.bitmapDataOffset = LittleLong( * ( long * ) buf_p );
- buf_p += 4;
- bmpHeader.bitmapHeaderSize = LittleLong( * ( long * ) buf_p );
- buf_p += 4;
- bmpHeader.width = LittleLong( * ( long * ) buf_p );
- buf_p += 4;
- bmpHeader.height = LittleLong( * ( long * ) buf_p );
- buf_p += 4;
- bmpHeader.planes = LittleShort( * ( short * ) buf_p );
- buf_p += 2;
- bmpHeader.bitsPerPixel = LittleShort( * ( short * ) buf_p );
- buf_p += 2;
- bmpHeader.compression = LittleLong( * ( long * ) buf_p );
- buf_p += 4;
- bmpHeader.bitmapDataSize = LittleLong( * ( long * ) buf_p );
- buf_p += 4;
- bmpHeader.hRes = LittleLong( * ( long * ) buf_p );
- buf_p += 4;
- bmpHeader.vRes = LittleLong( * ( long * ) buf_p );
- buf_p += 4;
- bmpHeader.colors = LittleLong( * ( long * ) buf_p );
- buf_p += 4;
- bmpHeader.importantColors = LittleLong( * ( long * ) buf_p );
- buf_p += 4;
- memcpy( bmpHeader.palette, buf_p, sizeof( bmpHeader.palette ) );
- if ( bmpHeader.bitsPerPixel == 8 )
- buf_p += 1024;
- if ( bmpHeader.id[0] != 'B' && bmpHeader.id[1] != 'M' )
- {
- Sys_Printf("LoadBMP: only Windows-style BMP files supported (%s)\n", name );
- }
- if ( bmpHeader.fileSize != length )
- {
- Sys_Printf("LoadBMP: header size does not match file size (%d vs. %d) (%s)\n", bmpHeader.fileSize, length, name );
- }
- if ( bmpHeader.compression != 0 )
- {
- Sys_Printf("LoadBMP: only uncompressed BMP files supported (%s)\n", name );
- }
- if ( bmpHeader.bitsPerPixel < 8 )
- {
- Sys_Printf("LoadBMP: monochrome and 4-bit BMP files not supported (%s)\n", name );
- }
- columns = bmpHeader.width;
- rows = bmpHeader.height;
- if ( rows < 0 )
- rows = -rows;
- numPixels = columns * rows;
- if ( width )
- *width = columns;
- if ( height )
- *height = rows;
- bmpRGBA = reinterpret_cast<unsigned char*>(malloc( numPixels * 4 ));
- *pic = bmpRGBA;
- for ( row = rows-1; row >= 0; row-- )
- {
- pixbuf = bmpRGBA + row*columns*4;
- for ( column = 0; column < columns; column++ )
- {
- unsigned char red, green, blue, alpha;
- int palIndex;
- unsigned short shortPixel;
- switch ( bmpHeader.bitsPerPixel )
- {
- case 8:
- palIndex = *buf_p++;
- *pixbuf++ = bmpHeader.palette[palIndex][2];
- *pixbuf++ = bmpHeader.palette[palIndex][1];
- *pixbuf++ = bmpHeader.palette[palIndex][0];
- *pixbuf++ = 0xff;
- break;
- case 16:
- shortPixel = * ( unsigned short * ) pixbuf;
- pixbuf += 2;
- *pixbuf++ = ( shortPixel & ( 31 << 10 ) ) >> 7;
- *pixbuf++ = ( shortPixel & ( 31 << 5 ) ) >> 2;
- *pixbuf++ = ( shortPixel & ( 31 ) ) << 3;
- *pixbuf++ = 0xff;
- break;
- case 24:
- blue = *buf_p++;
- green = *buf_p++;
- red = *buf_p++;
- *pixbuf++ = red;
- *pixbuf++ = green;
- *pixbuf++ = blue;
- *pixbuf++ = 255;
- break;
- case 32:
- blue = *buf_p++;
- green = *buf_p++;
- red = *buf_p++;
- alpha = *buf_p++;
- *pixbuf++ = red;
- *pixbuf++ = green;
- *pixbuf++ = blue;
- *pixbuf++ = alpha;
- break;
- default:
- Sys_Printf("LoadBMP: illegal pixel_size '%d' in file '%s'\n", bmpHeader.bitsPerPixel, name );
- break;
- }
- }
- }
- free( buffer );
- }
- /*
- =================================================================
- PCX LOADING
- =================================================================
- */
- /*
- ==============
- LoadPCX
- ==============
- */
- static void LoadPCX ( const char *filename, byte **pic, byte **palette, int *width, int *height)
- {
- byte *raw;
- pcx_t *pcx;
- int x, y;
- int len;
- int dataByte, runLength;
- byte *out, *pix;
- int xmax, ymax;
- *pic = NULL;
- *palette = NULL;
- //
- // load the file
- //
- len = LoadFile( ( char * ) filename, (void **)&raw);
- if (len == -1)
- {
- len = PakLoadAnyFile( (char*)filename, (void**)&raw);
- if (len == -1)
- {
- return;
- }
- }
- //
- // parse the PCX file
- //
- pcx = (pcx_t *)raw;
- raw = &pcx->data;
- xmax = LittleShort(pcx->xmax);
- ymax = LittleShort(pcx->ymax);
- if (pcx->manufacturer != 0x0a
- || pcx->version != 5
- || pcx->encoding != 1
- || pcx->bits_per_pixel != 8
- || xmax >= 1024
- || ymax >= 1024)
- {
- Sys_Printf ("Bad pcx file %s (%i x %i) (%i x %i)\n", filename, xmax+1, ymax+1, pcx->xmax, pcx->ymax);
- return;
- }
- out = reinterpret_cast<unsigned char*>(malloc ( (ymax+1) * (xmax+1) ));
- *pic = out;
- pix = out;
- if (palette)
- {
- *palette = reinterpret_cast<unsigned char*>(malloc(768));
- memcpy (*palette, (byte *)pcx + len - 768, 768);
- }
- if (width)
- *width = xmax+1;
- if (height)
- *height = ymax+1;
- // FIXME: use bytes_per_line here?
- for (y=0 ; y<=ymax ; y++, pix += xmax+1)
- {
- for (x=0 ; x<=xmax ; )
- {
- dataByte = *raw++;
- if((dataByte & 0xC0) == 0xC0)
- {
- runLength = dataByte & 0x3F;
- dataByte = *raw++;
- }
- else
- runLength = 1;
- while(runLength-- > 0)
- pix[x++] = dataByte;
- }
- }
- if ( raw - (byte *)pcx > len)
- {
- Sys_Printf ("PCX file %s was malformed", filename);
- free (*pic);
- *pic = NULL;
- }
- free(pcx);
- }
- /*
- ==============
- LoadPCX32
- ==============
- */
- static void LoadPCX32 ( const char *filename, byte **pic, int *width, int *height) {
- byte *palette;
- byte *pic8;
- int i, c, p;
- byte *pic32;
- LoadPCX (filename, &pic8, &palette, width, height);
- if (!pic8) {
- *pic = NULL;
- return;
- }
- c = (*width) * (*height);
- pic32 = *pic = reinterpret_cast<unsigned char*>(malloc(4 * c ));
- for (i = 0 ; i < c ; i++) {
- p = pic8[i];
- pic32[0] = palette[p*3];
- pic32[1] = palette[p*3 + 1];
- pic32[2] = palette[p*3 + 2];
- pic32[3] = 255;
- pic32 += 4;
- }
- free (pic8);
- free (palette);
- }
- /*
- =========================================================
- TARGA LOADING
- =========================================================
- */
- /*
- =============
- LoadTGA
- =============
- */
- void LoadTGA ( const char *name, byte **pic, int *width, int *height)
- {
- int columns, rows, numPixels;
- byte *pixbuf;
- int row, column;
- byte *buf_p;
- byte *buffer;
- TargaHeader targa_header;
- byte *targa_rgba;
- *pic = NULL;
- //
- // load the file
- //
- int nLen = LoadFile ( ( char * ) name, (void **)&buffer);
- if (nLen == -1)
- {
- nLen = PakLoadAnyFile((char*)name, (void**)&buffer);
- if (nLen == -1)
- {
- return;
- }
- }
- buf_p = buffer;
- targa_header.id_length = *buf_p++;
- targa_header.colormap_type = *buf_p++;
- targa_header.image_type = *buf_p++;
-
- targa_header.colormap_index = LittleShort ( *(short *)buf_p );
- buf_p += 2;
- targa_header.colormap_length = LittleShort ( *(short *)buf_p );
- buf_p += 2;
- targa_header.colormap_size = *buf_p++;
- targa_header.x_origin = LittleShort ( *(short *)buf_p );
- buf_p += 2;
- targa_header.y_origin = LittleShort ( *(short *)buf_p );
- buf_p += 2;
- targa_header.width = LittleShort ( *(short *)buf_p );
- buf_p += 2;
- targa_header.height = LittleShort ( *(short *)buf_p );
- buf_p += 2;
- targa_header.pixel_size = *buf_p++;
- targa_header.attributes = *buf_p++;
- //++timo debug
- if (targa_header.pixel_size == 32)
- Sys_Printf("%s is 32bit\n", name);
- bool bAlphaOK = false;
- if (targa_header.image_type!=2
- && targa_header.image_type!=10
- && targa_header.image_type != 3 )
- {
- Sys_Printf("LoadTGA: Only type 2 (RGB), 3 (gray), and 10 (RGB) TGA images supported\n");
- }
- if ( targa_header.colormap_type != 0 )
- {
- Sys_Printf("LoadTGA: colormaps not supported\n" );
- }
- if ( ( targa_header.pixel_size != 32 && targa_header.pixel_size != 24 ) && targa_header.image_type != 3 )
- {
- Sys_Printf("LoadTGA: Only 32 or 24 bit images supported (no colormaps)\n");
- }
- columns = targa_header.width;
- rows = targa_header.height;
- numPixels = columns * rows;
- if (width)
- *width = columns;
- if (height)
- *height = rows;
- targa_rgba = reinterpret_cast<unsigned char*>(malloc (numPixels*4));
- *pic = targa_rgba;
- if (targa_header.id_length != 0)
- buf_p += targa_header.id_length; // skip TARGA image comment
-
- if ( targa_header.image_type==2 || targa_header.image_type == 3 )
- {
- // Uncompressed RGB or gray scale image
- for(row=rows-1; row>=0; row--)
- {
- pixbuf = targa_rgba + row*columns*4;
- for(column=0; column<columns; column++)
- {
- unsigned char red,green,blue,alphabyte;
- switch (targa_header.pixel_size)
- {
-
- case 8:
- blue = *buf_p++;
- green = blue;
- red = blue;
- *pixbuf++ = red;
- *pixbuf++ = green;
- *pixbuf++ = blue;
- *pixbuf++ = 255;
- break;
- case 24:
- blue = *buf_p++;
- green = *buf_p++;
- red = *buf_p++;
- *pixbuf++ = red;
- *pixbuf++ = green;
- *pixbuf++ = blue;
- *pixbuf++ = 255;
- break;
- case 32:
- blue = *buf_p++;
- green = *buf_p++;
- red = *buf_p++;
- alphabyte = *buf_p++;
- //++timo debug: detect if the whole alpha channel is 0
- if (alphabyte != 0)
- bAlphaOK = true;
- *pixbuf++ = red;
- *pixbuf++ = green;
- *pixbuf++ = blue;
- *pixbuf++ = alphabyte;
- break;
- default:
- Sys_Printf("LoadTGA: illegal pixel_size '%d' in file '%s'\n", targa_header.pixel_size, name );
- break;
- }
- }
- }
- //++timo debug
- if (bAlphaOK)
- Sys_Printf("alpha channel OK");
- else
- Sys_Printf("empty alpha channel!");
- }
- else if (targa_header.image_type==10) { // Runlength encoded RGB images
- //++timo debug
- Sys_Printf("runlength encode RGB image");
- unsigned char red,green,blue,alphabyte,packetHeader,packetSize,j;
- red = 0;
- green = 0;
- blue = 0;
- alphabyte = 0xff;
- for(row=rows-1; row>=0; row--) {
- pixbuf = targa_rgba + row*columns*4;
- for(column=0; column<columns; ) {
- packetHeader= *buf_p++;
- packetSize = 1 + (packetHeader & 0x7f);
- if (packetHeader & 0x80) { // run-length packet
- switch (targa_header.pixel_size) {
- case 24:
- blue = *buf_p++;
- green = *buf_p++;
- red = *buf_p++;
- alphabyte = 255;
- break;
- case 32:
- blue = *buf_p++;
- green = *buf_p++;
- red = *buf_p++;
- alphabyte = *buf_p++;
- break;
- default:
- Sys_Printf("LoadTGA: illegal pixel_size '%d' in file '%s'\n", targa_header.pixel_size, name );
- break;
- }
-
- for(j=0;j<packetSize;j++) {
- *pixbuf++=red;
- *pixbuf++=green;
- *pixbuf++=blue;
- *pixbuf++=alphabyte;
- column++;
- if (column==columns) { // run spans across rows
- column=0;
- if (row>0)
- row--;
- else
- goto breakOut;
- pixbuf = targa_rgba + row*columns*4;
- }
- }
- }
- else { // non run-length packet
- for(j=0;j<packetSize;j++) {
- switch (targa_header.pixel_size) {
- case 24:
- blue = *buf_p++;
- green = *buf_p++;
- red = *buf_p++;
- *pixbuf++ = red;
- *pixbuf++ = green;
- *pixbuf++ = blue;
- *pixbuf++ = 255;
- break;
- case 32:
- blue = *buf_p++;
- green = *buf_p++;
- red = *buf_p++;
- alphabyte = *buf_p++;
- *pixbuf++ = red;
- *pixbuf++ = green;
- *pixbuf++ = blue;
- *pixbuf++ = alphabyte;
- break;
- default:
- Sys_Printf("LoadTGA: illegal pixel_size '%d' in file '%s'\n", targa_header.pixel_size, name );
- break;
- }
- column++;
- if (column==columns) { // pixel packet run spans across rows
- column=0;
- if (row>0)
- row--;
- else
- goto breakOut;
- pixbuf = targa_rgba + row*columns*4;
- }
- }
- }
- }
- breakOut:;
- }
- }
- free(buffer);
- }
- void LoadJPG( const char *filename, unsigned char **pic, int *width, int *height )
- {
- byte *fbuffer = NULL;
- int nLen = LoadFile( ( char * ) filename, (void **)&fbuffer);
- if (nLen == -1)
- {
- nLen = PakLoadAnyFile((char*)filename, (void**)&fbuffer);
- if (nLen == -1)
- {
- return;
- }
- }
- LoadJPGBuff(fbuffer, pic, width, height);
- free(fbuffer);
- }
- //===================================================================
- /*
- =================
- LoadImage
- Loads any of the supported image types into a cannonical
- 32 bit format.
- =================
- */
- void LoadImage( const char *name, byte **pic, int *width, int *height )
- {
- int len;
- *pic = NULL;
- *width = 0;
- *height = 0;
- len = strlen(name);
- if (len<5)
- {
- return;
- }
- if ( !stricmp( name+len-4, ".tga" ) )
- {
- LoadTGA( name, pic, width, height );
- }
- else if ( !stricmp(name+len-4, ".pcx") )
- {
- LoadPCX32( name, pic, width, height );
- }
- else if ( !stricmp( name+len-4, ".bmp" ) )
- {
- LoadBMP( name, pic, width, height );
- }
- else if ( !stricmp( name+len-4, ".jpg" ) )
- {
- LoadJPG( name, pic, width, height );
- }
- }
|