13 char image_q2animname[32];
15 static void Image_CopyAlphaFromBlueBGRA(unsigned char *outpixels, const unsigned char *inpixels, int w, int h)
19 for(i = 0; i < n; ++i)
20 outpixels[4*i+3] = inpixels[4*i]; // blue channel
24 // written by LordHavoc in a readable way, optimized by Vic, further optimized by LordHavoc (the non-special index case), readable version preserved below this
25 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)
28 const unsigned char *in, *line;
29 int row_inc = (inputflipy ? -inputwidth : inputwidth) * numinputcomponents, col_inc = (inputflipx ? -1 : 1) * numinputcomponents;
30 int row_ofs = (inputflipy ? (inputheight - 1) * inputwidth * numinputcomponents : 0), col_ofs = (inputflipx ? (inputwidth - 1) * numinputcomponents : 0);
32 for (c = 0; c < numoutputcomponents; c++)
33 if (outputinputcomponentindices[c] & 0x80000000)
35 if (c < numoutputcomponents)
37 // special indices used
38 if (inputflipdiagonal)
40 for (x = 0, line = inpixels + col_ofs; x < inputwidth; x++, line += col_inc)
41 for (y = 0, in = line + row_ofs; y < inputheight; y++, in += row_inc, outpixels += numoutputcomponents)
42 for (c = 0; c < numoutputcomponents; c++)
43 outpixels[c] = ((index = outputinputcomponentindices[c]) & 0x80000000) ? index : in[index];
47 for (y = 0, line = inpixels + row_ofs; y < inputheight; y++, line += row_inc)
48 for (x = 0, in = line + col_ofs; x < inputwidth; x++, in += col_inc, outpixels += numoutputcomponents)
49 for (c = 0; c < numoutputcomponents; c++)
50 outpixels[c] = ((index = outputinputcomponentindices[c]) & 0x80000000) ? index : in[index];
55 // special indices not used
56 if (inputflipdiagonal)
58 for (x = 0, line = inpixels + col_ofs; x < inputwidth; x++, line += col_inc)
59 for (y = 0, in = line + row_ofs; y < inputheight; y++, in += row_inc, outpixels += numoutputcomponents)
60 for (c = 0; c < numoutputcomponents; c++)
61 outpixels[c] = in[outputinputcomponentindices[c]];
65 for (y = 0, line = inpixels + row_ofs; y < inputheight; y++, line += row_inc)
66 for (x = 0, in = line + col_ofs; x < inputwidth; x++, in += col_inc, outpixels += numoutputcomponents)
67 for (c = 0; c < numoutputcomponents; c++)
68 outpixels[c] = in[outputinputcomponentindices[c]];
73 // intentionally readable version
74 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)
77 const unsigned char *in, *inrow, *incolumn;
78 if (inputflipdiagonal)
80 for (x = 0;x < inputwidth;x++)
82 for (y = 0;y < inputheight;y++)
84 in = inpixels + ((inputflipy ? inputheight - 1 - y : y) * inputwidth + (inputflipx ? inputwidth - 1 - x : x)) * numinputcomponents;
85 for (c = 0;c < numoutputcomponents;c++)
87 index = outputinputcomponentindices[c];
88 if (index & 0x80000000)
91 *outpixels++ = in[index];
98 for (y = 0;y < inputheight;y++)
100 for (x = 0;x < inputwidth;x++)
102 in = inpixels + ((inputflipy ? inputheight - 1 - y : y) * inputwidth + (inputflipx ? inputwidth - 1 - x : x)) * numinputcomponents;
103 for (c = 0;c < numoutputcomponents;c++)
105 index = outputinputcomponentindices[c];
106 if (index & 0x80000000)
107 *outpixels++ = index;
109 *outpixels++ = in[index];
117 void Image_GammaRemapRGB(const unsigned char *in, unsigned char *out, int pixels, const unsigned char *gammar, const unsigned char *gammag, const unsigned char *gammab)
121 out[0] = gammar[in[0]];
122 out[1] = gammag[in[1]];
123 out[2] = gammab[in[2]];
129 // note: pal must be 32bit color
130 void Image_Copy8bitBGRA(const unsigned char *in, unsigned char *out, int pixels, const unsigned int *pal)
132 int *iout = (int *)out;
135 iout[0] = pal[in[0]];
136 iout[1] = pal[in[1]];
137 iout[2] = pal[in[2]];
138 iout[3] = pal[in[3]];
139 iout[4] = pal[in[4]];
140 iout[5] = pal[in[5]];
141 iout[6] = pal[in[6]];
142 iout[7] = pal[in[7]];
149 iout[0] = pal[in[0]];
150 iout[1] = pal[in[1]];
151 iout[2] = pal[in[2]];
152 iout[3] = pal[in[3]];
158 iout[0] = pal[in[0]];
159 iout[1] = pal[in[1]];
164 iout[0] = pal[in[0]];
168 =================================================================
172 =================================================================
181 unsigned short xmin,ymin,xmax,ymax;
182 unsigned short hres,vres;
183 unsigned char palette[48];
186 unsigned short bytes_per_line;
187 unsigned short palette_type;
196 static unsigned char* LoadPCX_BGRA (const unsigned char *f, int filesize, int *miplevel)
199 unsigned char *a, *b, *image_buffer, *pbuf;
200 const unsigned char *palette, *fin, *enddata;
201 int x, y, x2, dataByte;
203 if (filesize < (int)sizeof(pcx) + 768)
205 Con_Print("Bad pcx file\n");
211 memcpy(&pcx, fin, sizeof(pcx));
214 // LordHavoc: big-endian support ported from QF newtree
215 pcx.xmax = LittleShort (pcx.xmax);
216 pcx.xmin = LittleShort (pcx.xmin);
217 pcx.ymax = LittleShort (pcx.ymax);
218 pcx.ymin = LittleShort (pcx.ymin);
219 pcx.hres = LittleShort (pcx.hres);
220 pcx.vres = LittleShort (pcx.vres);
221 pcx.bytes_per_line = LittleShort (pcx.bytes_per_line);
222 pcx.palette_type = LittleShort (pcx.palette_type);
224 image_width = pcx.xmax + 1 - pcx.xmin;
225 image_height = pcx.ymax + 1 - pcx.ymin;
226 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)
228 Con_Print("Bad pcx file\n");
232 palette = f + filesize - 768;
234 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width*image_height*4);
237 Con_Printf("LoadPCX: not enough memory for %i by %i image\n", image_width, image_height);
240 pbuf = image_buffer + image_width*image_height*3;
243 for (y = 0;y < image_height && fin < enddata;y++)
245 a = pbuf + y * image_width;
246 for (x = 0;x < image_width && fin < enddata;)
253 x2 = x + (dataByte & 0x3F);
255 if (x2 > image_width)
256 x2 = image_width; // technically an error
263 while(x < image_width)
270 for(x = 0;x < image_width*image_height;x++)
287 qboolean LoadPCX_QWSkin(const unsigned char *f, int filesize, unsigned char *pixels, int outwidth, int outheight)
291 const unsigned char *fin, *enddata;
292 int x, y, x2, dataByte, pcxwidth, pcxheight;
294 if (filesize < (int)sizeof(pcx) + 768)
297 image_width = outwidth;
298 image_height = outheight;
301 memcpy(&pcx, fin, sizeof(pcx));
304 // LordHavoc: big-endian support ported from QF newtree
305 pcx.xmax = LittleShort (pcx.xmax);
306 pcx.xmin = LittleShort (pcx.xmin);
307 pcx.ymax = LittleShort (pcx.ymax);
308 pcx.ymin = LittleShort (pcx.ymin);
309 pcx.hres = LittleShort (pcx.hres);
310 pcx.vres = LittleShort (pcx.vres);
311 pcx.bytes_per_line = LittleShort (pcx.bytes_per_line);
312 pcx.palette_type = LittleShort (pcx.palette_type);
314 pcxwidth = pcx.xmax + 1 - pcx.xmin;
315 pcxheight = pcx.ymax + 1 - pcx.ymin;
316 if (pcx.manufacturer != 0x0a || pcx.version != 5 || pcx.encoding != 1 || pcx.bits_per_pixel != 8 || pcxwidth > 4096 || pcxheight > 4096 || pcxwidth <= 0 || pcxheight <= 0)
319 enddata = f + filesize - 768;
321 for (y = 0;y < outheight && fin < enddata;y++)
323 a = pixels + y * outwidth;
324 // pad the output with blank lines if needed
327 memset(a, 0, outwidth);
330 for (x = 0;x < pcxwidth;)
337 x2 = x + (dataByte & 0x3F);
349 if (x < outwidth) // truncate to destination width
362 =========================================================
366 =========================================================
369 typedef struct _TargaHeader
371 unsigned char id_length, colormap_type, image_type;
372 unsigned short colormap_index, colormap_length;
373 unsigned char colormap_size;
374 unsigned short x_origin, y_origin, width, height;
375 unsigned char pixel_size, attributes;
379 static void PrintTargaHeader(TargaHeader *t)
381 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);
389 unsigned char *LoadTGA_BGRA (const unsigned char *f, int filesize, int *miplevel)
391 int x, y, pix_inc, row_inci, runlen, alphabits;
392 unsigned char *image_buffer;
393 unsigned int *pixbufi;
394 const unsigned char *fin, *enddata;
395 TargaHeader targa_header;
396 unsigned int palettei[256];
407 enddata = f + filesize;
409 targa_header.id_length = f[0];
410 targa_header.colormap_type = f[1];
411 targa_header.image_type = f[2];
413 targa_header.colormap_index = f[3] + f[4] * 256;
414 targa_header.colormap_length = f[5] + f[6] * 256;
415 targa_header.colormap_size = f[7];
416 targa_header.x_origin = f[8] + f[9] * 256;
417 targa_header.y_origin = f[10] + f[11] * 256;
418 targa_header.width = image_width = f[12] + f[13] * 256;
419 targa_header.height = image_height = f[14] + f[15] * 256;
420 targa_header.pixel_size = f[16];
421 targa_header.attributes = f[17];
423 if (image_width > 32768 || image_height > 32768 || image_width <= 0 || image_height <= 0)
425 Con_Print("LoadTGA: invalid size\n");
426 PrintTargaHeader(&targa_header);
430 // advance to end of header
433 // skip TARGA image comment (usually 0 bytes)
434 fin += targa_header.id_length;
436 // read/skip the colormap if present (note: according to the TARGA spec it
437 // can be present even on truecolor or greyscale images, just not used by
439 if (targa_header.colormap_type)
441 if (targa_header.colormap_length > 256)
443 Con_Print("LoadTGA: only up to 256 colormap_length supported\n");
444 PrintTargaHeader(&targa_header);
447 if (targa_header.colormap_index)
449 Con_Print("LoadTGA: colormap_index not supported\n");
450 PrintTargaHeader(&targa_header);
453 if (targa_header.colormap_size == 24)
455 for (x = 0;x < targa_header.colormap_length;x++)
461 palettei[x] = bgra.i;
464 else if (targa_header.colormap_size == 32)
466 memcpy(palettei, fin, targa_header.colormap_length*4);
467 fin += targa_header.colormap_length * 4;
471 Con_Print("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
472 PrintTargaHeader(&targa_header);
477 // check our pixel_size restrictions according to image_type
478 switch (targa_header.image_type & ~8)
481 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
483 Con_Print("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
484 PrintTargaHeader(&targa_header);
489 // set up a palette to make the loader easier
490 for (x = 0;x < 256;x++)
492 bgra.b[0] = bgra.b[1] = bgra.b[2] = x;
494 palettei[x] = bgra.i;
496 // fall through to colormap case
498 if (targa_header.pixel_size != 8)
500 Con_Print("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
501 PrintTargaHeader(&targa_header);
506 Con_Printf("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
507 PrintTargaHeader(&targa_header);
511 if (targa_header.attributes & 0x10)
513 Con_Print("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
517 // number of attribute bits per pixel, we only support 0 or 8
518 alphabits = targa_header.attributes & 0x0F;
519 if (alphabits != 8 && alphabits != 0)
521 Con_Print("LoadTGA: only 0 or 8 attribute (alpha) bits supported\n");
525 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
528 Con_Printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
532 // If bit 5 of attributes isn't set, the image has been stored from bottom to top
533 if ((targa_header.attributes & 0x20) == 0)
535 pixbufi = (unsigned int*)image_buffer + (image_height - 1)*image_width;
536 row_inci = -image_width*2;
540 pixbufi = (unsigned int*)image_buffer;
545 if ((targa_header.image_type & ~8) == 2)
546 pix_inc = (targa_header.pixel_size + 7) / 8;
547 switch (targa_header.image_type)
549 case 1: // colormapped, uncompressed
550 case 3: // greyscale, uncompressed
551 if (fin + image_width * image_height * pix_inc > enddata)
553 for (y = 0;y < image_height;y++, pixbufi += row_inci)
554 for (x = 0;x < image_width;x++)
555 *pixbufi++ = palettei[*fin++];
558 // BGR or BGRA, uncompressed
559 if (fin + image_width * image_height * pix_inc > enddata)
561 if (targa_header.pixel_size == 32 && alphabits)
563 for (y = 0;y < image_height;y++)
564 memcpy(pixbufi + y * (image_width + row_inci), fin + y * image_width * pix_inc, image_width*4);
568 for (y = 0;y < image_height;y++, pixbufi += row_inci)
570 for (x = 0;x < image_width;x++, fin += pix_inc)
581 case 9: // colormapped, RLE
582 case 11: // greyscale, RLE
583 for (y = 0;y < image_height;y++, pixbufi += row_inci)
585 for (x = 0;x < image_width;)
588 break; // error - truncated file
592 // RLE - all pixels the same color
594 if (fin + pix_inc > enddata)
595 break; // error - truncated file
596 if (x + runlen > image_width)
597 break; // error - line exceeds width
598 bgra.i = palettei[*fin++];
604 // uncompressed - all pixels different color
606 if (fin + pix_inc * runlen > enddata)
607 break; // error - truncated file
608 if (x + runlen > image_width)
609 break; // error - line exceeds width
611 *pixbufi++ = palettei[*fin++];
615 if (x != image_width)
617 // pixbufi is useless now
618 Con_Printf("LoadTGA: corrupt file\n");
625 if (targa_header.pixel_size == 32 && alphabits)
627 for (y = 0;y < image_height;y++, pixbufi += row_inci)
629 for (x = 0;x < image_width;)
632 break; // error - truncated file
636 // RLE - all pixels the same color
638 if (fin + pix_inc > enddata)
639 break; // error - truncated file
640 if (x + runlen > image_width)
641 break; // error - line exceeds width
652 // uncompressed - all pixels different color
654 if (fin + pix_inc * runlen > enddata)
655 break; // error - truncated file
656 if (x + runlen > image_width)
657 break; // error - line exceeds width
670 if (x != image_width)
672 // pixbufi is useless now
673 Con_Printf("LoadTGA: corrupt file\n");
680 for (y = 0;y < image_height;y++, pixbufi += row_inci)
682 for (x = 0;x < image_width;)
685 break; // error - truncated file
689 // RLE - all pixels the same color
691 if (fin + pix_inc > enddata)
692 break; // error - truncated file
693 if (x + runlen > image_width)
694 break; // error - line exceeds width
705 // uncompressed - all pixels different color
707 if (fin + pix_inc * runlen > enddata)
708 break; // error - truncated file
709 if (x + runlen > image_width)
710 break; // error - line exceeds width
723 if (x != image_width)
725 // pixbufi is useless now
726 Con_Printf("LoadTGA: corrupt file\n");
733 // unknown image_type
740 typedef struct q2wal_s
743 unsigned width, height;
744 unsigned offsets[MIPLEVELS]; // four mip maps stored
745 char animname[32]; // next frame in animation chain
751 static unsigned char *LoadWAL_BGRA (const unsigned char *f, int filesize, int *miplevel)
753 unsigned char *image_buffer;
754 const q2wal_t *inwal = (const q2wal_t *)f;
756 if (filesize < (int) sizeof(q2wal_t))
758 Con_Print("LoadWAL: invalid WAL file\n");
762 image_width = LittleLong(inwal->width);
763 image_height = LittleLong(inwal->height);
764 image_q2flags = LittleLong(inwal->flags);
765 image_q2value = LittleLong(inwal->value);
766 image_q2contents = LittleLong(inwal->contents);
767 memcpy(image_q2animname, inwal->animname, sizeof(inwal->animname));
768 image_q2animname[sizeof(image_q2animname)-1] = 0;
769 if (image_width > 32768 || image_height > 32768 || image_width <= 0 || image_height <= 0)
771 Con_Printf("LoadWAL: invalid size %ix%i\n", image_width, image_height);
775 if (filesize < (int) sizeof(q2wal_t) + (int) LittleLong(inwal->offsets[0]) + image_width * image_height)
777 Con_Print("LoadWAL: invalid WAL file\n");
781 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
784 Con_Printf("LoadWAL: not enough memory for %i by %i image\n", image_width, image_height);
787 Image_Copy8bitBGRA(f + LittleLong(inwal->offsets[0]), image_buffer, image_width * image_height, palette_bgra_complete);
792 void Image_StripImageExtension (const char *in, char *out, size_t size_out)
799 ext = FS_FileExtension(in);
800 if (ext && (!strcmp(ext, "tga") || !strcmp(ext, "pcx") || !strcmp(ext, "lmp") || !strcmp(ext, "png") || !strcmp(ext, "jpg")))
801 FS_StripExtension(in, out, size_out);
803 strlcpy(out, in, size_out);
806 static unsigned char image_linearfromsrgb[256];
807 static unsigned char image_srgbfromlinear_lightmap[256];
809 void Image_MakeLinearColorsFromsRGB(unsigned char *pout, const unsigned char *pin, int numpixels)
812 // this math from http://www.opengl.org/registry/specs/EXT/texture_sRGB.txt
813 if (!image_linearfromsrgb[255])
814 for (i = 0;i < 256;i++)
815 image_linearfromsrgb[i] = (unsigned char)floor(Image_LinearFloatFromsRGB(i) * 255.0f + 0.5f);
816 for (i = 0;i < numpixels;i++)
818 pout[i*4+0] = image_linearfromsrgb[pin[i*4+0]];
819 pout[i*4+1] = image_linearfromsrgb[pin[i*4+1]];
820 pout[i*4+2] = image_linearfromsrgb[pin[i*4+2]];
821 pout[i*4+3] = pin[i*4+3];
825 void Image_MakesRGBColorsFromLinear_Lightmap(unsigned char *pout, const unsigned char *pin, int numpixels)
828 // this math from http://www.opengl.org/registry/specs/EXT/texture_sRGB.txt
829 if (!image_srgbfromlinear_lightmap[255])
830 for (i = 0;i < 256;i++)
831 image_srgbfromlinear_lightmap[i] = (unsigned char)floor(bound(0.0f, Image_sRGBFloatFromLinear_Lightmap(i), 1.0f) * 255.0f + 0.5f);
832 for (i = 0;i < numpixels;i++)
834 pout[i*4+0] = image_srgbfromlinear_lightmap[pin[i*4+0]];
835 pout[i*4+1] = image_srgbfromlinear_lightmap[pin[i*4+1]];
836 pout[i*4+2] = image_srgbfromlinear_lightmap[pin[i*4+2]];
837 pout[i*4+3] = pin[i*4+3];
841 typedef struct imageformat_s
843 const char *formatstring;
844 unsigned char *(*loadfunc)(const unsigned char *f, int filesize, int *miplevel);
848 // GAME_TENEBRAE only
849 imageformat_t imageformats_tenebrae[] =
851 {"override/%s.tga", LoadTGA_BGRA},
852 {"override/%s.png", PNG_LoadImage_BGRA},
853 {"override/%s.jpg", JPEG_LoadImage_BGRA},
854 {"override/%s.pcx", LoadPCX_BGRA},
855 {"%s.tga", LoadTGA_BGRA},
856 {"%s.png", PNG_LoadImage_BGRA},
857 {"%s.jpg", JPEG_LoadImage_BGRA},
858 {"%s.pcx", LoadPCX_BGRA},
862 imageformat_t imageformats_nopath[] =
864 {"override/%s.tga", LoadTGA_BGRA},
865 {"override/%s.png", PNG_LoadImage_BGRA},
866 {"override/%s.jpg", JPEG_LoadImage_BGRA},
867 {"textures/%s.tga", LoadTGA_BGRA},
868 {"textures/%s.png", PNG_LoadImage_BGRA},
869 {"textures/%s.jpg", JPEG_LoadImage_BGRA},
870 {"%s.tga", LoadTGA_BGRA},
871 {"%s.png", PNG_LoadImage_BGRA},
872 {"%s.jpg", JPEG_LoadImage_BGRA},
873 {"%s.pcx", LoadPCX_BGRA},
877 // GAME_DELUXEQUAKE only
878 // VorteX: the point why i use such messy texture paths is
879 // that GtkRadiant can't detect normal/gloss textures
880 // and exclude them from texture browser
881 // so i just use additional folder to store this textures
882 imageformat_t imageformats_dq[] =
884 {"%s.tga", LoadTGA_BGRA},
885 {"%s.jpg", JPEG_LoadImage_BGRA},
886 {"texturemaps/%s.tga", LoadTGA_BGRA},
887 {"texturemaps/%s.jpg", JPEG_LoadImage_BGRA},
891 imageformat_t imageformats_textures[] =
893 {"%s.tga", LoadTGA_BGRA},
894 {"%s.png", PNG_LoadImage_BGRA},
895 {"%s.jpg", JPEG_LoadImage_BGRA},
896 {"%s.pcx", LoadPCX_BGRA},
897 {"%s.wal", LoadWAL_BGRA},
901 imageformat_t imageformats_gfx[] =
903 {"%s.tga", LoadTGA_BGRA},
904 {"%s.png", PNG_LoadImage_BGRA},
905 {"%s.jpg", JPEG_LoadImage_BGRA},
906 {"%s.pcx", LoadPCX_BGRA},
910 imageformat_t imageformats_other[] =
912 {"%s.tga", LoadTGA_BGRA},
913 {"%s.png", PNG_LoadImage_BGRA},
914 {"%s.jpg", JPEG_LoadImage_BGRA},
915 {"%s.pcx", LoadPCX_BGRA},
919 int fixtransparentpixels(unsigned char *data, int w, int h);
920 unsigned char *loadimagepixelsbgra (const char *filename, qboolean complain, qboolean allowFixtrans, qboolean convertsRGB, int *miplevel)
922 fs_offset_t filesize;
923 imageformat_t *firstformat, *format;
924 unsigned char *f, *data = NULL, *data2 = NULL;
925 char basename[MAX_QPATH], name[MAX_QPATH], name2[MAX_QPATH], *c;
927 //if (developer_memorydebug.integer)
928 // Mem_CheckSentinelsGlobal();
929 if (developer_texturelogging.integer)
930 Log_Printf("textures.log", "%s\n", filename);
931 Image_StripImageExtension(filename, basename, sizeof(basename)); // strip filename extensions to allow replacement by other types
932 // replace *'s with #, so commandline utils don't get confused when dealing with the external files
933 for (c = basename;*c;c++)
937 if (strchr(basename, '/'))
940 for (i = 0;i < (int)sizeof(name)-1 && basename[i] != '/';i++)
941 name[i] = basename[i];
944 if (gamemode == GAME_TENEBRAE)
945 firstformat = imageformats_tenebrae;
946 else if (gamemode == GAME_DELUXEQUAKE)
947 firstformat = imageformats_dq;
948 else if (!strcasecmp(name, "textures"))
949 firstformat = imageformats_textures;
950 else if (!strcasecmp(name, "gfx"))
951 firstformat = imageformats_gfx;
952 else if (!strchr(basename, '/'))
953 firstformat = imageformats_nopath;
955 firstformat = imageformats_other;
956 // now try all the formats in the selected list
957 for (format = firstformat;format->formatstring;format++)
959 dpsnprintf (name, sizeof(name), format->formatstring, basename);
960 f = FS_LoadFile(name, tempmempool, true, &filesize);
963 int mymiplevel = miplevel ? *miplevel : 0;
968 image_q2contents = 0;
969 image_q2animname[0] = 0;
970 data = format->loadfunc(f, (int)filesize, &mymiplevel);
974 if(format->loadfunc == JPEG_LoadImage_BGRA) // jpeg can't do alpha, so let's simulate it by loading another jpeg
976 dpsnprintf (name2, sizeof(name2), format->formatstring, va(vabuf, sizeof(vabuf), "%s_alpha", basename));
977 f = FS_LoadFile(name2, tempmempool, true, &filesize);
980 int mymiplevel2 = miplevel ? *miplevel : 0;
981 data2 = format->loadfunc(f, (int)filesize, &mymiplevel2);
982 if(data2 && mymiplevel == mymiplevel2)
983 Image_CopyAlphaFromBlueBGRA(data, data2, image_width, image_height);
985 Con_Printf("loadimagepixelsrgba: corrupt or invalid alpha image %s_alpha\n", basename);
991 if (developer_loading.integer)
992 Con_DPrintf("loaded image %s (%dx%d)\n", name, image_width, image_height);
994 *miplevel = mymiplevel;
995 //if (developer_memorydebug.integer)
996 // Mem_CheckSentinelsGlobal();
997 if(allowFixtrans && r_fixtrans_auto.integer)
999 int n = fixtransparentpixels(data, image_width, image_height);
1002 Con_Printf("- had to fix %s (%d pixels changed)\n", name, n);
1003 if(r_fixtrans_auto.integer >= 2)
1005 char outfilename[MAX_QPATH], buf[MAX_QPATH];
1006 Image_StripImageExtension(name, buf, sizeof(buf));
1007 dpsnprintf(outfilename, sizeof(outfilename), "fixtrans/%s.tga", buf);
1008 Image_WriteTGABGRA(outfilename, image_width, image_height, data);
1009 Con_Printf("- %s written.\n", outfilename);
1014 Image_MakeLinearColorsFromsRGB(data, data, image_width * image_height);
1018 Con_DPrintf("Error loading image %s (file loaded but decode failed)\n", name);
1023 Con_Printf("Couldn't load %s using ", filename);
1024 for (format = firstformat;format->formatstring;format++)
1026 dpsnprintf (name, sizeof(name), format->formatstring, basename);
1027 Con_Printf(format == firstformat ? "\"%s\"" : (format[1].formatstring ? ", \"%s\"" : " or \"%s\".\n"), format->formatstring);
1031 // texture loading can take a while, so make sure we're sending keepalives
1032 CL_KeepaliveMessage(false);
1034 //if (developer_memorydebug.integer)
1035 // Mem_CheckSentinelsGlobal();
1039 extern cvar_t gl_picmip;
1040 rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, qboolean complain, int flags, qboolean allowFixtrans, qboolean sRGB)
1042 unsigned char *data;
1044 int miplevel = R_PicmipForFlags(flags);
1045 if (!(data = loadimagepixelsbgra (filename, complain, allowFixtrans, false, &miplevel)))
1047 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, sRGB ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, flags, miplevel, NULL);
1052 int fixtransparentpixels(unsigned char *data, int w, int h)
1054 int const FIXTRANS_NEEDED = 1;
1055 int const FIXTRANS_HAS_L = 2;
1056 int const FIXTRANS_HAS_R = 4;
1057 int const FIXTRANS_HAS_U = 8;
1058 int const FIXTRANS_HAS_D = 16;
1059 int const FIXTRANS_FIXED = 32;
1060 unsigned char *fixMask = (unsigned char *) Mem_Alloc(tempmempool, w * h);
1062 int changedPixels = 0;
1065 #define FIXTRANS_PIXEL (y*w+x)
1066 #define FIXTRANS_PIXEL_U (((y+h-1)%h)*w+x)
1067 #define FIXTRANS_PIXEL_D (((y+1)%h)*w+x)
1068 #define FIXTRANS_PIXEL_L (y*w+((x+w-1)%w))
1069 #define FIXTRANS_PIXEL_R (y*w+((x+1)%w))
1071 memset(fixMask, 0, w * h);
1072 for(y = 0; y < h; ++y)
1073 for(x = 0; x < w; ++x)
1075 if(data[FIXTRANS_PIXEL * 4 + 3] == 0)
1077 fixMask[FIXTRANS_PIXEL] |= FIXTRANS_NEEDED;
1082 fixMask[FIXTRANS_PIXEL_D] |= FIXTRANS_HAS_U;
1083 fixMask[FIXTRANS_PIXEL_U] |= FIXTRANS_HAS_D;
1084 fixMask[FIXTRANS_PIXEL_R] |= FIXTRANS_HAS_L;
1085 fixMask[FIXTRANS_PIXEL_L] |= FIXTRANS_HAS_R;
1088 if(fixPixels == w * h)
1089 return 0; // sorry, can't do anything about this
1092 for(y = 0; y < h; ++y)
1093 for(x = 0; x < w; ++x)
1094 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_NEEDED)
1096 unsigned int sumR = 0, sumG = 0, sumB = 0, sumA = 0, sumRA = 0, sumGA = 0, sumBA = 0, cnt = 0;
1097 unsigned char r, g, b, a, r0, g0, b0;
1098 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_U)
1100 r = data[FIXTRANS_PIXEL_U * 4 + 2];
1101 g = data[FIXTRANS_PIXEL_U * 4 + 1];
1102 b = data[FIXTRANS_PIXEL_U * 4 + 0];
1103 a = data[FIXTRANS_PIXEL_U * 4 + 3];
1104 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
1106 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_D)
1108 r = data[FIXTRANS_PIXEL_D * 4 + 2];
1109 g = data[FIXTRANS_PIXEL_D * 4 + 1];
1110 b = data[FIXTRANS_PIXEL_D * 4 + 0];
1111 a = data[FIXTRANS_PIXEL_D * 4 + 3];
1112 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
1114 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_L)
1116 r = data[FIXTRANS_PIXEL_L * 4 + 2];
1117 g = data[FIXTRANS_PIXEL_L * 4 + 1];
1118 b = data[FIXTRANS_PIXEL_L * 4 + 0];
1119 a = data[FIXTRANS_PIXEL_L * 4 + 3];
1120 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
1122 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_R)
1124 r = data[FIXTRANS_PIXEL_R * 4 + 2];
1125 g = data[FIXTRANS_PIXEL_R * 4 + 1];
1126 b = data[FIXTRANS_PIXEL_R * 4 + 0];
1127 a = data[FIXTRANS_PIXEL_R * 4 + 3];
1128 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
1132 r0 = data[FIXTRANS_PIXEL * 4 + 2];
1133 g0 = data[FIXTRANS_PIXEL * 4 + 1];
1134 b0 = data[FIXTRANS_PIXEL * 4 + 0];
1137 // there is a surrounding non-alpha pixel
1138 r = (sumRA + sumA / 2) / sumA;
1139 g = (sumGA + sumA / 2) / sumA;
1140 b = (sumBA + sumA / 2) / sumA;
1144 // need to use a "regular" average
1145 r = (sumR + cnt / 2) / cnt;
1146 g = (sumG + cnt / 2) / cnt;
1147 b = (sumB + cnt / 2) / cnt;
1149 if(r != r0 || g != g0 || b != b0)
1151 data[FIXTRANS_PIXEL * 4 + 2] = r;
1152 data[FIXTRANS_PIXEL * 4 + 1] = g;
1153 data[FIXTRANS_PIXEL * 4 + 0] = b;
1154 fixMask[FIXTRANS_PIXEL] |= FIXTRANS_FIXED;
1156 for(y = 0; y < h; ++y)
1157 for(x = 0; x < w; ++x)
1158 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_FIXED)
1160 fixMask[FIXTRANS_PIXEL] &= ~(FIXTRANS_NEEDED | FIXTRANS_FIXED);
1161 fixMask[FIXTRANS_PIXEL_D] |= FIXTRANS_HAS_U;
1162 fixMask[FIXTRANS_PIXEL_U] |= FIXTRANS_HAS_D;
1163 fixMask[FIXTRANS_PIXEL_R] |= FIXTRANS_HAS_L;
1164 fixMask[FIXTRANS_PIXEL_L] |= FIXTRANS_HAS_R;
1168 return changedPixels;
1171 void Image_FixTransparentPixels_f(void)
1173 const char *filename, *filename_pattern;
1176 char outfilename[MAX_QPATH], buf[MAX_QPATH];
1177 unsigned char *data;
1180 Con_Printf("Usage: %s imagefile\n", Cmd_Argv(0));
1183 filename_pattern = Cmd_Argv(1);
1184 search = FS_Search(filename_pattern, true, true);
1187 for(i = 0; i < search->numfilenames; ++i)
1189 filename = search->filenames[i];
1190 Con_Printf("Processing %s... ", filename);
1191 Image_StripImageExtension(filename, buf, sizeof(buf));
1192 dpsnprintf(outfilename, sizeof(outfilename), "fixtrans/%s.tga", buf);
1193 if(!(data = loadimagepixelsbgra(filename, true, false, false, NULL)))
1195 if((n = fixtransparentpixels(data, image_width, image_height)))
1197 Image_WriteTGABGRA(outfilename, image_width, image_height, data);
1198 Con_Printf("%s written (%d pixels changed).\n", outfilename, n);
1201 Con_Printf("unchanged.\n");
1204 FS_FreeSearch(search);
1207 qboolean Image_WriteTGABGR_preflipped (const char *filename, int width, int height, const unsigned char *data)
1210 unsigned char buffer[18];
1211 const void *buffers[2];
1212 fs_offset_t sizes[2];
1214 memset (buffer, 0, 18);
1215 buffer[2] = 2; // uncompressed type
1216 buffer[12] = (width >> 0) & 0xFF;
1217 buffer[13] = (width >> 8) & 0xFF;
1218 buffer[14] = (height >> 0) & 0xFF;
1219 buffer[15] = (height >> 8) & 0xFF;
1220 buffer[16] = 24; // pixel size
1222 buffers[0] = buffer;
1225 sizes[1] = width*height*3;
1226 ret = FS_WriteFileInBlocks(filename, buffers, sizes, 2);
1231 qboolean Image_WriteTGABGRA (const char *filename, int width, int height, const unsigned char *data)
1234 unsigned char *buffer, *out;
1235 const unsigned char *in, *end;
1238 buffer = (unsigned char *)Mem_Alloc(tempmempool, width*height*4 + 18);
1240 memset (buffer, 0, 18);
1241 buffer[2] = 2; // uncompressed type
1242 buffer[12] = (width >> 0) & 0xFF;
1243 buffer[13] = (width >> 8) & 0xFF;
1244 buffer[14] = (height >> 0) & 0xFF;
1245 buffer[15] = (height >> 8) & 0xFF;
1247 for (y = 3;y < width*height*4;y += 4)
1251 if (y < width*height*4)
1253 // save the alpha channel
1254 buffer[16] = 32; // pixel size
1255 buffer[17] = 8; // 8 bits of alpha
1259 for (y = height - 1;y >= 0;y--)
1261 memcpy(out, data + y * width * 4, width * 4);
1267 // save only the color channels
1268 buffer[16] = 24; // pixel size
1269 buffer[17] = 0; // 8 bits of alpha
1271 // truncate bgra to bgr and flip upside down
1273 for (y = height - 1;y >= 0;y--)
1275 in = data + y * width * 4;
1276 end = in + width * 4;
1277 for (;in < end;in += 4)
1285 ret = FS_WriteFile (filename, buffer, out - buffer);
1292 static void Image_Resample32LerpLine (const unsigned char *in, unsigned char *out, int inwidth, int outwidth)
1294 int j, xi, oldx = 0, f, fstep, endx, lerp;
1295 fstep = (int) (inwidth*65536.0f/outwidth);
1297 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
1302 in += (xi - oldx) * 4;
1308 *out++ = (unsigned char) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
1309 *out++ = (unsigned char) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
1310 *out++ = (unsigned char) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
1311 *out++ = (unsigned char) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
1313 else // last pixel of the line has no pixel to lerp to
1323 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (unsigned char) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
1324 static void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1326 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
1328 const unsigned char *inrow;
1329 unsigned char *resamplerow1;
1330 unsigned char *resamplerow2;
1331 out = (unsigned char *)outdata;
1332 fstep = (int) (inheight*65536.0f/outheight);
1334 resamplerow1 = (unsigned char *)Mem_Alloc(tempmempool, outwidth*4*2);
1335 resamplerow2 = resamplerow1 + outwidth*4;
1337 inrow = (const unsigned char *)indata;
1339 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1340 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1341 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1349 inrow = (unsigned char *)indata + inwidth4*yi;
1351 memcpy(resamplerow1, resamplerow2, outwidth4);
1353 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1354 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1405 resamplerow1 -= outwidth4;
1406 resamplerow2 -= outwidth4;
1412 inrow = (unsigned char *)indata + inwidth4*yi;
1414 memcpy(resamplerow1, resamplerow2, outwidth4);
1416 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1419 memcpy(out, resamplerow1, outwidth4);
1423 Mem_Free(resamplerow1);
1424 resamplerow1 = NULL;
1425 resamplerow2 = NULL;
1428 static void Image_Resample32Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1431 unsigned frac, fracstep;
1432 // relies on int being 4 bytes
1434 out = (int *)outdata;
1436 fracstep = inwidth*0x10000/outwidth;
1437 for (i = 0;i < outheight;i++)
1439 inrow = (int *)indata + inwidth*(i*inheight/outheight);
1440 frac = fracstep >> 1;
1444 out[0] = inrow[frac >> 16];frac += fracstep;
1445 out[1] = inrow[frac >> 16];frac += fracstep;
1446 out[2] = inrow[frac >> 16];frac += fracstep;
1447 out[3] = inrow[frac >> 16];frac += fracstep;
1453 out[0] = inrow[frac >> 16];frac += fracstep;
1454 out[1] = inrow[frac >> 16];frac += fracstep;
1459 out[0] = inrow[frac >> 16];frac += fracstep;
1470 void Image_Resample32(const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int quality)
1472 if (indepth != 1 || outdepth != 1)
1474 Con_Printf ("Image_Resample: 3D resampling not supported\n");
1478 Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1480 Image_Resample32Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1483 // in can be the same as out
1484 void Image_MipReduce32(const unsigned char *in, unsigned char *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth)
1486 const unsigned char *inrow;
1488 if (*depth != 1 || destdepth != 1)
1490 Con_Printf ("Image_Resample: 3D resampling not supported\n");
1491 if (*width > destwidth)
1493 if (*height > destheight)
1495 if (*depth > destdepth)
1499 // note: if given odd width/height this discards the last row/column of
1500 // pixels, rather than doing a proper box-filter scale down
1502 nextrow = *width * 4;
1503 if (*width > destwidth)
1506 if (*height > destheight)
1510 for (y = 0;y < *height;y++, inrow += nextrow * 2)
1512 for (in = inrow, x = 0;x < *width;x++)
1514 out[0] = (unsigned char) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
1515 out[1] = (unsigned char) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1516 out[2] = (unsigned char) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1517 out[3] = (unsigned char) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1526 for (y = 0;y < *height;y++, inrow += nextrow)
1528 for (in = inrow, x = 0;x < *width;x++)
1530 out[0] = (unsigned char) ((in[0] + in[4]) >> 1);
1531 out[1] = (unsigned char) ((in[1] + in[5]) >> 1);
1532 out[2] = (unsigned char) ((in[2] + in[6]) >> 1);
1533 out[3] = (unsigned char) ((in[3] + in[7]) >> 1);
1542 if (*height > destheight)
1546 for (y = 0;y < *height;y++, inrow += nextrow * 2)
1548 for (in = inrow, x = 0;x < *width;x++)
1550 out[0] = (unsigned char) ((in[0] + in[nextrow ]) >> 1);
1551 out[1] = (unsigned char) ((in[1] + in[nextrow+1]) >> 1);
1552 out[2] = (unsigned char) ((in[2] + in[nextrow+2]) >> 1);
1553 out[3] = (unsigned char) ((in[3] + in[nextrow+3]) >> 1);
1560 Con_Printf ("Image_MipReduce: desired size already achieved\n");
1564 void Image_HeightmapToNormalmap_BGRA(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
1566 int x, y, x1, x2, y1, y2;
1567 const unsigned char *b, *row[3];
1570 float ibumpscale, n[3];
1571 ibumpscale = (255.0f * 6.0f) / bumpscale;
1573 for (y = 0, y1 = height-1;y < height;y1 = y, y++)
1575 y2 = y + 1;if (y2 >= height) y2 = 0;
1576 row[0] = inpixels + (y1 * width) * 4;
1577 row[1] = inpixels + (y * width) * 4;
1578 row[2] = inpixels + (y2 * width) * 4;
1579 for (x = 0, x1 = width-1;x < width;x1 = x, x++)
1581 x2 = x + 1;if (x2 >= width) x2 = 0;
1583 b = row[1] + x1 * 4;p[0] = (b[0] + b[1] + b[2]);
1584 b = row[1] + x2 * 4;p[1] = (b[0] + b[1] + b[2]);
1586 b = row[0] + x * 4;p[2] = (b[0] + b[1] + b[2]);
1587 b = row[2] + x * 4;p[3] = (b[0] + b[1] + b[2]);
1589 b = row[1] + x * 4;p[4] = (b[0] + b[1] + b[2]);
1590 // calculate a normal from the slopes
1595 // turn it into a dot3 rgb vector texture
1596 out[2] = (int)(128.0f + n[0] * 127.0f);
1597 out[1] = (int)(128.0f + n[1] * 127.0f);
1598 out[0] = (int)(128.0f + n[2] * 127.0f);
1599 out[3] = (p[4]) / 3;