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