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