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