11 // written by LordHavoc in a readable way, optimized by Vic, further optimized by LordHavoc (the non-special index case), readable version preserved below this
12 void Image_CopyMux(qbyte *outpixels, const qbyte *inpixels, int inputwidth, int inputheight, qboolean inputflipx, qboolean inputflipy, qboolean inputflipdiagonal, int numoutputcomponents, int numinputcomponents, int *outputinputcomponentindices)
15 const qbyte *in, *line;
16 int row_inc = (inputflipy ? -inputwidth : inputwidth) * numinputcomponents, col_inc = (inputflipx ? -1 : 1) * numinputcomponents;
17 int row_ofs = (inputflipy ? (inputheight - 1) * inputwidth * numinputcomponents : 0), col_ofs = (inputflipx ? (inputwidth - 1) * numinputcomponents : 0);
19 for (c = 0; c < numoutputcomponents; c++)
20 if (outputinputcomponentindices[c] & 0x80000000)
22 if (c < numoutputcomponents)
24 // special indices used
25 if (inputflipdiagonal)
27 for (x = 0, line = inpixels + col_ofs; x < inputwidth; x++, line += col_inc)
28 for (y = 0, in = line + row_ofs; y < inputheight; y++, in += row_inc, outpixels += numinputcomponents)
29 for (c = 0; c < numoutputcomponents; c++)
30 outpixels[c] = ((index = outputinputcomponentindices[c]) & 0x80000000) ? index : in[index];
34 for (y = 0, line = inpixels + row_ofs; y < inputheight; y++, line += row_inc)
35 for (x = 0, in = line + col_ofs; x < inputwidth; x++, in += col_inc, outpixels += numinputcomponents)
36 for (c = 0; c < numoutputcomponents; c++)
37 outpixels[c] = ((index = outputinputcomponentindices[c]) & 0x80000000) ? index : in[index];
42 // special indices not used
43 if (inputflipdiagonal)
45 for (x = 0, line = inpixels + col_ofs; x < inputwidth; x++, line += col_inc)
46 for (y = 0, in = line + row_ofs; y < inputheight; y++, in += row_inc, outpixels += numinputcomponents)
47 for (c = 0; c < numoutputcomponents; c++)
48 outpixels[c] = in[outputinputcomponentindices[c]];
52 for (y = 0, line = inpixels + row_ofs; y < inputheight; y++, line += row_inc)
53 for (x = 0, in = line + col_ofs; x < inputwidth; x++, in += col_inc, outpixels += numinputcomponents)
54 for (c = 0; c < numoutputcomponents; c++)
55 outpixels[c] = in[outputinputcomponentindices[c]];
60 // intentionally readable version
61 void Image_CopyMux(qbyte *outpixels, const qbyte *inpixels, int inputwidth, int inputheight, qboolean inputflipx, qboolean inputflipy, qboolean inputflipdiagonal, int numoutputcomponents, int numinputcomponents, int *outputinputcomponentindices)
64 const qbyte *in, *inrow, *incolumn;
65 if (inputflipdiagonal)
67 for (x = 0;x < inputwidth;x++)
69 for (y = 0;y < inputheight;y++)
71 in = inpixels + ((inputflipy ? inputheight - 1 - y : y) * inputwidth + (inputflipx ? inputwidth - 1 - x : x)) * numinputcomponents;
72 for (c = 0;c < numoutputcomponents;c++)
74 index = outputinputcomponentindices[c];
75 if (index & 0x80000000)
78 *outpixels++ = in[index];
85 for (y = 0;y < inputheight;y++)
87 for (x = 0;x < inputwidth;x++)
89 in = inpixels + ((inputflipy ? inputheight - 1 - y : y) * inputwidth + (inputflipx ? inputwidth - 1 - x : x)) * numinputcomponents;
90 for (c = 0;c < numoutputcomponents;c++)
92 index = outputinputcomponentindices[c];
93 if (index & 0x80000000)
96 *outpixels++ = in[index];
104 void Image_GammaRemapRGB(const qbyte *in, qbyte *out, int pixels, const qbyte *gammar, const qbyte *gammag, const qbyte *gammab)
108 out[0] = gammar[in[0]];
109 out[1] = gammag[in[1]];
110 out[2] = gammab[in[2]];
116 // note: pal must be 32bit color
117 void Image_Copy8bitRGBA(const qbyte *in, qbyte *out, int pixels, const unsigned int *pal)
119 int *iout = (void *)out;
122 iout[0] = pal[in[0]];
123 iout[1] = pal[in[1]];
124 iout[2] = pal[in[2]];
125 iout[3] = pal[in[3]];
126 iout[4] = pal[in[4]];
127 iout[5] = pal[in[5]];
128 iout[6] = pal[in[6]];
129 iout[7] = pal[in[7]];
136 iout[0] = pal[in[0]];
137 iout[1] = pal[in[1]];
138 iout[2] = pal[in[2]];
139 iout[3] = pal[in[3]];
145 iout[0] = pal[in[0]];
146 iout[1] = pal[in[1]];
151 iout[0] = pal[in[0]];
155 =================================================================
159 =================================================================
168 unsigned short xmin,ymin,xmax,ymax;
169 unsigned short hres,vres;
170 unsigned char palette[48];
173 unsigned short bytes_per_line;
174 unsigned short palette_type;
183 qbyte* LoadPCX (const qbyte *f, int matchwidth, int matchheight)
186 qbyte *a, *b, *image_rgba, *pbuf;
187 const qbyte *palette, *fin, *enddata;
188 int x, y, x2, dataByte;
190 if (fs_filesize < (int)sizeof(pcx) + 768)
192 Con_Print("Bad pcx file\n");
198 memcpy(&pcx, fin, sizeof(pcx));
201 // LordHavoc: big-endian support ported from QF newtree
202 pcx.xmax = LittleShort (pcx.xmax);
203 pcx.xmin = LittleShort (pcx.xmin);
204 pcx.ymax = LittleShort (pcx.ymax);
205 pcx.ymin = LittleShort (pcx.ymin);
206 pcx.hres = LittleShort (pcx.hres);
207 pcx.vres = LittleShort (pcx.vres);
208 pcx.bytes_per_line = LittleShort (pcx.bytes_per_line);
209 pcx.palette_type = LittleShort (pcx.palette_type);
211 image_width = pcx.xmax + 1 - pcx.xmin;
212 image_height = pcx.ymax + 1 - pcx.ymin;
213 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)
215 Con_Print("Bad pcx file\n");
218 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
221 palette = f + fs_filesize - 768;
223 image_rgba = Mem_Alloc(tempmempool, image_width*image_height*4);
226 Con_Printf("LoadPCX: not enough memory for %i by %i image\n", image_width, image_height);
229 pbuf = image_rgba + image_width*image_height*3;
232 for (y = 0;y < image_height && fin < enddata;y++)
234 a = pbuf + y * image_width;
235 for (x = 0;x < image_width && fin < enddata;)
242 x2 = x + (dataByte & 0x3F);
244 if (x2 > image_width)
245 x2 = image_width; // technically an error
252 fin += pcx.bytes_per_line - image_width; // the number of bytes per line is always forced to an even number
253 while(x < image_width)
260 for(x = 0;x < image_width*image_height;x++)
273 =========================================================
277 =========================================================
280 typedef struct _TargaHeader
282 unsigned char id_length, colormap_type, image_type;
283 unsigned short colormap_index, colormap_length;
284 unsigned char colormap_size;
285 unsigned short x_origin, y_origin, width, height;
286 unsigned char pixel_size, attributes;
290 void PrintTargaHeader(TargaHeader *t)
292 Con_Print("TargaHeader:\n");
293 Con_Printf("uint8 id_length = %i;\n", t->id_length);
294 Con_Printf("uint8 colormap_type = %i;\n", t->colormap_type);
295 Con_Printf("uint8 image_type = %i;\n", t->image_type);
296 Con_Printf("uint16 colormap_index = %i;\n", t->colormap_index);
297 Con_Printf("uint16 colormap_length = %i;\n", t->colormap_length);
298 Con_Printf("uint8 colormap_size = %i;\n", t->colormap_size);
299 Con_Printf("uint16 x_origin = %i;\n", t->x_origin);
300 Con_Printf("uint16 y_origin = %i;\n", t->y_origin);
301 Con_Printf("uint16 width = %i;\n", t->width);
302 Con_Printf("uint16 height = %i;\n", t->height);
303 Con_Printf("uint8 pixel_size = %i;\n", t->pixel_size);
304 Con_Printf("uint8 attributes = %i;\n", t->attributes);
312 qbyte *LoadTGA (const qbyte *f, int matchwidth, int matchheight)
314 int x, y, row_inc, compressed, readpixelcount, red, green, blue, alpha, runlen, pindex;
315 qbyte *pixbuf, *image_rgba;
316 const qbyte *fin, *enddata;
317 TargaHeader targa_header;
318 unsigned char palette[256*4], *p;
320 if (fs_filesize < 19)
323 enddata = f + fs_filesize;
325 targa_header.id_length = f[0];
326 targa_header.colormap_type = f[1];
327 targa_header.image_type = f[2];
329 targa_header.colormap_index = f[3] + f[4] * 256;
330 targa_header.colormap_length = f[5] + f[6] * 256;
331 targa_header.colormap_size = f[7];
332 targa_header.x_origin = f[8] + f[9] * 256;
333 targa_header.y_origin = f[10] + f[11] * 256;
334 targa_header.width = image_width = f[12] + f[13] * 256;
335 targa_header.height = image_height = f[14] + f[15] * 256;
336 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
338 Con_Print("LoadTGA: invalid size\n");
339 PrintTargaHeader(&targa_header);
342 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
344 targa_header.pixel_size = f[16];
345 targa_header.attributes = f[17];
348 if (targa_header.id_length != 0)
349 fin += targa_header.id_length; // skip TARGA image comment
350 if (targa_header.image_type == 2 || targa_header.image_type == 10)
352 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
354 Con_Print("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
355 PrintTargaHeader(&targa_header);
359 else if (targa_header.image_type == 1 || targa_header.image_type == 9)
361 if (targa_header.pixel_size != 8)
363 Con_Print("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
364 PrintTargaHeader(&targa_header);
367 if (targa_header.colormap_length > 256)
369 Con_Print("LoadTGA: only up to 256 colormap_length supported\n");
370 PrintTargaHeader(&targa_header);
373 if (targa_header.colormap_index)
375 Con_Print("LoadTGA: colormap_index not supported\n");
376 PrintTargaHeader(&targa_header);
379 if (targa_header.colormap_size == 24)
381 for (x = 0;x < targa_header.colormap_length;x++)
383 palette[x*4+2] = *fin++;
384 palette[x*4+1] = *fin++;
385 palette[x*4+0] = *fin++;
386 palette[x*4+3] = 255;
389 else if (targa_header.colormap_size == 32)
391 for (x = 0;x < targa_header.colormap_length;x++)
393 palette[x*4+2] = *fin++;
394 palette[x*4+1] = *fin++;
395 palette[x*4+0] = *fin++;
396 palette[x*4+3] = *fin++;
401 Con_Print("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
402 PrintTargaHeader(&targa_header);
406 else if (targa_header.image_type == 3 || targa_header.image_type == 11)
408 if (targa_header.pixel_size != 8)
410 Con_Print("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
411 PrintTargaHeader(&targa_header);
417 Con_Printf("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
418 PrintTargaHeader(&targa_header);
422 if (targa_header.attributes & 0x10)
424 Con_Print("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
428 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
431 Con_Printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
435 // If bit 5 of attributes isn't set, the image has been stored from bottom to top
436 if ((targa_header.attributes & 0x20) == 0)
438 pixbuf = image_rgba + (image_height - 1)*image_width*4;
439 row_inc = -image_width*4*2;
447 compressed = targa_header.image_type == 9 || targa_header.image_type == 10 || targa_header.image_type == 11;
450 red = green = blue = alpha = 255;
451 while (y < image_height)
453 // decoder is mostly the same whether it's compressed or not
454 readpixelcount = 1000000;
456 if (compressed && fin < enddata)
459 // high bit indicates this is an RLE compressed run
462 runlen = 1 + (runlen & 0x7f);
465 while((runlen--) && y < image_height)
467 if (readpixelcount > 0)
470 red = green = blue = alpha = 255;
473 switch(targa_header.image_type)
479 if (pindex >= targa_header.colormap_length)
481 p = palette + pindex * 4;
495 if (targa_header.pixel_size == 32 && fin < enddata)
501 red = green = blue = *fin++;
511 if (x == image_width)
513 // end of line, advance to next
529 qbyte *LoadLMP (const qbyte *f, int matchwidth, int matchheight, qboolean loadAs8Bit)
535 Con_Print("LoadLMP: invalid LMP file\n");
539 // parse the very complicated header *chuckle*
540 image_width = BuffLittleLong(f);
541 image_height = BuffLittleLong(f + 4);
542 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
544 Con_Printf("LoadLMP: invalid size %ix%i\n", image_width, image_height);
547 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
550 if (fs_filesize < 8 + image_width * image_height)
552 Con_Print("LoadLMP: invalid LMP file\n");
558 image_buffer = Mem_Alloc(tempmempool, image_width * image_height);
559 memcpy(image_buffer, f + 8, image_width * image_height);
563 image_buffer = Mem_Alloc(tempmempool, image_width * image_height * 4);
564 Image_Copy8bitRGBA(f + 8, image_buffer, image_width * image_height, palette_complete);
569 static qbyte *LoadLMPRGBA (const qbyte *f, int matchwidth, int matchheight)
571 return LoadLMP(f, matchwidth, matchheight, false);
574 qbyte *LoadLMPAs8Bit (const qbyte *f, int matchwidth, int matchheight)
576 return LoadLMP(f, matchwidth, matchheight, true);
583 unsigned width, height;
584 unsigned offsets[MIPLEVELS]; // four mip maps stored
585 char animname[32]; // next frame in animation chain
591 qbyte *LoadWAL (const qbyte *f, int matchwidth, int matchheight)
594 const q2wal_t *inwal = (const void *)f;
596 if (fs_filesize < (int) sizeof(q2wal_t))
598 Con_Print("LoadWAL: invalid WAL file\n");
602 image_width = LittleLong(inwal->width);
603 image_height = LittleLong(inwal->height);
604 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
606 Con_Printf("LoadWAL: invalid size %ix%i\n", image_width, image_height);
609 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
612 if ((int) fs_filesize < (int) sizeof(q2wal_t) + (int) LittleLong(inwal->offsets[0]) + image_width * image_height)
614 Con_Print("LoadWAL: invalid WAL file\n");
618 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
621 Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
624 Image_Copy8bitRGBA(f + LittleLong(inwal->offsets[0]), image_rgba, image_width * image_height, palette_complete);
629 void Image_StripImageExtension (const char *in, char *out)
631 const char *end, *temp;
632 end = in + strlen(in);
636 if (strcmp(temp, ".tga") == 0
637 || strcmp(temp, ".pcx") == 0
638 || strcmp(temp, ".lmp") == 0
639 || strcmp(temp, ".png") == 0
640 || strcmp(temp, ".jpg") == 0)
652 const char *formatstring;
653 qbyte *(*loadfunc)(const qbyte *f, int matchwidth, int matchheight);
657 {"override/%s.tga", LoadTGA},
658 {"override/%s.jpg", JPEG_LoadImage},
659 {"textures/%s.tga", LoadTGA},
660 {"textures/%s.jpg", JPEG_LoadImage},
661 {"textures/%s.pcx", LoadPCX},
662 {"textures/%s.wal", LoadWAL},
664 {"%s.jpg", JPEG_LoadImage},
666 {"%s.lmp", LoadLMPRGBA},
670 qbyte *loadimagepixels (const char *filename, qboolean complain, int matchwidth, int matchheight)
673 qbyte *f, *data = NULL;
674 char basename[MAX_QPATH], name[MAX_QPATH], *c;
675 if (developer_memorydebug.integer)
676 Mem_CheckSentinelsGlobal();
677 Image_StripImageExtension(filename, basename); // strip filename extensions to allow replacement by other types
678 // replace *'s with #, so commandline utils don't get confused when dealing with the external files
679 for (c = basename;*c;c++)
682 for (i = 0;imageformats[i].formatstring;i++)
684 sprintf (name, imageformats[i].formatstring, basename);
685 f = FS_LoadFile(name, tempmempool, true);
688 data = imageformats[i].loadfunc(f, matchwidth, matchheight);
692 Con_DPrintf("loaded image %s (%dx%d)\n", name, image_width, image_height);
693 if (developer_memorydebug.integer)
694 Mem_CheckSentinelsGlobal();
701 Con_Printf("Couldn't load %s using ", filename);
702 for (i = 0;imageformats[i].formatstring;i++)
704 sprintf (name, imageformats[i].formatstring, basename);
705 Con_Printf(i == 0 ? "\"%s\"" : (imageformats[i+1].formatstring ? ", \"%s\"" : " or \"%s\".\n"), imageformats[i].formatstring);
708 if (developer_memorydebug.integer)
709 Mem_CheckSentinelsGlobal();
713 int image_makemask (const qbyte *in, qbyte *out, int size)
717 for (i = 0;i < size;i++)
719 out[0] = out[1] = out[2] = 255;
729 qbyte* loadimagepixelsmask (const char *filename, qboolean complain, int matchwidth, int matchheight)
732 in = data = loadimagepixels(filename, complain, matchwidth, matchheight);
735 if (image_makemask(data, data, image_width * image_height))
736 return data; // some transparency
740 return NULL; // all opaque
744 rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
748 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
750 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
755 rtexture_t *loadtextureimagemask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
759 if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
761 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
766 rtexture_t *image_masktex;
767 rtexture_t *image_nmaptex;
768 rtexture_t *loadtextureimagewithmask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
772 image_masktex = NULL;
773 image_nmaptex = NULL;
774 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
777 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
779 if (flags & TEXF_ALPHA && image_makemask(data, data, image_width * image_height))
780 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
786 rtexture_t *loadtextureimagewithmaskandnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
790 image_masktex = NULL;
791 image_nmaptex = NULL;
792 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
795 data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
797 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
799 Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
800 image_nmaptex = R_LoadTexture2D(pool, va("%s_nmap", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
802 if (flags & TEXF_ALPHA && image_makemask(data, data2, image_width * image_height))
803 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
811 rtexture_t *loadtextureimagebumpasnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
815 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
817 data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
819 Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
820 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
827 qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int height, const qbyte *data, qbyte *buffer)
831 const qbyte *in, *end;
833 memset (buffer, 0, 18);
834 buffer[2] = 2; // uncompressed type
835 buffer[12] = (width >> 0) & 0xFF;
836 buffer[13] = (width >> 8) & 0xFF;
837 buffer[14] = (height >> 0) & 0xFF;
838 buffer[15] = (height >> 8) & 0xFF;
839 buffer[16] = 24; // pixel size
844 end = in + width*height*3;
845 for (;in < end;in += 3)
851 ret = FS_WriteFile (filename, buffer, width*height*3 + 18 );
856 void Image_WriteTGARGB (const char *filename, int width, int height, const qbyte *data)
860 const qbyte *in, *end;
862 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
864 memset (buffer, 0, 18);
865 buffer[2] = 2; // uncompressed type
866 buffer[12] = (width >> 0) & 0xFF;
867 buffer[13] = (width >> 8) & 0xFF;
868 buffer[14] = (height >> 0) & 0xFF;
869 buffer[15] = (height >> 8) & 0xFF;
870 buffer[16] = 24; // pixel size
872 // swap rgb to bgr and flip upside down
874 for (y = height - 1;y >= 0;y--)
876 in = data + y * width * 3;
877 end = in + width * 3;
878 for (;in < end;in += 3)
885 FS_WriteFile (filename, buffer, width*height*3 + 18 );
890 void Image_WriteTGARGBA (const char *filename, int width, int height, const qbyte *data)
894 const qbyte *in, *end;
896 buffer = Mem_Alloc(tempmempool, width*height*4 + 18);
898 memset (buffer, 0, 18);
899 buffer[2] = 2; // uncompressed type
900 buffer[12] = (width >> 0) & 0xFF;
901 buffer[13] = (width >> 8) & 0xFF;
902 buffer[14] = (height >> 0) & 0xFF;
903 buffer[15] = (height >> 8) & 0xFF;
904 buffer[16] = 32; // pixel size
905 buffer[17] = 8; // transparent flag? (seems to be needed by gimp)
907 // swap rgba to bgra and flip upside down
909 for (y = height - 1;y >= 0;y--)
911 in = data + y * width * 4;
912 end = in + width * 4;
913 for (;in < end;in += 4)
921 FS_WriteFile (filename, buffer, width*height*4 + 18 );
926 qboolean Image_CheckAlpha(const qbyte *data, int size, qboolean rgba)
932 for (end = data + size * 4, data += 3;data < end;data += 4)
938 // color 255 is transparent
939 for (end = data + size;data < end;data++)
946 static void Image_Resample32LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
948 int j, xi, oldx = 0, f, fstep, endx, lerp;
949 fstep = (int) (inwidth*65536.0f/outwidth);
951 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
956 in += (xi - oldx) * 4;
962 *out++ = (qbyte) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
963 *out++ = (qbyte) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
964 *out++ = (qbyte) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
965 *out++ = (qbyte) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
967 else // last pixel of the line has no pixel to lerp to
977 static void Image_Resample24LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
979 int j, xi, oldx = 0, f, fstep, endx, lerp;
980 fstep = (int) (inwidth*65536.0f/outwidth);
982 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
987 in += (xi - oldx) * 3;
993 *out++ = (qbyte) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
994 *out++ = (qbyte) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
995 *out++ = (qbyte) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
997 else // last pixel of the line has no pixel to lerp to
1006 int resamplerowsize = 0;
1007 qbyte *resamplerow1 = NULL;
1008 qbyte *resamplerow2 = NULL;
1009 mempool_t *resamplemempool = NULL;
1011 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (qbyte) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
1012 void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1014 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
1018 fstep = (int) (inheight*65536.0f/outheight);
1022 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1023 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1024 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1032 inrow = (qbyte *)indata + inwidth4*yi;
1034 memcpy(resamplerow1, resamplerow2, outwidth4);
1036 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1037 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1088 resamplerow1 -= outwidth4;
1089 resamplerow2 -= outwidth4;
1095 inrow = (qbyte *)indata + inwidth4*yi;
1097 memcpy(resamplerow1, resamplerow2, outwidth4);
1099 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1102 memcpy(out, resamplerow1, outwidth4);
1107 void Image_Resample32Nearest(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1110 unsigned frac, fracstep;
1111 // relies on int being 4 bytes
1115 fracstep = inwidth*0x10000/outwidth;
1116 for (i = 0;i < outheight;i++)
1118 inrow = (int *)indata + inwidth*(i*inheight/outheight);
1119 frac = fracstep >> 1;
1123 out[0] = inrow[frac >> 16];frac += fracstep;
1124 out[1] = inrow[frac >> 16];frac += fracstep;
1125 out[2] = inrow[frac >> 16];frac += fracstep;
1126 out[3] = inrow[frac >> 16];frac += fracstep;
1132 out[0] = inrow[frac >> 16];frac += fracstep;
1133 out[1] = inrow[frac >> 16];frac += fracstep;
1138 out[0] = inrow[frac >> 16];frac += fracstep;
1144 void Image_Resample24Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1146 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
1150 fstep = (int) (inheight*65536.0f/outheight);
1154 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1155 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1156 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1164 inrow = (qbyte *)indata + inwidth3*yi;
1166 memcpy(resamplerow1, resamplerow2, outwidth3);
1168 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1169 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1213 resamplerow1 -= outwidth3;
1214 resamplerow2 -= outwidth3;
1220 inrow = (qbyte *)indata + inwidth3*yi;
1222 memcpy(resamplerow1, resamplerow2, outwidth3);
1224 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1227 memcpy(out, resamplerow1, outwidth3);
1232 void Image_Resample24Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1234 int i, j, f, inwidth3 = inwidth * 3;
1235 unsigned frac, fracstep;
1239 fracstep = inwidth*0x10000/outwidth;
1240 for (i = 0;i < outheight;i++)
1242 inrow = (qbyte *)indata + inwidth3*(i*inheight/outheight);
1243 frac = fracstep >> 1;
1247 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1248 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1249 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1250 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1255 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1256 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1261 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1272 void Image_Resample (const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int bytesperpixel, int quality)
1274 if (indepth != 1 || outdepth != 1)
1275 Sys_Error("Image_Resample: 3D resampling not supported\n");
1276 if (resamplerowsize < outwidth*4)
1279 Mem_Free(resamplerow1);
1280 resamplerowsize = outwidth*4;
1281 if (!resamplemempool)
1282 resamplemempool = Mem_AllocPool("Image Scaling Buffer", 0, NULL);
1283 resamplerow1 = Mem_Alloc(resamplemempool, resamplerowsize*2);
1284 resamplerow2 = resamplerow1 + resamplerowsize;
1286 if (bytesperpixel == 4)
1289 Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1291 Image_Resample32Nearest(indata, inwidth, inheight, outdata, outwidth, outheight);
1293 else if (bytesperpixel == 3)
1296 Image_Resample24Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1298 Image_Resample24Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1301 Sys_Error("Image_Resample: unsupported bytesperpixel %i\n", bytesperpixel);
1304 // in can be the same as out
1305 void Image_MipReduce(const qbyte *in, qbyte *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth, int bytesperpixel)
1308 if (*depth != 1 || destdepth != 1)
1309 Sys_Error("Image_Resample: 3D resampling not supported\n");
1310 nextrow = *width * bytesperpixel;
1311 if (*width > destwidth)
1314 if (*height > destheight)
1318 if (bytesperpixel == 4)
1320 for (y = 0;y < *height;y++)
1322 for (x = 0;x < *width;x++)
1324 out[0] = (qbyte) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
1325 out[1] = (qbyte) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1326 out[2] = (qbyte) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1327 out[3] = (qbyte) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1331 in += nextrow; // skip a line
1334 else if (bytesperpixel == 3)
1336 for (y = 0;y < *height;y++)
1338 for (x = 0;x < *width;x++)
1340 out[0] = (qbyte) ((in[0] + in[3] + in[nextrow ] + in[nextrow+3]) >> 2);
1341 out[1] = (qbyte) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
1342 out[2] = (qbyte) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
1346 in += nextrow; // skip a line
1350 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1355 if (bytesperpixel == 4)
1357 for (y = 0;y < *height;y++)
1359 for (x = 0;x < *width;x++)
1361 out[0] = (qbyte) ((in[0] + in[4]) >> 1);
1362 out[1] = (qbyte) ((in[1] + in[5]) >> 1);
1363 out[2] = (qbyte) ((in[2] + in[6]) >> 1);
1364 out[3] = (qbyte) ((in[3] + in[7]) >> 1);
1370 else if (bytesperpixel == 3)
1372 for (y = 0;y < *height;y++)
1374 for (x = 0;x < *width;x++)
1376 out[0] = (qbyte) ((in[0] + in[3]) >> 1);
1377 out[1] = (qbyte) ((in[1] + in[4]) >> 1);
1378 out[2] = (qbyte) ((in[2] + in[5]) >> 1);
1385 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1390 if (*height > destheight)
1394 if (bytesperpixel == 4)
1396 for (y = 0;y < *height;y++)
1398 for (x = 0;x < *width;x++)
1400 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1401 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1402 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1403 out[3] = (qbyte) ((in[3] + in[nextrow+3]) >> 1);
1407 in += nextrow; // skip a line
1410 else if (bytesperpixel == 3)
1412 for (y = 0;y < *height;y++)
1414 for (x = 0;x < *width;x++)
1416 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1417 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1418 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1422 in += nextrow; // skip a line
1426 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1429 Sys_Error("Image_MipReduce: desired size already achieved\n");
1433 void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
1436 const unsigned char *p0, *p1, *p2;
1438 float iwidth, iheight, ibumpscale, n[3];
1439 iwidth = 1.0f / width;
1440 iheight = 1.0f / height;
1441 ibumpscale = (255.0f * 3.0f) / bumpscale;
1443 for (y = 0;y < height;y++)
1445 for (x = 0;x < width;x++)
1447 p0 = inpixels + (y * width + x) * 4;
1451 p1 = inpixels + (y * width + x) * 4;
1453 p1 = inpixels + (y * width) * 4;
1456 p1 = inpixels + (y * width + x + 1) * 4;
1457 if (y == height - 1)
1460 p2 = inpixels + (y * width + x) * 4;
1462 p2 = inpixels + x * 4;
1465 p2 = inpixels + ((y + 1) * width + x) * 4;
1469 dv[0][2] = ((p0[0] + p0[1] + p0[2]) * ibumpscale) - ((p1[0] + p1[1] + p1[2]) * ibumpscale);
1472 dv[1][2] = ((p2[0] + p2[1] + p2[2]) * ibumpscale) - ((p0[0] + p0[1] + p0[2]) * ibumpscale);
1473 n[0] = dv[0][1]*dv[1][2]-dv[0][2]*dv[1][1];
1474 n[1] = dv[0][2]*dv[1][0]-dv[0][0]*dv[1][2];
1475 n[2] = dv[0][0]*dv[1][1]-dv[0][1]*dv[1][0];
1477 n[0] = ((p1[0] + p1[1] + p1[2]) - (p0[0] + p0[1] + p0[2]));
1478 n[1] = ((p0[0] + p0[1] + p0[2]) - (p2[0] + p2[1] + p2[2]));
1482 // this should work for the bottom right triangle if anyone wants
1483 // code for that for some reason
1484 n[0] = ((p3[0] + p3[1] + p3[2]) - (p1[0] + p1[1] + p1[2]));
1485 n[1] = ((p2[0] + p2[1] + p2[2]) - (p3[0] + p3[1] + p3[2]));
1489 out[0] = 128.0f + n[0] * 127.0f;
1490 out[1] = 128.0f + n[1] * 127.0f;
1491 out[2] = 128.0f + n[2] * 127.0f;
1492 out[3] = (p0[0] + p0[1] + p0[2]) / 3;
1498 int image_loadskin(imageskin_t *s, char *shadername)
1502 int bumppixels_width, bumppixels_height;
1503 char name[MAX_QPATH];
1504 Image_StripImageExtension(shadername, name);
1505 memset(s, 0, sizeof(*s));
1506 s->basepixels = loadimagepixels(name, false, 0, 0);
1507 if (s->basepixels == NULL)
1509 s->basepixels_width = image_width;
1510 s->basepixels_height = image_height;
1512 bumppixels = NULL;bumppixels_width = 0;bumppixels_height = 0;
1513 if (Image_CheckAlpha(s->basepixels, s->basepixels_width * s->basepixels_height, true))
1515 s->maskpixels = Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1516 s->maskpixels_width = s->basepixels_width;
1517 s->maskpixels_height = s->basepixels_height;
1518 memcpy(s->maskpixels, s->basepixels, s->maskpixels_width * s->maskpixels_height * 4);
1519 for (j = 0;j < s->basepixels_width * s->basepixels_height * 4;j += 4)
1521 s->maskpixels[j+0] = 255;
1522 s->maskpixels[j+1] = 255;
1523 s->maskpixels[j+2] = 255;
1527 // _luma is supported for tenebrae compatibility
1528 // (I think it's a very stupid name, but oh well)
1529 if ((s->glowpixels = loadimagepixels(va("%s_glow", name), false, 0, 0)) != NULL
1530 || (s->glowpixels = loadimagepixels(va("%s_luma", name), false, 0, 0)) != NULL)
1532 s->glowpixels_width = image_width;
1533 s->glowpixels_height = image_height;
1535 // _norm is the name used by tenebrae
1536 // (I don't like the name much)
1537 if ((s->nmappixels = loadimagepixels(va("%s_norm", name), false, 0, 0)) != NULL)
1539 s->nmappixels_width = image_width;
1540 s->nmappixels_height = image_height;
1542 else if ((bumppixels = loadimagepixels(va("%s_bump", name), false, 0, 0)) != NULL)
1544 bumppixels_width = image_width;
1545 bumppixels_height = image_height;
1547 if ((s->glosspixels = loadimagepixels(va("%s_gloss", name), false, 0, 0)) != NULL)
1549 s->glosspixels_width = image_width;
1550 s->glosspixels_height = image_height;
1552 if ((s->pantspixels = loadimagepixels(va("%s_pants", name), false, 0, 0)) != NULL)
1554 s->pantspixels_width = image_width;
1555 s->pantspixels_height = image_height;
1557 if ((s->shirtpixels = loadimagepixels(va("%s_shirt", name), false, 0, 0)) != NULL)
1559 s->shirtpixels_width = image_width;
1560 s->shirtpixels_height = image_height;
1563 if (s->nmappixels == NULL)
1565 if (bumppixels != NULL)
1567 if (r_shadow_bumpscale_bumpmap.value > 0)
1569 s->nmappixels = Mem_Alloc(loadmodel->mempool, bumppixels_width * bumppixels_height * 4);
1570 s->nmappixels_width = bumppixels_width;
1571 s->nmappixels_height = bumppixels_height;
1572 Image_HeightmapToNormalmap(bumppixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_bumpmap.value);
1577 if (r_shadow_bumpscale_basetexture.value > 0)
1579 s->nmappixels = Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1580 s->nmappixels_width = s->basepixels_width;
1581 s->nmappixels_height = s->basepixels_height;
1582 Image_HeightmapToNormalmap(s->basepixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_basetexture.value);
1586 if (bumppixels != NULL)
1587 Mem_Free(bumppixels);
1591 void image_freeskin(imageskin_t *s)
1594 Mem_Free(s->basepixels);
1596 Mem_Free(s->maskpixels);
1598 Mem_Free(s->nmappixels);
1600 Mem_Free(s->glowpixels);
1602 Mem_Free(s->glosspixels);
1604 Mem_Free(s->pantspixels);
1606 Mem_Free(s->shirtpixels);
1607 memset(s, 0, sizeof(*s));