7 void Image_GammaRemapRGB(qbyte *in, qbyte *out, int pixels, qbyte *gammar, qbyte *gammag, qbyte *gammab)
11 out[0] = gammar[in[0]];
12 out[1] = gammag[in[1]];
13 out[2] = gammab[in[2]];
19 // note: pal must be 32bit color
20 void Image_Copy8bitRGBA(qbyte *in, qbyte *out, int pixels, int *pal)
22 int *iout = (void *)out;
58 =================================================================
62 =================================================================
71 unsigned short xmin,ymin,xmax,ymax;
72 unsigned short hres,vres;
73 unsigned char palette[48];
76 unsigned short bytes_per_line;
77 unsigned short palette_type;
86 qbyte* LoadPCX (qbyte *f, int matchwidth, int matchheight)
89 qbyte *palette, *a, *b, *image_rgba, *fin, *pbuf, *enddata;
90 int x, y, x2, dataByte;
92 if (loadsize < sizeof(pcx) + 768)
94 Con_Printf ("Bad pcx file\n");
100 memcpy(&pcx, fin, sizeof(pcx));
103 // LordHavoc: big-endian support ported from QF newtree
104 pcx.xmax = LittleShort (pcx.xmax);
105 pcx.xmin = LittleShort (pcx.xmin);
106 pcx.ymax = LittleShort (pcx.ymax);
107 pcx.ymin = LittleShort (pcx.ymin);
108 pcx.hres = LittleShort (pcx.hres);
109 pcx.vres = LittleShort (pcx.vres);
110 pcx.bytes_per_line = LittleShort (pcx.bytes_per_line);
111 pcx.palette_type = LittleShort (pcx.palette_type);
113 if (pcx.manufacturer != 0x0a || pcx.version != 5 || pcx.encoding != 1 || pcx.bits_per_pixel != 8 || pcx.xmax > 320 || pcx.ymax > 256)
115 Con_Printf ("Bad pcx file\n");
119 if (matchwidth && (pcx.xmax+1) != matchwidth)
123 if (matchheight && (pcx.ymax+1) != matchheight)
128 image_width = pcx.xmax+1;
129 image_height = pcx.ymax+1;
131 palette = f + loadsize - 768;
133 image_rgba = Mem_Alloc(tempmempool, image_width*image_height*4);
136 Con_Printf("LoadPCX: not enough memory for %i by %i image\n", image_width, image_height);
139 pbuf = image_rgba + image_width*image_height*3;
142 for (y = 0;y < image_height && fin < enddata;y++)
144 a = pbuf + y * image_width;
145 for (x = 0;x < image_width && fin < enddata;)
152 x2 = x + (dataByte & 0x3F);
154 if (x2 > image_width)
155 x2 = image_width; // technically an error
162 while(x < image_width)
169 for(x = 0;x < image_width*image_height;x++)
182 =========================================================
186 =========================================================
189 typedef struct _TargaHeader
191 unsigned char id_length, colormap_type, image_type;
192 unsigned short colormap_index, colormap_length;
193 unsigned char colormap_size;
194 unsigned short x_origin, y_origin, width, height;
195 unsigned char pixel_size, attributes;
199 TargaHeader targa_header;
207 qbyte *LoadTGA (qbyte *f, int matchwidth, int matchheight)
209 int columns, rows, row, column;
210 qbyte *pixbuf, *image_rgba, *fin, *enddata;
214 targa_header.id_length = f[0];
215 targa_header.colormap_type = f[1];
216 targa_header.image_type = f[2];
218 targa_header.colormap_index = f[3] + f[4] * 256;
219 targa_header.colormap_length = f[5] + f[6] * 256;
220 targa_header.colormap_size = f[7];
221 targa_header.x_origin = f[8] + f[9] * 256;
222 targa_header.y_origin = f[10] + f[11] * 256;
223 targa_header.width = f[12] + f[13] * 256;
224 targa_header.height = f[14] + f[15] * 256;
225 if (matchwidth && targa_header.width != matchwidth)
227 if (matchheight && targa_header.height != matchheight)
229 targa_header.pixel_size = f[16];
230 targa_header.attributes = f[17];
232 if (targa_header.image_type != 2 && targa_header.image_type != 10)
234 Con_Printf ("LoadTGA: Only type 2 and 10 targa RGB images supported\n");
238 if (targa_header.colormap_type != 0 || (targa_header.pixel_size != 32 && targa_header.pixel_size != 24))
240 Con_Printf ("LoadTGA: Only 32 or 24 bit images supported (no colormaps)\n");
244 enddata = f + loadsize;
246 columns = targa_header.width;
247 rows = targa_header.height;
249 image_rgba = Mem_Alloc(tempmempool, columns * rows * 4);
252 Con_Printf ("LoadTGA: not enough memory for %i by %i image\n", columns, rows);
257 if (targa_header.id_length != 0)
258 fin += targa_header.id_length; // skip TARGA image comment
260 if (targa_header.image_type == 2)
262 // Uncompressed, RGB images
263 for(row = rows - 1;row >= 0;row--)
265 pixbuf = image_rgba + row*columns*4;
266 for(column = 0;column < columns;column++)
268 switch (targa_header.pixel_size)
271 if (fin + 3 > enddata)
280 if (fin + 4 > enddata)
292 else if (targa_header.image_type==10)
294 // Runlength encoded RGB images
295 unsigned char red = 0, green = 0, blue = 0, alphabyte = 0, packetHeader, packetSize, j;
296 for(row = rows - 1;row >= 0;row--)
298 pixbuf = image_rgba + row * columns * 4;
299 for(column = 0;column < columns;)
303 packetHeader = *fin++;
304 packetSize = 1 + (packetHeader & 0x7f);
305 if (packetHeader & 0x80)
308 switch (targa_header.pixel_size)
311 if (fin + 3 > enddata)
319 if (fin + 4 > enddata)
328 for(j = 0;j < packetSize;j++)
333 *pixbuf++ = alphabyte;
335 if (column == columns)
337 // run spans across rows
343 pixbuf = image_rgba + row * columns * 4;
349 // non run-length packet
350 for(j = 0;j < packetSize;j++)
352 switch (targa_header.pixel_size)
355 if (fin + 3 > enddata)
364 if (fin + 4 > enddata)
374 if (column == columns)
376 // pixel packet run spans across rows
382 pixbuf = image_rgba + row * columns * 4;
392 image_width = columns;
402 qbyte *LoadLMP (qbyte *f, int matchwidth, int matchheight)
409 Con_Printf("LoadLMP: invalid LMP file\n");
413 // parse the very complicated header *chuckle*
414 width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
415 height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
416 if ((unsigned) width > 4096 || (unsigned) height > 4096)
418 Con_Printf("LoadLMP: invalid size\n");
421 if ((matchwidth && width != matchwidth) || (matchheight && height != matchheight))
424 if (loadsize < 8 + width * height)
426 Con_Printf("LoadLMP: invalid LMP file\n");
431 image_height = height;
433 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
436 Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
439 Image_Copy8bitRGBA(f + 8, image_rgba, image_width * image_height, d_8to24table);
443 void Image_StripImageExtension (char *in, char *out)
446 end = in + strlen(in);
450 if (strcmp(temp, ".tga") == 0 || strcmp(temp, ".pcx") == 0 || strcmp(temp, ".lmp") == 0)
460 qbyte *loadimagepixels (char *filename, qboolean complain, int matchwidth, int matchheight)
463 char basename[256], name[256], *c;
464 Image_StripImageExtension(filename, basename); // strip .tga, .pcx and .lmp extensions to allow replacement by other types
465 // replace *'s with #, so commandline utils don't get confused when dealing with the external files
466 for (c = basename;*c;c++)
469 sprintf (name, "textures/%s.tga", basename);
470 f = COM_LoadFile(name, true);
473 data = LoadTGA (f, matchwidth, matchheight);
477 sprintf (name, "textures/%s.pcx", basename);
478 f = COM_LoadFile(name, true);
481 data = LoadPCX (f, matchwidth, matchheight);
485 sprintf (name, "%s.tga", basename);
486 f = COM_LoadFile(name, true);
489 data = LoadTGA (f, matchwidth, matchheight);
493 sprintf (name, "%s.pcx", basename);
494 f = COM_LoadFile(name, true);
497 data = LoadPCX (f, matchwidth, matchheight);
501 sprintf (name, "%s.lmp", basename);
502 f = COM_LoadFile(name, true);
505 data = LoadLMP (f, matchwidth, matchheight);
510 Con_Printf ("Couldn't load %s.tga, .pcx, .lmp\n", filename);
514 int image_makemask (qbyte *in, qbyte *out, int size)
518 for (i = 0;i < size;i++)
520 out[0] = out[1] = out[2] = 255;
530 qbyte* loadimagepixelsmask (char* filename, qboolean complain, int matchwidth, int matchheight)
533 in = data = loadimagepixels(filename, complain, matchwidth, matchheight);
536 if (image_makemask(data, data, image_width * image_height))
537 return data; // some transparency
541 return NULL; // all opaque
545 rtexture_t *loadtextureimage (rtexturepool_t *pool, char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap, qboolean precache)
549 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
551 rt = R_LoadTexture (pool, filename, image_width, image_height, data, TEXTYPE_RGBA, TEXF_ALPHA | (mipmap ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
556 rtexture_t *loadtextureimagemask (rtexturepool_t *pool, char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap, qboolean precache)
560 if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
562 rt = R_LoadTexture (pool, filename, image_width, image_height, data, TEXTYPE_RGBA, TEXF_ALPHA | (mipmap ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
567 rtexture_t *image_masktex;
568 rtexture_t *loadtextureimagewithmask (rtexturepool_t *pool, char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap, qboolean precache)
574 image_masktex = NULL;
575 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
577 rt = R_LoadTexture (pool, filename, image_width, image_height, data, TEXTYPE_RGBA, TEXF_ALPHA | (mipmap ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
578 count = image_makemask(data, data, image_width * image_height);
581 filename2 = Mem_Alloc(tempmempool, strlen(filename) + 6);
582 sprintf(filename2, "%s_mask", filename);
583 image_masktex = R_LoadTexture (pool, filename2, image_width, image_height, data, TEXTYPE_RGBA, TEXF_ALPHA | (mipmap ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
590 void Image_WriteTGARGB_preflipped (char *filename, int width, int height, qbyte *data)
592 qbyte *buffer, *in, *out, *end;
594 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
596 memset (buffer, 0, 18);
597 buffer[2] = 2; // uncompressed type
598 buffer[12] = (width >> 0) & 0xFF;
599 buffer[13] = (width >> 8) & 0xFF;
600 buffer[14] = (height >> 0) & 0xFF;
601 buffer[15] = (height >> 8) & 0xFF;
602 buffer[16] = 24; // pixel size
607 end = in + width*height*3;
608 for (;in < end;in += 3)
614 COM_WriteFile (filename, buffer, width*height*3 + 18 );
619 void Image_WriteTGARGB (char *filename, int width, int height, qbyte *data)
622 qbyte *buffer, *in, *out, *end;
624 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
626 memset (buffer, 0, 18);
627 buffer[2] = 2; // uncompressed type
628 buffer[12] = (width >> 0) & 0xFF;
629 buffer[13] = (width >> 8) & 0xFF;
630 buffer[14] = (height >> 0) & 0xFF;
631 buffer[15] = (height >> 8) & 0xFF;
632 buffer[16] = 24; // pixel size
634 // swap rgb to bgr and flip upside down
636 for (y = height - 1;y >= 0;y--)
638 in = data + y * width * 3;
639 end = in + width * 3;
640 for (;in < end;in += 3)
647 COM_WriteFile (filename, buffer, width*height*3 + 18 );
652 void Image_WriteTGARGBA (char *filename, int width, int height, qbyte *data)
655 qbyte *buffer, *in, *out, *end;
657 buffer = Mem_Alloc(tempmempool, width*height*4 + 18);
659 memset (buffer, 0, 18);
660 buffer[2] = 2; // uncompressed type
661 buffer[12] = (width >> 0) & 0xFF;
662 buffer[13] = (width >> 8) & 0xFF;
663 buffer[14] = (height >> 0) & 0xFF;
664 buffer[15] = (height >> 8) & 0xFF;
665 buffer[16] = 32; // pixel size
667 // swap rgba to bgra and flip upside down
669 for (y = height - 1;y >= 0;y--)
671 in = data + y * width * 4;
672 end = in + width * 4;
673 for (;in < end;in += 4)
681 COM_WriteFile (filename, buffer, width*height*4 + 18 );
686 qboolean Image_CheckAlpha(qbyte *data, int size, qboolean rgba)
692 for (end = data + size * 4, data += 3;data < end;data += 4)
698 // color 255 is transparent
699 for (end = data + size;data < end;data++)