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);
559 sprintf (name, "override/%s.jpg", basename);
560 f = COM_LoadFile(name, true);
563 data = JPEG_LoadImage (f, matchwidth, matchheight);
566 sprintf (name, "textures/%s.tga", basename);
567 f = COM_LoadFile(name, true);
570 data = LoadTGA (f, matchwidth, matchheight);
573 sprintf (name, "textures/%s.jpg", basename);
574 f = COM_LoadFile(name, true);
577 data = JPEG_LoadImage (f, matchwidth, matchheight);
580 sprintf (name, "textures/%s.pcx", basename);
581 f = COM_LoadFile(name, true);
584 data = LoadPCX (f, matchwidth, matchheight);
587 sprintf (name, "%s.tga", basename);
588 f = COM_LoadFile(name, true);
591 data = LoadTGA (f, matchwidth, matchheight);
594 sprintf (name, "%s.jpg", basename);
595 f = COM_LoadFile(name, true);
598 data = JPEG_LoadImage (f, matchwidth, matchheight);
601 sprintf (name, "%s.pcx", basename);
602 f = COM_LoadFile(name, true);
605 data = LoadPCX (f, matchwidth, matchheight);
608 sprintf (name, "%s.lmp", basename);
609 f = COM_LoadFile(name, true);
612 data = LoadLMP (f, matchwidth, matchheight);
616 Con_Printf ("Couldn't load %s.tga, .jpg, .pcx, .lmp\n", filename);
620 Con_DPrintf("loaded image %s (%dx%d)\n", name, image_width, image_height);
621 if (image_width == 0 || image_height == 0)
623 Con_Printf("error loading image %s - it is a %dx%d pixel image!\n", name);
631 int image_makemask (const qbyte *in, qbyte *out, int size)
635 for (i = 0;i < size;i++)
637 out[0] = out[1] = out[2] = 255;
647 qbyte* loadimagepixelsmask (const char *filename, qboolean complain, int matchwidth, int matchheight)
650 in = data = loadimagepixels(filename, complain, matchwidth, matchheight);
653 if (image_makemask(data, data, image_width * image_height))
654 return data; // some transparency
658 return NULL; // all opaque
662 rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
666 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
668 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
673 rtexture_t *loadtextureimagemask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
677 if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
679 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
684 rtexture_t *image_masktex;
685 rtexture_t *image_nmaptex;
686 rtexture_t *loadtextureimagewithmask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
690 image_masktex = NULL;
691 image_nmaptex = NULL;
692 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
695 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
697 if (flags & TEXF_ALPHA && image_makemask(data, data, image_width * image_height))
698 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
704 rtexture_t *loadtextureimagewithmaskandnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
708 image_masktex = NULL;
709 image_nmaptex = NULL;
710 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
713 data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
715 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
717 Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
718 image_nmaptex = R_LoadTexture2D(pool, va("%s_nmap", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
720 if (flags & TEXF_ALPHA && image_makemask(data, data2, image_width * image_height))
721 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
729 rtexture_t *loadtextureimagebumpasnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
733 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
735 data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
737 Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
738 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
745 qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int height, const qbyte *data)
749 const qbyte *in, *end;
751 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
753 memset (buffer, 0, 18);
754 buffer[2] = 2; // uncompressed type
755 buffer[12] = (width >> 0) & 0xFF;
756 buffer[13] = (width >> 8) & 0xFF;
757 buffer[14] = (height >> 0) & 0xFF;
758 buffer[15] = (height >> 8) & 0xFF;
759 buffer[16] = 24; // pixel size
764 end = in + width*height*3;
765 for (;in < end;in += 3)
771 ret = COM_WriteFile (filename, buffer, width*height*3 + 18 );
777 void Image_WriteTGARGB (const char *filename, int width, int height, const qbyte *data)
781 const qbyte *in, *end;
783 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
785 memset (buffer, 0, 18);
786 buffer[2] = 2; // uncompressed type
787 buffer[12] = (width >> 0) & 0xFF;
788 buffer[13] = (width >> 8) & 0xFF;
789 buffer[14] = (height >> 0) & 0xFF;
790 buffer[15] = (height >> 8) & 0xFF;
791 buffer[16] = 24; // pixel size
793 // swap rgb to bgr and flip upside down
795 for (y = height - 1;y >= 0;y--)
797 in = data + y * width * 3;
798 end = in + width * 3;
799 for (;in < end;in += 3)
806 COM_WriteFile (filename, buffer, width*height*3 + 18 );
811 void Image_WriteTGARGBA (const char *filename, int width, int height, const qbyte *data)
815 const qbyte *in, *end;
817 buffer = Mem_Alloc(tempmempool, width*height*4 + 18);
819 memset (buffer, 0, 18);
820 buffer[2] = 2; // uncompressed type
821 buffer[12] = (width >> 0) & 0xFF;
822 buffer[13] = (width >> 8) & 0xFF;
823 buffer[14] = (height >> 0) & 0xFF;
824 buffer[15] = (height >> 8) & 0xFF;
825 buffer[16] = 32; // pixel size
827 // swap rgba to bgra and flip upside down
829 for (y = height - 1;y >= 0;y--)
831 in = data + y * width * 4;
832 end = in + width * 4;
833 for (;in < end;in += 4)
841 COM_WriteFile (filename, buffer, width*height*4 + 18 );
846 qboolean Image_CheckAlpha(const qbyte *data, int size, qboolean rgba)
852 for (end = data + size * 4, data += 3;data < end;data += 4)
858 // color 255 is transparent
859 for (end = data + size;data < end;data++)
866 static void Image_Resample32LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
868 int j, xi, oldx = 0, f, fstep, endx, lerp;
869 fstep = (int) (inwidth*65536.0f/outwidth);
871 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
876 in += (xi - oldx) * 4;
882 *out++ = (qbyte) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
883 *out++ = (qbyte) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
884 *out++ = (qbyte) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
885 *out++ = (qbyte) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
887 else // last pixel of the line has no pixel to lerp to
897 static void Image_Resample24LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
899 int j, xi, oldx = 0, f, fstep, endx, lerp;
900 fstep = (int) (inwidth*65536.0f/outwidth);
902 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
907 in += (xi - oldx) * 3;
913 *out++ = (qbyte) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
914 *out++ = (qbyte) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
915 *out++ = (qbyte) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
917 else // last pixel of the line has no pixel to lerp to
926 int resamplerowsize = 0;
927 qbyte *resamplerow1 = NULL;
928 qbyte *resamplerow2 = NULL;
929 mempool_t *resamplemempool = NULL;
931 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (qbyte) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
932 void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
934 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
938 fstep = (int) (inheight*65536.0f/outheight);
942 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
943 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
944 for (i = 0, f = 0;i < outheight;i++,f += fstep)
952 inrow = (qbyte *)indata + inwidth4*yi;
954 memcpy(resamplerow1, resamplerow2, outwidth4);
956 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
957 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1008 resamplerow1 -= outwidth4;
1009 resamplerow2 -= outwidth4;
1015 inrow = (qbyte *)indata + inwidth4*yi;
1017 memcpy(resamplerow1, resamplerow2, outwidth4);
1019 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1022 memcpy(out, resamplerow1, outwidth4);
1027 void Image_Resample32Nearest(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1030 unsigned frac, fracstep;
1031 // relies on int being 4 bytes
1035 fracstep = inwidth*0x10000/outwidth;
1036 for (i = 0;i < outheight;i++)
1038 inrow = (int *)indata + inwidth*(i*inheight/outheight);
1039 frac = fracstep >> 1;
1043 out[0] = inrow[frac >> 16];frac += fracstep;
1044 out[1] = inrow[frac >> 16];frac += fracstep;
1045 out[2] = inrow[frac >> 16];frac += fracstep;
1046 out[3] = inrow[frac >> 16];frac += fracstep;
1052 out[0] = inrow[frac >> 16];frac += fracstep;
1053 out[1] = inrow[frac >> 16];frac += fracstep;
1058 out[0] = inrow[frac >> 16];frac += fracstep;
1064 void Image_Resample24Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1066 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
1070 fstep = (int) (inheight*65536.0f/outheight);
1074 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1075 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1076 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1084 inrow = (qbyte *)indata + inwidth3*yi;
1086 memcpy(resamplerow1, resamplerow2, outwidth3);
1088 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1089 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1133 resamplerow1 -= outwidth3;
1134 resamplerow2 -= outwidth3;
1140 inrow = (qbyte *)indata + inwidth3*yi;
1142 memcpy(resamplerow1, resamplerow2, outwidth3);
1144 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1147 memcpy(out, resamplerow1, outwidth3);
1152 void Image_Resample24Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1154 int i, j, f, inwidth3 = inwidth * 3;
1155 unsigned frac, fracstep;
1159 fracstep = inwidth*0x10000/outwidth;
1160 for (i = 0;i < outheight;i++)
1162 inrow = (qbyte *)indata + inwidth3*(i*inheight/outheight);
1163 frac = fracstep >> 1;
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;
1169 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1170 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1175 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1176 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1181 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1192 void Image_Resample (const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int bytesperpixel, int quality)
1194 if (indepth != 1 || outdepth != 1)
1195 Sys_Error("Image_Resample: 3D resampling not supported\n");
1196 if (resamplerowsize < outwidth*4)
1199 Mem_Free(resamplerow1);
1200 resamplerowsize = outwidth*4;
1201 if (!resamplemempool)
1202 resamplemempool = Mem_AllocPool("Image Scaling Buffer");
1203 resamplerow1 = Mem_Alloc(resamplemempool, resamplerowsize*2);
1204 resamplerow2 = resamplerow1 + resamplerowsize;
1206 if (bytesperpixel == 4)
1209 Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1211 Image_Resample32Nearest(indata, inwidth, inheight, outdata, outwidth, outheight);
1213 else if (bytesperpixel == 3)
1216 Image_Resample24Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1218 Image_Resample24Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1221 Sys_Error("Image_Resample: unsupported bytesperpixel %i\n", bytesperpixel);
1224 // in can be the same as out
1225 void Image_MipReduce(const qbyte *in, qbyte *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth, int bytesperpixel)
1228 if (*depth != 1 || destdepth != 1)
1229 Sys_Error("Image_Resample: 3D resampling not supported\n");
1230 nextrow = *width * bytesperpixel;
1231 if (*width > destwidth)
1234 if (*height > destheight)
1238 if (bytesperpixel == 4)
1240 for (y = 0;y < *height;y++)
1242 for (x = 0;x < *width;x++)
1244 out[0] = (qbyte) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
1245 out[1] = (qbyte) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1246 out[2] = (qbyte) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1247 out[3] = (qbyte) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1251 in += nextrow; // skip a line
1254 else if (bytesperpixel == 3)
1256 for (y = 0;y < *height;y++)
1258 for (x = 0;x < *width;x++)
1260 out[0] = (qbyte) ((in[0] + in[3] + in[nextrow ] + in[nextrow+3]) >> 2);
1261 out[1] = (qbyte) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
1262 out[2] = (qbyte) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
1266 in += nextrow; // skip a line
1270 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1275 if (bytesperpixel == 4)
1277 for (y = 0;y < *height;y++)
1279 for (x = 0;x < *width;x++)
1281 out[0] = (qbyte) ((in[0] + in[4]) >> 1);
1282 out[1] = (qbyte) ((in[1] + in[5]) >> 1);
1283 out[2] = (qbyte) ((in[2] + in[6]) >> 1);
1284 out[3] = (qbyte) ((in[3] + in[7]) >> 1);
1290 else if (bytesperpixel == 3)
1292 for (y = 0;y < *height;y++)
1294 for (x = 0;x < *width;x++)
1296 out[0] = (qbyte) ((in[0] + in[3]) >> 1);
1297 out[1] = (qbyte) ((in[1] + in[4]) >> 1);
1298 out[2] = (qbyte) ((in[2] + in[5]) >> 1);
1305 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1310 if (*height > destheight)
1314 if (bytesperpixel == 4)
1316 for (y = 0;y < *height;y++)
1318 for (x = 0;x < *width;x++)
1320 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1321 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1322 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1323 out[3] = (qbyte) ((in[3] + in[nextrow+3]) >> 1);
1327 in += nextrow; // skip a line
1330 else if (bytesperpixel == 3)
1332 for (y = 0;y < *height;y++)
1334 for (x = 0;x < *width;x++)
1336 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1337 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1338 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1342 in += nextrow; // skip a line
1346 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1349 Sys_Error("Image_MipReduce: desired size already achieved\n");
1353 void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
1356 const unsigned char *p0, *p1, *p2;
1358 float iwidth, iheight, ibumpscale, n[3];
1359 iwidth = 1.0f / width;
1360 iheight = 1.0f / height;
1361 ibumpscale = (255.0f * 3.0f) / bumpscale;
1363 for (y = 0;y < height;y++)
1365 for (x = 0;x < width;x++)
1367 p0 = inpixels + (y * width + x) * 4;
1371 p1 = inpixels + (y * width + x) * 4;
1373 p1 = inpixels + (y * width) * 4;
1376 p1 = inpixels + (y * width + x + 1) * 4;
1377 if (y == height - 1)
1380 p2 = inpixels + (y * width + x) * 4;
1382 p2 = inpixels + x * 4;
1385 p2 = inpixels + ((y + 1) * width + x) * 4;
1389 dv[0][2] = ((p1[0] + p1[1] + p1[2]) * ibumpscale) - ((p0[0] + p0[1] + p0[2]) * ibumpscale);
1392 dv[1][2] = ((p2[0] + p2[1] + p2[2]) * ibumpscale) - ((p0[0] + p0[1] + p0[2]) * ibumpscale);
1393 n[0] = dv[0][1]*dv[1][2]-dv[0][2]*dv[1][1];
1394 n[1] = dv[0][2]*dv[1][0]-dv[0][0]*dv[1][2];
1395 n[2] = dv[0][0]*dv[1][1]-dv[0][1]*dv[1][0];
1397 n[0] = ((p0[0] + p0[1] + p0[2]) - (p1[0] + p1[1] + p1[2]));
1398 n[1] = ((p0[0] + p0[1] + p0[2]) - (p2[0] + p2[1] + p2[2]));
1401 out[0] = 128.0f + n[0] * 127.0f;
1402 out[1] = 128.0f + n[1] * 127.0f;
1403 out[2] = 128.0f + n[2] * 127.0f;
1410 int image_loadskin(imageskin_t *s, char *name)
1414 int bumppixels_width, bumppixels_height;
1415 memset(s, 0, sizeof(*s));
1416 s->basepixels = loadimagepixels(name, false, 0, 0);
1417 if (s->basepixels == NULL)
1419 s->basepixels_width = image_width;
1420 s->basepixels_height = image_height;
1422 bumppixels = NULL;bumppixels_width = 0;bumppixels_height = 0;
1423 for (j = 3;j < s->basepixels_width * s->basepixels_height * 4;j += 4)
1424 if (s->basepixels[j] < 255)
1426 if (j < s->basepixels_width * s->basepixels_height * 4)
1428 s->maskpixels = Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1429 s->maskpixels_width = s->basepixels_width;
1430 s->maskpixels_height = s->basepixels_height;
1431 memcpy(s->maskpixels, s->basepixels, s->maskpixels_width * s->maskpixels_height * 4);
1432 for (j = 0;j < s->basepixels_width * s->basepixels_height * 4;j += 4)
1434 s->maskpixels[j+0] = 255;
1435 s->maskpixels[j+1] = 255;
1436 s->maskpixels[j+2] = 255;
1440 // _luma is supported for tenebrae compatibility
1441 // (I think it's a very stupid name, but oh well)
1442 if ((s->glowpixels = loadimagepixels(va("%s_glow", name), false, 0, 0)) != NULL
1443 || (s->glowpixels = loadimagepixels(va("%s_luma", name), false, 0, 0)) != NULL)
1445 s->glowpixels_width = image_width;
1446 s->glowpixels_height = image_height;
1448 // _norm is the name used by tenebrae
1449 // (I don't like the name much)
1450 if ((s->nmappixels = loadimagepixels(va("%s_norm", name), false, 0, 0)) != NULL)
1452 s->nmappixels_width = image_width;
1453 s->nmappixels_height = image_height;
1455 else if ((bumppixels = loadimagepixels(va("%s_bump", name), false, 0, 0)) != NULL)
1457 bumppixels_width = image_width;
1458 bumppixels_height = image_height;
1460 if ((s->glosspixels = loadimagepixels(va("%s_gloss", name), false, 0, 0)) != NULL)
1462 s->glosspixels_width = image_width;
1463 s->glosspixels_height = image_height;
1465 if ((s->pantspixels = loadimagepixels(va("%s_pants", name), false, 0, 0)) != NULL)
1467 s->pantspixels_width = image_width;
1468 s->pantspixels_height = image_height;
1470 if ((s->shirtpixels = loadimagepixels(va("%s_shirt", name), false, 0, 0)) != NULL)
1472 s->shirtpixels_width = image_width;
1473 s->shirtpixels_height = image_height;
1476 if (s->nmappixels == NULL)
1478 if (bumppixels != NULL)
1480 if (r_shadow_bumpscale_bumpmap.value > 0)
1482 s->nmappixels = Mem_Alloc(loadmodel->mempool, bumppixels_width * bumppixels_height * 4);
1483 s->nmappixels_width = bumppixels_width;
1484 s->nmappixels_height = bumppixels_height;
1485 Image_HeightmapToNormalmap(bumppixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_bumpmap.value);
1490 if (r_shadow_bumpscale_basetexture.value > 0)
1492 s->nmappixels = Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1493 s->nmappixels_width = s->basepixels_width;
1494 s->nmappixels_height = s->basepixels_height;
1495 Image_HeightmapToNormalmap(s->basepixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_basetexture.value);
1499 if (bumppixels != NULL)
1500 Mem_Free(bumppixels);
1504 void image_freeskin(imageskin_t *s)
1507 Mem_Free(s->basepixels);
1509 Mem_Free(s->nmappixels);
1511 Mem_Free(s->glowpixels);
1513 Mem_Free(s->glosspixels);
1515 Mem_Free(s->pantspixels);
1517 Mem_Free(s->shirtpixels);
1518 memset(s, 0, sizeof(*s));