]> icculus.org git repositories - divverent/darkplaces.git/blob - image.c
Factorized LoadLMP and LoadLMPAs8Bit. Made a bunch of buffers const
[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 qbyte *LoadLMPAs8Bit (const qbyte *f, int matchwidth, int matchheight)
575 {
576         return LoadLMP(f, matchwidth, matchheight, true);
577 }
578
579
580 typedef struct
581 {
582         char            name[32];
583         unsigned        width, height;
584         unsigned        offsets[MIPLEVELS];             // four mip maps stored
585         char            animname[32];                   // next frame in animation chain
586         int                     flags;
587         int                     contents;
588         int                     value;
589 } q2wal_t;
590
591 qbyte *LoadWAL (const qbyte *f, int matchwidth, int matchheight)
592 {
593         qbyte *image_rgba;
594         const q2wal_t *inwal = (const void *)f;
595
596         if (fs_filesize < (int) sizeof(q2wal_t))
597         {
598                 Con_Print("LoadWAL: invalid WAL file\n");
599                 return NULL;
600         }
601
602         image_width = LittleLong(inwal->width);
603         image_height = LittleLong(inwal->height);
604         if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
605         {
606                 Con_Printf("LoadWAL: invalid size %ix%i\n", image_width, image_height);
607                 return NULL;
608         }
609         if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
610                 return NULL;
611
612         if ((int) fs_filesize < (int) sizeof(q2wal_t) + (int) LittleLong(inwal->offsets[0]) + image_width * image_height)
613         {
614                 Con_Print("LoadWAL: invalid WAL file\n");
615                 return NULL;
616         }
617
618         image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
619         if (!image_rgba)
620         {
621                 Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
622                 return NULL;
623         }
624         Image_Copy8bitRGBA(f + LittleLong(inwal->offsets[0]), image_rgba, image_width * image_height, palette_complete);
625         return image_rgba;
626 }
627
628
629 void Image_StripImageExtension (const char *in, char *out)
630 {
631         const char *end, *temp;
632         end = in + strlen(in);
633         if ((end - in) >= 4)
634         {
635                 temp = end - 4;
636                 if (strcmp(temp, ".tga") == 0
637                  || strcmp(temp, ".pcx") == 0
638                  || strcmp(temp, ".lmp") == 0
639                  || strcmp(temp, ".png") == 0
640                  || strcmp(temp, ".jpg") == 0)
641                         end = temp;
642                 while (in < end)
643                         *out++ = *in++;
644                 *out++ = 0;
645         }
646         else
647                 strcpy(out, in);
648 }
649
650 struct
651 {
652         const char *formatstring;
653         qbyte *(*loadfunc)(const qbyte *f, int matchwidth, int matchheight);
654 }
655 imageformats[] =
656 {
657         {"override/%s.tga", LoadTGA},
658         {"override/%s.jpg", JPEG_LoadImage},
659         {"textures/%s.tga", LoadTGA},
660         {"textures/%s.jpg", JPEG_LoadImage},
661         {"textures/%s.pcx", LoadPCX},
662         {"textures/%s.wal", LoadWAL},
663         {"%s.tga", LoadTGA},
664         {"%s.jpg", JPEG_LoadImage},
665         {"%s.pcx", LoadPCX},
666         {"%s.lmp", LoadLMPRGBA},
667         {NULL, NULL}
668 };
669
670 qbyte *loadimagepixels (const char *filename, qboolean complain, int matchwidth, int matchheight)
671 {
672         int i;
673         qbyte *f, *data = NULL;
674         char basename[MAX_QPATH], name[MAX_QPATH], *c;
675         if (developer_memorydebug.integer)
676                 Mem_CheckSentinelsGlobal();
677         Image_StripImageExtension(filename, basename); // strip filename extensions to allow replacement by other types
678         // replace *'s with #, so commandline utils don't get confused when dealing with the external files
679         for (c = basename;*c;c++)
680                 if (*c == '*')
681                         *c = '#';
682         for (i = 0;imageformats[i].formatstring;i++)
683         {
684                 sprintf (name, imageformats[i].formatstring, basename);
685                 f = FS_LoadFile(name, tempmempool, true);
686                 if (f)
687                 {
688                         data = imageformats[i].loadfunc(f, matchwidth, matchheight);
689                         Mem_Free(f);
690                         if (data)
691                         {
692                                 Con_DPrintf("loaded image %s (%dx%d)\n", name, image_width, image_height);
693                                 if (developer_memorydebug.integer)
694                                         Mem_CheckSentinelsGlobal();
695                                 return data;
696                         }
697                 }
698         }
699         if (complain)
700         {
701                 Con_Printf("Couldn't load %s using ", filename);
702                 for (i = 0;imageformats[i].formatstring;i++)
703                 {
704                         sprintf (name, imageformats[i].formatstring, basename);
705                         Con_Printf(i == 0 ? "\"%s\"" : (imageformats[i+1].formatstring ? ", \"%s\"" : " or \"%s\".\n"), imageformats[i].formatstring);
706                 }
707         }
708         if (developer_memorydebug.integer)
709                 Mem_CheckSentinelsGlobal();
710         return NULL;
711 }
712
713 int image_makemask (const qbyte *in, qbyte *out, int size)
714 {
715         int i, count;
716         count = 0;
717         for (i = 0;i < size;i++)
718         {
719                 out[0] = out[1] = out[2] = 255;
720                 out[3] = in[3];
721                 if (in[3] != 255)
722                         count++;
723                 in += 4;
724                 out += 4;
725         }
726         return count;
727 }
728
729 qbyte* loadimagepixelsmask (const char *filename, qboolean complain, int matchwidth, int matchheight)
730 {
731         qbyte *in, *data;
732         in = data = loadimagepixels(filename, complain, matchwidth, matchheight);
733         if (!data)
734                 return NULL;
735         if (image_makemask(data, data, image_width * image_height))
736                 return data; // some transparency
737         else
738         {
739                 Mem_Free(data);
740                 return NULL; // all opaque
741         }
742 }
743
744 rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
745 {
746         qbyte *data;
747         rtexture_t *rt;
748         if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
749                 return 0;
750         rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
751         Mem_Free(data);
752         return rt;
753 }
754
755 rtexture_t *loadtextureimagemask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
756 {
757         qbyte *data;
758         rtexture_t *rt;
759         if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
760                 return 0;
761         rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
762         Mem_Free(data);
763         return rt;
764 }
765
766 rtexture_t *image_masktex;
767 rtexture_t *image_nmaptex;
768 rtexture_t *loadtextureimagewithmask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
769 {
770         qbyte *data;
771         rtexture_t *rt;
772         image_masktex = NULL;
773         image_nmaptex = NULL;
774         if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
775                 return 0;
776
777         rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
778
779         if (flags & TEXF_ALPHA && image_makemask(data, data, image_width * image_height))
780                 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
781
782         Mem_Free(data);
783         return rt;
784 }
785
786 rtexture_t *loadtextureimagewithmaskandnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
787 {
788         qbyte *data, *data2;
789         rtexture_t *rt;
790         image_masktex = NULL;
791         image_nmaptex = NULL;
792         if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
793                 return 0;
794
795         data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
796
797         rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
798
799         Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
800         image_nmaptex = R_LoadTexture2D(pool, va("%s_nmap", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
801
802         if (flags & TEXF_ALPHA && image_makemask(data, data2, image_width * image_height))
803                 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
804
805         Mem_Free(data2);
806
807         Mem_Free(data);
808         return rt;
809 }
810
811 rtexture_t *loadtextureimagebumpasnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
812 {
813         qbyte *data, *data2;
814         rtexture_t *rt;
815         if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
816                 return 0;
817         data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
818
819         Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
820         rt = R_LoadTexture2D(pool, filename, image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
821
822         Mem_Free(data2);
823         Mem_Free(data);
824         return rt;
825 }
826
827 qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int height, const qbyte *data, qbyte *buffer)
828 {
829         qboolean ret;
830         qbyte *out;
831         const qbyte *in, *end;
832
833         memset (buffer, 0, 18);
834         buffer[2] = 2;          // uncompressed type
835         buffer[12] = (width >> 0) & 0xFF;
836         buffer[13] = (width >> 8) & 0xFF;
837         buffer[14] = (height >> 0) & 0xFF;
838         buffer[15] = (height >> 8) & 0xFF;
839         buffer[16] = 24;        // pixel size
840
841         // swap rgb to bgr
842         in = data;
843         out = buffer + 18;
844         end = in + width*height*3;
845         for (;in < end;in += 3)
846         {
847                 *out++ = in[2];
848                 *out++ = in[1];
849                 *out++ = in[0];
850         }
851         ret = FS_WriteFile (filename, buffer, width*height*3 + 18 );
852
853         return ret;
854 }
855
856 void Image_WriteTGARGB (const char *filename, int width, int height, const qbyte *data)
857 {
858         int y;
859         qbyte *buffer, *out;
860         const qbyte *in, *end;
861
862         buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
863
864         memset (buffer, 0, 18);
865         buffer[2] = 2;          // uncompressed type
866         buffer[12] = (width >> 0) & 0xFF;
867         buffer[13] = (width >> 8) & 0xFF;
868         buffer[14] = (height >> 0) & 0xFF;
869         buffer[15] = (height >> 8) & 0xFF;
870         buffer[16] = 24;        // pixel size
871
872         // swap rgb to bgr and flip upside down
873         out = buffer + 18;
874         for (y = height - 1;y >= 0;y--)
875         {
876                 in = data + y * width * 3;
877                 end = in + width * 3;
878                 for (;in < end;in += 3)
879                 {
880                         *out++ = in[2];
881                         *out++ = in[1];
882                         *out++ = in[0];
883                 }
884         }
885         FS_WriteFile (filename, buffer, width*height*3 + 18 );
886
887         Mem_Free(buffer);
888 }
889
890 void Image_WriteTGARGBA (const char *filename, int width, int height, const qbyte *data)
891 {
892         int y;
893         qbyte *buffer, *out;
894         const qbyte *in, *end;
895
896         buffer = Mem_Alloc(tempmempool, width*height*4 + 18);
897
898         memset (buffer, 0, 18);
899         buffer[2] = 2;          // uncompressed type
900         buffer[12] = (width >> 0) & 0xFF;
901         buffer[13] = (width >> 8) & 0xFF;
902         buffer[14] = (height >> 0) & 0xFF;
903         buffer[15] = (height >> 8) & 0xFF;
904         buffer[16] = 32;        // pixel size
905         buffer[17] = 8; // transparent flag? (seems to be needed by gimp)
906
907         // swap rgba to bgra and flip upside down
908         out = buffer + 18;
909         for (y = height - 1;y >= 0;y--)
910         {
911                 in = data + y * width * 4;
912                 end = in + width * 4;
913                 for (;in < end;in += 4)
914                 {
915                         *out++ = in[2];
916                         *out++ = in[1];
917                         *out++ = in[0];
918                         *out++ = in[3];
919                 }
920         }
921         FS_WriteFile (filename, buffer, width*height*4 + 18 );
922
923         Mem_Free(buffer);
924 }
925
926 qboolean Image_CheckAlpha(const qbyte *data, int size, qboolean rgba)
927 {
928         const qbyte *end;
929         if (rgba)
930         {
931                 // check alpha bytes
932                 for (end = data + size * 4, data += 3;data < end;data += 4)
933                         if (*data < 255)
934                                 return 1;
935         }
936         else
937         {
938                 // color 255 is transparent
939                 for (end = data + size;data < end;data++)
940                         if (*data == 255)
941                                 return 1;
942         }
943         return 0;
944 }
945
946 static void Image_Resample32LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
947 {
948         int             j, xi, oldx = 0, f, fstep, endx, lerp;
949         fstep = (int) (inwidth*65536.0f/outwidth);
950         endx = (inwidth-1);
951         for (j = 0,f = 0;j < outwidth;j++, f += fstep)
952         {
953                 xi = f >> 16;
954                 if (xi != oldx)
955                 {
956                         in += (xi - oldx) * 4;
957                         oldx = xi;
958                 }
959                 if (xi < endx)
960                 {
961                         lerp = f & 0xFFFF;
962                         *out++ = (qbyte) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
963                         *out++ = (qbyte) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
964                         *out++ = (qbyte) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
965                         *out++ = (qbyte) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
966                 }
967                 else // last pixel of the line has no pixel to lerp to
968                 {
969                         *out++ = in[0];
970                         *out++ = in[1];
971                         *out++ = in[2];
972                         *out++ = in[3];
973                 }
974         }
975 }
976
977 static void Image_Resample24LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
978 {
979         int             j, xi, oldx = 0, f, fstep, endx, lerp;
980         fstep = (int) (inwidth*65536.0f/outwidth);
981         endx = (inwidth-1);
982         for (j = 0,f = 0;j < outwidth;j++, f += fstep)
983         {
984                 xi = f >> 16;
985                 if (xi != oldx)
986                 {
987                         in += (xi - oldx) * 3;
988                         oldx = xi;
989                 }
990                 if (xi < endx)
991                 {
992                         lerp = f & 0xFFFF;
993                         *out++ = (qbyte) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
994                         *out++ = (qbyte) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
995                         *out++ = (qbyte) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
996                 }
997                 else // last pixel of the line has no pixel to lerp to
998                 {
999                         *out++ = in[0];
1000                         *out++ = in[1];
1001                         *out++ = in[2];
1002                 }
1003         }
1004 }
1005
1006 int resamplerowsize = 0;
1007 qbyte *resamplerow1 = NULL;
1008 qbyte *resamplerow2 = NULL;
1009 mempool_t *resamplemempool = NULL;
1010
1011 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (qbyte) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
1012 void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1013 {
1014         int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
1015         qbyte *out;
1016         const qbyte *inrow;
1017         out = outdata;
1018         fstep = (int) (inheight*65536.0f/outheight);
1019
1020         inrow = indata;
1021         oldy = 0;
1022         Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1023         Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1024         for (i = 0, f = 0;i < outheight;i++,f += fstep)
1025         {
1026                 yi = f >> 16;
1027                 if (yi < endy)
1028                 {
1029                         lerp = f & 0xFFFF;
1030                         if (yi != oldy)
1031                         {
1032                                 inrow = (qbyte *)indata + inwidth4*yi;
1033                                 if (yi == oldy+1)
1034                                         memcpy(resamplerow1, resamplerow2, outwidth4);
1035                                 else
1036                                         Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1037                                 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1038                                 oldy = yi;
1039                         }
1040                         j = outwidth - 4;
1041                         while(j >= 0)
1042                         {
1043                                 LERPBYTE( 0);
1044                                 LERPBYTE( 1);
1045                                 LERPBYTE( 2);
1046                                 LERPBYTE( 3);
1047                                 LERPBYTE( 4);
1048                                 LERPBYTE( 5);
1049                                 LERPBYTE( 6);
1050                                 LERPBYTE( 7);
1051                                 LERPBYTE( 8);
1052                                 LERPBYTE( 9);
1053                                 LERPBYTE(10);
1054                                 LERPBYTE(11);
1055                                 LERPBYTE(12);
1056                                 LERPBYTE(13);
1057                                 LERPBYTE(14);
1058                                 LERPBYTE(15);
1059                                 out += 16;
1060                                 resamplerow1 += 16;
1061                                 resamplerow2 += 16;
1062                                 j -= 4;
1063                         }
1064                         if (j & 2)
1065                         {
1066                                 LERPBYTE( 0);
1067                                 LERPBYTE( 1);
1068                                 LERPBYTE( 2);
1069                                 LERPBYTE( 3);
1070                                 LERPBYTE( 4);
1071                                 LERPBYTE( 5);
1072                                 LERPBYTE( 6);
1073                                 LERPBYTE( 7);
1074                                 out += 8;
1075                                 resamplerow1 += 8;
1076                                 resamplerow2 += 8;
1077                         }
1078                         if (j & 1)
1079                         {
1080                                 LERPBYTE( 0);
1081                                 LERPBYTE( 1);
1082                                 LERPBYTE( 2);
1083                                 LERPBYTE( 3);
1084                                 out += 4;
1085                                 resamplerow1 += 4;
1086                                 resamplerow2 += 4;
1087                         }
1088                         resamplerow1 -= outwidth4;
1089                         resamplerow2 -= outwidth4;
1090                 }
1091                 else
1092                 {
1093                         if (yi != oldy)
1094                         {
1095                                 inrow = (qbyte *)indata + inwidth4*yi;
1096                                 if (yi == oldy+1)
1097                                         memcpy(resamplerow1, resamplerow2, outwidth4);
1098                                 else
1099                                         Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1100                                 oldy = yi;
1101                         }
1102                         memcpy(out, resamplerow1, outwidth4);
1103                 }
1104         }
1105 }
1106
1107 void Image_Resample32Nearest(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1108 {
1109         int i, j;
1110         unsigned frac, fracstep;
1111         // relies on int being 4 bytes
1112         int *inrow, *out;
1113         out = outdata;
1114
1115         fracstep = inwidth*0x10000/outwidth;
1116         for (i = 0;i < outheight;i++)
1117         {
1118                 inrow = (int *)indata + inwidth*(i*inheight/outheight);
1119                 frac = fracstep >> 1;
1120                 j = outwidth - 4;
1121                 while (j >= 0)
1122                 {
1123                         out[0] = inrow[frac >> 16];frac += fracstep;
1124                         out[1] = inrow[frac >> 16];frac += fracstep;
1125                         out[2] = inrow[frac >> 16];frac += fracstep;
1126                         out[3] = inrow[frac >> 16];frac += fracstep;
1127                         out += 4;
1128                         j -= 4;
1129                 }
1130                 if (j & 2)
1131                 {
1132                         out[0] = inrow[frac >> 16];frac += fracstep;
1133                         out[1] = inrow[frac >> 16];frac += fracstep;
1134                         out += 2;
1135                 }
1136                 if (j & 1)
1137                 {
1138                         out[0] = inrow[frac >> 16];frac += fracstep;
1139                         out += 1;
1140                 }
1141         }
1142 }
1143
1144 void Image_Resample24Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1145 {
1146         int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
1147         qbyte *out;
1148         const qbyte *inrow;
1149         out = outdata;
1150         fstep = (int) (inheight*65536.0f/outheight);
1151
1152         inrow = indata;
1153         oldy = 0;
1154         Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1155         Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1156         for (i = 0, f = 0;i < outheight;i++,f += fstep)
1157         {
1158                 yi = f >> 16;
1159                 if (yi < endy)
1160                 {
1161                         lerp = f & 0xFFFF;
1162                         if (yi != oldy)
1163                         {
1164                                 inrow = (qbyte *)indata + inwidth3*yi;
1165                                 if (yi == oldy+1)
1166                                         memcpy(resamplerow1, resamplerow2, outwidth3);
1167                                 else
1168                                         Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1169                                 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1170                                 oldy = yi;
1171                         }
1172                         j = outwidth - 4;
1173                         while(j >= 0)
1174                         {
1175                                 LERPBYTE( 0);
1176                                 LERPBYTE( 1);
1177                                 LERPBYTE( 2);
1178                                 LERPBYTE( 3);
1179                                 LERPBYTE( 4);
1180                                 LERPBYTE( 5);
1181                                 LERPBYTE( 6);
1182                                 LERPBYTE( 7);
1183                                 LERPBYTE( 8);
1184                                 LERPBYTE( 9);
1185                                 LERPBYTE(10);
1186                                 LERPBYTE(11);
1187                                 out += 12;
1188                                 resamplerow1 += 12;
1189                                 resamplerow2 += 12;
1190                                 j -= 4;
1191                         }
1192                         if (j & 2)
1193                         {
1194                                 LERPBYTE( 0);
1195                                 LERPBYTE( 1);
1196                                 LERPBYTE( 2);
1197                                 LERPBYTE( 3);
1198                                 LERPBYTE( 4);
1199                                 LERPBYTE( 5);
1200                                 out += 6;
1201                                 resamplerow1 += 6;
1202                                 resamplerow2 += 6;
1203                         }
1204                         if (j & 1)
1205                         {
1206                                 LERPBYTE( 0);
1207                                 LERPBYTE( 1);
1208                                 LERPBYTE( 2);
1209                                 out += 3;
1210                                 resamplerow1 += 3;
1211                                 resamplerow2 += 3;
1212                         }
1213                         resamplerow1 -= outwidth3;
1214                         resamplerow2 -= outwidth3;
1215                 }
1216                 else
1217                 {
1218                         if (yi != oldy)
1219                         {
1220                                 inrow = (qbyte *)indata + inwidth3*yi;
1221                                 if (yi == oldy+1)
1222                                         memcpy(resamplerow1, resamplerow2, outwidth3);
1223                                 else
1224                                         Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1225                                 oldy = yi;
1226                         }
1227                         memcpy(out, resamplerow1, outwidth3);
1228                 }
1229         }
1230 }
1231
1232 void Image_Resample24Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1233 {
1234         int i, j, f, inwidth3 = inwidth * 3;
1235         unsigned frac, fracstep;
1236         qbyte *inrow, *out;
1237         out = outdata;
1238
1239         fracstep = inwidth*0x10000/outwidth;
1240         for (i = 0;i < outheight;i++)
1241         {
1242                 inrow = (qbyte *)indata + inwidth3*(i*inheight/outheight);
1243                 frac = fracstep >> 1;
1244                 j = outwidth - 4;
1245                 while (j >= 0)
1246                 {
1247                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1248                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1249                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1250                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1251                         j -= 4;
1252                 }
1253                 if (j & 2)
1254                 {
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                         out += 2;
1258                 }
1259                 if (j & 1)
1260                 {
1261                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1262                         out += 1;
1263                 }
1264         }
1265 }
1266
1267 /*
1268 ================
1269 Image_Resample
1270 ================
1271 */
1272 void Image_Resample (const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int bytesperpixel, int quality)
1273 {
1274         if (indepth != 1 || outdepth != 1)
1275                 Sys_Error("Image_Resample: 3D resampling not supported\n");
1276         if (resamplerowsize < outwidth*4)
1277         {
1278                 if (resamplerow1)
1279                         Mem_Free(resamplerow1);
1280                 resamplerowsize = outwidth*4;
1281                 if (!resamplemempool)
1282                         resamplemempool = Mem_AllocPool("Image Scaling Buffer", 0, NULL);
1283                 resamplerow1 = Mem_Alloc(resamplemempool, resamplerowsize*2);
1284                 resamplerow2 = resamplerow1 + resamplerowsize;
1285         }
1286         if (bytesperpixel == 4)
1287         {
1288                 if (quality)
1289                         Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1290                 else
1291                         Image_Resample32Nearest(indata, inwidth, inheight, outdata, outwidth, outheight);
1292         }
1293         else if (bytesperpixel == 3)
1294         {
1295                 if (quality)
1296                         Image_Resample24Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1297                 else
1298                         Image_Resample24Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1299         }
1300         else
1301                 Sys_Error("Image_Resample: unsupported bytesperpixel %i\n", bytesperpixel);
1302 }
1303
1304 // in can be the same as out
1305 void Image_MipReduce(const qbyte *in, qbyte *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth, int bytesperpixel)
1306 {
1307         int x, y, nextrow;
1308         if (*depth != 1 || destdepth != 1)
1309                 Sys_Error("Image_Resample: 3D resampling not supported\n");
1310         nextrow = *width * bytesperpixel;
1311         if (*width > destwidth)
1312         {
1313                 *width >>= 1;
1314                 if (*height > destheight)
1315                 {
1316                         // reduce both
1317                         *height >>= 1;
1318                         if (bytesperpixel == 4)
1319                         {
1320                                 for (y = 0;y < *height;y++)
1321                                 {
1322                                         for (x = 0;x < *width;x++)
1323                                         {
1324                                                 out[0] = (qbyte) ((in[0] + in[4] + in[nextrow  ] + in[nextrow+4]) >> 2);
1325                                                 out[1] = (qbyte) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1326                                                 out[2] = (qbyte) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1327                                                 out[3] = (qbyte) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1328                                                 out += 4;
1329                                                 in += 8;
1330                                         }
1331                                         in += nextrow; // skip a line
1332                                 }
1333                         }
1334                         else if (bytesperpixel == 3)
1335                         {
1336                                 for (y = 0;y < *height;y++)
1337                                 {
1338                                         for (x = 0;x < *width;x++)
1339                                         {
1340                                                 out[0] = (qbyte) ((in[0] + in[3] + in[nextrow  ] + in[nextrow+3]) >> 2);
1341                                                 out[1] = (qbyte) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
1342                                                 out[2] = (qbyte) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
1343                                                 out += 3;
1344                                                 in += 6;
1345                                         }
1346                                         in += nextrow; // skip a line
1347                                 }
1348                         }
1349                         else
1350                                 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1351                 }
1352                 else
1353                 {
1354                         // reduce width
1355                         if (bytesperpixel == 4)
1356                         {
1357                                 for (y = 0;y < *height;y++)
1358                                 {
1359                                         for (x = 0;x < *width;x++)
1360                                         {
1361                                                 out[0] = (qbyte) ((in[0] + in[4]) >> 1);
1362                                                 out[1] = (qbyte) ((in[1] + in[5]) >> 1);
1363                                                 out[2] = (qbyte) ((in[2] + in[6]) >> 1);
1364                                                 out[3] = (qbyte) ((in[3] + in[7]) >> 1);
1365                                                 out += 4;
1366                                                 in += 8;
1367                                         }
1368                                 }
1369                         }
1370                         else if (bytesperpixel == 3)
1371                         {
1372                                 for (y = 0;y < *height;y++)
1373                                 {
1374                                         for (x = 0;x < *width;x++)
1375                                         {
1376                                                 out[0] = (qbyte) ((in[0] + in[3]) >> 1);
1377                                                 out[1] = (qbyte) ((in[1] + in[4]) >> 1);
1378                                                 out[2] = (qbyte) ((in[2] + in[5]) >> 1);
1379                                                 out += 3;
1380                                                 in += 6;
1381                                         }
1382                                 }
1383                         }
1384                         else
1385                                 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1386                 }
1387         }
1388         else
1389         {
1390                 if (*height > destheight)
1391                 {
1392                         // reduce height
1393                         *height >>= 1;
1394                         if (bytesperpixel == 4)
1395                         {
1396                                 for (y = 0;y < *height;y++)
1397                                 {
1398                                         for (x = 0;x < *width;x++)
1399                                         {
1400                                                 out[0] = (qbyte) ((in[0] + in[nextrow  ]) >> 1);
1401                                                 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1402                                                 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1403                                                 out[3] = (qbyte) ((in[3] + in[nextrow+3]) >> 1);
1404                                                 out += 4;
1405                                                 in += 4;
1406                                         }
1407                                         in += nextrow; // skip a line
1408                                 }
1409                         }
1410                         else if (bytesperpixel == 3)
1411                         {
1412                                 for (y = 0;y < *height;y++)
1413                                 {
1414                                         for (x = 0;x < *width;x++)
1415                                         {
1416                                                 out[0] = (qbyte) ((in[0] + in[nextrow  ]) >> 1);
1417                                                 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1418                                                 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1419                                                 out += 3;
1420                                                 in += 3;
1421                                         }
1422                                         in += nextrow; // skip a line
1423                                 }
1424                         }
1425                         else
1426                                 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1427                 }
1428                 else
1429                         Sys_Error("Image_MipReduce: desired size already achieved\n");
1430         }
1431 }
1432
1433 void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
1434 {
1435         int x, y;
1436         const unsigned char *p0, *p1, *p2;
1437         unsigned char *out;
1438         float iwidth, iheight, ibumpscale, n[3];
1439         iwidth = 1.0f / width;
1440         iheight = 1.0f / height;
1441         ibumpscale = (255.0f * 3.0f) / bumpscale;
1442         out = outpixels;
1443         for (y = 0;y < height;y++)
1444         {
1445                 for (x = 0;x < width;x++)
1446                 {
1447                         p0 = inpixels + (y * width + x) * 4;
1448                         if (x == width - 1)
1449                         {
1450                                 if (clamp)
1451                                         p1 = inpixels + (y * width + x) * 4;
1452                                 else
1453                                         p1 = inpixels + (y * width) * 4;
1454                         }
1455                         else
1456                                 p1 = inpixels + (y * width + x + 1) * 4;
1457                         if (y == height - 1)
1458                         {
1459                                 if (clamp)
1460                                         p2 = inpixels + (y * width + x) * 4;
1461                                 else
1462                                         p2 = inpixels + x * 4;
1463                         }
1464                         else
1465                                 p2 = inpixels + ((y + 1) * width + x) * 4;
1466                         /*
1467                         dv[0][0] = iwidth;
1468                         dv[0][1] = 0;
1469                         dv[0][2] = ((p0[0] + p0[1] + p0[2]) * ibumpscale) - ((p1[0] + p1[1] + p1[2]) * ibumpscale);
1470                         dv[1][0] = 0;
1471                         dv[1][1] = iheight;
1472                         dv[1][2] = ((p2[0] + p2[1] + p2[2]) * ibumpscale) - ((p0[0] + p0[1] + p0[2]) * ibumpscale);
1473                         n[0] = dv[0][1]*dv[1][2]-dv[0][2]*dv[1][1];
1474                         n[1] = dv[0][2]*dv[1][0]-dv[0][0]*dv[1][2];
1475                         n[2] = dv[0][0]*dv[1][1]-dv[0][1]*dv[1][0];
1476                         */
1477                         n[0] = ((p1[0] + p1[1] + p1[2]) - (p0[0] + p0[1] + p0[2]));
1478                         n[1] = ((p0[0] + p0[1] + p0[2]) - (p2[0] + p2[1] + p2[2]));
1479                         n[2] = ibumpscale;
1480                         VectorNormalize(n);
1481                         /*
1482                         // this should work for the bottom right triangle if anyone wants
1483                         // code for that for some reason
1484                         n[0] = ((p3[0] + p3[1] + p3[2]) - (p1[0] + p1[1] + p1[2]));
1485                         n[1] = ((p2[0] + p2[1] + p2[2]) - (p3[0] + p3[1] + p3[2]));
1486                         n[2] = ibumpscale;
1487                         VectorNormalize(n);
1488                         */
1489                         out[0] = 128.0f + n[0] * 127.0f;
1490                         out[1] = 128.0f + n[1] * 127.0f;
1491                         out[2] = 128.0f + n[2] * 127.0f;
1492                         out[3] = (p0[0] + p0[1] + p0[2]) / 3;
1493                         out += 4;
1494                 }
1495         }
1496 }
1497
1498 int image_loadskin(imageskin_t *s, char *shadername)
1499 {
1500         int j;
1501         qbyte *bumppixels;
1502         int bumppixels_width, bumppixels_height;
1503         char name[MAX_QPATH];
1504         Image_StripImageExtension(shadername, name);
1505         memset(s, 0, sizeof(*s));
1506         s->basepixels = loadimagepixels(name, false, 0, 0);
1507         if (s->basepixels == NULL)
1508                 return false;
1509         s->basepixels_width = image_width;
1510         s->basepixels_height = image_height;
1511
1512         bumppixels = NULL;bumppixels_width = 0;bumppixels_height = 0;
1513         if (Image_CheckAlpha(s->basepixels, s->basepixels_width * s->basepixels_height, true))
1514         {
1515                 s->maskpixels = Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1516                 s->maskpixels_width = s->basepixels_width;
1517                 s->maskpixels_height = s->basepixels_height;
1518                 memcpy(s->maskpixels, s->basepixels, s->maskpixels_width * s->maskpixels_height * 4);
1519                 for (j = 0;j < s->basepixels_width * s->basepixels_height * 4;j += 4)
1520                 {
1521                         s->maskpixels[j+0] = 255;
1522                         s->maskpixels[j+1] = 255;
1523                         s->maskpixels[j+2] = 255;
1524                 }
1525         }
1526
1527         // _luma is supported for tenebrae compatibility
1528         // (I think it's a very stupid name, but oh well)
1529         if ((s->glowpixels = loadimagepixels(va("%s_glow", name), false, 0, 0)) != NULL
1530          || (s->glowpixels = loadimagepixels(va("%s_luma", name), false, 0, 0)) != NULL)
1531         {
1532                 s->glowpixels_width = image_width;
1533                 s->glowpixels_height = image_height;
1534         }
1535         // _norm is the name used by tenebrae
1536         // (I don't like the name much)
1537         if ((s->nmappixels = loadimagepixels(va("%s_norm", name), false, 0, 0)) != NULL)
1538         {
1539                 s->nmappixels_width = image_width;
1540                 s->nmappixels_height = image_height;
1541         }
1542         else if ((bumppixels = loadimagepixels(va("%s_bump", name), false, 0, 0)) != NULL)
1543         {
1544                 bumppixels_width = image_width;
1545                 bumppixels_height = image_height;
1546         }
1547         if ((s->glosspixels = loadimagepixels(va("%s_gloss", name), false, 0, 0)) != NULL)
1548         {
1549                 s->glosspixels_width = image_width;
1550                 s->glosspixels_height = image_height;
1551         }
1552         if ((s->pantspixels = loadimagepixels(va("%s_pants", name), false, 0, 0)) != NULL)
1553         {
1554                 s->pantspixels_width = image_width;
1555                 s->pantspixels_height = image_height;
1556         }
1557         if ((s->shirtpixels = loadimagepixels(va("%s_shirt", name), false, 0, 0)) != NULL)
1558         {
1559                 s->shirtpixels_width = image_width;
1560                 s->shirtpixels_height = image_height;
1561         }
1562
1563         if (s->nmappixels == NULL)
1564         {
1565                 if (bumppixels != NULL)
1566                 {
1567                         if (r_shadow_bumpscale_bumpmap.value > 0)
1568                         {
1569                                 s->nmappixels = Mem_Alloc(loadmodel->mempool, bumppixels_width * bumppixels_height * 4);
1570                                 s->nmappixels_width = bumppixels_width;
1571                                 s->nmappixels_height = bumppixels_height;
1572                                 Image_HeightmapToNormalmap(bumppixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_bumpmap.value);
1573                         }
1574                 }
1575                 else
1576                 {
1577                         if (r_shadow_bumpscale_basetexture.value > 0)
1578                         {
1579                                 s->nmappixels = Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1580                                 s->nmappixels_width = s->basepixels_width;
1581                                 s->nmappixels_height = s->basepixels_height;
1582                                 Image_HeightmapToNormalmap(s->basepixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_basetexture.value);
1583                         }
1584                 }
1585         }
1586         if (bumppixels != NULL)
1587                 Mem_Free(bumppixels);
1588         return true;
1589 }
1590
1591 void image_freeskin(imageskin_t *s)
1592 {
1593         if (s->basepixels)
1594                 Mem_Free(s->basepixels);
1595         if (s->maskpixels)
1596                 Mem_Free(s->maskpixels);
1597         if (s->nmappixels)
1598                 Mem_Free(s->nmappixels);
1599         if (s->glowpixels)
1600                 Mem_Free(s->glowpixels);
1601         if (s->glosspixels)
1602                 Mem_Free(s->glosspixels);
1603         if (s->pantspixels)
1604                 Mem_Free(s->pantspixels);
1605         if (s->shirtpixels)
1606                 Mem_Free(s->shirtpixels);
1607         memset(s, 0, sizeof(*s));
1608 }
1609