9 void Image_GammaRemapRGB(const qbyte *in, qbyte *out, int pixels, const qbyte *gammar, const qbyte *gammag, const qbyte *gammab)
13 out[0] = gammar[in[0]];
14 out[1] = gammag[in[1]];
15 out[2] = gammab[in[2]];
21 // note: pal must be 32bit color
22 void Image_Copy8bitRGBA(const qbyte *in, qbyte *out, int pixels, const unsigned int *pal)
24 int *iout = (void *)out;
60 =================================================================
64 =================================================================
73 unsigned short xmin,ymin,xmax,ymax;
74 unsigned short hres,vres;
75 unsigned char palette[48];
78 unsigned short bytes_per_line;
79 unsigned short palette_type;
88 qbyte* LoadPCX (const qbyte *f, int matchwidth, int matchheight)
91 qbyte *a, *b, *image_rgba, *pbuf;
92 const qbyte *palette, *fin, *enddata;
93 int x, y, x2, dataByte;
95 if (loadsize < (int)sizeof(pcx) + 768)
97 Con_Printf ("Bad pcx file\n");
103 memcpy(&pcx, fin, sizeof(pcx));
106 // LordHavoc: big-endian support ported from QF newtree
107 pcx.xmax = LittleShort (pcx.xmax);
108 pcx.xmin = LittleShort (pcx.xmin);
109 pcx.ymax = LittleShort (pcx.ymax);
110 pcx.ymin = LittleShort (pcx.ymin);
111 pcx.hres = LittleShort (pcx.hres);
112 pcx.vres = LittleShort (pcx.vres);
113 pcx.bytes_per_line = LittleShort (pcx.bytes_per_line);
114 pcx.palette_type = LittleShort (pcx.palette_type);
116 if (pcx.manufacturer != 0x0a || pcx.version != 5 || pcx.encoding != 1 || pcx.bits_per_pixel != 8 || pcx.xmax > 320 || pcx.ymax > 256)
118 Con_Printf ("Bad pcx file\n");
122 if (matchwidth && (pcx.xmax+1) != matchwidth)
126 if (matchheight && (pcx.ymax+1) != matchheight)
131 image_width = pcx.xmax+1;
132 image_height = pcx.ymax+1;
134 palette = f + loadsize - 768;
136 image_rgba = Mem_Alloc(tempmempool, image_width*image_height*4);
139 Con_Printf("LoadPCX: not enough memory for %i by %i image\n", image_width, image_height);
142 pbuf = image_rgba + image_width*image_height*3;
145 for (y = 0;y < image_height && fin < enddata;y++)
147 a = pbuf + y * image_width;
148 for (x = 0;x < image_width && fin < enddata;)
155 x2 = x + (dataByte & 0x3F);
157 if (x2 > image_width)
158 x2 = image_width; // technically an error
165 fin += pcx.bytes_per_line - image_width; // the number of bytes per line is always forced to an even number
166 while(x < image_width)
173 for(x = 0;x < image_width*image_height;x++)
186 =========================================================
190 =========================================================
193 typedef struct _TargaHeader
195 unsigned char id_length, colormap_type, image_type;
196 unsigned short colormap_index, colormap_length;
197 unsigned char colormap_size;
198 unsigned short x_origin, y_origin, width, height;
199 unsigned char pixel_size, attributes;
203 void PrintTargaHeader(TargaHeader *t)
205 Con_Printf("TargaHeader:\n");
206 Con_Printf("uint8 id_length = %i;\n", t->id_length);
207 Con_Printf("uint8 colormap_type = %i;\n", t->colormap_type);
208 Con_Printf("uint8 image_type = %i;\n", t->image_type);
209 Con_Printf("uint16 colormap_index = %i;\n", t->colormap_index);
210 Con_Printf("uint16 colormap_length = %i;\n", t->colormap_length);
211 Con_Printf("uint8 colormap_size = %i;\n", t->colormap_size);
212 Con_Printf("uint16 x_origin = %i;\n", t->x_origin);
213 Con_Printf("uint16 y_origin = %i;\n", t->y_origin);
214 Con_Printf("uint16 width = %i;\n", t->width);
215 Con_Printf("uint16 height = %i;\n", t->height);
216 Con_Printf("uint8 pixel_size = %i;\n", t->pixel_size);
217 Con_Printf("uint8 attributes = %i;\n", t->attributes);
225 qbyte *LoadTGA (const qbyte *f, int matchwidth, int matchheight)
227 int x, y, row_inc, compressed, readpixelcount, red, green, blue, alpha, runlen;
228 qbyte *pixbuf, *image_rgba;
229 const qbyte *fin, *enddata;
230 TargaHeader targa_header;
231 unsigned char palette[256*4], *p;
236 enddata = f + loadsize;
238 targa_header.id_length = f[0];
239 targa_header.colormap_type = f[1];
240 targa_header.image_type = f[2];
242 targa_header.colormap_index = f[3] + f[4] * 256;
243 targa_header.colormap_length = f[5] + f[6] * 256;
244 targa_header.colormap_size = f[7];
245 targa_header.x_origin = f[8] + f[9] * 256;
246 targa_header.y_origin = f[10] + f[11] * 256;
247 targa_header.width = f[12] + f[13] * 256;
248 targa_header.height = f[14] + f[15] * 256;
249 if (matchwidth && targa_header.width != matchwidth)
251 if (matchheight && targa_header.height != matchheight)
253 targa_header.pixel_size = f[16];
254 targa_header.attributes = f[17];
256 image_width = targa_header.width;
257 image_height = targa_header.height;
260 if (targa_header.id_length != 0)
261 fin += targa_header.id_length; // skip TARGA image comment
262 if (targa_header.image_type == 2 || targa_header.image_type == 10)
264 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
266 Con_Printf ("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
267 PrintTargaHeader(&targa_header);
271 else if (targa_header.image_type == 1 || targa_header.image_type == 9)
273 if (targa_header.pixel_size != 8)
275 Con_Printf ("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
276 PrintTargaHeader(&targa_header);
279 if (targa_header.colormap_length != 256)
281 Con_Printf ("LoadTGA: only 256 colormap_length supported\n");
282 PrintTargaHeader(&targa_header);
285 if (targa_header.colormap_index)
287 Con_Printf ("LoadTGA: colormap_index not supported\n");
288 PrintTargaHeader(&targa_header);
291 if (targa_header.colormap_size == 24)
293 for (x = 0;x < targa_header.colormap_length;x++)
295 palette[x*4+2] = *fin++;
296 palette[x*4+1] = *fin++;
297 palette[x*4+0] = *fin++;
298 palette[x*4+3] = 255;
301 else if (targa_header.colormap_size == 32)
303 for (x = 0;x < targa_header.colormap_length;x++)
305 palette[x*4+2] = *fin++;
306 palette[x*4+1] = *fin++;
307 palette[x*4+0] = *fin++;
308 palette[x*4+3] = *fin++;
313 Con_Printf ("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
314 PrintTargaHeader(&targa_header);
318 else if (targa_header.image_type == 3 || targa_header.image_type == 11)
320 if (targa_header.pixel_size != 8)
322 Con_Printf ("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
323 PrintTargaHeader(&targa_header);
329 Con_Printf ("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
330 PrintTargaHeader(&targa_header);
334 if (targa_header.attributes & 0x10)
336 Con_Printf ("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
340 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
343 Con_Printf ("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
347 // If bit 5 of attributes isn't set, the image has been stored from bottom to top
348 if ((targa_header.attributes & 0x20) == 0)
350 pixbuf = image_rgba + (image_height - 1)*image_width*4;
351 row_inc = -image_width*4*2;
359 compressed = targa_header.image_type == 9 || targa_header.image_type == 10 || targa_header.image_type == 11;
362 red = green = blue = alpha = 255;
363 while (y < image_height)
365 // decoder is mostly the same whether it's compressed or not
366 readpixelcount = 1000000;
368 if (compressed && fin < enddata)
371 // high bit indicates this is an RLE compressed run
374 runlen = 1 + (runlen & 0x7f);
377 while((runlen--) && y < image_height)
379 if (readpixelcount > 0)
382 red = green = blue = alpha = 255;
385 switch(targa_header.image_type)
390 p = palette + (*fin++) * 4;
404 if (targa_header.pixel_size == 32 && fin < enddata)
410 red = green = blue = *fin++;
420 if (x == image_width)
422 // end of line, advance to next
438 qbyte *LoadLMP (const qbyte *f, int matchwidth, int matchheight)
445 Con_Printf("LoadLMP: invalid LMP file\n");
449 // parse the very complicated header *chuckle*
450 width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
451 height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
452 if ((unsigned) width > 4096 || (unsigned) height > 4096)
454 Con_Printf("LoadLMP: invalid size\n");
457 if ((matchwidth && width != matchwidth) || (matchheight && height != matchheight))
460 if (loadsize < 8 + width * height)
462 Con_Printf("LoadLMP: invalid LMP file\n");
467 image_height = height;
469 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
472 Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
475 Image_Copy8bitRGBA(f + 8, image_rgba, image_width * image_height, palette_complete);
484 qbyte *LoadLMPAs8Bit (const qbyte *f, int matchwidth, int matchheight)
491 Con_Printf("LoadLMPAs8Bit: invalid LMP file\n");
495 // parse the very complicated header *chuckle*
496 width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
497 height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
498 if ((unsigned) width > 4096 || (unsigned) height > 4096)
500 Con_Printf("LoadLMPAs8Bit: invalid size\n");
503 if ((matchwidth && width != matchwidth) || (matchheight && height != matchheight))
506 if (loadsize < 8 + width * height)
508 Con_Printf("LoadLMPAs8Bit: invalid LMP file\n");
513 image_height = height;
515 image_8bit = Mem_Alloc(tempmempool, image_width * image_height);
518 Con_Printf("LoadLMPAs8Bit: not enough memory for %i by %i image\n", image_width, image_height);
521 memcpy(image_8bit, f + 8, image_width * image_height);
525 void Image_StripImageExtension (const char *in, char *out)
527 const char *end, *temp;
528 end = in + strlen(in);
532 if (strcmp(temp, ".tga") == 0 || strcmp(temp, ".pcx") == 0 || strcmp(temp, ".lmp") == 0)
542 qbyte *loadimagepixels (const char *filename, qboolean complain, int matchwidth, int matchheight)
545 char basename[MAX_QPATH], name[MAX_QPATH], *c;
546 Image_StripImageExtension(filename, basename); // strip filename extensions to allow replacement by other types
547 // replace *'s with #, so commandline utils don't get confused when dealing with the external files
548 for (c = basename;*c;c++)
551 sprintf (name, "override/%s.tga", basename);
552 f = COM_LoadFile(name, true);
555 data = LoadTGA (f, matchwidth, matchheight);
559 sprintf (name, "override/%s.jpg", basename);
560 f = COM_LoadFile(name, true);
563 data = JPEG_LoadImage (f, matchwidth, matchheight);
567 sprintf (name, "textures/%s.tga", basename);
568 f = COM_LoadFile(name, true);
571 data = LoadTGA (f, matchwidth, matchheight);
575 sprintf (name, "textures/%s.jpg", basename);
576 f = COM_LoadFile(name, true);
579 data = JPEG_LoadImage (f, matchwidth, matchheight);
583 sprintf (name, "textures/%s.pcx", basename);
584 f = COM_LoadFile(name, true);
587 data = LoadPCX (f, matchwidth, matchheight);
591 sprintf (name, "%s.tga", basename);
592 f = COM_LoadFile(name, true);
595 data = LoadTGA (f, matchwidth, matchheight);
599 sprintf (name, "%s.jpg", basename);
600 f = COM_LoadFile(name, true);
603 data = JPEG_LoadImage (f, matchwidth, matchheight);
607 sprintf (name, "%s.pcx", basename);
608 f = COM_LoadFile(name, true);
611 data = LoadPCX (f, matchwidth, matchheight);
615 sprintf (name, "%s.lmp", basename);
616 f = COM_LoadFile(name, true);
619 data = LoadLMP (f, matchwidth, matchheight);
624 Con_Printf ("Couldn't load %s.tga, .jpg, .pcx, .lmp\n", filename);
628 int image_makemask (const qbyte *in, qbyte *out, int size)
632 for (i = 0;i < size;i++)
634 out[0] = out[1] = out[2] = 255;
644 qbyte* loadimagepixelsmask (const char *filename, qboolean complain, int matchwidth, int matchheight)
647 in = data = loadimagepixels(filename, complain, matchwidth, matchheight);
650 if (image_makemask(data, data, image_width * image_height))
651 return data; // some transparency
655 return NULL; // all opaque
659 rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
663 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
665 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
670 rtexture_t *loadtextureimagemask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
674 if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
676 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
681 rtexture_t *image_masktex;
682 rtexture_t *image_nmaptex;
683 rtexture_t *loadtextureimagewithmask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
687 image_masktex = NULL;
688 image_nmaptex = NULL;
689 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
692 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
694 if (flags & TEXF_ALPHA && image_makemask(data, data, image_width * image_height))
695 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
701 rtexture_t *loadtextureimagewithmaskandnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
705 image_masktex = NULL;
706 image_nmaptex = NULL;
707 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
710 data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
712 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
714 Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
715 image_nmaptex = R_LoadTexture2D(pool, va("%s_nmap", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
717 if (flags & TEXF_ALPHA && image_makemask(data, data2, image_width * image_height))
718 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
726 qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int height, const qbyte *data)
730 const qbyte *in, *end;
732 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
734 memset (buffer, 0, 18);
735 buffer[2] = 2; // uncompressed type
736 buffer[12] = (width >> 0) & 0xFF;
737 buffer[13] = (width >> 8) & 0xFF;
738 buffer[14] = (height >> 0) & 0xFF;
739 buffer[15] = (height >> 8) & 0xFF;
740 buffer[16] = 24; // pixel size
745 end = in + width*height*3;
746 for (;in < end;in += 3)
752 ret = COM_WriteFile (filename, buffer, width*height*3 + 18 );
758 void Image_WriteTGARGB (const char *filename, int width, int height, const qbyte *data)
762 const qbyte *in, *end;
764 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
766 memset (buffer, 0, 18);
767 buffer[2] = 2; // uncompressed type
768 buffer[12] = (width >> 0) & 0xFF;
769 buffer[13] = (width >> 8) & 0xFF;
770 buffer[14] = (height >> 0) & 0xFF;
771 buffer[15] = (height >> 8) & 0xFF;
772 buffer[16] = 24; // pixel size
774 // swap rgb to bgr and flip upside down
776 for (y = height - 1;y >= 0;y--)
778 in = data + y * width * 3;
779 end = in + width * 3;
780 for (;in < end;in += 3)
787 COM_WriteFile (filename, buffer, width*height*3 + 18 );
792 void Image_WriteTGARGBA (const char *filename, int width, int height, const qbyte *data)
796 const qbyte *in, *end;
798 buffer = Mem_Alloc(tempmempool, width*height*4 + 18);
800 memset (buffer, 0, 18);
801 buffer[2] = 2; // uncompressed type
802 buffer[12] = (width >> 0) & 0xFF;
803 buffer[13] = (width >> 8) & 0xFF;
804 buffer[14] = (height >> 0) & 0xFF;
805 buffer[15] = (height >> 8) & 0xFF;
806 buffer[16] = 32; // pixel size
808 // swap rgba to bgra and flip upside down
810 for (y = height - 1;y >= 0;y--)
812 in = data + y * width * 4;
813 end = in + width * 4;
814 for (;in < end;in += 4)
822 COM_WriteFile (filename, buffer, width*height*4 + 18 );
827 qboolean Image_CheckAlpha(const qbyte *data, int size, qboolean rgba)
833 for (end = data + size * 4, data += 3;data < end;data += 4)
839 // color 255 is transparent
840 for (end = data + size;data < end;data++)
847 static void Image_Resample32LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
849 int j, xi, oldx = 0, f, fstep, endx, lerp;
850 fstep = (int) (inwidth*65536.0f/outwidth);
852 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
857 in += (xi - oldx) * 4;
863 *out++ = (qbyte) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
864 *out++ = (qbyte) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
865 *out++ = (qbyte) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
866 *out++ = (qbyte) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
868 else // last pixel of the line has no pixel to lerp to
878 static void Image_Resample24LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
880 int j, xi, oldx = 0, f, fstep, endx, lerp;
881 fstep = (int) (inwidth*65536.0f/outwidth);
883 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
888 in += (xi - oldx) * 3;
894 *out++ = (qbyte) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
895 *out++ = (qbyte) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
896 *out++ = (qbyte) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
898 else // last pixel of the line has no pixel to lerp to
907 int resamplerowsize = 0;
908 qbyte *resamplerow1 = NULL;
909 qbyte *resamplerow2 = NULL;
910 mempool_t *resamplemempool = NULL;
912 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (qbyte) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
913 void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
915 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
919 fstep = (int) (inheight*65536.0f/outheight);
923 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
924 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
925 for (i = 0, f = 0;i < outheight;i++,f += fstep)
933 inrow = (qbyte *)indata + inwidth4*yi;
935 memcpy(resamplerow1, resamplerow2, outwidth4);
937 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
938 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
989 resamplerow1 -= outwidth4;
990 resamplerow2 -= outwidth4;
996 inrow = (qbyte *)indata + inwidth4*yi;
998 memcpy(resamplerow1, resamplerow2, outwidth4);
1000 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1003 memcpy(out, resamplerow1, outwidth4);
1008 void Image_Resample32Nearest(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1011 unsigned frac, fracstep;
1012 // relies on int being 4 bytes
1016 fracstep = inwidth*0x10000/outwidth;
1017 for (i = 0;i < outheight;i++)
1019 inrow = (int *)indata + inwidth*(i*inheight/outheight);
1020 frac = fracstep >> 1;
1024 out[0] = inrow[frac >> 16];frac += fracstep;
1025 out[1] = inrow[frac >> 16];frac += fracstep;
1026 out[2] = inrow[frac >> 16];frac += fracstep;
1027 out[3] = inrow[frac >> 16];frac += fracstep;
1033 out[0] = inrow[frac >> 16];frac += fracstep;
1034 out[1] = inrow[frac >> 16];frac += fracstep;
1039 out[0] = inrow[frac >> 16];frac += fracstep;
1045 void Image_Resample24Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1047 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
1051 fstep = (int) (inheight*65536.0f/outheight);
1055 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1056 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1057 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1065 inrow = (qbyte *)indata + inwidth3*yi;
1067 memcpy(resamplerow1, resamplerow2, outwidth3);
1069 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1070 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1114 resamplerow1 -= outwidth3;
1115 resamplerow2 -= outwidth3;
1121 inrow = (qbyte *)indata + inwidth3*yi;
1123 memcpy(resamplerow1, resamplerow2, outwidth3);
1125 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1128 memcpy(out, resamplerow1, outwidth3);
1133 void Image_Resample24Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1135 int i, j, f, inwidth3 = inwidth * 3;
1136 unsigned frac, fracstep;
1140 fracstep = inwidth*0x10000/outwidth;
1141 for (i = 0;i < outheight;i++)
1143 inrow = (qbyte *)indata + inwidth3*(i*inheight/outheight);
1144 frac = fracstep >> 1;
1148 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1149 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1150 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1151 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1156 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1157 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1162 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1173 void Image_Resample (const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int bytesperpixel, int quality)
1175 if (indepth != 1 || outdepth != 1)
1176 Sys_Error("Image_Resample: 3D resampling not supported\n");
1177 if (resamplerowsize < outwidth*4)
1180 Mem_Free(resamplerow1);
1181 resamplerowsize = outwidth*4;
1182 if (!resamplemempool)
1183 resamplemempool = Mem_AllocPool("Image Scaling Buffer");
1184 resamplerow1 = Mem_Alloc(resamplemempool, resamplerowsize*2);
1185 resamplerow2 = resamplerow1 + resamplerowsize;
1187 if (bytesperpixel == 4)
1190 Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1192 Image_Resample32Nearest(indata, inwidth, inheight, outdata, outwidth, outheight);
1194 else if (bytesperpixel == 3)
1197 Image_Resample24Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1199 Image_Resample24Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1202 Sys_Error("Image_Resample: unsupported bytesperpixel %i\n", bytesperpixel);
1205 // in can be the same as out
1206 void Image_MipReduce(const qbyte *in, qbyte *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth, int bytesperpixel)
1209 if (*depth != 1 || destdepth != 1)
1210 Sys_Error("Image_Resample: 3D resampling not supported\n");
1211 nextrow = *width * bytesperpixel;
1212 if (*width > destwidth)
1215 if (*height > destheight)
1219 if (bytesperpixel == 4)
1221 for (y = 0;y < *height;y++)
1223 for (x = 0;x < *width;x++)
1225 out[0] = (qbyte) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
1226 out[1] = (qbyte) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1227 out[2] = (qbyte) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1228 out[3] = (qbyte) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1232 in += nextrow; // skip a line
1235 else if (bytesperpixel == 3)
1237 for (y = 0;y < *height;y++)
1239 for (x = 0;x < *width;x++)
1241 out[0] = (qbyte) ((in[0] + in[3] + in[nextrow ] + in[nextrow+3]) >> 2);
1242 out[1] = (qbyte) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
1243 out[2] = (qbyte) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
1247 in += nextrow; // skip a line
1251 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1256 if (bytesperpixel == 4)
1258 for (y = 0;y < *height;y++)
1260 for (x = 0;x < *width;x++)
1262 out[0] = (qbyte) ((in[0] + in[4]) >> 1);
1263 out[1] = (qbyte) ((in[1] + in[5]) >> 1);
1264 out[2] = (qbyte) ((in[2] + in[6]) >> 1);
1265 out[3] = (qbyte) ((in[3] + in[7]) >> 1);
1271 else if (bytesperpixel == 3)
1273 for (y = 0;y < *height;y++)
1275 for (x = 0;x < *width;x++)
1277 out[0] = (qbyte) ((in[0] + in[3]) >> 1);
1278 out[1] = (qbyte) ((in[1] + in[4]) >> 1);
1279 out[2] = (qbyte) ((in[2] + in[5]) >> 1);
1286 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1291 if (*height > destheight)
1295 if (bytesperpixel == 4)
1297 for (y = 0;y < *height;y++)
1299 for (x = 0;x < *width;x++)
1301 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1302 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1303 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1304 out[3] = (qbyte) ((in[3] + in[nextrow+3]) >> 1);
1308 in += nextrow; // skip a line
1311 else if (bytesperpixel == 3)
1313 for (y = 0;y < *height;y++)
1315 for (x = 0;x < *width;x++)
1317 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1318 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1319 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1323 in += nextrow; // skip a line
1327 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1330 Sys_Error("Image_MipReduce: desired size already achieved\n");
1334 extern cvar_t r_shadow_bumpscale;
1335 void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
1338 const unsigned char *p0, *p1, *p2;
1340 float iwidth, iheight, ibumpscale, n[3];
1341 iwidth = 1.0f / width;
1342 iheight = 1.0f / height;
1343 ibumpscale = (255.0f * 3.0f) / (bumpscale * r_shadow_bumpscale.value);
1345 for (y = 0;y < height;y++)
1347 for (x = 0;x < width;x++)
1349 p0 = inpixels + (y * width + x) * 4;
1353 p1 = inpixels + (y * width + x) * 4;
1355 p1 = inpixels + (y * width) * 4;
1358 p1 = inpixels + (y * width + x + 1) * 4;
1359 if (y == height - 1)
1362 p2 = inpixels + (y * width + x) * 4;
1364 p2 = inpixels + x * 4;
1367 p2 = inpixels + ((y + 1) * width + x) * 4;
1371 dv[0][2] = ((p1[0] + p1[1] + p1[2]) * ibumpscale) - ((p0[0] + p0[1] + p0[2]) * ibumpscale);
1374 dv[1][2] = ((p2[0] + p2[1] + p2[2]) * ibumpscale) - ((p0[0] + p0[1] + p0[2]) * ibumpscale);
1375 n[0] = dv[0][1]*dv[1][2]-dv[0][2]*dv[1][1];
1376 n[1] = dv[0][2]*dv[1][0]-dv[0][0]*dv[1][2];
1377 n[2] = dv[0][0]*dv[1][1]-dv[0][1]*dv[1][0];
1379 n[0] = ((p0[0] + p0[1] + p0[2]) - (p1[0] + p1[1] + p1[2]));
1380 n[1] = ((p0[0] + p0[1] + p0[2]) - (p2[0] + p2[1] + p2[2]));
1383 out[0] = 128.0f + n[0] * 127.0f;
1384 out[1] = 128.0f + n[1] * 127.0f;
1385 out[2] = 128.0f + n[2] * 127.0f;