11 static void Image_CopyAlphaFromBlueBGRA(unsigned char *outpixels, const unsigned char *inpixels, int w, int h)
15 for(i = 0; i < n; ++i)
16 outpixels[4*i+3] = inpixels[4*i]; // blue channel
20 // written by LordHavoc in a readable way, optimized by Vic, further optimized by LordHavoc (the non-special index case), readable version preserved below this
21 void Image_CopyMux(unsigned char *outpixels, const unsigned char *inpixels, int inputwidth, int inputheight, qboolean inputflipx, qboolean inputflipy, qboolean inputflipdiagonal, int numoutputcomponents, int numinputcomponents, int *outputinputcomponentindices)
24 const unsigned char *in, *line;
25 int row_inc = (inputflipy ? -inputwidth : inputwidth) * numinputcomponents, col_inc = (inputflipx ? -1 : 1) * numinputcomponents;
26 int row_ofs = (inputflipy ? (inputheight - 1) * inputwidth * numinputcomponents : 0), col_ofs = (inputflipx ? (inputwidth - 1) * numinputcomponents : 0);
28 for (c = 0; c < numoutputcomponents; c++)
29 if (outputinputcomponentindices[c] & 0x80000000)
31 if (c < numoutputcomponents)
33 // special indices used
34 if (inputflipdiagonal)
36 for (x = 0, line = inpixels + col_ofs; x < inputwidth; x++, line += col_inc)
37 for (y = 0, in = line + row_ofs; y < inputheight; y++, in += row_inc, outpixels += numoutputcomponents)
38 for (c = 0; c < numoutputcomponents; c++)
39 outpixels[c] = ((index = outputinputcomponentindices[c]) & 0x80000000) ? index : in[index];
43 for (y = 0, line = inpixels + row_ofs; y < inputheight; y++, line += row_inc)
44 for (x = 0, in = line + col_ofs; x < inputwidth; x++, in += col_inc, outpixels += numoutputcomponents)
45 for (c = 0; c < numoutputcomponents; c++)
46 outpixels[c] = ((index = outputinputcomponentindices[c]) & 0x80000000) ? index : in[index];
51 // special indices not used
52 if (inputflipdiagonal)
54 for (x = 0, line = inpixels + col_ofs; x < inputwidth; x++, line += col_inc)
55 for (y = 0, in = line + row_ofs; y < inputheight; y++, in += row_inc, outpixels += numoutputcomponents)
56 for (c = 0; c < numoutputcomponents; c++)
57 outpixels[c] = in[outputinputcomponentindices[c]];
61 for (y = 0, line = inpixels + row_ofs; y < inputheight; y++, line += row_inc)
62 for (x = 0, in = line + col_ofs; x < inputwidth; x++, in += col_inc, outpixels += numoutputcomponents)
63 for (c = 0; c < numoutputcomponents; c++)
64 outpixels[c] = in[outputinputcomponentindices[c]];
69 // intentionally readable version
70 void Image_CopyMux(unsigned char *outpixels, const unsigned char *inpixels, int inputwidth, int inputheight, qboolean inputflipx, qboolean inputflipy, qboolean inputflipdiagonal, int numoutputcomponents, int numinputcomponents, int *outputinputcomponentindices)
73 const unsigned char *in, *inrow, *incolumn;
74 if (inputflipdiagonal)
76 for (x = 0;x < inputwidth;x++)
78 for (y = 0;y < inputheight;y++)
80 in = inpixels + ((inputflipy ? inputheight - 1 - y : y) * inputwidth + (inputflipx ? inputwidth - 1 - x : x)) * numinputcomponents;
81 for (c = 0;c < numoutputcomponents;c++)
83 index = outputinputcomponentindices[c];
84 if (index & 0x80000000)
87 *outpixels++ = in[index];
94 for (y = 0;y < inputheight;y++)
96 for (x = 0;x < inputwidth;x++)
98 in = inpixels + ((inputflipy ? inputheight - 1 - y : y) * inputwidth + (inputflipx ? inputwidth - 1 - x : x)) * numinputcomponents;
99 for (c = 0;c < numoutputcomponents;c++)
101 index = outputinputcomponentindices[c];
102 if (index & 0x80000000)
103 *outpixels++ = index;
105 *outpixels++ = in[index];
113 void Image_GammaRemapRGB(const unsigned char *in, unsigned char *out, int pixels, const unsigned char *gammar, const unsigned char *gammag, const unsigned char *gammab)
117 out[0] = gammar[in[0]];
118 out[1] = gammag[in[1]];
119 out[2] = gammab[in[2]];
125 // note: pal must be 32bit color
126 void Image_Copy8bitBGRA(const unsigned char *in, unsigned char *out, int pixels, const unsigned int *pal)
128 int *iout = (int *)out;
131 iout[0] = pal[in[0]];
132 iout[1] = pal[in[1]];
133 iout[2] = pal[in[2]];
134 iout[3] = pal[in[3]];
135 iout[4] = pal[in[4]];
136 iout[5] = pal[in[5]];
137 iout[6] = pal[in[6]];
138 iout[7] = pal[in[7]];
145 iout[0] = pal[in[0]];
146 iout[1] = pal[in[1]];
147 iout[2] = pal[in[2]];
148 iout[3] = pal[in[3]];
154 iout[0] = pal[in[0]];
155 iout[1] = pal[in[1]];
160 iout[0] = pal[in[0]];
164 =================================================================
168 =================================================================
177 unsigned short xmin,ymin,xmax,ymax;
178 unsigned short hres,vres;
179 unsigned char palette[48];
182 unsigned short bytes_per_line;
183 unsigned short palette_type;
192 static unsigned char* LoadPCX_BGRA (const unsigned char *f, int filesize, int *miplevel)
195 unsigned char *a, *b, *image_buffer, *pbuf;
196 const unsigned char *palette, *fin, *enddata;
197 int x, y, x2, dataByte;
199 if (filesize < (int)sizeof(pcx) + 768)
201 Con_Print("Bad pcx file\n");
207 memcpy(&pcx, fin, sizeof(pcx));
210 // LordHavoc: big-endian support ported from QF newtree
211 pcx.xmax = LittleShort (pcx.xmax);
212 pcx.xmin = LittleShort (pcx.xmin);
213 pcx.ymax = LittleShort (pcx.ymax);
214 pcx.ymin = LittleShort (pcx.ymin);
215 pcx.hres = LittleShort (pcx.hres);
216 pcx.vres = LittleShort (pcx.vres);
217 pcx.bytes_per_line = LittleShort (pcx.bytes_per_line);
218 pcx.palette_type = LittleShort (pcx.palette_type);
220 image_width = pcx.xmax + 1 - pcx.xmin;
221 image_height = pcx.ymax + 1 - pcx.ymin;
222 if (pcx.manufacturer != 0x0a || pcx.version != 5 || pcx.encoding != 1 || pcx.bits_per_pixel != 8 || image_width > 32768 || image_height > 32768 || image_width <= 0 || image_height <= 0)
224 Con_Print("Bad pcx file\n");
228 palette = f + filesize - 768;
230 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width*image_height*4);
233 Con_Printf("LoadPCX: not enough memory for %i by %i image\n", image_width, image_height);
236 pbuf = image_buffer + image_width*image_height*3;
239 for (y = 0;y < image_height && fin < enddata;y++)
241 a = pbuf + y * image_width;
242 for (x = 0;x < image_width && fin < enddata;)
249 x2 = x + (dataByte & 0x3F);
251 if (x2 > image_width)
252 x2 = image_width; // technically an error
259 while(x < image_width)
266 for(x = 0;x < image_width*image_height;x++)
283 qboolean LoadPCX_QWSkin(const unsigned char *f, int filesize, unsigned char *pixels, int outwidth, int outheight)
287 const unsigned char *fin, *enddata;
288 int x, y, x2, dataByte, pcxwidth, pcxheight;
290 if (filesize < (int)sizeof(pcx) + 768)
293 image_width = outwidth;
294 image_height = outheight;
297 memcpy(&pcx, fin, sizeof(pcx));
300 // LordHavoc: big-endian support ported from QF newtree
301 pcx.xmax = LittleShort (pcx.xmax);
302 pcx.xmin = LittleShort (pcx.xmin);
303 pcx.ymax = LittleShort (pcx.ymax);
304 pcx.ymin = LittleShort (pcx.ymin);
305 pcx.hres = LittleShort (pcx.hres);
306 pcx.vres = LittleShort (pcx.vres);
307 pcx.bytes_per_line = LittleShort (pcx.bytes_per_line);
308 pcx.palette_type = LittleShort (pcx.palette_type);
310 pcxwidth = pcx.xmax + 1 - pcx.xmin;
311 pcxheight = pcx.ymax + 1 - pcx.ymin;
312 if (pcx.manufacturer != 0x0a || pcx.version != 5 || pcx.encoding != 1 || pcx.bits_per_pixel != 8 || pcxwidth > 4096 || pcxheight > 4096 || pcxwidth <= 0 || pcxheight <= 0)
315 enddata = f + filesize - 768;
317 for (y = 0;y < outheight && fin < enddata;y++)
319 a = pixels + y * outwidth;
320 // pad the output with blank lines if needed
323 memset(a, 0, outwidth);
326 for (x = 0;x < pcxwidth;)
333 x2 = x + (dataByte & 0x3F);
345 if (x < outwidth) // truncate to destination width
362 qboolean LoadPCX_PaletteOnly(const unsigned char *f, int filesize, unsigned char *palette768b)
366 memcpy(palette768b, f + filesize - 768, 768);
371 =========================================================
375 =========================================================
378 typedef struct _TargaHeader
380 unsigned char id_length, colormap_type, image_type;
381 unsigned short colormap_index, colormap_length;
382 unsigned char colormap_size;
383 unsigned short x_origin, y_origin, width, height;
384 unsigned char pixel_size, attributes;
388 static void PrintTargaHeader(TargaHeader *t)
390 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);
398 unsigned char *LoadTGA_BGRA (const unsigned char *f, int filesize, int *miplevel)
400 int x, y, pix_inc, row_inci, runlen, alphabits;
401 unsigned char *image_buffer;
402 unsigned int *pixbufi;
403 const unsigned char *fin, *enddata;
404 TargaHeader targa_header;
405 unsigned int palettei[256];
416 enddata = f + filesize;
418 targa_header.id_length = f[0];
419 targa_header.colormap_type = f[1];
420 targa_header.image_type = f[2];
422 targa_header.colormap_index = f[3] + f[4] * 256;
423 targa_header.colormap_length = f[5] + f[6] * 256;
424 targa_header.colormap_size = f[7];
425 targa_header.x_origin = f[8] + f[9] * 256;
426 targa_header.y_origin = f[10] + f[11] * 256;
427 targa_header.width = image_width = f[12] + f[13] * 256;
428 targa_header.height = image_height = f[14] + f[15] * 256;
429 targa_header.pixel_size = f[16];
430 targa_header.attributes = f[17];
432 if (image_width > 32768 || image_height > 32768 || image_width <= 0 || image_height <= 0)
434 Con_Print("LoadTGA: invalid size\n");
435 PrintTargaHeader(&targa_header);
439 memset(palettei, 0, sizeof(palettei));
441 // advance to end of header
444 // skip TARGA image comment (usually 0 bytes)
445 fin += targa_header.id_length;
447 // read/skip the colormap if present (note: according to the TARGA spec it
448 // can be present even on truecolor or greyscale images, just not used by
450 if (targa_header.colormap_type)
452 if (targa_header.colormap_length > 256)
454 Con_Print("LoadTGA: only up to 256 colormap_length supported\n");
455 PrintTargaHeader(&targa_header);
458 if (targa_header.colormap_index)
460 Con_Print("LoadTGA: colormap_index not supported\n");
461 PrintTargaHeader(&targa_header);
464 if (targa_header.colormap_size == 24)
466 for (x = 0;x < targa_header.colormap_length;x++)
472 palettei[x] = bgra.i;
475 else if (targa_header.colormap_size == 32)
477 memcpy(palettei, fin, targa_header.colormap_length*4);
478 fin += targa_header.colormap_length * 4;
482 Con_Print("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
483 PrintTargaHeader(&targa_header);
488 // check our pixel_size restrictions according to image_type
489 switch (targa_header.image_type & ~8)
492 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
494 Con_Print("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
495 PrintTargaHeader(&targa_header);
500 // set up a palette to make the loader easier
501 for (x = 0;x < 256;x++)
503 bgra.b[0] = bgra.b[1] = bgra.b[2] = x;
505 palettei[x] = bgra.i;
507 // fall through to colormap case
509 if (targa_header.pixel_size != 8)
511 Con_Print("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
512 PrintTargaHeader(&targa_header);
517 Con_Printf("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
518 PrintTargaHeader(&targa_header);
522 if (targa_header.attributes & 0x10)
524 Con_Print("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
528 // number of attribute bits per pixel, we only support 0 or 8
529 alphabits = targa_header.attributes & 0x0F;
530 if (alphabits != 8 && alphabits != 0)
532 Con_Print("LoadTGA: only 0 or 8 attribute (alpha) bits supported\n");
536 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
539 Con_Printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
543 // If bit 5 of attributes isn't set, the image has been stored from bottom to top
544 if ((targa_header.attributes & 0x20) == 0)
546 pixbufi = (unsigned int*)image_buffer + (image_height - 1)*image_width;
547 row_inci = -image_width*2;
551 pixbufi = (unsigned int*)image_buffer;
556 if ((targa_header.image_type & ~8) == 2)
557 pix_inc = (targa_header.pixel_size + 7) / 8;
558 switch (targa_header.image_type)
560 case 1: // colormapped, uncompressed
561 case 3: // greyscale, uncompressed
562 if (fin + image_width * image_height * pix_inc > enddata)
564 for (y = 0;y < image_height;y++, pixbufi += row_inci)
565 for (x = 0;x < image_width;x++)
566 *pixbufi++ = palettei[*fin++];
569 // BGR or BGRA, uncompressed
570 if (fin + image_width * image_height * pix_inc > enddata)
572 if (targa_header.pixel_size == 32 && alphabits)
574 for (y = 0;y < image_height;y++)
575 memcpy(pixbufi + y * (image_width + row_inci), fin + y * image_width * pix_inc, image_width*4);
579 for (y = 0;y < image_height;y++, pixbufi += row_inci)
581 for (x = 0;x < image_width;x++, fin += pix_inc)
592 case 9: // colormapped, RLE
593 case 11: // greyscale, RLE
594 for (y = 0;y < image_height;y++, pixbufi += row_inci)
596 for (x = 0;x < image_width;)
599 break; // error - truncated file
603 // RLE - all pixels the same color
605 if (fin + pix_inc > enddata)
606 break; // error - truncated file
607 if (x + runlen > image_width)
608 break; // error - line exceeds width
609 bgra.i = palettei[*fin++];
615 // uncompressed - all pixels different color
617 if (fin + pix_inc * runlen > enddata)
618 break; // error - truncated file
619 if (x + runlen > image_width)
620 break; // error - line exceeds width
622 *pixbufi++ = palettei[*fin++];
626 if (x != image_width)
628 // pixbufi is useless now
629 Con_Printf("LoadTGA: corrupt file\n");
636 if (targa_header.pixel_size == 32 && alphabits)
638 for (y = 0;y < image_height;y++, pixbufi += row_inci)
640 for (x = 0;x < image_width;)
643 break; // error - truncated file
647 // RLE - all pixels the same color
649 if (fin + pix_inc > enddata)
650 break; // error - truncated file
651 if (x + runlen > image_width)
652 break; // error - line exceeds width
663 // uncompressed - all pixels different color
665 if (fin + pix_inc * runlen > enddata)
666 break; // error - truncated file
667 if (x + runlen > image_width)
668 break; // error - line exceeds width
681 if (x != image_width)
683 // pixbufi is useless now
684 Con_Printf("LoadTGA: corrupt file\n");
691 for (y = 0;y < image_height;y++, pixbufi += row_inci)
693 for (x = 0;x < image_width;)
696 break; // error - truncated file
700 // RLE - all pixels the same color
702 if (fin + pix_inc > enddata)
703 break; // error - truncated file
704 if (x + runlen > image_width)
705 break; // error - line exceeds width
716 // uncompressed - all pixels different color
718 if (fin + pix_inc * runlen > enddata)
719 break; // error - truncated file
720 if (x + runlen > image_width)
721 break; // error - line exceeds width
734 if (x != image_width)
736 // pixbufi is useless now
737 Con_Printf("LoadTGA: corrupt file\n");
744 // unknown image_type
751 typedef struct q2wal_s
754 unsigned width, height;
755 unsigned offsets[MIPLEVELS]; // four mip maps stored
756 char animname[32]; // next frame in animation chain
762 static unsigned char *LoadWAL_BGRA (const unsigned char *f, int filesize, int *miplevel)
764 unsigned char *image_buffer;
765 const q2wal_t *inwal = (const q2wal_t *)f;
767 if (filesize < (int) sizeof(q2wal_t))
769 Con_Print("LoadWAL: invalid WAL file\n");
773 image_width = LittleLong(inwal->width);
774 image_height = LittleLong(inwal->height);
775 if (image_width > 32768 || image_height > 32768 || image_width <= 0 || image_height <= 0)
777 Con_Printf("LoadWAL: invalid size %ix%i\n", image_width, image_height);
781 if (filesize < (int) LittleLong(inwal->offsets[0]) + image_width * image_height)
783 Con_Print("LoadWAL: invalid WAL file\n");
787 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
790 Con_Printf("LoadWAL: not enough memory for %i by %i image\n", image_width, image_height);
793 Image_Copy8bitBGRA(f + LittleLong(inwal->offsets[0]), image_buffer, image_width * image_height, q2palette_bgra_complete);
797 qboolean LoadWAL_GetMetadata(const unsigned char *f, int filesize, int *retwidth, int *retheight, int *retflags, int *retvalue, int *retcontents, char *retanimname32c)
799 const q2wal_t *inwal = (const q2wal_t *)f;
801 if (filesize < (int) sizeof(q2wal_t))
803 Con_Print("LoadWAL: invalid WAL file\n");
809 memset(retanimname32c, 0, 32);
813 *retwidth = LittleLong(inwal->width);
814 *retheight = LittleLong(inwal->height);
815 *retflags = LittleLong(inwal->flags);
816 *retvalue = LittleLong(inwal->value);
817 *retcontents = LittleLong(inwal->contents);
818 memcpy(retanimname32c, inwal->animname, 32);
819 retanimname32c[31] = 0;
824 void Image_StripImageExtension (const char *in, char *out, size_t size_out)
831 ext = FS_FileExtension(in);
832 if (ext && (!strcmp(ext, "tga") || !strcmp(ext, "pcx") || !strcmp(ext, "lmp") || !strcmp(ext, "png") || !strcmp(ext, "jpg") || !strcmp(ext, "wal")))
833 FS_StripExtension(in, out, size_out);
835 strlcpy(out, in, size_out);
838 static unsigned char image_linearfromsrgb[256];
839 static unsigned char image_srgbfromlinear_lightmap[256];
841 void Image_MakeLinearColorsFromsRGB(unsigned char *pout, const unsigned char *pin, int numpixels)
844 // this math from http://www.opengl.org/registry/specs/EXT/texture_sRGB.txt
845 if (!image_linearfromsrgb[255])
846 for (i = 0;i < 256;i++)
847 image_linearfromsrgb[i] = (unsigned char)floor(Image_LinearFloatFromsRGB(i) * 255.0f + 0.5f);
848 for (i = 0;i < numpixels;i++)
850 pout[i*4+0] = image_linearfromsrgb[pin[i*4+0]];
851 pout[i*4+1] = image_linearfromsrgb[pin[i*4+1]];
852 pout[i*4+2] = image_linearfromsrgb[pin[i*4+2]];
853 pout[i*4+3] = pin[i*4+3];
857 void Image_MakesRGBColorsFromLinear_Lightmap(unsigned char *pout, const unsigned char *pin, int numpixels)
860 // this math from http://www.opengl.org/registry/specs/EXT/texture_sRGB.txt
861 if (!image_srgbfromlinear_lightmap[255])
862 for (i = 0;i < 256;i++)
863 image_srgbfromlinear_lightmap[i] = (unsigned char)floor(bound(0.0f, Image_sRGBFloatFromLinear_Lightmap(i), 1.0f) * 255.0f + 0.5f);
864 for (i = 0;i < numpixels;i++)
866 pout[i*4+0] = image_srgbfromlinear_lightmap[pin[i*4+0]];
867 pout[i*4+1] = image_srgbfromlinear_lightmap[pin[i*4+1]];
868 pout[i*4+2] = image_srgbfromlinear_lightmap[pin[i*4+2]];
869 pout[i*4+3] = pin[i*4+3];
873 typedef struct imageformat_s
875 const char *formatstring;
876 unsigned char *(*loadfunc)(const unsigned char *f, int filesize, int *miplevel);
880 // GAME_TENEBRAE only
881 imageformat_t imageformats_tenebrae[] =
883 {"override/%s.tga", LoadTGA_BGRA},
884 {"override/%s.png", PNG_LoadImage_BGRA},
885 {"override/%s.jpg", JPEG_LoadImage_BGRA},
886 {"override/%s.pcx", LoadPCX_BGRA},
887 {"%s.tga", LoadTGA_BGRA},
888 {"%s.png", PNG_LoadImage_BGRA},
889 {"%s.jpg", JPEG_LoadImage_BGRA},
890 {"%s.pcx", LoadPCX_BGRA},
894 imageformat_t imageformats_nopath[] =
896 {"override/%s.tga", LoadTGA_BGRA},
897 {"override/%s.png", PNG_LoadImage_BGRA},
898 {"override/%s.jpg", JPEG_LoadImage_BGRA},
899 {"textures/%s.tga", LoadTGA_BGRA},
900 {"textures/%s.png", PNG_LoadImage_BGRA},
901 {"textures/%s.jpg", JPEG_LoadImage_BGRA},
902 {"%s.tga", LoadTGA_BGRA},
903 {"%s.png", PNG_LoadImage_BGRA},
904 {"%s.jpg", JPEG_LoadImage_BGRA},
905 {"%s.pcx", LoadPCX_BGRA},
909 // GAME_DELUXEQUAKE only
910 // VorteX: the point why i use such messy texture paths is
911 // that GtkRadiant can't detect normal/gloss textures
912 // and exclude them from texture browser
913 // so i just use additional folder to store this textures
914 imageformat_t imageformats_dq[] =
916 {"%s.tga", LoadTGA_BGRA},
917 {"%s.jpg", JPEG_LoadImage_BGRA},
918 {"texturemaps/%s.tga", LoadTGA_BGRA},
919 {"texturemaps/%s.jpg", JPEG_LoadImage_BGRA},
923 imageformat_t imageformats_textures[] =
925 {"%s.tga", LoadTGA_BGRA},
926 {"%s.png", PNG_LoadImage_BGRA},
927 {"%s.jpg", JPEG_LoadImage_BGRA},
928 {"%s.pcx", LoadPCX_BGRA},
929 {"%s.wal", LoadWAL_BGRA},
933 imageformat_t imageformats_gfx[] =
935 {"%s.tga", LoadTGA_BGRA},
936 {"%s.png", PNG_LoadImage_BGRA},
937 {"%s.jpg", JPEG_LoadImage_BGRA},
938 {"%s.pcx", LoadPCX_BGRA},
942 imageformat_t imageformats_other[] =
944 {"%s.tga", LoadTGA_BGRA},
945 {"%s.png", PNG_LoadImage_BGRA},
946 {"%s.jpg", JPEG_LoadImage_BGRA},
947 {"%s.pcx", LoadPCX_BGRA},
951 int fixtransparentpixels(unsigned char *data, int w, int h);
952 unsigned char *loadimagepixelsbgra (const char *filename, qboolean complain, qboolean allowFixtrans, qboolean convertsRGB, int *miplevel)
954 fs_offset_t filesize;
955 imageformat_t *firstformat, *format;
956 unsigned char *f, *data = NULL, *data2 = NULL;
957 char basename[MAX_QPATH], name[MAX_QPATH], name2[MAX_QPATH], *c;
959 //if (developer_memorydebug.integer)
960 // Mem_CheckSentinelsGlobal();
961 if (developer_texturelogging.integer)
962 Log_Printf("textures.log", "%s\n", filename);
963 Image_StripImageExtension(filename, basename, sizeof(basename)); // strip filename extensions to allow replacement by other types
964 // replace *'s with #, so commandline utils don't get confused when dealing with the external files
965 for (c = basename;*c;c++)
969 if (strchr(basename, '/'))
972 for (i = 0;i < (int)sizeof(name)-1 && basename[i] != '/';i++)
973 name[i] = basename[i];
976 if (gamemode == GAME_TENEBRAE)
977 firstformat = imageformats_tenebrae;
978 else if (gamemode == GAME_DELUXEQUAKE)
979 firstformat = imageformats_dq;
980 else if (!strcasecmp(name, "textures"))
981 firstformat = imageformats_textures;
982 else if (!strcasecmp(name, "gfx"))
983 firstformat = imageformats_gfx;
984 else if (!strchr(basename, '/'))
985 firstformat = imageformats_nopath;
987 firstformat = imageformats_other;
988 // now try all the formats in the selected list
989 for (format = firstformat;format->formatstring;format++)
991 dpsnprintf (name, sizeof(name), format->formatstring, basename);
992 f = FS_LoadFile(name, tempmempool, true, &filesize);
995 int mymiplevel = miplevel ? *miplevel : 0;
998 data = format->loadfunc(f, (int)filesize, &mymiplevel);
1002 if(format->loadfunc == JPEG_LoadImage_BGRA) // jpeg can't do alpha, so let's simulate it by loading another jpeg
1004 dpsnprintf (name2, sizeof(name2), format->formatstring, va(vabuf, sizeof(vabuf), "%s_alpha", basename));
1005 f = FS_LoadFile(name2, tempmempool, true, &filesize);
1008 int mymiplevel2 = miplevel ? *miplevel : 0;
1009 int image_width_save = image_width;
1010 int image_height_save = image_height;
1011 data2 = format->loadfunc(f, (int)filesize, &mymiplevel2);
1012 if(data2 && mymiplevel == mymiplevel2 && image_width == image_width_save && image_height == image_height_save)
1013 Image_CopyAlphaFromBlueBGRA(data, data2, image_width, image_height);
1015 Con_Printf("loadimagepixelsrgba: corrupt or invalid alpha image %s_alpha\n", basename);
1016 image_width = image_width_save;
1017 image_height = image_height_save;
1023 if (developer_loading.integer)
1024 Con_DPrintf("loaded image %s (%dx%d)\n", name, image_width, image_height);
1026 *miplevel = mymiplevel;
1027 //if (developer_memorydebug.integer)
1028 // Mem_CheckSentinelsGlobal();
1029 if(allowFixtrans && r_fixtrans_auto.integer)
1031 int n = fixtransparentpixels(data, image_width, image_height);
1034 Con_Printf("- had to fix %s (%d pixels changed)\n", name, n);
1035 if(r_fixtrans_auto.integer >= 2)
1037 char outfilename[MAX_QPATH], buf[MAX_QPATH];
1038 Image_StripImageExtension(name, buf, sizeof(buf));
1039 dpsnprintf(outfilename, sizeof(outfilename), "fixtrans/%s.tga", buf);
1040 Image_WriteTGABGRA(outfilename, image_width, image_height, data);
1041 Con_Printf("- %s written.\n", outfilename);
1046 Image_MakeLinearColorsFromsRGB(data, data, image_width * image_height);
1050 Con_DPrintf("Error loading image %s (file loaded but decode failed)\n", name);
1055 Con_Printf("Couldn't load %s using ", filename);
1056 for (format = firstformat;format->formatstring;format++)
1058 dpsnprintf (name, sizeof(name), format->formatstring, basename);
1059 Con_Printf(format == firstformat ? "\"%s\"" : (format[1].formatstring ? ", \"%s\"" : " or \"%s\".\n"), format->formatstring);
1063 // texture loading can take a while, so make sure we're sending keepalives
1064 CL_KeepaliveMessage(false);
1066 //if (developer_memorydebug.integer)
1067 // Mem_CheckSentinelsGlobal();
1071 extern cvar_t gl_picmip;
1072 rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, qboolean complain, int flags, qboolean allowFixtrans, qboolean sRGB)
1074 unsigned char *data;
1076 int miplevel = R_PicmipForFlags(flags);
1077 if (!(data = loadimagepixelsbgra (filename, complain, allowFixtrans, false, &miplevel)))
1079 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, sRGB ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, flags, miplevel, NULL);
1084 int fixtransparentpixels(unsigned char *data, int w, int h)
1086 int const FIXTRANS_NEEDED = 1;
1087 int const FIXTRANS_HAS_L = 2;
1088 int const FIXTRANS_HAS_R = 4;
1089 int const FIXTRANS_HAS_U = 8;
1090 int const FIXTRANS_HAS_D = 16;
1091 int const FIXTRANS_FIXED = 32;
1092 unsigned char *fixMask = (unsigned char *) Mem_Alloc(tempmempool, w * h);
1094 int changedPixels = 0;
1097 #define FIXTRANS_PIXEL (y*w+x)
1098 #define FIXTRANS_PIXEL_U (((y+h-1)%h)*w+x)
1099 #define FIXTRANS_PIXEL_D (((y+1)%h)*w+x)
1100 #define FIXTRANS_PIXEL_L (y*w+((x+w-1)%w))
1101 #define FIXTRANS_PIXEL_R (y*w+((x+1)%w))
1103 memset(fixMask, 0, w * h);
1104 for(y = 0; y < h; ++y)
1105 for(x = 0; x < w; ++x)
1107 if(data[FIXTRANS_PIXEL * 4 + 3] == 0)
1109 fixMask[FIXTRANS_PIXEL] |= FIXTRANS_NEEDED;
1114 fixMask[FIXTRANS_PIXEL_D] |= FIXTRANS_HAS_U;
1115 fixMask[FIXTRANS_PIXEL_U] |= FIXTRANS_HAS_D;
1116 fixMask[FIXTRANS_PIXEL_R] |= FIXTRANS_HAS_L;
1117 fixMask[FIXTRANS_PIXEL_L] |= FIXTRANS_HAS_R;
1120 if(fixPixels == w * h)
1121 return 0; // sorry, can't do anything about this
1124 for(y = 0; y < h; ++y)
1125 for(x = 0; x < w; ++x)
1126 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_NEEDED)
1128 unsigned int sumR = 0, sumG = 0, sumB = 0, sumA = 0, sumRA = 0, sumGA = 0, sumBA = 0, cnt = 0;
1129 unsigned char r, g, b, a, r0, g0, b0;
1130 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_U)
1132 r = data[FIXTRANS_PIXEL_U * 4 + 2];
1133 g = data[FIXTRANS_PIXEL_U * 4 + 1];
1134 b = data[FIXTRANS_PIXEL_U * 4 + 0];
1135 a = data[FIXTRANS_PIXEL_U * 4 + 3];
1136 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
1138 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_D)
1140 r = data[FIXTRANS_PIXEL_D * 4 + 2];
1141 g = data[FIXTRANS_PIXEL_D * 4 + 1];
1142 b = data[FIXTRANS_PIXEL_D * 4 + 0];
1143 a = data[FIXTRANS_PIXEL_D * 4 + 3];
1144 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
1146 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_L)
1148 r = data[FIXTRANS_PIXEL_L * 4 + 2];
1149 g = data[FIXTRANS_PIXEL_L * 4 + 1];
1150 b = data[FIXTRANS_PIXEL_L * 4 + 0];
1151 a = data[FIXTRANS_PIXEL_L * 4 + 3];
1152 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
1154 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_R)
1156 r = data[FIXTRANS_PIXEL_R * 4 + 2];
1157 g = data[FIXTRANS_PIXEL_R * 4 + 1];
1158 b = data[FIXTRANS_PIXEL_R * 4 + 0];
1159 a = data[FIXTRANS_PIXEL_R * 4 + 3];
1160 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
1164 r0 = data[FIXTRANS_PIXEL * 4 + 2];
1165 g0 = data[FIXTRANS_PIXEL * 4 + 1];
1166 b0 = data[FIXTRANS_PIXEL * 4 + 0];
1169 // there is a surrounding non-alpha pixel
1170 r = (sumRA + sumA / 2) / sumA;
1171 g = (sumGA + sumA / 2) / sumA;
1172 b = (sumBA + sumA / 2) / sumA;
1176 // need to use a "regular" average
1177 r = (sumR + cnt / 2) / cnt;
1178 g = (sumG + cnt / 2) / cnt;
1179 b = (sumB + cnt / 2) / cnt;
1181 if(r != r0 || g != g0 || b != b0)
1183 data[FIXTRANS_PIXEL * 4 + 2] = r;
1184 data[FIXTRANS_PIXEL * 4 + 1] = g;
1185 data[FIXTRANS_PIXEL * 4 + 0] = b;
1186 fixMask[FIXTRANS_PIXEL] |= FIXTRANS_FIXED;
1188 for(y = 0; y < h; ++y)
1189 for(x = 0; x < w; ++x)
1190 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_FIXED)
1192 fixMask[FIXTRANS_PIXEL] &= ~(FIXTRANS_NEEDED | FIXTRANS_FIXED);
1193 fixMask[FIXTRANS_PIXEL_D] |= FIXTRANS_HAS_U;
1194 fixMask[FIXTRANS_PIXEL_U] |= FIXTRANS_HAS_D;
1195 fixMask[FIXTRANS_PIXEL_R] |= FIXTRANS_HAS_L;
1196 fixMask[FIXTRANS_PIXEL_L] |= FIXTRANS_HAS_R;
1200 return changedPixels;
1203 void Image_FixTransparentPixels_f(void)
1205 const char *filename, *filename_pattern;
1208 char outfilename[MAX_QPATH], buf[MAX_QPATH];
1209 unsigned char *data;
1212 Con_Printf("Usage: %s imagefile\n", Cmd_Argv(0));
1215 filename_pattern = Cmd_Argv(1);
1216 search = FS_Search(filename_pattern, true, true);
1219 for(i = 0; i < search->numfilenames; ++i)
1221 filename = search->filenames[i];
1222 Con_Printf("Processing %s... ", filename);
1223 Image_StripImageExtension(filename, buf, sizeof(buf));
1224 dpsnprintf(outfilename, sizeof(outfilename), "fixtrans/%s.tga", buf);
1225 if(!(data = loadimagepixelsbgra(filename, true, false, false, NULL)))
1227 if((n = fixtransparentpixels(data, image_width, image_height)))
1229 Image_WriteTGABGRA(outfilename, image_width, image_height, data);
1230 Con_Printf("%s written (%d pixels changed).\n", outfilename, n);
1233 Con_Printf("unchanged.\n");
1236 FS_FreeSearch(search);
1239 qboolean Image_WriteTGABGR_preflipped (const char *filename, int width, int height, const unsigned char *data)
1242 unsigned char buffer[18];
1243 const void *buffers[2];
1244 fs_offset_t sizes[2];
1246 memset (buffer, 0, 18);
1247 buffer[2] = 2; // uncompressed type
1248 buffer[12] = (width >> 0) & 0xFF;
1249 buffer[13] = (width >> 8) & 0xFF;
1250 buffer[14] = (height >> 0) & 0xFF;
1251 buffer[15] = (height >> 8) & 0xFF;
1252 buffer[16] = 24; // pixel size
1254 buffers[0] = buffer;
1257 sizes[1] = width*height*3;
1258 ret = FS_WriteFileInBlocks(filename, buffers, sizes, 2);
1263 qboolean Image_WriteTGABGRA (const char *filename, int width, int height, const unsigned char *data)
1266 unsigned char *buffer, *out;
1267 const unsigned char *in, *end;
1270 buffer = (unsigned char *)Mem_Alloc(tempmempool, width*height*4 + 18);
1272 memset (buffer, 0, 18);
1273 buffer[2] = 2; // uncompressed type
1274 buffer[12] = (width >> 0) & 0xFF;
1275 buffer[13] = (width >> 8) & 0xFF;
1276 buffer[14] = (height >> 0) & 0xFF;
1277 buffer[15] = (height >> 8) & 0xFF;
1279 for (y = 3;y < width*height*4;y += 4)
1283 if (y < width*height*4)
1285 // save the alpha channel
1286 buffer[16] = 32; // pixel size
1287 buffer[17] = 8; // 8 bits of alpha
1291 for (y = height - 1;y >= 0;y--)
1293 memcpy(out, data + y * width * 4, width * 4);
1299 // save only the color channels
1300 buffer[16] = 24; // pixel size
1301 buffer[17] = 0; // 8 bits of alpha
1303 // truncate bgra to bgr and flip upside down
1305 for (y = height - 1;y >= 0;y--)
1307 in = data + y * width * 4;
1308 end = in + width * 4;
1309 for (;in < end;in += 4)
1317 ret = FS_WriteFile (filename, buffer, out - buffer);
1324 static void Image_Resample32LerpLine (const unsigned char *in, unsigned char *out, int inwidth, int outwidth)
1326 int j, xi, oldx = 0, f, fstep, endx, lerp;
1327 fstep = (int) (inwidth*65536.0f/outwidth);
1329 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
1334 in += (xi - oldx) * 4;
1340 *out++ = (unsigned char) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
1341 *out++ = (unsigned char) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
1342 *out++ = (unsigned char) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
1343 *out++ = (unsigned char) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
1345 else // last pixel of the line has no pixel to lerp to
1355 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (unsigned char) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
1356 static void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1358 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
1360 const unsigned char *inrow;
1361 unsigned char *resamplerow1;
1362 unsigned char *resamplerow2;
1363 out = (unsigned char *)outdata;
1364 fstep = (int) (inheight*65536.0f/outheight);
1366 resamplerow1 = (unsigned char *)Mem_Alloc(tempmempool, outwidth*4*2);
1367 resamplerow2 = resamplerow1 + outwidth*4;
1369 inrow = (const unsigned char *)indata;
1371 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1372 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1373 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1381 inrow = (unsigned char *)indata + inwidth4*yi;
1383 memcpy(resamplerow1, resamplerow2, outwidth4);
1385 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1386 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1437 resamplerow1 -= outwidth4;
1438 resamplerow2 -= outwidth4;
1444 inrow = (unsigned char *)indata + inwidth4*yi;
1446 memcpy(resamplerow1, resamplerow2, outwidth4);
1448 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1451 memcpy(out, resamplerow1, outwidth4);
1455 Mem_Free(resamplerow1);
1456 resamplerow1 = NULL;
1457 resamplerow2 = NULL;
1460 static void Image_Resample32Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1463 unsigned frac, fracstep;
1464 // relies on int being 4 bytes
1466 out = (int *)outdata;
1468 fracstep = inwidth*0x10000/outwidth;
1469 for (i = 0;i < outheight;i++)
1471 inrow = (int *)indata + inwidth*(i*inheight/outheight);
1472 frac = fracstep >> 1;
1476 out[0] = inrow[frac >> 16];frac += fracstep;
1477 out[1] = inrow[frac >> 16];frac += fracstep;
1478 out[2] = inrow[frac >> 16];frac += fracstep;
1479 out[3] = inrow[frac >> 16];frac += fracstep;
1485 out[0] = inrow[frac >> 16];frac += fracstep;
1486 out[1] = inrow[frac >> 16];frac += fracstep;
1491 out[0] = inrow[frac >> 16];frac += fracstep;
1502 void Image_Resample32(const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int quality)
1504 if (indepth != 1 || outdepth != 1)
1506 Con_Printf ("Image_Resample: 3D resampling not supported\n");
1510 Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1512 Image_Resample32Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1515 // in can be the same as out
1516 void Image_MipReduce32(const unsigned char *in, unsigned char *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth)
1518 const unsigned char *inrow;
1520 if (*depth != 1 || destdepth != 1)
1522 Con_Printf ("Image_Resample: 3D resampling not supported\n");
1523 if (*width > destwidth)
1525 if (*height > destheight)
1527 if (*depth > destdepth)
1531 // note: if given odd width/height this discards the last row/column of
1532 // pixels, rather than doing a proper box-filter scale down
1534 nextrow = *width * 4;
1535 if (*width > destwidth)
1538 if (*height > destheight)
1542 for (y = 0;y < *height;y++, inrow += nextrow * 2)
1544 for (in = inrow, x = 0;x < *width;x++)
1546 out[0] = (unsigned char) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
1547 out[1] = (unsigned char) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1548 out[2] = (unsigned char) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1549 out[3] = (unsigned char) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1558 for (y = 0;y < *height;y++, inrow += nextrow)
1560 for (in = inrow, x = 0;x < *width;x++)
1562 out[0] = (unsigned char) ((in[0] + in[4]) >> 1);
1563 out[1] = (unsigned char) ((in[1] + in[5]) >> 1);
1564 out[2] = (unsigned char) ((in[2] + in[6]) >> 1);
1565 out[3] = (unsigned char) ((in[3] + in[7]) >> 1);
1574 if (*height > destheight)
1578 for (y = 0;y < *height;y++, inrow += nextrow * 2)
1580 for (in = inrow, x = 0;x < *width;x++)
1582 out[0] = (unsigned char) ((in[0] + in[nextrow ]) >> 1);
1583 out[1] = (unsigned char) ((in[1] + in[nextrow+1]) >> 1);
1584 out[2] = (unsigned char) ((in[2] + in[nextrow+2]) >> 1);
1585 out[3] = (unsigned char) ((in[3] + in[nextrow+3]) >> 1);
1592 Con_Printf ("Image_MipReduce: desired size already achieved\n");
1596 void Image_HeightmapToNormalmap_BGRA(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
1598 int x, y, x1, x2, y1, y2;
1599 const unsigned char *b, *row[3];
1602 float ibumpscale, n[3];
1603 ibumpscale = (255.0f * 6.0f) / bumpscale;
1605 for (y = 0, y1 = height-1;y < height;y1 = y, y++)
1607 y2 = y + 1;if (y2 >= height) y2 = 0;
1608 row[0] = inpixels + (y1 * width) * 4;
1609 row[1] = inpixels + (y * width) * 4;
1610 row[2] = inpixels + (y2 * width) * 4;
1611 for (x = 0, x1 = width-1;x < width;x1 = x, x++)
1613 x2 = x + 1;if (x2 >= width) x2 = 0;
1615 b = row[1] + x1 * 4;p[0] = (b[0] + b[1] + b[2]);
1616 b = row[1] + x2 * 4;p[1] = (b[0] + b[1] + b[2]);
1618 b = row[0] + x * 4;p[2] = (b[0] + b[1] + b[2]);
1619 b = row[2] + x * 4;p[3] = (b[0] + b[1] + b[2]);
1621 b = row[1] + x * 4;p[4] = (b[0] + b[1] + b[2]);
1622 // calculate a normal from the slopes
1627 // turn it into a dot3 rgb vector texture
1628 out[2] = (int)(128.0f + n[0] * 127.0f);
1629 out[1] = (int)(128.0f + n[1] * 127.0f);
1630 out[0] = (int)(128.0f + n[2] * 127.0f);
1631 out[3] = (p[4]) / 3;