2001-05-11 01:01:27 +00:00
|
|
|
/*
|
|
|
|
tga.c
|
|
|
|
|
|
|
|
targa image handling
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
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 2
|
|
|
|
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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
|
|
|
static __attribute__ ((unused)) const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
2001-05-11 01:01:27 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "QF/qendian.h"
|
2002-08-27 07:16:28 +00:00
|
|
|
#include "QF/quakefs.h"
|
2001-05-11 01:01:27 +00:00
|
|
|
#include "QF/sys.h"
|
2001-12-21 01:41:19 +00:00
|
|
|
#include "QF/texture.h"
|
2001-05-11 01:01:27 +00:00
|
|
|
#include "QF/tga.h"
|
2001-12-21 06:07:30 +00:00
|
|
|
#include "QF/zone.h"
|
2001-05-11 01:01:27 +00:00
|
|
|
|
2001-12-21 01:41:19 +00:00
|
|
|
#include "compat.h"
|
|
|
|
|
2001-05-11 01:01:27 +00:00
|
|
|
|
2001-12-21 05:29:33 +00:00
|
|
|
static inline byte *
|
2003-03-20 21:24:04 +00:00
|
|
|
blit_rgb (byte *buf, int count, byte red, byte green, byte blue)
|
2001-12-21 05:29:33 +00:00
|
|
|
{
|
|
|
|
while (count--) {
|
2003-03-20 21:24:04 +00:00
|
|
|
*buf++ = red;
|
|
|
|
*buf++ = green;
|
|
|
|
*buf++ = blue;
|
|
|
|
*buf++ = 255;
|
2001-12-21 05:29:33 +00:00
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline byte *
|
2003-03-20 21:24:04 +00:00
|
|
|
blit_rgba (byte *buf, int count, byte red, byte green, byte blue, byte alpha)
|
2001-12-21 05:29:33 +00:00
|
|
|
{
|
|
|
|
while (count--) {
|
2003-03-20 21:24:04 +00:00
|
|
|
*buf++ = red;
|
|
|
|
*buf++ = green;
|
|
|
|
*buf++ = blue;
|
|
|
|
*buf++ = alpha;
|
2001-12-21 05:29:33 +00:00
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2003-02-23 17:39:23 +00:00
|
|
|
static inline byte *
|
2003-03-20 21:24:04 +00:00
|
|
|
reverse_blit_rgb (byte *buf, int count, byte red, byte green, byte blue)
|
|
|
|
{
|
|
|
|
while (count--) {
|
|
|
|
*buf-- = 255;
|
|
|
|
*buf-- = blue;
|
|
|
|
*buf-- = green;
|
|
|
|
*buf-- = red;
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline byte *
|
|
|
|
reverse_blit_rgba (byte *buf, int count, byte red, byte green, byte blue,
|
|
|
|
byte alpha)
|
|
|
|
{
|
|
|
|
while (count--) {
|
|
|
|
*buf-- = alpha;
|
|
|
|
*buf-- = blue;
|
|
|
|
*buf-- = green;
|
|
|
|
*buf-- = red;
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline byte *
|
|
|
|
read_bgr (byte *buf, int count, byte **data)
|
|
|
|
{
|
|
|
|
byte blue = *(*data)++;
|
|
|
|
byte green = *(*data)++;
|
|
|
|
byte red = *(*data)++;
|
|
|
|
|
|
|
|
return blit_rgb (buf, count, red, green, blue);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline byte *
|
|
|
|
read_bgra (byte *buf, int count, byte **data)
|
|
|
|
{
|
|
|
|
byte blue = *(*data)++;
|
|
|
|
byte green = *(*data)++;
|
|
|
|
byte red = *(*data)++;
|
|
|
|
byte alpha = *(*data)++;
|
|
|
|
|
|
|
|
return blit_rgba (buf, count, red, green, blue, alpha);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline byte *
|
|
|
|
reverse_read_bgr (byte *buf, int count, byte **data)
|
2001-12-21 05:29:33 +00:00
|
|
|
{
|
2003-02-23 17:39:23 +00:00
|
|
|
byte blue = *(*data)++;
|
2001-12-21 16:08:56 +00:00
|
|
|
byte green = *(*data)++;
|
2003-02-23 17:39:23 +00:00
|
|
|
byte red = *(*data)++;
|
|
|
|
|
2003-03-20 21:24:04 +00:00
|
|
|
return reverse_blit_rgb (buf, count, red, green, blue);
|
2003-02-23 17:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline byte *
|
2003-03-20 21:24:04 +00:00
|
|
|
reverse_read_bgra (byte *buf, int count, byte **data)
|
2003-02-23 17:39:23 +00:00
|
|
|
{
|
2001-12-21 16:08:56 +00:00
|
|
|
byte blue = *(*data)++;
|
2003-02-23 17:39:23 +00:00
|
|
|
byte green = *(*data)++;
|
|
|
|
byte red = *(*data)++;
|
2001-12-21 16:08:56 +00:00
|
|
|
byte alpha = *(*data)++;
|
2001-12-21 05:29:33 +00:00
|
|
|
|
2003-03-20 21:24:04 +00:00
|
|
|
return reverse_blit_rgba (buf, count, red, green, blue, alpha);
|
2001-12-21 05:29:33 +00:00
|
|
|
}
|
|
|
|
|
2003-03-20 22:54:44 +00:00
|
|
|
static inline void
|
|
|
|
setup_pixrow_span (TargaHeader *targa, tex_t *tex, byte **_pixrow, int *_span)
|
|
|
|
{
|
|
|
|
byte *pixrow;
|
|
|
|
int span;
|
|
|
|
|
|
|
|
span = targa->width * 4;
|
|
|
|
pixrow = tex->data;
|
|
|
|
if (targa->attributes & 0x10) {
|
|
|
|
// right to left
|
|
|
|
pixrow += span - 4;
|
|
|
|
}
|
|
|
|
if (!(targa->attributes & 0x20)) {
|
|
|
|
// bottom to top
|
|
|
|
pixrow += (targa->height - 1) * span;
|
|
|
|
span = -span;
|
|
|
|
}
|
|
|
|
|
|
|
|
*_pixrow = pixrow;
|
|
|
|
*_span = span;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
decode_truecolor_24 (TargaHeader *targa, tex_t *tex, byte *dataByte)
|
|
|
|
{
|
|
|
|
byte *pixcol, *pixrow;
|
|
|
|
int column, columns, rows, span;
|
|
|
|
|
|
|
|
columns = targa->width;
|
|
|
|
rows = targa->height;
|
|
|
|
|
|
|
|
setup_pixrow_span (targa, tex, &pixrow, &span);
|
|
|
|
|
|
|
|
if (targa->attributes & 0x10) {
|
|
|
|
while (rows-- > 0) {
|
|
|
|
pixcol = pixrow;
|
|
|
|
for (column = columns; column > 0; column--)
|
|
|
|
pixcol = reverse_read_bgr (pixcol, 1, &dataByte);
|
|
|
|
pixrow += span;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (rows-- > 0) {
|
|
|
|
pixcol = pixrow;
|
|
|
|
for (column = columns; column > 0; column--)
|
|
|
|
pixcol = read_bgr (pixcol, 1, &dataByte);
|
|
|
|
pixrow += span;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
decode_truecolor_32 (TargaHeader *targa, tex_t *tex, byte *dataByte)
|
|
|
|
{
|
|
|
|
byte *pixcol, *pixrow;
|
|
|
|
int column, columns, rows, span;
|
|
|
|
|
|
|
|
columns = targa->width;
|
|
|
|
rows = targa->height;
|
|
|
|
|
|
|
|
setup_pixrow_span (targa, tex, &pixrow, &span);
|
|
|
|
|
|
|
|
if (targa->attributes & 0x10) {
|
|
|
|
while (rows-- > 0) {
|
|
|
|
pixcol = pixrow;
|
|
|
|
for (column = columns; column > 0; column--)
|
|
|
|
pixcol = reverse_read_bgra (pixcol, 1, &dataByte);
|
|
|
|
pixrow += span;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (rows-- > 0) {
|
|
|
|
pixcol = pixrow;
|
|
|
|
for (column = 0; column < columns; column++)
|
|
|
|
pixcol = read_bgra (pixcol, 1, &dataByte);
|
|
|
|
pixrow += span;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define rle_expand(expand) \
|
|
|
|
do { \
|
|
|
|
unsigned char packetHeader, packetSize; \
|
|
|
|
while (rows-- > 0) { \
|
|
|
|
pixcol = pixrow; \
|
|
|
|
pixrow += span; \
|
|
|
|
for (column = columns; column > 0; ) { \
|
|
|
|
packetHeader = *dataByte++; \
|
|
|
|
packetSize = 1 + (packetHeader & 0x7f); \
|
|
|
|
while (packetSize > column) { \
|
|
|
|
int count = column; \
|
|
|
|
\
|
|
|
|
packetSize -= count; \
|
|
|
|
if (packetHeader & 0x80) { /* run-length packet */ \
|
|
|
|
expand (pixcol, count, &dataByte); \
|
|
|
|
} else { /* non run-length packet */ \
|
|
|
|
while (count--) \
|
|
|
|
expand (pixcol, 1, &dataByte); \
|
|
|
|
} \
|
|
|
|
column = columns; \
|
|
|
|
pixcol = pixrow; \
|
|
|
|
pixrow += span; \
|
|
|
|
if (rows-- <= 0) \
|
2003-03-20 23:11:58 +00:00
|
|
|
goto done_##expand; \
|
2003-03-20 22:54:44 +00:00
|
|
|
} \
|
|
|
|
column -= packetSize; \
|
|
|
|
if (packetHeader & 0x80) { /* run-length packet */ \
|
|
|
|
pixcol = expand (pixcol, packetSize, &dataByte); \
|
|
|
|
} else { /* non run-length packet */ \
|
|
|
|
while (packetSize--) \
|
|
|
|
pixcol = expand (pixcol, 1, &dataByte); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
2003-03-20 23:11:58 +00:00
|
|
|
done_##expand:; \
|
2003-03-20 22:54:44 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
static void
|
|
|
|
decode_truecolor_24_rle (TargaHeader *targa, tex_t *tex, byte *dataByte)
|
|
|
|
{
|
|
|
|
byte *pixcol, *pixrow;
|
|
|
|
int column, columns, rows, span;
|
|
|
|
|
|
|
|
columns = targa->width;
|
|
|
|
rows = targa->height;
|
|
|
|
|
|
|
|
setup_pixrow_span (targa, tex, &pixrow, &span);
|
|
|
|
|
|
|
|
if (targa->attributes & 0x10)
|
|
|
|
rle_expand (reverse_read_bgr);
|
|
|
|
else
|
|
|
|
rle_expand (read_bgr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
decode_truecolor_32_rle (TargaHeader *targa, tex_t *tex, byte *dataByte)
|
|
|
|
{
|
|
|
|
byte *pixcol, *pixrow;
|
|
|
|
int column, columns, rows, span;
|
|
|
|
|
|
|
|
columns = targa->width;
|
|
|
|
rows = targa->height;
|
|
|
|
|
|
|
|
setup_pixrow_span (targa, tex, &pixrow, &span);
|
|
|
|
|
|
|
|
if (targa->attributes & 0x10)
|
2003-03-20 23:11:58 +00:00
|
|
|
rle_expand (reverse_read_bgra);
|
2003-03-20 22:54:44 +00:00
|
|
|
else
|
2003-03-20 23:11:58 +00:00
|
|
|
rle_expand (read_bgra);
|
2003-03-20 22:54:44 +00:00
|
|
|
}
|
|
|
|
|
2001-12-21 01:41:19 +00:00
|
|
|
struct tex_s *
|
2002-08-27 07:16:28 +00:00
|
|
|
LoadTGA (QFile *fin)
|
2001-05-11 01:01:27 +00:00
|
|
|
{
|
2003-03-20 23:02:53 +00:00
|
|
|
byte *dataByte;
|
|
|
|
int numPixels, targa_mark;
|
2001-12-21 06:07:30 +00:00
|
|
|
TargaHeader *targa;
|
|
|
|
tex_t *tex;
|
|
|
|
|
|
|
|
targa_mark = Hunk_LowMark ();
|
2003-02-14 19:46:07 +00:00
|
|
|
targa = Hunk_AllocName (qfs_filesize, "TGA");
|
|
|
|
Qread (fin, targa, qfs_filesize);
|
2001-12-21 06:07:30 +00:00
|
|
|
|
|
|
|
targa->colormap_index = LittleShort (targa->colormap_index);
|
|
|
|
targa->colormap_length = LittleShort (targa->colormap_length);
|
|
|
|
targa->x_origin = LittleShort (targa->x_origin);
|
|
|
|
targa->y_origin = LittleShort (targa->y_origin);
|
|
|
|
targa->width = LittleShort (targa->width);
|
|
|
|
targa->height = LittleShort (targa->height);
|
|
|
|
|
|
|
|
if (targa->image_type != 2 && targa->image_type != 10)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("LoadTGA: Only type 2 and 10 targa RGB images supported");
|
2001-05-11 01:01:27 +00:00
|
|
|
|
2001-12-21 06:07:30 +00:00
|
|
|
if (targa->colormap_type != 0
|
|
|
|
|| (targa->pixel_size != 32 && targa->pixel_size != 24))
|
2001-08-22 22:03:16 +00:00
|
|
|
Sys_Error ("Texture_LoadTGA: Only 32 or 24 bit images supported "
|
2002-05-14 06:12:29 +00:00
|
|
|
"(no colormaps)");
|
2001-05-11 01:01:27 +00:00
|
|
|
|
2003-03-20 23:02:53 +00:00
|
|
|
numPixels = targa->width * targa->height;
|
|
|
|
|
|
|
|
tex = Hunk_TempAlloc (field_offset (tex_t, data[numPixels * 4]));
|
|
|
|
tex->width = targa->width;
|
|
|
|
tex->height = targa->height;
|
|
|
|
tex->palette = 0;
|
2001-05-11 01:01:27 +00:00
|
|
|
|
2001-12-21 06:07:30 +00:00
|
|
|
switch (targa->pixel_size) {
|
2001-12-21 01:41:19 +00:00
|
|
|
case 24:
|
2001-12-21 06:07:30 +00:00
|
|
|
tex->format = tex_rgb;
|
2001-12-21 01:41:19 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case 32:
|
2001-12-21 06:07:30 +00:00
|
|
|
tex->format = tex_rgba;
|
2001-12-21 01:41:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-12-21 06:07:30 +00:00
|
|
|
// skip TARGA image comment
|
2001-12-21 16:08:56 +00:00
|
|
|
dataByte = (byte *) (targa + 1);
|
2001-12-21 06:07:30 +00:00
|
|
|
dataByte += targa->id_length;
|
2001-05-11 01:01:27 +00:00
|
|
|
|
2001-12-21 06:07:30 +00:00
|
|
|
if (targa->image_type == 2) { // Uncompressed image
|
2003-03-20 22:54:44 +00:00
|
|
|
switch (targa->pixel_size) {
|
|
|
|
case 24:
|
|
|
|
decode_truecolor_24 (targa, tex, dataByte);
|
2003-02-23 17:39:23 +00:00
|
|
|
break;
|
2003-03-20 22:54:44 +00:00
|
|
|
case 32:
|
|
|
|
decode_truecolor_32 (targa, tex, dataByte);
|
2003-02-23 17:39:23 +00:00
|
|
|
break;
|
2001-05-11 01:01:27 +00:00
|
|
|
}
|
2001-12-21 06:07:30 +00:00
|
|
|
} else if (targa->image_type == 10) { // RLE compressed image
|
2003-03-20 22:54:44 +00:00
|
|
|
switch (targa->pixel_size) {
|
|
|
|
case 24:
|
|
|
|
decode_truecolor_24_rle (targa, tex, dataByte);
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
decode_truecolor_32_rle (targa, tex, dataByte);
|
|
|
|
break;
|
2001-05-11 01:01:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-21 06:07:30 +00:00
|
|
|
Hunk_FreeToLowMark (targa_mark);
|
|
|
|
return tex;
|
2001-05-11 01:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WriteTGAfile (const char *tganame, byte *data, int width, int height)
|
|
|
|
{
|
|
|
|
TargaHeader header;
|
|
|
|
|
|
|
|
memset (&header, 0, sizeof (header));
|
|
|
|
header.image_type = 2; // uncompressed type
|
|
|
|
header.width = LittleShort (width);
|
|
|
|
header.height = LittleShort (height);
|
|
|
|
header.pixel_size = 24;
|
|
|
|
|
2003-02-14 19:46:07 +00:00
|
|
|
QFS_WriteBuffers (tganame, 2, &header, sizeof (header), data,
|
2001-05-11 01:01:27 +00:00
|
|
|
width * height * 3);
|
|
|
|
}
|