12 static unsigned char *Image_GetEmbeddedPicBGRA(const char *name);
14 static void Image_CopyAlphaFromBlueBGRA(unsigned char *outpixels, const unsigned char *inpixels, int w, int h)
18 for(i = 0; i < n; ++i)
19 outpixels[4*i+3] = inpixels[4*i]; // blue channel
23 // written by LadyHavoc in a readable way, optimized by Vic, further optimized by LadyHavoc (the non-special index case), readable version preserved below this
24 void Image_CopyMux(unsigned char *outpixels, const unsigned char *inpixels, int inputwidth, int inputheight, qbool inputflipx, qbool inputflipy, qbool inputflipdiagonal, int numoutputcomponents, int numinputcomponents, int *outputinputcomponentindices)
27 const unsigned char *in, *line;
28 int row_inc = (inputflipy ? -inputwidth : inputwidth) * numinputcomponents, col_inc = (inputflipx ? -1 : 1) * numinputcomponents;
29 int row_ofs = (inputflipy ? (inputheight - 1) * inputwidth * numinputcomponents : 0), col_ofs = (inputflipx ? (inputwidth - 1) * numinputcomponents : 0);
31 for (c = 0; c < numoutputcomponents; c++)
32 if (outputinputcomponentindices[c] & 0x80000000)
34 if (c < numoutputcomponents)
36 // special indices used
37 if (inputflipdiagonal)
39 for (x = 0, line = inpixels + col_ofs; x < inputwidth; x++, line += col_inc)
40 for (y = 0, in = line + row_ofs; y < inputheight; y++, in += row_inc, outpixels += numoutputcomponents)
41 for (c = 0; c < numoutputcomponents; c++)
42 outpixels[c] = ((index = outputinputcomponentindices[c]) & 0x80000000) ? index : in[index];
46 for (y = 0, line = inpixels + row_ofs; y < inputheight; y++, line += row_inc)
47 for (x = 0, in = line + col_ofs; x < inputwidth; x++, in += col_inc, outpixels += numoutputcomponents)
48 for (c = 0; c < numoutputcomponents; c++)
49 outpixels[c] = ((index = outputinputcomponentindices[c]) & 0x80000000) ? index : in[index];
54 // special indices not used
55 if (inputflipdiagonal)
57 for (x = 0, line = inpixels + col_ofs; x < inputwidth; x++, line += col_inc)
58 for (y = 0, in = line + row_ofs; y < inputheight; y++, in += row_inc, outpixels += numoutputcomponents)
59 for (c = 0; c < numoutputcomponents; c++)
60 outpixels[c] = in[outputinputcomponentindices[c]];
64 for (y = 0, line = inpixels + row_ofs; y < inputheight; y++, line += row_inc)
65 for (x = 0, in = line + col_ofs; x < inputwidth; x++, in += col_inc, outpixels += numoutputcomponents)
66 for (c = 0; c < numoutputcomponents; c++)
67 outpixels[c] = in[outputinputcomponentindices[c]];
72 // intentionally readable version
73 void Image_CopyMux(unsigned char *outpixels, const unsigned char *inpixels, int inputwidth, int inputheight, qbool inputflipx, qbool inputflipy, qbool inputflipdiagonal, int numoutputcomponents, int numinputcomponents, int *outputinputcomponentindices)
76 const unsigned char *in, *inrow, *incolumn;
77 if (inputflipdiagonal)
79 for (x = 0;x < inputwidth;x++)
81 for (y = 0;y < inputheight;y++)
83 in = inpixels + ((inputflipy ? inputheight - 1 - y : y) * inputwidth + (inputflipx ? inputwidth - 1 - x : x)) * numinputcomponents;
84 for (c = 0;c < numoutputcomponents;c++)
86 index = outputinputcomponentindices[c];
87 if (index & 0x80000000)
90 *outpixels++ = in[index];
97 for (y = 0;y < inputheight;y++)
99 for (x = 0;x < inputwidth;x++)
101 in = inpixels + ((inputflipy ? inputheight - 1 - y : y) * inputwidth + (inputflipx ? inputwidth - 1 - x : x)) * numinputcomponents;
102 for (c = 0;c < numoutputcomponents;c++)
104 index = outputinputcomponentindices[c];
105 if (index & 0x80000000)
106 *outpixels++ = index;
108 *outpixels++ = in[index];
116 void Image_GammaRemapRGB(const unsigned char *in, unsigned char *out, int pixels, const unsigned char *gammar, const unsigned char *gammag, const unsigned char *gammab)
120 out[0] = gammar[in[0]];
121 out[1] = gammag[in[1]];
122 out[2] = gammab[in[2]];
128 // note: pal must be 32bit color
129 void Image_Copy8bitBGRA(const unsigned char *in, unsigned char *out, int pixels, const unsigned int *pal)
131 int *iout = (int *)out;
134 iout[0] = pal[in[0]];
135 iout[1] = pal[in[1]];
136 iout[2] = pal[in[2]];
137 iout[3] = pal[in[3]];
138 iout[4] = pal[in[4]];
139 iout[5] = pal[in[5]];
140 iout[6] = pal[in[6]];
141 iout[7] = pal[in[7]];
148 iout[0] = pal[in[0]];
149 iout[1] = pal[in[1]];
150 iout[2] = pal[in[2]];
151 iout[3] = pal[in[3]];
157 iout[0] = pal[in[0]];
158 iout[1] = pal[in[1]];
163 iout[0] = pal[in[0]];
167 =================================================================
171 =================================================================
180 unsigned short xmin,ymin,xmax,ymax;
181 unsigned short hres,vres;
182 unsigned char palette[48];
185 unsigned short bytes_per_line;
186 unsigned short palette_type;
195 static unsigned char* LoadPCX_BGRA (const unsigned char *f, int filesize, int *miplevel)
198 unsigned char *a, *b, *image_buffer, *pbuf;
199 const unsigned char *palette, *fin, *enddata;
200 int x, y, x2, dataByte;
202 if (filesize < (int)sizeof(pcx) + 768)
204 Con_Print("Bad pcx file\n");
210 memcpy(&pcx, fin, sizeof(pcx));
213 // LadyHavoc: big-endian support ported from QF newtree
214 pcx.xmax = LittleShort (pcx.xmax);
215 pcx.xmin = LittleShort (pcx.xmin);
216 pcx.ymax = LittleShort (pcx.ymax);
217 pcx.ymin = LittleShort (pcx.ymin);
218 pcx.hres = LittleShort (pcx.hres);
219 pcx.vres = LittleShort (pcx.vres);
220 pcx.bytes_per_line = LittleShort (pcx.bytes_per_line);
221 pcx.palette_type = LittleShort (pcx.palette_type);
223 image_width = pcx.xmax + 1 - pcx.xmin;
224 image_height = pcx.ymax + 1 - pcx.ymin;
225 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)
227 Con_Print("Bad pcx file\n");
231 palette = f + filesize - 768;
233 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width*image_height*4);
236 Con_Printf("LoadPCX: not enough memory for %i by %i image\n", image_width, image_height);
239 pbuf = image_buffer + image_width*image_height*3;
242 for (y = 0;y < image_height && fin < enddata;y++)
244 a = pbuf + y * image_width;
245 for (x = 0;x < image_width && fin < enddata;)
252 x2 = x + (dataByte & 0x3F);
254 if (x2 > image_width)
255 x2 = image_width; // technically an error
262 while(x < image_width)
269 for(x = 0;x < image_width*image_height;x++)
286 qbool LoadPCX_QWSkin(const unsigned char *f, int filesize, unsigned char *pixels, int outwidth, int outheight)
290 const unsigned char *fin, *enddata;
291 int x, y, x2, dataByte, pcxwidth, pcxheight;
293 if (filesize < (int)sizeof(pcx) + 768)
296 image_width = outwidth;
297 image_height = outheight;
300 memcpy(&pcx, fin, sizeof(pcx));
303 // LadyHavoc: big-endian support ported from QF newtree
304 pcx.xmax = LittleShort (pcx.xmax);
305 pcx.xmin = LittleShort (pcx.xmin);
306 pcx.ymax = LittleShort (pcx.ymax);
307 pcx.ymin = LittleShort (pcx.ymin);
308 pcx.hres = LittleShort (pcx.hres);
309 pcx.vres = LittleShort (pcx.vres);
310 pcx.bytes_per_line = LittleShort (pcx.bytes_per_line);
311 pcx.palette_type = LittleShort (pcx.palette_type);
313 pcxwidth = pcx.xmax + 1 - pcx.xmin;
314 pcxheight = pcx.ymax + 1 - pcx.ymin;
315 if (pcx.manufacturer != 0x0a || pcx.version != 5 || pcx.encoding != 1 || pcx.bits_per_pixel != 8 || pcxwidth > 4096 || pcxheight > 4096 || pcxwidth <= 0 || pcxheight <= 0)
318 enddata = f + filesize - 768;
320 for (y = 0;y < outheight && fin < enddata;y++)
322 a = pixels + y * outwidth;
323 // pad the output with blank lines if needed
326 memset(a, 0, outwidth);
329 for (x = 0;x < pcxwidth;)
336 x2 = x + (dataByte & 0x3F);
348 if (x < outwidth) // truncate to destination width
365 qbool LoadPCX_PaletteOnly(const unsigned char *f, int filesize, unsigned char *palette768b)
369 memcpy(palette768b, f + filesize - 768, 768);
374 =========================================================
378 =========================================================
381 typedef struct _TargaHeader
383 unsigned char id_length, colormap_type, image_type;
384 unsigned short colormap_index, colormap_length;
385 unsigned char colormap_size;
386 unsigned short x_origin, y_origin, width, height;
387 unsigned char pixel_size, attributes;
391 static void PrintTargaHeader(TargaHeader *t)
393 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);
401 unsigned char *LoadTGA_BGRA (const unsigned char *f, int filesize, int *miplevel)
403 int x, y, pix_inc, row_inci, runlen, alphabits;
404 unsigned char *image_buffer;
405 unsigned int *pixbufi;
406 const unsigned char *fin, *enddata;
407 TargaHeader targa_header;
408 unsigned int palettei[256];
419 enddata = f + filesize;
421 targa_header.id_length = f[0];
422 targa_header.colormap_type = f[1];
423 targa_header.image_type = f[2];
425 targa_header.colormap_index = f[3] + f[4] * 256;
426 targa_header.colormap_length = f[5] + f[6] * 256;
427 targa_header.colormap_size = f[7];
428 targa_header.x_origin = f[8] + f[9] * 256;
429 targa_header.y_origin = f[10] + f[11] * 256;
430 targa_header.width = image_width = f[12] + f[13] * 256;
431 targa_header.height = image_height = f[14] + f[15] * 256;
432 targa_header.pixel_size = f[16];
433 targa_header.attributes = f[17];
435 if (image_width > 32768 || image_height > 32768 || image_width <= 0 || image_height <= 0)
437 Con_Print("LoadTGA: invalid size\n");
438 PrintTargaHeader(&targa_header);
442 memset(palettei, 0, sizeof(palettei));
444 // advance to end of header
447 // skip TARGA image comment (usually 0 bytes)
448 fin += targa_header.id_length;
450 // read/skip the colormap if present (note: according to the TARGA spec it
451 // can be present even on truecolor or greyscale images, just not used by
453 if (targa_header.colormap_type)
455 if (targa_header.colormap_length > 256)
457 Con_Print("LoadTGA: only up to 256 colormap_length supported\n");
458 PrintTargaHeader(&targa_header);
461 if (targa_header.colormap_index)
463 Con_Print("LoadTGA: colormap_index not supported\n");
464 PrintTargaHeader(&targa_header);
467 if (targa_header.colormap_size == 24)
469 for (x = 0;x < targa_header.colormap_length;x++)
475 palettei[x] = bgra.i;
478 else if (targa_header.colormap_size == 32)
480 memcpy(palettei, fin, targa_header.colormap_length*4);
481 fin += targa_header.colormap_length * 4;
485 Con_Print("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
486 PrintTargaHeader(&targa_header);
491 // check our pixel_size restrictions according to image_type
492 switch (targa_header.image_type & ~8)
495 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
497 Con_Print("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
498 PrintTargaHeader(&targa_header);
503 // set up a palette to make the loader easier
504 for (x = 0;x < 256;x++)
506 bgra.b[0] = bgra.b[1] = bgra.b[2] = x;
508 palettei[x] = bgra.i;
510 // fall through to colormap case
512 if (targa_header.pixel_size != 8)
514 Con_Print("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
515 PrintTargaHeader(&targa_header);
520 Con_Printf("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
521 PrintTargaHeader(&targa_header);
525 if (targa_header.attributes & 0x10)
527 Con_Print("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
531 // number of attribute bits per pixel, we only support 0 or 8
532 alphabits = targa_header.attributes & 0x0F;
533 if (alphabits != 8 && alphabits != 0)
535 Con_Print("LoadTGA: only 0 or 8 attribute (alpha) bits supported\n");
539 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
542 Con_Printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
546 // If bit 5 of attributes isn't set, the image has been stored from bottom to top
547 if ((targa_header.attributes & 0x20) == 0)
549 pixbufi = (unsigned int*)image_buffer + (image_height - 1)*image_width;
550 row_inci = -image_width*2;
554 pixbufi = (unsigned int*)image_buffer;
559 if ((targa_header.image_type & ~8) == 2)
560 pix_inc = (targa_header.pixel_size + 7) / 8;
561 switch (targa_header.image_type)
563 case 1: // colormapped, uncompressed
564 case 3: // greyscale, uncompressed
565 if (fin + image_width * image_height * pix_inc > enddata)
567 for (y = 0;y < image_height;y++, pixbufi += row_inci)
568 for (x = 0;x < image_width;x++)
569 *pixbufi++ = palettei[*fin++];
572 // BGR or BGRA, uncompressed
573 if (fin + image_width * image_height * pix_inc > enddata)
575 if (targa_header.pixel_size == 32 && alphabits)
577 for (y = 0;y < image_height;y++)
578 memcpy(pixbufi + y * (image_width + row_inci), fin + y * image_width * pix_inc, image_width*4);
582 for (y = 0;y < image_height;y++, pixbufi += row_inci)
584 for (x = 0;x < image_width;x++, fin += pix_inc)
595 case 9: // colormapped, RLE
596 case 11: // greyscale, RLE
597 for (y = 0;y < image_height;y++, pixbufi += row_inci)
599 for (x = 0;x < image_width;)
602 break; // error - truncated file
606 // RLE - all pixels the same color
608 if (fin + pix_inc > enddata)
609 break; // error - truncated file
610 if (x + runlen > image_width)
611 break; // error - line exceeds width
612 bgra.i = palettei[*fin++];
618 // uncompressed - all pixels different color
620 if (fin + pix_inc * runlen > enddata)
621 break; // error - truncated file
622 if (x + runlen > image_width)
623 break; // error - line exceeds width
625 *pixbufi++ = palettei[*fin++];
629 if (x != image_width)
631 // pixbufi is useless now
632 Con_Printf("LoadTGA: corrupt file\n");
639 if (targa_header.pixel_size == 32 && alphabits)
641 for (y = 0;y < image_height;y++, pixbufi += row_inci)
643 for (x = 0;x < image_width;)
646 break; // error - truncated file
650 // RLE - all pixels the same color
652 if (fin + pix_inc > enddata)
653 break; // error - truncated file
654 if (x + runlen > image_width)
655 break; // error - line exceeds width
666 // uncompressed - all pixels different color
668 if (fin + pix_inc * runlen > enddata)
669 break; // error - truncated file
670 if (x + runlen > image_width)
671 break; // error - line exceeds width
684 if (x != image_width)
686 // pixbufi is useless now
687 Con_Printf("LoadTGA: corrupt file\n");
694 for (y = 0;y < image_height;y++, pixbufi += row_inci)
696 for (x = 0;x < image_width;)
699 break; // error - truncated file
703 // RLE - all pixels the same color
705 if (fin + pix_inc > enddata)
706 break; // error - truncated file
707 if (x + runlen > image_width)
708 break; // error - line exceeds width
719 // uncompressed - all pixels different color
721 if (fin + pix_inc * runlen > enddata)
722 break; // error - truncated file
723 if (x + runlen > image_width)
724 break; // error - line exceeds width
737 if (x != image_width)
739 // pixbufi is useless now
740 Con_Printf("LoadTGA: corrupt file\n");
747 // unknown image_type
754 typedef struct q2wal_s
757 unsigned width, height;
758 unsigned offsets[MIPLEVELS]; // four mip maps stored
759 char animname[32]; // next frame in animation chain
765 static unsigned char *LoadWAL_BGRA (const unsigned char *f, int filesize, int *miplevel)
767 unsigned char *image_buffer;
768 const q2wal_t *inwal = (const q2wal_t *)f;
770 if (filesize < (int) sizeof(q2wal_t))
772 Con_Print("LoadWAL: invalid WAL file\n");
776 image_width = LittleLong(inwal->width);
777 image_height = LittleLong(inwal->height);
778 if (image_width > 32768 || image_height > 32768 || image_width <= 0 || image_height <= 0)
780 Con_Printf("LoadWAL: invalid size %ix%i\n", image_width, image_height);
784 if (filesize < (int) LittleLong(inwal->offsets[0]) + image_width * image_height)
786 Con_Print("LoadWAL: invalid WAL file\n");
790 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
793 Con_Printf("LoadWAL: not enough memory for %i by %i image\n", image_width, image_height);
796 Image_Copy8bitBGRA(f + LittleLong(inwal->offsets[0]), image_buffer, image_width * image_height, q2palette_bgra_complete);
800 qbool LoadWAL_GetMetadata(const unsigned char *f, int filesize, int *retwidth, int *retheight, int *retflags, int *retvalue, int *retcontents, char *retanimname32c)
802 const q2wal_t *inwal = (const q2wal_t *)f;
804 if (filesize < (int) sizeof(q2wal_t))
806 Con_Print("LoadWAL: invalid WAL file\n");
818 memset(retanimname32c, 0, 32);
823 *retwidth = LittleLong(inwal->width);
825 *retheight = LittleLong(inwal->height);
827 *retflags = LittleLong(inwal->flags);
829 *retvalue = LittleLong(inwal->value);
831 *retcontents = LittleLong(inwal->contents);
834 memcpy(retanimname32c, inwal->animname, 32);
835 retanimname32c[31] = 0;
840 // gfx/* wad lumps and gfx/*.lmp files are simply width and height and paletted pixels, with color 255 as transparent
841 static unsigned char* LoadLMP_BGRA(const unsigned char *f, int filesize, int *miplevel)
843 unsigned char *image_buffer;
848 Con_Print("Bad lmp file\n");
852 image_width = f[0] + f[1] * 0x100 + f[2] * 0x10000 + f[3] * 0x1000000;
853 image_height = f[4] + f[5] * 0x100 + f[6] * 0x10000 + f[7] * 0x1000000;
855 if (image_width > 32768 || image_height > 32768 || image_width <= 0 || image_height <= 0 || image_width * image_height > filesize - 8)
857 Con_Print("Bad lmp file\n");
861 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width*image_height * 4);
864 Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
868 for (i = 0; i < image_width * image_height; i++)
870 const unsigned char *p = (const unsigned char *)palette_bgra_transparent + 4 * f[8 + i];
871 image_buffer[i * 4 + 0] = p[0];
872 image_buffer[i * 4 + 1] = p[1];
873 image_buffer[i * 4 + 2] = p[2];
874 image_buffer[i * 4 + 3] = p[3];
880 // gfx/conchars is a raw 128x128 image with 0 as transparent color rather than 255
881 static unsigned char *LoadConChars_BGRA(const unsigned char *f, int filesize, int *miplevel)
883 unsigned char *image_buffer;
888 if (image_width * image_height > filesize)
890 Con_Print("Bad lmp file\n");
894 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width*image_height * 4);
897 Con_Printf("LoadConChars: not enough memory for %i by %i image\n", image_width, image_height);
901 for (i = 0; i < image_width * image_height; i++)
903 const unsigned char *p = (const unsigned char *)palette_bgra_font + 4 * f[i];
904 image_buffer[i * 4 + 0] = p[0];
905 image_buffer[i * 4 + 1] = p[1];
906 image_buffer[i * 4 + 2] = p[2];
907 image_buffer[i * 4 + 3] = p[3];
913 void Image_StripImageExtension (const char *in, char *out, size_t size_out)
920 ext = FS_FileExtension(in);
921 if (ext && (!strcmp(ext, "tga") || !strcmp(ext, "pcx") || !strcmp(ext, "lmp") || !strcmp(ext, "png") || !strcmp(ext, "jpg") || !strcmp(ext, "wal")))
922 FS_StripExtension(in, out, size_out);
924 strlcpy(out, in, size_out);
927 static unsigned char image_linearfromsrgb[256];
928 static unsigned char image_srgbfromlinear_lightmap[256];
930 void Image_MakeLinearColorsFromsRGB(unsigned char *pout, const unsigned char *pin, int numpixels)
933 // this math from http://www.opengl.org/registry/specs/EXT/texture_sRGB.txt
934 if (!image_linearfromsrgb[255])
935 for (i = 0;i < 256;i++)
936 image_linearfromsrgb[i] = (unsigned char)floor(Image_LinearFloatFromsRGB(i) * 255.0f + 0.5f);
937 for (i = 0;i < numpixels;i++)
939 pout[i*4+0] = image_linearfromsrgb[pin[i*4+0]];
940 pout[i*4+1] = image_linearfromsrgb[pin[i*4+1]];
941 pout[i*4+2] = image_linearfromsrgb[pin[i*4+2]];
942 pout[i*4+3] = pin[i*4+3];
946 void Image_MakesRGBColorsFromLinear_Lightmap(unsigned char *pout, const unsigned char *pin, int numpixels)
949 // this math from http://www.opengl.org/registry/specs/EXT/texture_sRGB.txt
950 if (!image_srgbfromlinear_lightmap[255])
951 for (i = 0;i < 256;i++)
952 image_srgbfromlinear_lightmap[i] = (unsigned char)floor(bound(0.0f, Image_sRGBFloatFromLinear_Lightmap(i), 1.0f) * 255.0f + 0.5f);
953 for (i = 0;i < numpixels;i++)
955 pout[i*4+0] = image_srgbfromlinear_lightmap[pin[i*4+0]];
956 pout[i*4+1] = image_srgbfromlinear_lightmap[pin[i*4+1]];
957 pout[i*4+2] = image_srgbfromlinear_lightmap[pin[i*4+2]];
958 pout[i*4+3] = pin[i*4+3];
962 typedef struct imageformat_s
964 const char *formatstring;
965 unsigned char *(*loadfunc)(const unsigned char *f, int filesize, int *miplevel);
969 // GAME_TENEBRAE only
970 imageformat_t imageformats_tenebrae[] =
972 {"override/%s.tga", LoadTGA_BGRA},
973 {"override/%s.png", PNG_LoadImage_BGRA},
974 {"override/%s.jpg", JPEG_LoadImage_BGRA},
975 {"override/%s.pcx", LoadPCX_BGRA},
976 {"%s.tga", LoadTGA_BGRA},
977 {"%s.png", PNG_LoadImage_BGRA},
978 {"%s.jpg", JPEG_LoadImage_BGRA},
979 {"%s.pcx", LoadPCX_BGRA},
983 imageformat_t imageformats_nopath[] =
985 {"override/%s.tga", LoadTGA_BGRA},
986 {"override/%s.png", PNG_LoadImage_BGRA},
987 {"override/%s.jpg", JPEG_LoadImage_BGRA},
988 {"textures/%s.tga", LoadTGA_BGRA},
989 {"textures/%s.png", PNG_LoadImage_BGRA},
990 {"textures/%s.jpg", JPEG_LoadImage_BGRA},
991 {"%s.tga", LoadTGA_BGRA},
992 {"%s.png", PNG_LoadImage_BGRA},
993 {"%s.jpg", JPEG_LoadImage_BGRA},
994 {"%s.pcx", LoadPCX_BGRA},
998 // GAME_DELUXEQUAKE only
999 // VorteX: the point why i use such messy texture paths is
1000 // that GtkRadiant can't detect normal/gloss textures
1001 // and exclude them from texture browser
1002 // so i just use additional folder to store this textures
1003 imageformat_t imageformats_dq[] =
1005 {"%s.tga", LoadTGA_BGRA},
1006 {"%s.jpg", JPEG_LoadImage_BGRA},
1007 {"texturemaps/%s.tga", LoadTGA_BGRA},
1008 {"texturemaps/%s.jpg", JPEG_LoadImage_BGRA},
1012 imageformat_t imageformats_textures[] =
1014 {"%s.tga", LoadTGA_BGRA},
1015 {"%s.png", PNG_LoadImage_BGRA},
1016 {"%s.jpg", JPEG_LoadImage_BGRA},
1017 {"%s.pcx", LoadPCX_BGRA},
1018 {"%s.wal", LoadWAL_BGRA},
1022 imageformat_t imageformats_gfx[] =
1024 {"%s.tga", LoadTGA_BGRA},
1025 {"%s.png", PNG_LoadImage_BGRA},
1026 {"%s.jpg", JPEG_LoadImage_BGRA},
1027 {"%s.pcx", LoadPCX_BGRA},
1028 {"%s.lmp", LoadLMP_BGRA},
1032 imageformat_t imageformats_other[] =
1034 {"%s.tga", LoadTGA_BGRA},
1035 {"%s.png", PNG_LoadImage_BGRA},
1036 {"%s.jpg", JPEG_LoadImage_BGRA},
1037 {"%s.pcx", LoadPCX_BGRA},
1038 {"%s.lmp", LoadLMP_BGRA},
1042 int fixtransparentpixels(unsigned char *data, int w, int h);
1043 unsigned char *loadimagepixelsbgra (const char *filename, qbool complain, qbool allowFixtrans, qbool convertsRGB, int *miplevel)
1045 fs_offset_t filesize;
1046 imageformat_t *firstformat, *format;
1048 unsigned char *f, *data = NULL, *data2 = NULL;
1049 char basename[MAX_QPATH], name[MAX_QPATH], name2[MAX_QPATH], path[MAX_QPATH], afterpath[MAX_QPATH], *c;
1051 //if (developer_memorydebug.integer)
1052 // Mem_CheckSentinelsGlobal();
1053 if (developer_texturelogging.integer)
1054 Log_Printf("textures.log", "%s\n", filename);
1055 Image_StripImageExtension(filename, basename, sizeof(basename)); // strip filename extensions to allow replacement by other types
1056 // replace *'s with #, so commandline utils don't get confused when dealing with the external files
1057 for (c = basename;*c;c++)
1062 strlcpy(afterpath, basename, sizeof(afterpath));
1063 if (strchr(basename, '/'))
1066 for (i = 0;i < (int)sizeof(path)-1 && basename[i] != '/' && basename[i];i++)
1067 path[i] = basename[i];
1069 strlcpy(afterpath, basename + i + 1, sizeof(afterpath));
1071 if (gamemode == GAME_TENEBRAE)
1072 firstformat = imageformats_tenebrae;
1073 else if (gamemode == GAME_DELUXEQUAKE)
1074 firstformat = imageformats_dq;
1075 else if (!strcasecmp(path, "textures"))
1076 firstformat = imageformats_textures;
1077 else if (!strcasecmp(path, "gfx") || !strcasecmp(path, "locale")) // locale/ is used in GAME_BLOODOMNICIDE
1078 firstformat = imageformats_gfx;
1080 firstformat = imageformats_nopath;
1082 firstformat = imageformats_other;
1083 // now try all the formats in the selected list
1084 for (format = firstformat;format->formatstring;format++)
1086 dpsnprintf (name, sizeof(name), format->formatstring, basename);
1088 FS_SanitizePath(name);
1090 if(FS_FileExists(name) && (f = FS_LoadFile(name, tempmempool, true, &filesize)) != NULL)
1092 mymiplevel = miplevel ? *miplevel : 0;
1095 data = format->loadfunc(f, (int)filesize, &mymiplevel);
1099 if(format->loadfunc == JPEG_LoadImage_BGRA) // jpeg can't do alpha, so let's simulate it by loading another jpeg
1101 dpsnprintf (name2, sizeof(name2), format->formatstring, va(vabuf, sizeof(vabuf), "%s_alpha", basename));
1102 f = FS_LoadFile(name2, tempmempool, true, &filesize);
1105 int mymiplevel2 = miplevel ? *miplevel : 0;
1106 int image_width_save = image_width;
1107 int image_height_save = image_height;
1108 data2 = format->loadfunc(f, (int)filesize, &mymiplevel2);
1109 if(data2 && mymiplevel == mymiplevel2 && image_width == image_width_save && image_height == image_height_save)
1110 Image_CopyAlphaFromBlueBGRA(data, data2, image_width, image_height);
1112 Con_Printf("loadimagepixelsrgba: corrupt or invalid alpha image %s_alpha\n", basename);
1113 image_width = image_width_save;
1114 image_height = image_height_save;
1120 if (developer_loading.integer)
1121 Con_DPrintf("loaded image %s (%dx%d)\n", name, image_width, image_height);
1123 *miplevel = mymiplevel;
1124 //if (developer_memorydebug.integer)
1125 // Mem_CheckSentinelsGlobal();
1126 if(allowFixtrans && r_fixtrans_auto.integer)
1128 int n = fixtransparentpixels(data, image_width, image_height);
1131 Con_Printf("- had to fix %s (%d pixels changed)\n", name, n);
1132 if(r_fixtrans_auto.integer >= 2)
1134 char outfilename[MAX_QPATH], buf[MAX_QPATH];
1135 Image_StripImageExtension(name, buf, sizeof(buf));
1136 dpsnprintf(outfilename, sizeof(outfilename), "fixtrans/%s.tga", buf);
1137 Image_WriteTGABGRA(outfilename, image_width, image_height, data);
1138 Con_Printf("- %s written.\n", outfilename);
1143 Image_MakeLinearColorsFromsRGB(data, data, image_width * image_height);
1147 Con_DPrintf("Error loading image %s (file loaded but decode failed)\n", name);
1150 if (!strcasecmp(path, "gfx"))
1152 unsigned char *lmpdata;
1153 if ((lmpdata = W_GetLumpName(afterpath, &filesize)))
1155 if (developer_loading.integer)
1156 Con_Printf("loading gfx.wad lump \"%s\"\n", afterpath);
1158 mymiplevel = miplevel ? *miplevel : 0;
1159 if (!strcmp(afterpath, "conchars"))
1161 // conchars is a raw image and with color 0 as transparent instead of 255
1162 data = LoadConChars_BGRA(lmpdata, filesize, &mymiplevel);
1165 data = LoadLMP_BGRA(lmpdata, filesize, &mymiplevel);
1166 // no cleanup after looking up a wad lump - the whole gfx.wad is loaded at once
1169 Con_DPrintf("Error loading image %s (file loaded but decode failed)\n", name);
1173 // check if the image name exists as an embedded pic
1174 if ((data = Image_GetEmbeddedPicBGRA(basename)))
1179 Con_Printf("Couldn't load %s using ", filename);
1180 for (format = firstformat;format->formatstring;format++)
1182 dpsnprintf (name, sizeof(name), format->formatstring, basename);
1183 Con_Printf(format == firstformat ? "\"%s\"" : (format[1].formatstring ? ", \"%s\"" : " or \"%s\".\n"), format->formatstring);
1187 // texture loading can take a while, so make sure we're sending keepalives
1188 CL_KeepaliveMessage(false);
1190 //if (developer_memorydebug.integer)
1191 // Mem_CheckSentinelsGlobal();
1196 qbool Image_GetStockPicSize(const char *filename, int *returnwidth, int *returnheight)
1198 unsigned char *data;
1199 fs_offset_t filesize;
1200 char lmppath[MAX_QPATH];
1201 if (!strcasecmp(filename, "gfx/conchars"))
1204 *returnheight = 128;
1208 dpsnprintf(lmppath, sizeof(lmppath), "%s.lmp", filename);
1209 data = FS_LoadFile(lmppath, tempmempool, true, &filesize);
1214 int w = data[0] + data[1] * 0x100 + data[2] * 0x10000 + data[3] * 0x1000000;
1215 int h = data[4] + data[5] * 0x100 + data[6] * 0x10000 + data[7] * 0x1000000;
1216 if (w >= 1 && w <= 32768 && h >= 1 && h <= 32768)
1226 if (!strncasecmp(filename, "gfx/", 4))
1228 data = W_GetLumpName(filename + 4, &filesize);
1229 if (data && filesize > 8)
1231 int w = data[0] + data[1] * 0x100 + data[2] * 0x10000 + data[3] * 0x1000000;
1232 int h = data[4] + data[5] * 0x100 + data[6] * 0x10000 + data[7] * 0x1000000;
1233 if (w >= 1 && w <= 32768 && h >= 1 && h <= 32768)
1244 extern cvar_t gl_picmip;
1245 rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, qbool complain, int flags, qbool allowFixtrans, qbool sRGB)
1247 unsigned char *data;
1249 int miplevel = R_PicmipForFlags(flags);
1250 if (!(data = loadimagepixelsbgra (filename, complain, allowFixtrans, false, &miplevel)))
1252 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, sRGB ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, flags, miplevel, NULL);
1257 int fixtransparentpixels(unsigned char *data, int w, int h)
1259 int const FIXTRANS_NEEDED = 1;
1260 int const FIXTRANS_HAS_L = 2;
1261 int const FIXTRANS_HAS_R = 4;
1262 int const FIXTRANS_HAS_U = 8;
1263 int const FIXTRANS_HAS_D = 16;
1264 int const FIXTRANS_FIXED = 32;
1265 unsigned char *fixMask = (unsigned char *) Mem_Alloc(tempmempool, w * h);
1267 int changedPixels = 0;
1270 #define FIXTRANS_PIXEL (y*w+x)
1271 #define FIXTRANS_PIXEL_U (((y+h-1)%h)*w+x)
1272 #define FIXTRANS_PIXEL_D (((y+1)%h)*w+x)
1273 #define FIXTRANS_PIXEL_L (y*w+((x+w-1)%w))
1274 #define FIXTRANS_PIXEL_R (y*w+((x+1)%w))
1276 memset(fixMask, 0, w * h);
1277 for(y = 0; y < h; ++y)
1278 for(x = 0; x < w; ++x)
1280 if(data[FIXTRANS_PIXEL * 4 + 3] == 0)
1282 fixMask[FIXTRANS_PIXEL] |= FIXTRANS_NEEDED;
1287 fixMask[FIXTRANS_PIXEL_D] |= FIXTRANS_HAS_U;
1288 fixMask[FIXTRANS_PIXEL_U] |= FIXTRANS_HAS_D;
1289 fixMask[FIXTRANS_PIXEL_R] |= FIXTRANS_HAS_L;
1290 fixMask[FIXTRANS_PIXEL_L] |= FIXTRANS_HAS_R;
1293 if(fixPixels == w * h)
1294 return 0; // sorry, can't do anything about this
1297 for(y = 0; y < h; ++y)
1298 for(x = 0; x < w; ++x)
1299 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_NEEDED)
1301 unsigned int sumR = 0, sumG = 0, sumB = 0, sumA = 0, sumRA = 0, sumGA = 0, sumBA = 0, cnt = 0;
1302 unsigned char r, g, b, a, r0, g0, b0;
1303 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_U)
1305 r = data[FIXTRANS_PIXEL_U * 4 + 2];
1306 g = data[FIXTRANS_PIXEL_U * 4 + 1];
1307 b = data[FIXTRANS_PIXEL_U * 4 + 0];
1308 a = data[FIXTRANS_PIXEL_U * 4 + 3];
1309 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
1311 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_D)
1313 r = data[FIXTRANS_PIXEL_D * 4 + 2];
1314 g = data[FIXTRANS_PIXEL_D * 4 + 1];
1315 b = data[FIXTRANS_PIXEL_D * 4 + 0];
1316 a = data[FIXTRANS_PIXEL_D * 4 + 3];
1317 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
1319 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_L)
1321 r = data[FIXTRANS_PIXEL_L * 4 + 2];
1322 g = data[FIXTRANS_PIXEL_L * 4 + 1];
1323 b = data[FIXTRANS_PIXEL_L * 4 + 0];
1324 a = data[FIXTRANS_PIXEL_L * 4 + 3];
1325 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
1327 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_R)
1329 r = data[FIXTRANS_PIXEL_R * 4 + 2];
1330 g = data[FIXTRANS_PIXEL_R * 4 + 1];
1331 b = data[FIXTRANS_PIXEL_R * 4 + 0];
1332 a = data[FIXTRANS_PIXEL_R * 4 + 3];
1333 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
1337 r0 = data[FIXTRANS_PIXEL * 4 + 2];
1338 g0 = data[FIXTRANS_PIXEL * 4 + 1];
1339 b0 = data[FIXTRANS_PIXEL * 4 + 0];
1342 // there is a surrounding non-alpha pixel
1343 r = (sumRA + sumA / 2) / sumA;
1344 g = (sumGA + sumA / 2) / sumA;
1345 b = (sumBA + sumA / 2) / sumA;
1349 // need to use a "regular" average
1350 r = (sumR + cnt / 2) / cnt;
1351 g = (sumG + cnt / 2) / cnt;
1352 b = (sumB + cnt / 2) / cnt;
1354 if(r != r0 || g != g0 || b != b0)
1356 data[FIXTRANS_PIXEL * 4 + 2] = r;
1357 data[FIXTRANS_PIXEL * 4 + 1] = g;
1358 data[FIXTRANS_PIXEL * 4 + 0] = b;
1359 fixMask[FIXTRANS_PIXEL] |= FIXTRANS_FIXED;
1361 for(y = 0; y < h; ++y)
1362 for(x = 0; x < w; ++x)
1363 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_FIXED)
1365 fixMask[FIXTRANS_PIXEL] &= ~(FIXTRANS_NEEDED | FIXTRANS_FIXED);
1366 fixMask[FIXTRANS_PIXEL_D] |= FIXTRANS_HAS_U;
1367 fixMask[FIXTRANS_PIXEL_U] |= FIXTRANS_HAS_D;
1368 fixMask[FIXTRANS_PIXEL_R] |= FIXTRANS_HAS_L;
1369 fixMask[FIXTRANS_PIXEL_L] |= FIXTRANS_HAS_R;
1373 return changedPixels;
1376 void Image_FixTransparentPixels_f(cmd_state_t *cmd)
1378 const char *filename, *filename_pattern;
1381 char outfilename[MAX_QPATH], buf[MAX_QPATH];
1382 unsigned char *data;
1383 if(Cmd_Argc(cmd) != 2)
1385 Con_Printf("Usage: %s imagefile\n", Cmd_Argv(cmd, 0));
1388 filename_pattern = Cmd_Argv(cmd, 1);
1389 search = FS_Search(filename_pattern, true, true, NULL);
1392 for(i = 0; i < search->numfilenames; ++i)
1394 filename = search->filenames[i];
1395 Con_Printf("Processing %s... ", filename);
1396 Image_StripImageExtension(filename, buf, sizeof(buf));
1397 dpsnprintf(outfilename, sizeof(outfilename), "fixtrans/%s.tga", buf);
1398 if(!(data = loadimagepixelsbgra(filename, true, false, false, NULL)))
1400 if((n = fixtransparentpixels(data, image_width, image_height)))
1402 Image_WriteTGABGRA(outfilename, image_width, image_height, data);
1403 Con_Printf("%s written (%d pixels changed).\n", outfilename, n);
1406 Con_Printf("unchanged.\n");
1409 FS_FreeSearch(search);
1412 qbool Image_WriteTGABGR_preflipped (const char *filename, int width, int height, const unsigned char *data)
1415 unsigned char buffer[18];
1416 const void *buffers[2];
1417 fs_offset_t sizes[2];
1419 memset (buffer, 0, 18);
1420 buffer[2] = 2; // uncompressed type
1421 buffer[12] = (width >> 0) & 0xFF;
1422 buffer[13] = (width >> 8) & 0xFF;
1423 buffer[14] = (height >> 0) & 0xFF;
1424 buffer[15] = (height >> 8) & 0xFF;
1425 buffer[16] = 24; // pixel size
1427 buffers[0] = buffer;
1430 sizes[1] = width*height*3;
1431 ret = FS_WriteFileInBlocks(filename, buffers, sizes, 2);
1436 qbool Image_WriteTGABGRA (const char *filename, int width, int height, const unsigned char *data)
1439 unsigned char *buffer, *out;
1440 const unsigned char *in, *end;
1443 buffer = (unsigned char *)Mem_Alloc(tempmempool, width*height*4 + 18);
1445 memset (buffer, 0, 18);
1446 buffer[2] = 2; // uncompressed type
1447 buffer[12] = (width >> 0) & 0xFF;
1448 buffer[13] = (width >> 8) & 0xFF;
1449 buffer[14] = (height >> 0) & 0xFF;
1450 buffer[15] = (height >> 8) & 0xFF;
1452 for (y = 3;y < width*height*4;y += 4)
1456 if (y < width*height*4)
1458 // save the alpha channel
1459 buffer[16] = 32; // pixel size
1460 buffer[17] = 8; // 8 bits of alpha
1464 for (y = height - 1;y >= 0;y--)
1466 memcpy(out, data + y * width * 4, width * 4);
1472 // save only the color channels
1473 buffer[16] = 24; // pixel size
1474 buffer[17] = 0; // 8 bits of alpha
1476 // truncate bgra to bgr and flip upside down
1478 for (y = height - 1;y >= 0;y--)
1480 in = data + y * width * 4;
1481 end = in + width * 4;
1482 for (;in < end;in += 4)
1490 ret = FS_WriteFile (filename, buffer, out - buffer);
1497 static void Image_Resample32LerpLine (const unsigned char *in, unsigned char *out, int inwidth, int outwidth)
1499 int j, xi, oldx = 0, f, fstep, endx, lerp;
1500 fstep = (int) (inwidth*65536.0f/outwidth);
1502 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
1507 in += (xi - oldx) * 4;
1513 *out++ = (unsigned char) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
1514 *out++ = (unsigned char) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
1515 *out++ = (unsigned char) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
1516 *out++ = (unsigned char) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
1518 else // last pixel of the line has no pixel to lerp to
1528 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (unsigned char) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
1529 static void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1531 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
1533 const unsigned char *inrow;
1534 unsigned char *resamplerow1;
1535 unsigned char *resamplerow2;
1536 out = (unsigned char *)outdata;
1537 fstep = (int) (inheight*65536.0f/outheight);
1539 resamplerow1 = (unsigned char *)Mem_Alloc(tempmempool, outwidth*4*2);
1540 resamplerow2 = resamplerow1 + outwidth*4;
1542 inrow = (const unsigned char *)indata;
1544 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1545 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1546 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1554 inrow = (unsigned char *)indata + inwidth4*yi;
1556 memcpy(resamplerow1, resamplerow2, outwidth4);
1558 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1559 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1610 resamplerow1 -= outwidth4;
1611 resamplerow2 -= outwidth4;
1617 inrow = (unsigned char *)indata + inwidth4*yi;
1619 memcpy(resamplerow1, resamplerow2, outwidth4);
1621 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1624 memcpy(out, resamplerow1, outwidth4);
1628 Mem_Free(resamplerow1);
1629 resamplerow1 = NULL;
1630 resamplerow2 = NULL;
1633 static void Image_Resample32Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1636 unsigned frac, fracstep;
1637 // relies on int being 4 bytes
1639 out = (int *)outdata;
1641 fracstep = inwidth*0x10000/outwidth;
1642 for (i = 0;i < outheight;i++)
1644 inrow = (int *)indata + inwidth*(i*inheight/outheight);
1645 frac = fracstep >> 1;
1649 out[0] = inrow[frac >> 16];frac += fracstep;
1650 out[1] = inrow[frac >> 16];frac += fracstep;
1651 out[2] = inrow[frac >> 16];frac += fracstep;
1652 out[3] = inrow[frac >> 16];frac += fracstep;
1658 out[0] = inrow[frac >> 16];frac += fracstep;
1659 out[1] = inrow[frac >> 16];frac += fracstep;
1664 out[0] = inrow[frac >> 16];frac += fracstep;
1675 void Image_Resample32(const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int quality)
1677 if (indepth != 1 || outdepth != 1)
1679 Con_Printf ("Image_Resample: 3D resampling not supported\n");
1683 Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1685 Image_Resample32Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1688 // in can be the same as out
1689 void Image_MipReduce32(const unsigned char *in, unsigned char *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth)
1691 const unsigned char *inrow;
1693 if (*depth != 1 || destdepth != 1)
1695 Con_Printf ("Image_Resample: 3D resampling not supported\n");
1696 if (*width > destwidth)
1698 if (*height > destheight)
1700 if (*depth > destdepth)
1704 // note: if given odd width/height this discards the last row/column of
1705 // pixels, rather than doing a proper box-filter scale down
1707 nextrow = *width * 4;
1708 if (*width > destwidth)
1711 if (*height > destheight)
1715 for (y = 0;y < *height;y++, inrow += nextrow * 2)
1717 for (in = inrow, x = 0;x < *width;x++)
1719 out[0] = (unsigned char) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
1720 out[1] = (unsigned char) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1721 out[2] = (unsigned char) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1722 out[3] = (unsigned char) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1731 for (y = 0;y < *height;y++, inrow += nextrow)
1733 for (in = inrow, x = 0;x < *width;x++)
1735 out[0] = (unsigned char) ((in[0] + in[4]) >> 1);
1736 out[1] = (unsigned char) ((in[1] + in[5]) >> 1);
1737 out[2] = (unsigned char) ((in[2] + in[6]) >> 1);
1738 out[3] = (unsigned char) ((in[3] + in[7]) >> 1);
1747 if (*height > destheight)
1751 for (y = 0;y < *height;y++, inrow += nextrow * 2)
1753 for (in = inrow, x = 0;x < *width;x++)
1755 out[0] = (unsigned char) ((in[0] + in[nextrow ]) >> 1);
1756 out[1] = (unsigned char) ((in[1] + in[nextrow+1]) >> 1);
1757 out[2] = (unsigned char) ((in[2] + in[nextrow+2]) >> 1);
1758 out[3] = (unsigned char) ((in[3] + in[nextrow+3]) >> 1);
1765 Con_Printf ("Image_MipReduce: desired size already achieved\n");
1769 void Image_HeightmapToNormalmap_BGRA(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
1771 int x, y, x1, x2, y1, y2;
1772 const unsigned char *b, *row[3];
1775 float ibumpscale, n[3];
1776 ibumpscale = (255.0f * 6.0f) / bumpscale;
1778 for (y = 0, y1 = height-1;y < height;y1 = y, y++)
1780 y2 = y + 1;if (y2 >= height) y2 = 0;
1781 row[0] = inpixels + (y1 * width) * 4;
1782 row[1] = inpixels + (y * width) * 4;
1783 row[2] = inpixels + (y2 * width) * 4;
1784 for (x = 0, x1 = width-1;x < width;x1 = x, x++)
1786 x2 = x + 1;if (x2 >= width) x2 = 0;
1788 b = row[1] + x1 * 4;p[0] = (b[0] + b[1] + b[2]);
1789 b = row[1] + x2 * 4;p[1] = (b[0] + b[1] + b[2]);
1791 b = row[0] + x * 4;p[2] = (b[0] + b[1] + b[2]);
1792 b = row[2] + x * 4;p[3] = (b[0] + b[1] + b[2]);
1794 b = row[1] + x * 4;p[4] = (b[0] + b[1] + b[2]);
1795 // calculate a normal from the slopes
1800 // turn it into a dot3 rgb vector texture
1801 out[2] = (int)(128.0f + n[0] * 127.0f);
1802 out[1] = (int)(128.0f + n[1] * 127.0f);
1803 out[0] = (int)(128.0f + n[2] * 127.0f);
1804 out[3] = (p[4]) / 3;
1813 static unsigned char *Image_GenerateConChars(void)
1816 unsigned char *data;
1822 data = LoadTGA_BGRA(concharimage, sizeof(concharimage), NULL);
1824 for (i = 0; i < 8192; i++)
1826 random = lhrandom(0.0, 1.0);
1827 data[i * 4 + 3] = data[i * 4 + 0];
1828 data[i * 4 + 2] = 83 + (unsigned char)(random * 64);
1829 data[i * 4 + 1] = 71 + (unsigned char)(random * 32);
1830 data[i * 4 + 0] = 23 + (unsigned char)(random * 16);
1833 for (i = 8192; i < 32768; i++)
1835 random = lhrandom(0.0, 1.0);
1836 data[i * 4 + 3] = data[i * 4 + 0];
1837 data[i * 4 + 2] = 95 + (unsigned char)(random * 64);
1838 data[i * 4 + 1] = 95 + (unsigned char)(random * 64);
1839 data[i * 4 + 0] = 95 + (unsigned char)(random * 64);
1842 for (i = 32768; i < 40960; i++)
1844 random = lhrandom(0.0, 1.0);
1845 data[i * 4 + 3] = data[i * 4 + 0];
1846 data[i * 4 + 2] = 83 + (unsigned char)(random * 64);
1847 data[i * 4 + 1] = 71 + (unsigned char)(random * 32);
1848 data[i * 4 + 0] = 23 + (unsigned char)(random * 16);
1851 for (i = 40960; i < 65536; i++)
1853 random = lhrandom(0.0, 1.0);
1854 data[i * 4 + 3] = data[i * 4 + 0];
1855 data[i * 4 + 2] = 96 + (unsigned char)(random * 64);
1856 data[i * 4 + 1] = 43 + (unsigned char)(random * 32);
1857 data[i * 4 + 0] = 27 + (unsigned char)(random * 32);
1861 Image_WriteTGABGRA("gfx/generated_conchars.tga", 256, 256, data);
1867 static unsigned char *Image_GenerateDitherPattern(void)
1870 unsigned char *data = (unsigned char *)Mem_Alloc(tempmempool, 8 * 8 * 4);
1873 for (y = 0; y < 8; y++)
1875 for (x = 0; x < 8; x++)
1877 data[(y * 8 + x) * 4 + 0] = ((x^y) & 4) ? 255 : 0;
1878 data[(y * 8 + x) * 4 + 1] = ((x^y) & 4) ? 255 : 0;
1879 data[(y * 8 + x) * 4 + 2] = ((x^y) & 4) ? 255 : 0;
1880 data[(y * 8 + x) * 4 + 3] = 255;
1886 // also used in R_SkinFrame code
1887 unsigned char *Image_GenerateNoTexture(void)
1890 unsigned char *data = (unsigned char *)Mem_Alloc(tempmempool, 16 * 16 * 4);
1893 // this makes a light grey/dark grey checkerboard texture
1894 for (y = 0; y < 16; y++)
1896 for (x = 0; x < 16; x++)
1898 data[(y * 16 + x) * 4 + 0] =
1899 data[(y * 16 + x) * 4 + 1] =
1900 data[(y * 16 + x) * 4 + 2] = (y < 8) ^ (x < 8) ? 128 : 64;
1901 data[(y * 16 + x) * 4 + 3] = 255;
1907 static unsigned char *Image_GenerateWhite(void)
1909 unsigned char *data = (unsigned char *)Mem_Alloc(tempmempool, 1 * 1 * 4);
1912 data[0] = data[1] = data[2] = data[3] = 255;
1916 typedef struct embeddedpic_s
1925 static const embeddedpic_t embeddedpics[] =
1928 "gfx/prydoncursor001", 16, 16,
1947 "ui/mousepointer", 16, 16,
1966 "gfx/crosshair1", 16, 16,
1985 "gfx/crosshair2", 16, 16,
2004 "gfx/crosshair3", 16, 16,
2023 "gfx/crosshair4", 16, 16,
2042 "gfx/crosshair5", 8, 8,
2053 "gfx/crosshair6", 2, 2,
2058 "gfx/crosshair7", 16, 16,
2076 { NULL, 0, 0, NULL }
2079 unsigned char *Image_GetEmbeddedPicBGRA(const char *name)
2081 const embeddedpic_t *p;
2082 for (p = embeddedpics; p->name; p++)
2084 if (!strcmp(name, p->name))
2087 unsigned char *data = (unsigned char *)Mem_Alloc(tempmempool, p->width * p->height * 4);
2088 image_width = p->width;
2089 image_height = p->height;
2090 for (i = 0; i < p->width * p->height; i++)
2092 const unsigned char *c = (const unsigned char *)palette_bgra_embeddedpic + 4 * p->pixels[i];
2093 Vector4Copy(c, data + 4 * i);
2098 if (!strcmp(name, "white") || !strcmp(name, "#white") || !strcmp(name, "*white") || !strcmp(name, "$whiteimage"))
2099 return Image_GenerateWhite();
2100 if (!strcmp(name, "gfx/conchars"))
2101 return Image_GenerateConChars();
2102 if (!strcmp(name, "gfx/colorcontrol/ditherpattern"))
2103 return Image_GenerateDitherPattern();