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_Copy8bitRGBA(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 (const unsigned char *f, int filesize, int matchwidth, int matchheight)
187 unsigned char *a, *b, *image_rgba, *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");
219 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
222 palette = f + filesize - 768;
224 image_rgba = (unsigned char *)Mem_Alloc(tempmempool, image_width*image_height*4);
227 Con_Printf("LoadPCX: not enough memory for %i by %i image\n", image_width, image_height);
230 pbuf = image_rgba + image_width*image_height*3;
233 for (y = 0;y < image_height && fin < enddata;y++)
235 a = pbuf + y * image_width;
236 for (x = 0;x < image_width && fin < enddata;)
243 x2 = x + (dataByte & 0x3F);
245 if (x2 > image_width)
246 x2 = image_width; // technically an error
253 fin += pcx.bytes_per_line - image_width; // the number of bytes per line is always forced to an even number
254 while(x < image_width)
261 for(x = 0;x < image_width*image_height;x++)
274 =========================================================
278 =========================================================
281 typedef struct _TargaHeader
283 unsigned char id_length, colormap_type, image_type;
284 unsigned short colormap_index, colormap_length;
285 unsigned char colormap_size;
286 unsigned short x_origin, y_origin, width, height;
287 unsigned char pixel_size, attributes;
291 void PrintTargaHeader(TargaHeader *t)
293 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);
301 unsigned char *LoadTGA (const unsigned char *f, int filesize, int matchwidth, int matchheight)
303 int x, y, pix_inc, row_inc, red, green, blue, alpha, runlen, alphabits;
304 unsigned char *pixbuf, *image_rgba;
305 const unsigned char *fin, *enddata;
307 TargaHeader targa_header;
308 unsigned char palette[256*4];
313 enddata = f + filesize;
315 targa_header.id_length = f[0];
316 targa_header.colormap_type = f[1];
317 targa_header.image_type = f[2];
319 targa_header.colormap_index = f[3] + f[4] * 256;
320 targa_header.colormap_length = f[5] + f[6] * 256;
321 targa_header.colormap_size = f[7];
322 targa_header.x_origin = f[8] + f[9] * 256;
323 targa_header.y_origin = f[10] + f[11] * 256;
324 targa_header.width = image_width = f[12] + f[13] * 256;
325 targa_header.height = image_height = f[14] + f[15] * 256;
326 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
328 Con_Print("LoadTGA: invalid size\n");
329 PrintTargaHeader(&targa_header);
332 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
334 targa_header.pixel_size = f[16];
335 targa_header.attributes = f[17];
337 // advance to end of header
340 // skip TARGA image comment (usually 0 bytes)
341 fin += targa_header.id_length;
343 // read/skip the colormap if present (note: according to the TARGA spec it
344 // can be present even on truecolor or greyscale images, just not used by
346 if (targa_header.colormap_type)
348 if (targa_header.colormap_length > 256)
350 Con_Print("LoadTGA: only up to 256 colormap_length supported\n");
351 PrintTargaHeader(&targa_header);
354 if (targa_header.colormap_index)
356 Con_Print("LoadTGA: colormap_index not supported\n");
357 PrintTargaHeader(&targa_header);
360 if (targa_header.colormap_size == 24)
362 for (x = 0;x < targa_header.colormap_length;x++)
364 palette[x*4+2] = *fin++;
365 palette[x*4+1] = *fin++;
366 palette[x*4+0] = *fin++;
367 palette[x*4+3] = 255;
370 else if (targa_header.colormap_size == 32)
372 for (x = 0;x < targa_header.colormap_length;x++)
374 palette[x*4+2] = *fin++;
375 palette[x*4+1] = *fin++;
376 palette[x*4+0] = *fin++;
377 palette[x*4+3] = *fin++;
382 Con_Print("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
383 PrintTargaHeader(&targa_header);
388 // check our pixel_size restrictions according to image_type
389 switch (targa_header.image_type & ~8)
392 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
394 Con_Print("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
395 PrintTargaHeader(&targa_header);
400 // set up a palette to make the loader easier
401 for (x = 0;x < 256;x++)
406 palette[x*4+3] = 255;
408 // fall through to colormap case
410 if (targa_header.pixel_size != 8)
412 Con_Print("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
413 PrintTargaHeader(&targa_header);
418 Con_Printf("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
419 PrintTargaHeader(&targa_header);
423 if (targa_header.attributes & 0x10)
425 Con_Print("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
429 // number of attribute bits per pixel, we only support 0 or 8
430 alphabits = targa_header.attributes & 0x0F;
431 if (alphabits != 8 && alphabits != 0)
433 Con_Print("LoadTGA: only 0 or 8 attribute (alpha) bits supported\n");
437 image_rgba = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
440 Con_Printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
444 // If bit 5 of attributes isn't set, the image has been stored from bottom to top
445 if ((targa_header.attributes & 0x20) == 0)
447 pixbuf = image_rgba + (image_height - 1)*image_width*4;
448 row_inc = -image_width*4*2;
458 red = green = blue = alpha = 255;
460 if ((targa_header.image_type & ~8) == 2)
461 pix_inc = targa_header.pixel_size / 8;
462 switch (targa_header.image_type)
464 case 1: // colormapped, uncompressed
465 case 3: // greyscale, uncompressed
466 if (fin + image_width * image_height * pix_inc > enddata)
468 for (y = 0;y < image_height;y++, pixbuf += row_inc)
470 for (x = 0;x < image_width;x++)
472 p = palette + *fin++ * 4;
481 // BGR or BGRA, uncompressed
482 if (fin + image_width * image_height * pix_inc > enddata)
484 if (targa_header.pixel_size == 32 && alphabits)
486 for (y = 0;y < image_height;y++, pixbuf += row_inc)
488 for (x = 0;x < image_width;x++, fin += pix_inc)
499 for (y = 0;y < image_height;y++, pixbuf += row_inc)
501 for (x = 0;x < image_width;x++, fin += pix_inc)
511 case 9: // colormapped, RLE
512 case 11: // greyscale, RLE
513 for (y = 0;y < image_height;y++, pixbuf += row_inc)
515 for (x = 0;x < image_width;)
518 break; // error - truncated file
522 // RLE - all pixels the same color
524 if (fin + pix_inc > enddata)
525 break; // error - truncated file
526 if (x + runlen > image_width)
527 break; // error - line exceeds width
528 p = palette + *fin++ * 4;
543 // uncompressed - all pixels different color
545 if (fin + pix_inc * runlen > enddata)
546 break; // error - truncated file
547 if (x + runlen > image_width)
548 break; // error - line exceeds width
551 p = palette + *fin++ * 4;
563 if (targa_header.pixel_size == 32 && alphabits)
565 for (y = 0;y < image_height;y++, pixbuf += row_inc)
567 for (x = 0;x < image_width;)
570 break; // error - truncated file
574 // RLE - all pixels the same color
576 if (fin + pix_inc > enddata)
577 break; // error - truncated file
578 if (x + runlen > image_width)
579 break; // error - line exceeds width
595 // uncompressed - all pixels different color
597 if (fin + pix_inc * runlen > enddata)
598 break; // error - truncated file
599 if (x + runlen > image_width)
600 break; // error - line exceeds width
601 for (;runlen--;x++, fin += pix_inc)
614 for (y = 0;y < image_height;y++, pixbuf += row_inc)
616 for (x = 0;x < image_width;)
619 break; // error - truncated file
623 // RLE - all pixels the same color
625 if (fin + pix_inc > enddata)
626 break; // error - truncated file
627 if (x + runlen > image_width)
628 break; // error - line exceeds width
644 // uncompressed - all pixels different color
646 if (fin + pix_inc * runlen > enddata)
647 break; // error - truncated file
648 if (x + runlen > image_width)
649 break; // error - line exceeds width
650 for (;runlen--;x++, fin += pix_inc)
663 // unknown image_type
675 unsigned char *LoadLMP (const unsigned char *f, int filesize, int matchwidth, int matchheight, qboolean loadAs8Bit)
677 unsigned char *image_buffer;
681 Con_Print("LoadLMP: invalid LMP file\n");
685 // parse the very complicated header *chuckle*
686 image_width = BuffLittleLong(f);
687 image_height = BuffLittleLong(f + 4);
688 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
690 Con_Printf("LoadLMP: invalid size %ix%i\n", image_width, image_height);
693 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
696 if (filesize < (8 + image_width * image_height))
698 Con_Print("LoadLMP: invalid LMP file\n");
704 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height);
705 memcpy(image_buffer, f + 8, image_width * image_height);
709 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
710 Image_Copy8bitRGBA(f + 8, image_buffer, image_width * image_height, palette_transparent);
716 typedef struct q2wal_s
719 unsigned width, height;
720 unsigned offsets[MIPLEVELS]; // four mip maps stored
721 char animname[32]; // next frame in animation chain
727 unsigned char *LoadWAL (const unsigned char *f, int filesize, int matchwidth, int matchheight)
729 unsigned char *image_rgba;
730 const q2wal_t *inwal = (const q2wal_t *)f;
732 if (filesize < (int) sizeof(q2wal_t))
734 Con_Print("LoadWAL: invalid WAL file\n");
738 image_width = LittleLong(inwal->width);
739 image_height = LittleLong(inwal->height);
740 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
742 Con_Printf("LoadWAL: invalid size %ix%i\n", image_width, image_height);
745 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
748 if (filesize < (int) sizeof(q2wal_t) + (int) LittleLong(inwal->offsets[0]) + image_width * image_height)
750 Con_Print("LoadWAL: invalid WAL file\n");
754 image_rgba = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
757 Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
760 Image_Copy8bitRGBA(f + LittleLong(inwal->offsets[0]), image_rgba, image_width * image_height, palette_complete);
765 void Image_StripImageExtension (const char *in, char *out, size_t size_out)
772 ext = FS_FileExtension(in);
773 if (ext && (!strcmp(ext, "tga") || !strcmp(ext, "pcx") || !strcmp(ext, "lmp") || !strcmp(ext, "png") || !strcmp(ext, "jpg")))
774 FS_StripExtension(in, out, size_out);
776 strlcpy(out, in, size_out);
779 typedef struct imageformat_s
781 const char *formatstring;
782 unsigned char *(*loadfunc)(const unsigned char *f, int filesize, int matchwidth, int matchheight);
786 // GAME_TENEBRAE only
787 imageformat_t imageformats_tenebrae[] =
789 {"override/%s.tga", LoadTGA},
790 {"override/%s.png", PNG_LoadImage},
791 {"override/%s.jpg", JPEG_LoadImage},
792 {"override/%s.pcx", LoadPCX},
796 imageformat_t imageformats_nopath[] =
798 {"override/%s.tga", LoadTGA},
799 {"override/%s.png", PNG_LoadImage},
800 {"override/%s.jpg", JPEG_LoadImage},
801 {"textures/%s.tga", LoadTGA},
802 {"textures/%s.png", PNG_LoadImage},
803 {"textures/%s.jpg", JPEG_LoadImage},
805 {"%s.png", PNG_LoadImage},
806 {"%s.jpg", JPEG_LoadImage},
811 imageformat_t imageformats_textures[] =
814 {"%s.png", PNG_LoadImage},
815 {"%s.jpg", JPEG_LoadImage},
821 imageformat_t imageformats_gfx[] =
824 {"%s.png", PNG_LoadImage},
825 {"%s.jpg", JPEG_LoadImage},
830 imageformat_t imageformats_other[] =
833 {"%s.png", PNG_LoadImage},
834 {"%s.jpg", JPEG_LoadImage},
839 unsigned char *loadimagepixels (const char *filename, qboolean complain, int matchwidth, int matchheight)
841 fs_offset_t filesize;
842 imageformat_t *firstformat, *format;
843 unsigned char *f, *data = NULL;
844 char basename[MAX_QPATH], name[MAX_QPATH], *c;
845 if (developer_memorydebug.integer)
846 Mem_CheckSentinelsGlobal();
847 if (developer_texturelogging.integer)
848 Log_Printf("textures.log", "%s\n", filename);
849 Image_StripImageExtension(filename, basename, sizeof(basename)); // strip filename extensions to allow replacement by other types
850 // replace *'s with #, so commandline utils don't get confused when dealing with the external files
851 for (c = basename;*c;c++)
855 if (strchr(basename, '/'))
858 for (i = 0;i < (int)sizeof(name)-1 && basename[i] != '/';i++)
859 name[i] = basename[i];
862 if (gamemode == GAME_TENEBRAE)
863 firstformat = imageformats_tenebrae;
864 else if (!strcasecmp(name, "textures"))
865 firstformat = imageformats_textures;
866 else if (!strcasecmp(name, "gfx"))
867 firstformat = imageformats_gfx;
868 else if (!strchr(basename, '/'))
869 firstformat = imageformats_nopath;
871 firstformat = imageformats_other;
872 // now try all the formats in the selected list
873 for (format = firstformat;format->formatstring;format++)
875 sprintf (name, format->formatstring, basename);
876 f = FS_LoadFile(name, tempmempool, true, &filesize);
879 data = format->loadfunc(f, filesize, matchwidth, matchheight);
883 if (developer.integer >= 10)
884 Con_Printf("loaded image %s (%dx%d)\n", name, image_width, image_height);
885 if (developer_memorydebug.integer)
886 Mem_CheckSentinelsGlobal();
891 if (developer.integer >= 1)
892 Con_DPrintf("Error loading image %s (file loaded but decode failed)\n", name);
898 Con_Printf("Couldn't load %s using ", filename);
899 for (format = firstformat;format->formatstring;format++)
901 sprintf (name, format->formatstring, basename);
902 Con_Printf(format == firstformat ? "\"%s\"" : (format[1].formatstring ? ", \"%s\"" : " or \"%s\".\n"), format->formatstring);
905 if (developer_memorydebug.integer)
906 Mem_CheckSentinelsGlobal();
910 rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
914 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
916 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
921 int fixtransparentpixels(unsigned char *data, int w, int h)
923 int const FIXTRANS_NEEDED = 1;
924 int const FIXTRANS_HAS_L = 2;
925 int const FIXTRANS_HAS_R = 4;
926 int const FIXTRANS_HAS_U = 8;
927 int const FIXTRANS_HAS_D = 16;
928 int const FIXTRANS_FIXED = 32;
929 unsigned char *fixMask = Mem_Alloc(tempmempool, w * h);
931 int changedPixels = 0;
934 #define FIXTRANS_PIXEL (y*w+x)
935 #define FIXTRANS_PIXEL_U (((y+h-1)%h)*w+x)
936 #define FIXTRANS_PIXEL_D (((y+1)%h)*w+x)
937 #define FIXTRANS_PIXEL_L (y*w+((x+w-1)%w))
938 #define FIXTRANS_PIXEL_R (y*w+((x+1)%w))
940 memset(fixMask, 0, w * h);
941 for(y = 0; y < h; ++y)
942 for(x = 0; x < w; ++x)
944 if(data[FIXTRANS_PIXEL * 4 + 3] == 0)
946 fixMask[FIXTRANS_PIXEL] |= FIXTRANS_NEEDED;
951 fixMask[FIXTRANS_PIXEL_D] |= FIXTRANS_HAS_U;
952 fixMask[FIXTRANS_PIXEL_U] |= FIXTRANS_HAS_D;
953 fixMask[FIXTRANS_PIXEL_R] |= FIXTRANS_HAS_L;
954 fixMask[FIXTRANS_PIXEL_L] |= FIXTRANS_HAS_R;
957 if(fixPixels == w * h)
958 return 0; // sorry, can't do anything about this
961 for(y = 0; y < h; ++y)
962 for(x = 0; x < w; ++x)
963 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_NEEDED)
965 unsigned int sumR = 0, sumG = 0, sumB = 0, sumA = 0, sumRA = 0, sumGA = 0, sumBA = 0, cnt = 0;
966 unsigned char r, g, b, a, r0, g0, b0;
967 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_U)
969 r = data[FIXTRANS_PIXEL_U * 4 + 0];
970 g = data[FIXTRANS_PIXEL_U * 4 + 1];
971 b = data[FIXTRANS_PIXEL_U * 4 + 2];
972 a = data[FIXTRANS_PIXEL_U * 4 + 3];
973 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
975 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_D)
977 r = data[FIXTRANS_PIXEL_D * 4 + 0];
978 g = data[FIXTRANS_PIXEL_D * 4 + 1];
979 b = data[FIXTRANS_PIXEL_D * 4 + 2];
980 a = data[FIXTRANS_PIXEL_D * 4 + 3];
981 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
983 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_L)
985 r = data[FIXTRANS_PIXEL_L * 4 + 0];
986 g = data[FIXTRANS_PIXEL_L * 4 + 1];
987 b = data[FIXTRANS_PIXEL_L * 4 + 2];
988 a = data[FIXTRANS_PIXEL_L * 4 + 3];
989 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
991 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_HAS_R)
993 r = data[FIXTRANS_PIXEL_R * 4 + 0];
994 g = data[FIXTRANS_PIXEL_R * 4 + 1];
995 b = data[FIXTRANS_PIXEL_R * 4 + 2];
996 a = data[FIXTRANS_PIXEL_R * 4 + 3];
997 sumR += r; sumG += g; sumB += b; sumA += a; sumRA += r*a; sumGA += g*a; sumBA += b*a; ++cnt;
1001 r0 = data[FIXTRANS_PIXEL * 4 + 0];
1002 g0 = data[FIXTRANS_PIXEL * 4 + 1];
1003 b0 = data[FIXTRANS_PIXEL * 4 + 2];
1006 // there is a surrounding non-alpha pixel
1007 r = (sumRA + sumA / 2) / sumA;
1008 g = (sumGA + sumA / 2) / sumA;
1009 b = (sumBA + sumA / 2) / sumA;
1013 // need to use a "regular" average
1014 r = (sumR + cnt / 2) / cnt;
1015 g = (sumG + cnt / 2) / cnt;
1016 b = (sumB + cnt / 2) / cnt;
1018 if(r != r0 || g != g0 || b != b0)
1020 data[FIXTRANS_PIXEL * 4 + 0] = r;
1021 data[FIXTRANS_PIXEL * 4 + 1] = g;
1022 data[FIXTRANS_PIXEL * 4 + 2] = b;
1023 fixMask[FIXTRANS_PIXEL] |= FIXTRANS_FIXED;
1025 for(y = 0; y < h; ++y)
1026 for(x = 0; x < w; ++x)
1027 if(fixMask[FIXTRANS_PIXEL] & FIXTRANS_FIXED)
1029 fixMask[FIXTRANS_PIXEL] &= ~(FIXTRANS_NEEDED | FIXTRANS_FIXED);
1030 fixMask[FIXTRANS_PIXEL_D] |= FIXTRANS_HAS_U;
1031 fixMask[FIXTRANS_PIXEL_U] |= FIXTRANS_HAS_D;
1032 fixMask[FIXTRANS_PIXEL_R] |= FIXTRANS_HAS_L;
1033 fixMask[FIXTRANS_PIXEL_L] |= FIXTRANS_HAS_R;
1037 return changedPixels;
1040 void Image_FixTransparentPixels_f(void)
1042 const char *filename, *filename_pattern;
1045 char outfilename[MAX_QPATH], buf[MAX_QPATH];
1046 unsigned char *data;
1049 Con_Printf("Usage: %s imagefile\n", Cmd_Argv(0));
1052 filename_pattern = Cmd_Argv(1);
1053 search = FS_Search(filename_pattern, true, true);
1056 for(i = 0; i < search->numfilenames; ++i)
1058 filename = search->filenames[i];
1059 Con_Printf("Processing %s... ", filename);
1060 Image_StripImageExtension(filename, buf, sizeof(buf));
1061 dpsnprintf(outfilename, sizeof(outfilename), "fixtrans/%s.tga", buf);
1062 if(!(data = loadimagepixels(filename, true, 0, 0)))
1064 if((n = fixtransparentpixels(data, image_width, image_height)))
1066 Image_WriteTGARGBA(outfilename, image_width, image_height, data);
1067 Con_Printf("%s written (%d pixels changed).\n", outfilename, n);
1070 Con_Printf("unchanged.\n");
1075 qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int height, const unsigned char *data, unsigned char *buffer)
1079 const unsigned char *in, *end;
1081 memset (buffer, 0, 18);
1082 buffer[2] = 2; // uncompressed type
1083 buffer[12] = (width >> 0) & 0xFF;
1084 buffer[13] = (width >> 8) & 0xFF;
1085 buffer[14] = (height >> 0) & 0xFF;
1086 buffer[15] = (height >> 8) & 0xFF;
1087 buffer[16] = 24; // pixel size
1092 end = in + width*height*3;
1093 for (;in < end;in += 3)
1099 ret = FS_WriteFile (filename, buffer, width*height*3 + 18 );
1104 void Image_WriteTGARGBA (const char *filename, int width, int height, const unsigned char *data)
1107 unsigned char *buffer, *out;
1108 const unsigned char *in, *end;
1110 buffer = (unsigned char *)Mem_Alloc(tempmempool, width*height*4 + 18);
1112 memset (buffer, 0, 18);
1113 buffer[2] = 2; // uncompressed type
1114 buffer[12] = (width >> 0) & 0xFF;
1115 buffer[13] = (width >> 8) & 0xFF;
1116 buffer[14] = (height >> 0) & 0xFF;
1117 buffer[15] = (height >> 8) & 0xFF;
1119 for (y = 3;y < width*height*4;y += 4)
1123 if (y < width*height*4)
1125 // save the alpha channel
1126 buffer[16] = 32; // pixel size
1127 buffer[17] = 8; // 8 bits of alpha
1129 // swap rgba to bgra and flip upside down
1131 for (y = height - 1;y >= 0;y--)
1133 in = data + y * width * 4;
1134 end = in + width * 4;
1135 for (;in < end;in += 4)
1143 FS_WriteFile (filename, buffer, width*height*4 + 18 );
1147 // save only the color channels
1148 buffer[16] = 24; // pixel size
1149 buffer[17] = 0; // 8 bits of alpha
1151 // swap rgba to bgr and flip upside down
1153 for (y = height - 1;y >= 0;y--)
1155 in = data + y * width * 4;
1156 end = in + width * 4;
1157 for (;in < end;in += 4)
1164 FS_WriteFile (filename, buffer, width*height*3 + 18 );
1170 static void Image_Resample32LerpLine (const unsigned char *in, unsigned char *out, int inwidth, int outwidth)
1172 int j, xi, oldx = 0, f, fstep, endx, lerp;
1173 fstep = (int) (inwidth*65536.0f/outwidth);
1175 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
1180 in += (xi - oldx) * 4;
1186 *out++ = (unsigned char) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
1187 *out++ = (unsigned char) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
1188 *out++ = (unsigned char) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
1189 *out++ = (unsigned char) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
1191 else // last pixel of the line has no pixel to lerp to
1201 static void Image_Resample24LerpLine (const unsigned char *in, unsigned char *out, int inwidth, int outwidth)
1203 int j, xi, oldx = 0, f, fstep, endx, lerp;
1204 fstep = (int) (inwidth*65536.0f/outwidth);
1206 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
1211 in += (xi - oldx) * 3;
1217 *out++ = (unsigned char) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
1218 *out++ = (unsigned char) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
1219 *out++ = (unsigned char) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
1221 else // last pixel of the line has no pixel to lerp to
1230 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (unsigned char) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
1231 void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1233 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
1235 const unsigned char *inrow;
1236 unsigned char *resamplerow1;
1237 unsigned char *resamplerow2;
1238 out = (unsigned char *)outdata;
1239 fstep = (int) (inheight*65536.0f/outheight);
1241 resamplerow1 = (unsigned char *)Mem_Alloc(tempmempool, outwidth*4*2);
1242 resamplerow2 = resamplerow1 + outwidth*4;
1244 inrow = (const unsigned char *)indata;
1246 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1247 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1248 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1256 inrow = (unsigned char *)indata + inwidth4*yi;
1258 memcpy(resamplerow1, resamplerow2, outwidth4);
1260 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1261 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1312 resamplerow1 -= outwidth4;
1313 resamplerow2 -= outwidth4;
1319 inrow = (unsigned char *)indata + inwidth4*yi;
1321 memcpy(resamplerow1, resamplerow2, outwidth4);
1323 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1326 memcpy(out, resamplerow1, outwidth4);
1330 Mem_Free(resamplerow1);
1331 resamplerow1 = NULL;
1332 resamplerow2 = NULL;
1335 void Image_Resample32Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1338 unsigned frac, fracstep;
1339 // relies on int being 4 bytes
1341 out = (int *)outdata;
1343 fracstep = inwidth*0x10000/outwidth;
1344 for (i = 0;i < outheight;i++)
1346 inrow = (int *)indata + inwidth*(i*inheight/outheight);
1347 frac = fracstep >> 1;
1351 out[0] = inrow[frac >> 16];frac += fracstep;
1352 out[1] = inrow[frac >> 16];frac += fracstep;
1353 out[2] = inrow[frac >> 16];frac += fracstep;
1354 out[3] = inrow[frac >> 16];frac += fracstep;
1360 out[0] = inrow[frac >> 16];frac += fracstep;
1361 out[1] = inrow[frac >> 16];frac += fracstep;
1366 out[0] = inrow[frac >> 16];frac += fracstep;
1372 void Image_Resample24Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1374 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
1376 const unsigned char *inrow;
1377 unsigned char *resamplerow1;
1378 unsigned char *resamplerow2;
1379 out = (unsigned char *)outdata;
1380 fstep = (int) (inheight*65536.0f/outheight);
1382 resamplerow1 = (unsigned char *)Mem_Alloc(tempmempool, outwidth*3*2);
1383 resamplerow2 = resamplerow1 + outwidth*3;
1385 inrow = (const unsigned char *)indata;
1387 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1388 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1389 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1397 inrow = (unsigned char *)indata + inwidth3*yi;
1399 memcpy(resamplerow1, resamplerow2, outwidth3);
1401 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1402 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1446 resamplerow1 -= outwidth3;
1447 resamplerow2 -= outwidth3;
1453 inrow = (unsigned char *)indata + inwidth3*yi;
1455 memcpy(resamplerow1, resamplerow2, outwidth3);
1457 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1460 memcpy(out, resamplerow1, outwidth3);
1463 Mem_Free(resamplerow1);
1464 resamplerow1 = NULL;
1465 resamplerow2 = NULL;
1468 void Image_Resample24Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1470 int i, j, f, inwidth3 = inwidth * 3;
1471 unsigned frac, fracstep;
1472 unsigned char *inrow, *out;
1473 out = (unsigned char *)outdata;
1475 fracstep = inwidth*0x10000/outwidth;
1476 for (i = 0;i < outheight;i++)
1478 inrow = (unsigned char *)indata + inwidth3*(i*inheight/outheight);
1479 frac = fracstep >> 1;
1483 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1484 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1485 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1486 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1491 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1492 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1497 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1508 void Image_Resample (const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int bytesperpixel, int quality)
1510 if (indepth != 1 || outdepth != 1)
1512 Con_Printf ("Image_Resample: 3D resampling not supported\n");
1515 if (bytesperpixel == 4)
1518 Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1520 Image_Resample32Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1522 else if (bytesperpixel == 3)
1525 Image_Resample24Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1527 Image_Resample24Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1530 Con_Printf ("Image_Resample: unsupported bytesperpixel %i\n", bytesperpixel);
1533 // in can be the same as out
1534 void Image_MipReduce(const unsigned char *in, unsigned char *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth, int bytesperpixel)
1536 const unsigned char *inrow;
1538 if (*depth != 1 || destdepth != 1)
1540 Con_Printf ("Image_Resample: 3D resampling not supported\n");
1541 if (*width > destwidth)
1543 if (*height > destheight)
1545 if (*depth > destdepth)
1549 // note: if given odd width/height this discards the last row/column of
1550 // pixels, rather than doing a proper box-filter scale down
1552 nextrow = *width * bytesperpixel;
1553 if (*width > destwidth)
1556 if (*height > destheight)
1560 if (bytesperpixel == 4)
1562 for (y = 0;y < *height;y++, inrow += nextrow * 2)
1564 for (in = inrow, x = 0;x < *width;x++)
1566 out[0] = (unsigned char) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
1567 out[1] = (unsigned char) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1568 out[2] = (unsigned char) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1569 out[3] = (unsigned char) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1575 else if (bytesperpixel == 3)
1577 for (y = 0;y < *height;y++, inrow += nextrow * 2)
1579 for (in = inrow, x = 0;x < *width;x++)
1581 out[0] = (unsigned char) ((in[0] + in[3] + in[nextrow ] + in[nextrow+3]) >> 2);
1582 out[1] = (unsigned char) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
1583 out[2] = (unsigned char) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
1590 Con_Printf ("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1595 if (bytesperpixel == 4)
1597 for (y = 0;y < *height;y++, inrow += nextrow)
1599 for (in = inrow, x = 0;x < *width;x++)
1601 out[0] = (unsigned char) ((in[0] + in[4]) >> 1);
1602 out[1] = (unsigned char) ((in[1] + in[5]) >> 1);
1603 out[2] = (unsigned char) ((in[2] + in[6]) >> 1);
1604 out[3] = (unsigned char) ((in[3] + in[7]) >> 1);
1610 else if (bytesperpixel == 3)
1612 for (y = 0;y < *height;y++, inrow += nextrow)
1614 for (in = inrow, x = 0;x < *width;x++)
1616 out[0] = (unsigned char) ((in[0] + in[3]) >> 1);
1617 out[1] = (unsigned char) ((in[1] + in[4]) >> 1);
1618 out[2] = (unsigned char) ((in[2] + in[5]) >> 1);
1625 Con_Printf ("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1630 if (*height > destheight)
1634 if (bytesperpixel == 4)
1636 for (y = 0;y < *height;y++, inrow += nextrow * 2)
1638 for (in = inrow, x = 0;x < *width;x++)
1640 out[0] = (unsigned char) ((in[0] + in[nextrow ]) >> 1);
1641 out[1] = (unsigned char) ((in[1] + in[nextrow+1]) >> 1);
1642 out[2] = (unsigned char) ((in[2] + in[nextrow+2]) >> 1);
1643 out[3] = (unsigned char) ((in[3] + in[nextrow+3]) >> 1);
1649 else if (bytesperpixel == 3)
1651 for (y = 0;y < *height;y++, inrow += nextrow * 2)
1653 for (in = inrow, x = 0;x < *width;x++)
1655 out[0] = (unsigned char) ((in[0] + in[nextrow ]) >> 1);
1656 out[1] = (unsigned char) ((in[1] + in[nextrow+1]) >> 1);
1657 out[2] = (unsigned char) ((in[2] + in[nextrow+2]) >> 1);
1664 Con_Printf ("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1667 Con_Printf ("Image_MipReduce: desired size already achieved\n");
1671 void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
1673 int x, y, x1, x2, y1, y2;
1674 const unsigned char *b, *row[3];
1677 float iwidth, iheight, ibumpscale, n[3];
1678 iwidth = 1.0f / width;
1679 iheight = 1.0f / height;
1680 ibumpscale = (255.0f * 6.0f) / bumpscale;
1682 for (y = 0, y1 = height-1;y < height;y1 = y, y++)
1684 y2 = y + 1;if (y2 >= height) y2 = 0;
1685 row[0] = inpixels + (y1 * width) * 4;
1686 row[1] = inpixels + (y * width) * 4;
1687 row[2] = inpixels + (y2 * width) * 4;
1688 for (x = 0, x1 = width-1;x < width;x1 = x, x++)
1690 x2 = x + 1;if (x2 >= width) x2 = 0;
1692 b = row[1] + x1 * 4;p[0] = (b[0] + b[1] + b[2]);
1693 b = row[1] + x2 * 4;p[1] = (b[0] + b[1] + b[2]);
1695 b = row[0] + x * 4;p[2] = (b[0] + b[1] + b[2]);
1696 b = row[2] + x * 4;p[3] = (b[0] + b[1] + b[2]);
1698 b = row[1] + x * 4;p[4] = (b[0] + b[1] + b[2]);
1699 // calculate a normal from the slopes
1704 // turn it into a dot3 rgb vector texture
1705 out[0] = (int)(128.0f + n[0] * 127.0f);
1706 out[1] = (int)(128.0f + n[1] * 127.0f);
1707 out[2] = (int)(128.0f + n[2] * 127.0f);
1708 out[3] = (p[4]) / 3;