]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - image.c
added more image search paths so that md2 model skins load properly again, and to...
[xonotic/darkplaces.git] / image.c
1
2 #include "quakedef.h"
3 #include "image.h"
4 #include "jpeg.h"
5 #include "image_png.h"
6 #include "r_shadow.h"
7
8 int             image_width;
9 int             image_height;
10
11 #if 1
12 // written by LordHavoc in a readable way, optimized by Vic, further optimized by LordHavoc (the non-special index case), readable version preserved below this
13 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)
14 {
15         int index, c, x, y;
16         const unsigned char *in, *line;
17         int row_inc = (inputflipy ? -inputwidth : inputwidth) * numinputcomponents, col_inc = (inputflipx ? -1 : 1) * numinputcomponents;
18         int row_ofs = (inputflipy ? (inputheight - 1) * inputwidth * numinputcomponents : 0), col_ofs = (inputflipx ? (inputwidth - 1) * numinputcomponents : 0);
19
20         for (c = 0; c < numoutputcomponents; c++)
21                 if (outputinputcomponentindices[c] & 0x80000000)
22                         break;
23         if (c < numoutputcomponents)
24         {
25                 // special indices used
26                 if (inputflipdiagonal)
27                 {
28                         for (x = 0, line = inpixels + col_ofs; x < inputwidth; x++, line += col_inc)
29                                 for (y = 0, in = line + row_ofs; y < inputheight; y++, in += row_inc, outpixels += numinputcomponents)
30                                         for (c = 0; c < numoutputcomponents; c++)
31                                                 outpixels[c] = ((index = outputinputcomponentindices[c]) & 0x80000000) ? index : in[index];
32                 }
33                 else
34                 {
35                         for (y = 0, line = inpixels + row_ofs; y < inputheight; y++, line += row_inc)
36                                 for (x = 0, in = line + col_ofs; x < inputwidth; x++, in += col_inc, outpixels += numinputcomponents)
37                                         for (c = 0; c < numoutputcomponents; c++)
38                                                 outpixels[c] = ((index = outputinputcomponentindices[c]) & 0x80000000) ? index : in[index];
39                 }
40         }
41         else
42         {
43                 // special indices not used
44                 if (inputflipdiagonal)
45                 {
46                         for (x = 0, line = inpixels + col_ofs; x < inputwidth; x++, line += col_inc)
47                                 for (y = 0, in = line + row_ofs; y < inputheight; y++, in += row_inc, outpixels += numinputcomponents)
48                                         for (c = 0; c < numoutputcomponents; c++)
49                                                 outpixels[c] = in[outputinputcomponentindices[c]];
50                 }
51                 else
52                 {
53                         for (y = 0, line = inpixels + row_ofs; y < inputheight; y++, line += row_inc)
54                                 for (x = 0, in = line + col_ofs; x < inputwidth; x++, in += col_inc, outpixels += numinputcomponents)
55                                         for (c = 0; c < numoutputcomponents; c++)
56                                                 outpixels[c] = in[outputinputcomponentindices[c]];
57                 }
58         }
59 }
60 #else
61 // intentionally readable version
62 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)
63 {
64         int index, c, x, y;
65         const unsigned char *in, *inrow, *incolumn;
66         if (inputflipdiagonal)
67         {
68                 for (x = 0;x < inputwidth;x++)
69                 {
70                         for (y = 0;y < inputheight;y++)
71                         {
72                                 in = inpixels + ((inputflipy ? inputheight - 1 - y : y) * inputwidth + (inputflipx ? inputwidth - 1 - x : x)) * numinputcomponents;
73                                 for (c = 0;c < numoutputcomponents;c++)
74                                 {
75                                         index = outputinputcomponentindices[c];
76                                         if (index & 0x80000000)
77                                                 *outpixels++ = index;
78                                         else
79                                                 *outpixels++ = in[index];
80                                 }
81                         }
82                 }
83         }
84         else
85         {
86                 for (y = 0;y < inputheight;y++)
87                 {
88                         for (x = 0;x < inputwidth;x++)
89                         {
90                                 in = inpixels + ((inputflipy ? inputheight - 1 - y : y) * inputwidth + (inputflipx ? inputwidth - 1 - x : x)) * numinputcomponents;
91                                 for (c = 0;c < numoutputcomponents;c++)
92                                 {
93                                         index = outputinputcomponentindices[c];
94                                         if (index & 0x80000000)
95                                                 *outpixels++ = index;
96                                         else
97                                                 *outpixels++ = in[index];
98                                 }
99                         }
100                 }
101         }
102 }
103 #endif
104
105 void Image_GammaRemapRGB(const unsigned char *in, unsigned char *out, int pixels, const unsigned char *gammar, const unsigned char *gammag, const unsigned char *gammab)
106 {
107         while (pixels--)
108         {
109                 out[0] = gammar[in[0]];
110                 out[1] = gammag[in[1]];
111                 out[2] = gammab[in[2]];
112                 in += 3;
113                 out += 3;
114         }
115 }
116
117 // note: pal must be 32bit color
118 void Image_Copy8bitRGBA(const unsigned char *in, unsigned char *out, int pixels, const unsigned int *pal)
119 {
120         int *iout = (int *)out;
121         while (pixels >= 8)
122         {
123                 iout[0] = pal[in[0]];
124                 iout[1] = pal[in[1]];
125                 iout[2] = pal[in[2]];
126                 iout[3] = pal[in[3]];
127                 iout[4] = pal[in[4]];
128                 iout[5] = pal[in[5]];
129                 iout[6] = pal[in[6]];
130                 iout[7] = pal[in[7]];
131                 in += 8;
132                 iout += 8;
133                 pixels -= 8;
134         }
135         if (pixels & 4)
136         {
137                 iout[0] = pal[in[0]];
138                 iout[1] = pal[in[1]];
139                 iout[2] = pal[in[2]];
140                 iout[3] = pal[in[3]];
141                 in += 4;
142                 iout += 4;
143         }
144         if (pixels & 2)
145         {
146                 iout[0] = pal[in[0]];
147                 iout[1] = pal[in[1]];
148                 in += 2;
149                 iout += 2;
150         }
151         if (pixels & 1)
152                 iout[0] = pal[in[0]];
153 }
154
155 /*
156 =================================================================
157
158   PCX Loading
159
160 =================================================================
161 */
162
163 typedef struct pcx_s
164 {
165     char        manufacturer;
166     char        version;
167     char        encoding;
168     char        bits_per_pixel;
169     unsigned short      xmin,ymin,xmax,ymax;
170     unsigned short      hres,vres;
171     unsigned char       palette[48];
172     char        reserved;
173     char        color_planes;
174     unsigned short      bytes_per_line;
175     unsigned short      palette_type;
176     char        filler[58];
177 } pcx_t;
178
179 /*
180 ============
181 LoadPCX
182 ============
183 */
184 unsigned char* LoadPCX (const unsigned char *f, int filesize, int matchwidth, int matchheight)
185 {
186         pcx_t pcx;
187         unsigned char *a, *b, *image_rgba, *pbuf;
188         const unsigned char *palette, *fin, *enddata;
189         int x, y, x2, dataByte;
190
191         if (filesize < (int)sizeof(pcx) + 768)
192         {
193                 Con_Print("Bad pcx file\n");
194                 return NULL;
195         }
196
197         fin = f;
198
199         memcpy(&pcx, fin, sizeof(pcx));
200         fin += sizeof(pcx);
201
202         // LordHavoc: big-endian support ported from QF newtree
203         pcx.xmax = LittleShort (pcx.xmax);
204         pcx.xmin = LittleShort (pcx.xmin);
205         pcx.ymax = LittleShort (pcx.ymax);
206         pcx.ymin = LittleShort (pcx.ymin);
207         pcx.hres = LittleShort (pcx.hres);
208         pcx.vres = LittleShort (pcx.vres);
209         pcx.bytes_per_line = LittleShort (pcx.bytes_per_line);
210         pcx.palette_type = LittleShort (pcx.palette_type);
211
212         image_width = pcx.xmax + 1 - pcx.xmin;
213         image_height = pcx.ymax + 1 - pcx.ymin;
214         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         {
216                 Con_Print("Bad pcx file\n");
217                 return NULL;
218         }
219         if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
220                 return NULL;
221
222         palette = f + filesize - 768;
223
224         image_rgba = (unsigned char *)Mem_Alloc(tempmempool, image_width*image_height*4);
225         if (!image_rgba)
226         {
227                 Con_Printf("LoadPCX: not enough memory for %i by %i image\n", image_width, image_height);
228                 return NULL;
229         }
230         pbuf = image_rgba + image_width*image_height*3;
231         enddata = palette;
232
233         for (y = 0;y < image_height && fin < enddata;y++)
234         {
235                 a = pbuf + y * image_width;
236                 for (x = 0;x < image_width && fin < enddata;)
237                 {
238                         dataByte = *fin++;
239                         if(dataByte >= 0xC0)
240                         {
241                                 if (fin >= enddata)
242                                         break;
243                                 x2 = x + (dataByte & 0x3F);
244                                 dataByte = *fin++;
245                                 if (x2 > image_width)
246                                         x2 = image_width; // technically an error
247                                 while(x < x2)
248                                         a[x++] = dataByte;
249                         }
250                         else
251                                 a[x++] = dataByte;
252                 }
253                 fin += pcx.bytes_per_line - image_width; // the number of bytes per line is always forced to an even number
254                 while(x < image_width)
255                         a[x++] = 0;
256         }
257
258         a = image_rgba;
259         b = pbuf;
260
261         for(x = 0;x < image_width*image_height;x++)
262         {
263                 y = *b++ * 3;
264                 *a++ = palette[y];
265                 *a++ = palette[y+1];
266                 *a++ = palette[y+2];
267                 *a++ = 255;
268         }
269
270         return image_rgba;
271 }
272
273 /*
274 =========================================================
275
276 TARGA LOADING
277
278 =========================================================
279 */
280
281 typedef struct _TargaHeader
282 {
283         unsigned char   id_length, colormap_type, image_type;
284         unsigned short  colormap_index, colormap_length;
285         unsigned char   colormap_size;
286         unsigned short  x_origin, y_origin, width, height;
287         unsigned char   pixel_size, attributes;
288 }
289 TargaHeader;
290
291 void PrintTargaHeader(TargaHeader *t)
292 {
293         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);
294 }
295
296 /*
297 =============
298 LoadTGA
299 =============
300 */
301 unsigned char *LoadTGA (const unsigned char *f, int filesize, int matchwidth, int matchheight)
302 {
303         int x, y, pix_inc, row_inc, red, green, blue, alpha, runlen, alphabits;
304         unsigned char *pixbuf, *image_rgba;
305         const unsigned char *fin, *enddata;
306         unsigned char *p;
307         TargaHeader targa_header;
308         unsigned char palette[256*4];
309
310         if (filesize < 19)
311                 return NULL;
312
313         enddata = f + filesize;
314
315         targa_header.id_length = f[0];
316         targa_header.colormap_type = f[1];
317         targa_header.image_type = f[2];
318
319         targa_header.colormap_index = f[3] + f[4] * 256;
320         targa_header.colormap_length = f[5] + f[6] * 256;
321         targa_header.colormap_size = f[7];
322         targa_header.x_origin = f[8] + f[9] * 256;
323         targa_header.y_origin = f[10] + f[11] * 256;
324         targa_header.width = image_width = f[12] + f[13] * 256;
325         targa_header.height = image_height = f[14] + f[15] * 256;
326         if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
327         {
328                 Con_Print("LoadTGA: invalid size\n");
329                 PrintTargaHeader(&targa_header);
330                 return NULL;
331         }
332         if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
333                 return NULL;
334         targa_header.pixel_size = f[16];
335         targa_header.attributes = f[17];
336
337         // advance to end of header
338         fin = f + 18;
339
340         // skip TARGA image comment (usually 0 bytes)
341         fin += targa_header.id_length;
342
343         // read/skip the colormap if present (note: according to the TARGA spec it
344         // can be present even on truecolor or greyscale images, just not used by
345         // the image data)
346         if (targa_header.colormap_type)
347         {
348                 if (targa_header.colormap_length > 256)
349                 {
350                         Con_Print("LoadTGA: only up to 256 colormap_length supported\n");
351                         PrintTargaHeader(&targa_header);
352                         return NULL;
353                 }
354                 if (targa_header.colormap_index)
355                 {
356                         Con_Print("LoadTGA: colormap_index not supported\n");
357                         PrintTargaHeader(&targa_header);
358                         return NULL;
359                 }
360                 if (targa_header.colormap_size == 24)
361                 {
362                         for (x = 0;x < targa_header.colormap_length;x++)
363                         {
364                                 palette[x*4+2] = *fin++;
365                                 palette[x*4+1] = *fin++;
366                                 palette[x*4+0] = *fin++;
367                                 palette[x*4+3] = 255;
368                         }
369                 }
370                 else if (targa_header.colormap_size == 32)
371                 {
372                         for (x = 0;x < targa_header.colormap_length;x++)
373                         {
374                                 palette[x*4+2] = *fin++;
375                                 palette[x*4+1] = *fin++;
376                                 palette[x*4+0] = *fin++;
377                                 palette[x*4+3] = *fin++;
378                         }
379                 }
380                 else
381                 {
382                         Con_Print("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
383                         PrintTargaHeader(&targa_header);
384                         return NULL;
385                 }
386         }
387
388         // check our pixel_size restrictions according to image_type
389         switch (targa_header.image_type & ~8)
390         {
391         case 2:
392                 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
393                 {
394                         Con_Print("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
395                         PrintTargaHeader(&targa_header);
396                         return NULL;
397                 }
398                 break;
399         case 3:
400                 // set up a palette to make the loader easier
401                 for (x = 0;x < 256;x++)
402                 {
403                         palette[x*4+2] = x;
404                         palette[x*4+1] = x;
405                         palette[x*4+0] = x;
406                         palette[x*4+3] = 255;
407                 }
408                 // fall through to colormap case
409         case 1:
410                 if (targa_header.pixel_size != 8)
411                 {
412                         Con_Print("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
413                         PrintTargaHeader(&targa_header);
414                         return NULL;
415                 }
416                 break;
417         default:
418                 Con_Printf("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
419                 PrintTargaHeader(&targa_header);
420                 return NULL;
421         }
422
423         if (targa_header.attributes & 0x10)
424         {
425                 Con_Print("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
426                 return NULL;
427         }
428
429         // number of attribute bits per pixel, we only support 0 or 8
430         alphabits = targa_header.attributes & 0x0F;
431         if (alphabits != 8 && alphabits != 0)
432         {
433                 Con_Print("LoadTGA: only 0 or 8 attribute (alpha) bits supported\n");
434                 return NULL;
435         }
436
437         image_rgba = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
438         if (!image_rgba)
439         {
440                 Con_Printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
441                 return NULL;
442         }
443
444         // If bit 5 of attributes isn't set, the image has been stored from bottom to top
445         if ((targa_header.attributes & 0x20) == 0)
446         {
447                 pixbuf = image_rgba + (image_height - 1)*image_width*4;
448                 row_inc = -image_width*4*2;
449         }
450         else
451         {
452                 pixbuf = image_rgba;
453                 row_inc = 0;
454         }
455
456         x = 0;
457         y = 0;
458         red = green = blue = alpha = 255;
459         pix_inc = 1;
460         if ((targa_header.image_type & ~8) == 2)
461                 pix_inc = targa_header.pixel_size / 8;
462         switch (targa_header.image_type)
463         {
464         case 1: // colormapped, uncompressed
465         case 3: // greyscale, uncompressed
466                 if (fin + image_width * image_height * pix_inc > enddata)
467                         break;
468                 for (y = 0;y < image_height;y++, pixbuf += row_inc)
469                 {
470                         for (x = 0;x < image_width;x++)
471                         {
472                                 p = palette + *fin++ * 4;
473                                 *pixbuf++ = p[0];
474                                 *pixbuf++ = p[1];
475                                 *pixbuf++ = p[2];
476                                 *pixbuf++ = p[3];
477                         }
478                 }
479                 break;
480         case 2:
481                 // BGR or BGRA, uncompressed
482                 if (fin + image_width * image_height * pix_inc > enddata)
483                         break;
484                 if (targa_header.pixel_size == 32 && alphabits)
485                 {
486                         for (y = 0;y < image_height;y++, pixbuf += row_inc)
487                         {
488                                 for (x = 0;x < image_width;x++, fin += pix_inc)
489                                 {
490                                         *pixbuf++ = fin[2];
491                                         *pixbuf++ = fin[1];
492                                         *pixbuf++ = fin[0];
493                                         *pixbuf++ = fin[3];
494                                 }
495                         }
496                 }
497                 else
498                 {
499                         for (y = 0;y < image_height;y++, pixbuf += row_inc)
500                         {
501                                 for (x = 0;x < image_width;x++, fin += pix_inc)
502                                 {
503                                         *pixbuf++ = fin[2];
504                                         *pixbuf++ = fin[1];
505                                         *pixbuf++ = fin[0];
506                                         *pixbuf++ = 255;
507                                 }
508                         }
509                 }
510                 break;
511         case 9: // colormapped, RLE
512         case 11: // greyscale, RLE
513                 for (y = 0;y < image_height;y++, pixbuf += row_inc)
514                 {
515                         for (x = 0;x < image_width;)
516                         {
517                                 if (fin >= enddata)
518                                         break; // error - truncated file
519                                 runlen = *fin++;
520                                 if (runlen & 0x80)
521                                 {
522                                         // RLE - all pixels the same color
523                                         runlen += 1 - 0x80;
524                                         if (fin + pix_inc > enddata)
525                                                 break; // error - truncated file
526                                         if (x + runlen > image_width)
527                                                 break; // error - line exceeds width
528                                         p = palette + *fin++ * 4;
529                                         red = p[0];
530                                         green = p[1];
531                                         blue = p[2];
532                                         alpha = p[3];
533                                         for (;runlen--;x++)
534                                         {
535                                                 *pixbuf++ = red;
536                                                 *pixbuf++ = green;
537                                                 *pixbuf++ = blue;
538                                                 *pixbuf++ = alpha;
539                                         }
540                                 }
541                                 else
542                                 {
543                                         // uncompressed - all pixels different color
544                                         runlen++;
545                                         if (fin + pix_inc * runlen > enddata)
546                                                 break; // error - truncated file
547                                         if (x + runlen > image_width)
548                                                 break; // error - line exceeds width
549                                         for (;runlen--;x++)
550                                         {
551                                                 p = palette + *fin++ * 4;
552                                                 *pixbuf++ = p[0];
553                                                 *pixbuf++ = p[1];
554                                                 *pixbuf++ = p[2];
555                                                 *pixbuf++ = p[3];
556                                         }
557                                 }
558                         }
559                 }
560                 break;
561         case 10:
562                 // BGR or BGRA, RLE
563                 if (targa_header.pixel_size == 32 && alphabits)
564                 {
565                         for (y = 0;y < image_height;y++, pixbuf += row_inc)
566                         {
567                                 for (x = 0;x < image_width;)
568                                 {
569                                         if (fin >= enddata)
570                                                 break; // error - truncated file
571                                         runlen = *fin++;
572                                         if (runlen & 0x80)
573                                         {
574                                                 // RLE - all pixels the same color
575                                                 runlen += 1 - 0x80;
576                                                 if (fin + pix_inc > enddata)
577                                                         break; // error - truncated file
578                                                 if (x + runlen > image_width)
579                                                         break; // error - line exceeds width
580                                                 red = fin[2];
581                                                 green = fin[1];
582                                                 blue = fin[0];
583                                                 alpha = fin[3];
584                                                 fin += pix_inc;
585                                                 for (;runlen--;x++)
586                                                 {
587                                                         *pixbuf++ = red;
588                                                         *pixbuf++ = green;
589                                                         *pixbuf++ = blue;
590                                                         *pixbuf++ = alpha;
591                                                 }
592                                         }
593                                         else
594                                         {
595                                                 // uncompressed - all pixels different color
596                                                 runlen++;
597                                                 if (fin + pix_inc * runlen > enddata)
598                                                         break; // error - truncated file
599                                                 if (x + runlen > image_width)
600                                                         break; // error - line exceeds width
601                                                 for (;runlen--;x++, fin += pix_inc)
602                                                 {
603                                                         *pixbuf++ = fin[2];
604                                                         *pixbuf++ = fin[1];
605                                                         *pixbuf++ = fin[0];
606                                                         *pixbuf++ = fin[3];
607                                                 }
608                                         }
609                                 }
610                         }
611                 }
612                 else
613                 {
614                         for (y = 0;y < image_height;y++, pixbuf += row_inc)
615                         {
616                                 for (x = 0;x < image_width;)
617                                 {
618                                         if (fin >= enddata)
619                                                 break; // error - truncated file
620                                         runlen = *fin++;
621                                         if (runlen & 0x80)
622                                         {
623                                                 // RLE - all pixels the same color
624                                                 runlen += 1 - 0x80;
625                                                 if (fin + pix_inc > enddata)
626                                                         break; // error - truncated file
627                                                 if (x + runlen > image_width)
628                                                         break; // error - line exceeds width
629                                                 red = fin[2];
630                                                 green = fin[1];
631                                                 blue = fin[0];
632                                                 alpha = 255;
633                                                 fin += pix_inc;
634                                                 for (;runlen--;x++)
635                                                 {
636                                                         *pixbuf++ = red;
637                                                         *pixbuf++ = green;
638                                                         *pixbuf++ = blue;
639                                                         *pixbuf++ = alpha;
640                                                 }
641                                         }
642                                         else
643                                         {
644                                                 // uncompressed - all pixels different color
645                                                 runlen++;
646                                                 if (fin + pix_inc * runlen > enddata)
647                                                         break; // error - truncated file
648                                                 if (x + runlen > image_width)
649                                                         break; // error - line exceeds width
650                                                 for (;runlen--;x++, fin += pix_inc)
651                                                 {
652                                                         *pixbuf++ = fin[2];
653                                                         *pixbuf++ = fin[1];
654                                                         *pixbuf++ = fin[0];
655                                                         *pixbuf++ = 255;
656                                                 }
657                                         }
658                                 }
659                         }
660                 }
661                 break;
662         default:
663                 // unknown image_type
664                 break;
665         }
666
667         return image_rgba;
668 }
669
670 /*
671 ============
672 LoadLMP
673 ============
674 */
675 unsigned char *LoadLMP (const unsigned char *f, int filesize, int matchwidth, int matchheight, qboolean loadAs8Bit)
676 {
677         unsigned char *image_buffer;
678
679         if (filesize < 9)
680         {
681                 Con_Print("LoadLMP: invalid LMP file\n");
682                 return NULL;
683         }
684
685         // parse the very complicated header *chuckle*
686         image_width = BuffLittleLong(f);
687         image_height = BuffLittleLong(f + 4);
688         if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
689         {
690                 Con_Printf("LoadLMP: invalid size %ix%i\n", image_width, image_height);
691                 return NULL;
692         }
693         if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
694                 return NULL;
695
696         if (filesize < (8 + image_width * image_height))
697         {
698                 Con_Print("LoadLMP: invalid LMP file\n");
699                 return NULL;
700         }
701
702         if (loadAs8Bit)
703         {
704                 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height);
705                 memcpy(image_buffer, f + 8, image_width * image_height);
706         }
707         else
708         {
709                 image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
710                 Image_Copy8bitRGBA(f + 8, image_buffer, image_width * image_height, palette_transparent);
711         }
712         return image_buffer;
713 }
714
715 static unsigned char *LoadLMPRGBA (const unsigned char *f, int filesize, int matchwidth, int matchheight)
716 {
717         return LoadLMP(f, filesize, matchwidth, matchheight, false);
718 }
719
720
721 typedef struct q2wal_s
722 {
723         char            name[32];
724         unsigned        width, height;
725         unsigned        offsets[MIPLEVELS];             // four mip maps stored
726         char            animname[32];                   // next frame in animation chain
727         int                     flags;
728         int                     contents;
729         int                     value;
730 } q2wal_t;
731
732 unsigned char *LoadWAL (const unsigned char *f, int filesize, int matchwidth, int matchheight)
733 {
734         unsigned char *image_rgba;
735         const q2wal_t *inwal = (const q2wal_t *)f;
736
737         if (filesize < (int) sizeof(q2wal_t))
738         {
739                 Con_Print("LoadWAL: invalid WAL file\n");
740                 return NULL;
741         }
742
743         image_width = LittleLong(inwal->width);
744         image_height = LittleLong(inwal->height);
745         if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
746         {
747                 Con_Printf("LoadWAL: invalid size %ix%i\n", image_width, image_height);
748                 return NULL;
749         }
750         if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
751                 return NULL;
752
753         if (filesize < (int) sizeof(q2wal_t) + (int) LittleLong(inwal->offsets[0]) + image_width * image_height)
754         {
755                 Con_Print("LoadWAL: invalid WAL file\n");
756                 return NULL;
757         }
758
759         image_rgba = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
760         if (!image_rgba)
761         {
762                 Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
763                 return NULL;
764         }
765         Image_Copy8bitRGBA(f + LittleLong(inwal->offsets[0]), image_rgba, image_width * image_height, palette_complete);
766         return image_rgba;
767 }
768
769
770 void Image_StripImageExtension (const char *in, char *out)
771 {
772         const char *end, *temp;
773         end = in + strlen(in);
774         if ((end - in) >= 4)
775         {
776                 temp = end - 4;
777                 if (strcmp(temp, ".tga") == 0
778                  || strcmp(temp, ".pcx") == 0
779                  || strcmp(temp, ".lmp") == 0
780                  || strcmp(temp, ".png") == 0
781                  || strcmp(temp, ".jpg") == 0)
782                         end = temp;
783                 while (in < end)
784                         *out++ = *in++;
785                 *out++ = 0;
786         }
787         else
788                 strcpy(out, in);
789 }
790
791 typedef struct imageformat_s
792 {
793         const char *formatstring;
794         unsigned char *(*loadfunc)(const unsigned char *f, int filesize, int matchwidth, int matchheight);
795 }
796 imageformat_t;
797
798 // GAME_TENEBRAE only
799 imageformat_t imageformats_tenebrae[] =
800 {
801         {"override/%s.tga", LoadTGA},
802         {"override/%s.png", PNG_LoadImage},
803         {"override/%s.jpg", JPEG_LoadImage},
804         {"override/%s.pcx", LoadPCX},
805         {NULL, NULL}
806 };
807
808 imageformat_t imageformats_nopath[] =
809 {
810         {"override/%s.tga", LoadTGA},
811         {"override/%s.png", PNG_LoadImage},
812         {"override/%s.jpg", JPEG_LoadImage},
813         {"textures/%s.tga", LoadTGA},
814         {"textures/%s.png", PNG_LoadImage},
815         {"textures/%s.jpg", JPEG_LoadImage},
816         {"%s.tga", LoadTGA},
817         {"%s.png", PNG_LoadImage},
818         {"%s.jpg", JPEG_LoadImage},
819         {"%s.pcx", LoadPCX},
820         {NULL, NULL}
821 };
822
823 imageformat_t imageformats_textures[] =
824 {
825         {"%s.tga", LoadTGA},
826         {"%s.png", PNG_LoadImage},
827         {"%s.jpg", JPEG_LoadImage},
828         {"%s.pcx", LoadPCX},
829         {"%s.wal", LoadWAL},
830         {NULL, NULL}
831 };
832
833 imageformat_t imageformats_gfx[] =
834 {
835         {"%s.tga", LoadTGA},
836         {"%s.png", PNG_LoadImage},
837         {"%s.jpg", JPEG_LoadImage},
838         {"%s.pcx", LoadPCX},
839         {"%s.lmp", LoadLMPRGBA},
840         {NULL, NULL}
841 };
842
843 imageformat_t imageformats_other[] =
844 {
845         {"%s.tga", LoadTGA},
846         {"%s.png", PNG_LoadImage},
847         {"%s.jpg", JPEG_LoadImage},
848         {"%s.pcx", LoadPCX},
849         {NULL, NULL}
850 };
851
852 unsigned char *loadimagepixels (const char *filename, qboolean complain, int matchwidth, int matchheight)
853 {
854         fs_offset_t filesize;
855         imageformat_t *firstformat, *format;
856         unsigned char *f, *data = NULL;
857         char basename[MAX_QPATH], name[MAX_QPATH], *c;
858         if (developer_memorydebug.integer)
859                 Mem_CheckSentinelsGlobal();
860         if (developer_texturelogging.integer)
861                 Log_Printf("textures.log", "%s\n", filename);
862         strlcpy(basename, filename, sizeof(basename));
863         Image_StripImageExtension(basename, basename); // strip filename extensions to allow replacement by other types
864         // replace *'s with #, so commandline utils don't get confused when dealing with the external files
865         for (c = basename;*c;c++)
866                 if (*c == '*')
867                         *c = '#';
868         name[0] = 0;
869         if (strchr(basename, '/'))
870         {
871                 int i;
872                 for (i = 0;i < (int)sizeof(name)-1 && basename[i] != '/';i++)
873                         name[i] = basename[i];
874                 name[i] = 0;
875         }
876         if (gamemode == GAME_TENEBRAE)
877                 firstformat = imageformats_tenebrae;
878         else if (!strcasecmp(name, "textures"))
879                 firstformat = imageformats_textures;
880         else if (!strcasecmp(name, "gfx"))
881                 firstformat = imageformats_gfx;
882         else if (!strchr(name, '/'))
883                 firstformat = imageformats_nopath;
884         else
885                 firstformat = imageformats_other;
886         // now try all the formats in the selected list
887         for (format = firstformat;format->formatstring;format++)
888         {
889                 sprintf (name, format->formatstring, basename);
890                 f = FS_LoadFile(name, tempmempool, true, &filesize);
891                 if (f)
892                 {
893                         data = format->loadfunc(f, filesize, matchwidth, matchheight);
894                         Mem_Free(f);
895                         if (data)
896                         {
897                                 if (developer.integer >= 10)
898                                         Con_Printf("loaded image %s (%dx%d)\n", name, image_width, image_height);
899                                 if (developer_memorydebug.integer)
900                                         Mem_CheckSentinelsGlobal();
901                                 return data;
902                         }
903                 }
904         }
905         if (complain)
906         {
907                 Con_Printf("Couldn't load %s using ", filename);
908                 for (format = firstformat;format->formatstring;format++)
909                 {
910                         sprintf (name, format->formatstring, basename);
911                         Con_Printf(format == firstformat ? "\"%s\"" : (format[1].formatstring ? ", \"%s\"" : " or \"%s\".\n"), format->formatstring);
912                 }
913         }
914         if (developer_memorydebug.integer)
915                 Mem_CheckSentinelsGlobal();
916         return NULL;
917 }
918
919 rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
920 {
921         unsigned char *data;
922         rtexture_t *rt;
923         if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
924                 return 0;
925         rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
926         Mem_Free(data);
927         return rt;
928 }
929
930 qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int height, const unsigned char *data, unsigned char *buffer)
931 {
932         qboolean ret;
933         unsigned char *out;
934         const unsigned char *in, *end;
935
936         memset (buffer, 0, 18);
937         buffer[2] = 2;          // uncompressed type
938         buffer[12] = (width >> 0) & 0xFF;
939         buffer[13] = (width >> 8) & 0xFF;
940         buffer[14] = (height >> 0) & 0xFF;
941         buffer[15] = (height >> 8) & 0xFF;
942         buffer[16] = 24;        // pixel size
943
944         // swap rgb to bgr
945         in = data;
946         out = buffer + 18;
947         end = in + width*height*3;
948         for (;in < end;in += 3)
949         {
950                 *out++ = in[2];
951                 *out++ = in[1];
952                 *out++ = in[0];
953         }
954         ret = FS_WriteFile (filename, buffer, width*height*3 + 18 );
955
956         return ret;
957 }
958
959 void Image_WriteTGARGBA (const char *filename, int width, int height, const unsigned char *data)
960 {
961         int y;
962         unsigned char *buffer, *out;
963         const unsigned char *in, *end;
964
965         buffer = (unsigned char *)Mem_Alloc(tempmempool, width*height*4 + 18);
966
967         memset (buffer, 0, 18);
968         buffer[2] = 2;          // uncompressed type
969         buffer[12] = (width >> 0) & 0xFF;
970         buffer[13] = (width >> 8) & 0xFF;
971         buffer[14] = (height >> 0) & 0xFF;
972         buffer[15] = (height >> 8) & 0xFF;
973         buffer[16] = 32;        // pixel size
974         buffer[17] = 8; // 8 bits of alpha
975
976         // swap rgba to bgra and flip upside down
977         out = buffer + 18;
978         for (y = height - 1;y >= 0;y--)
979         {
980                 in = data + y * width * 4;
981                 end = in + width * 4;
982                 for (;in < end;in += 4)
983                 {
984                         *out++ = in[2];
985                         *out++ = in[1];
986                         *out++ = in[0];
987                         *out++ = in[3];
988                 }
989         }
990         FS_WriteFile (filename, buffer, width*height*4 + 18 );
991
992         Mem_Free(buffer);
993 }
994
995 static void Image_Resample32LerpLine (const unsigned char *in, unsigned char *out, int inwidth, int outwidth)
996 {
997         int             j, xi, oldx = 0, f, fstep, endx, lerp;
998         fstep = (int) (inwidth*65536.0f/outwidth);
999         endx = (inwidth-1);
1000         for (j = 0,f = 0;j < outwidth;j++, f += fstep)
1001         {
1002                 xi = f >> 16;
1003                 if (xi != oldx)
1004                 {
1005                         in += (xi - oldx) * 4;
1006                         oldx = xi;
1007                 }
1008                 if (xi < endx)
1009                 {
1010                         lerp = f & 0xFFFF;
1011                         *out++ = (unsigned char) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
1012                         *out++ = (unsigned char) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
1013                         *out++ = (unsigned char) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
1014                         *out++ = (unsigned char) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
1015                 }
1016                 else // last pixel of the line has no pixel to lerp to
1017                 {
1018                         *out++ = in[0];
1019                         *out++ = in[1];
1020                         *out++ = in[2];
1021                         *out++ = in[3];
1022                 }
1023         }
1024 }
1025
1026 static void Image_Resample24LerpLine (const unsigned char *in, unsigned char *out, int inwidth, int outwidth)
1027 {
1028         int             j, xi, oldx = 0, f, fstep, endx, lerp;
1029         fstep = (int) (inwidth*65536.0f/outwidth);
1030         endx = (inwidth-1);
1031         for (j = 0,f = 0;j < outwidth;j++, f += fstep)
1032         {
1033                 xi = f >> 16;
1034                 if (xi != oldx)
1035                 {
1036                         in += (xi - oldx) * 3;
1037                         oldx = xi;
1038                 }
1039                 if (xi < endx)
1040                 {
1041                         lerp = f & 0xFFFF;
1042                         *out++ = (unsigned char) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
1043                         *out++ = (unsigned char) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
1044                         *out++ = (unsigned char) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
1045                 }
1046                 else // last pixel of the line has no pixel to lerp to
1047                 {
1048                         *out++ = in[0];
1049                         *out++ = in[1];
1050                         *out++ = in[2];
1051                 }
1052         }
1053 }
1054
1055 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (unsigned char) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
1056 void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1057 {
1058         int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
1059         unsigned char *out;
1060         const unsigned char *inrow;
1061         unsigned char *resamplerow1;
1062         unsigned char *resamplerow2;
1063         out = (unsigned char *)outdata;
1064         fstep = (int) (inheight*65536.0f/outheight);
1065
1066         resamplerow1 = (unsigned char *)Mem_Alloc(tempmempool, outwidth*4*2);
1067         resamplerow2 = resamplerow1 + outwidth*4;
1068
1069         inrow = (const unsigned char *)indata;
1070         oldy = 0;
1071         Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1072         Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1073         for (i = 0, f = 0;i < outheight;i++,f += fstep)
1074         {
1075                 yi = f >> 16;
1076                 if (yi < endy)
1077                 {
1078                         lerp = f & 0xFFFF;
1079                         if (yi != oldy)
1080                         {
1081                                 inrow = (unsigned char *)indata + inwidth4*yi;
1082                                 if (yi == oldy+1)
1083                                         memcpy(resamplerow1, resamplerow2, outwidth4);
1084                                 else
1085                                         Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1086                                 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1087                                 oldy = yi;
1088                         }
1089                         j = outwidth - 4;
1090                         while(j >= 0)
1091                         {
1092                                 LERPBYTE( 0);
1093                                 LERPBYTE( 1);
1094                                 LERPBYTE( 2);
1095                                 LERPBYTE( 3);
1096                                 LERPBYTE( 4);
1097                                 LERPBYTE( 5);
1098                                 LERPBYTE( 6);
1099                                 LERPBYTE( 7);
1100                                 LERPBYTE( 8);
1101                                 LERPBYTE( 9);
1102                                 LERPBYTE(10);
1103                                 LERPBYTE(11);
1104                                 LERPBYTE(12);
1105                                 LERPBYTE(13);
1106                                 LERPBYTE(14);
1107                                 LERPBYTE(15);
1108                                 out += 16;
1109                                 resamplerow1 += 16;
1110                                 resamplerow2 += 16;
1111                                 j -= 4;
1112                         }
1113                         if (j & 2)
1114                         {
1115                                 LERPBYTE( 0);
1116                                 LERPBYTE( 1);
1117                                 LERPBYTE( 2);
1118                                 LERPBYTE( 3);
1119                                 LERPBYTE( 4);
1120                                 LERPBYTE( 5);
1121                                 LERPBYTE( 6);
1122                                 LERPBYTE( 7);
1123                                 out += 8;
1124                                 resamplerow1 += 8;
1125                                 resamplerow2 += 8;
1126                         }
1127                         if (j & 1)
1128                         {
1129                                 LERPBYTE( 0);
1130                                 LERPBYTE( 1);
1131                                 LERPBYTE( 2);
1132                                 LERPBYTE( 3);
1133                                 out += 4;
1134                                 resamplerow1 += 4;
1135                                 resamplerow2 += 4;
1136                         }
1137                         resamplerow1 -= outwidth4;
1138                         resamplerow2 -= outwidth4;
1139                 }
1140                 else
1141                 {
1142                         if (yi != oldy)
1143                         {
1144                                 inrow = (unsigned char *)indata + inwidth4*yi;
1145                                 if (yi == oldy+1)
1146                                         memcpy(resamplerow1, resamplerow2, outwidth4);
1147                                 else
1148                                         Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1149                                 oldy = yi;
1150                         }
1151                         memcpy(out, resamplerow1, outwidth4);
1152                 }
1153         }
1154
1155         Mem_Free(resamplerow1);
1156         resamplerow1 = NULL;
1157         resamplerow2 = NULL;
1158 }
1159
1160 void Image_Resample32Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1161 {
1162         int i, j;
1163         unsigned frac, fracstep;
1164         // relies on int being 4 bytes
1165         int *inrow, *out;
1166         out = (int *)outdata;
1167
1168         fracstep = inwidth*0x10000/outwidth;
1169         for (i = 0;i < outheight;i++)
1170         {
1171                 inrow = (int *)indata + inwidth*(i*inheight/outheight);
1172                 frac = fracstep >> 1;
1173                 j = outwidth - 4;
1174                 while (j >= 0)
1175                 {
1176                         out[0] = inrow[frac >> 16];frac += fracstep;
1177                         out[1] = inrow[frac >> 16];frac += fracstep;
1178                         out[2] = inrow[frac >> 16];frac += fracstep;
1179                         out[3] = inrow[frac >> 16];frac += fracstep;
1180                         out += 4;
1181                         j -= 4;
1182                 }
1183                 if (j & 2)
1184                 {
1185                         out[0] = inrow[frac >> 16];frac += fracstep;
1186                         out[1] = inrow[frac >> 16];frac += fracstep;
1187                         out += 2;
1188                 }
1189                 if (j & 1)
1190                 {
1191                         out[0] = inrow[frac >> 16];frac += fracstep;
1192                         out += 1;
1193                 }
1194         }
1195 }
1196
1197 void Image_Resample24Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1198 {
1199         int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
1200         unsigned char *out;
1201         const unsigned char *inrow;
1202         unsigned char *resamplerow1;
1203         unsigned char *resamplerow2;
1204         out = (unsigned char *)outdata;
1205         fstep = (int) (inheight*65536.0f/outheight);
1206
1207         resamplerow1 = (unsigned char *)Mem_Alloc(tempmempool, outwidth*3*2);
1208         resamplerow2 = resamplerow1 + outwidth*3;
1209
1210         inrow = (const unsigned char *)indata;
1211         oldy = 0;
1212         Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1213         Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1214         for (i = 0, f = 0;i < outheight;i++,f += fstep)
1215         {
1216                 yi = f >> 16;
1217                 if (yi < endy)
1218                 {
1219                         lerp = f & 0xFFFF;
1220                         if (yi != oldy)
1221                         {
1222                                 inrow = (unsigned char *)indata + inwidth3*yi;
1223                                 if (yi == oldy+1)
1224                                         memcpy(resamplerow1, resamplerow2, outwidth3);
1225                                 else
1226                                         Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1227                                 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1228                                 oldy = yi;
1229                         }
1230                         j = outwidth - 4;
1231                         while(j >= 0)
1232                         {
1233                                 LERPBYTE( 0);
1234                                 LERPBYTE( 1);
1235                                 LERPBYTE( 2);
1236                                 LERPBYTE( 3);
1237                                 LERPBYTE( 4);
1238                                 LERPBYTE( 5);
1239                                 LERPBYTE( 6);
1240                                 LERPBYTE( 7);
1241                                 LERPBYTE( 8);
1242                                 LERPBYTE( 9);
1243                                 LERPBYTE(10);
1244                                 LERPBYTE(11);
1245                                 out += 12;
1246                                 resamplerow1 += 12;
1247                                 resamplerow2 += 12;
1248                                 j -= 4;
1249                         }
1250                         if (j & 2)
1251                         {
1252                                 LERPBYTE( 0);
1253                                 LERPBYTE( 1);
1254                                 LERPBYTE( 2);
1255                                 LERPBYTE( 3);
1256                                 LERPBYTE( 4);
1257                                 LERPBYTE( 5);
1258                                 out += 6;
1259                                 resamplerow1 += 6;
1260                                 resamplerow2 += 6;
1261                         }
1262                         if (j & 1)
1263                         {
1264                                 LERPBYTE( 0);
1265                                 LERPBYTE( 1);
1266                                 LERPBYTE( 2);
1267                                 out += 3;
1268                                 resamplerow1 += 3;
1269                                 resamplerow2 += 3;
1270                         }
1271                         resamplerow1 -= outwidth3;
1272                         resamplerow2 -= outwidth3;
1273                 }
1274                 else
1275                 {
1276                         if (yi != oldy)
1277                         {
1278                                 inrow = (unsigned char *)indata + inwidth3*yi;
1279                                 if (yi == oldy+1)
1280                                         memcpy(resamplerow1, resamplerow2, outwidth3);
1281                                 else
1282                                         Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1283                                 oldy = yi;
1284                         }
1285                         memcpy(out, resamplerow1, outwidth3);
1286                 }
1287         }
1288         Mem_Free(resamplerow1);
1289         resamplerow1 = NULL;
1290         resamplerow2 = NULL;
1291 }
1292
1293 void Image_Resample24Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1294 {
1295         int i, j, f, inwidth3 = inwidth * 3;
1296         unsigned frac, fracstep;
1297         unsigned char *inrow, *out;
1298         out = (unsigned char *)outdata;
1299
1300         fracstep = inwidth*0x10000/outwidth;
1301         for (i = 0;i < outheight;i++)
1302         {
1303                 inrow = (unsigned char *)indata + inwidth3*(i*inheight/outheight);
1304                 frac = fracstep >> 1;
1305                 j = outwidth - 4;
1306                 while (j >= 0)
1307                 {
1308                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1309                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1310                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1311                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1312                         j -= 4;
1313                 }
1314                 if (j & 2)
1315                 {
1316                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1317                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1318                         out += 2;
1319                 }
1320                 if (j & 1)
1321                 {
1322                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1323                         out += 1;
1324                 }
1325         }
1326 }
1327
1328 /*
1329 ================
1330 Image_Resample
1331 ================
1332 */
1333 void Image_Resample (const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int bytesperpixel, int quality)
1334 {
1335         if (indepth != 1 || outdepth != 1)
1336         {
1337                 Con_Printf ("Image_Resample: 3D resampling not supported\n");
1338                 return;
1339         }
1340         if (bytesperpixel == 4)
1341         {
1342                 if (quality)
1343                         Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1344                 else
1345                         Image_Resample32Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1346         }
1347         else if (bytesperpixel == 3)
1348         {
1349                 if (quality)
1350                         Image_Resample24Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1351                 else
1352                         Image_Resample24Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1353         }
1354         else
1355                 Con_Printf ("Image_Resample: unsupported bytesperpixel %i\n", bytesperpixel);
1356 }
1357
1358 // in can be the same as out
1359 void Image_MipReduce(const unsigned char *in, unsigned char *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth, int bytesperpixel)
1360 {
1361         int x, y, nextrow;
1362         if (*depth != 1 || destdepth != 1)
1363         {
1364                 Con_Printf ("Image_Resample: 3D resampling not supported\n");
1365                 if (*width > destwidth)
1366                         *width >>= 1;
1367                 if (*height > destheight)
1368                         *height >>= 1;
1369                 if (*depth > destdepth)
1370                         *depth >>= 1;
1371                 return;
1372         }
1373         nextrow = *width * bytesperpixel;
1374         if (*width > destwidth)
1375         {
1376                 *width >>= 1;
1377                 if (*height > destheight)
1378                 {
1379                         // reduce both
1380                         *height >>= 1;
1381                         if (bytesperpixel == 4)
1382                         {
1383                                 for (y = 0;y < *height;y++)
1384                                 {
1385                                         for (x = 0;x < *width;x++)
1386                                         {
1387                                                 out[0] = (unsigned char) ((in[0] + in[4] + in[nextrow  ] + in[nextrow+4]) >> 2);
1388                                                 out[1] = (unsigned char) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1389                                                 out[2] = (unsigned char) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1390                                                 out[3] = (unsigned char) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1391                                                 out += 4;
1392                                                 in += 8;
1393                                         }
1394                                         in += nextrow; // skip a line
1395                                 }
1396                         }
1397                         else if (bytesperpixel == 3)
1398                         {
1399                                 for (y = 0;y < *height;y++)
1400                                 {
1401                                         for (x = 0;x < *width;x++)
1402                                         {
1403                                                 out[0] = (unsigned char) ((in[0] + in[3] + in[nextrow  ] + in[nextrow+3]) >> 2);
1404                                                 out[1] = (unsigned char) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
1405                                                 out[2] = (unsigned char) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
1406                                                 out += 3;
1407                                                 in += 6;
1408                                         }
1409                                         in += nextrow; // skip a line
1410                                 }
1411                         }
1412                         else
1413                                 Con_Printf ("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1414                 }
1415                 else
1416                 {
1417                         // reduce width
1418                         if (bytesperpixel == 4)
1419                         {
1420                                 for (y = 0;y < *height;y++)
1421                                 {
1422                                         for (x = 0;x < *width;x++)
1423                                         {
1424                                                 out[0] = (unsigned char) ((in[0] + in[4]) >> 1);
1425                                                 out[1] = (unsigned char) ((in[1] + in[5]) >> 1);
1426                                                 out[2] = (unsigned char) ((in[2] + in[6]) >> 1);
1427                                                 out[3] = (unsigned char) ((in[3] + in[7]) >> 1);
1428                                                 out += 4;
1429                                                 in += 8;
1430                                         }
1431                                 }
1432                         }
1433                         else if (bytesperpixel == 3)
1434                         {
1435                                 for (y = 0;y < *height;y++)
1436                                 {
1437                                         for (x = 0;x < *width;x++)
1438                                         {
1439                                                 out[0] = (unsigned char) ((in[0] + in[3]) >> 1);
1440                                                 out[1] = (unsigned char) ((in[1] + in[4]) >> 1);
1441                                                 out[2] = (unsigned char) ((in[2] + in[5]) >> 1);
1442                                                 out += 3;
1443                                                 in += 6;
1444                                         }
1445                                 }
1446                         }
1447                         else
1448                                 Con_Printf ("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1449                 }
1450         }
1451         else
1452         {
1453                 if (*height > destheight)
1454                 {
1455                         // reduce height
1456                         *height >>= 1;
1457                         if (bytesperpixel == 4)
1458                         {
1459                                 for (y = 0;y < *height;y++)
1460                                 {
1461                                         for (x = 0;x < *width;x++)
1462                                         {
1463                                                 out[0] = (unsigned char) ((in[0] + in[nextrow  ]) >> 1);
1464                                                 out[1] = (unsigned char) ((in[1] + in[nextrow+1]) >> 1);
1465                                                 out[2] = (unsigned char) ((in[2] + in[nextrow+2]) >> 1);
1466                                                 out[3] = (unsigned char) ((in[3] + in[nextrow+3]) >> 1);
1467                                                 out += 4;
1468                                                 in += 4;
1469                                         }
1470                                         in += nextrow; // skip a line
1471                                 }
1472                         }
1473                         else if (bytesperpixel == 3)
1474                         {
1475                                 for (y = 0;y < *height;y++)
1476                                 {
1477                                         for (x = 0;x < *width;x++)
1478                                         {
1479                                                 out[0] = (unsigned char) ((in[0] + in[nextrow  ]) >> 1);
1480                                                 out[1] = (unsigned char) ((in[1] + in[nextrow+1]) >> 1);
1481                                                 out[2] = (unsigned char) ((in[2] + in[nextrow+2]) >> 1);
1482                                                 out += 3;
1483                                                 in += 3;
1484                                         }
1485                                         in += nextrow; // skip a line
1486                                 }
1487                         }
1488                         else
1489                                 Con_Printf ("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1490                 }
1491                 else
1492                         Con_Printf ("Image_MipReduce: desired size already achieved\n");
1493         }
1494 }
1495
1496 void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
1497 {
1498         int x, y;
1499         const unsigned char *p0, *p1, *p2;
1500         unsigned char *out;
1501         float iwidth, iheight, ibumpscale, n[3];
1502         iwidth = 1.0f / width;
1503         iheight = 1.0f / height;
1504         ibumpscale = (255.0f * 3.0f) / bumpscale;
1505         out = outpixels;
1506         for (y = 0;y < height;y++)
1507         {
1508                 for (x = 0;x < width;x++)
1509                 {
1510                         p0 = inpixels + (y * width + x) * 4;
1511                         if (x == width - 1)
1512                         {
1513                                 if (clamp)
1514                                         p1 = inpixels + (y * width + x) * 4;
1515                                 else
1516                                         p1 = inpixels + (y * width) * 4;
1517                         }
1518                         else
1519                                 p1 = inpixels + (y * width + x + 1) * 4;
1520                         if (y == height - 1)
1521                         {
1522                                 if (clamp)
1523                                         p2 = inpixels + (y * width + x) * 4;
1524                                 else
1525                                         p2 = inpixels + x * 4;
1526                         }
1527                         else
1528                                 p2 = inpixels + ((y + 1) * width + x) * 4;
1529                         /*
1530                         dv[0][0] = iwidth;
1531                         dv[0][1] = 0;
1532                         dv[0][2] = ((p0[0] + p0[1] + p0[2]) * ibumpscale) - ((p1[0] + p1[1] + p1[2]) * ibumpscale);
1533                         dv[1][0] = 0;
1534                         dv[1][1] = iheight;
1535                         dv[1][2] = ((p2[0] + p2[1] + p2[2]) * ibumpscale) - ((p0[0] + p0[1] + p0[2]) * ibumpscale);
1536                         n[0] = dv[0][1]*dv[1][2]-dv[0][2]*dv[1][1];
1537                         n[1] = dv[0][2]*dv[1][0]-dv[0][0]*dv[1][2];
1538                         n[2] = dv[0][0]*dv[1][1]-dv[0][1]*dv[1][0];
1539                         */
1540                         n[0] = ((p0[0] + p0[1] + p0[2]) - (p1[0] + p1[1] + p1[2]));
1541                         n[1] = ((p2[0] + p2[1] + p2[2]) - (p0[0] + p0[1] + p0[2]));
1542                         n[2] = ibumpscale;
1543                         VectorNormalize(n);
1544                         /*
1545                         // this should work for the bottom right triangle if anyone wants
1546                         // code for that for some reason
1547                         n[0] = ((p3[0] + p3[1] + p3[2]) - (p1[0] + p1[1] + p1[2]));
1548                         n[1] = ((p2[0] + p2[1] + p2[2]) - (p3[0] + p3[1] + p3[2]));
1549                         n[2] = ibumpscale;
1550                         VectorNormalize(n);
1551                         */
1552                         out[0] = (int)(128.0f + n[0] * 127.0f);
1553                         out[1] = (int)(128.0f + n[1] * 127.0f);
1554                         out[2] = (int)(128.0f + n[2] * 127.0f);
1555                         out[3] = (p0[0] + p0[1] + p0[2]) / 3;
1556                         out += 4;
1557                 }
1558         }
1559 }
1560
1561 int image_loadskin(imageskin_t *s, const char *shadername)
1562 {
1563         int j;
1564         unsigned char *bumppixels;
1565         int bumppixels_width, bumppixels_height;
1566         char name[MAX_QPATH];
1567         strlcpy(name, shadername, sizeof(name));
1568         Image_StripImageExtension(name, name);
1569         memset(s, 0, sizeof(*s));
1570         s->basepixels = loadimagepixels(name, false, 0, 0);
1571         if (s->basepixels == NULL)
1572                 return false;
1573         s->basepixels_width = image_width;
1574         s->basepixels_height = image_height;
1575
1576         bumppixels = NULL;bumppixels_width = 0;bumppixels_height = 0;
1577         for (j = 3;j < s->basepixels_width * s->basepixels_height * 4;j += 4)
1578                 if (s->basepixels[j] < 255)
1579                         break;
1580         if (j < s->basepixels_width * s->basepixels_height * 4)
1581         {
1582                 // has transparent pixels
1583                 s->maskpixels = (unsigned char *)Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1584                 s->maskpixels_width = s->basepixels_width;
1585                 s->maskpixels_height = s->basepixels_height;
1586                 memcpy(s->maskpixels, s->basepixels, s->maskpixels_width * s->maskpixels_height * 4);
1587                 for (j = 0;j < s->basepixels_width * s->basepixels_height * 4;j += 4)
1588                 {
1589                         s->maskpixels[j+0] = 255;
1590                         s->maskpixels[j+1] = 255;
1591                         s->maskpixels[j+2] = 255;
1592                 }
1593         }
1594
1595         // _luma is supported for tenebrae compatibility
1596         // (I think it's a very stupid name, but oh well)
1597         if ((s->glowpixels = loadimagepixels(va("%s_glow", name), false, 0, 0)) != NULL
1598          || (s->glowpixels = loadimagepixels(va("%s_luma", name), false, 0, 0)) != NULL)
1599         {
1600                 s->glowpixels_width = image_width;
1601                 s->glowpixels_height = image_height;
1602         }
1603         // _norm is the name used by tenebrae
1604         // (I don't like the name much)
1605         if ((s->nmappixels = loadimagepixels(va("%s_norm", name), false, 0, 0)) != NULL)
1606         {
1607                 s->nmappixels_width = image_width;
1608                 s->nmappixels_height = image_height;
1609         }
1610         else if ((bumppixels = loadimagepixels(va("%s_bump", name), false, 0, 0)) != NULL)
1611         {
1612                 bumppixels_width = image_width;
1613                 bumppixels_height = image_height;
1614         }
1615         if ((s->glosspixels = loadimagepixels(va("%s_gloss", name), false, 0, 0)) != NULL)
1616         {
1617                 s->glosspixels_width = image_width;
1618                 s->glosspixels_height = image_height;
1619         }
1620         if ((s->pantspixels = loadimagepixels(va("%s_pants", name), false, 0, 0)) != NULL)
1621         {
1622                 s->pantspixels_width = image_width;
1623                 s->pantspixels_height = image_height;
1624         }
1625         if ((s->shirtpixels = loadimagepixels(va("%s_shirt", name), false, 0, 0)) != NULL)
1626         {
1627                 s->shirtpixels_width = image_width;
1628                 s->shirtpixels_height = image_height;
1629         }
1630
1631         if (s->nmappixels == NULL)
1632         {
1633                 if (bumppixels != NULL)
1634                 {
1635                         if (r_shadow_bumpscale_bumpmap.value > 0)
1636                         {
1637                                 s->nmappixels = (unsigned char *)Mem_Alloc(loadmodel->mempool, bumppixels_width * bumppixels_height * 4);
1638                                 s->nmappixels_width = bumppixels_width;
1639                                 s->nmappixels_height = bumppixels_height;
1640                                 Image_HeightmapToNormalmap(bumppixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_bumpmap.value);
1641                         }
1642                 }
1643                 else
1644                 {
1645                         if (r_shadow_bumpscale_basetexture.value > 0)
1646                         {
1647                                 s->nmappixels = (unsigned char *)Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1648                                 s->nmappixels_width = s->basepixels_width;
1649                                 s->nmappixels_height = s->basepixels_height;
1650                                 Image_HeightmapToNormalmap(s->basepixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_basetexture.value);
1651                         }
1652                 }
1653         }
1654         if (bumppixels != NULL)
1655                 Mem_Free(bumppixels);
1656         return true;
1657 }
1658
1659 void image_freeskin(imageskin_t *s)
1660 {
1661         if (s->basepixels)
1662                 Mem_Free(s->basepixels);
1663         if (s->maskpixels)
1664                 Mem_Free(s->maskpixels);
1665         if (s->nmappixels)
1666                 Mem_Free(s->nmappixels);
1667         if (s->glowpixels)
1668                 Mem_Free(s->glowpixels);
1669         if (s->glosspixels)
1670                 Mem_Free(s->glosspixels);
1671         if (s->pantspixels)
1672                 Mem_Free(s->pantspixels);
1673         if (s->shirtpixels)
1674                 Mem_Free(s->shirtpixels);
1675         memset(s, 0, sizeof(*s));
1676 }
1677