+}
+TargaHeader;
+
+void PrintTargaHeader(TargaHeader *t)
+{
+ Con_Printf("TargaHeader:\nuint8 id_length = %i;\nuint8 colormap_type = %i;\nuint8 image_type = %i;\nuint16 colormap_index = %i;\nuint16 colormap_length = %i;\nuint8 colormap_size = %i;\nuint16 x_origin = %i;\nuint16 y_origin = %i;\nuint16 width = %i;\nuint16 height = %i;\nuint8 pixel_size = %i;\nuint8 attributes = %i;\n", t->id_length, t->colormap_type, t->image_type, t->colormap_index, t->colormap_length, t->colormap_size, t->x_origin, t->y_origin, t->width, t->height, t->pixel_size, t->attributes);
+}
+
+/*
+=============
+LoadTGA
+=============
+*/
+unsigned char *LoadTGA (const unsigned char *f, int filesize, int matchwidth, int matchheight)
+{
+ int x, y, row_inc, compressed, readpixelcount, red, green, blue, alpha, runlen, pindex, alphabits;
+ unsigned char *pixbuf, *image_rgba;
+ const unsigned char *fin, *enddata;
+ unsigned char *p;
+ TargaHeader targa_header;
+ unsigned char palette[256*4];
+
+ if (filesize < 19)
+ return NULL;
+
+ enddata = f + filesize;
+
+ targa_header.id_length = f[0];
+ targa_header.colormap_type = f[1];
+ targa_header.image_type = f[2];
+
+ targa_header.colormap_index = f[3] + f[4] * 256;
+ targa_header.colormap_length = f[5] + f[6] * 256;
+ targa_header.colormap_size = f[7];
+ targa_header.x_origin = f[8] + f[9] * 256;
+ targa_header.y_origin = f[10] + f[11] * 256;
+ targa_header.width = image_width = f[12] + f[13] * 256;
+ targa_header.height = image_height = f[14] + f[15] * 256;
+ if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
+ {
+ Con_Print("LoadTGA: invalid size\n");
+ PrintTargaHeader(&targa_header);
+ return NULL;
+ }
+ if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
+ return NULL;
+ targa_header.pixel_size = f[16];
+ targa_header.attributes = f[17];
+
+ // advance to end of header
+ fin = f + 18;
+
+ // skip TARGA image comment (usually 0 bytes)
+ fin += targa_header.id_length;
+
+ // read/skip the colormap if present (note: according to the TARGA spec it
+ // can be present even on truecolor or greyscale images, just not used by
+ // the image data)
+ if (targa_header.colormap_type)
+ {
+ if (targa_header.colormap_length > 256)
+ {
+ Con_Print("LoadTGA: only up to 256 colormap_length supported\n");
+ PrintTargaHeader(&targa_header);
+ return NULL;
+ }
+ if (targa_header.colormap_index)
+ {
+ Con_Print("LoadTGA: colormap_index not supported\n");
+ PrintTargaHeader(&targa_header);
+ return NULL;
+ }
+ if (targa_header.colormap_size == 24)
+ {
+ for (x = 0;x < targa_header.colormap_length;x++)
+ {
+ palette[x*4+2] = *fin++;
+ palette[x*4+1] = *fin++;
+ palette[x*4+0] = *fin++;
+ palette[x*4+3] = 255;
+ }
+ }
+ else if (targa_header.colormap_size == 32)
+ {
+ for (x = 0;x < targa_header.colormap_length;x++)
+ {
+ palette[x*4+2] = *fin++;
+ palette[x*4+1] = *fin++;
+ palette[x*4+0] = *fin++;
+ palette[x*4+3] = *fin++;
+ }
+ }
+ else
+ {
+ Con_Print("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
+ PrintTargaHeader(&targa_header);
+ return NULL;
+ }
+ }
+
+ // check our pixel_size restrictions according to image_type
+ if (targa_header.image_type == 2 || targa_header.image_type == 10)
+ {
+ if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
+ {
+ Con_Print("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
+ PrintTargaHeader(&targa_header);
+ return NULL;
+ }
+ }
+ else if (targa_header.image_type == 1 || targa_header.image_type == 9)
+ {
+ if (targa_header.pixel_size != 8)
+ {
+ Con_Print("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
+ PrintTargaHeader(&targa_header);
+ return NULL;
+ }
+ }
+ else if (targa_header.image_type == 3 || targa_header.image_type == 11)
+ {
+ if (targa_header.pixel_size != 8)
+ {
+ Con_Print("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
+ PrintTargaHeader(&targa_header);
+ return NULL;
+ }
+ }
+ else
+ {
+ Con_Printf("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
+ PrintTargaHeader(&targa_header);
+ return NULL;
+ }
+
+ if (targa_header.attributes & 0x10)
+ {
+ Con_Print("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
+ return NULL;
+ }
+
+ // number of attribute bits per pixel, we only support 0 or 8
+ alphabits = targa_header.attributes & 0x0F;
+ if (alphabits != 8 && alphabits != 0)
+ {
+ Con_Print("LoadTGA: only 0 or 8 attribute (alpha) bits supported\n");
+ return NULL;
+ }
+
+ image_rgba = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
+ if (!image_rgba)
+ {
+ Con_Printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
+ return NULL;
+ }
+
+ // If bit 5 of attributes isn't set, the image has been stored from bottom to top
+ if ((targa_header.attributes & 0x20) == 0)
+ {
+ pixbuf = image_rgba + (image_height - 1)*image_width*4;
+ row_inc = -image_width*4*2;
+ }
+ else
+ {
+ pixbuf = image_rgba;
+ row_inc = 0;
+ }
+
+ compressed = targa_header.image_type == 9 || targa_header.image_type == 10 || targa_header.image_type == 11;
+ x = 0;
+ y = 0;
+ red = green = blue = alpha = 255;
+ while (y < image_height)
+ {
+ // decoder is mostly the same whether it's compressed or not
+ readpixelcount = 1000000;
+ runlen = 1000000;
+ if (compressed && fin < enddata)
+ {
+ runlen = *fin++;
+ // high bit indicates this is an RLE compressed run
+ if (runlen & 0x80)
+ readpixelcount = 1;
+ runlen = 1 + (runlen & 0x7f);
+ }
+
+ while((runlen--) && y < image_height)
+ {
+ if (readpixelcount > 0)
+ {
+ readpixelcount--;
+ red = green = blue = alpha = 255;
+ if (fin < enddata)
+ {
+ switch(targa_header.image_type)
+ {
+ case 1:
+ case 9:
+ // colormapped
+ pindex = *fin++;
+ if (pindex >= targa_header.colormap_length)
+ pindex = 0; // error
+ p = palette + pindex * 4;
+ red = p[0];
+ green = p[1];
+ blue = p[2];
+ alpha = p[3];
+ break;
+ case 2:
+ case 10:
+ // BGR or BGRA
+ blue = *fin++;
+ if (fin < enddata)
+ green = *fin++;
+ if (fin < enddata)
+ red = *fin++;
+ if (targa_header.pixel_size == 32 && fin < enddata)
+ alpha = *fin++;
+ break;
+ case 3:
+ case 11:
+ // greyscale
+ red = green = blue = *fin++;
+ break;
+ }
+ if (!alphabits)
+ alpha = 255;
+ }
+ }
+ *pixbuf++ = red;
+ *pixbuf++ = green;
+ *pixbuf++ = blue;
+ *pixbuf++ = alpha;
+ x++;
+ if (x == image_width)
+ {
+ // end of line, advance to next
+ x = 0;
+ y++;
+ pixbuf += row_inc;
+ }
+ }
+ }
+
+ return image_rgba;
+}
+
+/*
+============
+LoadLMP
+============
+*/
+unsigned char *LoadLMP (const unsigned char *f, int filesize, int matchwidth, int matchheight, qboolean loadAs8Bit)
+{
+ unsigned char *image_buffer;
+
+ if (filesize < 9)
+ {
+ Con_Print("LoadLMP: invalid LMP file\n");
+ return NULL;
+ }
+
+ // parse the very complicated header *chuckle*
+ image_width = BuffLittleLong(f);
+ image_height = BuffLittleLong(f + 4);
+ if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
+ {
+ Con_Printf("LoadLMP: invalid size %ix%i\n", image_width, image_height);
+ return NULL;
+ }
+ if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
+ return NULL;
+
+ if (filesize < (8 + image_width * image_height))
+ {
+ Con_Print("LoadLMP: invalid LMP file\n");
+ return NULL;
+ }
+
+ if (loadAs8Bit)
+ {
+ image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height);
+ memcpy(image_buffer, f + 8, image_width * image_height);
+ }
+ else
+ {
+ image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
+ Image_Copy8bitRGBA(f + 8, image_buffer, image_width * image_height, palette_transparent);
+ }
+ return image_buffer;
+}
+
+static unsigned char *LoadLMPRGBA (const unsigned char *f, int filesize, int matchwidth, int matchheight)
+{
+ return LoadLMP(f, filesize, matchwidth, matchheight, false);
+}
+
+
+typedef struct q2wal_s
+{
+ char name[32];
+ unsigned width, height;
+ unsigned offsets[MIPLEVELS]; // four mip maps stored
+ char animname[32]; // next frame in animation chain
+ int flags;
+ int contents;
+ int value;
+} q2wal_t;
+
+unsigned char *LoadWAL (const unsigned char *f, int filesize, int matchwidth, int matchheight)
+{
+ unsigned char *image_rgba;
+ const q2wal_t *inwal = (const q2wal_t *)f;
+
+ if (filesize < (int) sizeof(q2wal_t))
+ {
+ Con_Print("LoadWAL: invalid WAL file\n");
+ return NULL;
+ }
+
+ image_width = LittleLong(inwal->width);
+ image_height = LittleLong(inwal->height);
+ if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
+ {
+ Con_Printf("LoadWAL: invalid size %ix%i\n", image_width, image_height);
+ return NULL;
+ }
+ if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
+ return NULL;
+
+ if (filesize < (int) sizeof(q2wal_t) + (int) LittleLong(inwal->offsets[0]) + image_width * image_height)
+ {
+ Con_Print("LoadWAL: invalid WAL file\n");
+ return NULL;
+ }
+
+ image_rgba = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
+ if (!image_rgba)
+ {
+ Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
+ return NULL;
+ }
+ Image_Copy8bitRGBA(f + LittleLong(inwal->offsets[0]), image_rgba, image_width * image_height, palette_complete);
+ return image_rgba;
+}
+
+
+void Image_StripImageExtension (const char *in, char *out)
+{
+ const char *end, *temp;
+ end = in + strlen(in);
+ if ((end - in) >= 4)
+ {
+ temp = end - 4;
+ if (strcmp(temp, ".tga") == 0
+ || strcmp(temp, ".pcx") == 0
+ || strcmp(temp, ".lmp") == 0
+ || strcmp(temp, ".png") == 0
+ || strcmp(temp, ".jpg") == 0)
+ end = temp;
+ while (in < end)
+ *out++ = *in++;
+ *out++ = 0;
+ }
+ else
+ strcpy(out, in);
+}
+
+struct imageformat_s
+{
+ const char *formatstring;
+ unsigned char *(*loadfunc)(const unsigned char *f, int filesize, int matchwidth, int matchheight);
+}
+imageformats[] =
+{
+ {"override/%s.tga", LoadTGA},
+ {"override/%s.png", PNG_LoadImage},
+ {"override/%s.jpg", JPEG_LoadImage},
+ {"textures/%s.tga", LoadTGA},
+ {"textures/%s.png", PNG_LoadImage},
+ {"textures/%s.jpg", JPEG_LoadImage},
+ {"textures/%s.pcx", LoadPCX},
+ {"textures/%s.wal", LoadWAL},
+ {"%s.tga", LoadTGA},
+ {"%s.png", PNG_LoadImage},
+ {"%s.jpg", JPEG_LoadImage},
+ {"%s.pcx", LoadPCX},
+ {"%s.lmp", LoadLMPRGBA},
+ {NULL, NULL}
+};
+
+unsigned char *loadimagepixels (const char *filename, qboolean complain, int matchwidth, int matchheight)
+{
+ int i;
+ fs_offset_t filesize;
+ unsigned char *f, *data = NULL;
+ char basename[MAX_QPATH], name[MAX_QPATH], *c;
+ if (developer_memorydebug.integer)
+ Mem_CheckSentinelsGlobal();
+ if (developer_texturelogging.integer)
+ Log_Printf("textures.log", "%s\n", filename);
+ strlcpy(basename, filename, sizeof(basename));
+ Image_StripImageExtension(basename, basename); // strip filename extensions to allow replacement by other types
+ // replace *'s with #, so commandline utils don't get confused when dealing with the external files
+ for (c = basename;*c;c++)
+ if (*c == '*')
+ *c = '#';
+ for (i = 0;imageformats[i].formatstring;i++)
+ {
+ sprintf (name, imageformats[i].formatstring, basename);
+ f = FS_LoadFile(name, tempmempool, true, &filesize);
+ if (f)
+ {
+ data = imageformats[i].loadfunc(f, filesize, matchwidth, matchheight);
+ Mem_Free(f);
+ if (data)
+ {
+ Con_DPrintf("loaded image %s (%dx%d)\n", name, image_width, image_height);
+ if (developer_memorydebug.integer)
+ Mem_CheckSentinelsGlobal();
+ return data;
+ }
+ }
+ }
+ if (complain)
+ {
+ Con_Printf("Couldn't load %s using ", filename);
+ for (i = 0;imageformats[i].formatstring;i++)
+ {
+ sprintf (name, imageformats[i].formatstring, basename);
+ Con_Printf(i == 0 ? "\"%s\"" : (imageformats[i+1].formatstring ? ", \"%s\"" : " or \"%s\".\n"), imageformats[i].formatstring);
+ }
+ }
+ if (developer_memorydebug.integer)
+ Mem_CheckSentinelsGlobal();
+ return NULL;
+}
+
+int image_makemask (const unsigned char *in, unsigned char *out, int size)
+{
+ int i, count;
+ count = 0;
+ for (i = 0;i < size;i++)
+ {
+ out[0] = out[1] = out[2] = 255;
+ out[3] = in[3];
+ if (in[3] != 255)
+ count++;
+ in += 4;
+ out += 4;
+ }
+ return count;
+}
+
+unsigned char* loadimagepixelsmask (const char *filename, qboolean complain, int matchwidth, int matchheight)
+{
+ unsigned char *in, *data;
+ in = data = loadimagepixels(filename, complain, matchwidth, matchheight);
+ if (!data)
+ return NULL;
+ if (image_makemask(data, data, image_width * image_height))
+ return data; // some transparency
+ else
+ {
+ Mem_Free(data);
+ return NULL; // all opaque
+ }
+}