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