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