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