11 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 unsigned char* LoadPCX_BGRA (const unsigned char *f, int filesize)
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 > 4096 || image_height > 4096 || 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
358 =========================================================
362 =========================================================
365 typedef struct _TargaHeader
367 unsigned char id_length, colormap_type, image_type;
368 unsigned short colormap_index, colormap_length;
369 unsigned char colormap_size;
370 unsigned short x_origin, y_origin, width, height;
371 unsigned char pixel_size, attributes;
375 void PrintTargaHeader(TargaHeader *t)
377 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);
385 unsigned char *LoadTGA_BGRA (const unsigned char *f, int filesize)
387 int x, y, pix_inc, row_inci, runlen, alphabits;
388 unsigned char *image_buffer;
389 unsigned int *pixbufi;
390 const unsigned char *fin, *enddata;
391 TargaHeader targa_header;
392 unsigned int palettei[256];
403 enddata = f + filesize;
405 targa_header.id_length = f[0];
406 targa_header.colormap_type = f[1];
407 targa_header.image_type = f[2];
409 targa_header.colormap_index = f[3] + f[4] * 256;
410 targa_header.colormap_length = f[5] + f[6] * 256;
411 targa_header.colormap_size = f[7];
412 targa_header.x_origin = f[8] + f[9] * 256;
413 targa_header.y_origin = f[10] + f[11] * 256;
414 targa_header.width = image_width = f[12] + f[13] * 256;
415 targa_header.height = image_height = f[14] + f[15] * 256;
416 targa_header.pixel_size = f[16];
417 targa_header.attributes = f[17];
419 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
421 Con_Print("LoadTGA: invalid size\n");
422 PrintTargaHeader(&targa_header);
426 // advance to end of header
429 // skip TARGA image comment (usually 0 bytes)
430 fin += targa_header.id_length;
432 // read/skip the colormap if present (note: according to the TARGA spec it
433 // can be present even on truecolor or greyscale images, just not used by
435 if (targa_header.colormap_type)
437 if (targa_header.colormap_length > 256)
439 Con_Print("LoadTGA: only up to 256 colormap_length supported\n");
440 PrintTargaHeader(&targa_header);
443 if (targa_header.colormap_index)
445 Con_Print("LoadTGA: colormap_index not supported\n");
446 PrintTargaHeader(&targa_header);
449 if (targa_header.colormap_size == 24)
451 for (x = 0;x < targa_header.colormap_length;x++)
457 palettei[x] = bgra.i;
460 else if (targa_header.colormap_size == 32)
462 memcpy(palettei, fin, targa_header.colormap_length*4);
463 fin += targa_header.colormap_length * 4;
467 Con_Print("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
468 PrintTargaHeader(&targa_header);
473 // check our pixel_size restrictions according to image_type
474 switch (targa_header.image_type & ~8)
477 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
479 Con_Print("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
480 PrintTargaHeader(&targa_header);
485 // set up a palette to make the loader easier
486 for (x = 0;x < 256;x++)
488 bgra.b[0] = bgra.b[1] = bgra.b[2] = x;
490 palettei[x] = bgra.i;
492 // fall through to colormap case
494 if (targa_header.pixel_size != 8)
496 Con_Print("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
497 PrintTargaHeader(&targa_header);
502 Con_Printf("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
503 PrintTargaHeader(&targa_header);
507 if (targa_header.attributes & 0x10)
509 Con_Print("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
513 // number of attribute bits per pixel, we only support 0 or 8
514 alphabits = targa_header.attributes & 0x0F;
515 if (alphabits != 8 && alphabits != 0)
517 Con_Print("LoadTGA: only 0 or 8 attribute (alpha) bits supported\n");
521 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
524 Con_Printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
528 // If bit 5 of attributes isn't set, the image has been stored from bottom to top
529 if ((targa_header.attributes & 0x20) == 0)
531 pixbufi = (unsigned int*)image_buffer + (image_height - 1)*image_width;
532 row_inci = -image_width*2;
536 pixbufi = (unsigned int*)image_buffer;
543 if ((targa_header.image_type & ~8) == 2)
544 pix_inc = (targa_header.pixel_size + 7) / 8;
545 switch (targa_header.image_type)
547 case 1: // colormapped, uncompressed
548 case 3: // greyscale, uncompressed
549 if (fin + image_width * image_height * pix_inc > enddata)
551 for (y = 0;y < image_height;y++, pixbufi += row_inci)
552 for (x = 0;x < image_width;x++)
553 *pixbufi++ = palettei[*fin++];
556 // BGR or BGRA, uncompressed
557 if (fin + image_width * image_height * pix_inc > enddata)
559 if (targa_header.pixel_size == 32 && alphabits)
561 for (y = 0;y < image_height;y++)
562 memcpy(pixbufi + y * (image_width + row_inci), fin + y * image_width * pix_inc, image_width*4);
566 for (y = 0;y < image_height;y++, pixbufi += row_inci)
568 for (x = 0;x < image_width;x++, fin += pix_inc)
579 case 9: // colormapped, RLE
580 case 11: // greyscale, RLE
581 for (y = 0;y < image_height;y++, pixbufi += row_inci)
583 for (x = 0;x < image_width;)
586 break; // error - truncated file
590 // RLE - all pixels the same color
592 if (fin + pix_inc > enddata)
593 break; // error - truncated file
594 if (x + runlen > image_width)
595 break; // error - line exceeds width
596 bgra.i = palettei[*fin++];
602 // uncompressed - all pixels different color
604 if (fin + pix_inc * runlen > enddata)
605 break; // error - truncated file
606 if (x + runlen > image_width)
607 break; // error - line exceeds width
609 *pixbufi++ = palettei[*fin++];
616 if (targa_header.pixel_size == 32 && alphabits)
618 for (y = 0;y < image_height;y++, pixbufi += row_inci)
620 for (x = 0;x < image_width;)
623 break; // error - truncated file
627 // RLE - all pixels the same color
629 if (fin + pix_inc > enddata)
630 break; // error - truncated file
631 if (x + runlen > image_width)
632 break; // error - line exceeds width
643 // uncompressed - all pixels different color
645 if (fin + pix_inc * runlen > enddata)
646 break; // error - truncated file
647 if (x + runlen > image_width)
648 break; // error - line exceeds width
664 for (y = 0;y < image_height;y++, pixbufi += row_inci)
666 for (x = 0;x < image_width;)
669 break; // error - truncated file
673 // RLE - all pixels the same color
675 if (fin + pix_inc > enddata)
676 break; // error - truncated file
677 if (x + runlen > image_width)
678 break; // error - line exceeds width
689 // uncompressed - all pixels different color
691 if (fin + pix_inc * runlen > enddata)
692 break; // error - truncated file
693 if (x + runlen > image_width)
694 break; // error - line exceeds width
710 // unknown image_type
717 typedef struct q2wal_s
720 unsigned width, height;
721 unsigned offsets[MIPLEVELS]; // four mip maps stored
722 char animname[32]; // next frame in animation chain
728 unsigned char *LoadWAL_BGRA (const unsigned char *f, int filesize)
730 unsigned char *image_buffer;
731 const q2wal_t *inwal = (const q2wal_t *)f;
733 if (filesize < (int) sizeof(q2wal_t))
735 Con_Print("LoadWAL: invalid WAL file\n");
739 image_width = LittleLong(inwal->width);
740 image_height = LittleLong(inwal->height);
741 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
743 Con_Printf("LoadWAL: invalid size %ix%i\n", image_width, image_height);
747 if (filesize < (int) sizeof(q2wal_t) + (int) LittleLong(inwal->offsets[0]) + image_width * image_height)
749 Con_Print("LoadWAL: invalid WAL file\n");
753 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
756 Con_Printf("LoadWAL: not enough memory for %i by %i image\n", image_width, image_height);
759 Image_Copy8bitBGRA(f + LittleLong(inwal->offsets[0]), image_buffer, image_width * image_height, palette_bgra_complete);
764 void Image_StripImageExtension (const char *in, char *out, size_t size_out)
771 ext = FS_FileExtension(in);
772 if (ext && (!strcmp(ext, "tga") || !strcmp(ext, "pcx") || !strcmp(ext, "lmp") || !strcmp(ext, "png") || !strcmp(ext, "jpg")))
773 FS_StripExtension(in, out, size_out);
775 strlcpy(out, in, size_out);
778 typedef struct imageformat_s
780 const char *formatstring;
781 unsigned char *(*loadfunc)(const unsigned char *f, int filesize);
785 // GAME_TENEBRAE only
786 imageformat_t imageformats_tenebrae[] =
788 {"override/%s.tga", LoadTGA_BGRA},
789 {"override/%s.png", PNG_LoadImage_BGRA},
790 {"override/%s.jpg", JPEG_LoadImage_BGRA},
791 {"override/%s.pcx", LoadPCX_BGRA},
792 {"%s.tga", LoadTGA_BGRA},
793 {"%s.png", PNG_LoadImage_BGRA},
794 {"%s.jpg", JPEG_LoadImage_BGRA},
795 {"%s.pcx", LoadPCX_BGRA},
799 imageformat_t imageformats_nopath[] =
801 {"override/%s.tga", LoadTGA_BGRA},
802 {"override/%s.png", PNG_LoadImage_BGRA},
803 {"override/%s.jpg", JPEG_LoadImage_BGRA},
804 {"textures/%s.tga", LoadTGA_BGRA},
805 {"textures/%s.png", PNG_LoadImage_BGRA},
806 {"textures/%s.jpg", JPEG_LoadImage_BGRA},
807 {"%s.tga", LoadTGA_BGRA},
808 {"%s.png", PNG_LoadImage_BGRA},
809 {"%s.jpg", JPEG_LoadImage_BGRA},
810 {"%s.pcx", LoadPCX_BGRA},
814 // GAME_DELUXEQUAKE only
815 // VorteX: the point why i use such messy texture paths is
816 // that GtkRadiant can't detect normal/gloss textures
817 // and exclude them from texture browser
818 // so i just use additional folder to store this textures
819 imageformat_t imageformats_dq[] =
821 {"%s.tga", LoadTGA_BGRA},
822 {"%s.jpg", JPEG_LoadImage_BGRA},
823 {"texturemaps/%s.tga", LoadTGA_BGRA},
824 {"texturemaps/%s.jpg", JPEG_LoadImage_BGRA},
828 imageformat_t imageformats_textures[] =
830 {"%s.tga", LoadTGA_BGRA},
831 {"%s.png", PNG_LoadImage_BGRA},
832 {"%s.jpg", JPEG_LoadImage_BGRA},
833 {"%s.pcx", LoadPCX_BGRA},
834 {"%s.wal", LoadWAL_BGRA},
838 imageformat_t imageformats_gfx[] =
840 {"%s.tga", LoadTGA_BGRA},
841 {"%s.png", PNG_LoadImage_BGRA},
842 {"%s.jpg", JPEG_LoadImage_BGRA},
843 {"%s.pcx", LoadPCX_BGRA},
847 imageformat_t imageformats_other[] =
849 {"%s.tga", LoadTGA_BGRA},
850 {"%s.png", PNG_LoadImage_BGRA},
851 {"%s.jpg", JPEG_LoadImage_BGRA},
852 {"%s.pcx", LoadPCX_BGRA},
856 int fixtransparentpixels(unsigned char *data, int w, int h);
857 unsigned char *loadimagepixelsbgra (const char *filename, qboolean complain, qboolean allowFixtrans)
859 fs_offset_t filesize;
860 imageformat_t *firstformat, *format;
861 unsigned char *f, *data = NULL, *data2 = NULL;
862 char basename[MAX_QPATH], name[MAX_QPATH], name2[MAX_QPATH], *c;
863 //if (developer_memorydebug.integer)
864 // Mem_CheckSentinelsGlobal();
865 if (developer_texturelogging.integer)
866 Log_Printf("textures.log", "%s\n", filename);
867 Image_StripImageExtension(filename, basename, sizeof(basename)); // strip filename extensions to allow replacement by other types
868 // replace *'s with #, so commandline utils don't get confused when dealing with the external files
869 for (c = basename;*c;c++)
873 if (strchr(basename, '/'))
876 for (i = 0;i < (int)sizeof(name)-1 && basename[i] != '/';i++)
877 name[i] = basename[i];
880 if (gamemode == GAME_TENEBRAE)
881 firstformat = imageformats_tenebrae;
882 else if (gamemode == GAME_DELUXEQUAKE)
883 firstformat = imageformats_dq;
884 else if (!strcasecmp(name, "textures"))
885 firstformat = imageformats_textures;
886 else if (!strcasecmp(name, "gfx"))
887 firstformat = imageformats_gfx;
888 else if (!strchr(basename, '/'))
889 firstformat = imageformats_nopath;
891 firstformat = imageformats_other;
892 // now try all the formats in the selected list
893 for (format = firstformat;format->formatstring;format++)
895 dpsnprintf (name, sizeof(name), format->formatstring, basename);
896 f = FS_LoadFile(name, tempmempool, true, &filesize);
899 data = format->loadfunc(f, filesize);
901 if(format->loadfunc == JPEG_LoadImage_BGRA) // jpeg can't do alpha, so let's simulate it by loading another jpeg
903 dpsnprintf (name2, sizeof(name2), format->formatstring, va("%s_alpha", basename));
904 f = FS_LoadFile(name2, tempmempool, true, &filesize);
907 data2 = format->loadfunc(f, filesize);
909 Image_CopyAlphaFromBlueBGRA(data, data2, image_width, image_height);
915 if (developer.integer >= 10)
916 Con_Printf("loaded image %s (%dx%d)\n", name, image_width, image_height);
917 //if (developer_memorydebug.integer)
918 // Mem_CheckSentinelsGlobal();
919 if(allowFixtrans && r_fixtrans_auto.integer)
921 int n = fixtransparentpixels(data, image_width, image_height);
924 Con_Printf("- had to fix %s (%d pixels changed)\n", name, n);
925 if(r_fixtrans_auto.integer >= 2)
927 char outfilename[MAX_QPATH], buf[MAX_QPATH];
928 Image_StripImageExtension(name, buf, sizeof(buf));
929 dpsnprintf(outfilename, sizeof(outfilename), "fixtrans/%s.tga", buf);
930 Image_WriteTGABGRA(outfilename, image_width, image_height, data);
931 Con_Printf("- %s written.\n", outfilename);
938 Con_DPrintf("Error loading image %s (file loaded but decode failed)\n", name);
943 Con_Printf("Couldn't load %s using ", filename);
944 for (format = firstformat;format->formatstring;format++)
946 dpsnprintf (name, sizeof(name), format->formatstring, basename);
947 Con_Printf(format == firstformat ? "\"%s\"" : (format[1].formatstring ? ", \"%s\"" : " or \"%s\".\n"), format->formatstring);
951 // texture loading can take a while, so make sure we're sending keepalives
952 CL_KeepaliveMessage(false);
954 //if (developer_memorydebug.integer)
955 // Mem_CheckSentinelsGlobal();
959 rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, qboolean complain, int flags, qboolean allowFixtrans)
963 if (!(data = loadimagepixelsbgra (filename, complain, allowFixtrans)))
965 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_BGRA, flags, NULL);
970 int fixtransparentpixels(unsigned char *data, int w, int h)
972 int const FIXTRANS_NEEDED = 1;
973 int const FIXTRANS_HAS_L = 2;
974 int const FIXTRANS_HAS_R = 4;
975 int const FIXTRANS_HAS_U = 8;
976 int const FIXTRANS_HAS_D = 16;
977 int const FIXTRANS_FIXED = 32;
978 unsigned char *fixMask = (unsigned char *) Mem_Alloc(tempmempool, w * h);
980 int changedPixels = 0;
983 #define FIXTRANS_PIXEL (y*w+x)
984 #define FIXTRANS_PIXEL_U (((y+h-1)%h)*w+x)
985 #define FIXTRANS_PIXEL_D (((y+1)%h)*w+x)
986 #define FIXTRANS_PIXEL_L (y*w+((x+w-1)%w))
987 #define FIXTRANS_PIXEL_R (y*w+((x+1)%w))
989 memset(fixMask, 0, w * h);
990 for(y = 0; y < h; ++y)
991 for(x = 0; x < w; ++x)
993 if(data[FIXTRANS_PIXEL * 4 + 3] == 0)
995 fixMask[FIXTRANS_PIXEL] |= FIXTRANS_NEEDED;
1000 fixMask[FIXTRANS_PIXEL_D] |= FIXTRANS_HAS_U;
1001 fixMask[FIXTRANS_PIXEL_U] |= FIXTRANS_HAS_D;
1002 fixMask[FIXTRANS_PIXEL_R] |= FIXTRANS_HAS_L;
1003 fixMask[FIXTRANS_PIXEL_L] |= FIXTRANS_HAS_R;
1006 if(fixPixels == w * h)
1007 return 0; // sorry, can't do anything about this
1010 for(y = 0; y < h; ++y)
1011 for(x = 0; x < w; ++x)
1012 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_NEEDED)
1014 unsigned int sumR = 0, sumG = 0, sumB = 0, sumA = 0, sumRA = 0, sumGA = 0, sumBA = 0, cnt = 0;
1015 unsigned char r, g, b, a, r0, g0, b0;
1016 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_U)
1018 r = data[FIXTRANS_PIXEL_U * 4 + 2];
1019 g = data[FIXTRANS_PIXEL_U * 4 + 1];
1020 b = data[FIXTRANS_PIXEL_U * 4 + 0];
1021 a = data[FIXTRANS_PIXEL_U * 4 + 3];
1022 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
1024 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_D)
1026 r = data[FIXTRANS_PIXEL_D * 4 + 2];
1027 g = data[FIXTRANS_PIXEL_D * 4 + 1];
1028 b = data[FIXTRANS_PIXEL_D * 4 + 0];
1029 a = data[FIXTRANS_PIXEL_D * 4 + 3];
1030 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
1032 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_L)
1034 r = data[FIXTRANS_PIXEL_L * 4 + 2];
1035 g = data[FIXTRANS_PIXEL_L * 4 + 1];
1036 b = data[FIXTRANS_PIXEL_L * 4 + 0];
1037 a = data[FIXTRANS_PIXEL_L * 4 + 3];
1038 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
1040 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_R)
1042 r = data[FIXTRANS_PIXEL_R * 4 + 2];
1043 g = data[FIXTRANS_PIXEL_R * 4 + 1];
1044 b = data[FIXTRANS_PIXEL_R * 4 + 0];
1045 a = data[FIXTRANS_PIXEL_R * 4 + 3];
1046 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
1050 r0 = data[FIXTRANS_PIXEL * 4 + 2];
1051 g0 = data[FIXTRANS_PIXEL * 4 + 1];
1052 b0 = data[FIXTRANS_PIXEL * 4 + 0];
1055 // there is a surrounding non-alpha pixel
1056 r = (sumRA + sumA / 2) / sumA;
1057 g = (sumGA + sumA / 2) / sumA;
1058 b = (sumBA + sumA / 2) / sumA;
1062 // need to use a "regular" average
1063 r = (sumR + cnt / 2) / cnt;
1064 g = (sumG + cnt / 2) / cnt;
1065 b = (sumB + cnt / 2) / cnt;
1067 if(r != r0 || g != g0 || b != b0)
1069 data[FIXTRANS_PIXEL * 4 + 2] = r;
1070 data[FIXTRANS_PIXEL * 4 + 1] = g;
1071 data[FIXTRANS_PIXEL * 4 + 0] = b;
1072 fixMask[FIXTRANS_PIXEL] |= FIXTRANS_FIXED;
1074 for(y = 0; y < h; ++y)
1075 for(x = 0; x < w; ++x)
1076 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_FIXED)
1078 fixMask[FIXTRANS_PIXEL] &= ~(FIXTRANS_NEEDED | FIXTRANS_FIXED);
1079 fixMask[FIXTRANS_PIXEL_D] |= FIXTRANS_HAS_U;
1080 fixMask[FIXTRANS_PIXEL_U] |= FIXTRANS_HAS_D;
1081 fixMask[FIXTRANS_PIXEL_R] |= FIXTRANS_HAS_L;
1082 fixMask[FIXTRANS_PIXEL_L] |= FIXTRANS_HAS_R;
1086 return changedPixels;
1089 void Image_FixTransparentPixels_f(void)
1091 const char *filename, *filename_pattern;
1094 char outfilename[MAX_QPATH], buf[MAX_QPATH];
1095 unsigned char *data;
1098 Con_Printf("Usage: %s imagefile\n", Cmd_Argv(0));
1101 filename_pattern = Cmd_Argv(1);
1102 search = FS_Search(filename_pattern, true, true);
1105 for(i = 0; i < search->numfilenames; ++i)
1107 filename = search->filenames[i];
1108 Con_Printf("Processing %s... ", filename);
1109 Image_StripImageExtension(filename, buf, sizeof(buf));
1110 dpsnprintf(outfilename, sizeof(outfilename), "fixtrans/%s.tga", buf);
1111 if(!(data = loadimagepixelsbgra(filename, true, false)))
1113 if((n = fixtransparentpixels(data, image_width, image_height)))
1115 Image_WriteTGABGRA(outfilename, image_width, image_height, data);
1116 Con_Printf("%s written (%d pixels changed).\n", outfilename, n);
1119 Con_Printf("unchanged.\n");
1122 FS_FreeSearch(search);
1125 qboolean Image_WriteTGABGR_preflipped (const char *filename, int width, int height, const unsigned char *data, unsigned char *buffer)
1129 memset (buffer, 0, 18);
1130 buffer[2] = 2; // uncompressed type
1131 buffer[12] = (width >> 0) & 0xFF;
1132 buffer[13] = (width >> 8) & 0xFF;
1133 buffer[14] = (height >> 0) & 0xFF;
1134 buffer[15] = (height >> 8) & 0xFF;
1135 buffer[16] = 24; // pixel size
1138 memcpy(buffer + 18, data, width*height*3);
1139 ret = FS_WriteFile (filename, buffer, width*height*3 + 18 );
1144 void Image_WriteTGABGRA (const char *filename, int width, int height, const unsigned char *data)
1147 unsigned char *buffer, *out;
1148 const unsigned char *in, *end;
1150 buffer = (unsigned char *)Mem_Alloc(tempmempool, width*height*4 + 18);
1152 memset (buffer, 0, 18);
1153 buffer[2] = 2; // uncompressed type
1154 buffer[12] = (width >> 0) & 0xFF;
1155 buffer[13] = (width >> 8) & 0xFF;
1156 buffer[14] = (height >> 0) & 0xFF;
1157 buffer[15] = (height >> 8) & 0xFF;
1159 for (y = 3;y < width*height*4;y += 4)
1163 if (y < width*height*4)
1165 // save the alpha channel
1166 buffer[16] = 32; // pixel size
1167 buffer[17] = 8; // 8 bits of alpha
1171 for (y = height - 1;y >= 0;y--)
1173 memcpy(out, data + y * width * 4, width * 4);
1179 // save only the color channels
1180 buffer[16] = 24; // pixel size
1181 buffer[17] = 0; // 8 bits of alpha
1183 // truncate bgra to bgr and flip upside down
1185 for (y = height - 1;y >= 0;y--)
1187 in = data + y * width * 4;
1188 end = in + width * 4;
1189 for (;in < end;in += 4)
1197 FS_WriteFile (filename, buffer, out - buffer);
1202 static void Image_Resample32LerpLine (const unsigned char *in, unsigned char *out, int inwidth, int outwidth)
1204 int j, xi, oldx = 0, f, fstep, endx, lerp;
1205 fstep = (int) (inwidth*65536.0f/outwidth);
1207 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
1212 in += (xi - oldx) * 4;
1218 *out++ = (unsigned char) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
1219 *out++ = (unsigned char) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
1220 *out++ = (unsigned char) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
1221 *out++ = (unsigned char) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
1223 else // last pixel of the line has no pixel to lerp to
1233 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (unsigned char) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
1234 void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1236 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
1238 const unsigned char *inrow;
1239 unsigned char *resamplerow1;
1240 unsigned char *resamplerow2;
1241 out = (unsigned char *)outdata;
1242 fstep = (int) (inheight*65536.0f/outheight);
1244 resamplerow1 = (unsigned char *)Mem_Alloc(tempmempool, outwidth*4*2);
1245 resamplerow2 = resamplerow1 + outwidth*4;
1247 inrow = (const unsigned char *)indata;
1249 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1250 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1251 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1259 inrow = (unsigned char *)indata + inwidth4*yi;
1261 memcpy(resamplerow1, resamplerow2, outwidth4);
1263 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1264 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1315 resamplerow1 -= outwidth4;
1316 resamplerow2 -= outwidth4;
1322 inrow = (unsigned char *)indata + inwidth4*yi;
1324 memcpy(resamplerow1, resamplerow2, outwidth4);
1326 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1329 memcpy(out, resamplerow1, outwidth4);
1333 Mem_Free(resamplerow1);
1334 resamplerow1 = NULL;
1335 resamplerow2 = NULL;
1338 void Image_Resample32Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1341 unsigned frac, fracstep;
1342 // relies on int being 4 bytes
1344 out = (int *)outdata;
1346 fracstep = inwidth*0x10000/outwidth;
1347 for (i = 0;i < outheight;i++)
1349 inrow = (int *)indata + inwidth*(i*inheight/outheight);
1350 frac = fracstep >> 1;
1354 out[0] = inrow[frac >> 16];frac += fracstep;
1355 out[1] = inrow[frac >> 16];frac += fracstep;
1356 out[2] = inrow[frac >> 16];frac += fracstep;
1357 out[3] = inrow[frac >> 16];frac += fracstep;
1363 out[0] = inrow[frac >> 16];frac += fracstep;
1364 out[1] = inrow[frac >> 16];frac += fracstep;
1369 out[0] = inrow[frac >> 16];frac += fracstep;
1380 void Image_Resample32(const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int quality)
1382 if (indepth != 1 || outdepth != 1)
1384 Con_Printf ("Image_Resample: 3D resampling not supported\n");
1388 Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1390 Image_Resample32Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1393 // in can be the same as out
1394 void Image_MipReduce32(const unsigned char *in, unsigned char *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth)
1396 const unsigned char *inrow;
1398 if (*depth != 1 || destdepth != 1)
1400 Con_Printf ("Image_Resample: 3D resampling not supported\n");
1401 if (*width > destwidth)
1403 if (*height > destheight)
1405 if (*depth > destdepth)
1409 // note: if given odd width/height this discards the last row/column of
1410 // pixels, rather than doing a proper box-filter scale down
1412 nextrow = *width * 4;
1413 if (*width > destwidth)
1416 if (*height > destheight)
1420 for (y = 0;y < *height;y++, inrow += nextrow * 2)
1422 for (in = inrow, x = 0;x < *width;x++)
1424 out[0] = (unsigned char) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
1425 out[1] = (unsigned char) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1426 out[2] = (unsigned char) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1427 out[3] = (unsigned char) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1436 for (y = 0;y < *height;y++, inrow += nextrow)
1438 for (in = inrow, x = 0;x < *width;x++)
1440 out[0] = (unsigned char) ((in[0] + in[4]) >> 1);
1441 out[1] = (unsigned char) ((in[1] + in[5]) >> 1);
1442 out[2] = (unsigned char) ((in[2] + in[6]) >> 1);
1443 out[3] = (unsigned char) ((in[3] + in[7]) >> 1);
1452 if (*height > destheight)
1456 for (y = 0;y < *height;y++, inrow += nextrow * 2)
1458 for (in = inrow, x = 0;x < *width;x++)
1460 out[0] = (unsigned char) ((in[0] + in[nextrow ]) >> 1);
1461 out[1] = (unsigned char) ((in[1] + in[nextrow+1]) >> 1);
1462 out[2] = (unsigned char) ((in[2] + in[nextrow+2]) >> 1);
1463 out[3] = (unsigned char) ((in[3] + in[nextrow+3]) >> 1);
1470 Con_Printf ("Image_MipReduce: desired size already achieved\n");
1474 void Image_HeightmapToNormalmap_BGRA(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
1476 int x, y, x1, x2, y1, y2;
1477 const unsigned char *b, *row[3];
1480 float iwidth, iheight, ibumpscale, n[3];
1481 iwidth = 1.0f / width;
1482 iheight = 1.0f / height;
1483 ibumpscale = (255.0f * 6.0f) / bumpscale;
1485 for (y = 0, y1 = height-1;y < height;y1 = y, y++)
1487 y2 = y + 1;if (y2 >= height) y2 = 0;
1488 row[0] = inpixels + (y1 * width) * 4;
1489 row[1] = inpixels + (y * width) * 4;
1490 row[2] = inpixels + (y2 * width) * 4;
1491 for (x = 0, x1 = width-1;x < width;x1 = x, x++)
1493 x2 = x + 1;if (x2 >= width) x2 = 0;
1495 b = row[1] + x1 * 4;p[0] = (b[0] + b[1] + b[2]);
1496 b = row[1] + x2 * 4;p[1] = (b[0] + b[1] + b[2]);
1498 b = row[0] + x * 4;p[2] = (b[0] + b[1] + b[2]);
1499 b = row[2] + x * 4;p[3] = (b[0] + b[1] + b[2]);
1501 b = row[1] + x * 4;p[4] = (b[0] + b[1] + b[2]);
1502 // calculate a normal from the slopes
1507 // turn it into a dot3 rgb vector texture
1508 out[2] = (int)(128.0f + n[0] * 127.0f);
1509 out[1] = (int)(128.0f + n[1] * 127.0f);
1510 out[0] = (int)(128.0f + n[2] * 127.0f);
1511 out[3] = (p[4]) / 3;