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