every malloc/calloc/free converted to qmalloc/qfree with tracking (memstats command...
[divverent/darkplaces.git] / image.c
1
2 #include "quakedef.h"
3
4 int             image_width;
5 int             image_height;
6
7 // note: pal must be 32bit color
8 void Image_Copy8bitRGBA(byte *in, byte *out, int pixels, int *pal)
9 {
10         int *iout = (void *)out;
11         while (pixels >= 8)
12         {
13                 iout[0] = pal[in[0]];
14                 iout[1] = pal[in[1]];
15                 iout[2] = pal[in[2]];
16                 iout[3] = pal[in[3]];
17                 iout[4] = pal[in[4]];
18                 iout[5] = pal[in[5]];
19                 iout[6] = pal[in[6]];
20                 iout[7] = pal[in[7]];
21                 in += 8;
22                 iout += 8;
23                 pixels -= 8;
24         }
25         if (pixels & 4)
26         {
27                 iout[0] = pal[in[0]];
28                 iout[1] = pal[in[1]];
29                 iout[2] = pal[in[2]];
30                 iout[3] = pal[in[3]];
31                 in += 4;
32                 iout += 4;
33         }
34         if (pixels & 2)
35         {
36                 iout[0] = pal[in[0]];
37                 iout[1] = pal[in[1]];
38                 in += 2;
39                 iout += 2;
40         }
41         if (pixels & 1)
42                 iout[0] = pal[in[0]];
43 }
44
45 void Image_CopyRGBAGamma(byte *in, byte *out, int pixels)
46 {
47         while (pixels--)
48         {
49                 out[0] = qgamma[in[0]];
50                 out[1] = qgamma[in[1]];
51                 out[2] = qgamma[in[2]];
52                 out[3] =        in[3] ;
53                 in += 4;
54                 out += 4;
55         }
56 }
57
58
59 /*
60 =================================================================
61
62   PCX Loading
63
64 =================================================================
65 */
66
67 typedef struct
68 {
69     char        manufacturer;
70     char        version;
71     char        encoding;
72     char        bits_per_pixel;
73     unsigned short      xmin,ymin,xmax,ymax;
74     unsigned short      hres,vres;
75     unsigned char       palette[48];
76     char        reserved;
77     char        color_planes;
78     unsigned short      bytes_per_line;
79     unsigned short      palette_type;
80     char        filler[58];
81     unsigned    data;                   // unbounded
82 } pcx_t;
83
84 /*
85 ============
86 LoadPCX
87 ============
88 */
89 byte* LoadPCX (FILE *f, int matchwidth, int matchheight)
90 {
91         pcx_t   *pcx, pcxbuf;
92         byte    palette[768];
93         byte    *pix, *image_rgba;
94         int             x, y;
95         int             dataByte, runLength;
96         int             count;
97
98 //
99 // parse the PCX file
100 //
101         fread (&pcxbuf, 1, sizeof(pcxbuf), f);
102
103         pcx = &pcxbuf;
104
105         // LordHavoc: big-endian support ported from QF newtree
106         pcx->xmax = LittleShort (pcx->xmax);
107         pcx->xmin = LittleShort (pcx->xmin);
108         pcx->ymax = LittleShort (pcx->ymax);
109         pcx->ymin = LittleShort (pcx->ymin);
110         pcx->hres = LittleShort (pcx->hres);
111         pcx->vres = LittleShort (pcx->vres);
112         pcx->bytes_per_line = LittleShort (pcx->bytes_per_line);
113         pcx->palette_type = LittleShort (pcx->palette_type);
114
115         if (pcx->manufacturer != 0x0a || pcx->version != 5 || pcx->encoding != 1 || pcx->bits_per_pixel != 8 || pcx->xmax > 320 || pcx->ymax > 256)
116         {
117                 Con_Printf ("Bad pcx file\n");
118                 return NULL;
119         }
120
121         if (matchwidth && (pcx->xmax+1) != matchwidth)
122                 return NULL;
123         if (matchheight && (pcx->ymax+1) != matchheight)
124                 return NULL;
125
126         // seek to palette
127         fseek (f, -768, SEEK_END);
128         fread (palette, 1, 768, f);
129
130         fseek (f, sizeof(pcxbuf) - 4, SEEK_SET);
131
132         count = (pcx->xmax+1) * (pcx->ymax+1);
133         image_rgba = qmalloc( count * 4);
134
135         for (y=0 ; y<=pcx->ymax ; y++)
136         {
137                 pix = image_rgba + 4*y*(pcx->xmax+1);
138                 for (x=0 ; x<=pcx->xmax ; )
139                 {
140                         dataByte = fgetc(f);
141
142                         if((dataByte & 0xC0) == 0xC0)
143                         {
144                                 runLength = dataByte & 0x3F;
145                                 dataByte = fgetc(f);
146                                 if (runLength)
147                                 {
148                                         x += runLength;
149                                         while(runLength--)
150                                         {
151                                                 pix[0] = palette[dataByte*3];
152                                                 pix[1] = palette[dataByte*3+1];
153                                                 pix[2] = palette[dataByte*3+2];
154                                                 pix[3] = 255;
155                                                 pix += 4;
156                                         }
157                                 }
158                         }
159                         else
160                         {
161                                 x++;
162                                 pix[0] = palette[dataByte*3];
163                                 pix[1] = palette[dataByte*3+1];
164                                 pix[2] = palette[dataByte*3+2];
165                                 pix[3] = 255;
166                                 pix += 4;
167                         }
168
169                 }
170         }
171         fclose(f);
172         image_width = pcx->xmax+1;
173         image_height = pcx->ymax+1;
174         return image_rgba;
175 }
176
177 /*
178 =========================================================
179
180 TARGA LOADING
181
182 =========================================================
183 */
184
185 typedef struct _TargaHeader {
186         unsigned char   id_length, colormap_type, image_type;
187         unsigned short  colormap_index, colormap_length;
188         unsigned char   colormap_size;
189         unsigned short  x_origin, y_origin, width, height;
190         unsigned char   pixel_size, attributes;
191 } TargaHeader;
192
193
194 TargaHeader             targa_header;
195
196 int fgetLittleShort (FILE *f)
197 {
198         byte    b1, b2;
199
200         b1 = fgetc(f);
201         b2 = fgetc(f);
202
203         return (short)(b1 + b2*256);
204 }
205
206 int fgetLittleLong (FILE *f)
207 {
208         byte    b1, b2, b3, b4;
209
210         b1 = fgetc(f);
211         b2 = fgetc(f);
212         b3 = fgetc(f);
213         b4 = fgetc(f);
214
215         return b1 + (b2<<8) + (b3<<16) + (b4<<24);
216 }
217
218
219 /*
220 =============
221 LoadTGA
222 =============
223 */
224 byte* LoadTGA (FILE *fin, int matchwidth, int matchheight)
225 {
226         int                             columns, rows, numPixels;
227         byte                    *pixbuf;
228         int                             row, column;
229         byte                    *image_rgba;
230
231         targa_header.id_length = fgetc(fin);
232         targa_header.colormap_type = fgetc(fin);
233         targa_header.image_type = fgetc(fin);
234         
235         targa_header.colormap_index = fgetLittleShort(fin);
236         targa_header.colormap_length = fgetLittleShort(fin);
237         targa_header.colormap_size = fgetc(fin);
238         targa_header.x_origin = fgetLittleShort(fin);
239         targa_header.y_origin = fgetLittleShort(fin);
240         targa_header.width = fgetLittleShort(fin);
241         targa_header.height = fgetLittleShort(fin);
242         if (matchwidth && targa_header.width != matchwidth)
243                 return NULL;
244         if (matchheight && targa_header.height != matchheight)
245                 return NULL;
246         targa_header.pixel_size = fgetc(fin);
247         targa_header.attributes = fgetc(fin);
248
249         if (targa_header.image_type!=2 
250                 && targa_header.image_type!=10) 
251                 Host_Error ("LoadTGA: Only type 2 and 10 targa RGB images supported\n");
252
253         if (targa_header.colormap_type !=0 
254                 || (targa_header.pixel_size!=32 && targa_header.pixel_size!=24))
255                 Host_Error ("LoadTGA: Only 32 or 24 bit images supported (no colormaps)\n");
256
257         columns = targa_header.width;
258         rows = targa_header.height;
259         numPixels = columns * rows;
260
261         image_rgba = qmalloc(numPixels*4);
262         
263         if (targa_header.id_length != 0)
264                 fseek(fin, targa_header.id_length, SEEK_CUR);  // skip TARGA image comment
265         
266         if (targa_header.image_type==2) {  // Uncompressed, RGB images
267                 for(row=rows-1; row>=0; row--) {
268                         pixbuf = image_rgba + row*columns*4;
269                         for(column=0; column<columns; column++) {
270                                 unsigned char red = 0,green = 0,blue = 0,alphabyte = 0;
271                                 switch (targa_header.pixel_size) {
272                                         case 24:
273                                                         
274                                                         blue = getc(fin);
275                                                         green = getc(fin);
276                                                         red = getc(fin);
277                                                         *pixbuf++ = red;
278                                                         *pixbuf++ = green;
279                                                         *pixbuf++ = blue;
280                                                         *pixbuf++ = 255;
281                                                         break;
282                                         case 32:
283                                                         blue = getc(fin);
284                                                         green = getc(fin);
285                                                         red = getc(fin);
286                                                         alphabyte = getc(fin);
287                                                         *pixbuf++ = red;
288                                                         *pixbuf++ = green;
289                                                         *pixbuf++ = blue;
290                                                         *pixbuf++ = alphabyte;
291                                                         break;
292                                 }
293                         }
294                 }
295         }
296         else if (targa_header.image_type==10) {   // Runlength encoded RGB images
297                 unsigned char red = 0,green = 0,blue = 0,alphabyte = 0,packetHeader,packetSize,j;
298                 for(row=rows-1; row>=0; row--) {
299                         pixbuf = image_rgba + row*columns*4;
300                         for(column=0; column<columns; ) {
301                                 packetHeader=getc(fin);
302                                 packetSize = 1 + (packetHeader & 0x7f);
303                                 if (packetHeader & 0x80) {        // run-length packet
304                                         switch (targa_header.pixel_size) {
305                                                 case 24:
306                                                                 blue = getc(fin);
307                                                                 green = getc(fin);
308                                                                 red = getc(fin);
309                                                                 alphabyte = 255;
310                                                                 break;
311                                                 case 32:
312                                                                 blue = getc(fin);
313                                                                 green = getc(fin);
314                                                                 red = getc(fin);
315                                                                 alphabyte = getc(fin);
316                                                                 break;
317                                         }
318         
319                                         for(j=0;j<packetSize;j++) {
320                                                 *pixbuf++=red;
321                                                 *pixbuf++=green;
322                                                 *pixbuf++=blue;
323                                                 *pixbuf++=alphabyte;
324                                                 column++;
325                                                 if (column==columns) { // run spans across rows
326                                                         column=0;
327                                                         if (row>0)
328                                                                 row--;
329                                                         else
330                                                                 goto breakOut;
331                                                         pixbuf = image_rgba + row*columns*4;
332                                                 }
333                                         }
334                                 }
335                                 else {                            // non run-length packet
336                                         for(j=0;j<packetSize;j++) {
337                                                 switch (targa_header.pixel_size) {
338                                                         case 24:
339                                                                         blue = getc(fin);
340                                                                         green = getc(fin);
341                                                                         red = getc(fin);
342                                                                         *pixbuf++ = red;
343                                                                         *pixbuf++ = green;
344                                                                         *pixbuf++ = blue;
345                                                                         *pixbuf++ = 255;
346                                                                         break;
347                                                         case 32:
348                                                                         blue = getc(fin);
349                                                                         green = getc(fin);
350                                                                         red = getc(fin);
351                                                                         alphabyte = getc(fin);
352                                                                         *pixbuf++ = red;
353                                                                         *pixbuf++ = green;
354                                                                         *pixbuf++ = blue;
355                                                                         *pixbuf++ = alphabyte;
356                                                                         break;
357                                                 }
358                                                 column++;
359                                                 if (column==columns) { // pixel packet run spans across rows
360                                                         column=0;
361                                                         if (row>0)
362                                                                 row--;
363                                                         else
364                                                                 goto breakOut;
365                                                         pixbuf = image_rgba + row*columns*4;
366                                                 }                                               
367                                         }
368                                 }
369                         }
370                         breakOut:;
371                 }
372         }
373         
374         fclose(fin);
375         image_width = columns;
376         image_height = rows;
377         return image_rgba;
378 }
379
380 /*
381 ============
382 LoadLMP
383 ============
384 */
385 byte* LoadLMP (FILE *f, int matchwidth, int matchheight)
386 {
387         byte    *image_rgba;
388         int             width, height;
389
390         // parse the very complicated header *chuckle*
391         width = fgetLittleLong(f);
392         height = fgetLittleLong(f);
393         if ((unsigned) width > 4096 || (unsigned) height > 4096)
394                 Host_Error("LoadLMP: invalid size\n");
395         if (matchwidth && width != matchwidth)
396                 return NULL;
397         if (matchheight && height != matchheight)
398                 return NULL;
399
400         image_rgba = qmalloc(width*height*4);
401         fread(image_rgba + width*height*3, 1, width*height, f);
402         fclose(f);
403
404         Image_Copy8bitRGBA(image_rgba + width*height*3, image_rgba, width*height, d_8to24table);
405         image_width = width;
406         image_height = height;
407         return image_rgba;
408 }
409
410 void Image_StripImageExtension (char *in, char *out)
411 {
412         char *end, *temp;
413         end = in + strlen(in);
414         if ((end - in) >= 4)
415         {
416                 temp = end - 4;
417                 if (strcmp(temp, ".tga") == 0 || strcmp(temp, ".pcx") == 0 || strcmp(temp, ".lmp") == 0)
418                         end = temp;
419                 while (in < end)
420                         *out++ = *in++;
421                 *out++ = 0;
422         }
423         else
424                 strcpy(out, in);
425 }
426
427 byte* loadimagepixels (char* filename, qboolean complain, int matchwidth, int matchheight)
428 {
429         FILE    *f;
430         char    basename[256], name[256];
431         byte    *image_rgba, *c;
432         Image_StripImageExtension(filename, basename); // strip .tga, .pcx and .lmp extensions to allow replacement by other types
433         // replace *'s with #, so commandline utils don't get confused when dealing with the external files
434         for (c = basename;*c;c++)
435                 if (*c == '*')
436                         *c = '#';
437         sprintf (name, "textures/%s.tga", basename);
438         COM_FOpenFile (name, &f, true);
439         if (f)
440                 return LoadTGA (f, matchwidth, matchheight);
441         sprintf (name, "textures/%s.pcx", basename);
442         COM_FOpenFile (name, &f, true);
443         if (f)
444                 return LoadPCX (f, matchwidth, matchheight);
445         sprintf (name, "%s.tga", basename);
446         COM_FOpenFile (name, &f, true);
447         if (f)
448                 return LoadTGA (f, matchwidth, matchheight);
449         sprintf (name, "%s.pcx", basename);
450         COM_FOpenFile (name, &f, true);
451         if (f)
452                 return LoadPCX (f, matchwidth, matchheight);
453         sprintf (name, "%s.lmp", basename);
454         COM_FOpenFile (name, &f, true);
455         if (f)
456                 return LoadLMP (f, matchwidth, matchheight);
457         if ((image_rgba = W_GetTexture(basename, matchwidth, matchheight)))
458                 return image_rgba;
459         COM_StripExtension(filename, basename); // do it again with a * this time
460         if ((image_rgba = W_GetTexture(basename, matchwidth, matchheight)))
461                 return image_rgba;
462         if (complain)
463                 Con_Printf ("Couldn't load %s.tga or .pcx\n", filename);
464         return NULL;
465 }
466
467 int image_makemask (byte *in, byte *out, int size)
468 {
469         int             i, count;
470         count = 0;
471         for (i = 0;i < size;i++)
472         {
473                 out[0] = out[1] = out[2] = 255;
474                 out[3] = in[3];
475                 if (in[3] != 255)
476                         count++;
477                 in += 4;
478                 out += 4;
479         }
480         return count;
481 }
482
483 byte* loadimagepixelsmask (char* filename, qboolean complain, int matchwidth, int matchheight)
484 {
485         byte    *in, *data;
486         in = data = loadimagepixels(filename, complain, matchwidth, matchheight);
487         if (!data)
488                 return NULL;
489         if (image_makemask(data, data, image_width * image_height))
490                 return data; // some transparency
491         else
492         {
493                 qfree(data);
494                 return NULL; // all opaque
495         }
496 }
497
498 int loadtextureimage (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap)
499 {
500         int texnum;
501         byte *data;
502         if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
503                 return 0;
504         texnum = GL_LoadTexture (filename, image_width, image_height, data, mipmap, true, 4);
505         qfree(data);
506         return texnum;
507 }
508
509 int loadtextureimagemask (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap)
510 {
511         int texnum;
512         byte *data;
513         if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
514                 return 0;
515         texnum = GL_LoadTexture (filename, image_width, image_height, data, mipmap, true, 4);
516         qfree(data);
517         return texnum;
518 }
519
520 int image_masktexnum;
521 int loadtextureimagewithmask (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap)
522 {
523         int texnum, count;
524         byte *data;
525         char *filename2;
526         image_masktexnum = 0;
527         if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
528                 return 0;
529         texnum = GL_LoadTexture (filename, image_width, image_height, data, mipmap, true, 4);
530         count = image_makemask(data, data, image_width * image_height);
531         if (count)
532         {
533                 filename2 = qmalloc(strlen(filename) + 6);
534                 sprintf(filename2, "%s_mask", filename);
535                 image_masktexnum = GL_LoadTexture (filename2, image_width, image_height, data, mipmap, true, 4);
536                 qfree(filename2);
537         }
538         qfree(data);
539         return texnum;
540 }
541
542 void Image_WriteTGARGB_preflipped (char *filename, int width, int height, byte *data)
543 {
544         byte *buffer, *in, *out, *end;
545
546         buffer = qmalloc(width*height*3 + 18);
547
548         memset (buffer, 0, 18);
549         buffer[2] = 2;          // uncompressed type
550         buffer[12] = (width >> 0) & 0xFF;
551         buffer[13] = (width >> 8) & 0xFF;
552         buffer[14] = (height >> 0) & 0xFF;
553         buffer[15] = (height >> 8) & 0xFF;
554         buffer[16] = 24;        // pixel size
555
556         // swap rgb to bgr
557         in = data;
558         out = buffer + 18;
559         end = in + width*height*3;
560         for (;in < end;in += 3)
561         {
562                 *out++ = in[2];
563                 *out++ = in[1];
564                 *out++ = in[0];
565         }
566         COM_WriteFile (filename, buffer, width*height*3 + 18 );
567
568         qfree(buffer);
569 }
570
571 void Image_WriteTGARGB (char *filename, int width, int height, byte *data)
572 {
573         int y;
574         byte *buffer, *in, *out, *end;
575
576         buffer = qmalloc(width*height*3 + 18);
577
578         memset (buffer, 0, 18);
579         buffer[2] = 2;          // uncompressed type
580         buffer[12] = (width >> 0) & 0xFF;
581         buffer[13] = (width >> 8) & 0xFF;
582         buffer[14] = (height >> 0) & 0xFF;
583         buffer[15] = (height >> 8) & 0xFF;
584         buffer[16] = 24;        // pixel size
585
586         // swap rgb to bgr and flip upside down
587         out = buffer + 18;
588         for (y = height - 1;y >= 0;y--)
589         {
590                 in = data + y * width * 3;
591                 end = in + width * 3;
592                 for (;in < end;in += 3)
593                 {
594                         *out++ = in[2];
595                         *out++ = in[1];
596                         *out++ = in[0];
597                 }
598         }
599         COM_WriteFile (filename, buffer, width*height*3 + 18 );
600
601         qfree(buffer);
602 }
603
604 void Image_WriteTGARGBA (char *filename, int width, int height, byte *data)
605 {
606         int y;
607         byte *buffer, *in, *out, *end;
608
609         buffer = qmalloc(width*height*4 + 18);
610
611         memset (buffer, 0, 18);
612         buffer[2] = 2;          // uncompressed type
613         buffer[12] = (width >> 0) & 0xFF;
614         buffer[13] = (width >> 8) & 0xFF;
615         buffer[14] = (height >> 0) & 0xFF;
616         buffer[15] = (height >> 8) & 0xFF;
617         buffer[16] = 32;        // pixel size
618
619         // swap rgba to bgra and flip upside down
620         out = buffer + 18;
621         for (y = height - 1;y >= 0;y--)
622         {
623                 in = data + y * width * 4;
624                 end = in + width * 4;
625                 for (;in < end;in += 4)
626                 {
627                         *out++ = in[2];
628                         *out++ = in[1];
629                         *out++ = in[0];
630                         *out++ = in[3];
631                 }
632         }
633         COM_WriteFile (filename, buffer, width*height*4 + 18 );
634
635         qfree(buffer);
636 }