10 void Image_CopyMux(qbyte *outpixels, const qbyte *inpixels, int width, int height, int flipx, int flipy, int flipdiagonal, int numincomponents, int numoutcomponents, int *inputcomponentindices)
13 const qbyte *in, *inrow, *incolumn;
16 for (y = 0;y < height;y++)
18 incolumn = inpixels + (flipx ? width - 1 - y : y) * numincomponents;
19 for (x = 0;x < width;x++)
21 in = incolumn + (flipy ? height - 1 - x : x) * width * numincomponents;
22 for (c = 0;c < numoutcomponents;c++)
24 index = inputcomponentindices[c];
25 *outpixels++ = (index & 0x80000000) ? (index - 0x8000000) : in[index];
32 for (y = 0;y < height;y++)
34 inrow = inpixels + (flipy ? height - 1 - y : y) * width * numincomponents;
35 for (x = 0;x < width;x++)
37 in = inrow + (flipx ? width - 1 - x : x) * numincomponents;
38 for (c = 0;c < numoutcomponents;c++)
40 index = inputcomponentindices[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 if ((f = FS_LoadFile(name, true)) && (data = imageformats[i].loadfunc(f, matchwidth, matchheight)))
661 Con_DPrintf("loaded image %s (%dx%d)\n", name, image_width, image_height);
662 if (developer_memorydebug.integer)
663 Mem_CheckSentinelsGlobal();
669 Con_Printf("Couldn't load %s using ", filename);
670 for (i = 0;imageformats[i].formatstring;i++)
672 sprintf (name, imageformats[i].formatstring, basename);
673 Con_Printf(i == 0 ? "\"%s\"" : (imageformats[i+1].formatstring ? ", \"%s\"" : " or \"%s\".\n"), imageformats[i].formatstring);
676 if (developer_memorydebug.integer)
677 Mem_CheckSentinelsGlobal();
681 int image_makemask (const qbyte *in, qbyte *out, int size)
685 for (i = 0;i < size;i++)
687 out[0] = out[1] = out[2] = 255;
697 qbyte* loadimagepixelsmask (const char *filename, qboolean complain, int matchwidth, int matchheight)
700 in = data = loadimagepixels(filename, complain, matchwidth, matchheight);
703 if (image_makemask(data, data, image_width * image_height))
704 return data; // some transparency
708 return NULL; // all opaque
712 rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
716 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
718 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
723 rtexture_t *loadtextureimagemask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
727 if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
729 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
734 rtexture_t *image_masktex;
735 rtexture_t *image_nmaptex;
736 rtexture_t *loadtextureimagewithmask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
740 image_masktex = NULL;
741 image_nmaptex = NULL;
742 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
745 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
747 if (flags & TEXF_ALPHA && image_makemask(data, data, image_width * image_height))
748 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
754 rtexture_t *loadtextureimagewithmaskandnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
758 image_masktex = NULL;
759 image_nmaptex = NULL;
760 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
763 data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
765 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
767 Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
768 image_nmaptex = R_LoadTexture2D(pool, va("%s_nmap", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
770 if (flags & TEXF_ALPHA && image_makemask(data, data2, image_width * image_height))
771 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
779 rtexture_t *loadtextureimagebumpasnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
783 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
785 data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
787 Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
788 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
795 qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int height, const qbyte *data)
799 const qbyte *in, *end;
801 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
803 memset (buffer, 0, 18);
804 buffer[2] = 2; // uncompressed type
805 buffer[12] = (width >> 0) & 0xFF;
806 buffer[13] = (width >> 8) & 0xFF;
807 buffer[14] = (height >> 0) & 0xFF;
808 buffer[15] = (height >> 8) & 0xFF;
809 buffer[16] = 24; // pixel size
814 end = in + width*height*3;
815 for (;in < end;in += 3)
821 ret = FS_WriteFile (filename, buffer, width*height*3 + 18 );
827 void Image_WriteTGARGB (const char *filename, int width, int height, const qbyte *data)
831 const qbyte *in, *end;
833 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
835 memset (buffer, 0, 18);
836 buffer[2] = 2; // uncompressed type
837 buffer[12] = (width >> 0) & 0xFF;
838 buffer[13] = (width >> 8) & 0xFF;
839 buffer[14] = (height >> 0) & 0xFF;
840 buffer[15] = (height >> 8) & 0xFF;
841 buffer[16] = 24; // pixel size
843 // swap rgb to bgr and flip upside down
845 for (y = height - 1;y >= 0;y--)
847 in = data + y * width * 3;
848 end = in + width * 3;
849 for (;in < end;in += 3)
856 FS_WriteFile (filename, buffer, width*height*3 + 18 );
861 void Image_WriteTGARGBA (const char *filename, int width, int height, const qbyte *data)
865 const qbyte *in, *end;
867 buffer = Mem_Alloc(tempmempool, width*height*4 + 18);
869 memset (buffer, 0, 18);
870 buffer[2] = 2; // uncompressed type
871 buffer[12] = (width >> 0) & 0xFF;
872 buffer[13] = (width >> 8) & 0xFF;
873 buffer[14] = (height >> 0) & 0xFF;
874 buffer[15] = (height >> 8) & 0xFF;
875 buffer[16] = 32; // pixel size
877 // swap rgba to bgra and flip upside down
879 for (y = height - 1;y >= 0;y--)
881 in = data + y * width * 4;
882 end = in + width * 4;
883 for (;in < end;in += 4)
891 FS_WriteFile (filename, buffer, width*height*4 + 18 );
896 qboolean Image_CheckAlpha(const qbyte *data, int size, qboolean rgba)
902 for (end = data + size * 4, data += 3;data < end;data += 4)
908 // color 255 is transparent
909 for (end = data + size;data < end;data++)
916 static void Image_Resample32LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
918 int j, xi, oldx = 0, f, fstep, endx, lerp;
919 fstep = (int) (inwidth*65536.0f/outwidth);
921 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
926 in += (xi - oldx) * 4;
932 *out++ = (qbyte) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
933 *out++ = (qbyte) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
934 *out++ = (qbyte) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
935 *out++ = (qbyte) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
937 else // last pixel of the line has no pixel to lerp to
947 static void Image_Resample24LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
949 int j, xi, oldx = 0, f, fstep, endx, lerp;
950 fstep = (int) (inwidth*65536.0f/outwidth);
952 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
957 in += (xi - oldx) * 3;
963 *out++ = (qbyte) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
964 *out++ = (qbyte) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
965 *out++ = (qbyte) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
967 else // last pixel of the line has no pixel to lerp to
976 int resamplerowsize = 0;
977 qbyte *resamplerow1 = NULL;
978 qbyte *resamplerow2 = NULL;
979 mempool_t *resamplemempool = NULL;
981 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (qbyte) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
982 void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
984 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
988 fstep = (int) (inheight*65536.0f/outheight);
992 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
993 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
994 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1002 inrow = (qbyte *)indata + inwidth4*yi;
1004 memcpy(resamplerow1, resamplerow2, outwidth4);
1006 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1007 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1058 resamplerow1 -= outwidth4;
1059 resamplerow2 -= outwidth4;
1065 inrow = (qbyte *)indata + inwidth4*yi;
1067 memcpy(resamplerow1, resamplerow2, outwidth4);
1069 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1072 memcpy(out, resamplerow1, outwidth4);
1077 void Image_Resample32Nearest(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1080 unsigned frac, fracstep;
1081 // relies on int being 4 bytes
1085 fracstep = inwidth*0x10000/outwidth;
1086 for (i = 0;i < outheight;i++)
1088 inrow = (int *)indata + inwidth*(i*inheight/outheight);
1089 frac = fracstep >> 1;
1093 out[0] = inrow[frac >> 16];frac += fracstep;
1094 out[1] = inrow[frac >> 16];frac += fracstep;
1095 out[2] = inrow[frac >> 16];frac += fracstep;
1096 out[3] = inrow[frac >> 16];frac += fracstep;
1102 out[0] = inrow[frac >> 16];frac += fracstep;
1103 out[1] = inrow[frac >> 16];frac += fracstep;
1108 out[0] = inrow[frac >> 16];frac += fracstep;
1114 void Image_Resample24Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1116 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
1120 fstep = (int) (inheight*65536.0f/outheight);
1124 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1125 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1126 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1134 inrow = (qbyte *)indata + inwidth3*yi;
1136 memcpy(resamplerow1, resamplerow2, outwidth3);
1138 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1139 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1183 resamplerow1 -= outwidth3;
1184 resamplerow2 -= outwidth3;
1190 inrow = (qbyte *)indata + inwidth3*yi;
1192 memcpy(resamplerow1, resamplerow2, outwidth3);
1194 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1197 memcpy(out, resamplerow1, outwidth3);
1202 void Image_Resample24Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1204 int i, j, f, inwidth3 = inwidth * 3;
1205 unsigned frac, fracstep;
1209 fracstep = inwidth*0x10000/outwidth;
1210 for (i = 0;i < outheight;i++)
1212 inrow = (qbyte *)indata + inwidth3*(i*inheight/outheight);
1213 frac = fracstep >> 1;
1217 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1218 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1219 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1220 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;
1226 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;
1242 void Image_Resample (const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int bytesperpixel, int quality)
1244 if (indepth != 1 || outdepth != 1)
1245 Sys_Error("Image_Resample: 3D resampling not supported\n");
1246 if (resamplerowsize < outwidth*4)
1249 Mem_Free(resamplerow1);
1250 resamplerowsize = outwidth*4;
1251 if (!resamplemempool)
1252 resamplemempool = Mem_AllocPool("Image Scaling Buffer");
1253 resamplerow1 = Mem_Alloc(resamplemempool, resamplerowsize*2);
1254 resamplerow2 = resamplerow1 + resamplerowsize;
1256 if (bytesperpixel == 4)
1259 Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1261 Image_Resample32Nearest(indata, inwidth, inheight, outdata, outwidth, outheight);
1263 else if (bytesperpixel == 3)
1266 Image_Resample24Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1268 Image_Resample24Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1271 Sys_Error("Image_Resample: unsupported bytesperpixel %i\n", bytesperpixel);
1274 // in can be the same as out
1275 void Image_MipReduce(const qbyte *in, qbyte *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth, int bytesperpixel)
1278 if (*depth != 1 || destdepth != 1)
1279 Sys_Error("Image_Resample: 3D resampling not supported\n");
1280 nextrow = *width * bytesperpixel;
1281 if (*width > destwidth)
1284 if (*height > destheight)
1288 if (bytesperpixel == 4)
1290 for (y = 0;y < *height;y++)
1292 for (x = 0;x < *width;x++)
1294 out[0] = (qbyte) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
1295 out[1] = (qbyte) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1296 out[2] = (qbyte) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1297 out[3] = (qbyte) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1301 in += nextrow; // skip a line
1304 else if (bytesperpixel == 3)
1306 for (y = 0;y < *height;y++)
1308 for (x = 0;x < *width;x++)
1310 out[0] = (qbyte) ((in[0] + in[3] + in[nextrow ] + in[nextrow+3]) >> 2);
1311 out[1] = (qbyte) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
1312 out[2] = (qbyte) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
1316 in += nextrow; // skip a line
1320 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1325 if (bytesperpixel == 4)
1327 for (y = 0;y < *height;y++)
1329 for (x = 0;x < *width;x++)
1331 out[0] = (qbyte) ((in[0] + in[4]) >> 1);
1332 out[1] = (qbyte) ((in[1] + in[5]) >> 1);
1333 out[2] = (qbyte) ((in[2] + in[6]) >> 1);
1334 out[3] = (qbyte) ((in[3] + in[7]) >> 1);
1340 else if (bytesperpixel == 3)
1342 for (y = 0;y < *height;y++)
1344 for (x = 0;x < *width;x++)
1346 out[0] = (qbyte) ((in[0] + in[3]) >> 1);
1347 out[1] = (qbyte) ((in[1] + in[4]) >> 1);
1348 out[2] = (qbyte) ((in[2] + in[5]) >> 1);
1355 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1360 if (*height > destheight)
1364 if (bytesperpixel == 4)
1366 for (y = 0;y < *height;y++)
1368 for (x = 0;x < *width;x++)
1370 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1371 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1372 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1373 out[3] = (qbyte) ((in[3] + in[nextrow+3]) >> 1);
1377 in += nextrow; // skip a line
1380 else if (bytesperpixel == 3)
1382 for (y = 0;y < *height;y++)
1384 for (x = 0;x < *width;x++)
1386 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1387 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1388 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1392 in += nextrow; // skip a line
1396 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1399 Sys_Error("Image_MipReduce: desired size already achieved\n");
1403 void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
1406 const unsigned char *p0, *p1, *p2;
1408 float iwidth, iheight, ibumpscale, n[3];
1409 iwidth = 1.0f / width;
1410 iheight = 1.0f / height;
1411 ibumpscale = (255.0f * 3.0f) / bumpscale;
1413 for (y = 0;y < height;y++)
1415 for (x = 0;x < width;x++)
1417 p0 = inpixels + (y * width + x) * 4;
1421 p1 = inpixels + (y * width + x) * 4;
1423 p1 = inpixels + (y * width) * 4;
1426 p1 = inpixels + (y * width + x + 1) * 4;
1427 if (y == height - 1)
1430 p2 = inpixels + (y * width + x) * 4;
1432 p2 = inpixels + x * 4;
1435 p2 = inpixels + ((y + 1) * width + x) * 4;
1439 dv[0][2] = ((p0[0] + p0[1] + p0[2]) * ibumpscale) - ((p1[0] + p1[1] + p1[2]) * ibumpscale);
1442 dv[1][2] = ((p2[0] + p2[1] + p2[2]) * ibumpscale) - ((p0[0] + p0[1] + p0[2]) * ibumpscale);
1443 n[0] = dv[0][1]*dv[1][2]-dv[0][2]*dv[1][1];
1444 n[1] = dv[0][2]*dv[1][0]-dv[0][0]*dv[1][2];
1445 n[2] = dv[0][0]*dv[1][1]-dv[0][1]*dv[1][0];
1447 n[0] = ((p1[0] + p1[1] + p1[2]) - (p0[0] + p0[1] + p0[2]));
1448 n[1] = ((p0[0] + p0[1] + p0[2]) - (p2[0] + p2[1] + p2[2]));
1452 // this should work for the bottom right triangle if anyone wants
1453 // code for that for some reason
1454 n[0] = ((p3[0] + p3[1] + p3[2]) - (p1[0] + p1[1] + p1[2]));
1455 n[1] = ((p2[0] + p2[1] + p2[2]) - (p3[0] + p3[1] + p3[2]));
1459 out[0] = 128.0f + n[0] * 127.0f;
1460 out[1] = 128.0f + n[1] * 127.0f;
1461 out[2] = 128.0f + n[2] * 127.0f;
1468 int image_loadskin(imageskin_t *s, char *shadername)
1472 int bumppixels_width, bumppixels_height;
1473 char name[MAX_QPATH];
1474 Image_StripImageExtension(shadername, name);
1475 memset(s, 0, sizeof(*s));
1476 s->basepixels = loadimagepixels(name, false, 0, 0);
1477 if (s->basepixels == NULL)
1479 s->basepixels_width = image_width;
1480 s->basepixels_height = image_height;
1482 bumppixels = NULL;bumppixels_width = 0;bumppixels_height = 0;
1483 if (Image_CheckAlpha(s->basepixels, s->basepixels_width * s->basepixels_height, true))
1485 s->maskpixels = Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1486 s->maskpixels_width = s->basepixels_width;
1487 s->maskpixels_height = s->basepixels_height;
1488 memcpy(s->maskpixels, s->basepixels, s->maskpixels_width * s->maskpixels_height * 4);
1489 for (j = 0;j < s->basepixels_width * s->basepixels_height * 4;j += 4)
1491 s->maskpixels[j+0] = 255;
1492 s->maskpixels[j+1] = 255;
1493 s->maskpixels[j+2] = 255;
1497 // _luma is supported for tenebrae compatibility
1498 // (I think it's a very stupid name, but oh well)
1499 if ((s->glowpixels = loadimagepixels(va("%s_glow", name), false, 0, 0)) != NULL
1500 || (s->glowpixels = loadimagepixels(va("%s_luma", name), false, 0, 0)) != NULL)
1502 s->glowpixels_width = image_width;
1503 s->glowpixels_height = image_height;
1505 // _norm is the name used by tenebrae
1506 // (I don't like the name much)
1507 if ((s->nmappixels = loadimagepixels(va("%s_norm", name), false, 0, 0)) != NULL)
1509 s->nmappixels_width = image_width;
1510 s->nmappixels_height = image_height;
1512 else if ((bumppixels = loadimagepixels(va("%s_bump", name), false, 0, 0)) != NULL)
1514 bumppixels_width = image_width;
1515 bumppixels_height = image_height;
1517 if ((s->glosspixels = loadimagepixels(va("%s_gloss", name), false, 0, 0)) != NULL)
1519 s->glosspixels_width = image_width;
1520 s->glosspixels_height = image_height;
1522 if ((s->pantspixels = loadimagepixels(va("%s_pants", name), false, 0, 0)) != NULL)
1524 s->pantspixels_width = image_width;
1525 s->pantspixels_height = image_height;
1527 if ((s->shirtpixels = loadimagepixels(va("%s_shirt", name), false, 0, 0)) != NULL)
1529 s->shirtpixels_width = image_width;
1530 s->shirtpixels_height = image_height;
1533 if (s->nmappixels == NULL)
1535 if (bumppixels != NULL)
1537 if (r_shadow_bumpscale_bumpmap.value > 0)
1539 s->nmappixels = Mem_Alloc(loadmodel->mempool, bumppixels_width * bumppixels_height * 4);
1540 s->nmappixels_width = bumppixels_width;
1541 s->nmappixels_height = bumppixels_height;
1542 Image_HeightmapToNormalmap(bumppixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_bumpmap.value);
1547 if (r_shadow_bumpscale_basetexture.value > 0)
1549 s->nmappixels = Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1550 s->nmappixels_width = s->basepixels_width;
1551 s->nmappixels_height = s->basepixels_height;
1552 Image_HeightmapToNormalmap(s->basepixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_basetexture.value);
1556 if (bumppixels != NULL)
1557 Mem_Free(bumppixels);
1561 void image_freeskin(imageskin_t *s)
1564 Mem_Free(s->basepixels);
1566 Mem_Free(s->maskpixels);
1568 Mem_Free(s->nmappixels);
1570 Mem_Free(s->glowpixels);
1572 Mem_Free(s->glosspixels);
1574 Mem_Free(s->pantspixels);
1576 Mem_Free(s->shirtpixels);
1577 memset(s, 0, sizeof(*s));