12 // written by LordHavoc in a readable way, optimized by Vic, further optimized by LordHavoc (the non-special index case), readable version preserved below this
13 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)
16 const unsigned char *in, *line;
17 int row_inc = (inputflipy ? -inputwidth : inputwidth) * numinputcomponents, col_inc = (inputflipx ? -1 : 1) * numinputcomponents;
18 int row_ofs = (inputflipy ? (inputheight - 1) * inputwidth * numinputcomponents : 0), col_ofs = (inputflipx ? (inputwidth - 1) * numinputcomponents : 0);
20 for (c = 0; c < numoutputcomponents; c++)
21 if (outputinputcomponentindices[c] & 0x80000000)
23 if (c < numoutputcomponents)
25 // special indices used
26 if (inputflipdiagonal)
28 for (x = 0, line = inpixels + col_ofs; x < inputwidth; x++, line += col_inc)
29 for (y = 0, in = line + row_ofs; y < inputheight; y++, in += row_inc, outpixels += numinputcomponents)
30 for (c = 0; c < numoutputcomponents; c++)
31 outpixels[c] = ((index = outputinputcomponentindices[c]) & 0x80000000) ? index : in[index];
35 for (y = 0, line = inpixels + row_ofs; y < inputheight; y++, line += row_inc)
36 for (x = 0, in = line + col_ofs; x < inputwidth; x++, in += col_inc, outpixels += numinputcomponents)
37 for (c = 0; c < numoutputcomponents; c++)
38 outpixels[c] = ((index = outputinputcomponentindices[c]) & 0x80000000) ? index : in[index];
43 // special indices not used
44 if (inputflipdiagonal)
46 for (x = 0, line = inpixels + col_ofs; x < inputwidth; x++, line += col_inc)
47 for (y = 0, in = line + row_ofs; y < inputheight; y++, in += row_inc, outpixels += numinputcomponents)
48 for (c = 0; c < numoutputcomponents; c++)
49 outpixels[c] = in[outputinputcomponentindices[c]];
53 for (y = 0, line = inpixels + row_ofs; y < inputheight; y++, line += row_inc)
54 for (x = 0, in = line + col_ofs; x < inputwidth; x++, in += col_inc, outpixels += numinputcomponents)
55 for (c = 0; c < numoutputcomponents; c++)
56 outpixels[c] = in[outputinputcomponentindices[c]];
61 // intentionally readable version
62 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)
65 const unsigned char *in, *inrow, *incolumn;
66 if (inputflipdiagonal)
68 for (x = 0;x < inputwidth;x++)
70 for (y = 0;y < inputheight;y++)
72 in = inpixels + ((inputflipy ? inputheight - 1 - y : y) * inputwidth + (inputflipx ? inputwidth - 1 - x : x)) * numinputcomponents;
73 for (c = 0;c < numoutputcomponents;c++)
75 index = outputinputcomponentindices[c];
76 if (index & 0x80000000)
79 *outpixels++ = in[index];
86 for (y = 0;y < inputheight;y++)
88 for (x = 0;x < inputwidth;x++)
90 in = inpixels + ((inputflipy ? inputheight - 1 - y : y) * inputwidth + (inputflipx ? inputwidth - 1 - x : x)) * numinputcomponents;
91 for (c = 0;c < numoutputcomponents;c++)
93 index = outputinputcomponentindices[c];
94 if (index & 0x80000000)
97 *outpixels++ = in[index];
105 void Image_GammaRemapRGB(const unsigned char *in, unsigned char *out, int pixels, const unsigned char *gammar, const unsigned char *gammag, const unsigned char *gammab)
109 out[0] = gammar[in[0]];
110 out[1] = gammag[in[1]];
111 out[2] = gammab[in[2]];
117 // note: pal must be 32bit color
118 void Image_Copy8bitBGRA(const unsigned char *in, unsigned char *out, int pixels, const unsigned int *pal)
120 int *iout = (int *)out;
123 iout[0] = pal[in[0]];
124 iout[1] = pal[in[1]];
125 iout[2] = pal[in[2]];
126 iout[3] = pal[in[3]];
127 iout[4] = pal[in[4]];
128 iout[5] = pal[in[5]];
129 iout[6] = pal[in[6]];
130 iout[7] = pal[in[7]];
137 iout[0] = pal[in[0]];
138 iout[1] = pal[in[1]];
139 iout[2] = pal[in[2]];
140 iout[3] = pal[in[3]];
146 iout[0] = pal[in[0]];
147 iout[1] = pal[in[1]];
152 iout[0] = pal[in[0]];
156 =================================================================
160 =================================================================
169 unsigned short xmin,ymin,xmax,ymax;
170 unsigned short hres,vres;
171 unsigned char palette[48];
174 unsigned short bytes_per_line;
175 unsigned short palette_type;
184 unsigned char* LoadPCX_BGRA (const unsigned char *f, int filesize)
187 unsigned char *a, *b, *image_buffer, *pbuf;
188 const unsigned char *palette, *fin, *enddata;
189 int x, y, x2, dataByte;
191 if (filesize < (int)sizeof(pcx) + 768)
193 Con_Print("Bad pcx file\n");
199 memcpy(&pcx, fin, sizeof(pcx));
202 // LordHavoc: big-endian support ported from QF newtree
203 pcx.xmax = LittleShort (pcx.xmax);
204 pcx.xmin = LittleShort (pcx.xmin);
205 pcx.ymax = LittleShort (pcx.ymax);
206 pcx.ymin = LittleShort (pcx.ymin);
207 pcx.hres = LittleShort (pcx.hres);
208 pcx.vres = LittleShort (pcx.vres);
209 pcx.bytes_per_line = LittleShort (pcx.bytes_per_line);
210 pcx.palette_type = LittleShort (pcx.palette_type);
212 image_width = pcx.xmax + 1 - pcx.xmin;
213 image_height = pcx.ymax + 1 - pcx.ymin;
214 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)
216 Con_Print("Bad pcx file\n");
220 palette = f + filesize - 768;
222 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width*image_height*4);
225 Con_Printf("LoadPCX: not enough memory for %i by %i image\n", image_width, image_height);
228 pbuf = image_buffer + image_width*image_height*3;
231 for (y = 0;y < image_height && fin < enddata;y++)
233 a = pbuf + y * image_width;
234 for (x = 0;x < image_width && fin < enddata;)
241 x2 = x + (dataByte & 0x3F);
243 if (x2 > image_width)
244 x2 = image_width; // technically an error
251 fin += pcx.bytes_per_line - image_width; // the number of bytes per line is always forced to an even number
252 while(x < image_width)
259 for(x = 0;x < image_width*image_height;x++)
272 =========================================================
276 =========================================================
279 typedef struct _TargaHeader
281 unsigned char id_length, colormap_type, image_type;
282 unsigned short colormap_index, colormap_length;
283 unsigned char colormap_size;
284 unsigned short x_origin, y_origin, width, height;
285 unsigned char pixel_size, attributes;
289 void PrintTargaHeader(TargaHeader *t)
291 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);
299 unsigned char *LoadTGA_BGRA (const unsigned char *f, int filesize)
301 int x, y, pix_inc, row_inci, runlen, alphabits;
302 unsigned char *image_buffer;
303 unsigned int *pixbufi;
304 const unsigned char *fin, *enddata;
305 TargaHeader targa_header;
306 unsigned int palettei[256];
317 enddata = f + filesize;
319 targa_header.id_length = f[0];
320 targa_header.colormap_type = f[1];
321 targa_header.image_type = f[2];
323 targa_header.colormap_index = f[3] + f[4] * 256;
324 targa_header.colormap_length = f[5] + f[6] * 256;
325 targa_header.colormap_size = f[7];
326 targa_header.x_origin = f[8] + f[9] * 256;
327 targa_header.y_origin = f[10] + f[11] * 256;
328 targa_header.width = image_width = f[12] + f[13] * 256;
329 targa_header.height = image_height = f[14] + f[15] * 256;
330 targa_header.pixel_size = f[16];
331 targa_header.attributes = f[17];
333 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
335 Con_Print("LoadTGA: invalid size\n");
336 PrintTargaHeader(&targa_header);
340 // advance to end of header
343 // skip TARGA image comment (usually 0 bytes)
344 fin += targa_header.id_length;
346 // read/skip the colormap if present (note: according to the TARGA spec it
347 // can be present even on truecolor or greyscale images, just not used by
349 if (targa_header.colormap_type)
351 if (targa_header.colormap_length > 256)
353 Con_Print("LoadTGA: only up to 256 colormap_length supported\n");
354 PrintTargaHeader(&targa_header);
357 if (targa_header.colormap_index)
359 Con_Print("LoadTGA: colormap_index not supported\n");
360 PrintTargaHeader(&targa_header);
363 if (targa_header.colormap_size == 24)
365 for (x = 0;x < targa_header.colormap_length;x++)
371 palettei[x] = bgra.i;
374 else if (targa_header.colormap_size == 32)
376 memcpy(palettei, fin, targa_header.colormap_length*4);
377 fin += targa_header.colormap_length * 4;
381 Con_Print("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
382 PrintTargaHeader(&targa_header);
387 // check our pixel_size restrictions according to image_type
388 switch (targa_header.image_type & ~8)
391 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
393 Con_Print("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
394 PrintTargaHeader(&targa_header);
399 // set up a palette to make the loader easier
400 for (x = 0;x < 256;x++)
402 bgra.b[0] = bgra.b[1] = bgra.b[2] = x;
404 palettei[x] = bgra.i;
406 // fall through to colormap case
408 if (targa_header.pixel_size != 8)
410 Con_Print("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
411 PrintTargaHeader(&targa_header);
416 Con_Printf("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
417 PrintTargaHeader(&targa_header);
421 if (targa_header.attributes & 0x10)
423 Con_Print("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
427 // number of attribute bits per pixel, we only support 0 or 8
428 alphabits = targa_header.attributes & 0x0F;
429 if (alphabits != 8 && alphabits != 0)
431 Con_Print("LoadTGA: only 0 or 8 attribute (alpha) bits supported\n");
435 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
438 Con_Printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
442 // If bit 5 of attributes isn't set, the image has been stored from bottom to top
443 if ((targa_header.attributes & 0x20) == 0)
445 pixbufi = (unsigned int*)image_buffer + (image_height - 1)*image_width;
446 row_inci = -image_width*2;
450 pixbufi = (unsigned int*)image_buffer;
457 if ((targa_header.image_type & ~8) == 2)
458 pix_inc = (targa_header.pixel_size + 7) / 8;
459 switch (targa_header.image_type)
461 case 1: // colormapped, uncompressed
462 case 3: // greyscale, uncompressed
463 if (fin + image_width * image_height * pix_inc > enddata)
465 for (y = 0;y < image_height;y++, pixbufi += row_inci)
466 for (x = 0;x < image_width;x++)
467 *pixbufi++ = palettei[*fin++];
470 // BGR or BGRA, uncompressed
471 if (fin + image_width * image_height * pix_inc > enddata)
473 if (targa_header.pixel_size == 32 && alphabits)
475 for (y = 0;y < image_height;y++)
476 memcpy(pixbufi + y * (image_width + row_inci), fin + y * image_width * pix_inc, image_width*4);
480 for (y = 0;y < image_height;y++, pixbufi += row_inci)
482 for (x = 0;x < image_width;x++, fin += pix_inc)
493 case 9: // colormapped, RLE
494 case 11: // greyscale, RLE
495 for (y = 0;y < image_height;y++, pixbufi += row_inci)
497 for (x = 0;x < image_width;)
500 break; // error - truncated file
504 // RLE - all pixels the same color
506 if (fin + pix_inc > enddata)
507 break; // error - truncated file
508 if (x + runlen > image_width)
509 break; // error - line exceeds width
510 bgra.i = palettei[*fin++];
516 // uncompressed - all pixels different color
518 if (fin + pix_inc * runlen > enddata)
519 break; // error - truncated file
520 if (x + runlen > image_width)
521 break; // error - line exceeds width
523 *pixbufi++ = palettei[*fin++];
530 if (targa_header.pixel_size == 32 && alphabits)
532 for (y = 0;y < image_height;y++, pixbufi += row_inci)
534 for (x = 0;x < image_width;)
537 break; // error - truncated file
541 // RLE - all pixels the same color
543 if (fin + pix_inc > enddata)
544 break; // error - truncated file
545 if (x + runlen > image_width)
546 break; // error - line exceeds width
557 // uncompressed - all pixels different color
559 if (fin + pix_inc * runlen > enddata)
560 break; // error - truncated file
561 if (x + runlen > image_width)
562 break; // error - line exceeds width
578 for (y = 0;y < image_height;y++, pixbufi += row_inci)
580 for (x = 0;x < image_width;)
583 break; // error - truncated file
587 // RLE - all pixels the same color
589 if (fin + pix_inc > enddata)
590 break; // error - truncated file
591 if (x + runlen > image_width)
592 break; // error - line exceeds width
603 // uncompressed - all pixels different color
605 if (fin + pix_inc * runlen > enddata)
606 break; // error - truncated file
607 if (x + runlen > image_width)
608 break; // error - line exceeds width
624 // unknown image_type
631 typedef struct q2wal_s
634 unsigned width, height;
635 unsigned offsets[MIPLEVELS]; // four mip maps stored
636 char animname[32]; // next frame in animation chain
642 unsigned char *LoadWAL_BGRA (const unsigned char *f, int filesize)
644 unsigned char *image_buffer;
645 const q2wal_t *inwal = (const q2wal_t *)f;
647 if (filesize < (int) sizeof(q2wal_t))
649 Con_Print("LoadWAL: invalid WAL file\n");
653 image_width = LittleLong(inwal->width);
654 image_height = LittleLong(inwal->height);
655 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
657 Con_Printf("LoadWAL: invalid size %ix%i\n", image_width, image_height);
661 if (filesize < (int) sizeof(q2wal_t) + (int) LittleLong(inwal->offsets[0]) + image_width * image_height)
663 Con_Print("LoadWAL: invalid WAL file\n");
667 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
670 Con_Printf("LoadWAL: not enough memory for %i by %i image\n", image_width, image_height);
673 Image_Copy8bitBGRA(f + LittleLong(inwal->offsets[0]), image_buffer, image_width * image_height, palette_bgra_complete);
678 void Image_StripImageExtension (const char *in, char *out, size_t size_out)
685 ext = FS_FileExtension(in);
686 if (ext && (!strcmp(ext, "tga") || !strcmp(ext, "pcx") || !strcmp(ext, "lmp") || !strcmp(ext, "png") || !strcmp(ext, "jpg")))
687 FS_StripExtension(in, out, size_out);
689 strlcpy(out, in, size_out);
692 typedef struct imageformat_s
694 const char *formatstring;
695 unsigned char *(*loadfunc)(const unsigned char *f, int filesize);
699 // GAME_TENEBRAE only
700 imageformat_t imageformats_tenebrae[] =
702 {"override/%s.tga", LoadTGA_BGRA},
703 {"override/%s.png", PNG_LoadImage_BGRA},
704 {"override/%s.jpg", JPEG_LoadImage_BGRA},
705 {"override/%s.pcx", LoadPCX_BGRA},
706 {"%s.tga", LoadTGA_BGRA},
707 {"%s.png", PNG_LoadImage_BGRA},
708 {"%s.jpg", JPEG_LoadImage_BGRA},
709 {"%s.pcx", LoadPCX_BGRA},
713 imageformat_t imageformats_nopath[] =
715 {"override/%s.tga", LoadTGA_BGRA},
716 {"override/%s.png", PNG_LoadImage_BGRA},
717 {"override/%s.jpg", JPEG_LoadImage_BGRA},
718 {"textures/%s.tga", LoadTGA_BGRA},
719 {"textures/%s.png", PNG_LoadImage_BGRA},
720 {"textures/%s.jpg", JPEG_LoadImage_BGRA},
721 {"%s.tga", LoadTGA_BGRA},
722 {"%s.png", PNG_LoadImage_BGRA},
723 {"%s.jpg", JPEG_LoadImage_BGRA},
724 {"%s.pcx", LoadPCX_BGRA},
728 // GAME_DELUXEQUAKE only
729 // VorteX: the point why i use such messy texture paths is
730 // that GtkRadiant can't detect normal/gloss textures
731 // and exclude them from texture browser
732 // so i just use additional folder to store this textures
733 imageformat_t imageformats_dq[] =
735 {"%s.tga", LoadTGA_BGRA},
736 {"%s.jpg", JPEG_LoadImage_BGRA},
737 {"texturemaps/%s.tga", LoadTGA_BGRA},
738 {"texturemaps/%s.jpg", JPEG_LoadImage_BGRA},
742 imageformat_t imageformats_textures[] =
744 {"%s.tga", LoadTGA_BGRA},
745 {"%s.png", PNG_LoadImage_BGRA},
746 {"%s.jpg", JPEG_LoadImage_BGRA},
747 {"%s.pcx", LoadPCX_BGRA},
748 {"%s.wal", LoadWAL_BGRA},
752 imageformat_t imageformats_gfx[] =
754 {"%s.tga", LoadTGA_BGRA},
755 {"%s.png", PNG_LoadImage_BGRA},
756 {"%s.jpg", JPEG_LoadImage_BGRA},
757 {"%s.pcx", LoadPCX_BGRA},
761 imageformat_t imageformats_other[] =
763 {"%s.tga", LoadTGA_BGRA},
764 {"%s.png", PNG_LoadImage_BGRA},
765 {"%s.jpg", JPEG_LoadImage_BGRA},
766 {"%s.pcx", LoadPCX_BGRA},
770 int fixtransparentpixels(unsigned char *data, int w, int h);
771 unsigned char *loadimagepixelsbgra (const char *filename, qboolean complain, qboolean allowFixtrans)
773 fs_offset_t filesize;
774 imageformat_t *firstformat, *format;
775 unsigned char *f, *data = NULL;
776 char basename[MAX_QPATH], name[MAX_QPATH], *c;
777 if (developer_memorydebug.integer)
778 Mem_CheckSentinelsGlobal();
779 if (developer_texturelogging.integer)
780 Log_Printf("textures.log", "%s\n", filename);
781 Image_StripImageExtension(filename, basename, sizeof(basename)); // strip filename extensions to allow replacement by other types
782 // replace *'s with #, so commandline utils don't get confused when dealing with the external files
783 for (c = basename;*c;c++)
787 if (strchr(basename, '/'))
790 for (i = 0;i < (int)sizeof(name)-1 && basename[i] != '/';i++)
791 name[i] = basename[i];
794 if (gamemode == GAME_TENEBRAE)
795 firstformat = imageformats_tenebrae;
796 else if (gamemode == GAME_DELUXEQUAKE)
797 firstformat = imageformats_dq;
798 else if (!strcasecmp(name, "textures"))
799 firstformat = imageformats_textures;
800 else if (!strcasecmp(name, "gfx"))
801 firstformat = imageformats_gfx;
802 else if (!strchr(basename, '/'))
803 firstformat = imageformats_nopath;
805 firstformat = imageformats_other;
806 // now try all the formats in the selected list
807 for (format = firstformat;format->formatstring;format++)
809 sprintf (name, format->formatstring, basename);
810 f = FS_LoadFile(name, tempmempool, true, &filesize);
813 data = format->loadfunc(f, filesize);
817 if (developer.integer >= 10)
818 Con_Printf("loaded image %s (%dx%d)\n", name, image_width, image_height);
819 if (developer_memorydebug.integer)
820 Mem_CheckSentinelsGlobal();
821 if(allowFixtrans && r_fixtrans_auto.integer)
823 int n = fixtransparentpixels(data, image_width, image_height);
826 Con_Printf("- had to fix %s (%d pixels changed)\n", name, n);
827 if(r_fixtrans_auto.integer >= 2)
829 char outfilename[MAX_QPATH], buf[MAX_QPATH];
830 Image_StripImageExtension(name, buf, sizeof(buf));
831 dpsnprintf(outfilename, sizeof(outfilename), "fixtrans/%s.tga", buf);
832 Image_WriteTGABGRA(outfilename, image_width, image_height, data);
833 Con_Printf("- %s written.\n", outfilename);
840 Con_DPrintf("Error loading image %s (file loaded but decode failed)\n", name);
845 Con_Printf("Couldn't load %s using ", filename);
846 for (format = firstformat;format->formatstring;format++)
848 sprintf (name, format->formatstring, basename);
849 Con_Printf(format == firstformat ? "\"%s\"" : (format[1].formatstring ? ", \"%s\"" : " or \"%s\".\n"), format->formatstring);
852 if (developer_memorydebug.integer)
853 Mem_CheckSentinelsGlobal();
857 rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, qboolean complain, int flags, qboolean allowFixtrans)
861 if (!(data = loadimagepixelsbgra (filename, complain, allowFixtrans)))
863 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_BGRA, flags, NULL);
868 int fixtransparentpixels(unsigned char *data, int w, int h)
870 int const FIXTRANS_NEEDED = 1;
871 int const FIXTRANS_HAS_L = 2;
872 int const FIXTRANS_HAS_R = 4;
873 int const FIXTRANS_HAS_U = 8;
874 int const FIXTRANS_HAS_D = 16;
875 int const FIXTRANS_FIXED = 32;
876 unsigned char *fixMask = Mem_Alloc(tempmempool, w * h);
878 int changedPixels = 0;
881 #define FIXTRANS_PIXEL (y*w+x)
882 #define FIXTRANS_PIXEL_U (((y+h-1)%h)*w+x)
883 #define FIXTRANS_PIXEL_D (((y+1)%h)*w+x)
884 #define FIXTRANS_PIXEL_L (y*w+((x+w-1)%w))
885 #define FIXTRANS_PIXEL_R (y*w+((x+1)%w))
887 memset(fixMask, 0, w * h);
888 for(y = 0; y < h; ++y)
889 for(x = 0; x < w; ++x)
891 if(data[FIXTRANS_PIXEL * 4 + 3] == 0)
893 fixMask[FIXTRANS_PIXEL] |= FIXTRANS_NEEDED;
898 fixMask[FIXTRANS_PIXEL_D] |= FIXTRANS_HAS_U;
899 fixMask[FIXTRANS_PIXEL_U] |= FIXTRANS_HAS_D;
900 fixMask[FIXTRANS_PIXEL_R] |= FIXTRANS_HAS_L;
901 fixMask[FIXTRANS_PIXEL_L] |= FIXTRANS_HAS_R;
904 if(fixPixels == w * h)
905 return 0; // sorry, can't do anything about this
908 for(y = 0; y < h; ++y)
909 for(x = 0; x < w; ++x)
910 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_NEEDED)
912 unsigned int sumR = 0, sumG = 0, sumB = 0, sumA = 0, sumRA = 0, sumGA = 0, sumBA = 0, cnt = 0;
913 unsigned char r, g, b, a, r0, g0, b0;
914 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_U)
916 r = data[FIXTRANS_PIXEL_U * 4 + 2];
917 g = data[FIXTRANS_PIXEL_U * 4 + 1];
918 b = data[FIXTRANS_PIXEL_U * 4 + 0];
919 a = data[FIXTRANS_PIXEL_U * 4 + 3];
920 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
922 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_D)
924 r = data[FIXTRANS_PIXEL_D * 4 + 2];
925 g = data[FIXTRANS_PIXEL_D * 4 + 1];
926 b = data[FIXTRANS_PIXEL_D * 4 + 0];
927 a = data[FIXTRANS_PIXEL_D * 4 + 3];
928 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
930 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_L)
932 r = data[FIXTRANS_PIXEL_L * 4 + 2];
933 g = data[FIXTRANS_PIXEL_L * 4 + 1];
934 b = data[FIXTRANS_PIXEL_L * 4 + 0];
935 a = data[FIXTRANS_PIXEL_L * 4 + 3];
936 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
938 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_R)
940 r = data[FIXTRANS_PIXEL_R * 4 + 2];
941 g = data[FIXTRANS_PIXEL_R * 4 + 1];
942 b = data[FIXTRANS_PIXEL_R * 4 + 0];
943 a = data[FIXTRANS_PIXEL_R * 4 + 3];
944 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
948 r0 = data[FIXTRANS_PIXEL * 4 + 2];
949 g0 = data[FIXTRANS_PIXEL * 4 + 1];
950 b0 = data[FIXTRANS_PIXEL * 4 + 0];
953 // there is a surrounding non-alpha pixel
954 r = (sumRA + sumA / 2) / sumA;
955 g = (sumGA + sumA / 2) / sumA;
956 b = (sumBA + sumA / 2) / sumA;
960 // need to use a "regular" average
961 r = (sumR + cnt / 2) / cnt;
962 g = (sumG + cnt / 2) / cnt;
963 b = (sumB + cnt / 2) / cnt;
965 if(r != r0 || g != g0 || b != b0)
967 data[FIXTRANS_PIXEL * 4 + 2] = r;
968 data[FIXTRANS_PIXEL * 4 + 1] = g;
969 data[FIXTRANS_PIXEL * 4 + 0] = b;
970 fixMask[FIXTRANS_PIXEL] |= FIXTRANS_FIXED;
972 for(y = 0; y < h; ++y)
973 for(x = 0; x < w; ++x)
974 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_FIXED)
976 fixMask[FIXTRANS_PIXEL] &= ~(FIXTRANS_NEEDED | FIXTRANS_FIXED);
977 fixMask[FIXTRANS_PIXEL_D] |= FIXTRANS_HAS_U;
978 fixMask[FIXTRANS_PIXEL_U] |= FIXTRANS_HAS_D;
979 fixMask[FIXTRANS_PIXEL_R] |= FIXTRANS_HAS_L;
980 fixMask[FIXTRANS_PIXEL_L] |= FIXTRANS_HAS_R;
984 return changedPixels;
987 void Image_FixTransparentPixels_f(void)
989 const char *filename, *filename_pattern;
992 char outfilename[MAX_QPATH], buf[MAX_QPATH];
996 Con_Printf("Usage: %s imagefile\n", Cmd_Argv(0));
999 filename_pattern = Cmd_Argv(1);
1000 search = FS_Search(filename_pattern, true, true);
1003 for(i = 0; i < search->numfilenames; ++i)
1005 filename = search->filenames[i];
1006 Con_Printf("Processing %s... ", filename);
1007 Image_StripImageExtension(filename, buf, sizeof(buf));
1008 dpsnprintf(outfilename, sizeof(outfilename), "fixtrans/%s.tga", buf);
1009 if(!(data = loadimagepixelsbgra(filename, true, false)))
1011 if((n = fixtransparentpixels(data, image_width, image_height)))
1013 Image_WriteTGABGRA(outfilename, image_width, image_height, data);
1014 Con_Printf("%s written (%d pixels changed).\n", outfilename, n);
1017 Con_Printf("unchanged.\n");
1022 qboolean Image_WriteTGABGR_preflipped (const char *filename, int width, int height, const unsigned char *data, unsigned char *buffer)
1026 memset (buffer, 0, 18);
1027 buffer[2] = 2; // uncompressed type
1028 buffer[12] = (width >> 0) & 0xFF;
1029 buffer[13] = (width >> 8) & 0xFF;
1030 buffer[14] = (height >> 0) & 0xFF;
1031 buffer[15] = (height >> 8) & 0xFF;
1032 buffer[16] = 24; // pixel size
1035 memcpy(buffer + 18, data, width*height*3);
1036 ret = FS_WriteFile (filename, buffer, width*height*3 + 18 );
1041 void Image_WriteTGABGRA (const char *filename, int width, int height, const unsigned char *data)
1044 unsigned char *buffer, *out;
1045 const unsigned char *in, *end;
1047 buffer = (unsigned char *)Mem_Alloc(tempmempool, width*height*4 + 18);
1049 memset (buffer, 0, 18);
1050 buffer[2] = 2; // uncompressed type
1051 buffer[12] = (width >> 0) & 0xFF;
1052 buffer[13] = (width >> 8) & 0xFF;
1053 buffer[14] = (height >> 0) & 0xFF;
1054 buffer[15] = (height >> 8) & 0xFF;
1056 for (y = 3;y < width*height*4;y += 4)
1060 if (y < width*height*4)
1062 // save the alpha channel
1063 buffer[16] = 32; // pixel size
1064 buffer[17] = 8; // 8 bits of alpha
1068 for (y = height - 1;y >= 0;y--)
1070 memcpy(out, data + y * width * 4, width * 4);
1076 // save only the color channels
1077 buffer[16] = 24; // pixel size
1078 buffer[17] = 0; // 8 bits of alpha
1080 // truncate bgra to bgr and flip upside down
1082 for (y = height - 1;y >= 0;y--)
1084 in = data + y * width * 4;
1085 end = in + width * 4;
1086 for (;in < end;in += 4)
1094 FS_WriteFile (filename, buffer, out - buffer);
1099 static void Image_Resample32LerpLine (const unsigned char *in, unsigned char *out, int inwidth, int outwidth)
1101 int j, xi, oldx = 0, f, fstep, endx, lerp;
1102 fstep = (int) (inwidth*65536.0f/outwidth);
1104 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
1109 in += (xi - oldx) * 4;
1115 *out++ = (unsigned char) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
1116 *out++ = (unsigned char) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
1117 *out++ = (unsigned char) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
1118 *out++ = (unsigned char) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
1120 else // last pixel of the line has no pixel to lerp to
1130 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (unsigned char) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
1131 void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1133 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
1135 const unsigned char *inrow;
1136 unsigned char *resamplerow1;
1137 unsigned char *resamplerow2;
1138 out = (unsigned char *)outdata;
1139 fstep = (int) (inheight*65536.0f/outheight);
1141 resamplerow1 = (unsigned char *)Mem_Alloc(tempmempool, outwidth*4*2);
1142 resamplerow2 = resamplerow1 + outwidth*4;
1144 inrow = (const unsigned char *)indata;
1146 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1147 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1148 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1156 inrow = (unsigned char *)indata + inwidth4*yi;
1158 memcpy(resamplerow1, resamplerow2, outwidth4);
1160 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1161 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1212 resamplerow1 -= outwidth4;
1213 resamplerow2 -= outwidth4;
1219 inrow = (unsigned char *)indata + inwidth4*yi;
1221 memcpy(resamplerow1, resamplerow2, outwidth4);
1223 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1226 memcpy(out, resamplerow1, outwidth4);
1230 Mem_Free(resamplerow1);
1231 resamplerow1 = NULL;
1232 resamplerow2 = NULL;
1235 void Image_Resample32Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1238 unsigned frac, fracstep;
1239 // relies on int being 4 bytes
1241 out = (int *)outdata;
1243 fracstep = inwidth*0x10000/outwidth;
1244 for (i = 0;i < outheight;i++)
1246 inrow = (int *)indata + inwidth*(i*inheight/outheight);
1247 frac = fracstep >> 1;
1251 out[0] = inrow[frac >> 16];frac += fracstep;
1252 out[1] = inrow[frac >> 16];frac += fracstep;
1253 out[2] = inrow[frac >> 16];frac += fracstep;
1254 out[3] = inrow[frac >> 16];frac += fracstep;
1260 out[0] = inrow[frac >> 16];frac += fracstep;
1261 out[1] = inrow[frac >> 16];frac += fracstep;
1266 out[0] = inrow[frac >> 16];frac += fracstep;
1277 void Image_Resample32(const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int quality)
1279 if (indepth != 1 || outdepth != 1)
1281 Con_Printf ("Image_Resample: 3D resampling not supported\n");
1285 Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1287 Image_Resample32Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1290 // in can be the same as out
1291 void Image_MipReduce32(const unsigned char *in, unsigned char *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth)
1293 const unsigned char *inrow;
1295 if (*depth != 1 || destdepth != 1)
1297 Con_Printf ("Image_Resample: 3D resampling not supported\n");
1298 if (*width > destwidth)
1300 if (*height > destheight)
1302 if (*depth > destdepth)
1306 // note: if given odd width/height this discards the last row/column of
1307 // pixels, rather than doing a proper box-filter scale down
1309 nextrow = *width * 4;
1310 if (*width > destwidth)
1313 if (*height > destheight)
1317 for (y = 0;y < *height;y++, inrow += nextrow * 2)
1319 for (in = inrow, x = 0;x < *width;x++)
1321 out[0] = (unsigned char) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
1322 out[1] = (unsigned char) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1323 out[2] = (unsigned char) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1324 out[3] = (unsigned char) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1333 for (y = 0;y < *height;y++, inrow += nextrow)
1335 for (in = inrow, x = 0;x < *width;x++)
1337 out[0] = (unsigned char) ((in[0] + in[4]) >> 1);
1338 out[1] = (unsigned char) ((in[1] + in[5]) >> 1);
1339 out[2] = (unsigned char) ((in[2] + in[6]) >> 1);
1340 out[3] = (unsigned char) ((in[3] + in[7]) >> 1);
1349 if (*height > destheight)
1353 for (y = 0;y < *height;y++, inrow += nextrow * 2)
1355 for (in = inrow, x = 0;x < *width;x++)
1357 out[0] = (unsigned char) ((in[0] + in[nextrow ]) >> 1);
1358 out[1] = (unsigned char) ((in[1] + in[nextrow+1]) >> 1);
1359 out[2] = (unsigned char) ((in[2] + in[nextrow+2]) >> 1);
1360 out[3] = (unsigned char) ((in[3] + in[nextrow+3]) >> 1);
1367 Con_Printf ("Image_MipReduce: desired size already achieved\n");
1371 void Image_HeightmapToNormalmap_BGRA(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
1373 int x, y, x1, x2, y1, y2;
1374 const unsigned char *b, *row[3];
1377 float iwidth, iheight, ibumpscale, n[3];
1378 iwidth = 1.0f / width;
1379 iheight = 1.0f / height;
1380 ibumpscale = (255.0f * 6.0f) / bumpscale;
1382 for (y = 0, y1 = height-1;y < height;y1 = y, y++)
1384 y2 = y + 1;if (y2 >= height) y2 = 0;
1385 row[0] = inpixels + (y1 * width) * 4;
1386 row[1] = inpixels + (y * width) * 4;
1387 row[2] = inpixels + (y2 * width) * 4;
1388 for (x = 0, x1 = width-1;x < width;x1 = x, x++)
1390 x2 = x + 1;if (x2 >= width) x2 = 0;
1392 b = row[1] + x1 * 4;p[0] = (b[0] + b[1] + b[2]);
1393 b = row[1] + x2 * 4;p[1] = (b[0] + b[1] + b[2]);
1395 b = row[0] + x * 4;p[2] = (b[0] + b[1] + b[2]);
1396 b = row[2] + x * 4;p[3] = (b[0] + b[1] + b[2]);
1398 b = row[1] + x * 4;p[4] = (b[0] + b[1] + b[2]);
1399 // calculate a normal from the slopes
1404 // turn it into a dot3 rgb vector texture
1405 out[2] = (int)(128.0f + n[0] * 127.0f);
1406 out[1] = (int)(128.0f + n[1] * 127.0f);
1407 out[0] = (int)(128.0f + n[2] * 127.0f);
1408 out[3] = (p[4]) / 3;