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, row_inc, compressed, readpixelcount, red, green, blue, alpha, runlen, pindex, 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 if (targa_header.image_type == 2 || targa_header.image_type == 10)
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);
398 else if (targa_header.image_type == 1 || targa_header.image_type == 9)
400 if (targa_header.pixel_size != 8)
402 Con_Print("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
403 PrintTargaHeader(&targa_header);
407 else if (targa_header.image_type == 3 || targa_header.image_type == 11)
409 if (targa_header.pixel_size != 8)
411 Con_Print("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
412 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;
456 compressed = targa_header.image_type == 9 || targa_header.image_type == 10 || targa_header.image_type == 11;
459 red = green = blue = alpha = 255;
460 while (y < image_height)
462 // decoder is mostly the same whether it's compressed or not
463 readpixelcount = 1000000;
465 if (compressed && fin < enddata)
468 // high bit indicates this is an RLE compressed run
471 runlen = 1 + (runlen & 0x7f);
474 while((runlen--) && y < image_height)
476 if (readpixelcount > 0)
479 red = green = blue = alpha = 255;
482 switch(targa_header.image_type)
488 if (pindex >= targa_header.colormap_length)
490 p = palette + pindex * 4;
504 if (targa_header.pixel_size == 32 && fin < enddata)
510 red = green = blue = *fin++;
522 if (x == image_width)
524 // end of line, advance to next
540 unsigned char *LoadLMP (const unsigned char *f, int filesize, int matchwidth, int matchheight, qboolean loadAs8Bit)
542 unsigned char *image_buffer;
546 Con_Print("LoadLMP: invalid LMP file\n");
550 // parse the very complicated header *chuckle*
551 image_width = BuffLittleLong(f);
552 image_height = BuffLittleLong(f + 4);
553 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
555 Con_Printf("LoadLMP: invalid size %ix%i\n", image_width, image_height);
558 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
561 if (filesize < (8 + image_width * image_height))
563 Con_Print("LoadLMP: invalid LMP file\n");
569 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height);
570 memcpy(image_buffer, f + 8, image_width * image_height);
574 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
575 Image_Copy8bitRGBA(f + 8, image_buffer, image_width * image_height, palette_transparent);
580 static unsigned char *LoadLMPRGBA (const unsigned char *f, int filesize, int matchwidth, int matchheight)
582 return LoadLMP(f, filesize, matchwidth, matchheight, false);
586 typedef struct q2wal_s
589 unsigned width, height;
590 unsigned offsets[MIPLEVELS]; // four mip maps stored
591 char animname[32]; // next frame in animation chain
597 unsigned char *LoadWAL (const unsigned char *f, int filesize, int matchwidth, int matchheight)
599 unsigned char *image_rgba;
600 const q2wal_t *inwal = (const q2wal_t *)f;
602 if (filesize < (int) sizeof(q2wal_t))
604 Con_Print("LoadWAL: invalid WAL file\n");
608 image_width = LittleLong(inwal->width);
609 image_height = LittleLong(inwal->height);
610 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
612 Con_Printf("LoadWAL: invalid size %ix%i\n", image_width, image_height);
615 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
618 if (filesize < (int) sizeof(q2wal_t) + (int) LittleLong(inwal->offsets[0]) + image_width * image_height)
620 Con_Print("LoadWAL: invalid WAL file\n");
624 image_rgba = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
627 Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
630 Image_Copy8bitRGBA(f + LittleLong(inwal->offsets[0]), image_rgba, image_width * image_height, palette_complete);
635 void Image_StripImageExtension (const char *in, char *out)
637 const char *end, *temp;
638 end = in + strlen(in);
642 if (strcmp(temp, ".tga") == 0
643 || strcmp(temp, ".pcx") == 0
644 || strcmp(temp, ".lmp") == 0
645 || strcmp(temp, ".png") == 0
646 || strcmp(temp, ".jpg") == 0)
656 typedef struct imageformat_s
658 const char *formatstring;
659 unsigned char *(*loadfunc)(const unsigned char *f, int filesize, int matchwidth, int matchheight);
663 // GAME_TENEBRAE only
664 imageformat_t imageformats_tenebrae[] =
666 {"override/%s.tga", LoadTGA},
667 {"override/%s.png", PNG_LoadImage},
668 {"override/%s.jpg", JPEG_LoadImage},
672 imageformat_t imageformats_nopath[] =
674 {"textures/%s.tga", LoadTGA},
675 {"textures/%s.png", PNG_LoadImage},
676 {"textures/%s.jpg", JPEG_LoadImage},
678 {"%s.png", PNG_LoadImage},
679 {"%s.jpg", JPEG_LoadImage},
683 imageformat_t imageformats_textures[] =
686 {"%s.png", PNG_LoadImage},
687 {"%s.jpg", JPEG_LoadImage},
693 imageformat_t imageformats_gfx[] =
696 {"%s.png", PNG_LoadImage},
697 {"%s.jpg", JPEG_LoadImage},
699 {"%s.lmp", LoadLMPRGBA},
703 imageformat_t imageformats_other[] =
706 {"%s.png", PNG_LoadImage},
707 {"%s.jpg", JPEG_LoadImage},
712 unsigned char *loadimagepixels (const char *filename, qboolean complain, int matchwidth, int matchheight)
714 fs_offset_t filesize;
715 imageformat_t *firstformat, *format;
716 unsigned char *f, *data = NULL;
717 char basename[MAX_QPATH], name[MAX_QPATH], *c;
718 if (developer_memorydebug.integer)
719 Mem_CheckSentinelsGlobal();
720 if (developer_texturelogging.integer)
721 Log_Printf("textures.log", "%s\n", filename);
722 strlcpy(basename, filename, sizeof(basename));
723 Image_StripImageExtension(basename, basename); // strip filename extensions to allow replacement by other types
724 // replace *'s with #, so commandline utils don't get confused when dealing with the external files
725 for (c = basename;*c;c++)
729 if (strchr(basename, '/'))
732 for (i = 0;i < (int)sizeof(name)-1 && basename[i] != '/';i++)
733 name[i] = basename[i];
736 if (gamemode == GAME_TENEBRAE)
737 firstformat = imageformats_tenebrae;
738 else if (!strcasecmp(name, "textures"))
739 firstformat = imageformats_textures;
740 else if (!strcasecmp(name, "gfx"))
741 firstformat = imageformats_gfx;
742 else if (!strchr(name, '/'))
743 firstformat = imageformats_nopath;
745 firstformat = imageformats_other;
746 // now try all the formats in the selected list
747 for (format = firstformat;format->formatstring;format++)
749 sprintf (name, format->formatstring, basename);
750 f = FS_LoadFile(name, tempmempool, true, &filesize);
753 data = format->loadfunc(f, filesize, matchwidth, matchheight);
757 if (developer.integer >= 10)
758 Con_Printf("loaded image %s (%dx%d)\n", name, image_width, image_height);
759 if (developer_memorydebug.integer)
760 Mem_CheckSentinelsGlobal();
767 Con_Printf("Couldn't load %s using ", filename);
768 for (format = firstformat;format->formatstring;format++)
770 sprintf (name, format->formatstring, basename);
771 Con_Printf(format == firstformat ? "\"%s\"" : (format[1].formatstring ? ", \"%s\"" : " or \"%s\".\n"), format->formatstring);
774 if (developer_memorydebug.integer)
775 Mem_CheckSentinelsGlobal();
779 rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
783 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
785 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
790 qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int height, const unsigned char *data, unsigned char *buffer)
794 const unsigned char *in, *end;
796 memset (buffer, 0, 18);
797 buffer[2] = 2; // uncompressed type
798 buffer[12] = (width >> 0) & 0xFF;
799 buffer[13] = (width >> 8) & 0xFF;
800 buffer[14] = (height >> 0) & 0xFF;
801 buffer[15] = (height >> 8) & 0xFF;
802 buffer[16] = 24; // pixel size
807 end = in + width*height*3;
808 for (;in < end;in += 3)
814 ret = FS_WriteFile (filename, buffer, width*height*3 + 18 );
819 void Image_WriteTGARGBA (const char *filename, int width, int height, const unsigned char *data)
822 unsigned char *buffer, *out;
823 const unsigned char *in, *end;
825 buffer = (unsigned char *)Mem_Alloc(tempmempool, width*height*4 + 18);
827 memset (buffer, 0, 18);
828 buffer[2] = 2; // uncompressed type
829 buffer[12] = (width >> 0) & 0xFF;
830 buffer[13] = (width >> 8) & 0xFF;
831 buffer[14] = (height >> 0) & 0xFF;
832 buffer[15] = (height >> 8) & 0xFF;
833 buffer[16] = 32; // pixel size
834 buffer[17] = 8; // 8 bits of alpha
836 // swap rgba to bgra and flip upside down
838 for (y = height - 1;y >= 0;y--)
840 in = data + y * width * 4;
841 end = in + width * 4;
842 for (;in < end;in += 4)
850 FS_WriteFile (filename, buffer, width*height*4 + 18 );
855 static void Image_Resample32LerpLine (const unsigned char *in, unsigned char *out, int inwidth, int outwidth)
857 int j, xi, oldx = 0, f, fstep, endx, lerp;
858 fstep = (int) (inwidth*65536.0f/outwidth);
860 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
865 in += (xi - oldx) * 4;
871 *out++ = (unsigned char) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
872 *out++ = (unsigned char) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
873 *out++ = (unsigned char) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
874 *out++ = (unsigned char) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
876 else // last pixel of the line has no pixel to lerp to
886 static void Image_Resample24LerpLine (const unsigned char *in, unsigned char *out, int inwidth, int outwidth)
888 int j, xi, oldx = 0, f, fstep, endx, lerp;
889 fstep = (int) (inwidth*65536.0f/outwidth);
891 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
896 in += (xi - oldx) * 3;
902 *out++ = (unsigned char) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
903 *out++ = (unsigned char) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
904 *out++ = (unsigned char) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
906 else // last pixel of the line has no pixel to lerp to
915 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (unsigned char) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
916 void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
918 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
920 const unsigned char *inrow;
921 unsigned char *resamplerow1;
922 unsigned char *resamplerow2;
923 out = (unsigned char *)outdata;
924 fstep = (int) (inheight*65536.0f/outheight);
926 resamplerow1 = (unsigned char *)Mem_Alloc(tempmempool, outwidth*4*2);
927 resamplerow2 = resamplerow1 + outwidth*4;
929 inrow = (const unsigned char *)indata;
931 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
932 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
933 for (i = 0, f = 0;i < outheight;i++,f += fstep)
941 inrow = (unsigned char *)indata + inwidth4*yi;
943 memcpy(resamplerow1, resamplerow2, outwidth4);
945 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
946 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
997 resamplerow1 -= outwidth4;
998 resamplerow2 -= outwidth4;
1004 inrow = (unsigned char *)indata + inwidth4*yi;
1006 memcpy(resamplerow1, resamplerow2, outwidth4);
1008 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1011 memcpy(out, resamplerow1, outwidth4);
1015 Mem_Free(resamplerow1);
1016 resamplerow1 = NULL;
1017 resamplerow2 = NULL;
1020 void Image_Resample32Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1023 unsigned frac, fracstep;
1024 // relies on int being 4 bytes
1026 out = (int *)outdata;
1028 fracstep = inwidth*0x10000/outwidth;
1029 for (i = 0;i < outheight;i++)
1031 inrow = (int *)indata + inwidth*(i*inheight/outheight);
1032 frac = fracstep >> 1;
1036 out[0] = inrow[frac >> 16];frac += fracstep;
1037 out[1] = inrow[frac >> 16];frac += fracstep;
1038 out[2] = inrow[frac >> 16];frac += fracstep;
1039 out[3] = inrow[frac >> 16];frac += fracstep;
1045 out[0] = inrow[frac >> 16];frac += fracstep;
1046 out[1] = inrow[frac >> 16];frac += fracstep;
1051 out[0] = inrow[frac >> 16];frac += fracstep;
1057 void Image_Resample24Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1059 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
1061 const unsigned char *inrow;
1062 unsigned char *resamplerow1;
1063 unsigned char *resamplerow2;
1064 out = (unsigned char *)outdata;
1065 fstep = (int) (inheight*65536.0f/outheight);
1067 resamplerow1 = (unsigned char *)Mem_Alloc(tempmempool, outwidth*3*2);
1068 resamplerow2 = resamplerow1 + outwidth*3;
1070 inrow = (const unsigned char *)indata;
1072 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1073 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1074 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1082 inrow = (unsigned char *)indata + inwidth3*yi;
1084 memcpy(resamplerow1, resamplerow2, outwidth3);
1086 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1087 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1131 resamplerow1 -= outwidth3;
1132 resamplerow2 -= outwidth3;
1138 inrow = (unsigned char *)indata + inwidth3*yi;
1140 memcpy(resamplerow1, resamplerow2, outwidth3);
1142 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1145 memcpy(out, resamplerow1, outwidth3);
1148 Mem_Free(resamplerow1);
1149 resamplerow1 = NULL;
1150 resamplerow2 = NULL;
1153 void Image_Resample24Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1155 int i, j, f, inwidth3 = inwidth * 3;
1156 unsigned frac, fracstep;
1157 unsigned char *inrow, *out;
1158 out = (unsigned char *)outdata;
1160 fracstep = inwidth*0x10000/outwidth;
1161 for (i = 0;i < outheight;i++)
1163 inrow = (unsigned char *)indata + inwidth3*(i*inheight/outheight);
1164 frac = fracstep >> 1;
1168 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1169 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1170 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1171 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1176 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1177 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1182 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1193 void Image_Resample (const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int bytesperpixel, int quality)
1195 if (indepth != 1 || outdepth != 1)
1197 Con_Printf ("Image_Resample: 3D resampling not supported\n");
1200 if (bytesperpixel == 4)
1203 Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1205 Image_Resample32Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1207 else if (bytesperpixel == 3)
1210 Image_Resample24Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1212 Image_Resample24Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1215 Con_Printf ("Image_Resample: unsupported bytesperpixel %i\n", bytesperpixel);
1218 // in can be the same as out
1219 void Image_MipReduce(const unsigned char *in, unsigned char *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth, int bytesperpixel)
1222 if (*depth != 1 || destdepth != 1)
1224 Con_Printf ("Image_Resample: 3D resampling not supported\n");
1227 nextrow = *width * bytesperpixel;
1228 if (*width > destwidth)
1231 if (*height > destheight)
1235 if (bytesperpixel == 4)
1237 for (y = 0;y < *height;y++)
1239 for (x = 0;x < *width;x++)
1241 out[0] = (unsigned char) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
1242 out[1] = (unsigned char) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1243 out[2] = (unsigned char) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1244 out[3] = (unsigned char) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1248 in += nextrow; // skip a line
1251 else if (bytesperpixel == 3)
1253 for (y = 0;y < *height;y++)
1255 for (x = 0;x < *width;x++)
1257 out[0] = (unsigned char) ((in[0] + in[3] + in[nextrow ] + in[nextrow+3]) >> 2);
1258 out[1] = (unsigned char) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
1259 out[2] = (unsigned char) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
1263 in += nextrow; // skip a line
1267 Con_Printf ("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1272 if (bytesperpixel == 4)
1274 for (y = 0;y < *height;y++)
1276 for (x = 0;x < *width;x++)
1278 out[0] = (unsigned char) ((in[0] + in[4]) >> 1);
1279 out[1] = (unsigned char) ((in[1] + in[5]) >> 1);
1280 out[2] = (unsigned char) ((in[2] + in[6]) >> 1);
1281 out[3] = (unsigned char) ((in[3] + in[7]) >> 1);
1287 else if (bytesperpixel == 3)
1289 for (y = 0;y < *height;y++)
1291 for (x = 0;x < *width;x++)
1293 out[0] = (unsigned char) ((in[0] + in[3]) >> 1);
1294 out[1] = (unsigned char) ((in[1] + in[4]) >> 1);
1295 out[2] = (unsigned char) ((in[2] + in[5]) >> 1);
1302 Con_Printf ("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1307 if (*height > destheight)
1311 if (bytesperpixel == 4)
1313 for (y = 0;y < *height;y++)
1315 for (x = 0;x < *width;x++)
1317 out[0] = (unsigned char) ((in[0] + in[nextrow ]) >> 1);
1318 out[1] = (unsigned char) ((in[1] + in[nextrow+1]) >> 1);
1319 out[2] = (unsigned char) ((in[2] + in[nextrow+2]) >> 1);
1320 out[3] = (unsigned char) ((in[3] + in[nextrow+3]) >> 1);
1324 in += nextrow; // skip a line
1327 else if (bytesperpixel == 3)
1329 for (y = 0;y < *height;y++)
1331 for (x = 0;x < *width;x++)
1333 out[0] = (unsigned char) ((in[0] + in[nextrow ]) >> 1);
1334 out[1] = (unsigned char) ((in[1] + in[nextrow+1]) >> 1);
1335 out[2] = (unsigned char) ((in[2] + in[nextrow+2]) >> 1);
1339 in += nextrow; // skip a line
1343 Con_Printf ("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1346 Con_Printf ("Image_MipReduce: desired size already achieved\n");
1350 void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
1353 const unsigned char *p0, *p1, *p2;
1355 float iwidth, iheight, ibumpscale, n[3];
1356 iwidth = 1.0f / width;
1357 iheight = 1.0f / height;
1358 ibumpscale = (255.0f * 3.0f) / bumpscale;
1360 for (y = 0;y < height;y++)
1362 for (x = 0;x < width;x++)
1364 p0 = inpixels + (y * width + x) * 4;
1368 p1 = inpixels + (y * width + x) * 4;
1370 p1 = inpixels + (y * width) * 4;
1373 p1 = inpixels + (y * width + x + 1) * 4;
1374 if (y == height - 1)
1377 p2 = inpixels + (y * width + x) * 4;
1379 p2 = inpixels + x * 4;
1382 p2 = inpixels + ((y + 1) * width + x) * 4;
1386 dv[0][2] = ((p0[0] + p0[1] + p0[2]) * ibumpscale) - ((p1[0] + p1[1] + p1[2]) * ibumpscale);
1389 dv[1][2] = ((p2[0] + p2[1] + p2[2]) * ibumpscale) - ((p0[0] + p0[1] + p0[2]) * ibumpscale);
1390 n[0] = dv[0][1]*dv[1][2]-dv[0][2]*dv[1][1];
1391 n[1] = dv[0][2]*dv[1][0]-dv[0][0]*dv[1][2];
1392 n[2] = dv[0][0]*dv[1][1]-dv[0][1]*dv[1][0];
1394 n[0] = ((p0[0] + p0[1] + p0[2]) - (p1[0] + p1[1] + p1[2]));
1395 n[1] = ((p2[0] + p2[1] + p2[2]) - (p0[0] + p0[1] + p0[2]));
1399 // this should work for the bottom right triangle if anyone wants
1400 // code for that for some reason
1401 n[0] = ((p3[0] + p3[1] + p3[2]) - (p1[0] + p1[1] + p1[2]));
1402 n[1] = ((p2[0] + p2[1] + p2[2]) - (p3[0] + p3[1] + p3[2]));
1406 out[0] = (int)(128.0f + n[0] * 127.0f);
1407 out[1] = (int)(128.0f + n[1] * 127.0f);
1408 out[2] = (int)(128.0f + n[2] * 127.0f);
1409 out[3] = (p0[0] + p0[1] + p0[2]) / 3;
1415 int image_loadskin(imageskin_t *s, const char *shadername)
1418 unsigned char *bumppixels;
1419 int bumppixels_width, bumppixels_height;
1420 char name[MAX_QPATH];
1421 strlcpy(name, shadername, sizeof(name));
1422 Image_StripImageExtension(name, name);
1423 memset(s, 0, sizeof(*s));
1424 s->basepixels = loadimagepixels(name, false, 0, 0);
1425 if (s->basepixels == NULL)
1427 s->basepixels_width = image_width;
1428 s->basepixels_height = image_height;
1430 bumppixels = NULL;bumppixels_width = 0;bumppixels_height = 0;
1431 for (j = 3;j < s->basepixels_width * s->basepixels_height * 4;j += 4)
1432 if (s->basepixels[j] < 255)
1434 if (j < s->basepixels_width * s->basepixels_height * 4)
1436 // has transparent pixels
1437 s->maskpixels = (unsigned char *)Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1438 s->maskpixels_width = s->basepixels_width;
1439 s->maskpixels_height = s->basepixels_height;
1440 memcpy(s->maskpixels, s->basepixels, s->maskpixels_width * s->maskpixels_height * 4);
1441 for (j = 0;j < s->basepixels_width * s->basepixels_height * 4;j += 4)
1443 s->maskpixels[j+0] = 255;
1444 s->maskpixels[j+1] = 255;
1445 s->maskpixels[j+2] = 255;
1449 // _luma is supported for tenebrae compatibility
1450 // (I think it's a very stupid name, but oh well)
1451 if ((s->glowpixels = loadimagepixels(va("%s_glow", name), false, 0, 0)) != NULL
1452 || (s->glowpixels = loadimagepixels(va("%s_luma", name), false, 0, 0)) != NULL)
1454 s->glowpixels_width = image_width;
1455 s->glowpixels_height = image_height;
1457 // _norm is the name used by tenebrae
1458 // (I don't like the name much)
1459 if ((s->nmappixels = loadimagepixels(va("%s_norm", name), false, 0, 0)) != NULL)
1461 s->nmappixels_width = image_width;
1462 s->nmappixels_height = image_height;
1464 else if ((bumppixels = loadimagepixels(va("%s_bump", name), false, 0, 0)) != NULL)
1466 bumppixels_width = image_width;
1467 bumppixels_height = image_height;
1469 if ((s->glosspixels = loadimagepixels(va("%s_gloss", name), false, 0, 0)) != NULL)
1471 s->glosspixels_width = image_width;
1472 s->glosspixels_height = image_height;
1474 if ((s->pantspixels = loadimagepixels(va("%s_pants", name), false, 0, 0)) != NULL)
1476 s->pantspixels_width = image_width;
1477 s->pantspixels_height = image_height;
1479 if ((s->shirtpixels = loadimagepixels(va("%s_shirt", name), false, 0, 0)) != NULL)
1481 s->shirtpixels_width = image_width;
1482 s->shirtpixels_height = image_height;
1485 if (s->nmappixels == NULL)
1487 if (bumppixels != NULL)
1489 if (r_shadow_bumpscale_bumpmap.value > 0)
1491 s->nmappixels = (unsigned char *)Mem_Alloc(loadmodel->mempool, bumppixels_width * bumppixels_height * 4);
1492 s->nmappixels_width = bumppixels_width;
1493 s->nmappixels_height = bumppixels_height;
1494 Image_HeightmapToNormalmap(bumppixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_bumpmap.value);
1499 if (r_shadow_bumpscale_basetexture.value > 0)
1501 s->nmappixels = (unsigned char *)Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1502 s->nmappixels_width = s->basepixels_width;
1503 s->nmappixels_height = s->basepixels_height;
1504 Image_HeightmapToNormalmap(s->basepixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_basetexture.value);
1508 if (bumppixels != NULL)
1509 Mem_Free(bumppixels);
1513 void image_freeskin(imageskin_t *s)
1516 Mem_Free(s->basepixels);
1518 Mem_Free(s->maskpixels);
1520 Mem_Free(s->nmappixels);
1522 Mem_Free(s->glowpixels);
1524 Mem_Free(s->glosspixels);
1526 Mem_Free(s->pantspixels);
1528 Mem_Free(s->shirtpixels);
1529 memset(s, 0, sizeof(*s));