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 void PrintTargaHeader(TargaHeader *t)
204 Con_Printf("TargaHeader:\n");
205 Con_Printf("uint8 id_length = %i;\n", t->id_length);
206 Con_Printf("uint8 colormap_type = %i;\n", t->colormap_type);
207 Con_Printf("uint8 image_type = %i;\n", t->image_type);
208 Con_Printf("uint16 colormap_index = %i;\n", t->colormap_index);
209 Con_Printf("uint16 colormap_length = %i;\n", t->colormap_length);
210 Con_Printf("uint8 colormap_size = %i;\n", t->colormap_size);
211 Con_Printf("uint16 x_origin = %i;\n", t->x_origin);
212 Con_Printf("uint16 y_origin = %i;\n", t->y_origin);
213 Con_Printf("uint16 width = %i;\n", t->width);
214 Con_Printf("uint16 height = %i;\n", t->height);
215 Con_Printf("uint8 pixel_size = %i;\n", t->pixel_size);
216 Con_Printf("uint8 attributes = %i;\n", t->attributes);
224 qbyte *LoadTGA (const qbyte *f, int matchwidth, int matchheight)
226 int x, y, row_inc, compressed, readpixelcount, red, green, blue, alpha, runlen;
227 qbyte *pixbuf, *image_rgba;
228 const qbyte *fin, *enddata;
229 TargaHeader targa_header;
230 unsigned char palette[256*4], *p;
235 enddata = f + loadsize;
237 targa_header.id_length = f[0];
238 targa_header.colormap_type = f[1];
239 targa_header.image_type = f[2];
241 targa_header.colormap_index = f[3] + f[4] * 256;
242 targa_header.colormap_length = f[5] + f[6] * 256;
243 targa_header.colormap_size = f[7];
244 targa_header.x_origin = f[8] + f[9] * 256;
245 targa_header.y_origin = f[10] + f[11] * 256;
246 targa_header.width = f[12] + f[13] * 256;
247 targa_header.height = f[14] + f[15] * 256;
248 if (matchwidth && targa_header.width != matchwidth)
250 if (matchheight && targa_header.height != matchheight)
252 targa_header.pixel_size = f[16];
253 targa_header.attributes = f[17];
255 image_width = targa_header.width;
256 image_height = targa_header.height;
259 if (targa_header.id_length != 0)
260 fin += targa_header.id_length; // skip TARGA image comment
261 if (targa_header.image_type == 2 || targa_header.image_type == 10)
263 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
265 Con_Printf ("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
266 PrintTargaHeader(&targa_header);
270 else if (targa_header.image_type == 1 || targa_header.image_type == 9)
272 if (targa_header.pixel_size != 8)
274 Con_Printf ("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
275 PrintTargaHeader(&targa_header);
278 if (targa_header.colormap_length != 256)
280 Con_Printf ("LoadTGA: only 256 colormap_length supported\n");
281 PrintTargaHeader(&targa_header);
284 if (targa_header.colormap_index)
286 Con_Printf ("LoadTGA: colormap_index not supported\n");
287 PrintTargaHeader(&targa_header);
290 if (targa_header.colormap_size == 24)
292 for (x = 0;x < targa_header.colormap_length;x++)
294 palette[x*4+2] = *fin++;
295 palette[x*4+1] = *fin++;
296 palette[x*4+0] = *fin++;
297 palette[x*4+3] = 255;
300 else if (targa_header.colormap_size == 32)
302 for (x = 0;x < targa_header.colormap_length;x++)
304 palette[x*4+2] = *fin++;
305 palette[x*4+1] = *fin++;
306 palette[x*4+0] = *fin++;
307 palette[x*4+3] = *fin++;
312 Con_Printf ("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
313 PrintTargaHeader(&targa_header);
317 else if (targa_header.image_type == 3 || targa_header.image_type == 11)
319 if (targa_header.pixel_size != 8)
321 Con_Printf ("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
322 PrintTargaHeader(&targa_header);
328 Con_Printf ("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
329 PrintTargaHeader(&targa_header);
333 if (targa_header.attributes & 0x10)
335 Con_Printf ("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
339 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
342 Con_Printf ("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
346 // If bit 5 of attributes isn't set, the image has been stored from bottom to top
347 if ((targa_header.attributes & 0x20) == 0)
349 pixbuf = image_rgba + (image_height - 1)*image_width*4;
350 row_inc = -image_width*4*2;
358 compressed = targa_header.image_type == 9 || targa_header.image_type == 10 || targa_header.image_type == 11;
361 red = green = blue = alpha = 255;
362 while (y < image_height)
364 // decoder is mostly the same whether it's compressed or not
365 readpixelcount = 1000000;
367 if (compressed && fin < enddata)
370 // high bit indicates this is an RLE compressed run
373 runlen = 1 + (runlen & 0x7f);
376 while((runlen--) && y < image_height)
378 if (readpixelcount > 0)
381 red = green = blue = alpha = 255;
384 switch(targa_header.image_type)
389 p = palette + (*fin++) * 4;
403 if (targa_header.pixel_size == 32 && fin < enddata)
409 red = green = blue = *fin++;
419 if (x == image_width)
421 // end of line, advance to next
437 qbyte *LoadLMP (const qbyte *f, int matchwidth, int matchheight)
444 Con_Printf("LoadLMP: invalid LMP file\n");
448 // parse the very complicated header *chuckle*
449 width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
450 height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
451 if ((unsigned) width > 4096 || (unsigned) height > 4096)
453 Con_Printf("LoadLMP: invalid size\n");
456 if ((matchwidth && width != matchwidth) || (matchheight && height != matchheight))
459 if (loadsize < 8 + width * height)
461 Con_Printf("LoadLMP: invalid LMP file\n");
466 image_height = height;
468 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
471 Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
474 Image_Copy8bitRGBA(f + 8, image_rgba, image_width * image_height, palette_complete);
483 qbyte *LoadLMPAs8Bit (const qbyte *f, int matchwidth, int matchheight)
490 Con_Printf("LoadLMPAs8Bit: invalid LMP file\n");
494 // parse the very complicated header *chuckle*
495 width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
496 height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
497 if ((unsigned) width > 4096 || (unsigned) height > 4096)
499 Con_Printf("LoadLMPAs8Bit: invalid size\n");
502 if ((matchwidth && width != matchwidth) || (matchheight && height != matchheight))
505 if (loadsize < 8 + width * height)
507 Con_Printf("LoadLMPAs8Bit: invalid LMP file\n");
512 image_height = height;
514 image_8bit = Mem_Alloc(tempmempool, image_width * image_height);
517 Con_Printf("LoadLMPAs8Bit: not enough memory for %i by %i image\n", image_width, image_height);
520 memcpy(image_8bit, f + 8, image_width * image_height);
524 void Image_StripImageExtension (const char *in, char *out)
526 const char *end, *temp;
527 end = in + strlen(in);
531 if (strcmp(temp, ".tga") == 0 || strcmp(temp, ".pcx") == 0 || strcmp(temp, ".lmp") == 0)
541 qbyte *loadimagepixels (const char *filename, qboolean complain, int matchwidth, int matchheight)
544 char basename[256], name[256], *c;
545 Image_StripImageExtension(filename, basename); // strip .tga, .pcx and .lmp extensions to allow replacement by other types
546 // replace *'s with #, so commandline utils don't get confused when dealing with the external files
547 for (c = basename;*c;c++)
550 sprintf (name, "override/%s.tga", basename);
551 f = COM_LoadFile(name, true);
554 data = LoadTGA (f, matchwidth, matchheight);
558 sprintf (name, "textures/%s.tga", basename);
559 f = COM_LoadFile(name, true);
562 data = LoadTGA (f, matchwidth, matchheight);
566 sprintf (name, "textures/%s.pcx", basename);
567 f = COM_LoadFile(name, true);
570 data = LoadPCX (f, matchwidth, matchheight);
574 sprintf (name, "%s.tga", basename);
575 f = COM_LoadFile(name, true);
578 data = LoadTGA (f, matchwidth, matchheight);
582 sprintf (name, "%s.pcx", basename);
583 f = COM_LoadFile(name, true);
586 data = LoadPCX (f, matchwidth, matchheight);
590 sprintf (name, "%s.lmp", basename);
591 f = COM_LoadFile(name, true);
594 data = LoadLMP (f, matchwidth, matchheight);
599 Con_Printf ("Couldn't load %s.tga, .pcx, .lmp\n", filename);
603 int image_makemask (const qbyte *in, qbyte *out, int size)
607 for (i = 0;i < size;i++)
609 out[0] = out[1] = out[2] = 255;
619 qbyte* loadimagepixelsmask (const char *filename, qboolean complain, int matchwidth, int matchheight)
622 in = data = loadimagepixels(filename, complain, matchwidth, matchheight);
625 if (image_makemask(data, data, image_width * image_height))
626 return data; // some transparency
630 return NULL; // all opaque
634 rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
638 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
640 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
645 rtexture_t *loadtextureimagemask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
649 if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
651 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
656 rtexture_t *image_masktex;
657 rtexture_t *image_nmaptex;
658 rtexture_t *loadtextureimagewithmask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
662 image_masktex = NULL;
663 image_nmaptex = NULL;
664 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
667 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
669 if (flags & TEXF_ALPHA && image_makemask(data, data, image_width * image_height))
670 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
676 rtexture_t *loadtextureimagewithmaskandnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
680 image_masktex = NULL;
681 image_nmaptex = NULL;
682 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
685 data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
687 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
689 Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
690 image_nmaptex = R_LoadTexture2D(pool, va("%s_nmap", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
692 if (flags & TEXF_ALPHA && image_makemask(data, data2, image_width * image_height))
693 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
701 qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int height, const qbyte *data)
705 const qbyte *in, *end;
707 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
709 memset (buffer, 0, 18);
710 buffer[2] = 2; // uncompressed type
711 buffer[12] = (width >> 0) & 0xFF;
712 buffer[13] = (width >> 8) & 0xFF;
713 buffer[14] = (height >> 0) & 0xFF;
714 buffer[15] = (height >> 8) & 0xFF;
715 buffer[16] = 24; // pixel size
720 end = in + width*height*3;
721 for (;in < end;in += 3)
727 ret = COM_WriteFile (filename, buffer, width*height*3 + 18 );
733 void Image_WriteTGARGB (const char *filename, int width, int height, const qbyte *data)
737 const qbyte *in, *end;
739 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
741 memset (buffer, 0, 18);
742 buffer[2] = 2; // uncompressed type
743 buffer[12] = (width >> 0) & 0xFF;
744 buffer[13] = (width >> 8) & 0xFF;
745 buffer[14] = (height >> 0) & 0xFF;
746 buffer[15] = (height >> 8) & 0xFF;
747 buffer[16] = 24; // pixel size
749 // swap rgb to bgr and flip upside down
751 for (y = height - 1;y >= 0;y--)
753 in = data + y * width * 3;
754 end = in + width * 3;
755 for (;in < end;in += 3)
762 COM_WriteFile (filename, buffer, width*height*3 + 18 );
767 void Image_WriteTGARGBA (const char *filename, int width, int height, const qbyte *data)
771 const qbyte *in, *end;
773 buffer = Mem_Alloc(tempmempool, width*height*4 + 18);
775 memset (buffer, 0, 18);
776 buffer[2] = 2; // uncompressed type
777 buffer[12] = (width >> 0) & 0xFF;
778 buffer[13] = (width >> 8) & 0xFF;
779 buffer[14] = (height >> 0) & 0xFF;
780 buffer[15] = (height >> 8) & 0xFF;
781 buffer[16] = 32; // pixel size
783 // swap rgba to bgra and flip upside down
785 for (y = height - 1;y >= 0;y--)
787 in = data + y * width * 4;
788 end = in + width * 4;
789 for (;in < end;in += 4)
797 COM_WriteFile (filename, buffer, width*height*4 + 18 );
802 qboolean Image_CheckAlpha(const qbyte *data, int size, qboolean rgba)
808 for (end = data + size * 4, data += 3;data < end;data += 4)
814 // color 255 is transparent
815 for (end = data + size;data < end;data++)
822 static void Image_Resample32LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
824 int j, xi, oldx = 0, f, fstep, endx, lerp;
825 fstep = (int) (inwidth*65536.0f/outwidth);
827 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
832 in += (xi - oldx) * 4;
838 *out++ = (qbyte) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
839 *out++ = (qbyte) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
840 *out++ = (qbyte) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
841 *out++ = (qbyte) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
843 else // last pixel of the line has no pixel to lerp to
853 static void Image_Resample24LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
855 int j, xi, oldx = 0, f, fstep, endx, lerp;
856 fstep = (int) (inwidth*65536.0f/outwidth);
858 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
863 in += (xi - oldx) * 3;
869 *out++ = (qbyte) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
870 *out++ = (qbyte) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
871 *out++ = (qbyte) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
873 else // last pixel of the line has no pixel to lerp to
882 int resamplerowsize = 0;
883 qbyte *resamplerow1 = NULL;
884 qbyte *resamplerow2 = NULL;
885 mempool_t *resamplemempool = NULL;
887 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (qbyte) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
888 void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
890 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
894 fstep = (int) (inheight*65536.0f/outheight);
898 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
899 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
900 for (i = 0, f = 0;i < outheight;i++,f += fstep)
908 inrow = (qbyte *)indata + inwidth4*yi;
910 memcpy(resamplerow1, resamplerow2, outwidth4);
912 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
913 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
964 resamplerow1 -= outwidth4;
965 resamplerow2 -= outwidth4;
971 inrow = (qbyte *)indata + inwidth4*yi;
973 memcpy(resamplerow1, resamplerow2, outwidth4);
975 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
978 memcpy(out, resamplerow1, outwidth4);
983 void Image_Resample32Nearest(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
986 unsigned frac, fracstep;
987 // relies on int being 4 bytes
991 fracstep = inwidth*0x10000/outwidth;
992 for (i = 0;i < outheight;i++)
994 inrow = (int *)indata + inwidth*(i*inheight/outheight);
995 frac = fracstep >> 1;
999 out[0] = inrow[frac >> 16];frac += fracstep;
1000 out[1] = inrow[frac >> 16];frac += fracstep;
1001 out[2] = inrow[frac >> 16];frac += fracstep;
1002 out[3] = inrow[frac >> 16];frac += fracstep;
1008 out[0] = inrow[frac >> 16];frac += fracstep;
1009 out[1] = inrow[frac >> 16];frac += fracstep;
1014 out[0] = inrow[frac >> 16];frac += fracstep;
1020 void Image_Resample24Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1022 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
1026 fstep = (int) (inheight*65536.0f/outheight);
1030 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1031 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1032 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1040 inrow = (qbyte *)indata + inwidth3*yi;
1042 memcpy(resamplerow1, resamplerow2, outwidth3);
1044 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1045 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1089 resamplerow1 -= outwidth3;
1090 resamplerow2 -= outwidth3;
1096 inrow = (qbyte *)indata + inwidth3*yi;
1098 memcpy(resamplerow1, resamplerow2, outwidth3);
1100 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1103 memcpy(out, resamplerow1, outwidth3);
1108 void Image_Resample24Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1110 int i, j, f, inwidth3 = inwidth * 3;
1111 unsigned frac, fracstep;
1115 fracstep = inwidth*0x10000/outwidth;
1116 for (i = 0;i < outheight;i++)
1118 inrow = (qbyte *)indata + inwidth3*(i*inheight/outheight);
1119 frac = fracstep >> 1;
1123 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1124 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1125 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1126 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1131 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1132 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1137 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1148 void Image_Resample (const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int bytesperpixel, int quality)
1150 if (indepth != 1 || outdepth != 1)
1151 Sys_Error("Image_Resample: 3D resampling not supported\n");
1152 if (resamplerowsize < outwidth*4)
1155 Mem_Free(resamplerow1);
1156 resamplerowsize = outwidth*4;
1157 if (!resamplemempool)
1158 resamplemempool = Mem_AllocPool("Image Scaling Buffer");
1159 resamplerow1 = Mem_Alloc(resamplemempool, resamplerowsize*2);
1160 resamplerow2 = resamplerow1 + resamplerowsize;
1162 if (bytesperpixel == 4)
1165 Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1167 Image_Resample32Nearest(indata, inwidth, inheight, outdata, outwidth, outheight);
1169 else if (bytesperpixel == 3)
1172 Image_Resample24Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1174 Image_Resample24Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1177 Sys_Error("Image_Resample: unsupported bytesperpixel %i\n", bytesperpixel);
1180 // in can be the same as out
1181 void Image_MipReduce(const qbyte *in, qbyte *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth, int bytesperpixel)
1184 if (*depth != 1 || destdepth != 1)
1185 Sys_Error("Image_Resample: 3D resampling not supported\n");
1186 nextrow = *width * bytesperpixel;
1187 if (*width > destwidth)
1190 if (*height > destheight)
1194 if (bytesperpixel == 4)
1196 for (y = 0;y < *height;y++)
1198 for (x = 0;x < *width;x++)
1200 out[0] = (qbyte) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
1201 out[1] = (qbyte) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1202 out[2] = (qbyte) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1203 out[3] = (qbyte) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1207 in += nextrow; // skip a line
1210 else if (bytesperpixel == 3)
1212 for (y = 0;y < *height;y++)
1214 for (x = 0;x < *width;x++)
1216 out[0] = (qbyte) ((in[0] + in[3] + in[nextrow ] + in[nextrow+3]) >> 2);
1217 out[1] = (qbyte) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
1218 out[2] = (qbyte) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
1222 in += nextrow; // skip a line
1226 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1231 if (bytesperpixel == 4)
1233 for (y = 0;y < *height;y++)
1235 for (x = 0;x < *width;x++)
1237 out[0] = (qbyte) ((in[0] + in[4]) >> 1);
1238 out[1] = (qbyte) ((in[1] + in[5]) >> 1);
1239 out[2] = (qbyte) ((in[2] + in[6]) >> 1);
1240 out[3] = (qbyte) ((in[3] + in[7]) >> 1);
1246 else if (bytesperpixel == 3)
1248 for (y = 0;y < *height;y++)
1250 for (x = 0;x < *width;x++)
1252 out[0] = (qbyte) ((in[0] + in[3]) >> 1);
1253 out[1] = (qbyte) ((in[1] + in[4]) >> 1);
1254 out[2] = (qbyte) ((in[2] + in[5]) >> 1);
1261 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1266 if (*height > destheight)
1270 if (bytesperpixel == 4)
1272 for (y = 0;y < *height;y++)
1274 for (x = 0;x < *width;x++)
1276 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1277 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1278 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1279 out[3] = (qbyte) ((in[3] + in[nextrow+3]) >> 1);
1283 in += nextrow; // skip a line
1286 else if (bytesperpixel == 3)
1288 for (y = 0;y < *height;y++)
1290 for (x = 0;x < *width;x++)
1292 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1293 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1294 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1298 in += nextrow; // skip a line
1302 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1305 Sys_Error("Image_MipReduce: desired size already achieved\n");
1309 extern cvar_t r_shadow_bumpscale;
1310 void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
1313 const unsigned char *p0, *p1, *p2;
1315 float iwidth, iheight, ibumpscale, n[3];
1316 iwidth = 1.0f / width;
1317 iheight = 1.0f / height;
1318 ibumpscale = (255.0f * 3.0f) / (bumpscale * r_shadow_bumpscale.value);
1320 for (y = 0;y < height;y++)
1322 for (x = 0;x < width;x++)
1324 p0 = inpixels + (y * width + x) * 4;
1328 p1 = inpixels + (y * width + x) * 4;
1330 p1 = inpixels + (y * width) * 4;
1333 p1 = inpixels + (y * width + x + 1) * 4;
1334 if (y == height - 1)
1337 p2 = inpixels + (y * width + x) * 4;
1339 p2 = inpixels + x * 4;
1342 p2 = inpixels + ((y + 1) * width + x) * 4;
1346 dv[0][2] = ((p1[0] + p1[1] + p1[2]) * ibumpscale) - ((p0[0] + p0[1] + p0[2]) * ibumpscale);
1349 dv[1][2] = ((p2[0] + p2[1] + p2[2]) * ibumpscale) - ((p0[0] + p0[1] + p0[2]) * ibumpscale);
1350 n[0] = dv[0][1]*dv[1][2]-dv[0][2]*dv[1][1];
1351 n[1] = dv[0][2]*dv[1][0]-dv[0][0]*dv[1][2];
1352 n[2] = dv[0][0]*dv[1][1]-dv[0][1]*dv[1][0];
1354 n[0] = ((p0[0] + p0[1] + p0[2]) - (p1[0] + p1[1] + p1[2]));
1355 n[1] = ((p0[0] + p0[1] + p0[2]) - (p2[0] + p2[1] + p2[2]));
1358 out[0] = 128.0f + n[0] * 127.0f;
1359 out[1] = 128.0f + n[1] * 127.0f;
1360 out[2] = 128.0f + n[2] * 127.0f;