2011-05-09 22:00:00 +02:00
|
|
|
/*
|
|
|
|
img_tga.c - tga format load & save
|
|
|
|
Copyright (C) 2007 Uncle Mike
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
*/
|
2008-01-28 22:00:00 +01:00
|
|
|
|
2008-10-22 22:00:00 +02:00
|
|
|
#include "imagelib.h"
|
2016-08-12 23:00:00 +02:00
|
|
|
#include "mathlib.h"
|
2008-01-28 22:00:00 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
=============
|
|
|
|
Image_LoadTGA
|
|
|
|
=============
|
|
|
|
*/
|
2010-10-26 22:00:00 +02:00
|
|
|
qboolean Image_LoadTGA( const char *name, const byte *buffer, size_t filesize )
|
2008-01-28 22:00:00 +01:00
|
|
|
{
|
2009-08-02 22:00:00 +02:00
|
|
|
int i, columns, rows, row_inc, row, col;
|
|
|
|
byte *buf_p, *pixbuf, *targa_rgba;
|
|
|
|
byte palette[256][4], red = 0, green = 0, blue = 0, alpha = 0;
|
|
|
|
int readpixelcount, pixelcount;
|
2016-08-12 23:00:00 +02:00
|
|
|
int reflectivity[3] = { 0, 0, 0 };
|
2010-10-26 22:00:00 +02:00
|
|
|
qboolean compressed;
|
2008-01-28 22:00:00 +01:00
|
|
|
tga_t targa_header;
|
|
|
|
|
2008-11-03 22:00:00 +01:00
|
|
|
if( filesize < sizeof( tga_t ))
|
2008-01-28 22:00:00 +01:00
|
|
|
return false;
|
|
|
|
|
2009-08-02 22:00:00 +02:00
|
|
|
buf_p = (byte *)buffer;
|
|
|
|
targa_header.id_length = *buf_p++;
|
|
|
|
targa_header.colormap_type = *buf_p++;
|
|
|
|
targa_header.image_type = *buf_p++;
|
|
|
|
|
|
|
|
targa_header.colormap_index = buf_p[0] + buf_p[1] * 256; buf_p += 2;
|
|
|
|
targa_header.colormap_length = buf_p[0] + buf_p[1] * 256; buf_p += 2;
|
|
|
|
targa_header.colormap_size = *buf_p; buf_p += 1;
|
2010-11-21 22:00:00 +01:00
|
|
|
targa_header.x_origin = *(short *)buf_p; buf_p += 2;
|
|
|
|
targa_header.y_origin = *(short *)buf_p; buf_p += 2;
|
|
|
|
targa_header.width = image.width = *(short *)buf_p; buf_p += 2;
|
|
|
|
targa_header.height = image.height = *(short *)buf_p; buf_p += 2;
|
2009-08-02 22:00:00 +02:00
|
|
|
targa_header.pixel_size = *buf_p++;
|
|
|
|
targa_header.attributes = *buf_p++;
|
|
|
|
if( targa_header.id_length != 0 ) buf_p += targa_header.id_length; // skip TARGA image comment
|
2008-01-28 22:00:00 +01:00
|
|
|
|
2008-08-14 22:00:00 +02:00
|
|
|
// check for tga file
|
2011-07-07 22:00:00 +02:00
|
|
|
if( !Image_ValidSize( name )) return false;
|
2008-01-28 22:00:00 +01:00
|
|
|
|
2008-10-19 22:00:00 +02:00
|
|
|
image.type = PF_RGBA_32; // always exctracted to 32-bit buffer
|
2008-01-28 22:00:00 +01:00
|
|
|
|
2009-08-02 22:00:00 +02:00
|
|
|
if( targa_header.image_type == 1 || targa_header.image_type == 9 )
|
2008-01-28 22:00:00 +01:00
|
|
|
{
|
2009-08-02 22:00:00 +02:00
|
|
|
// uncompressed colormapped image
|
|
|
|
if( targa_header.pixel_size != 8 )
|
2008-08-06 22:00:00 +02:00
|
|
|
{
|
2009-08-02 22:00:00 +02:00
|
|
|
MsgDev( D_WARN, "Image_LoadTGA: (%s) Only 8 bit images supported for type 1 and 9\n", name );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if( targa_header.colormap_length != 256 )
|
|
|
|
{
|
|
|
|
MsgDev( D_WARN, "Image_LoadTGA: (%s) Only 8 bit colormaps are supported for type 1 and 9\n", name );
|
2008-08-06 22:00:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if( targa_header.colormap_index )
|
|
|
|
{
|
2009-08-02 22:00:00 +02:00
|
|
|
MsgDev( D_WARN, "Image_LoadTGA: (%s) colormap_index is not supported for type 1 and 9\n", name );
|
2008-08-06 22:00:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if( targa_header.colormap_size == 24 )
|
|
|
|
{
|
2009-08-02 22:00:00 +02:00
|
|
|
for( i = 0; i < targa_header.colormap_length; i++ )
|
2008-08-06 22:00:00 +02:00
|
|
|
{
|
2009-08-02 22:00:00 +02:00
|
|
|
palette[i][2] = *buf_p++;
|
|
|
|
palette[i][1] = *buf_p++;
|
|
|
|
palette[i][0] = *buf_p++;
|
|
|
|
palette[i][3] = 255;
|
2008-08-06 22:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( targa_header.colormap_size == 32 )
|
|
|
|
{
|
2009-08-02 22:00:00 +02:00
|
|
|
for( i = 0; i < targa_header.colormap_length; i++ )
|
2008-08-06 22:00:00 +02:00
|
|
|
{
|
2009-08-02 22:00:00 +02:00
|
|
|
palette[i][2] = *buf_p++;
|
|
|
|
palette[i][1] = *buf_p++;
|
|
|
|
palette[i][0] = *buf_p++;
|
|
|
|
palette[i][3] = *buf_p++;
|
2008-08-06 22:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-08-02 22:00:00 +02:00
|
|
|
MsgDev( D_WARN, "Image_LoadTGA: (%s) only 24 and 32 bit colormaps are supported for type 1 and 9\n", name );
|
2008-08-06 22:00:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
2008-01-28 22:00:00 +01:00
|
|
|
}
|
2009-08-02 22:00:00 +02:00
|
|
|
else if( targa_header.image_type == 2 || targa_header.image_type == 10 )
|
2008-01-28 22:00:00 +01:00
|
|
|
{
|
2009-08-02 22:00:00 +02:00
|
|
|
// uncompressed or RLE compressed RGB
|
|
|
|
if( targa_header.pixel_size != 32 && targa_header.pixel_size != 24 )
|
2008-01-28 22:00:00 +01:00
|
|
|
{
|
2009-08-02 22:00:00 +02:00
|
|
|
MsgDev( D_WARN, "Image_LoadTGA: (%s) Only 32 or 24 bit images supported for type 2 and 10\n", name );
|
2008-01-28 22:00:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
2009-08-02 22:00:00 +02:00
|
|
|
}
|
|
|
|
else if( targa_header.image_type == 3 || targa_header.image_type == 11 )
|
|
|
|
{
|
|
|
|
// uncompressed greyscale
|
2008-08-06 22:00:00 +02:00
|
|
|
if( targa_header.pixel_size != 8 )
|
|
|
|
{
|
2009-08-02 22:00:00 +02:00
|
|
|
MsgDev( D_WARN, "Image_LoadTGA: (%s) Only 8 bit images supported for type 3 and 11\n", name );
|
2008-08-06 22:00:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
2008-01-28 22:00:00 +01:00
|
|
|
}
|
|
|
|
|
2009-08-02 22:00:00 +02:00
|
|
|
columns = targa_header.width;
|
|
|
|
rows = targa_header.height;
|
2008-01-28 22:00:00 +01:00
|
|
|
|
2008-10-19 22:00:00 +02:00
|
|
|
image.size = image.width * image.height * 4;
|
2018-05-26 23:00:00 +02:00
|
|
|
targa_rgba = image.rgba = Mem_Malloc( host.imagepool, image.size );
|
2008-01-28 22:00:00 +01:00
|
|
|
|
2009-08-02 22:00:00 +02:00
|
|
|
// if bit 5 of attributes isn't set, the image has been stored from bottom to top
|
2011-10-09 22:00:00 +02:00
|
|
|
if( !Image_CheckFlag( IL_DONTFLIP_TGA ) && targa_header.attributes & 0x20 )
|
2008-01-28 22:00:00 +01:00
|
|
|
{
|
2009-08-02 22:00:00 +02:00
|
|
|
pixbuf = targa_rgba;
|
|
|
|
row_inc = 0;
|
2008-01-28 22:00:00 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-08-02 22:00:00 +02:00
|
|
|
pixbuf = targa_rgba + ( rows - 1 ) * columns * 4;
|
|
|
|
row_inc = -columns * 4 * 2;
|
2008-01-28 22:00:00 +01:00
|
|
|
}
|
|
|
|
|
2009-08-02 22:00:00 +02:00
|
|
|
compressed = ( targa_header.image_type == 9 || targa_header.image_type == 10 || targa_header.image_type == 11 );
|
|
|
|
for( row = col = 0; row < rows; )
|
2008-01-28 22:00:00 +01:00
|
|
|
{
|
2009-08-02 22:00:00 +02:00
|
|
|
pixelcount = 0x10000;
|
|
|
|
readpixelcount = 0x10000;
|
2008-11-03 22:00:00 +01:00
|
|
|
|
2009-08-02 22:00:00 +02:00
|
|
|
if( compressed )
|
2008-01-28 22:00:00 +01:00
|
|
|
{
|
2009-08-02 22:00:00 +02:00
|
|
|
pixelcount = *buf_p++;
|
|
|
|
if( pixelcount & 0x80 ) // run-length packet
|
|
|
|
readpixelcount = 1;
|
|
|
|
pixelcount = 1 + ( pixelcount & 0x7f );
|
2008-01-28 22:00:00 +01:00
|
|
|
}
|
2009-08-02 22:00:00 +02:00
|
|
|
|
|
|
|
while( pixelcount-- && ( row < rows ) )
|
2008-08-06 22:00:00 +02:00
|
|
|
{
|
2009-08-02 22:00:00 +02:00
|
|
|
if( readpixelcount-- > 0 )
|
2008-08-06 22:00:00 +02:00
|
|
|
{
|
2009-08-02 22:00:00 +02:00
|
|
|
switch( targa_header.image_type )
|
2008-08-06 22:00:00 +02:00
|
|
|
{
|
2009-08-02 22:00:00 +02:00
|
|
|
case 1:
|
|
|
|
case 9:
|
|
|
|
// colormapped image
|
|
|
|
blue = *buf_p++;
|
|
|
|
red = palette[blue][0];
|
|
|
|
green = palette[blue][1];
|
|
|
|
alpha = palette[blue][3];
|
|
|
|
blue = palette[blue][2];
|
|
|
|
if( alpha != 255 ) image.flags |= IMAGE_HAS_ALPHA;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
case 10:
|
|
|
|
// 24 or 32 bit image
|
|
|
|
blue = *buf_p++;
|
|
|
|
green = *buf_p++;
|
|
|
|
red = *buf_p++;
|
|
|
|
alpha = 255;
|
|
|
|
if( targa_header.pixel_size == 32 )
|
2008-08-06 22:00:00 +02:00
|
|
|
{
|
2009-08-02 22:00:00 +02:00
|
|
|
alpha = *buf_p++;
|
|
|
|
if( alpha != 255 )
|
|
|
|
image.flags |= IMAGE_HAS_ALPHA;
|
2008-08-06 22:00:00 +02:00
|
|
|
}
|
2009-08-02 22:00:00 +02:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
case 11:
|
|
|
|
// greyscale image
|
|
|
|
blue = green = red = *buf_p++;
|
|
|
|
alpha = 255;
|
|
|
|
break;
|
2008-08-06 22:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
2008-11-03 22:00:00 +01:00
|
|
|
|
2009-08-02 22:00:00 +02:00
|
|
|
if( red != green || green != blue )
|
|
|
|
image.flags |= IMAGE_HAS_COLOR;
|
2008-11-03 22:00:00 +01:00
|
|
|
|
2016-08-12 23:00:00 +02:00
|
|
|
reflectivity[0] += red;
|
|
|
|
reflectivity[1] += green;
|
|
|
|
reflectivity[2] += blue;
|
|
|
|
|
2009-08-02 22:00:00 +02:00
|
|
|
*pixbuf++ = red;
|
|
|
|
*pixbuf++ = green;
|
|
|
|
*pixbuf++ = blue;
|
|
|
|
*pixbuf++ = alpha;
|
|
|
|
if( ++col == columns )
|
|
|
|
{
|
|
|
|
// run spans across rows
|
|
|
|
row++;
|
|
|
|
col = 0;
|
|
|
|
pixbuf += row_inc;
|
2008-01-28 22:00:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-12 23:00:00 +02:00
|
|
|
|
|
|
|
VectorDivide( reflectivity, ( image.width * image.height ), image.fogParams );
|
2016-09-20 23:00:00 +02:00
|
|
|
image.depth = 1;
|
2016-08-12 23:00:00 +02:00
|
|
|
|
2008-01-28 22:00:00 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=============
|
2008-08-06 22:00:00 +02:00
|
|
|
Image_SaveTGA
|
2008-01-28 22:00:00 +01:00
|
|
|
=============
|
|
|
|
*/
|
2010-10-26 22:00:00 +02:00
|
|
|
qboolean Image_SaveTGA( const char *name, rgbdata_t *pix )
|
2008-01-28 22:00:00 +01:00
|
|
|
{
|
|
|
|
int y, outsize, pixel_size;
|
|
|
|
const byte *bufend, *in;
|
|
|
|
byte *buffer, *out;
|
|
|
|
const char *comment = "Generated by Xash ImageLib\0";
|
|
|
|
|
2011-10-09 22:00:00 +02:00
|
|
|
if( FS_FileExists( name, false ) && !Image_CheckFlag( IL_ALLOW_OVERWRITE ))
|
2008-10-19 22:00:00 +02:00
|
|
|
return false; // already existed
|
|
|
|
|
2008-11-03 22:00:00 +01:00
|
|
|
if( pix->flags & IMAGE_HAS_ALPHA )
|
2011-03-09 22:00:00 +01:00
|
|
|
outsize = pix->width * pix->height * 4 + 18 + Q_strlen( comment );
|
|
|
|
else outsize = pix->width * pix->height * 3 + 18 + Q_strlen( comment );
|
2008-01-28 22:00:00 +01:00
|
|
|
|
2018-05-26 23:00:00 +02:00
|
|
|
buffer = (byte *)Mem_Calloc( host.imagepool, outsize );
|
2008-01-28 22:00:00 +01:00
|
|
|
|
|
|
|
// prepare header
|
2011-03-09 22:00:00 +01:00
|
|
|
buffer[0] = Q_strlen( comment ); // tga comment length
|
2008-01-28 22:00:00 +01:00
|
|
|
buffer[2] = 2; // uncompressed type
|
|
|
|
buffer[12] = (pix->width >> 0) & 0xFF;
|
|
|
|
buffer[13] = (pix->width >> 8) & 0xFF;
|
|
|
|
buffer[14] = (pix->height >> 0) & 0xFF;
|
|
|
|
buffer[15] = (pix->height >> 8) & 0xFF;
|
2008-11-03 22:00:00 +01:00
|
|
|
buffer[16] = ( pix->flags & IMAGE_HAS_ALPHA ) ? 32 : 24;
|
|
|
|
buffer[17] = ( pix->flags & IMAGE_HAS_ALPHA ) ? 8 : 0; // 8 bits of alpha
|
2011-03-09 22:00:00 +01:00
|
|
|
Q_strncpy( buffer + 18, comment, Q_strlen( comment ));
|
|
|
|
out = buffer + 18 + Q_strlen( comment );
|
2008-01-28 22:00:00 +01:00
|
|
|
|
|
|
|
// get image description
|
|
|
|
switch( pix->type )
|
|
|
|
{
|
2008-10-25 22:00:00 +02:00
|
|
|
case PF_RGB_24:
|
|
|
|
case PF_BGR_24: pixel_size = 3; break;
|
|
|
|
case PF_RGBA_32:
|
|
|
|
case PF_BGRA_32: pixel_size = 4; break;
|
2008-01-28 22:00:00 +01:00
|
|
|
default:
|
2008-08-08 22:00:00 +02:00
|
|
|
MsgDev( D_ERROR, "Image_SaveTGA: unsupported image type %s\n", PFDesc[pix->type].name );
|
2009-08-07 22:00:00 +02:00
|
|
|
Mem_Free( buffer );
|
2008-01-28 22:00:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch( pix->type )
|
|
|
|
{
|
|
|
|
case PF_RGB_24:
|
|
|
|
case PF_RGBA_32:
|
|
|
|
// swap rgba to bgra and flip upside down
|
2008-10-25 22:00:00 +02:00
|
|
|
for( y = pix->height - 1; y >= 0; y-- )
|
2008-01-28 22:00:00 +01:00
|
|
|
{
|
|
|
|
in = pix->buffer + y * pix->width * pixel_size;
|
|
|
|
bufend = in + pix->width * pixel_size;
|
2008-10-25 22:00:00 +02:00
|
|
|
for( ; in < bufend; in += pixel_size )
|
2008-01-28 22:00:00 +01:00
|
|
|
{
|
|
|
|
*out++ = in[2];
|
|
|
|
*out++ = in[1];
|
|
|
|
*out++ = in[0];
|
2008-11-13 22:00:00 +01:00
|
|
|
if( pix->flags & IMAGE_HAS_ALPHA )
|
2008-01-28 22:00:00 +01:00
|
|
|
*out++ = in[3];
|
|
|
|
}
|
|
|
|
}
|
2008-10-25 22:00:00 +02:00
|
|
|
break;
|
|
|
|
case PF_BGR_24:
|
|
|
|
case PF_BGRA_32:
|
|
|
|
// flip upside down
|
|
|
|
for( y = pix->height - 1; y >= 0; y-- )
|
|
|
|
{
|
|
|
|
in = pix->buffer + y * pix->width * pixel_size;
|
|
|
|
bufend = in + pix->width * pixel_size;
|
|
|
|
for( ; in < bufend; in += pixel_size )
|
|
|
|
{
|
|
|
|
*out++ = in[0];
|
|
|
|
*out++ = in[1];
|
|
|
|
*out++ = in[2];
|
2008-11-03 22:00:00 +01:00
|
|
|
if( pix->flags & IMAGE_HAS_ALPHA )
|
2008-10-25 22:00:00 +02:00
|
|
|
*out++ = in[3];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2008-01-28 22:00:00 +01:00
|
|
|
}
|
|
|
|
FS_WriteFile( name, buffer, outsize );
|
|
|
|
|
|
|
|
Mem_Free( buffer );
|
|
|
|
return true;
|
|
|
|
}
|