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