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 < 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, "textures/%s.tga", basename);
541 f = COM_LoadFile(name, true);
544 data = LoadTGA (f, matchwidth, matchheight);
548 sprintf (name, "textures/%s.pcx", basename);
549 f = COM_LoadFile(name, true);
552 data = LoadPCX (f, matchwidth, matchheight);
556 sprintf (name, "%s.tga", basename);
557 f = COM_LoadFile(name, true);
560 data = LoadTGA (f, matchwidth, matchheight);
564 sprintf (name, "%s.pcx", basename);
565 f = COM_LoadFile(name, true);
568 data = LoadPCX (f, matchwidth, matchheight);
572 sprintf (name, "%s.lmp", basename);
573 f = COM_LoadFile(name, true);
576 data = LoadLMP (f, matchwidth, matchheight);
581 Con_Printf ("Couldn't load %s.tga, .pcx, .lmp\n", filename);
585 int image_makemask (const qbyte *in, qbyte *out, int size)
589 for (i = 0;i < size;i++)
591 out[0] = out[1] = out[2] = 255;
601 qbyte* loadimagepixelsmask (const char *filename, qboolean complain, int matchwidth, int matchheight)
604 in = data = loadimagepixels(filename, complain, matchwidth, matchheight);
607 if (image_makemask(data, data, image_width * image_height))
608 return data; // some transparency
612 return NULL; // all opaque
616 rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
620 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
622 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
627 rtexture_t *loadtextureimagemask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
631 if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
633 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
638 rtexture_t *image_masktex;
639 rtexture_t *image_nmaptex;
640 rtexture_t *loadtextureimagewithmask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
644 image_masktex = NULL;
645 image_nmaptex = NULL;
646 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
649 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
651 if (flags & TEXF_ALPHA && image_makemask(data, data, image_width * image_height))
652 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
658 rtexture_t *loadtextureimagewithmaskandnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
662 image_masktex = NULL;
663 image_nmaptex = NULL;
664 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
667 data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
669 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
671 Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
672 image_nmaptex = R_LoadTexture2D(pool, va("%s_nmap", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
674 if (flags & TEXF_ALPHA && image_makemask(data, data2, image_width * image_height))
675 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
683 qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int height, const qbyte *data)
687 const qbyte *in, *end;
689 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
691 memset (buffer, 0, 18);
692 buffer[2] = 2; // uncompressed type
693 buffer[12] = (width >> 0) & 0xFF;
694 buffer[13] = (width >> 8) & 0xFF;
695 buffer[14] = (height >> 0) & 0xFF;
696 buffer[15] = (height >> 8) & 0xFF;
697 buffer[16] = 24; // pixel size
702 end = in + width*height*3;
703 for (;in < end;in += 3)
709 ret = COM_WriteFile (filename, buffer, width*height*3 + 18 );
715 void Image_WriteTGARGB (const char *filename, int width, int height, const qbyte *data)
719 const qbyte *in, *end;
721 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
723 memset (buffer, 0, 18);
724 buffer[2] = 2; // uncompressed type
725 buffer[12] = (width >> 0) & 0xFF;
726 buffer[13] = (width >> 8) & 0xFF;
727 buffer[14] = (height >> 0) & 0xFF;
728 buffer[15] = (height >> 8) & 0xFF;
729 buffer[16] = 24; // pixel size
731 // swap rgb to bgr and flip upside down
733 for (y = height - 1;y >= 0;y--)
735 in = data + y * width * 3;
736 end = in + width * 3;
737 for (;in < end;in += 3)
744 COM_WriteFile (filename, buffer, width*height*3 + 18 );
749 void Image_WriteTGARGBA (const char *filename, int width, int height, const qbyte *data)
753 const qbyte *in, *end;
755 buffer = Mem_Alloc(tempmempool, width*height*4 + 18);
757 memset (buffer, 0, 18);
758 buffer[2] = 2; // uncompressed type
759 buffer[12] = (width >> 0) & 0xFF;
760 buffer[13] = (width >> 8) & 0xFF;
761 buffer[14] = (height >> 0) & 0xFF;
762 buffer[15] = (height >> 8) & 0xFF;
763 buffer[16] = 32; // pixel size
765 // swap rgba to bgra and flip upside down
767 for (y = height - 1;y >= 0;y--)
769 in = data + y * width * 4;
770 end = in + width * 4;
771 for (;in < end;in += 4)
779 COM_WriteFile (filename, buffer, width*height*4 + 18 );
784 qboolean Image_CheckAlpha(const qbyte *data, int size, qboolean rgba)
790 for (end = data + size * 4, data += 3;data < end;data += 4)
796 // color 255 is transparent
797 for (end = data + size;data < end;data++)
804 static void Image_Resample32LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
806 int j, xi, oldx = 0, f, fstep, endx, lerp;
807 fstep = (int) (inwidth*65536.0f/outwidth);
809 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
814 in += (xi - oldx) * 4;
820 *out++ = (qbyte) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
821 *out++ = (qbyte) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
822 *out++ = (qbyte) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
823 *out++ = (qbyte) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
825 else // last pixel of the line has no pixel to lerp to
835 static void Image_Resample24LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
837 int j, xi, oldx = 0, f, fstep, endx, lerp;
838 fstep = (int) (inwidth*65536.0f/outwidth);
840 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
845 in += (xi - oldx) * 3;
851 *out++ = (qbyte) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
852 *out++ = (qbyte) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
853 *out++ = (qbyte) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
855 else // last pixel of the line has no pixel to lerp to
864 int resamplerowsize = 0;
865 qbyte *resamplerow1 = NULL;
866 qbyte *resamplerow2 = NULL;
867 mempool_t *resamplemempool = NULL;
869 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (qbyte) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
870 void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
872 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
876 fstep = (int) (inheight*65536.0f/outheight);
880 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
881 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
882 for (i = 0, f = 0;i < outheight;i++,f += fstep)
890 inrow = (qbyte *)indata + inwidth4*yi;
892 memcpy(resamplerow1, resamplerow2, outwidth4);
894 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
895 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
946 resamplerow1 -= outwidth4;
947 resamplerow2 -= outwidth4;
953 inrow = (qbyte *)indata + inwidth4*yi;
955 memcpy(resamplerow1, resamplerow2, outwidth4);
957 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
960 memcpy(out, resamplerow1, outwidth4);
965 void Image_Resample32Nearest(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
968 unsigned frac, fracstep;
969 // relies on int being 4 bytes
973 fracstep = inwidth*0x10000/outwidth;
974 for (i = 0;i < outheight;i++)
976 inrow = (int *)indata + inwidth*(i*inheight/outheight);
977 frac = fracstep >> 1;
981 out[0] = inrow[frac >> 16];frac += fracstep;
982 out[1] = inrow[frac >> 16];frac += fracstep;
983 out[2] = inrow[frac >> 16];frac += fracstep;
984 out[3] = inrow[frac >> 16];frac += fracstep;
990 out[0] = inrow[frac >> 16];frac += fracstep;
991 out[1] = inrow[frac >> 16];frac += fracstep;
996 out[0] = inrow[frac >> 16];frac += fracstep;
1002 void Image_Resample24Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1004 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
1008 fstep = (int) (inheight*65536.0f/outheight);
1012 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1013 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1014 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1022 inrow = (qbyte *)indata + inwidth3*yi;
1024 memcpy(resamplerow1, resamplerow2, outwidth3);
1026 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1027 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1071 resamplerow1 -= outwidth3;
1072 resamplerow2 -= outwidth3;
1078 inrow = (qbyte *)indata + inwidth3*yi;
1080 memcpy(resamplerow1, resamplerow2, outwidth3);
1082 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1085 memcpy(out, resamplerow1, outwidth3);
1090 void Image_Resample24Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1092 int i, j, f, inwidth3 = inwidth * 3;
1093 unsigned frac, fracstep;
1097 fracstep = inwidth*0x10000/outwidth;
1098 for (i = 0;i < outheight;i++)
1100 inrow = (qbyte *)indata + inwidth3*(i*inheight/outheight);
1101 frac = fracstep >> 1;
1105 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1106 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1107 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1108 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
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;
1119 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1130 void Image_Resample (const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int bytesperpixel, int quality)
1132 if (indepth != 1 || outdepth != 1)
1133 Sys_Error("Image_Resample: 3D resampling not supported\n");
1134 if (resamplerowsize < outwidth*4)
1137 Mem_Free(resamplerow1);
1138 resamplerowsize = outwidth*4;
1139 if (!resamplemempool)
1140 resamplemempool = Mem_AllocPool("Image Scaling Buffer");
1141 resamplerow1 = Mem_Alloc(resamplemempool, resamplerowsize*2);
1142 resamplerow2 = resamplerow1 + resamplerowsize;
1144 if (bytesperpixel == 4)
1147 Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1149 Image_Resample32Nearest(indata, inwidth, inheight, outdata, outwidth, outheight);
1151 else if (bytesperpixel == 3)
1154 Image_Resample24Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1156 Image_Resample24Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1159 Sys_Error("Image_Resample: unsupported bytesperpixel %i\n", bytesperpixel);
1162 // in can be the same as out
1163 void Image_MipReduce(const qbyte *in, qbyte *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth, int bytesperpixel)
1166 if (*depth != 1 || destdepth != 1)
1167 Sys_Error("Image_Resample: 3D resampling not supported\n");
1168 nextrow = *width * bytesperpixel;
1169 if (*width > destwidth)
1172 if (*height > destheight)
1176 if (bytesperpixel == 4)
1178 for (y = 0;y < *height;y++)
1180 for (x = 0;x < *width;x++)
1182 out[0] = (qbyte) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
1183 out[1] = (qbyte) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1184 out[2] = (qbyte) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1185 out[3] = (qbyte) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1189 in += nextrow; // skip a line
1192 else if (bytesperpixel == 3)
1194 for (y = 0;y < *height;y++)
1196 for (x = 0;x < *width;x++)
1198 out[0] = (qbyte) ((in[0] + in[3] + in[nextrow ] + in[nextrow+3]) >> 2);
1199 out[1] = (qbyte) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
1200 out[2] = (qbyte) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
1204 in += nextrow; // skip a line
1208 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1213 if (bytesperpixel == 4)
1215 for (y = 0;y < *height;y++)
1217 for (x = 0;x < *width;x++)
1219 out[0] = (qbyte) ((in[0] + in[4]) >> 1);
1220 out[1] = (qbyte) ((in[1] + in[5]) >> 1);
1221 out[2] = (qbyte) ((in[2] + in[6]) >> 1);
1222 out[3] = (qbyte) ((in[3] + in[7]) >> 1);
1228 else if (bytesperpixel == 3)
1230 for (y = 0;y < *height;y++)
1232 for (x = 0;x < *width;x++)
1234 out[0] = (qbyte) ((in[0] + in[3]) >> 1);
1235 out[1] = (qbyte) ((in[1] + in[4]) >> 1);
1236 out[2] = (qbyte) ((in[2] + in[5]) >> 1);
1243 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1248 if (*height > destheight)
1252 if (bytesperpixel == 4)
1254 for (y = 0;y < *height;y++)
1256 for (x = 0;x < *width;x++)
1258 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1259 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1260 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1261 out[3] = (qbyte) ((in[3] + in[nextrow+3]) >> 1);
1265 in += nextrow; // skip a line
1268 else if (bytesperpixel == 3)
1270 for (y = 0;y < *height;y++)
1272 for (x = 0;x < *width;x++)
1274 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1275 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1276 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1280 in += nextrow; // skip a line
1284 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1287 Sys_Error("Image_MipReduce: desired size already achieved\n");
1291 void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
1294 const unsigned char *p0, *p1, *p2;
1296 float iwidth, iheight, ibumpscale, n[3];
1297 iwidth = 1.0f / width;
1298 iheight = 1.0f / height;
1299 ibumpscale = (255.0f * 3.0f) / (bumpscale * 8.0f);
1301 for (y = 0;y < height;y++)
1303 for (x = 0;x < width;x++)
1305 p0 = inpixels + (y * width + x) * 4;
1309 p1 = inpixels + (y * width + x) * 4;
1311 p1 = inpixels + (y * width) * 4;
1314 p1 = inpixels + (y * width + x + 1) * 4;
1315 if (y == height - 1)
1318 p2 = inpixels + (y * width + x) * 4;
1320 p2 = inpixels + x * 4;
1323 p2 = inpixels + ((y + 1) * width + x) * 4;
1327 dv[0][2] = ((p1[0] + p1[1] + p1[2]) * ibumpscale) - ((p0[0] + p0[1] + p0[2]) * ibumpscale);
1330 dv[1][2] = ((p2[0] + p2[1] + p2[2]) * ibumpscale) - ((p0[0] + p0[1] + p0[2]) * ibumpscale);
1331 n[0] = dv[0][1]*dv[1][2]-dv[0][2]*dv[1][1];
1332 n[1] = dv[0][2]*dv[1][0]-dv[0][0]*dv[1][2];
1333 n[2] = dv[0][0]*dv[1][1]-dv[0][1]*dv[1][0];
1335 n[0] = ((p0[0] + p0[1] + p0[2]) - (p1[0] + p1[1] + p1[2]));
1336 n[1] = ((p0[0] + p0[1] + p0[2]) - (p2[0] + p2[1] + p2[2]));
1339 out[0] = 128.0f + n[0] * 127.0f;
1340 out[1] = 128.0f + n[1] * 127.0f;
1341 out[2] = 128.0f + n[2] * 127.0f;