10 void Image_GammaRemapRGB(const qbyte *in, qbyte *out, int pixels, const qbyte *gammar, const qbyte *gammag, const qbyte *gammab)
14 out[0] = gammar[in[0]];
15 out[1] = gammag[in[1]];
16 out[2] = gammab[in[2]];
22 // note: pal must be 32bit color
23 void Image_Copy8bitRGBA(const qbyte *in, qbyte *out, int pixels, const unsigned int *pal)
25 int *iout = (void *)out;
61 =================================================================
65 =================================================================
74 unsigned short xmin,ymin,xmax,ymax;
75 unsigned short hres,vres;
76 unsigned char palette[48];
79 unsigned short bytes_per_line;
80 unsigned short palette_type;
89 qbyte* LoadPCX (const qbyte *f, int matchwidth, int matchheight)
92 qbyte *a, *b, *image_rgba, *pbuf;
93 const qbyte *palette, *fin, *enddata;
94 int x, y, x2, dataByte;
96 if (loadsize < (int)sizeof(pcx) + 768)
98 Con_Printf ("Bad pcx file\n");
104 memcpy(&pcx, fin, sizeof(pcx));
107 // LordHavoc: big-endian support ported from QF newtree
108 pcx.xmax = LittleShort (pcx.xmax);
109 pcx.xmin = LittleShort (pcx.xmin);
110 pcx.ymax = LittleShort (pcx.ymax);
111 pcx.ymin = LittleShort (pcx.ymin);
112 pcx.hres = LittleShort (pcx.hres);
113 pcx.vres = LittleShort (pcx.vres);
114 pcx.bytes_per_line = LittleShort (pcx.bytes_per_line);
115 pcx.palette_type = LittleShort (pcx.palette_type);
117 if (pcx.manufacturer != 0x0a || pcx.version != 5 || pcx.encoding != 1 || pcx.bits_per_pixel != 8 || pcx.xmax > 320 || pcx.ymax > 256)
119 Con_Printf ("Bad pcx file\n");
123 if (matchwidth && (pcx.xmax+1) != matchwidth)
127 if (matchheight && (pcx.ymax+1) != matchheight)
132 image_width = pcx.xmax+1;
133 image_height = pcx.ymax+1;
135 palette = f + loadsize - 768;
137 image_rgba = Mem_Alloc(tempmempool, image_width*image_height*4);
140 Con_Printf("LoadPCX: not enough memory for %i by %i image\n", image_width, image_height);
143 pbuf = image_rgba + image_width*image_height*3;
146 for (y = 0;y < image_height && fin < enddata;y++)
148 a = pbuf + y * image_width;
149 for (x = 0;x < image_width && fin < enddata;)
156 x2 = x + (dataByte & 0x3F);
158 if (x2 > image_width)
159 x2 = image_width; // technically an error
166 fin += pcx.bytes_per_line - image_width; // the number of bytes per line is always forced to an even number
167 while(x < image_width)
174 for(x = 0;x < image_width*image_height;x++)
187 =========================================================
191 =========================================================
194 typedef struct _TargaHeader
196 unsigned char id_length, colormap_type, image_type;
197 unsigned short colormap_index, colormap_length;
198 unsigned char colormap_size;
199 unsigned short x_origin, y_origin, width, height;
200 unsigned char pixel_size, attributes;
204 void PrintTargaHeader(TargaHeader *t)
206 Con_Printf("TargaHeader:\n");
207 Con_Printf("uint8 id_length = %i;\n", t->id_length);
208 Con_Printf("uint8 colormap_type = %i;\n", t->colormap_type);
209 Con_Printf("uint8 image_type = %i;\n", t->image_type);
210 Con_Printf("uint16 colormap_index = %i;\n", t->colormap_index);
211 Con_Printf("uint16 colormap_length = %i;\n", t->colormap_length);
212 Con_Printf("uint8 colormap_size = %i;\n", t->colormap_size);
213 Con_Printf("uint16 x_origin = %i;\n", t->x_origin);
214 Con_Printf("uint16 y_origin = %i;\n", t->y_origin);
215 Con_Printf("uint16 width = %i;\n", t->width);
216 Con_Printf("uint16 height = %i;\n", t->height);
217 Con_Printf("uint8 pixel_size = %i;\n", t->pixel_size);
218 Con_Printf("uint8 attributes = %i;\n", t->attributes);
226 qbyte *LoadTGA (const qbyte *f, int matchwidth, int matchheight)
228 int x, y, row_inc, compressed, readpixelcount, red, green, blue, alpha, runlen;
229 qbyte *pixbuf, *image_rgba;
230 const qbyte *fin, *enddata;
231 TargaHeader targa_header;
232 unsigned char palette[256*4], *p;
237 enddata = f + loadsize;
239 targa_header.id_length = f[0];
240 targa_header.colormap_type = f[1];
241 targa_header.image_type = f[2];
243 targa_header.colormap_index = f[3] + f[4] * 256;
244 targa_header.colormap_length = f[5] + f[6] * 256;
245 targa_header.colormap_size = f[7];
246 targa_header.x_origin = f[8] + f[9] * 256;
247 targa_header.y_origin = f[10] + f[11] * 256;
248 targa_header.width = f[12] + f[13] * 256;
249 targa_header.height = f[14] + f[15] * 256;
250 if (matchwidth && targa_header.width != matchwidth)
252 if (matchheight && targa_header.height != matchheight)
254 targa_header.pixel_size = f[16];
255 targa_header.attributes = f[17];
257 image_width = targa_header.width;
258 image_height = targa_header.height;
261 if (targa_header.id_length != 0)
262 fin += targa_header.id_length; // skip TARGA image comment
263 if (targa_header.image_type == 2 || targa_header.image_type == 10)
265 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
267 Con_Printf ("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
268 PrintTargaHeader(&targa_header);
272 else if (targa_header.image_type == 1 || targa_header.image_type == 9)
274 if (targa_header.pixel_size != 8)
276 Con_Printf ("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
277 PrintTargaHeader(&targa_header);
280 if (targa_header.colormap_length != 256)
282 Con_Printf ("LoadTGA: only 256 colormap_length supported\n");
283 PrintTargaHeader(&targa_header);
286 if (targa_header.colormap_index)
288 Con_Printf ("LoadTGA: colormap_index not supported\n");
289 PrintTargaHeader(&targa_header);
292 if (targa_header.colormap_size == 24)
294 for (x = 0;x < targa_header.colormap_length;x++)
296 palette[x*4+2] = *fin++;
297 palette[x*4+1] = *fin++;
298 palette[x*4+0] = *fin++;
299 palette[x*4+3] = 255;
302 else if (targa_header.colormap_size == 32)
304 for (x = 0;x < targa_header.colormap_length;x++)
306 palette[x*4+2] = *fin++;
307 palette[x*4+1] = *fin++;
308 palette[x*4+0] = *fin++;
309 palette[x*4+3] = *fin++;
314 Con_Printf ("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
315 PrintTargaHeader(&targa_header);
319 else if (targa_header.image_type == 3 || targa_header.image_type == 11)
321 if (targa_header.pixel_size != 8)
323 Con_Printf ("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
324 PrintTargaHeader(&targa_header);
330 Con_Printf ("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
331 PrintTargaHeader(&targa_header);
335 if (targa_header.attributes & 0x10)
337 Con_Printf ("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
341 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
344 Con_Printf ("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
348 // If bit 5 of attributes isn't set, the image has been stored from bottom to top
349 if ((targa_header.attributes & 0x20) == 0)
351 pixbuf = image_rgba + (image_height - 1)*image_width*4;
352 row_inc = -image_width*4*2;
360 compressed = targa_header.image_type == 9 || targa_header.image_type == 10 || targa_header.image_type == 11;
363 red = green = blue = alpha = 255;
364 while (y < image_height)
366 // decoder is mostly the same whether it's compressed or not
367 readpixelcount = 1000000;
369 if (compressed && fin < enddata)
372 // high bit indicates this is an RLE compressed run
375 runlen = 1 + (runlen & 0x7f);
378 while((runlen--) && y < image_height)
380 if (readpixelcount > 0)
383 red = green = blue = alpha = 255;
386 switch(targa_header.image_type)
391 p = palette + (*fin++) * 4;
405 if (targa_header.pixel_size == 32 && fin < enddata)
411 red = green = blue = *fin++;
421 if (x == image_width)
423 // end of line, advance to next
439 qbyte *LoadLMP (const qbyte *f, int matchwidth, int matchheight)
446 Con_Printf("LoadLMP: invalid LMP file\n");
450 // parse the very complicated header *chuckle*
451 width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
452 height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
453 if ((unsigned) width > 4096 || (unsigned) height > 4096)
455 Con_Printf("LoadLMP: invalid size\n");
458 if ((matchwidth && width != matchwidth) || (matchheight && height != matchheight))
461 if (loadsize < 8 + width * height)
463 Con_Printf("LoadLMP: invalid LMP file\n");
468 image_height = height;
470 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
473 Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
476 Image_Copy8bitRGBA(f + 8, image_rgba, image_width * image_height, palette_complete);
485 qbyte *LoadLMPAs8Bit (const qbyte *f, int matchwidth, int matchheight)
492 Con_Printf("LoadLMPAs8Bit: invalid LMP file\n");
496 // parse the very complicated header *chuckle*
497 width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
498 height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
499 if ((unsigned) width > 4096 || (unsigned) height > 4096)
501 Con_Printf("LoadLMPAs8Bit: invalid size\n");
504 if ((matchwidth && width != matchwidth) || (matchheight && height != matchheight))
507 if (loadsize < 8 + width * height)
509 Con_Printf("LoadLMPAs8Bit: invalid LMP file\n");
514 image_height = height;
516 image_8bit = Mem_Alloc(tempmempool, image_width * image_height);
519 Con_Printf("LoadLMPAs8Bit: not enough memory for %i by %i image\n", image_width, image_height);
522 memcpy(image_8bit, f + 8, image_width * image_height);
526 void Image_StripImageExtension (const char *in, char *out)
528 const char *end, *temp;
529 end = in + strlen(in);
533 if (strcmp(temp, ".tga") == 0 || strcmp(temp, ".pcx") == 0 || strcmp(temp, ".lmp") == 0)
543 qbyte *loadimagepixels (const char *filename, qboolean complain, int matchwidth, int matchheight)
546 char basename[MAX_QPATH], name[MAX_QPATH], *c;
547 Image_StripImageExtension(filename, basename); // strip filename extensions to allow replacement by other types
548 // replace *'s with #, so commandline utils don't get confused when dealing with the external files
549 for (c = basename;*c;c++)
552 sprintf (name, "override/%s.tga", basename);
553 f = COM_LoadFile(name, true);
556 data = LoadTGA (f, matchwidth, matchheight);
560 sprintf (name, "override/%s.jpg", basename);
561 f = COM_LoadFile(name, true);
564 data = JPEG_LoadImage (f, matchwidth, matchheight);
568 sprintf (name, "textures/%s.tga", basename);
569 f = COM_LoadFile(name, true);
572 data = LoadTGA (f, matchwidth, matchheight);
576 sprintf (name, "textures/%s.jpg", basename);
577 f = COM_LoadFile(name, true);
580 data = JPEG_LoadImage (f, matchwidth, matchheight);
584 sprintf (name, "textures/%s.pcx", basename);
585 f = COM_LoadFile(name, true);
588 data = LoadPCX (f, matchwidth, matchheight);
592 sprintf (name, "%s.tga", basename);
593 f = COM_LoadFile(name, true);
596 data = LoadTGA (f, matchwidth, matchheight);
600 sprintf (name, "%s.jpg", basename);
601 f = COM_LoadFile(name, true);
604 data = JPEG_LoadImage (f, matchwidth, matchheight);
608 sprintf (name, "%s.pcx", basename);
609 f = COM_LoadFile(name, true);
612 data = LoadPCX (f, matchwidth, matchheight);
616 sprintf (name, "%s.lmp", basename);
617 f = COM_LoadFile(name, true);
620 data = LoadLMP (f, matchwidth, matchheight);
625 Con_Printf ("Couldn't load %s.tga, .jpg, .pcx, .lmp\n", filename);
629 int image_makemask (const qbyte *in, qbyte *out, int size)
633 for (i = 0;i < size;i++)
635 out[0] = out[1] = out[2] = 255;
645 qbyte* loadimagepixelsmask (const char *filename, qboolean complain, int matchwidth, int matchheight)
648 in = data = loadimagepixels(filename, complain, matchwidth, matchheight);
651 if (image_makemask(data, data, image_width * image_height))
652 return data; // some transparency
656 return NULL; // all opaque
660 rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
664 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
666 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
671 rtexture_t *loadtextureimagemask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
675 if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
677 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
682 rtexture_t *image_masktex;
683 rtexture_t *image_nmaptex;
684 rtexture_t *loadtextureimagewithmask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
688 image_masktex = NULL;
689 image_nmaptex = NULL;
690 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
693 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
695 if (flags & TEXF_ALPHA && image_makemask(data, data, image_width * image_height))
696 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
702 rtexture_t *loadtextureimagewithmaskandnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
706 image_masktex = NULL;
707 image_nmaptex = NULL;
708 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
711 data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
713 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
715 Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
716 image_nmaptex = R_LoadTexture2D(pool, va("%s_nmap", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
718 if (flags & TEXF_ALPHA && image_makemask(data, data2, image_width * image_height))
719 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
727 rtexture_t *loadtextureimagebumpasnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
731 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
733 data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
735 Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
736 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
743 qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int height, const qbyte *data)
747 const qbyte *in, *end;
749 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
751 memset (buffer, 0, 18);
752 buffer[2] = 2; // uncompressed type
753 buffer[12] = (width >> 0) & 0xFF;
754 buffer[13] = (width >> 8) & 0xFF;
755 buffer[14] = (height >> 0) & 0xFF;
756 buffer[15] = (height >> 8) & 0xFF;
757 buffer[16] = 24; // pixel size
762 end = in + width*height*3;
763 for (;in < end;in += 3)
769 ret = COM_WriteFile (filename, buffer, width*height*3 + 18 );
775 void Image_WriteTGARGB (const char *filename, int width, int height, const qbyte *data)
779 const qbyte *in, *end;
781 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
783 memset (buffer, 0, 18);
784 buffer[2] = 2; // uncompressed type
785 buffer[12] = (width >> 0) & 0xFF;
786 buffer[13] = (width >> 8) & 0xFF;
787 buffer[14] = (height >> 0) & 0xFF;
788 buffer[15] = (height >> 8) & 0xFF;
789 buffer[16] = 24; // pixel size
791 // swap rgb to bgr and flip upside down
793 for (y = height - 1;y >= 0;y--)
795 in = data + y * width * 3;
796 end = in + width * 3;
797 for (;in < end;in += 3)
804 COM_WriteFile (filename, buffer, width*height*3 + 18 );
809 void Image_WriteTGARGBA (const char *filename, int width, int height, const qbyte *data)
813 const qbyte *in, *end;
815 buffer = Mem_Alloc(tempmempool, width*height*4 + 18);
817 memset (buffer, 0, 18);
818 buffer[2] = 2; // uncompressed type
819 buffer[12] = (width >> 0) & 0xFF;
820 buffer[13] = (width >> 8) & 0xFF;
821 buffer[14] = (height >> 0) & 0xFF;
822 buffer[15] = (height >> 8) & 0xFF;
823 buffer[16] = 32; // pixel size
825 // swap rgba to bgra and flip upside down
827 for (y = height - 1;y >= 0;y--)
829 in = data + y * width * 4;
830 end = in + width * 4;
831 for (;in < end;in += 4)
839 COM_WriteFile (filename, buffer, width*height*4 + 18 );
844 qboolean Image_CheckAlpha(const qbyte *data, int size, qboolean rgba)
850 for (end = data + size * 4, data += 3;data < end;data += 4)
856 // color 255 is transparent
857 for (end = data + size;data < end;data++)
864 static void Image_Resample32LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
866 int j, xi, oldx = 0, f, fstep, endx, lerp;
867 fstep = (int) (inwidth*65536.0f/outwidth);
869 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
874 in += (xi - oldx) * 4;
880 *out++ = (qbyte) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
881 *out++ = (qbyte) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
882 *out++ = (qbyte) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
883 *out++ = (qbyte) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
885 else // last pixel of the line has no pixel to lerp to
895 static void Image_Resample24LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
897 int j, xi, oldx = 0, f, fstep, endx, lerp;
898 fstep = (int) (inwidth*65536.0f/outwidth);
900 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
905 in += (xi - oldx) * 3;
911 *out++ = (qbyte) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
912 *out++ = (qbyte) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
913 *out++ = (qbyte) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
915 else // last pixel of the line has no pixel to lerp to
924 int resamplerowsize = 0;
925 qbyte *resamplerow1 = NULL;
926 qbyte *resamplerow2 = NULL;
927 mempool_t *resamplemempool = NULL;
929 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (qbyte) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
930 void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
932 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
936 fstep = (int) (inheight*65536.0f/outheight);
940 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
941 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
942 for (i = 0, f = 0;i < outheight;i++,f += fstep)
950 inrow = (qbyte *)indata + inwidth4*yi;
952 memcpy(resamplerow1, resamplerow2, outwidth4);
954 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
955 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1006 resamplerow1 -= outwidth4;
1007 resamplerow2 -= outwidth4;
1013 inrow = (qbyte *)indata + inwidth4*yi;
1015 memcpy(resamplerow1, resamplerow2, outwidth4);
1017 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1020 memcpy(out, resamplerow1, outwidth4);
1025 void Image_Resample32Nearest(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1028 unsigned frac, fracstep;
1029 // relies on int being 4 bytes
1033 fracstep = inwidth*0x10000/outwidth;
1034 for (i = 0;i < outheight;i++)
1036 inrow = (int *)indata + inwidth*(i*inheight/outheight);
1037 frac = fracstep >> 1;
1041 out[0] = inrow[frac >> 16];frac += fracstep;
1042 out[1] = inrow[frac >> 16];frac += fracstep;
1043 out[2] = inrow[frac >> 16];frac += fracstep;
1044 out[3] = inrow[frac >> 16];frac += fracstep;
1050 out[0] = inrow[frac >> 16];frac += fracstep;
1051 out[1] = inrow[frac >> 16];frac += fracstep;
1056 out[0] = inrow[frac >> 16];frac += fracstep;
1062 void Image_Resample24Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1064 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
1068 fstep = (int) (inheight*65536.0f/outheight);
1072 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1073 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1074 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1082 inrow = (qbyte *)indata + inwidth3*yi;
1084 memcpy(resamplerow1, resamplerow2, outwidth3);
1086 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1087 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1131 resamplerow1 -= outwidth3;
1132 resamplerow2 -= outwidth3;
1138 inrow = (qbyte *)indata + inwidth3*yi;
1140 memcpy(resamplerow1, resamplerow2, outwidth3);
1142 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1145 memcpy(out, resamplerow1, outwidth3);
1150 void Image_Resample24Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1152 int i, j, f, inwidth3 = inwidth * 3;
1153 unsigned frac, fracstep;
1157 fracstep = inwidth*0x10000/outwidth;
1158 for (i = 0;i < outheight;i++)
1160 inrow = (qbyte *)indata + inwidth3*(i*inheight/outheight);
1161 frac = fracstep >> 1;
1165 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1166 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1167 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1168 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1173 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1174 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1179 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1190 void Image_Resample (const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int bytesperpixel, int quality)
1192 if (indepth != 1 || outdepth != 1)
1193 Sys_Error("Image_Resample: 3D resampling not supported\n");
1194 if (resamplerowsize < outwidth*4)
1197 Mem_Free(resamplerow1);
1198 resamplerowsize = outwidth*4;
1199 if (!resamplemempool)
1200 resamplemempool = Mem_AllocPool("Image Scaling Buffer");
1201 resamplerow1 = Mem_Alloc(resamplemempool, resamplerowsize*2);
1202 resamplerow2 = resamplerow1 + resamplerowsize;
1204 if (bytesperpixel == 4)
1207 Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1209 Image_Resample32Nearest(indata, inwidth, inheight, outdata, outwidth, outheight);
1211 else if (bytesperpixel == 3)
1214 Image_Resample24Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1216 Image_Resample24Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1219 Sys_Error("Image_Resample: unsupported bytesperpixel %i\n", bytesperpixel);
1222 // in can be the same as out
1223 void Image_MipReduce(const qbyte *in, qbyte *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth, int bytesperpixel)
1226 if (*depth != 1 || destdepth != 1)
1227 Sys_Error("Image_Resample: 3D resampling not supported\n");
1228 nextrow = *width * bytesperpixel;
1229 if (*width > destwidth)
1232 if (*height > destheight)
1236 if (bytesperpixel == 4)
1238 for (y = 0;y < *height;y++)
1240 for (x = 0;x < *width;x++)
1242 out[0] = (qbyte) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
1243 out[1] = (qbyte) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1244 out[2] = (qbyte) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1245 out[3] = (qbyte) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1249 in += nextrow; // skip a line
1252 else if (bytesperpixel == 3)
1254 for (y = 0;y < *height;y++)
1256 for (x = 0;x < *width;x++)
1258 out[0] = (qbyte) ((in[0] + in[3] + in[nextrow ] + in[nextrow+3]) >> 2);
1259 out[1] = (qbyte) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
1260 out[2] = (qbyte) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
1264 in += nextrow; // skip a line
1268 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1273 if (bytesperpixel == 4)
1275 for (y = 0;y < *height;y++)
1277 for (x = 0;x < *width;x++)
1279 out[0] = (qbyte) ((in[0] + in[4]) >> 1);
1280 out[1] = (qbyte) ((in[1] + in[5]) >> 1);
1281 out[2] = (qbyte) ((in[2] + in[6]) >> 1);
1282 out[3] = (qbyte) ((in[3] + in[7]) >> 1);
1288 else if (bytesperpixel == 3)
1290 for (y = 0;y < *height;y++)
1292 for (x = 0;x < *width;x++)
1294 out[0] = (qbyte) ((in[0] + in[3]) >> 1);
1295 out[1] = (qbyte) ((in[1] + in[4]) >> 1);
1296 out[2] = (qbyte) ((in[2] + in[5]) >> 1);
1303 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1308 if (*height > destheight)
1312 if (bytesperpixel == 4)
1314 for (y = 0;y < *height;y++)
1316 for (x = 0;x < *width;x++)
1318 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1319 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1320 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1321 out[3] = (qbyte) ((in[3] + in[nextrow+3]) >> 1);
1325 in += nextrow; // skip a line
1328 else if (bytesperpixel == 3)
1330 for (y = 0;y < *height;y++)
1332 for (x = 0;x < *width;x++)
1334 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1335 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1336 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1340 in += nextrow; // skip a line
1344 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1347 Sys_Error("Image_MipReduce: desired size already achieved\n");
1351 void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
1354 const unsigned char *p0, *p1, *p2;
1356 float iwidth, iheight, ibumpscale, n[3];
1357 iwidth = 1.0f / width;
1358 iheight = 1.0f / height;
1359 ibumpscale = (255.0f * 3.0f) / bumpscale;
1361 for (y = 0;y < height;y++)
1363 for (x = 0;x < width;x++)
1365 p0 = inpixels + (y * width + x) * 4;
1369 p1 = inpixels + (y * width + x) * 4;
1371 p1 = inpixels + (y * width) * 4;
1374 p1 = inpixels + (y * width + x + 1) * 4;
1375 if (y == height - 1)
1378 p2 = inpixels + (y * width + x) * 4;
1380 p2 = inpixels + x * 4;
1383 p2 = inpixels + ((y + 1) * width + x) * 4;
1387 dv[0][2] = ((p1[0] + p1[1] + p1[2]) * ibumpscale) - ((p0[0] + p0[1] + p0[2]) * ibumpscale);
1390 dv[1][2] = ((p2[0] + p2[1] + p2[2]) * ibumpscale) - ((p0[0] + p0[1] + p0[2]) * ibumpscale);
1391 n[0] = dv[0][1]*dv[1][2]-dv[0][2]*dv[1][1];
1392 n[1] = dv[0][2]*dv[1][0]-dv[0][0]*dv[1][2];
1393 n[2] = dv[0][0]*dv[1][1]-dv[0][1]*dv[1][0];
1395 n[0] = ((p0[0] + p0[1] + p0[2]) - (p1[0] + p1[1] + p1[2]));
1396 n[1] = ((p0[0] + p0[1] + p0[2]) - (p2[0] + p2[1] + p2[2]));
1399 out[0] = 128.0f + n[0] * 127.0f;
1400 out[1] = 128.0f + n[1] * 127.0f;
1401 out[2] = 128.0f + n[2] * 127.0f;
1408 int image_loadskin(imageskin_t *s, char *name)
1412 int bumppixels_width, bumppixels_height;
1413 memset(s, 0, sizeof(*s));
1414 s->basepixels = loadimagepixels(name, false, 0, 0);
1415 if (s->basepixels == NULL)
1418 bumppixels = NULL;bumppixels_width = 0;bumppixels_height = 0;
1419 for (j = 3;j < s->basepixels_width * s->basepixels_height * 4;j += 4)
1420 if (s->basepixels[j] < 255)
1422 if (j < s->basepixels_width * s->basepixels_height * 4)
1424 s->maskpixels = Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1425 s->maskpixels_width = s->basepixels_width;
1426 s->maskpixels_height = s->basepixels_height;
1427 memcpy(s->maskpixels, s->basepixels, s->maskpixels_width * s->maskpixels_height * 4);
1428 for (j = 0;j < s->basepixels_width * s->basepixels_height * 4;j += 4)
1430 s->maskpixels[j+0] = 255;
1431 s->maskpixels[j+1] = 255;
1432 s->maskpixels[j+2] = 255;
1436 // _luma is supported for tenebrae compatibility
1437 // (I think it's a very stupid name, but oh well)
1438 if ((s->glowpixels = loadimagepixels(va("%s_glow", name), false, 0, 0)) != NULL
1439 || (s->glowpixels = loadimagepixels(va("%s_luma", name), false, 0, 0)) != NULL)
1441 s->glowpixels_width = image_width;
1442 s->glowpixels_height = image_height;
1444 // _norm is the name used by tenebrae
1445 // (I don't like the name much)
1446 if ((s->nmappixels = loadimagepixels(va("%s_norm", name), false, 0, 0)) != NULL)
1448 s->nmappixels_width = image_width;
1449 s->nmappixels_height = image_height;
1451 else if ((bumppixels = loadimagepixels(va("%s_bump", name), false, 0, 0)) != NULL)
1453 bumppixels_width = image_width;
1454 bumppixels_height = image_height;
1456 if ((s->glosspixels = loadimagepixels(va("%s_gloss", name), false, 0, 0)) != NULL)
1458 s->glosspixels_width = image_width;
1459 s->glosspixels_height = image_height;
1461 if ((s->pantspixels = loadimagepixels(va("%s_pants", name), false, 0, 0)) != NULL)
1463 s->pantspixels_width = image_width;
1464 s->pantspixels_height = image_height;
1466 if ((s->shirtpixels = loadimagepixels(va("%s_shirt", name), false, 0, 0)) != NULL)
1468 s->shirtpixels_width = image_width;
1469 s->shirtpixels_height = image_height;
1472 if (s->nmappixels == NULL)
1474 if (bumppixels != NULL)
1476 if (r_shadow_bumpscale_bumpmap.value > 0)
1478 s->nmappixels = Mem_Alloc(loadmodel->mempool, bumppixels_width * bumppixels_height * 4);
1479 s->nmappixels_width = bumppixels_width;
1480 s->nmappixels_height = bumppixels_height;
1481 Image_HeightmapToNormalmap(bumppixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_bumpmap.value);
1486 if (r_shadow_bumpscale_basetexture.value > 0)
1488 s->nmappixels = Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1489 s->nmappixels_width = s->basepixels_width;
1490 s->nmappixels_height = s->basepixels_height;
1491 Image_HeightmapToNormalmap(s->basepixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_basetexture.value);
1495 if (bumppixels != NULL)
1496 Mem_Free(bumppixels);
1500 void image_freeskin(imageskin_t *s)
1503 Mem_Free(s->basepixels);
1505 Mem_Free(s->nmappixels);
1507 Mem_Free(s->glowpixels);
1509 Mem_Free(s->glosspixels);
1511 Mem_Free(s->pantspixels);
1513 Mem_Free(s->shirtpixels);
1514 memset(s, 0, sizeof(*s));