]> icculus.org git repositories - divverent/darkplaces.git/blob - image.c
f7cf892d4a2dcb453bd7fdaa6d8a1531222faadf
[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 (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 (qbyte *f, int matchwidth, int matchheight)
313 {
314         int x, y, row_inc, compressed, readpixelcount, red, green, blue, alpha, runlen, pindex;
315         qbyte *pixbuf, *image_rgba;
316         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         fin = f + 18;
348         if (targa_header.id_length != 0)
349                 fin += targa_header.id_length;  // skip TARGA image comment
350         if (targa_header.image_type == 2 || targa_header.image_type == 10)
351         {
352                 if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
353                 {
354                         Con_Print("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
355                         PrintTargaHeader(&targa_header);
356                         return NULL;
357                 }
358         }
359         else if (targa_header.image_type == 1 || targa_header.image_type == 9)
360         {
361                 if (targa_header.pixel_size != 8)
362                 {
363                         Con_Print("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
364                         PrintTargaHeader(&targa_header);
365                         return NULL;
366                 }
367                 if (targa_header.colormap_length > 256)
368                 {
369                         Con_Print("LoadTGA: only up to 256 colormap_length supported\n");
370                         PrintTargaHeader(&targa_header);
371                         return NULL;
372                 }
373                 if (targa_header.colormap_index)
374                 {
375                         Con_Print("LoadTGA: colormap_index not supported\n");
376                         PrintTargaHeader(&targa_header);
377                         return NULL;
378                 }
379                 if (targa_header.colormap_size == 24)
380                 {
381                         for (x = 0;x < targa_header.colormap_length;x++)
382                         {
383                                 palette[x*4+2] = *fin++;
384                                 palette[x*4+1] = *fin++;
385                                 palette[x*4+0] = *fin++;
386                                 palette[x*4+3] = 255;
387                         }
388                 }
389                 else if (targa_header.colormap_size == 32)
390                 {
391                         for (x = 0;x < targa_header.colormap_length;x++)
392                         {
393                                 palette[x*4+2] = *fin++;
394                                 palette[x*4+1] = *fin++;
395                                 palette[x*4+0] = *fin++;
396                                 palette[x*4+3] = *fin++;
397                         }
398                 }
399                 else
400                 {
401                         Con_Print("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
402                         PrintTargaHeader(&targa_header);
403                         return NULL;
404                 }
405         }
406         else if (targa_header.image_type == 3 || targa_header.image_type == 11)
407         {
408                 if (targa_header.pixel_size != 8)
409                 {
410                         Con_Print("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
411                         PrintTargaHeader(&targa_header);
412                         return NULL;
413                 }
414         }
415         else
416         {
417                 Con_Printf("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
418                 PrintTargaHeader(&targa_header);
419                 return NULL;
420         }
421
422         if (targa_header.attributes & 0x10)
423         {
424                 Con_Print("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
425                 return NULL;
426         }
427
428         image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
429         if (!image_rgba)
430         {
431                 Con_Printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
432                 return NULL;
433         }
434
435         // If bit 5 of attributes isn't set, the image has been stored from bottom to top
436         if ((targa_header.attributes & 0x20) == 0)
437         {
438                 pixbuf = image_rgba + (image_height - 1)*image_width*4;
439                 row_inc = -image_width*4*2;
440         }
441         else
442         {
443                 pixbuf = image_rgba;
444                 row_inc = 0;
445         }
446
447         compressed = targa_header.image_type == 9 || targa_header.image_type == 10 || targa_header.image_type == 11;
448         x = 0;
449         y = 0;
450         red = green = blue = alpha = 255;
451         while (y < image_height)
452         {
453                 // decoder is mostly the same whether it's compressed or not
454                 readpixelcount = 1000000;
455                 runlen = 1000000;
456                 if (compressed && fin < enddata)
457                 {
458                         runlen = *fin++;
459                         // high bit indicates this is an RLE compressed run
460                         if (runlen & 0x80)
461                                 readpixelcount = 1;
462                         runlen = 1 + (runlen & 0x7f);
463                 }
464
465                 while((runlen--) && y < image_height)
466                 {
467                         if (readpixelcount > 0)
468                         {
469                                 readpixelcount--;
470                                 red = green = blue = alpha = 255;
471                                 if (fin < enddata)
472                                 {
473                                         switch(targa_header.image_type)
474                                         {
475                                         case 1:
476                                         case 9:
477                                                 // colormapped
478                                                 pindex = *fin++;
479                                                 if (pindex >= targa_header.colormap_length)
480                                                         pindex = 0; // error
481                                                 p = palette + pindex * 4;
482                                                 red = p[0];
483                                                 green = p[1];
484                                                 blue = p[2];
485                                                 alpha = p[3];
486                                                 break;
487                                         case 2:
488                                         case 10:
489                                                 // BGR or BGRA
490                                                 blue = *fin++;
491                                                 if (fin < enddata)
492                                                         green = *fin++;
493                                                 if (fin < enddata)
494                                                         red = *fin++;
495                                                 if (targa_header.pixel_size == 32 && fin < enddata)
496                                                         alpha = *fin++;
497                                                 break;
498                                         case 3:
499                                         case 11:
500                                                 // greyscale
501                                                 red = green = blue = *fin++;
502                                                 break;
503                                         }
504                                 }
505                         }
506                         *pixbuf++ = red;
507                         *pixbuf++ = green;
508                         *pixbuf++ = blue;
509                         *pixbuf++ = alpha;
510                         x++;
511                         if (x == image_width)
512                         {
513                                 // end of line, advance to next
514                                 x = 0;
515                                 y++;
516                                 pixbuf += row_inc;
517                         }
518                 }
519         }
520
521         return image_rgba;
522 }
523
524 /*
525 ============
526 LoadLMP
527 ============
528 */
529 qbyte *LoadLMP (qbyte *f, int matchwidth, int matchheight)
530 {
531         qbyte *image_rgba;
532
533         if (fs_filesize < 9)
534         {
535                 Con_Print("LoadLMP: invalid LMP file\n");
536                 return NULL;
537         }
538
539         // parse the very complicated header *chuckle*
540         image_width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
541         image_height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
542         if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
543         {
544                 Con_Printf("LoadLMP: invalid size %ix%i\n", image_width, image_height);
545                 return NULL;
546         }
547         if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
548                 return NULL;
549
550         if (fs_filesize < 8 + image_width * image_height)
551         {
552                 Con_Print("LoadLMP: invalid LMP file\n");
553                 return NULL;
554         }
555
556         image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
557         if (!image_rgba)
558         {
559                 Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
560                 return NULL;
561         }
562         Image_Copy8bitRGBA(f + 8, image_rgba, image_width * image_height, palette_complete);
563         return image_rgba;
564 }
565
566 typedef struct
567 {
568         char            name[32];
569         unsigned        width, height;
570         unsigned        offsets[MIPLEVELS];             // four mip maps stored
571         char            animname[32];                   // next frame in animation chain
572         int                     flags;
573         int                     contents;
574         int                     value;
575 } q2wal_t;
576
577 qbyte *LoadWAL (qbyte *f, int matchwidth, int matchheight)
578 {
579         qbyte *image_rgba;
580         const q2wal_t *inwal = (const void *)f;
581
582         if (fs_filesize < (int) sizeof(q2wal_t))
583         {
584                 Con_Print("LoadWAL: invalid WAL file\n");
585                 return NULL;
586         }
587
588         image_width = LittleLong(inwal->width);
589         image_height = LittleLong(inwal->height);
590         if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
591         {
592                 Con_Printf("LoadWAL: invalid size %ix%i\n", image_width, image_height);
593                 return NULL;
594         }
595         if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
596                 return NULL;
597
598         if ((int) fs_filesize < (int) sizeof(q2wal_t) + (int) LittleLong(inwal->offsets[0]) + image_width * image_height)
599         {
600                 Con_Print("LoadWAL: invalid WAL file\n");
601                 return NULL;
602         }
603
604         image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
605         if (!image_rgba)
606         {
607                 Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
608                 return NULL;
609         }
610         Image_Copy8bitRGBA(f + LittleLong(inwal->offsets[0]), image_rgba, image_width * image_height, palette_complete);
611         return image_rgba;
612 }
613
614
615
616 /*
617 ============
618 LoadLMP
619 ============
620 */
621 qbyte *LoadLMPAs8Bit (qbyte *f, int matchwidth, int matchheight)
622 {
623         qbyte *image_8bit;
624
625         if (fs_filesize < 9)
626         {
627                 Con_Print("LoadLMPAs8Bit: invalid LMP file\n");
628                 return NULL;
629         }
630
631         // parse the very complicated header *chuckle*
632         image_width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
633         image_height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
634         if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
635         {
636                 Con_Printf("LoadLMPAs8Bit: invalid size %ix%i\n", image_width, image_height);
637                 return NULL;
638         }
639         if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
640                 return NULL;
641
642         if (fs_filesize < 8 + image_width * image_height)
643         {
644                 Con_Print("LoadLMPAs8Bit: invalid LMP file\n");
645                 return NULL;
646         }
647
648         image_8bit = Mem_Alloc(tempmempool, image_width * image_height);
649         if (!image_8bit)
650         {
651                 Con_Printf("LoadLMPAs8Bit: not enough memory for %i by %i image\n", image_width, image_height);
652                 return NULL;
653         }
654         memcpy(image_8bit, f + 8, image_width * image_height);
655         return image_8bit;
656 }
657
658 void Image_StripImageExtension (const char *in, char *out)
659 {
660         const char *end, *temp;
661         end = in + strlen(in);
662         if ((end - in) >= 4)
663         {
664                 temp = end - 4;
665                 if (strcmp(temp, ".tga") == 0
666                  || strcmp(temp, ".pcx") == 0
667                  || strcmp(temp, ".lmp") == 0
668                  || strcmp(temp, ".png") == 0
669                  || strcmp(temp, ".jpg") == 0)
670                         end = temp;
671                 while (in < end)
672                         *out++ = *in++;
673                 *out++ = 0;
674         }
675         else
676                 strcpy(out, in);
677 }
678
679 struct
680 {
681         const char *formatstring;
682         qbyte *(*loadfunc)(qbyte *f, int matchwidth, int matchheight);
683 }
684 imageformats[] =
685 {
686         {"override/%s.tga", LoadTGA},
687         {"override/%s.jpg", JPEG_LoadImage},
688         {"textures/%s.tga", LoadTGA},
689         {"textures/%s.jpg", JPEG_LoadImage},
690         {"textures/%s.pcx", LoadPCX},
691         {"textures/%s.wal", LoadWAL},
692         {"%s.tga", LoadTGA},
693         {"%s.jpg", JPEG_LoadImage},
694         {"%s.pcx", LoadPCX},
695         {"%s.lmp", LoadLMP},
696         {NULL, NULL}
697 };
698
699 qbyte *loadimagepixels (const char *filename, qboolean complain, int matchwidth, int matchheight)
700 {
701         int i;
702         qbyte *f, *data = NULL;
703         char basename[MAX_QPATH], name[MAX_QPATH], *c;
704         if (developer_memorydebug.integer)
705                 Mem_CheckSentinelsGlobal();
706         Image_StripImageExtension(filename, basename); // strip filename extensions to allow replacement by other types
707         // replace *'s with #, so commandline utils don't get confused when dealing with the external files
708         for (c = basename;*c;c++)
709                 if (*c == '*')
710                         *c = '#';
711         for (i = 0;imageformats[i].formatstring;i++)
712         {
713                 sprintf (name, imageformats[i].formatstring, basename);
714                 f = FS_LoadFile(name, tempmempool, true);
715                 if (f)
716                 {
717                         data = imageformats[i].loadfunc(f, matchwidth, matchheight);
718                         Mem_Free(f);
719                         if (data)
720                         {
721                                 Con_DPrintf("loaded image %s (%dx%d)\n", name, image_width, image_height);
722                                 if (developer_memorydebug.integer)
723                                         Mem_CheckSentinelsGlobal();
724                                 return data;
725                         }
726                 }
727         }
728         if (complain)
729         {
730                 Con_Printf("Couldn't load %s using ", filename);
731                 for (i = 0;imageformats[i].formatstring;i++)
732                 {
733                         sprintf (name, imageformats[i].formatstring, basename);
734                         Con_Printf(i == 0 ? "\"%s\"" : (imageformats[i+1].formatstring ? ", \"%s\"" : " or \"%s\".\n"), imageformats[i].formatstring);
735                 }
736         }
737         if (developer_memorydebug.integer)
738                 Mem_CheckSentinelsGlobal();
739         return NULL;
740 }
741
742 int image_makemask (const qbyte *in, qbyte *out, int size)
743 {
744         int i, count;
745         count = 0;
746         for (i = 0;i < size;i++)
747         {
748                 out[0] = out[1] = out[2] = 255;
749                 out[3] = in[3];
750                 if (in[3] != 255)
751                         count++;
752                 in += 4;
753                 out += 4;
754         }
755         return count;
756 }
757
758 qbyte* loadimagepixelsmask (const char *filename, qboolean complain, int matchwidth, int matchheight)
759 {
760         qbyte *in, *data;
761         in = data = loadimagepixels(filename, complain, matchwidth, matchheight);
762         if (!data)
763                 return NULL;
764         if (image_makemask(data, data, image_width * image_height))
765                 return data; // some transparency
766         else
767         {
768                 Mem_Free(data);
769                 return NULL; // all opaque
770         }
771 }
772
773 rtexture_t *loadtextureimage (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 = loadimagepixels (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 *loadtextureimagemask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
785 {
786         qbyte *data;
787         rtexture_t *rt;
788         if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
789                 return 0;
790         rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
791         Mem_Free(data);
792         return rt;
793 }
794
795 rtexture_t *image_masktex;
796 rtexture_t *image_nmaptex;
797 rtexture_t *loadtextureimagewithmask (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags)
798 {
799         qbyte *data;
800         rtexture_t *rt;
801         image_masktex = NULL;
802         image_nmaptex = NULL;
803         if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
804                 return 0;
805
806         rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
807
808         if (flags & TEXF_ALPHA && image_makemask(data, data, image_width * image_height))
809                 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
810
811         Mem_Free(data);
812         return rt;
813 }
814
815 rtexture_t *loadtextureimagewithmaskandnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
816 {
817         qbyte *data, *data2;
818         rtexture_t *rt;
819         image_masktex = NULL;
820         image_nmaptex = NULL;
821         if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
822                 return 0;
823
824         data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
825
826         rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL);
827
828         Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
829         image_nmaptex = R_LoadTexture2D(pool, va("%s_nmap", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
830
831         if (flags & TEXF_ALPHA && image_makemask(data, data2, image_width * image_height))
832                 image_masktex = R_LoadTexture2D(pool, va("%s_mask", filename), image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
833
834         Mem_Free(data2);
835
836         Mem_Free(data);
837         return rt;
838 }
839
840 rtexture_t *loadtextureimagebumpasnmap (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, float bumpscale)
841 {
842         qbyte *data, *data2;
843         rtexture_t *rt;
844         if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
845                 return 0;
846         data2 = Mem_Alloc(tempmempool, image_width * image_height * 4);
847
848         Image_HeightmapToNormalmap(data, data2, image_width, image_height, (flags & TEXF_CLAMP) != 0, bumpscale);
849         rt = R_LoadTexture2D(pool, filename, image_width, image_height, data2, TEXTYPE_RGBA, flags, NULL);
850
851         Mem_Free(data2);
852         Mem_Free(data);
853         return rt;
854 }
855
856 qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int height, const qbyte *data)
857 {
858         qboolean ret;
859         qbyte *buffer, *out;
860         const qbyte *in, *end;
861
862         buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
863
864         memset (buffer, 0, 18);
865         buffer[2] = 2;          // uncompressed type
866         buffer[12] = (width >> 0) & 0xFF;
867         buffer[13] = (width >> 8) & 0xFF;
868         buffer[14] = (height >> 0) & 0xFF;
869         buffer[15] = (height >> 8) & 0xFF;
870         buffer[16] = 24;        // pixel size
871
872         // swap rgb to bgr
873         in = data;
874         out = buffer + 18;
875         end = in + width*height*3;
876         for (;in < end;in += 3)
877         {
878                 *out++ = in[2];
879                 *out++ = in[1];
880                 *out++ = in[0];
881         }
882         ret = FS_WriteFile (filename, buffer, width*height*3 + 18 );
883
884         Mem_Free(buffer);
885         return ret;
886 }
887
888 void Image_WriteTGARGB (const char *filename, int width, int height, const qbyte *data)
889 {
890         int y;
891         qbyte *buffer, *out;
892         const qbyte *in, *end;
893
894         buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
895
896         memset (buffer, 0, 18);
897         buffer[2] = 2;          // uncompressed type
898         buffer[12] = (width >> 0) & 0xFF;
899         buffer[13] = (width >> 8) & 0xFF;
900         buffer[14] = (height >> 0) & 0xFF;
901         buffer[15] = (height >> 8) & 0xFF;
902         buffer[16] = 24;        // pixel size
903
904         // swap rgb to bgr and flip upside down
905         out = buffer + 18;
906         for (y = height - 1;y >= 0;y--)
907         {
908                 in = data + y * width * 3;
909                 end = in + width * 3;
910                 for (;in < end;in += 3)
911                 {
912                         *out++ = in[2];
913                         *out++ = in[1];
914                         *out++ = in[0];
915                 }
916         }
917         FS_WriteFile (filename, buffer, width*height*3 + 18 );
918
919         Mem_Free(buffer);
920 }
921
922 void Image_WriteTGARGBA (const char *filename, int width, int height, const qbyte *data)
923 {
924         int y;
925         qbyte *buffer, *out;
926         const qbyte *in, *end;
927
928         buffer = Mem_Alloc(tempmempool, width*height*4 + 18);
929
930         memset (buffer, 0, 18);
931         buffer[2] = 2;          // uncompressed type
932         buffer[12] = (width >> 0) & 0xFF;
933         buffer[13] = (width >> 8) & 0xFF;
934         buffer[14] = (height >> 0) & 0xFF;
935         buffer[15] = (height >> 8) & 0xFF;
936         buffer[16] = 32;        // pixel size
937
938         // swap rgba to bgra and flip upside down
939         out = buffer + 18;
940         for (y = height - 1;y >= 0;y--)
941         {
942                 in = data + y * width * 4;
943                 end = in + width * 4;
944                 for (;in < end;in += 4)
945                 {
946                         *out++ = in[2];
947                         *out++ = in[1];
948                         *out++ = in[0];
949                         *out++ = in[3];
950                 }
951         }
952         FS_WriteFile (filename, buffer, width*height*4 + 18 );
953
954         Mem_Free(buffer);
955 }
956
957 qboolean Image_CheckAlpha(const qbyte *data, int size, qboolean rgba)
958 {
959         const qbyte *end;
960         if (rgba)
961         {
962                 // check alpha bytes
963                 for (end = data + size * 4, data += 3;data < end;data += 4)
964                         if (*data < 255)
965                                 return 1;
966         }
967         else
968         {
969                 // color 255 is transparent
970                 for (end = data + size;data < end;data++)
971                         if (*data == 255)
972                                 return 1;
973         }
974         return 0;
975 }
976
977 static void Image_Resample32LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
978 {
979         int             j, xi, oldx = 0, f, fstep, endx, lerp;
980         fstep = (int) (inwidth*65536.0f/outwidth);
981         endx = (inwidth-1);
982         for (j = 0,f = 0;j < outwidth;j++, f += fstep)
983         {
984                 xi = f >> 16;
985                 if (xi != oldx)
986                 {
987                         in += (xi - oldx) * 4;
988                         oldx = xi;
989                 }
990                 if (xi < endx)
991                 {
992                         lerp = f & 0xFFFF;
993                         *out++ = (qbyte) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
994                         *out++ = (qbyte) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
995                         *out++ = (qbyte) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
996                         *out++ = (qbyte) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
997                 }
998                 else // last pixel of the line has no pixel to lerp to
999                 {
1000                         *out++ = in[0];
1001                         *out++ = in[1];
1002                         *out++ = in[2];
1003                         *out++ = in[3];
1004                 }
1005         }
1006 }
1007
1008 static void Image_Resample24LerpLine (const qbyte *in, qbyte *out, int inwidth, int outwidth)
1009 {
1010         int             j, xi, oldx = 0, f, fstep, endx, lerp;
1011         fstep = (int) (inwidth*65536.0f/outwidth);
1012         endx = (inwidth-1);
1013         for (j = 0,f = 0;j < outwidth;j++, f += fstep)
1014         {
1015                 xi = f >> 16;
1016                 if (xi != oldx)
1017                 {
1018                         in += (xi - oldx) * 3;
1019                         oldx = xi;
1020                 }
1021                 if (xi < endx)
1022                 {
1023                         lerp = f & 0xFFFF;
1024                         *out++ = (qbyte) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
1025                         *out++ = (qbyte) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
1026                         *out++ = (qbyte) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
1027                 }
1028                 else // last pixel of the line has no pixel to lerp to
1029                 {
1030                         *out++ = in[0];
1031                         *out++ = in[1];
1032                         *out++ = in[2];
1033                 }
1034         }
1035 }
1036
1037 int resamplerowsize = 0;
1038 qbyte *resamplerow1 = NULL;
1039 qbyte *resamplerow2 = NULL;
1040 mempool_t *resamplemempool = NULL;
1041
1042 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (qbyte) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
1043 void Image_Resample32Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1044 {
1045         int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
1046         qbyte *out;
1047         const qbyte *inrow;
1048         out = outdata;
1049         fstep = (int) (inheight*65536.0f/outheight);
1050
1051         inrow = indata;
1052         oldy = 0;
1053         Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1054         Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1055         for (i = 0, f = 0;i < outheight;i++,f += fstep)
1056         {
1057                 yi = f >> 16;
1058                 if (yi < endy)
1059                 {
1060                         lerp = f & 0xFFFF;
1061                         if (yi != oldy)
1062                         {
1063                                 inrow = (qbyte *)indata + inwidth4*yi;
1064                                 if (yi == oldy+1)
1065                                         memcpy(resamplerow1, resamplerow2, outwidth4);
1066                                 else
1067                                         Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1068                                 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
1069                                 oldy = yi;
1070                         }
1071                         j = outwidth - 4;
1072                         while(j >= 0)
1073                         {
1074                                 LERPBYTE( 0);
1075                                 LERPBYTE( 1);
1076                                 LERPBYTE( 2);
1077                                 LERPBYTE( 3);
1078                                 LERPBYTE( 4);
1079                                 LERPBYTE( 5);
1080                                 LERPBYTE( 6);
1081                                 LERPBYTE( 7);
1082                                 LERPBYTE( 8);
1083                                 LERPBYTE( 9);
1084                                 LERPBYTE(10);
1085                                 LERPBYTE(11);
1086                                 LERPBYTE(12);
1087                                 LERPBYTE(13);
1088                                 LERPBYTE(14);
1089                                 LERPBYTE(15);
1090                                 out += 16;
1091                                 resamplerow1 += 16;
1092                                 resamplerow2 += 16;
1093                                 j -= 4;
1094                         }
1095                         if (j & 2)
1096                         {
1097                                 LERPBYTE( 0);
1098                                 LERPBYTE( 1);
1099                                 LERPBYTE( 2);
1100                                 LERPBYTE( 3);
1101                                 LERPBYTE( 4);
1102                                 LERPBYTE( 5);
1103                                 LERPBYTE( 6);
1104                                 LERPBYTE( 7);
1105                                 out += 8;
1106                                 resamplerow1 += 8;
1107                                 resamplerow2 += 8;
1108                         }
1109                         if (j & 1)
1110                         {
1111                                 LERPBYTE( 0);
1112                                 LERPBYTE( 1);
1113                                 LERPBYTE( 2);
1114                                 LERPBYTE( 3);
1115                                 out += 4;
1116                                 resamplerow1 += 4;
1117                                 resamplerow2 += 4;
1118                         }
1119                         resamplerow1 -= outwidth4;
1120                         resamplerow2 -= outwidth4;
1121                 }
1122                 else
1123                 {
1124                         if (yi != oldy)
1125                         {
1126                                 inrow = (qbyte *)indata + inwidth4*yi;
1127                                 if (yi == oldy+1)
1128                                         memcpy(resamplerow1, resamplerow2, outwidth4);
1129                                 else
1130                                         Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
1131                                 oldy = yi;
1132                         }
1133                         memcpy(out, resamplerow1, outwidth4);
1134                 }
1135         }
1136 }
1137
1138 void Image_Resample32Nearest(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1139 {
1140         int i, j;
1141         unsigned frac, fracstep;
1142         // relies on int being 4 bytes
1143         int *inrow, *out;
1144         out = outdata;
1145
1146         fracstep = inwidth*0x10000/outwidth;
1147         for (i = 0;i < outheight;i++)
1148         {
1149                 inrow = (int *)indata + inwidth*(i*inheight/outheight);
1150                 frac = fracstep >> 1;
1151                 j = outwidth - 4;
1152                 while (j >= 0)
1153                 {
1154                         out[0] = inrow[frac >> 16];frac += fracstep;
1155                         out[1] = inrow[frac >> 16];frac += fracstep;
1156                         out[2] = inrow[frac >> 16];frac += fracstep;
1157                         out[3] = inrow[frac >> 16];frac += fracstep;
1158                         out += 4;
1159                         j -= 4;
1160                 }
1161                 if (j & 2)
1162                 {
1163                         out[0] = inrow[frac >> 16];frac += fracstep;
1164                         out[1] = inrow[frac >> 16];frac += fracstep;
1165                         out += 2;
1166                 }
1167                 if (j & 1)
1168                 {
1169                         out[0] = inrow[frac >> 16];frac += fracstep;
1170                         out += 1;
1171                 }
1172         }
1173 }
1174
1175 void Image_Resample24Lerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1176 {
1177         int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
1178         qbyte *out;
1179         const qbyte *inrow;
1180         out = outdata;
1181         fstep = (int) (inheight*65536.0f/outheight);
1182
1183         inrow = indata;
1184         oldy = 0;
1185         Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1186         Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1187         for (i = 0, f = 0;i < outheight;i++,f += fstep)
1188         {
1189                 yi = f >> 16;
1190                 if (yi < endy)
1191                 {
1192                         lerp = f & 0xFFFF;
1193                         if (yi != oldy)
1194                         {
1195                                 inrow = (qbyte *)indata + inwidth3*yi;
1196                                 if (yi == oldy+1)
1197                                         memcpy(resamplerow1, resamplerow2, outwidth3);
1198                                 else
1199                                         Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1200                                 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1201                                 oldy = yi;
1202                         }
1203                         j = outwidth - 4;
1204                         while(j >= 0)
1205                         {
1206                                 LERPBYTE( 0);
1207                                 LERPBYTE( 1);
1208                                 LERPBYTE( 2);
1209                                 LERPBYTE( 3);
1210                                 LERPBYTE( 4);
1211                                 LERPBYTE( 5);
1212                                 LERPBYTE( 6);
1213                                 LERPBYTE( 7);
1214                                 LERPBYTE( 8);
1215                                 LERPBYTE( 9);
1216                                 LERPBYTE(10);
1217                                 LERPBYTE(11);
1218                                 out += 12;
1219                                 resamplerow1 += 12;
1220                                 resamplerow2 += 12;
1221                                 j -= 4;
1222                         }
1223                         if (j & 2)
1224                         {
1225                                 LERPBYTE( 0);
1226                                 LERPBYTE( 1);
1227                                 LERPBYTE( 2);
1228                                 LERPBYTE( 3);
1229                                 LERPBYTE( 4);
1230                                 LERPBYTE( 5);
1231                                 out += 6;
1232                                 resamplerow1 += 6;
1233                                 resamplerow2 += 6;
1234                         }
1235                         if (j & 1)
1236                         {
1237                                 LERPBYTE( 0);
1238                                 LERPBYTE( 1);
1239                                 LERPBYTE( 2);
1240                                 out += 3;
1241                                 resamplerow1 += 3;
1242                                 resamplerow2 += 3;
1243                         }
1244                         resamplerow1 -= outwidth3;
1245                         resamplerow2 -= outwidth3;
1246                 }
1247                 else
1248                 {
1249                         if (yi != oldy)
1250                         {
1251                                 inrow = (qbyte *)indata + inwidth3*yi;
1252                                 if (yi == oldy+1)
1253                                         memcpy(resamplerow1, resamplerow2, outwidth3);
1254                                 else
1255                                         Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1256                                 oldy = yi;
1257                         }
1258                         memcpy(out, resamplerow1, outwidth3);
1259                 }
1260         }
1261 }
1262
1263 void Image_Resample24Nolerp(const void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1264 {
1265         int i, j, f, inwidth3 = inwidth * 3;
1266         unsigned frac, fracstep;
1267         qbyte *inrow, *out;
1268         out = outdata;
1269
1270         fracstep = inwidth*0x10000/outwidth;
1271         for (i = 0;i < outheight;i++)
1272         {
1273                 inrow = (qbyte *)indata + inwidth3*(i*inheight/outheight);
1274                 frac = fracstep >> 1;
1275                 j = outwidth - 4;
1276                 while (j >= 0)
1277                 {
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                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1282                         j -= 4;
1283                 }
1284                 if (j & 2)
1285                 {
1286                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1287                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1288                         out += 2;
1289                 }
1290                 if (j & 1)
1291                 {
1292                         f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1293                         out += 1;
1294                 }
1295         }
1296 }
1297
1298 /*
1299 ================
1300 Image_Resample
1301 ================
1302 */
1303 void Image_Resample (const void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int bytesperpixel, int quality)
1304 {
1305         if (indepth != 1 || outdepth != 1)
1306                 Sys_Error("Image_Resample: 3D resampling not supported\n");
1307         if (resamplerowsize < outwidth*4)
1308         {
1309                 if (resamplerow1)
1310                         Mem_Free(resamplerow1);
1311                 resamplerowsize = outwidth*4;
1312                 if (!resamplemempool)
1313                         resamplemempool = Mem_AllocPool("Image Scaling Buffer");
1314                 resamplerow1 = Mem_Alloc(resamplemempool, resamplerowsize*2);
1315                 resamplerow2 = resamplerow1 + resamplerowsize;
1316         }
1317         if (bytesperpixel == 4)
1318         {
1319                 if (quality)
1320                         Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1321                 else
1322                         Image_Resample32Nearest(indata, inwidth, inheight, outdata, outwidth, outheight);
1323         }
1324         else if (bytesperpixel == 3)
1325         {
1326                 if (quality)
1327                         Image_Resample24Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1328                 else
1329                         Image_Resample24Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1330         }
1331         else
1332                 Sys_Error("Image_Resample: unsupported bytesperpixel %i\n", bytesperpixel);
1333 }
1334
1335 // in can be the same as out
1336 void Image_MipReduce(const qbyte *in, qbyte *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth, int bytesperpixel)
1337 {
1338         int x, y, nextrow;
1339         if (*depth != 1 || destdepth != 1)
1340                 Sys_Error("Image_Resample: 3D resampling not supported\n");
1341         nextrow = *width * bytesperpixel;
1342         if (*width > destwidth)
1343         {
1344                 *width >>= 1;
1345                 if (*height > destheight)
1346                 {
1347                         // reduce both
1348                         *height >>= 1;
1349                         if (bytesperpixel == 4)
1350                         {
1351                                 for (y = 0;y < *height;y++)
1352                                 {
1353                                         for (x = 0;x < *width;x++)
1354                                         {
1355                                                 out[0] = (qbyte) ((in[0] + in[4] + in[nextrow  ] + in[nextrow+4]) >> 2);
1356                                                 out[1] = (qbyte) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1357                                                 out[2] = (qbyte) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1358                                                 out[3] = (qbyte) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1359                                                 out += 4;
1360                                                 in += 8;
1361                                         }
1362                                         in += nextrow; // skip a line
1363                                 }
1364                         }
1365                         else if (bytesperpixel == 3)
1366                         {
1367                                 for (y = 0;y < *height;y++)
1368                                 {
1369                                         for (x = 0;x < *width;x++)
1370                                         {
1371                                                 out[0] = (qbyte) ((in[0] + in[3] + in[nextrow  ] + in[nextrow+3]) >> 2);
1372                                                 out[1] = (qbyte) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
1373                                                 out[2] = (qbyte) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
1374                                                 out += 3;
1375                                                 in += 6;
1376                                         }
1377                                         in += nextrow; // skip a line
1378                                 }
1379                         }
1380                         else
1381                                 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1382                 }
1383                 else
1384                 {
1385                         // reduce width
1386                         if (bytesperpixel == 4)
1387                         {
1388                                 for (y = 0;y < *height;y++)
1389                                 {
1390                                         for (x = 0;x < *width;x++)
1391                                         {
1392                                                 out[0] = (qbyte) ((in[0] + in[4]) >> 1);
1393                                                 out[1] = (qbyte) ((in[1] + in[5]) >> 1);
1394                                                 out[2] = (qbyte) ((in[2] + in[6]) >> 1);
1395                                                 out[3] = (qbyte) ((in[3] + in[7]) >> 1);
1396                                                 out += 4;
1397                                                 in += 8;
1398                                         }
1399                                 }
1400                         }
1401                         else if (bytesperpixel == 3)
1402                         {
1403                                 for (y = 0;y < *height;y++)
1404                                 {
1405                                         for (x = 0;x < *width;x++)
1406                                         {
1407                                                 out[0] = (qbyte) ((in[0] + in[3]) >> 1);
1408                                                 out[1] = (qbyte) ((in[1] + in[4]) >> 1);
1409                                                 out[2] = (qbyte) ((in[2] + in[5]) >> 1);
1410                                                 out += 3;
1411                                                 in += 6;
1412                                         }
1413                                 }
1414                         }
1415                         else
1416                                 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1417                 }
1418         }
1419         else
1420         {
1421                 if (*height > destheight)
1422                 {
1423                         // reduce height
1424                         *height >>= 1;
1425                         if (bytesperpixel == 4)
1426                         {
1427                                 for (y = 0;y < *height;y++)
1428                                 {
1429                                         for (x = 0;x < *width;x++)
1430                                         {
1431                                                 out[0] = (qbyte) ((in[0] + in[nextrow  ]) >> 1);
1432                                                 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1433                                                 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1434                                                 out[3] = (qbyte) ((in[3] + in[nextrow+3]) >> 1);
1435                                                 out += 4;
1436                                                 in += 4;
1437                                         }
1438                                         in += nextrow; // skip a line
1439                                 }
1440                         }
1441                         else if (bytesperpixel == 3)
1442                         {
1443                                 for (y = 0;y < *height;y++)
1444                                 {
1445                                         for (x = 0;x < *width;x++)
1446                                         {
1447                                                 out[0] = (qbyte) ((in[0] + in[nextrow  ]) >> 1);
1448                                                 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1449                                                 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1450                                                 out += 3;
1451                                                 in += 3;
1452                                         }
1453                                         in += nextrow; // skip a line
1454                                 }
1455                         }
1456                         else
1457                                 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1458                 }
1459                 else
1460                         Sys_Error("Image_MipReduce: desired size already achieved\n");
1461         }
1462 }
1463
1464 void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
1465 {
1466         int x, y;
1467         const unsigned char *p0, *p1, *p2;
1468         unsigned char *out;
1469         float iwidth, iheight, ibumpscale, n[3];
1470         iwidth = 1.0f / width;
1471         iheight = 1.0f / height;
1472         ibumpscale = (255.0f * 3.0f) / bumpscale;
1473         out = outpixels;
1474         for (y = 0;y < height;y++)
1475         {
1476                 for (x = 0;x < width;x++)
1477                 {
1478                         p0 = inpixels + (y * width + x) * 4;
1479                         if (x == width - 1)
1480                         {
1481                                 if (clamp)
1482                                         p1 = inpixels + (y * width + x) * 4;
1483                                 else
1484                                         p1 = inpixels + (y * width) * 4;
1485                         }
1486                         else
1487                                 p1 = inpixels + (y * width + x + 1) * 4;
1488                         if (y == height - 1)
1489                         {
1490                                 if (clamp)
1491                                         p2 = inpixels + (y * width + x) * 4;
1492                                 else
1493                                         p2 = inpixels + x * 4;
1494                         }
1495                         else
1496                                 p2 = inpixels + ((y + 1) * width + x) * 4;
1497                         /*
1498                         dv[0][0] = iwidth;
1499                         dv[0][1] = 0;
1500                         dv[0][2] = ((p0[0] + p0[1] + p0[2]) * ibumpscale) - ((p1[0] + p1[1] + p1[2]) * ibumpscale);
1501                         dv[1][0] = 0;
1502                         dv[1][1] = iheight;
1503                         dv[1][2] = ((p2[0] + p2[1] + p2[2]) * ibumpscale) - ((p0[0] + p0[1] + p0[2]) * ibumpscale);
1504                         n[0] = dv[0][1]*dv[1][2]-dv[0][2]*dv[1][1];
1505                         n[1] = dv[0][2]*dv[1][0]-dv[0][0]*dv[1][2];
1506                         n[2] = dv[0][0]*dv[1][1]-dv[0][1]*dv[1][0];
1507                         */
1508                         n[0] = ((p1[0] + p1[1] + p1[2]) - (p0[0] + p0[1] + p0[2]));
1509                         n[1] = ((p0[0] + p0[1] + p0[2]) - (p2[0] + p2[1] + p2[2]));
1510                         n[2] = ibumpscale;
1511                         VectorNormalize(n);
1512                         /*
1513                         // this should work for the bottom right triangle if anyone wants
1514                         // code for that for some reason
1515                         n[0] = ((p3[0] + p3[1] + p3[2]) - (p1[0] + p1[1] + p1[2]));
1516                         n[1] = ((p2[0] + p2[1] + p2[2]) - (p3[0] + p3[1] + p3[2]));
1517                         n[2] = ibumpscale;
1518                         VectorNormalize(n);
1519                         */
1520                         out[0] = 128.0f + n[0] * 127.0f;
1521                         out[1] = 128.0f + n[1] * 127.0f;
1522                         out[2] = 128.0f + n[2] * 127.0f;
1523                         out[3] = 255;
1524                         out += 4;
1525                 }
1526         }
1527 }
1528
1529 int image_loadskin(imageskin_t *s, char *shadername)
1530 {
1531         int j;
1532         qbyte *bumppixels;
1533         int bumppixels_width, bumppixels_height;
1534         char name[MAX_QPATH];
1535         Image_StripImageExtension(shadername, name);
1536         memset(s, 0, sizeof(*s));
1537         s->basepixels = loadimagepixels(name, false, 0, 0);
1538         if (s->basepixels == NULL)
1539                 return false;
1540         s->basepixels_width = image_width;
1541         s->basepixels_height = image_height;
1542
1543         bumppixels = NULL;bumppixels_width = 0;bumppixels_height = 0;
1544         if (Image_CheckAlpha(s->basepixels, s->basepixels_width * s->basepixels_height, true))
1545         {
1546                 s->maskpixels = Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1547                 s->maskpixels_width = s->basepixels_width;
1548                 s->maskpixels_height = s->basepixels_height;
1549                 memcpy(s->maskpixels, s->basepixels, s->maskpixels_width * s->maskpixels_height * 4);
1550                 for (j = 0;j < s->basepixels_width * s->basepixels_height * 4;j += 4)
1551                 {
1552                         s->maskpixels[j+0] = 255;
1553                         s->maskpixels[j+1] = 255;
1554                         s->maskpixels[j+2] = 255;
1555                 }
1556         }
1557
1558         // _luma is supported for tenebrae compatibility
1559         // (I think it's a very stupid name, but oh well)
1560         if ((s->glowpixels = loadimagepixels(va("%s_glow", name), false, 0, 0)) != NULL
1561          || (s->glowpixels = loadimagepixels(va("%s_luma", name), false, 0, 0)) != NULL)
1562         {
1563                 s->glowpixels_width = image_width;
1564                 s->glowpixels_height = image_height;
1565         }
1566         // _norm is the name used by tenebrae
1567         // (I don't like the name much)
1568         if ((s->nmappixels = loadimagepixels(va("%s_norm", name), false, 0, 0)) != NULL)
1569         {
1570                 s->nmappixels_width = image_width;
1571                 s->nmappixels_height = image_height;
1572         }
1573         else if ((bumppixels = loadimagepixels(va("%s_bump", name), false, 0, 0)) != NULL)
1574         {
1575                 bumppixels_width = image_width;
1576                 bumppixels_height = image_height;
1577         }
1578         if ((s->glosspixels = loadimagepixels(va("%s_gloss", name), false, 0, 0)) != NULL)
1579         {
1580                 s->glosspixels_width = image_width;
1581                 s->glosspixels_height = image_height;
1582         }
1583         if ((s->pantspixels = loadimagepixels(va("%s_pants", name), false, 0, 0)) != NULL)
1584         {
1585                 s->pantspixels_width = image_width;
1586                 s->pantspixels_height = image_height;
1587         }
1588         if ((s->shirtpixels = loadimagepixels(va("%s_shirt", name), false, 0, 0)) != NULL)
1589         {
1590                 s->shirtpixels_width = image_width;
1591                 s->shirtpixels_height = image_height;
1592         }
1593
1594         if (s->nmappixels == NULL)
1595         {
1596                 if (bumppixels != NULL)
1597                 {
1598                         if (r_shadow_bumpscale_bumpmap.value > 0)
1599                         {
1600                                 s->nmappixels = Mem_Alloc(loadmodel->mempool, bumppixels_width * bumppixels_height * 4);
1601                                 s->nmappixels_width = bumppixels_width;
1602                                 s->nmappixels_height = bumppixels_height;
1603                                 Image_HeightmapToNormalmap(bumppixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_bumpmap.value);
1604                         }
1605                 }
1606                 else
1607                 {
1608                         if (r_shadow_bumpscale_basetexture.value > 0)
1609                         {
1610                                 s->nmappixels = Mem_Alloc(loadmodel->mempool, s->basepixels_width * s->basepixels_height * 4);
1611                                 s->nmappixels_width = s->basepixels_width;
1612                                 s->nmappixels_height = s->basepixels_height;
1613                                 Image_HeightmapToNormalmap(s->basepixels, s->nmappixels, s->nmappixels_width, s->nmappixels_height, false, r_shadow_bumpscale_basetexture.value);
1614                         }
1615                 }
1616         }
1617         if (bumppixels != NULL)
1618                 Mem_Free(bumppixels);
1619         return true;
1620 }
1621
1622 void image_freeskin(imageskin_t *s)
1623 {
1624         if (s->basepixels)
1625                 Mem_Free(s->basepixels);
1626         if (s->maskpixels)
1627                 Mem_Free(s->maskpixels);
1628         if (s->nmappixels)
1629                 Mem_Free(s->nmappixels);
1630         if (s->glowpixels)
1631                 Mem_Free(s->glowpixels);
1632         if (s->glosspixels)
1633                 Mem_Free(s->glosspixels);
1634         if (s->pantspixels)
1635                 Mem_Free(s->pantspixels);
1636         if (s->shirtpixels)
1637                 Mem_Free(s->shirtpixels);
1638         memset(s, 0, sizeof(*s));
1639 }
1640