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++)
23 *outpixels++ = in[inputcomponentindices[c]];
29 for (y = 0;y < height;y++)
31 inrow = inpixels + (flipy ? height - 1 - y : y) * width * numincomponents;
32 for (x = 0;x < width;x++)
34 in = inrow + (flipx ? width - 1 - x : x) * numincomponents;
35 for (c = 0;c < numoutcomponents;c++)
36 *outpixels++ = in[inputcomponentindices[c]];
42 void Image_GammaRemapRGB(const qbyte *in, qbyte *out, int pixels, const qbyte *gammar, const qbyte *gammag, const qbyte *gammab)
46 out[0] = gammar[in[0]];
47 out[1] = gammag[in[1]];
48 out[2] = gammab[in[2]];
54 // note: pal must be 32bit color
55 void Image_Copy8bitRGBA(const qbyte *in, qbyte *out, int pixels, const unsigned int *pal)
57 int *iout = (void *)out;
93 =================================================================
97 =================================================================
106 unsigned short xmin,ymin,xmax,ymax;
107 unsigned short hres,vres;
108 unsigned char palette[48];
111 unsigned short bytes_per_line;
112 unsigned short palette_type;
121 qbyte* LoadPCX (qbyte *f, int matchwidth, int matchheight)
124 qbyte *a, *b, *image_rgba, *pbuf;
125 const qbyte *palette, *fin, *enddata;
126 int x, y, x2, dataByte;
128 if (fs_filesize < (int)sizeof(pcx) + 768)
130 Con_Printf ("Bad pcx file\n");
136 memcpy(&pcx, fin, sizeof(pcx));
139 // LordHavoc: big-endian support ported from QF newtree
140 pcx.xmax = LittleShort (pcx.xmax);
141 pcx.xmin = LittleShort (pcx.xmin);
142 pcx.ymax = LittleShort (pcx.ymax);
143 pcx.ymin = LittleShort (pcx.ymin);
144 pcx.hres = LittleShort (pcx.hres);
145 pcx.vres = LittleShort (pcx.vres);
146 pcx.bytes_per_line = LittleShort (pcx.bytes_per_line);
147 pcx.palette_type = LittleShort (pcx.palette_type);
149 image_width = pcx.xmax + 1 - pcx.xmin;
150 image_height = pcx.ymax + 1 - pcx.ymin;
151 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)
153 Con_Printf ("Bad pcx file\n");
156 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
159 palette = f + fs_filesize - 768;
161 image_rgba = Mem_Alloc(tempmempool, image_width*image_height*4);
164 Con_Printf("LoadPCX: not enough memory for %i by %i image\n", image_width, image_height);
167 pbuf = image_rgba + image_width*image_height*3;
170 for (y = 0;y < image_height && fin < enddata;y++)
172 a = pbuf + y * image_width;
173 for (x = 0;x < image_width && fin < enddata;)
180 x2 = x + (dataByte & 0x3F);
182 if (x2 > image_width)
183 x2 = image_width; // technically an error
190 fin += pcx.bytes_per_line - image_width; // the number of bytes per line is always forced to an even number
191 while(x < image_width)
198 for(x = 0;x < image_width*image_height;x++)
211 =========================================================
215 =========================================================
218 typedef struct _TargaHeader
220 unsigned char id_length, colormap_type, image_type;
221 unsigned short colormap_index, colormap_length;
222 unsigned char colormap_size;
223 unsigned short x_origin, y_origin, width, height;
224 unsigned char pixel_size, attributes;
228 void PrintTargaHeader(TargaHeader *t)
230 Con_Printf("TargaHeader:\n");
231 Con_Printf("uint8 id_length = %i;\n", t->id_length);
232 Con_Printf("uint8 colormap_type = %i;\n", t->colormap_type);
233 Con_Printf("uint8 image_type = %i;\n", t->image_type);
234 Con_Printf("uint16 colormap_index = %i;\n", t->colormap_index);
235 Con_Printf("uint16 colormap_length = %i;\n", t->colormap_length);
236 Con_Printf("uint8 colormap_size = %i;\n", t->colormap_size);
237 Con_Printf("uint16 x_origin = %i;\n", t->x_origin);
238 Con_Printf("uint16 y_origin = %i;\n", t->y_origin);
239 Con_Printf("uint16 width = %i;\n", t->width);
240 Con_Printf("uint16 height = %i;\n", t->height);
241 Con_Printf("uint8 pixel_size = %i;\n", t->pixel_size);
242 Con_Printf("uint8 attributes = %i;\n", t->attributes);
250 qbyte *LoadTGA (qbyte *f, int matchwidth, int matchheight)
252 int x, y, row_inc, compressed, readpixelcount, red, green, blue, alpha, runlen, pindex;
253 qbyte *pixbuf, *image_rgba;
254 qbyte *fin, *enddata;
255 TargaHeader targa_header;
256 unsigned char palette[256*4], *p;
258 if (fs_filesize < 19)
261 enddata = f + fs_filesize;
263 targa_header.id_length = f[0];
264 targa_header.colormap_type = f[1];
265 targa_header.image_type = f[2];
267 targa_header.colormap_index = f[3] + f[4] * 256;
268 targa_header.colormap_length = f[5] + f[6] * 256;
269 targa_header.colormap_size = f[7];
270 targa_header.x_origin = f[8] + f[9] * 256;
271 targa_header.y_origin = f[10] + f[11] * 256;
272 targa_header.width = image_width = f[12] + f[13] * 256;
273 targa_header.height = image_height = f[14] + f[15] * 256;
274 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
276 Con_Printf("LoadTGA: invalid size\n");
277 PrintTargaHeader(&targa_header);
280 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
282 targa_header.pixel_size = f[16];
283 targa_header.attributes = f[17];
286 if (targa_header.id_length != 0)
287 fin += targa_header.id_length; // skip TARGA image comment
288 if (targa_header.image_type == 2 || targa_header.image_type == 10)
290 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
292 Con_Printf ("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
293 PrintTargaHeader(&targa_header);
297 else if (targa_header.image_type == 1 || targa_header.image_type == 9)
299 if (targa_header.pixel_size != 8)
301 Con_Printf ("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
302 PrintTargaHeader(&targa_header);
305 if (targa_header.colormap_length > 256)
307 Con_Printf ("LoadTGA: only up to 256 colormap_length supported\n");
308 PrintTargaHeader(&targa_header);
311 if (targa_header.colormap_index)
313 Con_Printf ("LoadTGA: colormap_index not supported\n");
314 PrintTargaHeader(&targa_header);
317 if (targa_header.colormap_size == 24)
319 for (x = 0;x < targa_header.colormap_length;x++)
321 palette[x*4+2] = *fin++;
322 palette[x*4+1] = *fin++;
323 palette[x*4+0] = *fin++;
324 palette[x*4+3] = 255;
327 else if (targa_header.colormap_size == 32)
329 for (x = 0;x < targa_header.colormap_length;x++)
331 palette[x*4+2] = *fin++;
332 palette[x*4+1] = *fin++;
333 palette[x*4+0] = *fin++;
334 palette[x*4+3] = *fin++;
339 Con_Printf ("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
340 PrintTargaHeader(&targa_header);
344 else if (targa_header.image_type == 3 || targa_header.image_type == 11)
346 if (targa_header.pixel_size != 8)
348 Con_Printf ("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
349 PrintTargaHeader(&targa_header);
355 Con_Printf ("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
356 PrintTargaHeader(&targa_header);
360 if (targa_header.attributes & 0x10)
362 Con_Printf ("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
366 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
369 Con_Printf ("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
373 // If bit 5 of attributes isn't set, the image has been stored from bottom to top
374 if ((targa_header.attributes & 0x20) == 0)
376 pixbuf = image_rgba + (image_height - 1)*image_width*4;
377 row_inc = -image_width*4*2;
385 compressed = targa_header.image_type == 9 || targa_header.image_type == 10 || targa_header.image_type == 11;
388 red = green = blue = alpha = 255;
389 while (y < image_height)
391 // decoder is mostly the same whether it's compressed or not
392 readpixelcount = 1000000;
394 if (compressed && fin < enddata)
397 // high bit indicates this is an RLE compressed run
400 runlen = 1 + (runlen & 0x7f);
403 while((runlen--) && y < image_height)
405 if (readpixelcount > 0)
408 red = green = blue = alpha = 255;
411 switch(targa_header.image_type)
417 if (pindex >= targa_header.colormap_length)
419 p = palette + pindex * 4;
433 if (targa_header.pixel_size == 32 && fin < enddata)
439 red = green = blue = *fin++;
449 if (x == image_width)
451 // end of line, advance to next
467 qbyte *LoadLMP (qbyte *f, int matchwidth, int matchheight)
473 Con_Printf("LoadLMP: invalid LMP file\n");
477 // parse the very complicated header *chuckle*
478 image_width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
479 image_height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
480 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
482 Con_Printf("LoadLMP: invalid size %ix%i\n", image_width, image_height);
485 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
488 if (fs_filesize < 8 + image_width * image_height)
490 Con_Printf("LoadLMP: invalid LMP file\n");
494 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
497 Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
500 Image_Copy8bitRGBA(f + 8, image_rgba, image_width * image_height, palette_complete);
507 unsigned width, height;
508 unsigned offsets[MIPLEVELS]; // four mip maps stored
509 char animname[32]; // next frame in animation chain
515 qbyte *LoadWAL (qbyte *f, int matchwidth, int matchheight)
518 const q2wal_t *inwal = (const void *)f;
520 if (fs_filesize < (int) sizeof(q2wal_t))
522 Con_Printf("LoadWAL: invalid WAL file\n");
526 image_width = LittleLong(inwal->width);
527 image_height = LittleLong(inwal->height);
528 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
530 Con_Printf("LoadWAL: invalid size %ix%i\n", image_width, image_height);
533 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
536 if ((int) fs_filesize < (int) sizeof(q2wal_t) + (int) LittleLong(inwal->offsets[0]) + image_width * image_height)
538 Con_Printf("LoadWAL: invalid WAL file\n");
542 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
545 Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
548 Image_Copy8bitRGBA(f + LittleLong(inwal->offsets[0]), image_rgba, image_width * image_height, palette_complete);
559 qbyte *LoadLMPAs8Bit (qbyte *f, int matchwidth, int matchheight)
565 Con_Printf("LoadLMPAs8Bit: invalid LMP file\n");
569 // parse the very complicated header *chuckle*
570 image_width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
571 image_height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
572 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
574 Con_Printf("LoadLMPAs8Bit: invalid size %ix%i\n", image_width, image_height);
577 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
580 if (fs_filesize < 8 + image_width * image_height)
582 Con_Printf("LoadLMPAs8Bit: invalid LMP file\n");
586 image_8bit = Mem_Alloc(tempmempool, image_width * image_height);
589 Con_Printf("LoadLMPAs8Bit: not enough memory for %i by %i image\n", image_width, image_height);
592 memcpy(image_8bit, f + 8, image_width * image_height);
596 void Image_StripImageExtension (const char *in, char *out)
598 const char *end, *temp;
599 end = in + strlen(in);
603 if (strcmp(temp, ".tga") == 0
604 || strcmp(temp, ".pcx") == 0
605 || strcmp(temp, ".lmp") == 0
606 || strcmp(temp, ".png") == 0
607 || strcmp(temp, ".jpg") == 0)
619 const char *formatstring;
620 qbyte *(*loadfunc)(qbyte *f, int matchwidth, int matchheight);
624 {"override/%s.tga", LoadTGA},
625 {"override/%s.jpg", JPEG_LoadImage},
626 {"textures/%s.tga", LoadTGA},
627 {"textures/%s.jpg", JPEG_LoadImage},
628 {"textures/%s.pcx", LoadPCX},
629 {"textures/%s.wal", LoadWAL},
631 {"%s.jpg", JPEG_LoadImage},
637 qbyte *loadimagepixels (const char *filename, qboolean complain, int matchwidth, int matchheight)
640 qbyte *f, *data = NULL;
641 char basename[MAX_QPATH], name[MAX_QPATH], *c;
642 if (developer_memorydebug.integer)
643 Mem_CheckSentinelsGlobal();
644 Image_StripImageExtension(filename, basename); // strip filename extensions to allow replacement by other types
645 // replace *'s with #, so commandline utils don't get confused when dealing with the external files
646 for (c = basename;*c;c++)
649 for (i = 0;imageformats[i].formatstring;i++)
651 sprintf (name, imageformats[i].formatstring, basename);
652 if ((f = FS_LoadFile(name, true)) && (data = imageformats[i].loadfunc(f, matchwidth, matchheight)))
655 Con_DPrintf("loaded image %s (%dx%d)\n", name, image_width, image_height);
656 if (developer_memorydebug.integer)
657 Mem_CheckSentinelsGlobal();
663 Con_Printf ("Couldn't load %s using ", filename);
664 for (i = 0;imageformats[i].formatstring;i++)
666 sprintf (name, imageformats[i].formatstring, basename);
667 Con_Printf (i == 0 ? "\"%s\"" : (imageformats[i+1].formatstring ? ", \"%s\"" : " or \"%s\".\n"), imageformats[i].formatstring);
670 if (developer_memorydebug.integer)
671 Mem_CheckSentinelsGlobal();
675 int image_makemask (const qbyte *in, qbyte *out, int size)
679 for (i = 0;i < size;i++)
681 out[0] = out[1] = out[2] = 255;
691 qbyte* loadimagepixelsmask (const char *filename, qboolean complain, int matchwidth, int matchheight)
694 in = data = loadimagepixels(filename, complain, matchwidth, matchheight);
697 if (image_makemask(data, data, image_width * image_height))
698 return data; // some transparency
702 return NULL; // all opaque
706 rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
710 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
712 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
717 rtexture_t *loadtextureimagemask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
721 if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
723 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
728 rtexture_t *image_masktex;
729 rtexture_t *image_nmaptex;
730 rtexture_t *loadtextureimagewithmask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
734 image_masktex = NULL;
735 image_nmaptex = NULL;
736 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
739 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
741 if (flags & TEXF_ALPHA && image_makemask(data, data, image_width * image_height))
742 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
748 rtexture_t *loadtextureimagewithmaskandnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
752 image_masktex = NULL;
753 image_nmaptex = NULL;
754 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
757 data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
759 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
761 Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
762 image_nmaptex = R_LoadTexture2D(pool, va("%s_nmap", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
764 if (flags & TEXF_ALPHA && image_makemask(data, data2, image_width * image_height))
765 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
773 rtexture_t *loadtextureimagebumpasnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
777 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
779 data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
781 Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
782 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
789 qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int height, const qbyte *data)
793 const qbyte *in, *end;
795 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
797 memset (buffer, 0, 18);
798 buffer[2] = 2; // uncompressed type
799 buffer[12] = (width >> 0) & 0xFF;
800 buffer[13] = (width >> 8) & 0xFF;
801 buffer[14] = (height >> 0) & 0xFF;
802 buffer[15] = (height >> 8) & 0xFF;
803 buffer[16] = 24; // pixel size
808 end = in + width*height*3;
809 for (;in < end;in += 3)
815 ret = FS_WriteFile (filename, buffer, width*height*3 + 18 );
821 void Image_WriteTGARGB (const char *filename, int width, int height, const qbyte *data)
825 const qbyte *in, *end;
827 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
829 memset (buffer, 0, 18);
830 buffer[2] = 2; // uncompressed type
831 buffer[12] = (width >> 0) & 0xFF;
832 buffer[13] = (width >> 8) & 0xFF;
833 buffer[14] = (height >> 0) & 0xFF;
834 buffer[15] = (height >> 8) & 0xFF;
835 buffer[16] = 24; // pixel size
837 // swap rgb to bgr and flip upside down
839 for (y = height - 1;y >= 0;y--)
841 in = data + y * width * 3;
842 end = in + width * 3;
843 for (;in < end;in += 3)
850 FS_WriteFile (filename, buffer, width*height*3 + 18 );
855 void Image_WriteTGARGBA (const char *filename, int width, int height, const qbyte *data)
859 const qbyte *in, *end;
861 buffer = Mem_Alloc(tempmempool, width*height*4 + 18);
863 memset (buffer, 0, 18);
864 buffer[2] = 2; // uncompressed type
865 buffer[12] = (width >> 0) & 0xFF;
866 buffer[13] = (width >> 8) & 0xFF;
867 buffer[14] = (height >> 0) & 0xFF;
868 buffer[15] = (height >> 8) & 0xFF;
869 buffer[16] = 32; // pixel size
871 // swap rgba to bgra and flip upside down
873 for (y = height - 1;y >= 0;y--)
875 in = data + y * width * 4;
876 end = in + width * 4;
877 for (;in < end;in += 4)
885 FS_WriteFile (filename, buffer, width*height*4 + 18 );
890 qboolean Image_CheckAlpha(const qbyte *data, int size, qboolean rgba)
896 for (end = data + size * 4, data += 3;data < end;data += 4)
902 // color 255 is transparent
903 for (end = data + size;data < end;data++)
910 static void Image_Resample32LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
912 int j, xi, oldx = 0, f, fstep, endx, lerp;
913 fstep = (int) (inwidth*65536.0f/outwidth);
915 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
920 in += (xi - oldx) * 4;
926 *out++ = (qbyte) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
927 *out++ = (qbyte) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
928 *out++ = (qbyte) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
929 *out++ = (qbyte) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
931 else // last pixel of the line has no pixel to lerp to
941 static void Image_Resample24LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
943 int j, xi, oldx = 0, f, fstep, endx, lerp;
944 fstep = (int) (inwidth*65536.0f/outwidth);
946 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
951 in += (xi - oldx) * 3;
957 *out++ = (qbyte) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
958 *out++ = (qbyte) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
959 *out++ = (qbyte) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
961 else // last pixel of the line has no pixel to lerp to
970 int resamplerowsize = 0;
971 qbyte *resamplerow1 = NULL;
972 qbyte *resamplerow2 = NULL;
973 mempool_t *resamplemempool = NULL;
975 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (qbyte) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
976 void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
978 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
982 fstep = (int) (inheight*65536.0f/outheight);
986 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
987 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
988 for (i = 0, f = 0;i < outheight;i++,f += fstep)
996 inrow = (qbyte *)indata + inwidth4*yi;
998 memcpy(resamplerow1, resamplerow2, outwidth4);
1000 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1001 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1052 resamplerow1 -= outwidth4;
1053 resamplerow2 -= outwidth4;
1059 inrow = (qbyte *)indata + inwidth4*yi;
1061 memcpy(resamplerow1, resamplerow2, outwidth4);
1063 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1066 memcpy(out, resamplerow1, outwidth4);
1071 void Image_Resample32Nearest(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1074 unsigned frac, fracstep;
1075 // relies on int being 4 bytes
1079 fracstep = inwidth*0x10000/outwidth;
1080 for (i = 0;i < outheight;i++)
1082 inrow = (int *)indata + inwidth*(i*inheight/outheight);
1083 frac = fracstep >> 1;
1087 out[0] = inrow[frac >> 16];frac += fracstep;
1088 out[1] = inrow[frac >> 16];frac += fracstep;
1089 out[2] = inrow[frac >> 16];frac += fracstep;
1090 out[3] = inrow[frac >> 16];frac += fracstep;
1096 out[0] = inrow[frac >> 16];frac += fracstep;
1097 out[1] = inrow[frac >> 16];frac += fracstep;
1102 out[0] = inrow[frac >> 16];frac += fracstep;
1108 void Image_Resample24Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1110 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
1114 fstep = (int) (inheight*65536.0f/outheight);
1118 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1119 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1120 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1128 inrow = (qbyte *)indata + inwidth3*yi;
1130 memcpy(resamplerow1, resamplerow2, outwidth3);
1132 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1133 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1177 resamplerow1 -= outwidth3;
1178 resamplerow2 -= outwidth3;
1184 inrow = (qbyte *)indata + inwidth3*yi;
1186 memcpy(resamplerow1, resamplerow2, outwidth3);
1188 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1191 memcpy(out, resamplerow1, outwidth3);
1196 void Image_Resample24Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1198 int i, j, f, inwidth3 = inwidth * 3;
1199 unsigned frac, fracstep;
1203 fracstep = inwidth*0x10000/outwidth;
1204 for (i = 0;i < outheight;i++)
1206 inrow = (qbyte *)indata + inwidth3*(i*inheight/outheight);
1207 frac = fracstep >> 1;
1211 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1212 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1213 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1214 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;
1236 void Image_Resample (const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int bytesperpixel, int quality)
1238 if (indepth != 1 || outdepth != 1)
1239 Sys_Error("Image_Resample: 3D resampling not supported\n");
1240 if (resamplerowsize < outwidth*4)
1243 Mem_Free(resamplerow1);
1244 resamplerowsize = outwidth*4;
1245 if (!resamplemempool)
1246 resamplemempool = Mem_AllocPool("Image Scaling Buffer");
1247 resamplerow1 = Mem_Alloc(resamplemempool, resamplerowsize*2);
1248 resamplerow2 = resamplerow1 + resamplerowsize;
1250 if (bytesperpixel == 4)
1253 Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1255 Image_Resample32Nearest(indata, inwidth, inheight, outdata, outwidth, outheight);
1257 else if (bytesperpixel == 3)
1260 Image_Resample24Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1262 Image_Resample24Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1265 Sys_Error("Image_Resample: unsupported bytesperpixel %i\n", bytesperpixel);
1268 // in can be the same as out
1269 void Image_MipReduce(const qbyte *in, qbyte *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth, int bytesperpixel)
1272 if (*depth != 1 || destdepth != 1)
1273 Sys_Error("Image_Resample: 3D resampling not supported\n");
1274 nextrow = *width * bytesperpixel;
1275 if (*width > destwidth)
1278 if (*height > destheight)
1282 if (bytesperpixel == 4)
1284 for (y = 0;y < *height;y++)
1286 for (x = 0;x < *width;x++)
1288 out[0] = (qbyte) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
1289 out[1] = (qbyte) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1290 out[2] = (qbyte) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1291 out[3] = (qbyte) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1295 in += nextrow; // skip a line
1298 else if (bytesperpixel == 3)
1300 for (y = 0;y < *height;y++)
1302 for (x = 0;x < *width;x++)
1304 out[0] = (qbyte) ((in[0] + in[3] + in[nextrow ] + in[nextrow+3]) >> 2);
1305 out[1] = (qbyte) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
1306 out[2] = (qbyte) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
1310 in += nextrow; // skip a line
1314 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1319 if (bytesperpixel == 4)
1321 for (y = 0;y < *height;y++)
1323 for (x = 0;x < *width;x++)
1325 out[0] = (qbyte) ((in[0] + in[4]) >> 1);
1326 out[1] = (qbyte) ((in[1] + in[5]) >> 1);
1327 out[2] = (qbyte) ((in[2] + in[6]) >> 1);
1328 out[3] = (qbyte) ((in[3] + in[7]) >> 1);
1334 else if (bytesperpixel == 3)
1336 for (y = 0;y < *height;y++)
1338 for (x = 0;x < *width;x++)
1340 out[0] = (qbyte) ((in[0] + in[3]) >> 1);
1341 out[1] = (qbyte) ((in[1] + in[4]) >> 1);
1342 out[2] = (qbyte) ((in[2] + in[5]) >> 1);
1349 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1354 if (*height > destheight)
1358 if (bytesperpixel == 4)
1360 for (y = 0;y < *height;y++)
1362 for (x = 0;x < *width;x++)
1364 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1365 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1366 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1367 out[3] = (qbyte) ((in[3] + in[nextrow+3]) >> 1);
1371 in += nextrow; // skip a line
1374 else if (bytesperpixel == 3)
1376 for (y = 0;y < *height;y++)
1378 for (x = 0;x < *width;x++)
1380 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1381 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1382 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1386 in += nextrow; // skip a line
1390 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1393 Sys_Error("Image_MipReduce: desired size already achieved\n");
1397 void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
1400 const unsigned char *p0, *p1, *p2;
1402 float iwidth, iheight, ibumpscale, n[3];
1403 iwidth = 1.0f / width;
1404 iheight = 1.0f / height;
1405 ibumpscale = (255.0f * 3.0f) / bumpscale;
1407 for (y = 0;y < height;y++)
1409 for (x = 0;x < width;x++)
1411 p0 = inpixels + (y * width + x) * 4;
1415 p1 = inpixels + (y * width + x) * 4;
1417 p1 = inpixels + (y * width) * 4;
1420 p1 = inpixels + (y * width + x + 1) * 4;
1421 if (y == height - 1)
1424 p2 = inpixels + (y * width + x) * 4;
1426 p2 = inpixels + x * 4;
1429 p2 = inpixels + ((y + 1) * width + x) * 4;
1433 dv[0][2] = ((p0[0] + p0[1] + p0[2]) * ibumpscale) - ((p1[0] + p1[1] + p1[2]) * ibumpscale);
1436 dv[1][2] = ((p2[0] + p2[1] + p2[2]) * ibumpscale) - ((p0[0] + p0[1] + p0[2]) * ibumpscale);
1437 n[0] = dv[0][1]*dv[1][2]-dv[0][2]*dv[1][1];
1438 n[1] = dv[0][2]*dv[1][0]-dv[0][0]*dv[1][2];
1439 n[2] = dv[0][0]*dv[1][1]-dv[0][1]*dv[1][0];
1441 n[0] = ((p1[0] + p1[1] + p1[2]) - (p0[0] + p0[1] + p0[2]));
1442 n[1] = ((p0[0] + p0[1] + p0[2]) - (p2[0] + p2[1] + p2[2]));
1446 // this should work for the bottom right triangle if anyone wants
1447 // code for that for some reason
1448 n[0] = ((p3[0] + p3[1] + p3[2]) - (p1[0] + p1[1] + p1[2]));
1449 n[1] = ((p2[0] + p2[1] + p2[2]) - (p3[0] + p3[1] + p3[2]));
1453 out[0] = 128.0f + n[0] * 127.0f;
1454 out[1] = 128.0f + n[1] * 127.0f;
1455 out[2] = 128.0f + n[2] * 127.0f;
1462 int image_loadskin(imageskin_t *s, char *shadername)
1466 int bumppixels_width, bumppixels_height;
1467 char name[MAX_QPATH];
1468 Image_StripImageExtension(shadername, name);
1469 memset(s, 0, sizeof(*s));
1470 s->basepixels = loadimagepixels(name, false, 0, 0);
1471 if (s->basepixels == NULL)
1473 s->basepixels_width = image_width;
1474 s->basepixels_height = image_height;
1476 bumppixels = NULL;bumppixels_width = 0;bumppixels_height = 0;
1477 if (Image_CheckAlpha(s->basepixels, s->basepixels_width * s->basepixels_height, true))
1479 s->maskpixels = Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1480 s->maskpixels_width = s->basepixels_width;
1481 s->maskpixels_height = s->basepixels_height;
1482 memcpy(s->maskpixels, s->basepixels, s->maskpixels_width * s->maskpixels_height * 4);
1483 for (j = 0;j < s->basepixels_width * s->basepixels_height * 4;j += 4)
1485 s->maskpixels[j+0] = 255;
1486 s->maskpixels[j+1] = 255;
1487 s->maskpixels[j+2] = 255;
1491 // _luma is supported for tenebrae compatibility
1492 // (I think it's a very stupid name, but oh well)
1493 if ((s->glowpixels = loadimagepixels(va("%s_glow", name), false, 0, 0)) != NULL
1494 || (s->glowpixels = loadimagepixels(va("%s_luma", name), false, 0, 0)) != NULL)
1496 s->glowpixels_width = image_width;
1497 s->glowpixels_height = image_height;
1499 // _norm is the name used by tenebrae
1500 // (I don't like the name much)
1501 if ((s->nmappixels = loadimagepixels(va("%s_norm", name), false, 0, 0)) != NULL)
1503 s->nmappixels_width = image_width;
1504 s->nmappixels_height = image_height;
1506 else if ((bumppixels = loadimagepixels(va("%s_bump", name), false, 0, 0)) != NULL)
1508 bumppixels_width = image_width;
1509 bumppixels_height = image_height;
1511 if ((s->glosspixels = loadimagepixels(va("%s_gloss", name), false, 0, 0)) != NULL)
1513 s->glosspixels_width = image_width;
1514 s->glosspixels_height = image_height;
1516 if ((s->pantspixels = loadimagepixels(va("%s_pants", name), false, 0, 0)) != NULL)
1518 s->pantspixels_width = image_width;
1519 s->pantspixels_height = image_height;
1521 if ((s->shirtpixels = loadimagepixels(va("%s_shirt", name), false, 0, 0)) != NULL)
1523 s->shirtpixels_width = image_width;
1524 s->shirtpixels_height = image_height;
1527 if (s->nmappixels == NULL)
1529 if (bumppixels != NULL)
1531 if (r_shadow_bumpscale_bumpmap.value > 0)
1533 s->nmappixels = Mem_Alloc(loadmodel->mempool, bumppixels_width * bumppixels_height * 4);
1534 s->nmappixels_width = bumppixels_width;
1535 s->nmappixels_height = bumppixels_height;
1536 Image_HeightmapToNormalmap(bumppixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_bumpmap.value);
1541 if (r_shadow_bumpscale_basetexture.value > 0)
1543 s->nmappixels = Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1544 s->nmappixels_width = s->basepixels_width;
1545 s->nmappixels_height = s->basepixels_height;
1546 Image_HeightmapToNormalmap(s->basepixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_basetexture.value);
1550 if (bumppixels != NULL)
1551 Mem_Free(bumppixels);
1555 void image_freeskin(imageskin_t *s)
1558 Mem_Free(s->basepixels);
1560 Mem_Free(s->maskpixels);
1562 Mem_Free(s->nmappixels);
1564 Mem_Free(s->glowpixels);
1566 Mem_Free(s->glosspixels);
1568 Mem_Free(s->pantspixels);
1570 Mem_Free(s->shirtpixels);
1571 memset(s, 0, sizeof(*s));