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(unsigned char *outpixels, const unsigned char *inpixels, int inputwidth, int inputheight, qboolean inputflipx, qboolean inputflipy, qboolean inputflipdiagonal, int numoutputcomponents, int numinputcomponents, int *outputinputcomponentindices)
15 const unsigned char *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(unsigned char *outpixels, const unsigned char *inpixels, int inputwidth, int inputheight, qboolean inputflipx, qboolean inputflipy, qboolean inputflipdiagonal, int numoutputcomponents, int numinputcomponents, int *outputinputcomponentindices)
64 const unsigned char *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 unsigned char *in, unsigned char *out, int pixels, const unsigned char *gammar, const unsigned char *gammag, const unsigned char *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 unsigned char *in, unsigned char *out, int pixels, const unsigned int *pal)
119 int *iout = (int *)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 unsigned char* LoadPCX (const unsigned char *f, int filesize, int matchwidth, int matchheight)
186 unsigned char *a, *b, *image_rgba, *pbuf;
187 const unsigned char *palette, *fin, *enddata;
188 int x, y, x2, dataByte;
190 if (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 + filesize - 768;
223 image_rgba = (unsigned char *)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_Printf("TargaHeader:\nuint8 id_length = %i;\nuint8 colormap_type = %i;\nuint8 image_type = %i;\nuint16 colormap_index = %i;\nuint16 colormap_length = %i;\nuint8 colormap_size = %i;\nuint16 x_origin = %i;\nuint16 y_origin = %i;\nuint16 width = %i;\nuint16 height = %i;\nuint8 pixel_size = %i;\nuint8 attributes = %i;\n", t->id_length, t->colormap_type, t->image_type, t->colormap_index, t->colormap_length, t->colormap_size, t->x_origin, t->y_origin, t->width, t->height, t->pixel_size, t->attributes);
300 unsigned char *LoadTGA (const unsigned char *f, int filesize, int matchwidth, int matchheight)
302 int x, y, row_inc, compressed, readpixelcount, red, green, blue, alpha, runlen, pindex, alphabits;
303 unsigned char *pixbuf, *image_rgba;
304 const unsigned char *fin, *enddata;
306 TargaHeader targa_header;
307 unsigned char palette[256*4];
312 enddata = f + filesize;
314 targa_header.id_length = f[0];
315 targa_header.colormap_type = f[1];
316 targa_header.image_type = f[2];
318 targa_header.colormap_index = f[3] + f[4] * 256;
319 targa_header.colormap_length = f[5] + f[6] * 256;
320 targa_header.colormap_size = f[7];
321 targa_header.x_origin = f[8] + f[9] * 256;
322 targa_header.y_origin = f[10] + f[11] * 256;
323 targa_header.width = image_width = f[12] + f[13] * 256;
324 targa_header.height = image_height = f[14] + f[15] * 256;
325 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
327 Con_Print("LoadTGA: invalid size\n");
328 PrintTargaHeader(&targa_header);
331 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
333 targa_header.pixel_size = f[16];
334 targa_header.attributes = f[17];
336 // advance to end of header
339 // skip TARGA image comment (usually 0 bytes)
340 fin += targa_header.id_length;
342 // read/skip the colormap if present (note: according to the TARGA spec it
343 // can be present even on truecolor or greyscale images, just not used by
345 if (targa_header.colormap_type)
347 if (targa_header.colormap_length > 256)
349 Con_Print("LoadTGA: only up to 256 colormap_length supported\n");
350 PrintTargaHeader(&targa_header);
353 if (targa_header.colormap_index)
355 Con_Print("LoadTGA: colormap_index not supported\n");
356 PrintTargaHeader(&targa_header);
359 if (targa_header.colormap_size == 24)
361 for (x = 0;x < targa_header.colormap_length;x++)
363 palette[x*4+2] = *fin++;
364 palette[x*4+1] = *fin++;
365 palette[x*4+0] = *fin++;
366 palette[x*4+3] = 255;
369 else if (targa_header.colormap_size == 32)
371 for (x = 0;x < targa_header.colormap_length;x++)
373 palette[x*4+2] = *fin++;
374 palette[x*4+1] = *fin++;
375 palette[x*4+0] = *fin++;
376 palette[x*4+3] = *fin++;
381 Con_Print("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
382 PrintTargaHeader(&targa_header);
387 // check our pixel_size restrictions according to image_type
388 if (targa_header.image_type == 2 || targa_header.image_type == 10)
390 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
392 Con_Print("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
393 PrintTargaHeader(&targa_header);
397 else if (targa_header.image_type == 1 || targa_header.image_type == 9)
399 if (targa_header.pixel_size != 8)
401 Con_Print("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images 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 // number of attribute bits per pixel, we only support 0 or 8
429 alphabits = targa_header.attributes & 0x0F;
430 if (alphabits != 8 && alphabits != 0)
432 Con_Print("LoadTGA: only 0 or 8 attribute (alpha) bits supported\n");
436 image_rgba = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
439 Con_Printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
443 // If bit 5 of attributes isn't set, the image has been stored from bottom to top
444 if ((targa_header.attributes & 0x20) == 0)
446 pixbuf = image_rgba + (image_height - 1)*image_width*4;
447 row_inc = -image_width*4*2;
455 compressed = targa_header.image_type == 9 || targa_header.image_type == 10 || targa_header.image_type == 11;
458 red = green = blue = alpha = 255;
459 while (y < image_height)
461 // decoder is mostly the same whether it's compressed or not
462 readpixelcount = 1000000;
464 if (compressed && fin < enddata)
467 // high bit indicates this is an RLE compressed run
470 runlen = 1 + (runlen & 0x7f);
473 while((runlen--) && y < image_height)
475 if (readpixelcount > 0)
478 red = green = blue = alpha = 255;
481 switch(targa_header.image_type)
487 if (pindex >= targa_header.colormap_length)
489 p = palette + pindex * 4;
503 if (targa_header.pixel_size == 32 && fin < enddata)
509 red = green = blue = *fin++;
521 if (x == image_width)
523 // end of line, advance to next
539 unsigned char *LoadLMP (const unsigned char *f, int filesize, int matchwidth, int matchheight, qboolean loadAs8Bit)
541 unsigned char *image_buffer;
545 Con_Print("LoadLMP: invalid LMP file\n");
549 // parse the very complicated header *chuckle*
550 image_width = BuffLittleLong(f);
551 image_height = BuffLittleLong(f + 4);
552 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
554 Con_Printf("LoadLMP: invalid size %ix%i\n", image_width, image_height);
557 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
560 if (filesize < (8 + image_width * image_height))
562 Con_Print("LoadLMP: invalid LMP file\n");
568 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height);
569 memcpy(image_buffer, f + 8, image_width * image_height);
573 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
574 Image_Copy8bitRGBA(f + 8, image_buffer, image_width * image_height, palette_transparent);
579 static unsigned char *LoadLMPRGBA (const unsigned char *f, int filesize, int matchwidth, int matchheight)
581 return LoadLMP(f, filesize, matchwidth, matchheight, false);
585 typedef struct q2wal_s
588 unsigned width, height;
589 unsigned offsets[MIPLEVELS]; // four mip maps stored
590 char animname[32]; // next frame in animation chain
596 unsigned char *LoadWAL (const unsigned char *f, int filesize, int matchwidth, int matchheight)
598 unsigned char *image_rgba;
599 const q2wal_t *inwal = (const q2wal_t *)f;
601 if (filesize < (int) sizeof(q2wal_t))
603 Con_Print("LoadWAL: invalid WAL file\n");
607 image_width = LittleLong(inwal->width);
608 image_height = LittleLong(inwal->height);
609 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
611 Con_Printf("LoadWAL: invalid size %ix%i\n", image_width, image_height);
614 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
617 if (filesize < (int) sizeof(q2wal_t) + (int) LittleLong(inwal->offsets[0]) + image_width * image_height)
619 Con_Print("LoadWAL: invalid WAL file\n");
623 image_rgba = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
626 Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
629 Image_Copy8bitRGBA(f + LittleLong(inwal->offsets[0]), image_rgba, image_width * image_height, palette_complete);
634 void Image_StripImageExtension (const char *in, char *out)
636 const char *end, *temp;
637 end = in + strlen(in);
641 if (strcmp(temp, ".tga") == 0
642 || strcmp(temp, ".pcx") == 0
643 || strcmp(temp, ".lmp") == 0
644 || strcmp(temp, ".png") == 0
645 || strcmp(temp, ".jpg") == 0)
657 const char *formatstring;
658 unsigned char *(*loadfunc)(const unsigned char *f, int filesize, int matchwidth, int matchheight);
662 {"override/%s.tga", LoadTGA},
663 {"override/%s.jpg", JPEG_LoadImage},
664 {"textures/%s.tga", LoadTGA},
665 {"textures/%s.jpg", JPEG_LoadImage},
666 {"textures/%s.pcx", LoadPCX},
667 {"textures/%s.wal", LoadWAL},
669 {"%s.jpg", JPEG_LoadImage},
671 {"%s.lmp", LoadLMPRGBA},
675 unsigned char *loadimagepixels (const char *filename, qboolean complain, int matchwidth, int matchheight)
678 fs_offset_t filesize;
679 unsigned char *f, *data = NULL;
680 char basename[MAX_QPATH], name[MAX_QPATH], *c;
681 if (developer_memorydebug.integer)
682 Mem_CheckSentinelsGlobal();
683 if (developer_texturelogging.integer)
684 Log_Printf("textures.log", "%s\n", filename);
685 strlcpy(basename, filename, sizeof(basename));
686 Image_StripImageExtension(basename, basename); // strip filename extensions to allow replacement by other types
687 // replace *'s with #, so commandline utils don't get confused when dealing with the external files
688 for (c = basename;*c;c++)
691 for (i = 0;imageformats[i].formatstring;i++)
693 sprintf (name, imageformats[i].formatstring, basename);
694 f = FS_LoadFile(name, tempmempool, true, &filesize);
697 data = imageformats[i].loadfunc(f, filesize, matchwidth, matchheight);
701 Con_DPrintf("loaded image %s (%dx%d)\n", name, image_width, image_height);
702 if (developer_memorydebug.integer)
703 Mem_CheckSentinelsGlobal();
710 Con_Printf("Couldn't load %s using ", filename);
711 for (i = 0;imageformats[i].formatstring;i++)
713 sprintf (name, imageformats[i].formatstring, basename);
714 Con_Printf(i == 0 ? "\"%s\"" : (imageformats[i+1].formatstring ? ", \"%s\"" : " or \"%s\".\n"), imageformats[i].formatstring);
717 if (developer_memorydebug.integer)
718 Mem_CheckSentinelsGlobal();
722 int image_makemask (const unsigned char *in, unsigned char *out, int size)
726 for (i = 0;i < size;i++)
728 out[0] = out[1] = out[2] = 255;
738 unsigned char* loadimagepixelsmask (const char *filename, qboolean complain, int matchwidth, int matchheight)
740 unsigned char *in, *data;
741 in = data = loadimagepixels(filename, complain, matchwidth, matchheight);
744 if (image_makemask(data, data, image_width * image_height))
745 return data; // some transparency
749 return NULL; // all opaque
753 rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
757 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
759 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
764 rtexture_t *loadtextureimagemask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
768 if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
770 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
775 rtexture_t *image_masktex;
776 rtexture_t *image_nmaptex;
777 rtexture_t *loadtextureimagewithmask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
781 image_masktex = NULL;
782 image_nmaptex = NULL;
783 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
786 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
788 if (flags & TEXF_ALPHA && image_makemask(data, data, image_width * image_height))
789 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
795 rtexture_t *loadtextureimagewithmaskandnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
797 unsigned char *data, *data2;
799 image_masktex = NULL;
800 image_nmaptex = NULL;
801 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
804 data2 = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
806 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
808 Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
809 image_nmaptex = R_LoadTexture2D(pool, va("%s_nmap", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
811 if (flags & TEXF_ALPHA && image_makemask(data, data2, image_width * image_height))
812 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
820 rtexture_t *loadtextureimagebumpasnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
822 unsigned char *data, *data2;
824 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
826 data2 = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
828 Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
829 rt = R_LoadTexture2D(pool, filename, image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
836 qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int height, const unsigned char *data, unsigned char *buffer)
840 const unsigned char *in, *end;
842 memset (buffer, 0, 18);
843 buffer[2] = 2; // uncompressed type
844 buffer[12] = (width >> 0) & 0xFF;
845 buffer[13] = (width >> 8) & 0xFF;
846 buffer[14] = (height >> 0) & 0xFF;
847 buffer[15] = (height >> 8) & 0xFF;
848 buffer[16] = 24; // pixel size
853 end = in + width*height*3;
854 for (;in < end;in += 3)
860 ret = FS_WriteFile (filename, buffer, width*height*3 + 18 );
865 void Image_WriteTGARGB (const char *filename, int width, int height, const unsigned char *data)
868 unsigned char *buffer, *out;
869 const unsigned char *in, *end;
871 buffer = (unsigned char *)Mem_Alloc(tempmempool, width*height*3 + 18);
873 memset (buffer, 0, 18);
874 buffer[2] = 2; // uncompressed type
875 buffer[12] = (width >> 0) & 0xFF;
876 buffer[13] = (width >> 8) & 0xFF;
877 buffer[14] = (height >> 0) & 0xFF;
878 buffer[15] = (height >> 8) & 0xFF;
879 buffer[16] = 24; // pixel size
881 // swap rgb to bgr and flip upside down
883 for (y = height - 1;y >= 0;y--)
885 in = data + y * width * 3;
886 end = in + width * 3;
887 for (;in < end;in += 3)
894 FS_WriteFile (filename, buffer, width*height*3 + 18 );
899 void Image_WriteTGARGBA (const char *filename, int width, int height, const unsigned char *data)
902 unsigned char *buffer, *out;
903 const unsigned char *in, *end;
905 buffer = (unsigned char *)Mem_Alloc(tempmempool, width*height*4 + 18);
907 memset (buffer, 0, 18);
908 buffer[2] = 2; // uncompressed type
909 buffer[12] = (width >> 0) & 0xFF;
910 buffer[13] = (width >> 8) & 0xFF;
911 buffer[14] = (height >> 0) & 0xFF;
912 buffer[15] = (height >> 8) & 0xFF;
913 buffer[16] = 32; // pixel size
914 buffer[17] = 8; // transparent flag? (seems to be needed by gimp)
916 // swap rgba to bgra and flip upside down
918 for (y = height - 1;y >= 0;y--)
920 in = data + y * width * 4;
921 end = in + width * 4;
922 for (;in < end;in += 4)
930 FS_WriteFile (filename, buffer, width*height*4 + 18 );
935 qboolean Image_CheckAlpha(const unsigned char *data, int size, qboolean rgba)
937 const unsigned char *end;
941 for (end = data + size * 4, data += 3;data < end;data += 4)
947 // color 255 is transparent
948 for (end = data + size;data < end;data++)
955 static void Image_Resample32LerpLine (const unsigned char *in, unsigned char *out, int inwidth, int outwidth)
957 int j, xi, oldx = 0, f, fstep, endx, lerp;
958 fstep = (int) (inwidth*65536.0f/outwidth);
960 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
965 in += (xi - oldx) * 4;
971 *out++ = (unsigned char) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
972 *out++ = (unsigned char) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
973 *out++ = (unsigned char) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
974 *out++ = (unsigned char) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
976 else // last pixel of the line has no pixel to lerp to
986 static void Image_Resample24LerpLine (const unsigned char *in, unsigned char *out, int inwidth, int outwidth)
988 int j, xi, oldx = 0, f, fstep, endx, lerp;
989 fstep = (int) (inwidth*65536.0f/outwidth);
991 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
996 in += (xi - oldx) * 3;
1002 *out++ = (unsigned char) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
1003 *out++ = (unsigned char) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
1004 *out++ = (unsigned char) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
1006 else // last pixel of the line has no pixel to lerp to
1015 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (unsigned char) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
1016 void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1018 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
1020 const unsigned char *inrow;
1021 unsigned char *resamplerow1;
1022 unsigned char *resamplerow2;
1023 out = (unsigned char *)outdata;
1024 fstep = (int) (inheight*65536.0f/outheight);
1026 resamplerow1 = (unsigned char *)Mem_Alloc(tempmempool, outwidth*4*2);
1027 resamplerow2 = resamplerow1 + outwidth*4;
1029 inrow = (const unsigned char *)indata;
1031 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1032 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1033 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1041 inrow = (unsigned char *)indata + inwidth4*yi;
1043 memcpy(resamplerow1, resamplerow2, outwidth4);
1045 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1046 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1097 resamplerow1 -= outwidth4;
1098 resamplerow2 -= outwidth4;
1104 inrow = (unsigned char *)indata + inwidth4*yi;
1106 memcpy(resamplerow1, resamplerow2, outwidth4);
1108 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1111 memcpy(out, resamplerow1, outwidth4);
1115 Mem_Free(resamplerow1);
1116 resamplerow1 = NULL;
1117 resamplerow2 = NULL;
1120 void Image_Resample32Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1123 unsigned frac, fracstep;
1124 // relies on int being 4 bytes
1126 out = (int *)outdata;
1128 fracstep = inwidth*0x10000/outwidth;
1129 for (i = 0;i < outheight;i++)
1131 inrow = (int *)indata + inwidth*(i*inheight/outheight);
1132 frac = fracstep >> 1;
1136 out[0] = inrow[frac >> 16];frac += fracstep;
1137 out[1] = inrow[frac >> 16];frac += fracstep;
1138 out[2] = inrow[frac >> 16];frac += fracstep;
1139 out[3] = inrow[frac >> 16];frac += fracstep;
1145 out[0] = inrow[frac >> 16];frac += fracstep;
1146 out[1] = inrow[frac >> 16];frac += fracstep;
1151 out[0] = inrow[frac >> 16];frac += fracstep;
1157 void Image_Resample24Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1159 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
1161 const unsigned char *inrow;
1162 unsigned char *resamplerow1;
1163 unsigned char *resamplerow2;
1164 out = (unsigned char *)outdata;
1165 fstep = (int) (inheight*65536.0f/outheight);
1167 resamplerow1 = (unsigned char *)Mem_Alloc(tempmempool, outwidth*3*2);
1168 resamplerow2 = resamplerow1 + outwidth*3;
1170 inrow = (const unsigned char *)indata;
1172 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1173 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1174 for (i = 0, f = 0;i < outheight;i++,f += fstep)
1182 inrow = (unsigned char *)indata + inwidth3*yi;
1184 memcpy(resamplerow1, resamplerow2, outwidth3);
1186 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1187 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1231 resamplerow1 -= outwidth3;
1232 resamplerow2 -= outwidth3;
1238 inrow = (unsigned char *)indata + inwidth3*yi;
1240 memcpy(resamplerow1, resamplerow2, outwidth3);
1242 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1245 memcpy(out, resamplerow1, outwidth3);
1248 Mem_Free(resamplerow1);
1249 resamplerow1 = NULL;
1250 resamplerow2 = NULL;
1253 void Image_Resample24Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1255 int i, j, f, inwidth3 = inwidth * 3;
1256 unsigned frac, fracstep;
1257 unsigned char *inrow, *out;
1258 out = (unsigned char *)outdata;
1260 fracstep = inwidth*0x10000/outwidth;
1261 for (i = 0;i < outheight;i++)
1263 inrow = (unsigned char *)indata + inwidth3*(i*inheight/outheight);
1264 frac = fracstep >> 1;
1268 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1269 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1270 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1271 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1276 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1277 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1282 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1293 void Image_Resample (const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int bytesperpixel, int quality)
1295 if (indepth != 1 || outdepth != 1)
1297 Con_Printf ("Image_Resample: 3D resampling not supported\n");
1300 if (bytesperpixel == 4)
1303 Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1305 Image_Resample32Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1307 else if (bytesperpixel == 3)
1310 Image_Resample24Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1312 Image_Resample24Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1315 Con_Printf ("Image_Resample: unsupported bytesperpixel %i\n", bytesperpixel);
1318 // in can be the same as out
1319 void Image_MipReduce(const unsigned char *in, unsigned char *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth, int bytesperpixel)
1322 if (*depth != 1 || destdepth != 1)
1324 Con_Printf ("Image_Resample: 3D resampling not supported\n");
1327 nextrow = *width * bytesperpixel;
1328 if (*width > destwidth)
1331 if (*height > destheight)
1335 if (bytesperpixel == 4)
1337 for (y = 0;y < *height;y++)
1339 for (x = 0;x < *width;x++)
1341 out[0] = (unsigned char) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
1342 out[1] = (unsigned char) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1343 out[2] = (unsigned char) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1344 out[3] = (unsigned char) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1348 in += nextrow; // skip a line
1351 else if (bytesperpixel == 3)
1353 for (y = 0;y < *height;y++)
1355 for (x = 0;x < *width;x++)
1357 out[0] = (unsigned char) ((in[0] + in[3] + in[nextrow ] + in[nextrow+3]) >> 2);
1358 out[1] = (unsigned char) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
1359 out[2] = (unsigned char) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
1363 in += nextrow; // skip a line
1367 Con_Printf ("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1372 if (bytesperpixel == 4)
1374 for (y = 0;y < *height;y++)
1376 for (x = 0;x < *width;x++)
1378 out[0] = (unsigned char) ((in[0] + in[4]) >> 1);
1379 out[1] = (unsigned char) ((in[1] + in[5]) >> 1);
1380 out[2] = (unsigned char) ((in[2] + in[6]) >> 1);
1381 out[3] = (unsigned char) ((in[3] + in[7]) >> 1);
1387 else if (bytesperpixel == 3)
1389 for (y = 0;y < *height;y++)
1391 for (x = 0;x < *width;x++)
1393 out[0] = (unsigned char) ((in[0] + in[3]) >> 1);
1394 out[1] = (unsigned char) ((in[1] + in[4]) >> 1);
1395 out[2] = (unsigned char) ((in[2] + in[5]) >> 1);
1402 Con_Printf ("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1407 if (*height > destheight)
1411 if (bytesperpixel == 4)
1413 for (y = 0;y < *height;y++)
1415 for (x = 0;x < *width;x++)
1417 out[0] = (unsigned char) ((in[0] + in[nextrow ]) >> 1);
1418 out[1] = (unsigned char) ((in[1] + in[nextrow+1]) >> 1);
1419 out[2] = (unsigned char) ((in[2] + in[nextrow+2]) >> 1);
1420 out[3] = (unsigned char) ((in[3] + in[nextrow+3]) >> 1);
1424 in += nextrow; // skip a line
1427 else if (bytesperpixel == 3)
1429 for (y = 0;y < *height;y++)
1431 for (x = 0;x < *width;x++)
1433 out[0] = (unsigned char) ((in[0] + in[nextrow ]) >> 1);
1434 out[1] = (unsigned char) ((in[1] + in[nextrow+1]) >> 1);
1435 out[2] = (unsigned char) ((in[2] + in[nextrow+2]) >> 1);
1439 in += nextrow; // skip a line
1443 Con_Printf ("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1446 Con_Printf ("Image_MipReduce: desired size already achieved\n");
1450 void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
1453 const unsigned char *p0, *p1, *p2;
1455 float iwidth, iheight, ibumpscale, n[3];
1456 iwidth = 1.0f / width;
1457 iheight = 1.0f / height;
1458 ibumpscale = (255.0f * 3.0f) / bumpscale;
1460 for (y = 0;y < height;y++)
1462 for (x = 0;x < width;x++)
1464 p0 = inpixels + (y * width + x) * 4;
1468 p1 = inpixels + (y * width + x) * 4;
1470 p1 = inpixels + (y * width) * 4;
1473 p1 = inpixels + (y * width + x + 1) * 4;
1474 if (y == height - 1)
1477 p2 = inpixels + (y * width + x) * 4;
1479 p2 = inpixels + x * 4;
1482 p2 = inpixels + ((y + 1) * width + x) * 4;
1486 dv[0][2] = ((p0[0] + p0[1] + p0[2]) * ibumpscale) - ((p1[0] + p1[1] + p1[2]) * ibumpscale);
1489 dv[1][2] = ((p2[0] + p2[1] + p2[2]) * ibumpscale) - ((p0[0] + p0[1] + p0[2]) * ibumpscale);
1490 n[0] = dv[0][1]*dv[1][2]-dv[0][2]*dv[1][1];
1491 n[1] = dv[0][2]*dv[1][0]-dv[0][0]*dv[1][2];
1492 n[2] = dv[0][0]*dv[1][1]-dv[0][1]*dv[1][0];
1494 n[0] = ((p0[0] + p0[1] + p0[2]) - (p1[0] + p1[1] + p1[2]));
1495 n[1] = ((p2[0] + p2[1] + p2[2]) - (p0[0] + p0[1] + p0[2]));
1499 // this should work for the bottom right triangle if anyone wants
1500 // code for that for some reason
1501 n[0] = ((p3[0] + p3[1] + p3[2]) - (p1[0] + p1[1] + p1[2]));
1502 n[1] = ((p2[0] + p2[1] + p2[2]) - (p3[0] + p3[1] + p3[2]));
1506 out[0] = 128.0f + n[0] * 127.0f;
1507 out[1] = 128.0f + n[1] * 127.0f;
1508 out[2] = 128.0f + n[2] * 127.0f;
1509 out[3] = (p0[0] + p0[1] + p0[2]) / 3;
1515 int image_loadskin(imageskin_t *s, const char *shadername)
1518 unsigned char *bumppixels;
1519 int bumppixels_width, bumppixels_height;
1520 char name[MAX_QPATH];
1521 strlcpy(name, shadername, sizeof(name));
1522 Image_StripImageExtension(name, name);
1523 memset(s, 0, sizeof(*s));
1524 s->basepixels = loadimagepixels(name, false, 0, 0);
1525 if (s->basepixels == NULL)
1527 s->basepixels_width = image_width;
1528 s->basepixels_height = image_height;
1530 bumppixels = NULL;bumppixels_width = 0;bumppixels_height = 0;
1531 if (Image_CheckAlpha(s->basepixels, s->basepixels_width * s->basepixels_height, true))
1533 s->maskpixels = (unsigned char *)Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1534 s->maskpixels_width = s->basepixels_width;
1535 s->maskpixels_height = s->basepixels_height;
1536 memcpy(s->maskpixels, s->basepixels, s->maskpixels_width * s->maskpixels_height * 4);
1537 for (j = 0;j < s->basepixels_width * s->basepixels_height * 4;j += 4)
1539 s->maskpixels[j+0] = 255;
1540 s->maskpixels[j+1] = 255;
1541 s->maskpixels[j+2] = 255;
1545 // _luma is supported for tenebrae compatibility
1546 // (I think it's a very stupid name, but oh well)
1547 if ((s->glowpixels = loadimagepixels(va("%s_glow", name), false, 0, 0)) != NULL
1548 || (s->glowpixels = loadimagepixels(va("%s_luma", name), false, 0, 0)) != NULL)
1550 s->glowpixels_width = image_width;
1551 s->glowpixels_height = image_height;
1553 // _norm is the name used by tenebrae
1554 // (I don't like the name much)
1555 if ((s->nmappixels = loadimagepixels(va("%s_norm", name), false, 0, 0)) != NULL)
1557 s->nmappixels_width = image_width;
1558 s->nmappixels_height = image_height;
1560 else if ((bumppixels = loadimagepixels(va("%s_bump", name), false, 0, 0)) != NULL)
1562 bumppixels_width = image_width;
1563 bumppixels_height = image_height;
1565 if ((s->glosspixels = loadimagepixels(va("%s_gloss", name), false, 0, 0)) != NULL)
1567 s->glosspixels_width = image_width;
1568 s->glosspixels_height = image_height;
1570 if ((s->pantspixels = loadimagepixels(va("%s_pants", name), false, 0, 0)) != NULL)
1572 s->pantspixels_width = image_width;
1573 s->pantspixels_height = image_height;
1575 if ((s->shirtpixels = loadimagepixels(va("%s_shirt", name), false, 0, 0)) != NULL)
1577 s->shirtpixels_width = image_width;
1578 s->shirtpixels_height = image_height;
1581 if (s->nmappixels == NULL)
1583 if (bumppixels != NULL)
1585 if (r_shadow_bumpscale_bumpmap.value > 0)
1587 s->nmappixels = (unsigned char *)Mem_Alloc(loadmodel->mempool, bumppixels_width * bumppixels_height * 4);
1588 s->nmappixels_width = bumppixels_width;
1589 s->nmappixels_height = bumppixels_height;
1590 Image_HeightmapToNormalmap(bumppixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_bumpmap.value);
1595 if (r_shadow_bumpscale_basetexture.value > 0)
1597 s->nmappixels = (unsigned char *)Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1598 s->nmappixels_width = s->basepixels_width;
1599 s->nmappixels_height = s->basepixels_height;
1600 Image_HeightmapToNormalmap(s->basepixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_basetexture.value);
1604 if (bumppixels != NULL)
1605 Mem_Free(bumppixels);
1609 void image_freeskin(imageskin_t *s)
1612 Mem_Free(s->basepixels);
1614 Mem_Free(s->maskpixels);
1616 Mem_Free(s->nmappixels);
1618 Mem_Free(s->glowpixels);
1620 Mem_Free(s->glosspixels);
1622 Mem_Free(s->pantspixels);
1624 Mem_Free(s->shirtpixels);
1625 memset(s, 0, sizeof(*s));