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 (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 (fs_filesize < (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 image_width = pcx.xmax + 1 - pcx.xmin;
118 image_height = pcx.ymax + 1 - pcx.ymin;
119 if (pcx.manufacturer != 0x0a || pcx.version != 5 || pcx.encoding != 1 || pcx.bits_per_pixel != 8 || image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
121 Con_Printf ("Bad pcx file\n");
124 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
127 palette = f + fs_filesize - 768;
129 image_rgba = Mem_Alloc(tempmempool, image_width*image_height*4);
132 Con_Printf("LoadPCX: not enough memory for %i by %i image\n", image_width, image_height);
135 pbuf = image_rgba + image_width*image_height*3;
138 for (y = 0;y < image_height && fin < enddata;y++)
140 a = pbuf + y * image_width;
141 for (x = 0;x < image_width && fin < enddata;)
148 x2 = x + (dataByte & 0x3F);
150 if (x2 > image_width)
151 x2 = image_width; // technically an error
158 fin += pcx.bytes_per_line - image_width; // the number of bytes per line is always forced to an even number
159 while(x < image_width)
166 for(x = 0;x < image_width*image_height;x++)
179 =========================================================
183 =========================================================
186 typedef struct _TargaHeader
188 unsigned char id_length, colormap_type, image_type;
189 unsigned short colormap_index, colormap_length;
190 unsigned char colormap_size;
191 unsigned short x_origin, y_origin, width, height;
192 unsigned char pixel_size, attributes;
196 void PrintTargaHeader(TargaHeader *t)
198 Con_Printf("TargaHeader:\n");
199 Con_Printf("uint8 id_length = %i;\n", t->id_length);
200 Con_Printf("uint8 colormap_type = %i;\n", t->colormap_type);
201 Con_Printf("uint8 image_type = %i;\n", t->image_type);
202 Con_Printf("uint16 colormap_index = %i;\n", t->colormap_index);
203 Con_Printf("uint16 colormap_length = %i;\n", t->colormap_length);
204 Con_Printf("uint8 colormap_size = %i;\n", t->colormap_size);
205 Con_Printf("uint16 x_origin = %i;\n", t->x_origin);
206 Con_Printf("uint16 y_origin = %i;\n", t->y_origin);
207 Con_Printf("uint16 width = %i;\n", t->width);
208 Con_Printf("uint16 height = %i;\n", t->height);
209 Con_Printf("uint8 pixel_size = %i;\n", t->pixel_size);
210 Con_Printf("uint8 attributes = %i;\n", t->attributes);
218 qbyte *LoadTGA (qbyte *f, int matchwidth, int matchheight)
220 int x, y, row_inc, compressed, readpixelcount, red, green, blue, alpha, runlen, pindex;
221 qbyte *pixbuf, *image_rgba;
222 qbyte *fin, *enddata;
223 TargaHeader targa_header;
224 unsigned char palette[256*4], *p;
226 if (fs_filesize < 19)
229 enddata = f + fs_filesize;
231 targa_header.id_length = f[0];
232 targa_header.colormap_type = f[1];
233 targa_header.image_type = f[2];
235 targa_header.colormap_index = f[3] + f[4] * 256;
236 targa_header.colormap_length = f[5] + f[6] * 256;
237 targa_header.colormap_size = f[7];
238 targa_header.x_origin = f[8] + f[9] * 256;
239 targa_header.y_origin = f[10] + f[11] * 256;
240 targa_header.width = image_width = f[12] + f[13] * 256;
241 targa_header.height = image_height = f[14] + f[15] * 256;
242 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
244 Con_Printf("LoadTGA: invalid size\n");
245 PrintTargaHeader(&targa_header);
248 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
250 targa_header.pixel_size = f[16];
251 targa_header.attributes = f[17];
254 if (targa_header.id_length != 0)
255 fin += targa_header.id_length; // skip TARGA image comment
256 if (targa_header.image_type == 2 || targa_header.image_type == 10)
258 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
260 Con_Printf ("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
261 PrintTargaHeader(&targa_header);
265 else if (targa_header.image_type == 1 || targa_header.image_type == 9)
267 if (targa_header.pixel_size != 8)
269 Con_Printf ("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
270 PrintTargaHeader(&targa_header);
273 if (targa_header.colormap_length > 256)
275 Con_Printf ("LoadTGA: only up to 256 colormap_length supported\n");
276 PrintTargaHeader(&targa_header);
279 if (targa_header.colormap_index)
281 Con_Printf ("LoadTGA: colormap_index not supported\n");
282 PrintTargaHeader(&targa_header);
285 if (targa_header.colormap_size == 24)
287 for (x = 0;x < targa_header.colormap_length;x++)
289 palette[x*4+2] = *fin++;
290 palette[x*4+1] = *fin++;
291 palette[x*4+0] = *fin++;
292 palette[x*4+3] = 255;
295 else if (targa_header.colormap_size == 32)
297 for (x = 0;x < targa_header.colormap_length;x++)
299 palette[x*4+2] = *fin++;
300 palette[x*4+1] = *fin++;
301 palette[x*4+0] = *fin++;
302 palette[x*4+3] = *fin++;
307 Con_Printf ("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
308 PrintTargaHeader(&targa_header);
312 else if (targa_header.image_type == 3 || targa_header.image_type == 11)
314 if (targa_header.pixel_size != 8)
316 Con_Printf ("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
317 PrintTargaHeader(&targa_header);
323 Con_Printf ("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
324 PrintTargaHeader(&targa_header);
328 if (targa_header.attributes & 0x10)
330 Con_Printf ("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
334 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
337 Con_Printf ("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
341 // If bit 5 of attributes isn't set, the image has been stored from bottom to top
342 if ((targa_header.attributes & 0x20) == 0)
344 pixbuf = image_rgba + (image_height - 1)*image_width*4;
345 row_inc = -image_width*4*2;
353 compressed = targa_header.image_type == 9 || targa_header.image_type == 10 || targa_header.image_type == 11;
356 red = green = blue = alpha = 255;
357 while (y < image_height)
359 // decoder is mostly the same whether it's compressed or not
360 readpixelcount = 1000000;
362 if (compressed && fin < enddata)
365 // high bit indicates this is an RLE compressed run
368 runlen = 1 + (runlen & 0x7f);
371 while((runlen--) && y < image_height)
373 if (readpixelcount > 0)
376 red = green = blue = alpha = 255;
379 switch(targa_header.image_type)
385 if (pindex >= targa_header.colormap_length)
387 p = palette + pindex * 4;
401 if (targa_header.pixel_size == 32 && fin < enddata)
407 red = green = blue = *fin++;
417 if (x == image_width)
419 // end of line, advance to next
435 qbyte *LoadLMP (qbyte *f, int matchwidth, int matchheight)
441 Con_Printf("LoadLMP: invalid LMP file\n");
445 // parse the very complicated header *chuckle*
446 image_width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
447 image_height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
448 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
450 Con_Printf("LoadLMP: invalid size %ix%i\n", image_width, image_height);
453 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
456 if (fs_filesize < 8 + image_width * image_height)
458 Con_Printf("LoadLMP: invalid LMP file\n");
462 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
465 Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
468 Image_Copy8bitRGBA(f + 8, image_rgba, image_width * image_height, palette_complete);
475 unsigned width, height;
476 unsigned offsets[MIPLEVELS]; // four mip maps stored
477 char animname[32]; // next frame in animation chain
483 qbyte *LoadWAL (qbyte *f, int matchwidth, int matchheight)
486 const q2wal_t *inwal = (const void *)f;
488 if (fs_filesize < (int) sizeof(q2wal_t))
490 Con_Printf("LoadWAL: invalid WAL file\n");
494 image_width = LittleLong(inwal->width);
495 image_height = LittleLong(inwal->height);
496 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
498 Con_Printf("LoadWAL: invalid size %ix%i\n", image_width, image_height);
501 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
504 if ((int) fs_filesize < (int) sizeof(q2wal_t) + (int) LittleLong(inwal->offsets[0]) + image_width * image_height)
506 Con_Printf("LoadWAL: invalid WAL file\n");
510 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
513 Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
516 Image_Copy8bitRGBA(f + LittleLong(inwal->offsets[0]), image_rgba, image_width * image_height, palette_complete);
527 qbyte *LoadLMPAs8Bit (qbyte *f, int matchwidth, int matchheight)
533 Con_Printf("LoadLMPAs8Bit: invalid LMP file\n");
537 // parse the very complicated header *chuckle*
538 image_width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
539 image_height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
540 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
542 Con_Printf("LoadLMPAs8Bit: invalid size %ix%i\n", image_width, image_height);
545 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
548 if (fs_filesize < 8 + image_width * image_height)
550 Con_Printf("LoadLMPAs8Bit: invalid LMP file\n");
554 image_8bit = Mem_Alloc(tempmempool, image_width * image_height);
557 Con_Printf("LoadLMPAs8Bit: not enough memory for %i by %i image\n", image_width, image_height);
560 memcpy(image_8bit, f + 8, image_width * image_height);
564 void Image_StripImageExtension (const char *in, char *out)
566 const char *end, *temp;
567 end = in + strlen(in);
571 if (strcmp(temp, ".tga") == 0
572 || strcmp(temp, ".pcx") == 0
573 || strcmp(temp, ".lmp") == 0
574 || strcmp(temp, ".png") == 0
575 || strcmp(temp, ".jpg") == 0)
587 const char *formatstring;
588 qbyte *(*loadfunc)(qbyte *f, int matchwidth, int matchheight);
592 {"override/%s.tga", LoadTGA},
593 {"override/%s.jpg", JPEG_LoadImage},
594 {"textures/%s.tga", LoadTGA},
595 {"textures/%s.jpg", JPEG_LoadImage},
596 {"textures/%s.pcx", LoadPCX},
597 {"textures/%s.wal", LoadWAL},
599 {"%s.jpg", JPEG_LoadImage},
605 qbyte *loadimagepixels (const char *filename, qboolean complain, int matchwidth, int matchheight)
608 qbyte *f, *data = NULL;
609 char basename[MAX_QPATH], name[MAX_QPATH], *c;
610 if (developer_memorydebug.integer)
611 Mem_CheckSentinelsGlobal();
612 Image_StripImageExtension(filename, basename); // strip filename extensions to allow replacement by other types
613 // replace *'s with #, so commandline utils don't get confused when dealing with the external files
614 for (c = basename;*c;c++)
617 for (i = 0;imageformats[i].formatstring;i++)
619 sprintf (name, imageformats[i].formatstring, basename);
620 if ((f = FS_LoadFile(name, true)) && (data = imageformats[i].loadfunc(f, matchwidth, matchheight)))
623 Con_DPrintf("loaded image %s (%dx%d)\n", name, image_width, image_height);
624 if (developer_memorydebug.integer)
625 Mem_CheckSentinelsGlobal();
631 Con_Printf ("Couldn't load %s using ", filename);
632 for (i = 0;imageformats[i].formatstring;i++)
634 sprintf (name, imageformats[i].formatstring, basename);
635 Con_Printf (i == 0 ? "\"%s\"" : (imageformats[i+1].formatstring ? ", \"%s\"" : " or \"%s\".\n"), imageformats[i].formatstring);
638 if (developer_memorydebug.integer)
639 Mem_CheckSentinelsGlobal();
643 int image_makemask (const qbyte *in, qbyte *out, int size)
647 for (i = 0;i < size;i++)
649 out[0] = out[1] = out[2] = 255;
659 qbyte* loadimagepixelsmask (const char *filename, qboolean complain, int matchwidth, int matchheight)
662 in = data = loadimagepixels(filename, complain, matchwidth, matchheight);
665 if (image_makemask(data, data, image_width * image_height))
666 return data; // some transparency
670 return NULL; // all opaque
674 rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
678 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
680 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
685 rtexture_t *loadtextureimagemask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
689 if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
691 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
696 rtexture_t *image_masktex;
697 rtexture_t *image_nmaptex;
698 rtexture_t *loadtextureimagewithmask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
702 image_masktex = NULL;
703 image_nmaptex = NULL;
704 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
707 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
709 if (flags & TEXF_ALPHA && image_makemask(data, data, image_width * image_height))
710 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
716 rtexture_t *loadtextureimagewithmaskandnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
720 image_masktex = NULL;
721 image_nmaptex = NULL;
722 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
725 data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
727 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
729 Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
730 image_nmaptex = R_LoadTexture2D(pool, va("%s_nmap", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
732 if (flags & TEXF_ALPHA && image_makemask(data, data2, image_width * image_height))
733 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
741 rtexture_t *loadtextureimagebumpasnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
745 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
747 data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
749 Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
750 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
757 qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int height, const qbyte *data)
761 const qbyte *in, *end;
763 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
765 memset (buffer, 0, 18);
766 buffer[2] = 2; // uncompressed type
767 buffer[12] = (width >> 0) & 0xFF;
768 buffer[13] = (width >> 8) & 0xFF;
769 buffer[14] = (height >> 0) & 0xFF;
770 buffer[15] = (height >> 8) & 0xFF;
771 buffer[16] = 24; // pixel size
776 end = in + width*height*3;
777 for (;in < end;in += 3)
783 ret = FS_WriteFile (filename, buffer, width*height*3 + 18 );
789 void Image_WriteTGARGB (const char *filename, int width, int height, const qbyte *data)
793 const qbyte *in, *end;
795 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
797 memset (buffer, 0, 18);
798 buffer[2] = 2; // uncompressed type
799 buffer[12] = (width >> 0) & 0xFF;
800 buffer[13] = (width >> 8) & 0xFF;
801 buffer[14] = (height >> 0) & 0xFF;
802 buffer[15] = (height >> 8) & 0xFF;
803 buffer[16] = 24; // pixel size
805 // swap rgb to bgr and flip upside down
807 for (y = height - 1;y >= 0;y--)
809 in = data + y * width * 3;
810 end = in + width * 3;
811 for (;in < end;in += 3)
818 FS_WriteFile (filename, buffer, width*height*3 + 18 );
823 void Image_WriteTGARGBA (const char *filename, int width, int height, const qbyte *data)
827 const qbyte *in, *end;
829 buffer = Mem_Alloc(tempmempool, width*height*4 + 18);
831 memset (buffer, 0, 18);
832 buffer[2] = 2; // uncompressed type
833 buffer[12] = (width >> 0) & 0xFF;
834 buffer[13] = (width >> 8) & 0xFF;
835 buffer[14] = (height >> 0) & 0xFF;
836 buffer[15] = (height >> 8) & 0xFF;
837 buffer[16] = 32; // pixel size
839 // swap rgba to bgra and flip upside down
841 for (y = height - 1;y >= 0;y--)
843 in = data + y * width * 4;
844 end = in + width * 4;
845 for (;in < end;in += 4)
853 FS_WriteFile (filename, buffer, width*height*4 + 18 );
858 qboolean Image_CheckAlpha(const qbyte *data, int size, qboolean rgba)
864 for (end = data + size * 4, data += 3;data < end;data += 4)
870 // color 255 is transparent
871 for (end = data + size;data < end;data++)
878 static void Image_Resample32LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
880 int j, xi, oldx = 0, f, fstep, endx, lerp;
881 fstep = (int) (inwidth*65536.0f/outwidth);
883 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
888 in += (xi - oldx) * 4;
894 *out++ = (qbyte) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
895 *out++ = (qbyte) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
896 *out++ = (qbyte) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
897 *out++ = (qbyte) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
899 else // last pixel of the line has no pixel to lerp to
909 static void Image_Resample24LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
911 int j, xi, oldx = 0, f, fstep, endx, lerp;
912 fstep = (int) (inwidth*65536.0f/outwidth);
914 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
919 in += (xi - oldx) * 3;
925 *out++ = (qbyte) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
926 *out++ = (qbyte) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
927 *out++ = (qbyte) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
929 else // last pixel of the line has no pixel to lerp to
938 int resamplerowsize = 0;
939 qbyte *resamplerow1 = NULL;
940 qbyte *resamplerow2 = NULL;
941 mempool_t *resamplemempool = NULL;
943 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (qbyte) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
944 void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
946 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
950 fstep = (int) (inheight*65536.0f/outheight);
954 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
955 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
956 for (i = 0, f = 0;i < outheight;i++,f += fstep)
964 inrow = (qbyte *)indata + inwidth4*yi;
966 memcpy(resamplerow1, resamplerow2, outwidth4);
968 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
969 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1020 resamplerow1 -= outwidth4;
1021 resamplerow2 -= outwidth4;
1027 inrow = (qbyte *)indata + inwidth4*yi;
1029 memcpy(resamplerow1, resamplerow2, outwidth4);
1031 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1034 memcpy(out, resamplerow1, outwidth4);
1039 void Image_Resample32Nearest(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1042 unsigned frac, fracstep;
1043 // relies on int being 4 bytes
1047 fracstep = inwidth*0x10000/outwidth;
1048 for (i = 0;i < outheight;i++)
1050 inrow = (int *)indata + inwidth*(i*inheight/outheight);
1051 frac = fracstep >> 1;
1055 out[0] = inrow[frac >> 16];frac += fracstep;
1056 out[1] = inrow[frac >> 16];frac += fracstep;
1057 out[2] = inrow[frac >> 16];frac += fracstep;
1058 out[3] = inrow[frac >> 16];frac += fracstep;
1064 out[0] = inrow[frac >> 16];frac += fracstep;
1065 out[1] = inrow[frac >> 16];frac += fracstep;
1070 out[0] = inrow[frac >> 16];frac += fracstep;
1076 void Image_Resample24Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1078 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
1082 fstep = (int) (inheight*65536.0f/outheight);
1086 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1087 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1088 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1096 inrow = (qbyte *)indata + inwidth3*yi;
1098 memcpy(resamplerow1, resamplerow2, outwidth3);
1100 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1101 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1145 resamplerow1 -= outwidth3;
1146 resamplerow2 -= outwidth3;
1152 inrow = (qbyte *)indata + inwidth3*yi;
1154 memcpy(resamplerow1, resamplerow2, outwidth3);
1156 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1159 memcpy(out, resamplerow1, outwidth3);
1164 void Image_Resample24Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1166 int i, j, f, inwidth3 = inwidth * 3;
1167 unsigned frac, fracstep;
1171 fracstep = inwidth*0x10000/outwidth;
1172 for (i = 0;i < outheight;i++)
1174 inrow = (qbyte *)indata + inwidth3*(i*inheight/outheight);
1175 frac = fracstep >> 1;
1179 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1180 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;
1182 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1187 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1188 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1193 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1204 void Image_Resample (const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int bytesperpixel, int quality)
1206 if (indepth != 1 || outdepth != 1)
1207 Sys_Error("Image_Resample: 3D resampling not supported\n");
1208 if (resamplerowsize < outwidth*4)
1211 Mem_Free(resamplerow1);
1212 resamplerowsize = outwidth*4;
1213 if (!resamplemempool)
1214 resamplemempool = Mem_AllocPool("Image Scaling Buffer");
1215 resamplerow1 = Mem_Alloc(resamplemempool, resamplerowsize*2);
1216 resamplerow2 = resamplerow1 + resamplerowsize;
1218 if (bytesperpixel == 4)
1221 Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1223 Image_Resample32Nearest(indata, inwidth, inheight, outdata, outwidth, outheight);
1225 else if (bytesperpixel == 3)
1228 Image_Resample24Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1230 Image_Resample24Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1233 Sys_Error("Image_Resample: unsupported bytesperpixel %i\n", bytesperpixel);
1236 // in can be the same as out
1237 void Image_MipReduce(const qbyte *in, qbyte *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth, int bytesperpixel)
1240 if (*depth != 1 || destdepth != 1)
1241 Sys_Error("Image_Resample: 3D resampling not supported\n");
1242 nextrow = *width * bytesperpixel;
1243 if (*width > destwidth)
1246 if (*height > destheight)
1250 if (bytesperpixel == 4)
1252 for (y = 0;y < *height;y++)
1254 for (x = 0;x < *width;x++)
1256 out[0] = (qbyte) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
1257 out[1] = (qbyte) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1258 out[2] = (qbyte) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1259 out[3] = (qbyte) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1263 in += nextrow; // skip a line
1266 else if (bytesperpixel == 3)
1268 for (y = 0;y < *height;y++)
1270 for (x = 0;x < *width;x++)
1272 out[0] = (qbyte) ((in[0] + in[3] + in[nextrow ] + in[nextrow+3]) >> 2);
1273 out[1] = (qbyte) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
1274 out[2] = (qbyte) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
1278 in += nextrow; // skip a line
1282 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1287 if (bytesperpixel == 4)
1289 for (y = 0;y < *height;y++)
1291 for (x = 0;x < *width;x++)
1293 out[0] = (qbyte) ((in[0] + in[4]) >> 1);
1294 out[1] = (qbyte) ((in[1] + in[5]) >> 1);
1295 out[2] = (qbyte) ((in[2] + in[6]) >> 1);
1296 out[3] = (qbyte) ((in[3] + in[7]) >> 1);
1302 else if (bytesperpixel == 3)
1304 for (y = 0;y < *height;y++)
1306 for (x = 0;x < *width;x++)
1308 out[0] = (qbyte) ((in[0] + in[3]) >> 1);
1309 out[1] = (qbyte) ((in[1] + in[4]) >> 1);
1310 out[2] = (qbyte) ((in[2] + in[5]) >> 1);
1317 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1322 if (*height > destheight)
1326 if (bytesperpixel == 4)
1328 for (y = 0;y < *height;y++)
1330 for (x = 0;x < *width;x++)
1332 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1333 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1334 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1335 out[3] = (qbyte) ((in[3] + in[nextrow+3]) >> 1);
1339 in += nextrow; // skip a line
1342 else if (bytesperpixel == 3)
1344 for (y = 0;y < *height;y++)
1346 for (x = 0;x < *width;x++)
1348 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1349 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1350 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1354 in += nextrow; // skip a line
1358 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1361 Sys_Error("Image_MipReduce: desired size already achieved\n");
1365 void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
1368 const unsigned char *p0, *p1, *p2;
1370 float iwidth, iheight, ibumpscale, n[3];
1371 iwidth = 1.0f / width;
1372 iheight = 1.0f / height;
1373 ibumpscale = (255.0f * 3.0f) / bumpscale;
1375 for (y = 0;y < height;y++)
1377 for (x = 0;x < width;x++)
1379 p0 = inpixels + (y * width + x) * 4;
1383 p1 = inpixels + (y * width + x) * 4;
1385 p1 = inpixels + (y * width) * 4;
1388 p1 = inpixels + (y * width + x + 1) * 4;
1389 if (y == height - 1)
1392 p2 = inpixels + (y * width + x) * 4;
1394 p2 = inpixels + x * 4;
1397 p2 = inpixels + ((y + 1) * width + x) * 4;
1401 dv[0][2] = ((p0[0] + p0[1] + p0[2]) * ibumpscale) - ((p1[0] + p1[1] + p1[2]) * ibumpscale);
1404 dv[1][2] = ((p2[0] + p2[1] + p2[2]) * ibumpscale) - ((p0[0] + p0[1] + p0[2]) * ibumpscale);
1405 n[0] = dv[0][1]*dv[1][2]-dv[0][2]*dv[1][1];
1406 n[1] = dv[0][2]*dv[1][0]-dv[0][0]*dv[1][2];
1407 n[2] = dv[0][0]*dv[1][1]-dv[0][1]*dv[1][0];
1409 n[0] = ((p1[0] + p1[1] + p1[2]) - (p0[0] + p0[1] + p0[2]));
1410 n[1] = ((p0[0] + p0[1] + p0[2]) - (p2[0] + p2[1] + p2[2]));
1414 // this should work for the bottom right triangle if anyone wants
1415 // code for that for some reason
1416 n[0] = ((p3[0] + p3[1] + p3[2]) - (p1[0] + p1[1] + p1[2]));
1417 n[1] = ((p2[0] + p2[1] + p2[2]) - (p3[0] + p3[1] + p3[2]));
1421 out[0] = 128.0f + n[0] * 127.0f;
1422 out[1] = 128.0f + n[1] * 127.0f;
1423 out[2] = 128.0f + n[2] * 127.0f;
1430 int image_loadskin(imageskin_t *s, char *shadername)
1434 int bumppixels_width, bumppixels_height;
1435 char name[MAX_QPATH];
1436 Image_StripImageExtension(shadername, name);
1437 memset(s, 0, sizeof(*s));
1438 s->basepixels = loadimagepixels(name, false, 0, 0);
1439 if (s->basepixels == NULL)
1441 s->basepixels_width = image_width;
1442 s->basepixels_height = image_height;
1444 bumppixels = NULL;bumppixels_width = 0;bumppixels_height = 0;
1445 if (Image_CheckAlpha(s->basepixels, s->basepixels_width * s->basepixels_height, true))
1447 s->maskpixels = Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1448 s->maskpixels_width = s->basepixels_width;
1449 s->maskpixels_height = s->basepixels_height;
1450 memcpy(s->maskpixels, s->basepixels, s->maskpixels_width * s->maskpixels_height * 4);
1451 for (j = 0;j < s->basepixels_width * s->basepixels_height * 4;j += 4)
1453 s->maskpixels[j+0] = 255;
1454 s->maskpixels[j+1] = 255;
1455 s->maskpixels[j+2] = 255;
1459 // _luma is supported for tenebrae compatibility
1460 // (I think it's a very stupid name, but oh well)
1461 if ((s->glowpixels = loadimagepixels(va("%s_glow", name), false, 0, 0)) != NULL
1462 || (s->glowpixels = loadimagepixels(va("%s_luma", name), false, 0, 0)) != NULL)
1464 s->glowpixels_width = image_width;
1465 s->glowpixels_height = image_height;
1467 // _norm is the name used by tenebrae
1468 // (I don't like the name much)
1469 if ((s->nmappixels = loadimagepixels(va("%s_norm", name), false, 0, 0)) != NULL)
1471 s->nmappixels_width = image_width;
1472 s->nmappixels_height = image_height;
1474 else if ((bumppixels = loadimagepixels(va("%s_bump", name), false, 0, 0)) != NULL)
1476 bumppixels_width = image_width;
1477 bumppixels_height = image_height;
1479 if ((s->glosspixels = loadimagepixels(va("%s_gloss", name), false, 0, 0)) != NULL)
1481 s->glosspixels_width = image_width;
1482 s->glosspixels_height = image_height;
1484 if ((s->pantspixels = loadimagepixels(va("%s_pants", name), false, 0, 0)) != NULL)
1486 s->pantspixels_width = image_width;
1487 s->pantspixels_height = image_height;
1489 if ((s->shirtpixels = loadimagepixels(va("%s_shirt", name), false, 0, 0)) != NULL)
1491 s->shirtpixels_width = image_width;
1492 s->shirtpixels_height = image_height;
1495 if (s->nmappixels == NULL)
1497 if (bumppixels != NULL)
1499 if (r_shadow_bumpscale_bumpmap.value > 0)
1501 s->nmappixels = Mem_Alloc(loadmodel->mempool, bumppixels_width * bumppixels_height * 4);
1502 s->nmappixels_width = bumppixels_width;
1503 s->nmappixels_height = bumppixels_height;
1504 Image_HeightmapToNormalmap(bumppixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_bumpmap.value);
1509 if (r_shadow_bumpscale_basetexture.value > 0)
1511 s->nmappixels = Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1512 s->nmappixels_width = s->basepixels_width;
1513 s->nmappixels_height = s->basepixels_height;
1514 Image_HeightmapToNormalmap(s->basepixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_basetexture.value);
1518 if (bumppixels != NULL)
1519 Mem_Free(bumppixels);
1523 void image_freeskin(imageskin_t *s)
1526 Mem_Free(s->basepixels);
1528 Mem_Free(s->maskpixels);
1530 Mem_Free(s->nmappixels);
1532 Mem_Free(s->glowpixels);
1534 Mem_Free(s->glosspixels);
1536 Mem_Free(s->pantspixels);
1538 Mem_Free(s->shirtpixels);
1539 memset(s, 0, sizeof(*s));