]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - image.c
e3b62d4bd2166835603824926a277193480564c4
[xonotic/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 = malloc( 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 = malloc (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 = malloc(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         byte *end;
413         end = in + strlen(in);
414         if ((end - in) >= 4)
415         {
416                 if (strcmp(end - 4, ".tga") == 0 || strcmp(end - 4, ".pcx") == 0 || strcmp(end - 4, ".lmp") == 0)
417                         end -= 4;
418                 while (in < end)
419                         *out++ = *in++;
420                 *out++ = 0;
421         }
422         else
423                 strcpy(out, in);
424 }
425
426 byte* loadimagepixels (char* filename, qboolean complain, int matchwidth, int matchheight)
427 {
428         FILE    *f;
429         char    basename[256], name[256];
430         byte    *image_rgba, *c;
431         Image_StripImageExtension(filename, basename); // strip .tga, .pcx and .lmp extensions to allow replacement by other types
432         // replace *'s with #, so commandline utils don't get confused when dealing with the external files
433         for (c = basename;*c;c++)
434                 if (*c == '*')
435                         *c = '#';
436         sprintf (name, "textures/%s.tga", basename);
437         COM_FOpenFile (name, &f, true);
438         if (f)
439                 return LoadTGA (f, matchwidth, matchheight);
440         sprintf (name, "textures/%s.pcx", basename);
441         COM_FOpenFile (name, &f, true);
442         if (f)
443                 return LoadPCX (f, matchwidth, matchheight);
444         sprintf (name, "%s.tga", basename);
445         Con_Printf("name = %s : ", name);
446         COM_FOpenFile (name, &f, true);
447         if (f)
448         {
449                 Con_Printf("succeeded\n");
450                 return LoadTGA (f, matchwidth, matchheight);
451         }
452         else
453                 Con_Printf("failed\n");
454         sprintf (name, "%s.pcx", basename);
455         COM_FOpenFile (name, &f, true);
456         if (f)
457                 return LoadPCX (f, matchwidth, matchheight);
458         sprintf (name, "%s.lmp", basename);
459         COM_FOpenFile (name, &f, true);
460         if (f)
461                 return LoadLMP (f, matchwidth, matchheight);
462         if ((image_rgba = W_GetTexture(basename, matchwidth, matchheight)))
463                 return image_rgba;
464         COM_StripExtension(filename, basename); // do it again with a * this time
465         if ((image_rgba = W_GetTexture(basename, matchwidth, matchheight)))
466                 return image_rgba;
467         if (complain)
468                 Con_Printf ("Couldn't load %s.tga or .pcx\n", filename);
469         return NULL;
470 }
471
472 int image_makemask (byte *in, byte *out, int size)
473 {
474         int             i, count;
475         count = 0;
476         for (i = 0;i < size;i++)
477         {
478                 out[0] = out[1] = out[2] = 255;
479                 out[3] = in[3];
480                 if (in[3] != 255)
481                         count++;
482                 in += 4;
483                 out += 4;
484         }
485         return count;
486 }
487
488 byte* loadimagepixelsmask (char* filename, qboolean complain, int matchwidth, int matchheight)
489 {
490         byte    *in, *data;
491         in = data = loadimagepixels(filename, complain, matchwidth, matchheight);
492         if (!data)
493                 return NULL;
494         if (image_makemask(data, data, image_width * image_height))
495                 return data; // some transparency
496         else
497         {
498                 free(data);
499                 return NULL; // all opaque
500         }
501 }
502
503 int loadtextureimage (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap)
504 {
505         int texnum;
506         byte *data;
507         if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
508                 return 0;
509         texnum = GL_LoadTexture (filename, image_width, image_height, data, mipmap, true, 4);
510         free(data);
511         return texnum;
512 }
513
514 int loadtextureimagemask (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap)
515 {
516         int texnum;
517         byte *data;
518         if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
519                 return 0;
520         texnum = GL_LoadTexture (filename, image_width, image_height, data, mipmap, true, 4);
521         free(data);
522         return texnum;
523 }
524
525 int image_masktexnum;
526 int loadtextureimagewithmask (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap)
527 {
528         int texnum, count;
529         byte *data;
530         char *filename2;
531         image_masktexnum = 0;
532         if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
533                 return 0;
534         texnum = GL_LoadTexture (filename, image_width, image_height, data, mipmap, true, 4);
535         count = image_makemask(data, data, image_width * image_height);
536         if (count)
537         {
538                 filename2 = malloc(strlen(filename) + 6);
539                 sprintf(filename2, "%s_mask", filename);
540                 image_masktexnum = GL_LoadTexture (filename2, image_width, image_height, data, mipmap, true, 4);
541                 free(filename2);
542         }
543         free(data);
544         return texnum;
545 }
546
547 void Image_WriteTGARGB (char *filename, int width, int height, byte *data)
548 {
549         byte *buffer, *in, *out, *end;
550
551         buffer = malloc(width*height*3 + 18);
552
553         memset (buffer, 0, 18);
554         buffer[2] = 2;          // uncompressed type
555         buffer[12] = (width >> 0) & 0xFF;
556         buffer[13] = (width >> 8) & 0xFF;
557         buffer[14] = (height >> 0) & 0xFF;
558         buffer[15] = (height >> 8) & 0xFF;
559         buffer[16] = 24;        // pixel size
560
561         // swap rgb to bgr
562         in = data;
563         end = in + width*height*3;
564         out = buffer + 18;
565         for (;in < end;in += 3)
566         {
567                 *out++ = in[2];
568                 *out++ = in[1];
569                 *out++ = in[0];
570         }
571         COM_WriteFile (filename, buffer, glwidth*glheight*3 + 18 );
572
573         free(buffer);
574 }