7 // note: pal must be 32bit color
8 void Image_Copy8bitRGBA(byte *in, byte *out, int pixels, int *pal)
10 int *iout = (void *)out;
45 void Image_CopyRGBAGamma(byte *in, byte *out, int pixels)
49 out[0] = texgamma[in[0]];
50 out[1] = texgamma[in[1]];
51 out[2] = texgamma[in[2]];
60 =================================================================
64 =================================================================
73 unsigned short xmin,ymin,xmax,ymax;
74 unsigned short hres,vres;
75 unsigned char palette[48];
78 unsigned short bytes_per_line;
79 unsigned short palette_type;
88 byte* LoadPCX (byte *f, int matchwidth, int matchheight)
91 byte *palette, *a, *b, *image_rgba, *fin, *pbuf, *enddata;
92 int x, y, x2, dataByte;
94 if (loadsize < sizeof(pcx) + 768)
96 Con_Printf ("Bad pcx file\n");
103 memcpy(&pcx, fin, sizeof(pcx));
106 // LordHavoc: big-endian support ported from QF newtree
107 pcx.xmax = LittleShort (pcx.xmax);
108 pcx.xmin = LittleShort (pcx.xmin);
109 pcx.ymax = LittleShort (pcx.ymax);
110 pcx.ymin = LittleShort (pcx.ymin);
111 pcx.hres = LittleShort (pcx.hres);
112 pcx.vres = LittleShort (pcx.vres);
113 pcx.bytes_per_line = LittleShort (pcx.bytes_per_line);
114 pcx.palette_type = LittleShort (pcx.palette_type);
116 if (pcx.manufacturer != 0x0a || pcx.version != 5 || pcx.encoding != 1 || pcx.bits_per_pixel != 8 || pcx.xmax > 320 || pcx.ymax > 256)
118 Con_Printf ("Bad pcx file\n");
123 if (matchwidth && (pcx.xmax+1) != matchwidth)
128 if (matchheight && (pcx.ymax+1) != matchheight)
134 image_width = pcx.xmax+1;
135 image_height = pcx.ymax+1;
137 palette = f + loadsize - 768;
139 image_rgba = qmalloc(image_width*image_height*4);
142 Con_Printf("LoadPCX: not enough memory for %i by %i image\n", image_width, image_height);
146 pbuf = image_rgba + image_width*image_height*3;
149 for (y = 0;y < image_height && fin < enddata;y++)
151 a = pbuf + y * image_width;
152 for (x = 0;x < image_width && fin < enddata;)
159 x2 = x + (dataByte & 0x3F);
161 if (x2 > image_width)
162 x2 = image_width; // technically an error
169 while(x < image_width)
176 for(x = 0;x < image_width*image_height;x++)
190 =========================================================
194 =========================================================
197 typedef struct _TargaHeader
199 unsigned char id_length, colormap_type, image_type;
200 unsigned short colormap_index, colormap_length;
201 unsigned char colormap_size;
202 unsigned short x_origin, y_origin, width, height;
203 unsigned char pixel_size, attributes;
207 TargaHeader targa_header;
215 byte* LoadTGA (byte *f, int matchwidth, int matchheight)
217 int columns, rows, row, column;
218 byte *pixbuf, *image_rgba, *fin, *enddata;
225 targa_header.id_length = f[0];
226 targa_header.colormap_type = f[1];
227 targa_header.image_type = f[2];
229 targa_header.colormap_index = f[3] + f[4] * 256;
230 targa_header.colormap_length = f[5] + f[6] * 256;
231 targa_header.colormap_size = f[7];
232 targa_header.x_origin = f[8] + f[9] * 256;
233 targa_header.y_origin = f[10] + f[11] * 256;
234 targa_header.width = f[12] + f[13] * 256;
235 targa_header.height = f[14] + f[15] * 256;
236 if (matchwidth && targa_header.width != matchwidth)
241 if (matchheight && targa_header.height != matchheight)
246 targa_header.pixel_size = f[16];
247 targa_header.attributes = f[17];
249 if (targa_header.image_type != 2 && targa_header.image_type != 10)
251 Con_Printf ("LoadTGA: Only type 2 and 10 targa RGB images supported\n");
256 if (targa_header.colormap_type != 0 || (targa_header.pixel_size != 32 && targa_header.pixel_size != 24))
258 Con_Printf ("LoadTGA: Only 32 or 24 bit images supported (no colormaps)\n");
263 enddata = f + loadsize;
265 columns = targa_header.width;
266 rows = targa_header.height;
268 image_rgba = qmalloc(columns * rows * 4);
271 Con_Printf ("LoadTGA: not enough memory for %i by %i image\n", columns, rows);
277 if (targa_header.id_length != 0)
278 fin += targa_header.id_length; // skip TARGA image comment
280 if (targa_header.image_type == 2)
282 // Uncompressed, RGB images
283 for(row = rows - 1;row >= 0;row--)
285 pixbuf = image_rgba + row*columns*4;
286 for(column = 0;column < columns;column++)
288 switch (targa_header.pixel_size)
291 if (fin + 3 > enddata)
300 if (fin + 4 > enddata)
312 else if (targa_header.image_type==10)
314 // Runlength encoded RGB images
315 unsigned char red = 0, green = 0, blue = 0, alphabyte = 0, packetHeader, packetSize, j;
316 for(row = rows - 1;row >= 0;row--)
318 pixbuf = image_rgba + row * columns * 4;
319 for(column = 0;column < columns;)
323 packetHeader = *fin++;
324 packetSize = 1 + (packetHeader & 0x7f);
325 if (packetHeader & 0x80)
328 switch (targa_header.pixel_size)
331 if (fin + 3 > enddata)
339 if (fin + 4 > enddata)
348 for(j = 0;j < packetSize;j++)
353 *pixbuf++ = alphabyte;
355 if (column == columns)
357 // run spans across rows
363 pixbuf = image_rgba + row * columns * 4;
369 // non run-length packet
370 for(j = 0;j < packetSize;j++)
372 switch (targa_header.pixel_size)
375 if (fin + 3 > enddata)
384 if (fin + 4 > enddata)
394 if (column == columns)
396 // pixel packet run spans across rows
402 pixbuf = image_rgba + row * columns * 4;
412 image_width = columns;
423 byte* LoadLMP (byte *f, int matchwidth, int matchheight)
430 Con_Printf("LoadLMP: invalid LMP file\n");
435 // parse the very complicated header *chuckle*
436 width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
437 height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
438 if ((unsigned) width > 4096 || (unsigned) height > 4096)
440 Con_Printf("LoadLMP: invalid size\n");
444 if (matchwidth && width != matchwidth)
449 if (matchheight && height != matchheight)
455 if (loadsize < 8 + width * height)
457 Con_Printf("LoadLMP: invalid LMP file\n");
463 image_height = height;
465 image_rgba = qmalloc(image_width * image_height * 4);
468 Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
472 Image_Copy8bitRGBA(f + 8, image_rgba, image_width * image_height, d_8to24table);
477 void Image_StripImageExtension (char *in, char *out)
480 end = in + strlen(in);
484 if (strcmp(temp, ".tga") == 0 || strcmp(temp, ".pcx") == 0 || strcmp(temp, ".lmp") == 0)
494 byte* loadimagepixels (char* filename, qboolean complain, int matchwidth, int matchheight)
497 char basename[256], name[256];
499 Image_StripImageExtension(filename, basename); // strip .tga, .pcx and .lmp extensions to allow replacement by other types
500 // replace *'s with #, so commandline utils don't get confused when dealing with the external files
501 for (c = basename;*c;c++)
504 sprintf (name, "textures/%s.tga", basename);
505 f = COM_LoadMallocFile(name, true);
507 return LoadTGA (f, matchwidth, matchheight);
508 sprintf (name, "textures/%s.pcx", basename);
509 f = COM_LoadMallocFile(name, true);
511 return LoadPCX (f, matchwidth, matchheight);
512 sprintf (name, "%s.tga", basename);
513 f = COM_LoadMallocFile(name, true);
515 return LoadTGA (f, matchwidth, matchheight);
516 sprintf (name, "%s.pcx", basename);
517 f = COM_LoadMallocFile(name, true);
519 return LoadPCX (f, matchwidth, matchheight);
520 sprintf (name, "%s.lmp", basename);
521 f = COM_LoadMallocFile(name, true);
523 return LoadLMP (f, matchwidth, matchheight);
525 Con_Printf ("Couldn't load %s.tga, .pcx, .lmp\n", filename);
529 int image_makemask (byte *in, byte *out, int size)
533 for (i = 0;i < size;i++)
535 out[0] = out[1] = out[2] = 255;
545 byte* loadimagepixelsmask (char* filename, qboolean complain, int matchwidth, int matchheight)
548 in = data = loadimagepixels(filename, complain, matchwidth, matchheight);
551 if (image_makemask(data, data, image_width * image_height))
552 return data; // some transparency
556 return NULL; // all opaque
560 rtexture_t *loadtextureimage (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap, qboolean precache)
564 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
566 rt = R_LoadTexture (filename, image_width, image_height, data, TEXF_ALPHA | TEXF_RGBA | (mipmap ? TEXF_MIPMAP : 0) | (mipmap ? TEXF_PRECACHE : 0));
571 rtexture_t *loadtextureimagemask (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap, qboolean precache)
575 if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
577 rt = R_LoadTexture (filename, image_width, image_height, data, TEXF_ALPHA | TEXF_RGBA | (mipmap ? TEXF_MIPMAP : 0) | (mipmap ? TEXF_PRECACHE : 0));
582 rtexture_t *image_masktex;
583 rtexture_t *loadtextureimagewithmask (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap, qboolean precache)
589 image_masktex = NULL;
590 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
592 rt = R_LoadTexture (filename, image_width, image_height, data, TEXF_ALPHA | TEXF_RGBA | (mipmap ? TEXF_MIPMAP : 0) | (mipmap ? TEXF_PRECACHE : 0));
593 count = image_makemask(data, data, image_width * image_height);
596 filename2 = qmalloc(strlen(filename) + 6);
597 sprintf(filename2, "%s_mask", filename);
598 image_masktex = R_LoadTexture (filename2, image_width, image_height, data, TEXF_ALPHA | TEXF_RGBA | (mipmap ? TEXF_MIPMAP : 0) | (mipmap ? TEXF_PRECACHE : 0));
605 void Image_WriteTGARGB_preflipped (char *filename, int width, int height, byte *data)
607 byte *buffer, *in, *out, *end;
609 buffer = qmalloc(width*height*3 + 18);
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] = 24; // pixel size
622 end = in + width*height*3;
623 for (;in < end;in += 3)
629 COM_WriteFile (filename, buffer, width*height*3 + 18 );
634 void Image_WriteTGARGB (char *filename, int width, int height, byte *data)
637 byte *buffer, *in, *out, *end;
639 buffer = qmalloc(width*height*3 + 18);
641 memset (buffer, 0, 18);
642 buffer[2] = 2; // uncompressed type
643 buffer[12] = (width >> 0) & 0xFF;
644 buffer[13] = (width >> 8) & 0xFF;
645 buffer[14] = (height >> 0) & 0xFF;
646 buffer[15] = (height >> 8) & 0xFF;
647 buffer[16] = 24; // pixel size
649 // swap rgb to bgr and flip upside down
651 for (y = height - 1;y >= 0;y--)
653 in = data + y * width * 3;
654 end = in + width * 3;
655 for (;in < end;in += 3)
662 COM_WriteFile (filename, buffer, width*height*3 + 18 );
667 void Image_WriteTGARGBA (char *filename, int width, int height, byte *data)
670 byte *buffer, *in, *out, *end;
672 buffer = qmalloc(width*height*4 + 18);
674 memset (buffer, 0, 18);
675 buffer[2] = 2; // uncompressed type
676 buffer[12] = (width >> 0) & 0xFF;
677 buffer[13] = (width >> 8) & 0xFF;
678 buffer[14] = (height >> 0) & 0xFF;
679 buffer[15] = (height >> 8) & 0xFF;
680 buffer[16] = 32; // pixel size
682 // swap rgba to bgra and flip upside down
684 for (y = height - 1;y >= 0;y--)
686 in = data + y * width * 4;
687 end = in + width * 4;
688 for (;in < end;in += 4)
696 COM_WriteFile (filename, buffer, width*height*4 + 18 );
701 qboolean Image_CheckAlpha(byte *data, int size, qboolean rgba)
707 for (end = data + size * 4, data += 3;data < end;data += 4)
713 // color 255 is transparent
714 for (end = data + size;data < end;data++)