8 void Image_GammaRemapRGB(const qbyte *in, qbyte *out, int pixels, const qbyte *gammar, const qbyte *gammag, const qbyte *gammab)
12 out[0] = gammar[in[0]];
13 out[1] = gammag[in[1]];
14 out[2] = gammab[in[2]];
20 // note: pal must be 32bit color
21 void Image_Copy8bitRGBA(const qbyte *in, qbyte *out, int pixels, const unsigned int *pal)
23 int *iout = (void *)out;
59 =================================================================
63 =================================================================
72 unsigned short xmin,ymin,xmax,ymax;
73 unsigned short hres,vres;
74 unsigned char palette[48];
77 unsigned short bytes_per_line;
78 unsigned short palette_type;
87 qbyte* LoadPCX (const qbyte *f, int matchwidth, int matchheight)
90 qbyte *a, *b, *image_rgba, *pbuf;
91 const qbyte *palette, *fin, *enddata;
92 int x, y, x2, dataByte;
94 if (loadsize < (int)sizeof(pcx) + 768)
96 Con_Printf ("Bad pcx file\n");
102 memcpy(&pcx, fin, sizeof(pcx));
105 // LordHavoc: big-endian support ported from QF newtree
106 pcx.xmax = LittleShort (pcx.xmax);
107 pcx.xmin = LittleShort (pcx.xmin);
108 pcx.ymax = LittleShort (pcx.ymax);
109 pcx.ymin = LittleShort (pcx.ymin);
110 pcx.hres = LittleShort (pcx.hres);
111 pcx.vres = LittleShort (pcx.vres);
112 pcx.bytes_per_line = LittleShort (pcx.bytes_per_line);
113 pcx.palette_type = LittleShort (pcx.palette_type);
115 if (pcx.manufacturer != 0x0a || pcx.version != 5 || pcx.encoding != 1 || pcx.bits_per_pixel != 8 || pcx.xmax > 320 || pcx.ymax > 256)
117 Con_Printf ("Bad pcx file\n");
121 if (matchwidth && (pcx.xmax+1) != matchwidth)
125 if (matchheight && (pcx.ymax+1) != matchheight)
130 image_width = pcx.xmax+1;
131 image_height = pcx.ymax+1;
133 palette = f + loadsize - 768;
135 image_rgba = Mem_Alloc(tempmempool, image_width*image_height*4);
138 Con_Printf("LoadPCX: not enough memory for %i by %i image\n", image_width, image_height);
141 pbuf = image_rgba + image_width*image_height*3;
144 for (y = 0;y < image_height && fin < enddata;y++)
146 a = pbuf + y * image_width;
147 for (x = 0;x < image_width && fin < enddata;)
154 x2 = x + (dataByte & 0x3F);
156 if (x2 > image_width)
157 x2 = image_width; // technically an error
164 fin += pcx.bytes_per_line - image_width; // the number of bytes per line is always forced to an even number
165 while(x < image_width)
172 for(x = 0;x < image_width*image_height;x++)
185 =========================================================
189 =========================================================
192 typedef struct _TargaHeader
194 unsigned char id_length, colormap_type, image_type;
195 unsigned short colormap_index, colormap_length;
196 unsigned char colormap_size;
197 unsigned short x_origin, y_origin, width, height;
198 unsigned char pixel_size, attributes;
202 TargaHeader targa_header;
210 qbyte *LoadTGA (const qbyte *f, int matchwidth, int matchheight)
213 unsigned char red, green, blue, alpha, run, runlen;
214 qbyte *pixbuf, *image_rgba;
215 const qbyte *fin, *enddata;
219 targa_header.id_length = f[0];
220 targa_header.colormap_type = f[1];
221 targa_header.image_type = f[2];
223 targa_header.colormap_index = f[3] + f[4] * 256;
224 targa_header.colormap_length = f[5] + f[6] * 256;
225 targa_header.colormap_size = f[7];
226 targa_header.x_origin = f[8] + f[9] * 256;
227 targa_header.y_origin = f[10] + f[11] * 256;
228 targa_header.width = f[12] + f[13] * 256;
229 targa_header.height = f[14] + f[15] * 256;
230 if (matchwidth && targa_header.width != matchwidth)
232 if (matchheight && targa_header.height != matchheight)
234 targa_header.pixel_size = f[16];
235 targa_header.attributes = f[17];
237 if (targa_header.image_type != 2 && targa_header.image_type != 10)
239 Con_Printf ("LoadTGA: Only type 2 and 10 targa RGB images supported\n");
243 if (targa_header.colormap_type != 0 || (targa_header.pixel_size != 32 && targa_header.pixel_size != 24))
245 Con_Printf ("LoadTGA: Only 32 or 24 bit images supported (no colormaps)\n");
249 enddata = f + loadsize;
251 image_width = targa_header.width;
252 image_height = targa_header.height;
254 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
257 Con_Printf ("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
262 if (targa_header.id_length != 0)
263 fin += targa_header.id_length; // skip TARGA image comment
265 // If bit 5 of attributes isn't set, the image has been stored from bottom to top
266 if ((targa_header.attributes & 0x20) == 0)
268 pixbuf = image_rgba + (image_height - 1)*image_width*4;
269 row_inc = -image_width*4*2;
277 if (targa_header.image_type == 2)
279 // Uncompressed, RGB images
280 if (targa_header.pixel_size == 24)
282 if (fin + image_width * image_height * 3 <= enddata)
284 for(y = 0;y < image_height;y++)
286 for(x = 0;x < image_width;x++)
300 if (fin + image_width * image_height * 4 <= enddata)
302 for(y = 0;y < image_height;y++)
304 for(x = 0;x < image_width;x++)
317 else if (targa_header.image_type==10)
319 // Runlength encoded RGB images
322 while (y < image_height && fin < enddata)
327 // RLE compressed run
328 runlen = 1 + (runlen & 0x7f);
329 if (targa_header.pixel_size == 24)
331 if (fin + 3 > enddata)
340 if (fin + 4 > enddata)
348 while (runlen && y < image_height)
351 if (run > image_width - x)
352 run = image_width - x;
362 if (x == image_width)
364 // end of line, advance to next
373 // RLE uncompressed run
374 runlen = 1 + (runlen & 0x7f);
375 while (runlen && y < image_height)
378 if (run > image_width - x)
379 run = image_width - x;
382 if (targa_header.pixel_size == 24)
384 if (fin + run * 3 > enddata)
397 if (fin + run * 4 > enddata)
408 if (x == image_width)
410 // end of line, advance to next
427 qbyte *LoadLMP (const qbyte *f, int matchwidth, int matchheight)
434 Con_Printf("LoadLMP: invalid LMP file\n");
438 // parse the very complicated header *chuckle*
439 width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
440 height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
441 if ((unsigned) width > 4096 || (unsigned) height > 4096)
443 Con_Printf("LoadLMP: invalid size\n");
446 if ((matchwidth && width != matchwidth) || (matchheight && height != matchheight))
449 if (loadsize < 8 + width * height)
451 Con_Printf("LoadLMP: invalid LMP file\n");
456 image_height = height;
458 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
461 Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
464 Image_Copy8bitRGBA(f + 8, image_rgba, image_width * image_height, palette_complete);
473 qbyte *LoadLMPAs8Bit (const qbyte *f, int matchwidth, int matchheight)
480 Con_Printf("LoadLMPAs8Bit: invalid LMP file\n");
484 // parse the very complicated header *chuckle*
485 width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
486 height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
487 if ((unsigned) width > 4096 || (unsigned) height > 4096)
489 Con_Printf("LoadLMPAs8Bit: invalid size\n");
492 if ((matchwidth && width != matchwidth) || (matchheight && height != matchheight))
495 if (loadsize < 8 + width * height)
497 Con_Printf("LoadLMPAs8Bit: invalid LMP file\n");
502 image_height = height;
504 image_8bit = Mem_Alloc(tempmempool, image_width * image_height);
507 Con_Printf("LoadLMPAs8Bit: not enough memory for %i by %i image\n", image_width, image_height);
510 memcpy(image_8bit, f + 8, image_width * image_height);
514 void Image_StripImageExtension (const char *in, char *out)
516 const char *end, *temp;
517 end = in + strlen(in);
521 if (strcmp(temp, ".tga") == 0 || strcmp(temp, ".pcx") == 0 || strcmp(temp, ".lmp") == 0)
531 qbyte *loadimagepixels (const char *filename, qboolean complain, int matchwidth, int matchheight)
534 char basename[256], name[256], *c;
535 Image_StripImageExtension(filename, basename); // strip .tga, .pcx and .lmp extensions to allow replacement by other types
536 // replace *'s with #, so commandline utils don't get confused when dealing with the external files
537 for (c = basename;*c;c++)
540 sprintf (name, "override/%s.tga", basename);
541 f = COM_LoadFile(name, true);
544 data = LoadTGA (f, matchwidth, matchheight);
548 sprintf (name, "textures/%s.tga", basename);
549 f = COM_LoadFile(name, true);
552 data = LoadTGA (f, matchwidth, matchheight);
556 sprintf (name, "textures/%s.pcx", basename);
557 f = COM_LoadFile(name, true);
560 data = LoadPCX (f, matchwidth, matchheight);
564 sprintf (name, "%s.tga", basename);
565 f = COM_LoadFile(name, true);
568 data = LoadTGA (f, matchwidth, matchheight);
572 sprintf (name, "%s.pcx", basename);
573 f = COM_LoadFile(name, true);
576 data = LoadPCX (f, matchwidth, matchheight);
580 sprintf (name, "%s.lmp", basename);
581 f = COM_LoadFile(name, true);
584 data = LoadLMP (f, matchwidth, matchheight);
589 Con_Printf ("Couldn't load %s.tga, .pcx, .lmp\n", filename);
593 int image_makemask (const qbyte *in, qbyte *out, int size)
597 for (i = 0;i < size;i++)
599 out[0] = out[1] = out[2] = 255;
609 qbyte* loadimagepixelsmask (const char *filename, qboolean complain, int matchwidth, int matchheight)
612 in = data = loadimagepixels(filename, complain, matchwidth, matchheight);
615 if (image_makemask(data, data, image_width * image_height))
616 return data; // some transparency
620 return NULL; // all opaque
624 rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
628 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
630 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
635 rtexture_t *loadtextureimagemask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
639 if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
641 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
646 rtexture_t *image_masktex;
647 rtexture_t *image_nmaptex;
648 rtexture_t *loadtextureimagewithmask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
652 image_masktex = NULL;
653 image_nmaptex = NULL;
654 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
657 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
659 if (flags & TEXF_ALPHA && image_makemask(data, data, image_width * image_height))
660 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
666 rtexture_t *loadtextureimagewithmaskandnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
670 image_masktex = NULL;
671 image_nmaptex = NULL;
672 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
675 data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
677 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
679 Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
680 image_nmaptex = R_LoadTexture2D(pool, va("%s_nmap", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
682 if (flags & TEXF_ALPHA && image_makemask(data, data2, image_width * image_height))
683 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
691 qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int height, const qbyte *data)
695 const qbyte *in, *end;
697 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
699 memset (buffer, 0, 18);
700 buffer[2] = 2; // uncompressed type
701 buffer[12] = (width >> 0) & 0xFF;
702 buffer[13] = (width >> 8) & 0xFF;
703 buffer[14] = (height >> 0) & 0xFF;
704 buffer[15] = (height >> 8) & 0xFF;
705 buffer[16] = 24; // pixel size
710 end = in + width*height*3;
711 for (;in < end;in += 3)
717 ret = COM_WriteFile (filename, buffer, width*height*3 + 18 );
723 void Image_WriteTGARGB (const char *filename, int width, int height, const qbyte *data)
727 const qbyte *in, *end;
729 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
731 memset (buffer, 0, 18);
732 buffer[2] = 2; // uncompressed type
733 buffer[12] = (width >> 0) & 0xFF;
734 buffer[13] = (width >> 8) & 0xFF;
735 buffer[14] = (height >> 0) & 0xFF;
736 buffer[15] = (height >> 8) & 0xFF;
737 buffer[16] = 24; // pixel size
739 // swap rgb to bgr and flip upside down
741 for (y = height - 1;y >= 0;y--)
743 in = data + y * width * 3;
744 end = in + width * 3;
745 for (;in < end;in += 3)
752 COM_WriteFile (filename, buffer, width*height*3 + 18 );
757 void Image_WriteTGARGBA (const char *filename, int width, int height, const qbyte *data)
761 const qbyte *in, *end;
763 buffer = Mem_Alloc(tempmempool, width*height*4 + 18);
765 memset (buffer, 0, 18);
766 buffer[2] = 2; // uncompressed type
767 buffer[12] = (width >> 0) & 0xFF;
768 buffer[13] = (width >> 8) & 0xFF;
769 buffer[14] = (height >> 0) & 0xFF;
770 buffer[15] = (height >> 8) & 0xFF;
771 buffer[16] = 32; // pixel size
773 // swap rgba to bgra and flip upside down
775 for (y = height - 1;y >= 0;y--)
777 in = data + y * width * 4;
778 end = in + width * 4;
779 for (;in < end;in += 4)
787 COM_WriteFile (filename, buffer, width*height*4 + 18 );
792 qboolean Image_CheckAlpha(const qbyte *data, int size, qboolean rgba)
798 for (end = data + size * 4, data += 3;data < end;data += 4)
804 // color 255 is transparent
805 for (end = data + size;data < end;data++)
812 static void Image_Resample32LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
814 int j, xi, oldx = 0, f, fstep, endx, lerp;
815 fstep = (int) (inwidth*65536.0f/outwidth);
817 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
822 in += (xi - oldx) * 4;
828 *out++ = (qbyte) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
829 *out++ = (qbyte) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
830 *out++ = (qbyte) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
831 *out++ = (qbyte) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
833 else // last pixel of the line has no pixel to lerp to
843 static void Image_Resample24LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
845 int j, xi, oldx = 0, f, fstep, endx, lerp;
846 fstep = (int) (inwidth*65536.0f/outwidth);
848 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
853 in += (xi - oldx) * 3;
859 *out++ = (qbyte) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
860 *out++ = (qbyte) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
861 *out++ = (qbyte) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
863 else // last pixel of the line has no pixel to lerp to
872 int resamplerowsize = 0;
873 qbyte *resamplerow1 = NULL;
874 qbyte *resamplerow2 = NULL;
875 mempool_t *resamplemempool = NULL;
877 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (qbyte) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
878 void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
880 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
884 fstep = (int) (inheight*65536.0f/outheight);
888 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
889 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
890 for (i = 0, f = 0;i < outheight;i++,f += fstep)
898 inrow = (qbyte *)indata + inwidth4*yi;
900 memcpy(resamplerow1, resamplerow2, outwidth4);
902 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
903 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
954 resamplerow1 -= outwidth4;
955 resamplerow2 -= outwidth4;
961 inrow = (qbyte *)indata + inwidth4*yi;
963 memcpy(resamplerow1, resamplerow2, outwidth4);
965 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
968 memcpy(out, resamplerow1, outwidth4);
973 void Image_Resample32Nearest(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
976 unsigned frac, fracstep;
977 // relies on int being 4 bytes
981 fracstep = inwidth*0x10000/outwidth;
982 for (i = 0;i < outheight;i++)
984 inrow = (int *)indata + inwidth*(i*inheight/outheight);
985 frac = fracstep >> 1;
989 out[0] = inrow[frac >> 16];frac += fracstep;
990 out[1] = inrow[frac >> 16];frac += fracstep;
991 out[2] = inrow[frac >> 16];frac += fracstep;
992 out[3] = inrow[frac >> 16];frac += fracstep;
998 out[0] = inrow[frac >> 16];frac += fracstep;
999 out[1] = inrow[frac >> 16];frac += fracstep;
1004 out[0] = inrow[frac >> 16];frac += fracstep;
1010 void Image_Resample24Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1012 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
1016 fstep = (int) (inheight*65536.0f/outheight);
1020 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1021 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1022 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1030 inrow = (qbyte *)indata + inwidth3*yi;
1032 memcpy(resamplerow1, resamplerow2, outwidth3);
1034 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1035 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1079 resamplerow1 -= outwidth3;
1080 resamplerow2 -= outwidth3;
1086 inrow = (qbyte *)indata + inwidth3*yi;
1088 memcpy(resamplerow1, resamplerow2, outwidth3);
1090 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1093 memcpy(out, resamplerow1, outwidth3);
1098 void Image_Resample24Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1100 int i, j, f, inwidth3 = inwidth * 3;
1101 unsigned frac, fracstep;
1105 fracstep = inwidth*0x10000/outwidth;
1106 for (i = 0;i < outheight;i++)
1108 inrow = (qbyte *)indata + inwidth3*(i*inheight/outheight);
1109 frac = fracstep >> 1;
1113 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1114 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1115 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1116 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1121 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1122 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1127 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1138 void Image_Resample (const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int bytesperpixel, int quality)
1140 if (indepth != 1 || outdepth != 1)
1141 Sys_Error("Image_Resample: 3D resampling not supported\n");
1142 if (resamplerowsize < outwidth*4)
1145 Mem_Free(resamplerow1);
1146 resamplerowsize = outwidth*4;
1147 if (!resamplemempool)
1148 resamplemempool = Mem_AllocPool("Image Scaling Buffer");
1149 resamplerow1 = Mem_Alloc(resamplemempool, resamplerowsize*2);
1150 resamplerow2 = resamplerow1 + resamplerowsize;
1152 if (bytesperpixel == 4)
1155 Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1157 Image_Resample32Nearest(indata, inwidth, inheight, outdata, outwidth, outheight);
1159 else if (bytesperpixel == 3)
1162 Image_Resample24Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1164 Image_Resample24Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1167 Sys_Error("Image_Resample: unsupported bytesperpixel %i\n", bytesperpixel);
1170 // in can be the same as out
1171 void Image_MipReduce(const qbyte *in, qbyte *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth, int bytesperpixel)
1174 if (*depth != 1 || destdepth != 1)
1175 Sys_Error("Image_Resample: 3D resampling not supported\n");
1176 nextrow = *width * bytesperpixel;
1177 if (*width > destwidth)
1180 if (*height > destheight)
1184 if (bytesperpixel == 4)
1186 for (y = 0;y < *height;y++)
1188 for (x = 0;x < *width;x++)
1190 out[0] = (qbyte) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
1191 out[1] = (qbyte) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1192 out[2] = (qbyte) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1193 out[3] = (qbyte) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1197 in += nextrow; // skip a line
1200 else if (bytesperpixel == 3)
1202 for (y = 0;y < *height;y++)
1204 for (x = 0;x < *width;x++)
1206 out[0] = (qbyte) ((in[0] + in[3] + in[nextrow ] + in[nextrow+3]) >> 2);
1207 out[1] = (qbyte) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
1208 out[2] = (qbyte) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
1212 in += nextrow; // skip a line
1216 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1221 if (bytesperpixel == 4)
1223 for (y = 0;y < *height;y++)
1225 for (x = 0;x < *width;x++)
1227 out[0] = (qbyte) ((in[0] + in[4]) >> 1);
1228 out[1] = (qbyte) ((in[1] + in[5]) >> 1);
1229 out[2] = (qbyte) ((in[2] + in[6]) >> 1);
1230 out[3] = (qbyte) ((in[3] + in[7]) >> 1);
1236 else if (bytesperpixel == 3)
1238 for (y = 0;y < *height;y++)
1240 for (x = 0;x < *width;x++)
1242 out[0] = (qbyte) ((in[0] + in[3]) >> 1);
1243 out[1] = (qbyte) ((in[1] + in[4]) >> 1);
1244 out[2] = (qbyte) ((in[2] + in[5]) >> 1);
1251 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1256 if (*height > destheight)
1260 if (bytesperpixel == 4)
1262 for (y = 0;y < *height;y++)
1264 for (x = 0;x < *width;x++)
1266 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1267 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1268 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1269 out[3] = (qbyte) ((in[3] + in[nextrow+3]) >> 1);
1273 in += nextrow; // skip a line
1276 else if (bytesperpixel == 3)
1278 for (y = 0;y < *height;y++)
1280 for (x = 0;x < *width;x++)
1282 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1283 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1284 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1288 in += nextrow; // skip a line
1292 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1295 Sys_Error("Image_MipReduce: desired size already achieved\n");
1299 extern cvar_t r_shadow_bumpscale;
1300 void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
1303 const unsigned char *p0, *p1, *p2;
1305 float iwidth, iheight, ibumpscale, n[3];
1306 iwidth = 1.0f / width;
1307 iheight = 1.0f / height;
1308 ibumpscale = (255.0f * 3.0f) / (bumpscale * r_shadow_bumpscale.value);
1310 for (y = 0;y < height;y++)
1312 for (x = 0;x < width;x++)
1314 p0 = inpixels + (y * width + x) * 4;
1318 p1 = inpixels + (y * width + x) * 4;
1320 p1 = inpixels + (y * width) * 4;
1323 p1 = inpixels + (y * width + x + 1) * 4;
1324 if (y == height - 1)
1327 p2 = inpixels + (y * width + x) * 4;
1329 p2 = inpixels + x * 4;
1332 p2 = inpixels + ((y + 1) * width + x) * 4;
1336 dv[0][2] = ((p1[0] + p1[1] + p1[2]) * ibumpscale) - ((p0[0] + p0[1] + p0[2]) * ibumpscale);
1339 dv[1][2] = ((p2[0] + p2[1] + p2[2]) * ibumpscale) - ((p0[0] + p0[1] + p0[2]) * ibumpscale);
1340 n[0] = dv[0][1]*dv[1][2]-dv[0][2]*dv[1][1];
1341 n[1] = dv[0][2]*dv[1][0]-dv[0][0]*dv[1][2];
1342 n[2] = dv[0][0]*dv[1][1]-dv[0][1]*dv[1][0];
1344 n[0] = ((p0[0] + p0[1] + p0[2]) - (p1[0] + p1[1] + p1[2]));
1345 n[1] = ((p0[0] + p0[1] + p0[2]) - (p2[0] + p2[1] + p2[2]));
1348 out[0] = 128.0f + n[0] * 127.0f;
1349 out[1] = 128.0f + n[1] * 127.0f;
1350 out[2] = 128.0f + n[2] * 127.0f;