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