10 void Image_CopyMux(qbyte *outpixels, const qbyte *inpixels, int inputwidth, int inputheight, qboolean inputflipx, qboolean inputflipy, qboolean inputflipdiagonal, int numoutputcomponents, int numinputcomponents, int *outputinputcomponentindices)
13 const qbyte *in, *inrow, *incolumn;
14 if (inputflipdiagonal)
16 for (x = 0;x < inputwidth;x++)
18 incolumn = inpixels + (inputflipx ? inputwidth - 1 - x : x) * numinputcomponents;
19 for (y = 0;y < inputheight;y++)
21 in = incolumn + (inputflipy ? inputheight - 1 - y : y) * inputwidth * numinputcomponents;
22 for (c = 0;c < numoutputcomponents;c++)
24 index = outputinputcomponentindices[c];
25 *outpixels++ = (index & 0x80000000) ? (index - 0x8000000) : in[index];
32 for (y = 0;y < inputheight;y++)
34 inrow = inpixels + (inputflipy ? inputheight - 1 - y : y) * inputwidth * numinputcomponents;
35 for (x = 0;x < inputwidth;x++)
37 in = inrow + (inputflipx ? inputwidth - 1 - x : x) * numinputcomponents;
38 for (c = 0;c < numoutputcomponents;c++)
40 index = outputinputcomponentindices[c];
41 *outpixels++ = (index & 0x80000000) ? (index - 0x8000000) : in[index];
48 void Image_GammaRemapRGB(const qbyte *in, qbyte *out, int pixels, const qbyte *gammar, const qbyte *gammag, const qbyte *gammab)
52 out[0] = gammar[in[0]];
53 out[1] = gammag[in[1]];
54 out[2] = gammab[in[2]];
60 // note: pal must be 32bit color
61 void Image_Copy8bitRGBA(const qbyte *in, qbyte *out, int pixels, const unsigned int *pal)
63 int *iout = (void *)out;
99 =================================================================
103 =================================================================
112 unsigned short xmin,ymin,xmax,ymax;
113 unsigned short hres,vres;
114 unsigned char palette[48];
117 unsigned short bytes_per_line;
118 unsigned short palette_type;
127 qbyte* LoadPCX (qbyte *f, int matchwidth, int matchheight)
130 qbyte *a, *b, *image_rgba, *pbuf;
131 const qbyte *palette, *fin, *enddata;
132 int x, y, x2, dataByte;
134 if (fs_filesize < (int)sizeof(pcx) + 768)
136 Con_Print("Bad pcx file\n");
142 memcpy(&pcx, fin, sizeof(pcx));
145 // LordHavoc: big-endian support ported from QF newtree
146 pcx.xmax = LittleShort (pcx.xmax);
147 pcx.xmin = LittleShort (pcx.xmin);
148 pcx.ymax = LittleShort (pcx.ymax);
149 pcx.ymin = LittleShort (pcx.ymin);
150 pcx.hres = LittleShort (pcx.hres);
151 pcx.vres = LittleShort (pcx.vres);
152 pcx.bytes_per_line = LittleShort (pcx.bytes_per_line);
153 pcx.palette_type = LittleShort (pcx.palette_type);
155 image_width = pcx.xmax + 1 - pcx.xmin;
156 image_height = pcx.ymax + 1 - pcx.ymin;
157 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)
159 Con_Print("Bad pcx file\n");
162 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
165 palette = f + fs_filesize - 768;
167 image_rgba = Mem_Alloc(tempmempool, image_width*image_height*4);
170 Con_Printf("LoadPCX: not enough memory for %i by %i image\n", image_width, image_height);
173 pbuf = image_rgba + image_width*image_height*3;
176 for (y = 0;y < image_height && fin < enddata;y++)
178 a = pbuf + y * image_width;
179 for (x = 0;x < image_width && fin < enddata;)
186 x2 = x + (dataByte & 0x3F);
188 if (x2 > image_width)
189 x2 = image_width; // technically an error
196 fin += pcx.bytes_per_line - image_width; // the number of bytes per line is always forced to an even number
197 while(x < image_width)
204 for(x = 0;x < image_width*image_height;x++)
217 =========================================================
221 =========================================================
224 typedef struct _TargaHeader
226 unsigned char id_length, colormap_type, image_type;
227 unsigned short colormap_index, colormap_length;
228 unsigned char colormap_size;
229 unsigned short x_origin, y_origin, width, height;
230 unsigned char pixel_size, attributes;
234 void PrintTargaHeader(TargaHeader *t)
236 Con_Print("TargaHeader:\n");
237 Con_Printf("uint8 id_length = %i;\n", t->id_length);
238 Con_Printf("uint8 colormap_type = %i;\n", t->colormap_type);
239 Con_Printf("uint8 image_type = %i;\n", t->image_type);
240 Con_Printf("uint16 colormap_index = %i;\n", t->colormap_index);
241 Con_Printf("uint16 colormap_length = %i;\n", t->colormap_length);
242 Con_Printf("uint8 colormap_size = %i;\n", t->colormap_size);
243 Con_Printf("uint16 x_origin = %i;\n", t->x_origin);
244 Con_Printf("uint16 y_origin = %i;\n", t->y_origin);
245 Con_Printf("uint16 width = %i;\n", t->width);
246 Con_Printf("uint16 height = %i;\n", t->height);
247 Con_Printf("uint8 pixel_size = %i;\n", t->pixel_size);
248 Con_Printf("uint8 attributes = %i;\n", t->attributes);
256 qbyte *LoadTGA (qbyte *f, int matchwidth, int matchheight)
258 int x, y, row_inc, compressed, readpixelcount, red, green, blue, alpha, runlen, pindex;
259 qbyte *pixbuf, *image_rgba;
260 qbyte *fin, *enddata;
261 TargaHeader targa_header;
262 unsigned char palette[256*4], *p;
264 if (fs_filesize < 19)
267 enddata = f + fs_filesize;
269 targa_header.id_length = f[0];
270 targa_header.colormap_type = f[1];
271 targa_header.image_type = f[2];
273 targa_header.colormap_index = f[3] + f[4] * 256;
274 targa_header.colormap_length = f[5] + f[6] * 256;
275 targa_header.colormap_size = f[7];
276 targa_header.x_origin = f[8] + f[9] * 256;
277 targa_header.y_origin = f[10] + f[11] * 256;
278 targa_header.width = image_width = f[12] + f[13] * 256;
279 targa_header.height = image_height = f[14] + f[15] * 256;
280 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
282 Con_Print("LoadTGA: invalid size\n");
283 PrintTargaHeader(&targa_header);
286 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
288 targa_header.pixel_size = f[16];
289 targa_header.attributes = f[17];
292 if (targa_header.id_length != 0)
293 fin += targa_header.id_length; // skip TARGA image comment
294 if (targa_header.image_type == 2 || targa_header.image_type == 10)
296 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
298 Con_Print("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
299 PrintTargaHeader(&targa_header);
303 else if (targa_header.image_type == 1 || targa_header.image_type == 9)
305 if (targa_header.pixel_size != 8)
307 Con_Print("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
308 PrintTargaHeader(&targa_header);
311 if (targa_header.colormap_length > 256)
313 Con_Print("LoadTGA: only up to 256 colormap_length supported\n");
314 PrintTargaHeader(&targa_header);
317 if (targa_header.colormap_index)
319 Con_Print("LoadTGA: colormap_index not supported\n");
320 PrintTargaHeader(&targa_header);
323 if (targa_header.colormap_size == 24)
325 for (x = 0;x < targa_header.colormap_length;x++)
327 palette[x*4+2] = *fin++;
328 palette[x*4+1] = *fin++;
329 palette[x*4+0] = *fin++;
330 palette[x*4+3] = 255;
333 else if (targa_header.colormap_size == 32)
335 for (x = 0;x < targa_header.colormap_length;x++)
337 palette[x*4+2] = *fin++;
338 palette[x*4+1] = *fin++;
339 palette[x*4+0] = *fin++;
340 palette[x*4+3] = *fin++;
345 Con_Print("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
346 PrintTargaHeader(&targa_header);
350 else if (targa_header.image_type == 3 || targa_header.image_type == 11)
352 if (targa_header.pixel_size != 8)
354 Con_Print("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
355 PrintTargaHeader(&targa_header);
361 Con_Printf("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
362 PrintTargaHeader(&targa_header);
366 if (targa_header.attributes & 0x10)
368 Con_Print("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
372 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
375 Con_Printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
379 // If bit 5 of attributes isn't set, the image has been stored from bottom to top
380 if ((targa_header.attributes & 0x20) == 0)
382 pixbuf = image_rgba + (image_height - 1)*image_width*4;
383 row_inc = -image_width*4*2;
391 compressed = targa_header.image_type == 9 || targa_header.image_type == 10 || targa_header.image_type == 11;
394 red = green = blue = alpha = 255;
395 while (y < image_height)
397 // decoder is mostly the same whether it's compressed or not
398 readpixelcount = 1000000;
400 if (compressed && fin < enddata)
403 // high bit indicates this is an RLE compressed run
406 runlen = 1 + (runlen & 0x7f);
409 while((runlen--) && y < image_height)
411 if (readpixelcount > 0)
414 red = green = blue = alpha = 255;
417 switch(targa_header.image_type)
423 if (pindex >= targa_header.colormap_length)
425 p = palette + pindex * 4;
439 if (targa_header.pixel_size == 32 && fin < enddata)
445 red = green = blue = *fin++;
455 if (x == image_width)
457 // end of line, advance to next
473 qbyte *LoadLMP (qbyte *f, int matchwidth, int matchheight)
479 Con_Print("LoadLMP: invalid LMP file\n");
483 // parse the very complicated header *chuckle*
484 image_width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
485 image_height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
486 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
488 Con_Printf("LoadLMP: invalid size %ix%i\n", image_width, image_height);
491 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
494 if (fs_filesize < 8 + image_width * image_height)
496 Con_Print("LoadLMP: invalid LMP file\n");
500 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
503 Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
506 Image_Copy8bitRGBA(f + 8, image_rgba, image_width * image_height, palette_complete);
513 unsigned width, height;
514 unsigned offsets[MIPLEVELS]; // four mip maps stored
515 char animname[32]; // next frame in animation chain
521 qbyte *LoadWAL (qbyte *f, int matchwidth, int matchheight)
524 const q2wal_t *inwal = (const void *)f;
526 if (fs_filesize < (int) sizeof(q2wal_t))
528 Con_Print("LoadWAL: invalid WAL file\n");
532 image_width = LittleLong(inwal->width);
533 image_height = LittleLong(inwal->height);
534 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
536 Con_Printf("LoadWAL: invalid size %ix%i\n", image_width, image_height);
539 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
542 if ((int) fs_filesize < (int) sizeof(q2wal_t) + (int) LittleLong(inwal->offsets[0]) + image_width * image_height)
544 Con_Print("LoadWAL: invalid WAL file\n");
548 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
551 Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
554 Image_Copy8bitRGBA(f + LittleLong(inwal->offsets[0]), image_rgba, image_width * image_height, palette_complete);
565 qbyte *LoadLMPAs8Bit (qbyte *f, int matchwidth, int matchheight)
571 Con_Print("LoadLMPAs8Bit: invalid LMP file\n");
575 // parse the very complicated header *chuckle*
576 image_width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
577 image_height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
578 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
580 Con_Printf("LoadLMPAs8Bit: invalid size %ix%i\n", image_width, image_height);
583 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
586 if (fs_filesize < 8 + image_width * image_height)
588 Con_Print("LoadLMPAs8Bit: invalid LMP file\n");
592 image_8bit = Mem_Alloc(tempmempool, image_width * image_height);
595 Con_Printf("LoadLMPAs8Bit: not enough memory for %i by %i image\n", image_width, image_height);
598 memcpy(image_8bit, f + 8, image_width * image_height);
602 void Image_StripImageExtension (const char *in, char *out)
604 const char *end, *temp;
605 end = in + strlen(in);
609 if (strcmp(temp, ".tga") == 0
610 || strcmp(temp, ".pcx") == 0
611 || strcmp(temp, ".lmp") == 0
612 || strcmp(temp, ".png") == 0
613 || strcmp(temp, ".jpg") == 0)
625 const char *formatstring;
626 qbyte *(*loadfunc)(qbyte *f, int matchwidth, int matchheight);
630 {"override/%s.tga", LoadTGA},
631 {"override/%s.jpg", JPEG_LoadImage},
632 {"textures/%s.tga", LoadTGA},
633 {"textures/%s.jpg", JPEG_LoadImage},
634 {"textures/%s.pcx", LoadPCX},
635 {"textures/%s.wal", LoadWAL},
637 {"%s.jpg", JPEG_LoadImage},
643 qbyte *loadimagepixels (const char *filename, qboolean complain, int matchwidth, int matchheight)
646 qbyte *f, *data = NULL;
647 char basename[MAX_QPATH], name[MAX_QPATH], *c;
648 if (developer_memorydebug.integer)
649 Mem_CheckSentinelsGlobal();
650 Image_StripImageExtension(filename, basename); // strip filename extensions to allow replacement by other types
651 // replace *'s with #, so commandline utils don't get confused when dealing with the external files
652 for (c = basename;*c;c++)
655 for (i = 0;imageformats[i].formatstring;i++)
657 sprintf (name, imageformats[i].formatstring, basename);
658 f = FS_LoadFile(name, tempmempool, true);
661 data = imageformats[i].loadfunc(f, matchwidth, matchheight);
665 Con_DPrintf("loaded image %s (%dx%d)\n", name, image_width, image_height);
666 if (developer_memorydebug.integer)
667 Mem_CheckSentinelsGlobal();
674 Con_Printf("Couldn't load %s using ", filename);
675 for (i = 0;imageformats[i].formatstring;i++)
677 sprintf (name, imageformats[i].formatstring, basename);
678 Con_Printf(i == 0 ? "\"%s\"" : (imageformats[i+1].formatstring ? ", \"%s\"" : " or \"%s\".\n"), imageformats[i].formatstring);
681 if (developer_memorydebug.integer)
682 Mem_CheckSentinelsGlobal();
686 int image_makemask (const qbyte *in, qbyte *out, int size)
690 for (i = 0;i < size;i++)
692 out[0] = out[1] = out[2] = 255;
702 qbyte* loadimagepixelsmask (const char *filename, qboolean complain, int matchwidth, int matchheight)
705 in = data = loadimagepixels(filename, complain, matchwidth, matchheight);
708 if (image_makemask(data, data, image_width * image_height))
709 return data; // some transparency
713 return NULL; // all opaque
717 rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
721 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
723 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
728 rtexture_t *loadtextureimagemask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
732 if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
734 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
739 rtexture_t *image_masktex;
740 rtexture_t *image_nmaptex;
741 rtexture_t *loadtextureimagewithmask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
745 image_masktex = NULL;
746 image_nmaptex = NULL;
747 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
750 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
752 if (flags & TEXF_ALPHA && image_makemask(data, data, image_width * image_height))
753 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
759 rtexture_t *loadtextureimagewithmaskandnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
763 image_masktex = NULL;
764 image_nmaptex = NULL;
765 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
768 data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
770 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
772 Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
773 image_nmaptex = R_LoadTexture2D(pool, va("%s_nmap", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
775 if (flags & TEXF_ALPHA && image_makemask(data, data2, image_width * image_height))
776 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
784 rtexture_t *loadtextureimagebumpasnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
788 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
790 data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
792 Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
793 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
800 qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int height, const qbyte *data)
804 const qbyte *in, *end;
806 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
808 memset (buffer, 0, 18);
809 buffer[2] = 2; // uncompressed type
810 buffer[12] = (width >> 0) & 0xFF;
811 buffer[13] = (width >> 8) & 0xFF;
812 buffer[14] = (height >> 0) & 0xFF;
813 buffer[15] = (height >> 8) & 0xFF;
814 buffer[16] = 24; // pixel size
819 end = in + width*height*3;
820 for (;in < end;in += 3)
826 ret = FS_WriteFile (filename, buffer, width*height*3 + 18 );
832 void Image_WriteTGARGB (const char *filename, int width, int height, const qbyte *data)
836 const qbyte *in, *end;
838 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
840 memset (buffer, 0, 18);
841 buffer[2] = 2; // uncompressed type
842 buffer[12] = (width >> 0) & 0xFF;
843 buffer[13] = (width >> 8) & 0xFF;
844 buffer[14] = (height >> 0) & 0xFF;
845 buffer[15] = (height >> 8) & 0xFF;
846 buffer[16] = 24; // pixel size
848 // swap rgb to bgr and flip upside down
850 for (y = height - 1;y >= 0;y--)
852 in = data + y * width * 3;
853 end = in + width * 3;
854 for (;in < end;in += 3)
861 FS_WriteFile (filename, buffer, width*height*3 + 18 );
866 void Image_WriteTGARGBA (const char *filename, int width, int height, const qbyte *data)
870 const qbyte *in, *end;
872 buffer = Mem_Alloc(tempmempool, width*height*4 + 18);
874 memset (buffer, 0, 18);
875 buffer[2] = 2; // uncompressed type
876 buffer[12] = (width >> 0) & 0xFF;
877 buffer[13] = (width >> 8) & 0xFF;
878 buffer[14] = (height >> 0) & 0xFF;
879 buffer[15] = (height >> 8) & 0xFF;
880 buffer[16] = 32; // pixel size
882 // swap rgba to bgra and flip upside down
884 for (y = height - 1;y >= 0;y--)
886 in = data + y * width * 4;
887 end = in + width * 4;
888 for (;in < end;in += 4)
896 FS_WriteFile (filename, buffer, width*height*4 + 18 );
901 qboolean Image_CheckAlpha(const qbyte *data, int size, qboolean rgba)
907 for (end = data + size * 4, data += 3;data < end;data += 4)
913 // color 255 is transparent
914 for (end = data + size;data < end;data++)
921 static void Image_Resample32LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
923 int j, xi, oldx = 0, f, fstep, endx, lerp;
924 fstep = (int) (inwidth*65536.0f/outwidth);
926 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
931 in += (xi - oldx) * 4;
937 *out++ = (qbyte) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
938 *out++ = (qbyte) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
939 *out++ = (qbyte) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
940 *out++ = (qbyte) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
942 else // last pixel of the line has no pixel to lerp to
952 static void Image_Resample24LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
954 int j, xi, oldx = 0, f, fstep, endx, lerp;
955 fstep = (int) (inwidth*65536.0f/outwidth);
957 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
962 in += (xi - oldx) * 3;
968 *out++ = (qbyte) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
969 *out++ = (qbyte) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
970 *out++ = (qbyte) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
972 else // last pixel of the line has no pixel to lerp to
981 int resamplerowsize = 0;
982 qbyte *resamplerow1 = NULL;
983 qbyte *resamplerow2 = NULL;
984 mempool_t *resamplemempool = NULL;
986 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (qbyte) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
987 void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
989 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
993 fstep = (int) (inheight*65536.0f/outheight);
997 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
998 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
999 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1007 inrow = (qbyte *)indata + inwidth4*yi;
1009 memcpy(resamplerow1, resamplerow2, outwidth4);
1011 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1012 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1063 resamplerow1 -= outwidth4;
1064 resamplerow2 -= outwidth4;
1070 inrow = (qbyte *)indata + inwidth4*yi;
1072 memcpy(resamplerow1, resamplerow2, outwidth4);
1074 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1077 memcpy(out, resamplerow1, outwidth4);
1082 void Image_Resample32Nearest(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1085 unsigned frac, fracstep;
1086 // relies on int being 4 bytes
1090 fracstep = inwidth*0x10000/outwidth;
1091 for (i = 0;i < outheight;i++)
1093 inrow = (int *)indata + inwidth*(i*inheight/outheight);
1094 frac = fracstep >> 1;
1098 out[0] = inrow[frac >> 16];frac += fracstep;
1099 out[1] = inrow[frac >> 16];frac += fracstep;
1100 out[2] = inrow[frac >> 16];frac += fracstep;
1101 out[3] = inrow[frac >> 16];frac += fracstep;
1107 out[0] = inrow[frac >> 16];frac += fracstep;
1108 out[1] = inrow[frac >> 16];frac += fracstep;
1113 out[0] = inrow[frac >> 16];frac += fracstep;
1119 void Image_Resample24Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1121 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
1125 fstep = (int) (inheight*65536.0f/outheight);
1129 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1130 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1131 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1139 inrow = (qbyte *)indata + inwidth3*yi;
1141 memcpy(resamplerow1, resamplerow2, outwidth3);
1143 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1144 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1188 resamplerow1 -= outwidth3;
1189 resamplerow2 -= outwidth3;
1195 inrow = (qbyte *)indata + inwidth3*yi;
1197 memcpy(resamplerow1, resamplerow2, outwidth3);
1199 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1202 memcpy(out, resamplerow1, outwidth3);
1207 void Image_Resample24Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1209 int i, j, f, inwidth3 = inwidth * 3;
1210 unsigned frac, fracstep;
1214 fracstep = inwidth*0x10000/outwidth;
1215 for (i = 0;i < outheight;i++)
1217 inrow = (qbyte *)indata + inwidth3*(i*inheight/outheight);
1218 frac = fracstep >> 1;
1222 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1223 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1224 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1225 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1230 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1231 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1236 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1247 void Image_Resample (const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int bytesperpixel, int quality)
1249 if (indepth != 1 || outdepth != 1)
1250 Sys_Error("Image_Resample: 3D resampling not supported\n");
1251 if (resamplerowsize < outwidth*4)
1254 Mem_Free(resamplerow1);
1255 resamplerowsize = outwidth*4;
1256 if (!resamplemempool)
1257 resamplemempool = Mem_AllocPool("Image Scaling Buffer");
1258 resamplerow1 = Mem_Alloc(resamplemempool, resamplerowsize*2);
1259 resamplerow2 = resamplerow1 + resamplerowsize;
1261 if (bytesperpixel == 4)
1264 Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1266 Image_Resample32Nearest(indata, inwidth, inheight, outdata, outwidth, outheight);
1268 else if (bytesperpixel == 3)
1271 Image_Resample24Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1273 Image_Resample24Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1276 Sys_Error("Image_Resample: unsupported bytesperpixel %i\n", bytesperpixel);
1279 // in can be the same as out
1280 void Image_MipReduce(const qbyte *in, qbyte *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth, int bytesperpixel)
1283 if (*depth != 1 || destdepth != 1)
1284 Sys_Error("Image_Resample: 3D resampling not supported\n");
1285 nextrow = *width * bytesperpixel;
1286 if (*width > destwidth)
1289 if (*height > destheight)
1293 if (bytesperpixel == 4)
1295 for (y = 0;y < *height;y++)
1297 for (x = 0;x < *width;x++)
1299 out[0] = (qbyte) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
1300 out[1] = (qbyte) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1301 out[2] = (qbyte) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1302 out[3] = (qbyte) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1306 in += nextrow; // skip a line
1309 else if (bytesperpixel == 3)
1311 for (y = 0;y < *height;y++)
1313 for (x = 0;x < *width;x++)
1315 out[0] = (qbyte) ((in[0] + in[3] + in[nextrow ] + in[nextrow+3]) >> 2);
1316 out[1] = (qbyte) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
1317 out[2] = (qbyte) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
1321 in += nextrow; // skip a line
1325 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1330 if (bytesperpixel == 4)
1332 for (y = 0;y < *height;y++)
1334 for (x = 0;x < *width;x++)
1336 out[0] = (qbyte) ((in[0] + in[4]) >> 1);
1337 out[1] = (qbyte) ((in[1] + in[5]) >> 1);
1338 out[2] = (qbyte) ((in[2] + in[6]) >> 1);
1339 out[3] = (qbyte) ((in[3] + in[7]) >> 1);
1345 else if (bytesperpixel == 3)
1347 for (y = 0;y < *height;y++)
1349 for (x = 0;x < *width;x++)
1351 out[0] = (qbyte) ((in[0] + in[3]) >> 1);
1352 out[1] = (qbyte) ((in[1] + in[4]) >> 1);
1353 out[2] = (qbyte) ((in[2] + in[5]) >> 1);
1360 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1365 if (*height > destheight)
1369 if (bytesperpixel == 4)
1371 for (y = 0;y < *height;y++)
1373 for (x = 0;x < *width;x++)
1375 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1376 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1377 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1378 out[3] = (qbyte) ((in[3] + in[nextrow+3]) >> 1);
1382 in += nextrow; // skip a line
1385 else if (bytesperpixel == 3)
1387 for (y = 0;y < *height;y++)
1389 for (x = 0;x < *width;x++)
1391 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1392 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1393 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1397 in += nextrow; // skip a line
1401 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1404 Sys_Error("Image_MipReduce: desired size already achieved\n");
1408 void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
1411 const unsigned char *p0, *p1, *p2;
1413 float iwidth, iheight, ibumpscale, n[3];
1414 iwidth = 1.0f / width;
1415 iheight = 1.0f / height;
1416 ibumpscale = (255.0f * 3.0f) / bumpscale;
1418 for (y = 0;y < height;y++)
1420 for (x = 0;x < width;x++)
1422 p0 = inpixels + (y * width + x) * 4;
1426 p1 = inpixels + (y * width + x) * 4;
1428 p1 = inpixels + (y * width) * 4;
1431 p1 = inpixels + (y * width + x + 1) * 4;
1432 if (y == height - 1)
1435 p2 = inpixels + (y * width + x) * 4;
1437 p2 = inpixels + x * 4;
1440 p2 = inpixels + ((y + 1) * width + x) * 4;
1444 dv[0][2] = ((p0[0] + p0[1] + p0[2]) * ibumpscale) - ((p1[0] + p1[1] + p1[2]) * ibumpscale);
1447 dv[1][2] = ((p2[0] + p2[1] + p2[2]) * ibumpscale) - ((p0[0] + p0[1] + p0[2]) * ibumpscale);
1448 n[0] = dv[0][1]*dv[1][2]-dv[0][2]*dv[1][1];
1449 n[1] = dv[0][2]*dv[1][0]-dv[0][0]*dv[1][2];
1450 n[2] = dv[0][0]*dv[1][1]-dv[0][1]*dv[1][0];
1452 n[0] = ((p1[0] + p1[1] + p1[2]) - (p0[0] + p0[1] + p0[2]));
1453 n[1] = ((p0[0] + p0[1] + p0[2]) - (p2[0] + p2[1] + p2[2]));
1457 // this should work for the bottom right triangle if anyone wants
1458 // code for that for some reason
1459 n[0] = ((p3[0] + p3[1] + p3[2]) - (p1[0] + p1[1] + p1[2]));
1460 n[1] = ((p2[0] + p2[1] + p2[2]) - (p3[0] + p3[1] + p3[2]));
1464 out[0] = 128.0f + n[0] * 127.0f;
1465 out[1] = 128.0f + n[1] * 127.0f;
1466 out[2] = 128.0f + n[2] * 127.0f;
1473 int image_loadskin(imageskin_t *s, char *shadername)
1477 int bumppixels_width, bumppixels_height;
1478 char name[MAX_QPATH];
1479 Image_StripImageExtension(shadername, name);
1480 memset(s, 0, sizeof(*s));
1481 s->basepixels = loadimagepixels(name, false, 0, 0);
1482 if (s->basepixels == NULL)
1484 s->basepixels_width = image_width;
1485 s->basepixels_height = image_height;
1487 bumppixels = NULL;bumppixels_width = 0;bumppixels_height = 0;
1488 if (Image_CheckAlpha(s->basepixels, s->basepixels_width * s->basepixels_height, true))
1490 s->maskpixels = Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1491 s->maskpixels_width = s->basepixels_width;
1492 s->maskpixels_height = s->basepixels_height;
1493 memcpy(s->maskpixels, s->basepixels, s->maskpixels_width * s->maskpixels_height * 4);
1494 for (j = 0;j < s->basepixels_width * s->basepixels_height * 4;j += 4)
1496 s->maskpixels[j+0] = 255;
1497 s->maskpixels[j+1] = 255;
1498 s->maskpixels[j+2] = 255;
1502 // _luma is supported for tenebrae compatibility
1503 // (I think it's a very stupid name, but oh well)
1504 if ((s->glowpixels = loadimagepixels(va("%s_glow", name), false, 0, 0)) != NULL
1505 || (s->glowpixels = loadimagepixels(va("%s_luma", name), false, 0, 0)) != NULL)
1507 s->glowpixels_width = image_width;
1508 s->glowpixels_height = image_height;
1510 // _norm is the name used by tenebrae
1511 // (I don't like the name much)
1512 if ((s->nmappixels = loadimagepixels(va("%s_norm", name), false, 0, 0)) != NULL)
1514 s->nmappixels_width = image_width;
1515 s->nmappixels_height = image_height;
1517 else if ((bumppixels = loadimagepixels(va("%s_bump", name), false, 0, 0)) != NULL)
1519 bumppixels_width = image_width;
1520 bumppixels_height = image_height;
1522 if ((s->glosspixels = loadimagepixels(va("%s_gloss", name), false, 0, 0)) != NULL)
1524 s->glosspixels_width = image_width;
1525 s->glosspixels_height = image_height;
1527 if ((s->pantspixels = loadimagepixels(va("%s_pants", name), false, 0, 0)) != NULL)
1529 s->pantspixels_width = image_width;
1530 s->pantspixels_height = image_height;
1532 if ((s->shirtpixels = loadimagepixels(va("%s_shirt", name), false, 0, 0)) != NULL)
1534 s->shirtpixels_width = image_width;
1535 s->shirtpixels_height = image_height;
1538 if (s->nmappixels == NULL)
1540 if (bumppixels != NULL)
1542 if (r_shadow_bumpscale_bumpmap.value > 0)
1544 s->nmappixels = Mem_Alloc(loadmodel->mempool, bumppixels_width * bumppixels_height * 4);
1545 s->nmappixels_width = bumppixels_width;
1546 s->nmappixels_height = bumppixels_height;
1547 Image_HeightmapToNormalmap(bumppixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_bumpmap.value);
1552 if (r_shadow_bumpscale_basetexture.value > 0)
1554 s->nmappixels = Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1555 s->nmappixels_width = s->basepixels_width;
1556 s->nmappixels_height = s->basepixels_height;
1557 Image_HeightmapToNormalmap(s->basepixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_basetexture.value);
1561 if (bumppixels != NULL)
1562 Mem_Free(bumppixels);
1566 void image_freeskin(imageskin_t *s)
1569 Mem_Free(s->basepixels);
1571 Mem_Free(s->maskpixels);
1573 Mem_Free(s->nmappixels);
1575 Mem_Free(s->glowpixels);
1577 Mem_Free(s->glosspixels);
1579 Mem_Free(s->pantspixels);
1581 Mem_Free(s->shirtpixels);
1582 memset(s, 0, sizeof(*s));