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