-/*\r
-Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
-For a list of contributors, see the accompanying CONTRIBUTORS file.\r
-\r
-This file is part of GtkRadiant.\r
-\r
-GtkRadiant is free software; you can redistribute it and/or modify\r
-it under the terms of the GNU General Public License as published by\r
-the Free Software Foundation; either version 2 of the License, or\r
-(at your option) any later version.\r
-\r
-GtkRadiant is distributed in the hope that it will be useful,\r
-but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-GNU General Public License for more details.\r
-\r
-You should have received a copy of the GNU General Public License\r
-along with GtkRadiant; if not, write to the Free Software\r
-Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\r
-*/\r
-// lbmlib.c\r
-\r
-#include "cmdlib.h"\r
-#include "inout.h"\r
-#include "lbmlib.h"\r
-\r
-\r
-\r
-/*\r
-============================================================================\r
-\r
- LBM STUFF\r
-\r
-============================================================================\r
-*/\r
-\r
-\r
-typedef unsigned char UBYTE;\r
-//conflicts with windows typedef short WORD;\r
-typedef unsigned short UWORD;\r
-typedef long LONG;\r
-\r
-typedef enum\r
-{\r
- ms_none,\r
- ms_mask,\r
- ms_transcolor,\r
- ms_lasso\r
-} mask_t;\r
-\r
-typedef enum\r
-{\r
- cm_none,\r
- cm_rle1\r
-} compress_t;\r
-\r
-typedef struct\r
-{\r
- UWORD w,h;\r
- short x,y;\r
- UBYTE nPlanes;\r
- UBYTE masking;\r
- UBYTE compression;\r
- UBYTE pad1;\r
- UWORD transparentColor;\r
- UBYTE xAspect,yAspect;\r
- short pageWidth,pageHeight;\r
-} bmhd_t;\r
-\r
-extern bmhd_t bmhd; // will be in native byte order\r
-\r
-\r
-\r
-#define FORMID ('F'+('O'<<8)+((int)'R'<<16)+((int)'M'<<24))\r
-#define ILBMID ('I'+('L'<<8)+((int)'B'<<16)+((int)'M'<<24))\r
-#define PBMID ('P'+('B'<<8)+((int)'M'<<16)+((int)' '<<24))\r
-#define BMHDID ('B'+('M'<<8)+((int)'H'<<16)+((int)'D'<<24))\r
-#define BODYID ('B'+('O'<<8)+((int)'D'<<16)+((int)'Y'<<24))\r
-#define CMAPID ('C'+('M'<<8)+((int)'A'<<16)+((int)'P'<<24))\r
-\r
-\r
-bmhd_t bmhd;\r
-\r
-int Align (int l)\r
-{\r
- if (l&1)\r
- return l+1;\r
- return l;\r
-}\r
-\r
-\r
-\r
-/*\r
-================\r
-LBMRLEdecompress\r
-\r
-Source must be evenly aligned!\r
-================\r
-*/\r
-byte *LBMRLEDecompress (byte *source,byte *unpacked, int bpwidth)\r
-{\r
- int count;\r
- byte b,rept;\r
-\r
- count = 0;\r
-\r
- do\r
- {\r
- rept = *source++;\r
-\r
- if (rept > 0x80)\r
- {\r
- rept = (rept^0xff)+2;\r
- b = *source++;\r
- memset(unpacked,b,rept);\r
- unpacked += rept;\r
- }\r
- else if (rept < 0x80)\r
- {\r
- rept++;\r
- memcpy(unpacked,source,rept);\r
- unpacked += rept;\r
- source += rept;\r
- }\r
- else\r
- rept = 0; // rept of 0x80 is NOP\r
-\r
- count += rept;\r
-\r
- } while (count<bpwidth);\r
-\r
- if (count>bpwidth)\r
- Error ("Decompression exceeded width!\n");\r
-\r
-\r
- return source;\r
-}\r
-\r
-\r
-/*\r
-=================\r
-LoadLBM\r
-=================\r
-*/\r
-void LoadLBM (char *filename, byte **picture, byte **palette)\r
-{\r
- byte *LBMbuffer, *picbuffer, *cmapbuffer;\r
- int y;\r
- byte *LBM_P, *LBMEND_P;\r
- byte *pic_p;\r
- byte *body_p;\r
-\r
- int formtype,formlength;\r
- int chunktype,chunklength;\r
-\r
-// qiet compiler warnings\r
- picbuffer = NULL;\r
- cmapbuffer = NULL;\r
-\r
-//\r
-// load the LBM\r
-//\r
- LoadFile (filename, (void **)&LBMbuffer);\r
-\r
-//\r
-// parse the LBM header\r
-//\r
- LBM_P = LBMbuffer;\r
- if ( *(int *)LBMbuffer != LittleLong(FORMID) )\r
- Error ("No FORM ID at start of file!\n");\r
-\r
- LBM_P += 4;\r
- formlength = BigLong( *(int *)LBM_P );\r
- LBM_P += 4;\r
- LBMEND_P = LBM_P + Align(formlength);\r
-\r
- formtype = LittleLong(*(int *)LBM_P);\r
-\r
- if (formtype != ILBMID && formtype != PBMID)\r
- Error ("Unrecognized form type: %c%c%c%c\n", formtype&0xff\r
- ,(formtype>>8)&0xff,(formtype>>16)&0xff,(formtype>>24)&0xff);\r
-\r
- LBM_P += 4;\r
-\r
-//\r
-// parse chunks\r
-//\r
-\r
- while (LBM_P < LBMEND_P)\r
- {\r
- chunktype = LBM_P[0] + (LBM_P[1]<<8) + (LBM_P[2]<<16) + (LBM_P[3]<<24);\r
- LBM_P += 4;\r
- chunklength = LBM_P[3] + (LBM_P[2]<<8) + (LBM_P[1]<<16) + (LBM_P[0]<<24);\r
- LBM_P += 4;\r
-\r
- switch ( chunktype )\r
- {\r
- case BMHDID:\r
- memcpy (&bmhd,LBM_P,sizeof(bmhd));\r
- bmhd.w = BigShort(bmhd.w);\r
- bmhd.h = BigShort(bmhd.h);\r
- bmhd.x = BigShort(bmhd.x);\r
- bmhd.y = BigShort(bmhd.y);\r
- bmhd.pageWidth = BigShort(bmhd.pageWidth);\r
- bmhd.pageHeight = BigShort(bmhd.pageHeight);\r
- break;\r
-\r
- case CMAPID:\r
- cmapbuffer = malloc (768);\r
- memset (cmapbuffer, 0, 768);\r
- memcpy (cmapbuffer, LBM_P, chunklength);\r
- break;\r
-\r
- case BODYID:\r
- body_p = LBM_P;\r
-\r
- pic_p = picbuffer = malloc (bmhd.w*bmhd.h);\r
- if (formtype == PBMID)\r
- {\r
- //\r
- // unpack PBM\r
- //\r
- for (y=0 ; y<bmhd.h ; y++, pic_p += bmhd.w)\r
- {\r
- if (bmhd.compression == cm_rle1)\r
- body_p = LBMRLEDecompress ((byte *)body_p\r
- , pic_p , bmhd.w);\r
- else if (bmhd.compression == cm_none)\r
- {\r
- memcpy (pic_p,body_p,bmhd.w);\r
- body_p += Align(bmhd.w);\r
- }\r
- }\r
-\r
- }\r
- else\r
- {\r
- //\r
- // unpack ILBM\r
- //\r
- Error ("%s is an interlaced LBM, not packed", filename);\r
- }\r
- break;\r
- }\r
-\r
- LBM_P += Align(chunklength);\r
- }\r
-\r
- free (LBMbuffer);\r
-\r
- *picture = picbuffer;\r
-\r
- if (palette)\r
- *palette = cmapbuffer;\r
-}\r
-\r
-\r
-/*\r
-============================================================================\r
-\r
- WRITE LBM\r
-\r
-============================================================================\r
-*/\r
-\r
-/*\r
-==============\r
-WriteLBMfile\r
-==============\r
-*/\r
-void WriteLBMfile (char *filename, byte *data,\r
- int width, int height, byte *palette)\r
-{\r
- byte *lbm, *lbmptr;\r
- int *formlength, *bmhdlength, *cmaplength, *bodylength;\r
- int length;\r
- bmhd_t basebmhd;\r
-\r
- lbm = lbmptr = malloc (width*height+1000);\r
-\r
-//\r
-// start FORM\r
-//\r
- *lbmptr++ = 'F';\r
- *lbmptr++ = 'O';\r
- *lbmptr++ = 'R';\r
- *lbmptr++ = 'M';\r
-\r
- formlength = (int*)lbmptr;\r
- lbmptr+=4; // leave space for length\r
-\r
- *lbmptr++ = 'P';\r
- *lbmptr++ = 'B';\r
- *lbmptr++ = 'M';\r
- *lbmptr++ = ' ';\r
-\r
-//\r
-// write BMHD\r
-//\r
- *lbmptr++ = 'B';\r
- *lbmptr++ = 'M';\r
- *lbmptr++ = 'H';\r
- *lbmptr++ = 'D';\r
-\r
- bmhdlength = (int *)lbmptr;\r
- lbmptr+=4; // leave space for length\r
-\r
- memset (&basebmhd,0,sizeof(basebmhd));\r
- basebmhd.w = BigShort((short)width);\r
- basebmhd.h = BigShort((short)height);\r
- basebmhd.nPlanes = BigShort(8);\r
- basebmhd.xAspect = BigShort(5);\r
- basebmhd.yAspect = BigShort(6);\r
- basebmhd.pageWidth = BigShort((short)width);\r
- basebmhd.pageHeight = BigShort((short)height);\r
-\r
- memcpy (lbmptr,&basebmhd,sizeof(basebmhd));\r
- lbmptr += sizeof(basebmhd);\r
-\r
- length = lbmptr-(byte *)bmhdlength-4;\r
- *bmhdlength = BigLong(length);\r
- if (length&1)\r
- *lbmptr++ = 0; // pad chunk to even offset\r
-\r
-//\r
-// write CMAP\r
-//\r
- *lbmptr++ = 'C';\r
- *lbmptr++ = 'M';\r
- *lbmptr++ = 'A';\r
- *lbmptr++ = 'P';\r
-\r
- cmaplength = (int *)lbmptr;\r
- lbmptr+=4; // leave space for length\r
-\r
- memcpy (lbmptr,palette,768);\r
- lbmptr += 768;\r
-\r
- length = lbmptr-(byte *)cmaplength-4;\r
- *cmaplength = BigLong(length);\r
- if (length&1)\r
- *lbmptr++ = 0; // pad chunk to even offset\r
-\r
-//\r
-// write BODY\r
-//\r
- *lbmptr++ = 'B';\r
- *lbmptr++ = 'O';\r
- *lbmptr++ = 'D';\r
- *lbmptr++ = 'Y';\r
-\r
- bodylength = (int *)lbmptr;\r
- lbmptr+=4; // leave space for length\r
-\r
- memcpy (lbmptr,data,width*height);\r
- lbmptr += width*height;\r
-\r
- length = lbmptr-(byte *)bodylength-4;\r
- *bodylength = BigLong(length);\r
- if (length&1)\r
- *lbmptr++ = 0; // pad chunk to even offset\r
-\r
-//\r
-// done\r
-//\r
- length = lbmptr-(byte *)formlength-4;\r
- *formlength = BigLong(length);\r
- if (length&1)\r
- *lbmptr++ = 0; // pad chunk to even offset\r
-\r
-//\r
-// write output file\r
-//\r
- SaveFile (filename, lbm, lbmptr-lbm);\r
- free (lbm);\r
-}\r
-\r
-\r
-/*\r
-============================================================================\r
-\r
-LOAD PCX\r
-\r
-============================================================================\r
-*/\r
-\r
-typedef struct\r
-{\r
- char manufacturer;\r
- char version;\r
- char encoding;\r
- char bits_per_pixel;\r
- unsigned short xmin,ymin,xmax,ymax;\r
- unsigned short hres,vres;\r
- unsigned char palette[48];\r
- char reserved;\r
- char color_planes;\r
- unsigned short bytes_per_line;\r
- unsigned short palette_type;\r
- char filler[58];\r
- unsigned char data; // unbounded\r
-} pcx_t;\r
-\r
-\r
-/*\r
-==============\r
-LoadPCX\r
-==============\r
-*/\r
-void LoadPCX (char *filename, byte **pic, byte **palette, int *width, int *height)\r
-{\r
- byte *raw;\r
- pcx_t *pcx;\r
- int x, y;\r
- int len;\r
- int dataByte, runLength;\r
- byte *out, *pix;\r
-\r
- //\r
- // load the file\r
- //\r
- len = LoadFile (filename, (void **)&raw);\r
-\r
- //\r
- // parse the PCX file\r
- //\r
- pcx = (pcx_t *)raw;\r
- raw = &pcx->data;\r
-\r
- pcx->xmin = LittleShort(pcx->xmin);\r
- pcx->ymin = LittleShort(pcx->ymin);\r
- pcx->xmax = LittleShort(pcx->xmax);\r
- pcx->ymax = LittleShort(pcx->ymax);\r
- pcx->hres = LittleShort(pcx->hres);\r
- pcx->vres = LittleShort(pcx->vres);\r
- pcx->bytes_per_line = LittleShort(pcx->bytes_per_line);\r
- pcx->palette_type = LittleShort(pcx->palette_type);\r
-\r
- if (pcx->manufacturer != 0x0a\r
- || pcx->version != 5\r
- || pcx->encoding != 1\r
- || pcx->bits_per_pixel != 8\r
- || pcx->xmax >= 640\r
- || pcx->ymax >= 480)\r
- Error ("Bad pcx file %s", filename);\r
- \r
- if (palette)\r
- {\r
- *palette = malloc(768);\r
- memcpy (*palette, (byte *)pcx + len - 768, 768);\r
- }\r
-\r
- if (width)\r
- *width = pcx->xmax+1;\r
- if (height)\r
- *height = pcx->ymax+1;\r
-\r
- if (!pic)\r
- return;\r
-\r
- out = malloc ( (pcx->ymax+1) * (pcx->xmax+1) );\r
- if (!out)\r
- Error ("Skin_Cache: couldn't allocate");\r
-\r
- *pic = out;\r
-\r
- pix = out;\r
-\r
- for (y=0 ; y<=pcx->ymax ; y++, pix += pcx->xmax+1)\r
- {\r
- for (x=0 ; x<=pcx->xmax ; )\r
- {\r
- dataByte = *raw++;\r
-\r
- if((dataByte & 0xC0) == 0xC0)\r
- {\r
- runLength = dataByte & 0x3F;\r
- dataByte = *raw++;\r
- }\r
- else\r
- runLength = 1;\r
-\r
- while(runLength-- > 0)\r
- pix[x++] = dataByte;\r
- }\r
-\r
- }\r
-\r
- if ( raw - (byte *)pcx > len)\r
- Error ("PCX file %s was malformed", filename);\r
-\r
- free (pcx);\r
-}\r
-\r
-/* \r
-============== \r
-WritePCXfile \r
-============== \r
-*/ \r
-void WritePCXfile (char *filename, byte *data, \r
- int width, int height, byte *palette) \r
-{\r
- int i, j, length;\r
- pcx_t *pcx;\r
- byte *pack;\r
- \r
- pcx = malloc (width*height*2+1000);\r
- memset (pcx, 0, sizeof(*pcx));\r
-\r
- pcx->manufacturer = 0x0a; // PCX id\r
- pcx->version = 5; // 256 color\r
- pcx->encoding = 1; // uncompressed\r
- pcx->bits_per_pixel = 8; // 256 color\r
- pcx->xmin = 0;\r
- pcx->ymin = 0;\r
- pcx->xmax = LittleShort((short)(width-1));\r
- pcx->ymax = LittleShort((short)(height-1));\r
- pcx->hres = LittleShort((short)width);\r
- pcx->vres = LittleShort((short)height);\r
- pcx->color_planes = 1; // chunky image\r
- pcx->bytes_per_line = LittleShort((short)width);\r
- pcx->palette_type = LittleShort(2); // not a grey scale\r
-\r
- // pack the image\r
- pack = &pcx->data;\r
- \r
- for (i=0 ; i<height ; i++)\r
- {\r
- for (j=0 ; j<width ; j++)\r
- {\r
- if ( (*data & 0xc0) != 0xc0)\r
- *pack++ = *data++;\r
- else\r
- {\r
- *pack++ = 0xc1;\r
- *pack++ = *data++;\r
- }\r
- }\r
- }\r
- \r
- // write the palette\r
- *pack++ = 0x0c; // palette ID byte\r
- for (i=0 ; i<768 ; i++)\r
- *pack++ = *palette++;\r
- \r
-// write output file \r
- length = pack - (byte *)pcx;\r
- SaveFile (filename, pcx, length);\r
-\r
- free (pcx);\r
-} \r
- \r
-\r
-/*\r
-============================================================================\r
-\r
-LOAD IMAGE\r
-\r
-============================================================================\r
-*/\r
-\r
-/*\r
-==============\r
-Load256Image\r
-\r
-Will load either an lbm or pcx, depending on extension.\r
-Any of the return pointers can be NULL if you don't want them.\r
-==============\r
-*/\r
-void Load256Image (char *name, byte **pixels, byte **palette,\r
- int *width, int *height)\r
-{\r
- char ext[128];\r
-\r
- ExtractFileExtension (name, ext);\r
- if (!Q_strncasecmp (ext, "lbm", strlen(ext) ))\r
- {\r
- LoadLBM (name, pixels, palette);\r
- if (width)\r
- *width = bmhd.w;\r
- if (height)\r
- *height = bmhd.h;\r
- }\r
- else if (!Q_strncasecmp (ext, "pcx",strlen(ext)))\r
- {\r
- LoadPCX (name, pixels, palette, width, height);\r
- }\r
- else\r
- Error ("%s doesn't have a known image extension", name);\r
-}\r
-\r
-\r
-/*\r
-==============\r
-Save256Image\r
-\r
-Will save either an lbm or pcx, depending on extension.\r
-==============\r
-*/\r
-void Save256Image (char *name, byte *pixels, byte *palette,\r
- int width, int height)\r
-{\r
- char ext[128];\r
-\r
- ExtractFileExtension (name, ext);\r
- if (!Q_strncasecmp (ext, "lbm",strlen(ext)))\r
- {\r
- WriteLBMfile (name, pixels, width, height, palette);\r
- }\r
- else if (!Q_strncasecmp (ext, "pcx",strlen(ext)))\r
- {\r
- WritePCXfile (name, pixels, width, height, palette);\r
- }\r
- else\r
- Error ("%s doesn't have a known image extension", name);\r
-}\r
-\r
-\r
-\r
-\r
-/*\r
-============================================================================\r
-\r
-TARGA IMAGE\r
-\r
-============================================================================\r
-*/\r
-\r
-typedef struct _TargaHeader {\r
- unsigned char id_length, colormap_type, image_type;\r
- unsigned short colormap_index, colormap_length;\r
- unsigned char colormap_size;\r
- unsigned short x_origin, y_origin, width, height;\r
- unsigned char pixel_size, attributes;\r
-} TargaHeader;\r
-\r
-int fgetLittleShort (FILE *f)\r
-{\r
- byte b1, b2;\r
-\r
- b1 = fgetc(f);\r
- b2 = fgetc(f);\r
-\r
- return (short)(b1 + b2*256);\r
-}\r
-\r
-int fgetLittleLong (FILE *f)\r
-{\r
- byte b1, b2, b3, b4;\r
-\r
- b1 = fgetc(f);\r
- b2 = fgetc(f);\r
- b3 = fgetc(f);\r
- b4 = fgetc(f);\r
-\r
- return b1 + (b2<<8) + (b3<<16) + (b4<<24);\r
-}\r
-\r
-\r
-/*\r
-=============\r
-LoadTGA\r
-=============\r
-*/\r
-void LoadTGA (char *name, byte **pixels, int *width, int *height)\r
-{\r
- int columns, rows, numPixels;\r
- byte *pixbuf;\r
- int row, column;\r
- FILE *fin;\r
- byte *targa_rgba;\r
- TargaHeader targa_header;\r
-\r
- fin = fopen (name, "rb");\r
- if (!fin)\r
- Error ("Couldn't read %s", name);\r
-\r
- targa_header.id_length = fgetc(fin);\r
- targa_header.colormap_type = fgetc(fin);\r
- targa_header.image_type = fgetc(fin);\r
- \r
- targa_header.colormap_index = fgetLittleShort(fin);\r
- targa_header.colormap_length = fgetLittleShort(fin);\r
- targa_header.colormap_size = fgetc(fin);\r
- targa_header.x_origin = fgetLittleShort(fin);\r
- targa_header.y_origin = fgetLittleShort(fin);\r
- targa_header.width = fgetLittleShort(fin);\r
- targa_header.height = fgetLittleShort(fin);\r
- targa_header.pixel_size = fgetc(fin);\r
- targa_header.attributes = fgetc(fin);\r
-\r
- if (targa_header.image_type!=2 \r
- && targa_header.image_type!=10) \r
- Error ("LoadTGA: Only type 2 and 10 targa RGB images supported\n");\r
-\r
- if (targa_header.colormap_type !=0 \r
- || (targa_header.pixel_size!=32 && targa_header.pixel_size!=24))\r
- Error ("Texture_LoadTGA: Only 32 or 24 bit images supported (no colormaps)\n");\r
-\r
- columns = targa_header.width;\r
- rows = targa_header.height;\r
- numPixels = columns * rows;\r
-\r
- if (width)\r
- *width = columns;\r
- if (height)\r
- *height = rows;\r
- targa_rgba = malloc(numPixels*4);\r
- *pixels = targa_rgba;\r
-\r
- if (targa_header.id_length != 0)\r
- fseek(fin, targa_header.id_length, SEEK_CUR); // skip TARGA image comment\r
- \r
- if (targa_header.image_type==2) { // Uncompressed, RGB images\r
- for(row=rows-1; row>=0; row--) {\r
- pixbuf = targa_rgba + row*columns*4;\r
- for(column=0; column<columns; column++) {\r
- unsigned char red,green,blue,alphabyte;\r
- switch (targa_header.pixel_size) {\r
- case 24:\r
- \r
- blue = getc(fin);\r
- green = getc(fin);\r
- red = getc(fin);\r
- *pixbuf++ = red;\r
- *pixbuf++ = green;\r
- *pixbuf++ = blue;\r
- *pixbuf++ = 255;\r
- break;\r
- case 32:\r
- blue = getc(fin);\r
- green = getc(fin);\r
- red = getc(fin);\r
- alphabyte = getc(fin);\r
- *pixbuf++ = red;\r
- *pixbuf++ = green;\r
- *pixbuf++ = blue;\r
- *pixbuf++ = alphabyte;\r
- break;\r
- }\r
- }\r
- }\r
- }\r
- else if (targa_header.image_type==10) { // Runlength encoded RGB images\r
- unsigned char red,green,blue,alphabyte,packetHeader,packetSize,j;\r
- for(row=rows-1; row>=0; row--) {\r
- pixbuf = targa_rgba + row*columns*4;\r
- for(column=0; column<columns; ) {\r
- packetHeader=getc(fin);\r
- packetSize = 1 + (packetHeader & 0x7f);\r
- if (packetHeader & 0x80) { // run-length packet\r
- switch (targa_header.pixel_size) {\r
- case 24:\r
- blue = getc(fin);\r
- green = getc(fin);\r
- red = getc(fin);\r
- alphabyte = 255;\r
- break;\r
- case 32:\r
- blue = getc(fin);\r
- green = getc(fin);\r
- red = getc(fin);\r
- alphabyte = getc(fin);\r
- break;\r
- }\r
- \r
- for(j=0;j<packetSize;j++) {\r
- *pixbuf++=red;\r
- *pixbuf++=green;\r
- *pixbuf++=blue;\r
- *pixbuf++=alphabyte;\r
- column++;\r
- if (column==columns) { // run spans across rows\r
- column=0;\r
- if (row>0)\r
- row--;\r
- else\r
- goto breakOut;\r
- pixbuf = targa_rgba + row*columns*4;\r
- }\r
- }\r
- }\r
- else { // non run-length packet\r
- for(j=0;j<packetSize;j++) {\r
- switch (targa_header.pixel_size) {\r
- case 24:\r
- blue = getc(fin);\r
- green = getc(fin);\r
- red = getc(fin);\r
- *pixbuf++ = red;\r
- *pixbuf++ = green;\r
- *pixbuf++ = blue;\r
- *pixbuf++ = 255;\r
- break;\r
- case 32:\r
- blue = getc(fin);\r
- green = getc(fin);\r
- red = getc(fin);\r
- alphabyte = getc(fin);\r
- *pixbuf++ = red;\r
- *pixbuf++ = green;\r
- *pixbuf++ = blue;\r
- *pixbuf++ = alphabyte;\r
- break;\r
- }\r
- column++;\r
- if (column==columns) { // pixel packet run spans across rows\r
- column=0;\r
- if (row>0)\r
- row--;\r
- else\r
- goto breakOut;\r
- pixbuf = targa_rgba + row*columns*4;\r
- } \r
- }\r
- }\r
- }\r
- breakOut:;\r
- }\r
- }\r
-\r
- // vertically flipped\r
- if ( (targa_header.attributes & (1<<5)) ) {\r
- int flip;\r
- for (row = 0; row < .5f * rows; row++)\r
- {\r
- for (column = 0; column < columns; column++)\r
- {\r
- flip = *( (int*)targa_rgba + row * columns + column);\r
- *( (int*)targa_rgba + row * columns + column) = *( (int*)targa_rgba + ( ( rows - 1 ) - row ) * columns + column );\r
- *( (int*)targa_rgba + ( ( rows - 1 ) - row ) * columns + column ) = flip;\r
- }\r
- }\r
- }\r
- \r
- fclose(fin);\r
-}\r
+/*
+ Copyright (C) 1999-2007 id Software, Inc. and contributors.
+ For a list of contributors, see the accompanying CONTRIBUTORS file.
+
+ This file is part of GtkRadiant.
+
+ GtkRadiant 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.
+
+ GtkRadiant 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 GtkRadiant; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+// lbmlib.c
+
+#include "cmdlib.h"
+#include "inout.h"
+#include "lbmlib.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 );
+
+//
+// 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 = malloc( 768 );
+ memset( cmapbuffer, 0, 768 );
+ memcpy( cmapbuffer, LBM_P, chunklength );
+ break;
+
+ case BODYID:
+ body_p = LBM_P;
+
+ pic_p = picbuffer = 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 = 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 = BigShort( 8 );
+ basebmhd.xAspect = BigShort( 5 );
+ basebmhd.yAspect = BigShort( 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;
+ pcx_t *pcx;
+ int x, y;
+ int len;
+ int dataByte, runLength;
+ byte *out, *pix;
+
+ //
+ // load the file
+ //
+ len = LoadFile( filename, (void **)&raw );
+
+ //
+ // 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 = malloc( 768 );
+ memcpy( *palette, (byte *)pcx + len - 768, 768 );
+ }
+
+ if ( width ) {
+ *width = pcx->xmax + 1;
+ }
+ if ( height ) {
+ *height = pcx->ymax + 1;
+ }
+
+ if ( !pic ) {
+ return;
+ }
+
+ out = 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 = 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];
+
+ ExtractFileExtension( name, ext );
+ if ( !Q_strncasecmp( ext, "lbm", strlen( ext ) ) ) {
+ LoadLBM( name, pixels, palette );
+ if ( width ) {
+ *width = bmhd.w;
+ }
+ if ( height ) {
+ *height = bmhd.h;
+ }
+ }
+ else if ( !Q_strncasecmp( ext, "pcx",strlen( ext ) ) ) {
+ LoadPCX( name, pixels, palette, width, height );
+ }
+ 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 ( !Q_strncasecmp( ext, "lbm",strlen( ext ) ) ) {
+ WriteLBMfile( name, pixels, width, height, palette );
+ }
+ else if ( !Q_strncasecmp( ext, "pcx",strlen( ext ) ) ) {
+ 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 fgetLittleLong( FILE *f ){
+ byte b1, b2, b3, b4;
+
+ b1 = fgetc( f );
+ b2 = fgetc( f );
+ b3 = fgetc( f );
+ b4 = fgetc( f );
+
+ return b1 + ( b2 << 8 ) + ( b3 << 16 ) + ( b4 << 24 );
+}
+
+
+/*
+ =============
+ LoadTGA
+ =============
+ */
+void LoadTGA( char *name, byte **pixels, int *width, int *height ){
+ int columns, rows, numPixels;
+ byte *pixbuf;
+ int row, column;
+ FILE *fin;
+ byte *targa_rgba;
+ TargaHeader targa_header;
+
+ fin = fopen( name, "rb" );
+ if ( !fin ) {
+ Error( "Couldn't read %s", name );
+ }
+
+ targa_header.id_length = fgetc( fin );
+ targa_header.colormap_type = fgetc( fin );
+ targa_header.image_type = fgetc( fin );
+
+ targa_header.colormap_index = fgetLittleShort( fin );
+ targa_header.colormap_length = fgetLittleShort( fin );
+ targa_header.colormap_size = fgetc( fin );
+ targa_header.x_origin = fgetLittleShort( fin );
+ targa_header.y_origin = fgetLittleShort( fin );
+ targa_header.width = fgetLittleShort( fin );
+ targa_header.height = fgetLittleShort( fin );
+ targa_header.pixel_size = fgetc( fin );
+ targa_header.attributes = fgetc( fin );
+
+ if ( targa_header.image_type != 2
+ && targa_header.image_type != 10 ) {
+ Error( "LoadTGA: Only type 2 and 10 targa RGB images supported\n" );
+ }
+
+ if ( targa_header.colormap_type != 0
+ || ( targa_header.pixel_size != 32 && targa_header.pixel_size != 24 ) ) {
+ Error( "Texture_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 = malloc( numPixels * 4 );
+ *pixels = targa_rgba;
+
+ if ( targa_header.id_length != 0 ) {
+ fseek( fin, targa_header.id_length, SEEK_CUR ); // skip TARGA image comment
+
+ }
+ if ( targa_header.image_type == 2 ) { // Uncompressed, RGB images
+ 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 24:
+
+ blue = getc( fin );
+ green = getc( fin );
+ red = getc( fin );
+ *pixbuf++ = red;
+ *pixbuf++ = green;
+ *pixbuf++ = blue;
+ *pixbuf++ = 255;
+ break;
+ case 32:
+ blue = getc( fin );
+ green = getc( fin );
+ red = getc( fin );
+ alphabyte = getc( fin );
+ *pixbuf++ = red;
+ *pixbuf++ = green;
+ *pixbuf++ = blue;
+ *pixbuf++ = alphabyte;
+ break;
+ }
+ }
+ }
+ }
+ else if ( targa_header.image_type == 10 ) { // Runlength encoded RGB images
+ unsigned char red,green,blue,alphabyte,packetHeader,packetSize,j;
+ for ( row = rows - 1; row >= 0; row-- ) {
+ pixbuf = targa_rgba + row * columns * 4;
+ for ( column = 0; column < columns; ) {
+ packetHeader = getc( fin );
+ packetSize = 1 + ( packetHeader & 0x7f );
+ if ( packetHeader & 0x80 ) { // run-length packet
+ switch ( targa_header.pixel_size ) {
+ case 24:
+ blue = getc( fin );
+ green = getc( fin );
+ red = getc( fin );
+ alphabyte = 255;
+ break;
+ case 32:
+ blue = getc( fin );
+ green = getc( fin );
+ red = getc( fin );
+ alphabyte = getc( fin );
+ 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 = getc( fin );
+ green = getc( fin );
+ red = getc( fin );
+ *pixbuf++ = red;
+ *pixbuf++ = green;
+ *pixbuf++ = blue;
+ *pixbuf++ = 255;
+ break;
+ case 32:
+ blue = getc( fin );
+ green = getc( fin );
+ red = getc( fin );
+ alphabyte = getc( fin );
+ *pixbuf++ = red;
+ *pixbuf++ = green;
+ *pixbuf++ = blue;
+ *pixbuf++ = alphabyte;
+ 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:;
+ }
+ }
+
+ // vertically flipped
+ if ( ( targa_header.attributes & ( 1 << 5 ) ) ) {
+ int flip;
+ for ( row = 0; row < .5f * rows; row++ )
+ {
+ for ( column = 0; column < columns; column++ )
+ {
+ flip = *( (int*)targa_rgba + row * columns + column );
+ *( (int*)targa_rgba + row * columns + column ) = *( (int*)targa_rgba + ( ( rows - 1 ) - row ) * columns + column );
+ *( (int*)targa_rgba + ( ( rows - 1 ) - row ) * columns + column ) = flip;
+ }
+ }
+ }
+
+ fclose( fin );
+}