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
|
|
|
|
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>
|
|
|
|
|
2003-09-04 18:59:39 +00:00
|
|
|
#include "QF/image.h"
|
2001-05-11 01:01:27 +00:00
|
|
|
#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"
|
|
|
|
#include "QF/tga.h"
|
2001-12-21 06:07:30 +00:00
|
|
|
#include "QF/zone.h"
|
2003-03-21 22:08:46 +00:00
|
|
|
#include "QF/GL/defines.h"
|
2001-05-11 01:01:27 +00:00
|
|
|
|
2001-12-21 01:41:19 +00:00
|
|
|
#include "compat.h"
|
|
|
|
|
2003-03-21 04:23:16 +00:00
|
|
|
typedef struct {
|
|
|
|
byte red;
|
|
|
|
byte green;
|
|
|
|
byte blue;
|
|
|
|
byte alpha;
|
|
|
|
} cmap_t;
|
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;
|
|
|
|
}
|
|
|
|
|
2003-03-21 05:03:32 +00:00
|
|
|
static inline byte *
|
2003-03-21 22:08:46 +00:00
|
|
|
blit_l (byte *buf, int count, byte lum)
|
2003-03-21 05:03:32 +00:00
|
|
|
{
|
|
|
|
while (count--) {
|
|
|
|
*buf++ = lum;
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline byte *
|
2003-03-21 22:08:46 +00:00
|
|
|
reverse_blit_l (byte *buf, int count, byte lum)
|
2003-03-21 05:03:32 +00:00
|
|
|
{
|
|
|
|
while (count--) {
|
|
|
|
*buf-- = lum;
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2003-03-20 21:24:04 +00:00
|
|
|
static inline byte *
|
2003-03-21 04:23:16 +00:00
|
|
|
read_bgr (byte *buf, int count, byte **data, cmap_t *cmap)
|
2003-03-20 21:24:04 +00:00
|
|
|
{
|
|
|
|
byte blue = *(*data)++;
|
|
|
|
byte green = *(*data)++;
|
|
|
|
byte red = *(*data)++;
|
|
|
|
|
|
|
|
return blit_rgb (buf, count, red, green, blue);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline byte *
|
2003-03-21 04:23:16 +00:00
|
|
|
read_bgra (byte *buf, int count, byte **data, cmap_t *cmap)
|
2003-03-20 21:24:04 +00:00
|
|
|
{
|
|
|
|
byte blue = *(*data)++;
|
|
|
|
byte green = *(*data)++;
|
|
|
|
byte red = *(*data)++;
|
|
|
|
byte alpha = *(*data)++;
|
|
|
|
|
|
|
|
return blit_rgba (buf, count, red, green, blue, alpha);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline byte *
|
2003-03-21 04:23:16 +00:00
|
|
|
reverse_read_bgr (byte *buf, int count, byte **data, cmap_t *cmap)
|
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-21 04:23:16 +00:00
|
|
|
reverse_read_bgra (byte *buf, int count, byte **data, cmap_t *cmap)
|
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-21 04:23:16 +00:00
|
|
|
static inline byte *
|
|
|
|
read_cmap (byte *buf, int count, byte **data, cmap_t *cmap)
|
|
|
|
{
|
|
|
|
byte ind = *(*data)++;
|
|
|
|
byte red = cmap[ind].red;
|
|
|
|
byte green = cmap[ind].green;
|
|
|
|
byte blue = cmap[ind].blue;
|
|
|
|
byte alpha = cmap[ind].alpha;
|
|
|
|
|
|
|
|
return blit_rgba (buf, count, red, green, blue, alpha);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline byte *
|
|
|
|
reverse_read_cmap (byte *buf, int count, byte **data, cmap_t *cmap)
|
|
|
|
{
|
|
|
|
byte ind = *(*data)++;
|
|
|
|
byte red = cmap[ind].red;
|
|
|
|
byte green = cmap[ind].green;
|
|
|
|
byte blue = cmap[ind].blue;
|
|
|
|
byte alpha = cmap[ind].alpha;
|
|
|
|
|
|
|
|
return reverse_blit_rgba (buf, count, red, green, blue, alpha);
|
|
|
|
}
|
|
|
|
|
2003-03-21 05:03:32 +00:00
|
|
|
static inline byte *
|
|
|
|
read_l (byte *buf, int count, byte **data, cmap_t *cmap)
|
|
|
|
{
|
|
|
|
byte lum = *(*data)++;
|
|
|
|
|
2003-03-21 22:08:46 +00:00
|
|
|
return blit_l (buf, count, lum);
|
2003-03-21 05:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline byte *
|
|
|
|
reverse_read_l (byte *buf, int count, byte **data, cmap_t *cmap)
|
|
|
|
{
|
|
|
|
byte lum = *(*data)++;
|
|
|
|
|
2003-03-21 22:08:46 +00:00
|
|
|
return reverse_blit_l (buf, count, lum);
|
2003-03-21 05:03:32 +00:00
|
|
|
}
|
|
|
|
|
2003-03-20 22:54:44 +00:00
|
|
|
static inline void
|
2003-03-21 22:18:20 +00:00
|
|
|
setup_pixrow_span (TargaHeader *targa, tex_t *tex, byte **_pixrow, int *_span,
|
|
|
|
int pixbytes)
|
2003-03-20 22:54:44 +00:00
|
|
|
{
|
|
|
|
byte *pixrow;
|
|
|
|
int span;
|
|
|
|
|
2003-03-21 22:18:20 +00:00
|
|
|
span = targa->width * pixbytes;
|
2003-03-20 22:54:44 +00:00
|
|
|
pixrow = tex->data;
|
|
|
|
if (targa->attributes & 0x10) {
|
|
|
|
// right to left
|
2003-03-21 22:18:20 +00:00
|
|
|
pixrow += span - pixbytes;
|
2003-03-20 22:54:44 +00:00
|
|
|
}
|
|
|
|
if (!(targa->attributes & 0x20)) {
|
|
|
|
// bottom to top
|
|
|
|
pixrow += (targa->height - 1) * span;
|
|
|
|
span = -span;
|
|
|
|
}
|
|
|
|
|
|
|
|
*_pixrow = pixrow;
|
|
|
|
*_span = span;
|
|
|
|
}
|
|
|
|
|
2003-03-21 06:08:17 +00:00
|
|
|
static byte *
|
|
|
|
skip_colormap (TargaHeader *targa, byte *data)
|
|
|
|
{
|
|
|
|
int bpe;
|
|
|
|
if (!targa->colormap_type)
|
|
|
|
return data;
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_dev, "LoadTGA: skipping colormap\n");
|
2003-03-21 06:08:17 +00:00
|
|
|
bpe = (targa->pixel_size +7) / 8;
|
|
|
|
return data + bpe * targa->colormap_length;
|
|
|
|
}
|
|
|
|
|
2003-03-21 04:23:16 +00:00
|
|
|
static cmap_t *
|
|
|
|
parse_colormap (TargaHeader *targa, byte **dataByte)
|
|
|
|
{
|
|
|
|
byte *data;
|
|
|
|
cmap_t *cm, *cmap;
|
|
|
|
int i;
|
|
|
|
unsigned short word;
|
|
|
|
|
|
|
|
if (!targa->colormap_type)
|
|
|
|
Sys_Error ("LoadTGA: colormap missing");
|
|
|
|
if (targa->colormap_type != 1)
|
|
|
|
Sys_Error ("LoadTGA: unkown colormap type");
|
|
|
|
if (targa->colormap_index + targa->colormap_length > 256)
|
|
|
|
Sys_Error ("LoadTGA: unsupported color map size");
|
|
|
|
if (targa->pixel_size != 8)
|
|
|
|
Sys_Error ("LoadTGA: unsupported color map index bits");
|
|
|
|
|
|
|
|
switch (targa->colormap_size) {
|
|
|
|
case 32:
|
|
|
|
if (!targa->colormap_index && targa->colormap_length == 256) {
|
|
|
|
cmap = (cmap_t *) *dataByte;
|
|
|
|
*dataByte += 256 * sizeof (cmap_t);
|
|
|
|
return cmap;
|
|
|
|
}
|
|
|
|
case 24:
|
|
|
|
case 16:
|
|
|
|
case 15:
|
2021-07-28 06:01:45 +00:00
|
|
|
cmap = Hunk_AllocName (0, 256 * sizeof (cmap_t), "TGA cmap");
|
2003-03-21 04:23:16 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Sys_Error ("LoadTGA: unsupported color map size");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = *dataByte;
|
|
|
|
cm = cmap + targa->colormap_index;
|
|
|
|
|
|
|
|
switch (targa->colormap_size) {
|
|
|
|
case 15:
|
|
|
|
for (i = 0; i < targa->colormap_length; i++, cm++) {
|
|
|
|
word = *data++;
|
|
|
|
word |= (*data++) << 8;
|
2003-03-21 16:18:51 +00:00
|
|
|
cm->red = (word << 3) & 0xf8;
|
|
|
|
cm->green = (word >> 2) & 0xf8;
|
|
|
|
cm->blue = (word >> 7) & 0xf8;
|
2003-03-21 04:23:16 +00:00
|
|
|
cm->alpha = 255;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
for (i = 0; i < targa->colormap_length; i++, cm++) {
|
|
|
|
word = *data++;
|
|
|
|
word |= (*data++) << 8;
|
2003-03-21 16:18:51 +00:00
|
|
|
cm->red = (word << 3) & 0xf8;
|
|
|
|
cm->green = (word >> 2) & 0xf8;
|
|
|
|
cm->blue = (word >> 7) & 0xf8;
|
2003-03-21 04:23:16 +00:00
|
|
|
cm->alpha = (word & 0x8000) ? 255 : 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 24:
|
|
|
|
for (i = 0; i < targa->colormap_length; i++, cm++) {
|
2003-03-21 16:18:51 +00:00
|
|
|
cm->red = *data++;
|
2003-03-21 04:23:16 +00:00
|
|
|
cm->green = *data++;
|
2003-03-21 16:18:51 +00:00
|
|
|
cm->blue = *data++;
|
2003-03-21 04:23:16 +00:00
|
|
|
cm->alpha = 255;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
for (i = 0; i < targa->colormap_length; i++, cm++) {
|
2003-03-21 16:18:51 +00:00
|
|
|
cm->red = *data++;
|
2003-03-21 04:23:16 +00:00
|
|
|
cm->green = *data++;
|
2003-03-21 16:18:51 +00:00
|
|
|
cm->blue = *data++;
|
2003-03-21 04:23:16 +00:00
|
|
|
cm->alpha = *data++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return cmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
decode_colormap (TargaHeader *targa, tex_t *tex, byte *dataByte)
|
|
|
|
{
|
|
|
|
byte *pixcol, *pixrow;
|
|
|
|
cmap_t *cmap;
|
|
|
|
int column, columns, rows, span;
|
|
|
|
|
|
|
|
cmap = parse_colormap (targa, &dataByte);
|
|
|
|
|
|
|
|
tex->format = tex_rgba;
|
|
|
|
|
|
|
|
columns = targa->width;
|
|
|
|
rows = targa->height;
|
|
|
|
|
2003-03-21 22:18:20 +00:00
|
|
|
setup_pixrow_span (targa, tex, &pixrow, &span, 4);
|
2003-03-21 04:23:16 +00:00
|
|
|
|
|
|
|
if (targa->attributes & 0x10) {
|
|
|
|
// right to left
|
|
|
|
while (rows-- > 0) {
|
|
|
|
pixcol = pixrow;
|
|
|
|
for (column = columns; column > 0; column--)
|
|
|
|
pixcol = reverse_read_cmap (pixcol, 1, &dataByte, cmap);
|
|
|
|
pixrow += span;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// left to right
|
|
|
|
while (rows-- > 0) {
|
|
|
|
pixcol = pixrow;
|
|
|
|
for (column = columns; column > 0; column--)
|
|
|
|
pixcol = read_cmap (pixcol, 1, &dataByte, cmap);
|
|
|
|
pixrow += span;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-20 22:54:44 +00:00
|
|
|
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;
|
|
|
|
|
2003-03-21 22:18:20 +00:00
|
|
|
setup_pixrow_span (targa, tex, &pixrow, &span, 4);
|
2003-03-20 22:54:44 +00:00
|
|
|
|
|
|
|
if (targa->attributes & 0x10) {
|
2003-03-21 04:23:16 +00:00
|
|
|
// right to left
|
2003-03-20 22:54:44 +00:00
|
|
|
while (rows-- > 0) {
|
|
|
|
pixcol = pixrow;
|
|
|
|
for (column = columns; column > 0; column--)
|
2003-03-21 04:23:16 +00:00
|
|
|
pixcol = reverse_read_bgr (pixcol, 1, &dataByte, 0);
|
2003-03-20 22:54:44 +00:00
|
|
|
pixrow += span;
|
|
|
|
}
|
|
|
|
} else {
|
2003-03-21 04:23:16 +00:00
|
|
|
// left to right
|
2003-03-20 22:54:44 +00:00
|
|
|
while (rows-- > 0) {
|
|
|
|
pixcol = pixrow;
|
|
|
|
for (column = columns; column > 0; column--)
|
2003-03-21 04:23:16 +00:00
|
|
|
pixcol = read_bgr (pixcol, 1, &dataByte, 0);
|
2003-03-20 22:54:44 +00:00
|
|
|
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;
|
|
|
|
|
2003-03-21 22:18:20 +00:00
|
|
|
setup_pixrow_span (targa, tex, &pixrow, &span, 4);
|
2003-03-20 22:54:44 +00:00
|
|
|
|
|
|
|
if (targa->attributes & 0x10) {
|
2003-03-21 04:23:16 +00:00
|
|
|
// right to left
|
2003-03-20 22:54:44 +00:00
|
|
|
while (rows-- > 0) {
|
|
|
|
pixcol = pixrow;
|
|
|
|
for (column = columns; column > 0; column--)
|
2003-03-21 04:23:16 +00:00
|
|
|
pixcol = reverse_read_bgra (pixcol, 1, &dataByte, 0);
|
2003-03-20 22:54:44 +00:00
|
|
|
pixrow += span;
|
|
|
|
}
|
|
|
|
} else {
|
2003-03-21 04:23:16 +00:00
|
|
|
// left to right
|
2003-03-20 22:54:44 +00:00
|
|
|
while (rows-- > 0) {
|
|
|
|
pixcol = pixrow;
|
2003-03-21 04:23:16 +00:00
|
|
|
for (column = columns; column > 0; column--)
|
|
|
|
pixcol = read_bgra (pixcol, 1, &dataByte, 0);
|
2003-03-20 22:54:44 +00:00
|
|
|
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 */ \
|
2003-03-21 04:23:16 +00:00
|
|
|
expand (pixcol, count, &dataByte, cmap); \
|
2003-03-20 22:54:44 +00:00
|
|
|
} else { /* non run-length packet */ \
|
|
|
|
while (count--) \
|
2003-03-21 04:23:16 +00:00
|
|
|
expand (pixcol, 1, &dataByte, cmap); \
|
2003-03-20 22:54:44 +00:00
|
|
|
} \
|
|
|
|
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 */ \
|
2003-03-21 04:23:16 +00:00
|
|
|
pixcol = expand (pixcol, packetSize, &dataByte, cmap); \
|
2003-03-20 22:54:44 +00:00
|
|
|
} else { /* non run-length packet */ \
|
|
|
|
while (packetSize--) \
|
2003-03-21 04:23:16 +00:00
|
|
|
pixcol = expand (pixcol, 1, &dataByte, cmap); \
|
2003-03-20 22:54:44 +00:00
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
2003-03-20 23:11:58 +00:00
|
|
|
done_##expand:; \
|
2003-03-20 22:54:44 +00:00
|
|
|
} while (0)
|
|
|
|
|
2003-03-21 04:23:16 +00:00
|
|
|
static void
|
|
|
|
decode_colormap_rle (TargaHeader *targa, tex_t *tex, byte *dataByte)
|
|
|
|
{
|
|
|
|
byte *pixcol, *pixrow;
|
|
|
|
cmap_t *cmap;
|
|
|
|
int column, columns, rows, span;
|
|
|
|
|
|
|
|
cmap = parse_colormap (targa, &dataByte);
|
|
|
|
|
|
|
|
tex->format = tex_rgba;
|
|
|
|
|
|
|
|
columns = targa->width;
|
|
|
|
rows = targa->height;
|
|
|
|
|
2003-03-21 22:18:20 +00:00
|
|
|
setup_pixrow_span (targa, tex, &pixrow, &span, 4);
|
2003-03-21 04:23:16 +00:00
|
|
|
|
|
|
|
if (targa->attributes & 0x10) // right to left
|
|
|
|
rle_expand (reverse_read_cmap);
|
|
|
|
else // left to right
|
|
|
|
rle_expand (read_cmap);
|
|
|
|
}
|
|
|
|
|
2003-03-20 22:54:44 +00:00
|
|
|
static void
|
|
|
|
decode_truecolor_24_rle (TargaHeader *targa, tex_t *tex, byte *dataByte)
|
|
|
|
{
|
|
|
|
byte *pixcol, *pixrow;
|
|
|
|
int column, columns, rows, span;
|
2003-03-21 04:23:16 +00:00
|
|
|
cmap_t *cmap = 0; // not really used but needed for rle_expand
|
2003-03-20 22:54:44 +00:00
|
|
|
|
|
|
|
columns = targa->width;
|
|
|
|
rows = targa->height;
|
|
|
|
|
2003-03-21 22:18:20 +00:00
|
|
|
setup_pixrow_span (targa, tex, &pixrow, &span, 4);
|
2003-03-20 22:54:44 +00:00
|
|
|
|
2003-03-21 04:23:16 +00:00
|
|
|
if (targa->attributes & 0x10) // right to left
|
2003-03-20 22:54:44 +00:00
|
|
|
rle_expand (reverse_read_bgr);
|
2003-03-21 04:23:16 +00:00
|
|
|
else // left to right
|
2003-03-20 22:54:44 +00:00
|
|
|
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;
|
2003-03-21 04:23:16 +00:00
|
|
|
cmap_t *cmap = 0; // not really used but needed for rle_expand
|
2003-03-20 22:54:44 +00:00
|
|
|
|
|
|
|
columns = targa->width;
|
|
|
|
rows = targa->height;
|
|
|
|
|
2003-03-21 22:18:20 +00:00
|
|
|
setup_pixrow_span (targa, tex, &pixrow, &span, 4);
|
2003-03-20 22:54:44 +00:00
|
|
|
|
2003-03-21 04:23:16 +00:00
|
|
|
if (targa->attributes & 0x10) // right to left
|
2003-03-20 23:11:58 +00:00
|
|
|
rle_expand (reverse_read_bgra);
|
2003-03-21 04:23:16 +00:00
|
|
|
else // left to right
|
2003-03-20 23:11:58 +00:00
|
|
|
rle_expand (read_bgra);
|
2003-03-20 22:54:44 +00:00
|
|
|
}
|
|
|
|
|
2003-03-21 04:23:16 +00:00
|
|
|
static void
|
|
|
|
decode_truecolor (TargaHeader *targa, tex_t *tex, byte *dataByte)
|
|
|
|
{
|
2003-03-21 06:08:17 +00:00
|
|
|
dataByte = skip_colormap (targa, dataByte);
|
|
|
|
|
2003-03-21 04:23:16 +00:00
|
|
|
switch (targa->pixel_size) {
|
|
|
|
case 24:
|
2003-09-04 15:14:09 +00:00
|
|
|
tex->format = tex_rgba; // image gets expadned to rgba
|
2003-03-21 04:23:16 +00:00
|
|
|
decode_truecolor_24 (targa, tex, dataByte);
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
tex->format = tex_rgba;
|
|
|
|
decode_truecolor_32 (targa, tex, dataByte);
|
|
|
|
break;
|
2003-03-21 05:03:32 +00:00
|
|
|
default:
|
|
|
|
Sys_Error ("LoadTGA: unsupported pixel size");
|
2003-03-21 04:23:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
decode_truecolor_rle (TargaHeader *targa, tex_t *tex, byte *dataByte)
|
|
|
|
{
|
2003-03-21 06:08:17 +00:00
|
|
|
dataByte = skip_colormap (targa, dataByte);
|
|
|
|
|
2003-03-21 04:23:16 +00:00
|
|
|
switch (targa->pixel_size) {
|
|
|
|
case 24:
|
2003-09-04 15:14:09 +00:00
|
|
|
tex->format = tex_rgba; // image gets expadned to rgba
|
2003-03-21 04:23:16 +00:00
|
|
|
decode_truecolor_24_rle (targa, tex, dataByte);
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
tex->format = tex_rgba;
|
|
|
|
decode_truecolor_32_rle (targa, tex, dataByte);
|
|
|
|
break;
|
2003-03-21 05:03:32 +00:00
|
|
|
default:
|
|
|
|
Sys_Error ("LoadTGA: unsupported truecolor pixel size");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
decode_greyscale (TargaHeader *targa, tex_t *tex, byte *dataByte)
|
|
|
|
{
|
|
|
|
byte *pixcol, *pixrow;
|
|
|
|
int column, columns, rows, span;
|
|
|
|
|
2003-03-21 06:08:17 +00:00
|
|
|
dataByte = skip_colormap (targa, dataByte);
|
|
|
|
|
2003-03-21 05:03:32 +00:00
|
|
|
if (targa->pixel_size != 8)
|
|
|
|
Sys_Error ("LoadTGA: unsupported truecolor pixel size");
|
2003-03-21 22:08:46 +00:00
|
|
|
tex->format = tex_l;
|
2003-03-21 05:03:32 +00:00
|
|
|
|
|
|
|
columns = targa->width;
|
|
|
|
rows = targa->height;
|
|
|
|
|
2003-03-21 22:18:20 +00:00
|
|
|
setup_pixrow_span (targa, tex, &pixrow, &span, 1);
|
2003-03-21 05:03:32 +00:00
|
|
|
|
|
|
|
if (targa->attributes & 0x10) {
|
|
|
|
// right to left
|
|
|
|
while (rows-- > 0) {
|
|
|
|
pixcol = pixrow;
|
|
|
|
for (column = columns; column > 0; column--)
|
|
|
|
pixcol = reverse_read_l (pixcol, 1, &dataByte, 0);
|
|
|
|
pixrow += span;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// left to right
|
|
|
|
while (rows-- > 0) {
|
|
|
|
pixcol = pixrow;
|
|
|
|
for (column = columns; column > 0; column--)
|
|
|
|
pixcol = read_l (pixcol, 1, &dataByte, 0);
|
|
|
|
pixrow += span;
|
|
|
|
}
|
2003-03-21 04:23:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-21 05:03:32 +00:00
|
|
|
static void
|
|
|
|
decode_greyscale_rle (TargaHeader *targa, tex_t *tex, byte *dataByte)
|
|
|
|
{
|
|
|
|
byte *pixcol, *pixrow;
|
|
|
|
int column, columns, rows, span;
|
|
|
|
cmap_t *cmap = 0; // not really used but needed for rle_expand
|
|
|
|
|
2003-03-21 06:08:57 +00:00
|
|
|
dataByte = skip_colormap (targa, dataByte);
|
|
|
|
|
2003-03-21 05:03:32 +00:00
|
|
|
if (targa->pixel_size != 8)
|
|
|
|
Sys_Error ("LoadTGA: unsupported truecolor pixel size");
|
2003-03-21 22:08:46 +00:00
|
|
|
tex->format = tex_l;
|
2003-03-21 05:03:32 +00:00
|
|
|
|
|
|
|
columns = targa->width;
|
|
|
|
rows = targa->height;
|
|
|
|
|
2003-03-21 22:18:20 +00:00
|
|
|
setup_pixrow_span (targa, tex, &pixrow, &span, 1);
|
2003-03-21 05:03:32 +00:00
|
|
|
|
|
|
|
if (targa->attributes & 0x10) // right to left
|
|
|
|
rle_expand (reverse_read_l);
|
|
|
|
else // left to right
|
|
|
|
rle_expand (read_l);
|
|
|
|
}
|
|
|
|
|
2003-03-21 04:23:16 +00:00
|
|
|
typedef void (*decoder_t) (TargaHeader *, tex_t *, byte *);
|
|
|
|
static decoder_t decoder_functions[] = {
|
2022-05-04 04:50:55 +00:00
|
|
|
[targa_colormap] = decode_colormap,
|
|
|
|
[targa_truecolor] = decode_truecolor,
|
|
|
|
[targa_greyscale] = decode_greyscale,
|
|
|
|
[targa_colormap_rle] = decode_colormap_rle,
|
|
|
|
[targa_truecolor_rle] = decode_truecolor_rle,
|
|
|
|
[targa_greyscale_rle] = decode_greyscale_rle,
|
|
|
|
[targa_max_image_type] = 0
|
2003-03-21 04:23:16 +00:00
|
|
|
};
|
|
|
|
#define NUM_DECODERS (sizeof (decoder_functions) \
|
|
|
|
/ sizeof (decoder_functions[0]))
|
2022-05-04 04:50:55 +00:00
|
|
|
static QFFormat targa_formats[] = {
|
|
|
|
[targa_colormap] = tex_palette,
|
|
|
|
[targa_truecolor] = tex_rgba,
|
|
|
|
[targa_greyscale] = tex_l,
|
|
|
|
[targa_colormap_rle] = tex_palette,
|
|
|
|
[targa_truecolor_rle] = tex_rgba,
|
|
|
|
[targa_greyscale_rle] = tex_l,
|
|
|
|
[targa_max_image_type] = 0
|
|
|
|
};
|
2003-03-21 04:23:16 +00:00
|
|
|
|
2001-12-21 01:41:19 +00:00
|
|
|
struct tex_s *
|
2021-01-19 01:15:57 +00:00
|
|
|
LoadTGA (QFile *fin, int load)
|
2001-05-11 01:01:27 +00:00
|
|
|
{
|
2003-03-20 23:02:53 +00:00
|
|
|
byte *dataByte;
|
2003-03-21 04:23:16 +00:00
|
|
|
decoder_t decode;
|
2021-01-19 01:15:57 +00:00
|
|
|
int fsize = sizeof (TargaHeader);
|
2021-07-29 02:12:37 +00:00
|
|
|
int numPixels;
|
|
|
|
size_t targa_mark;
|
2001-12-21 06:07:30 +00:00
|
|
|
TargaHeader *targa;
|
|
|
|
tex_t *tex;
|
|
|
|
|
2021-01-19 01:15:57 +00:00
|
|
|
if (load) {
|
|
|
|
fsize = Qfilesize (fin);
|
|
|
|
}
|
2021-07-28 06:01:45 +00:00
|
|
|
targa_mark = Hunk_LowMark (0);
|
|
|
|
targa = Hunk_AllocName (0, fsize, "TGA");
|
2011-01-09 05:55:40 +00:00
|
|
|
Qread (fin, targa, fsize);
|
2012-05-21 23:23:22 +00:00
|
|
|
|
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);
|
|
|
|
|
2003-03-21 04:23:16 +00:00
|
|
|
if (targa->image_type >= NUM_DECODERS
|
2021-01-19 01:15:57 +00:00
|
|
|
|| !(decode = decoder_functions[targa->image_type])) {
|
|
|
|
Sys_Printf ("LoadTGA: Unsupported targa type");
|
2021-07-28 06:01:45 +00:00
|
|
|
Hunk_FreeToLowMark (0, targa_mark);
|
2021-01-19 01:15:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2001-05-11 01:01:27 +00:00
|
|
|
|
2021-01-19 01:15:57 +00:00
|
|
|
if (load) {
|
|
|
|
numPixels = targa->width * targa->height;
|
|
|
|
} else {
|
|
|
|
numPixels = 0;
|
|
|
|
}
|
2021-07-28 06:01:45 +00:00
|
|
|
tex = Hunk_TempAlloc (0, sizeof (tex_t) + numPixels * 4);
|
2021-01-24 15:54:41 +00:00
|
|
|
tex->data = (byte *) (tex + 1);
|
2003-03-20 23:02:53 +00:00
|
|
|
tex->width = targa->width;
|
|
|
|
tex->height = targa->height;
|
|
|
|
tex->palette = 0;
|
2021-01-19 01:15:57 +00:00
|
|
|
tex->loaded = load;
|
2001-05-11 01:01:27 +00:00
|
|
|
|
2021-01-19 01:15:57 +00:00
|
|
|
if (load) {
|
|
|
|
// skip TARGA image comment
|
|
|
|
dataByte = (byte *) (targa + 1);
|
|
|
|
dataByte += targa->id_length;
|
2001-05-11 01:01:27 +00:00
|
|
|
|
2021-01-19 01:15:57 +00:00
|
|
|
decode (targa, tex, dataByte);
|
2022-05-04 04:50:55 +00:00
|
|
|
} else {
|
|
|
|
//FIXME
|
|
|
|
// assume the format is valid so we can return a format type without
|
|
|
|
// having to check individial image type specific data
|
|
|
|
tex->format = targa_formats[targa->image_type];
|
2021-01-19 01:15:57 +00:00
|
|
|
}
|
2001-05-11 01:01:27 +00:00
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
Hunk_FreeToLowMark (0, targa_mark);
|
2001-12-21 06:07:30 +00:00
|
|
|
return tex;
|
2001-05-11 01:01:27 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-05-11 01:01:27 +00:00
|
|
|
WriteTGAfile (const char *tganame, byte *data, int width, int height)
|
|
|
|
{
|
2010-08-24 07:50:33 +00:00
|
|
|
QFile *qfile;
|
2001-05-11 01:01:27 +00:00
|
|
|
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;
|
|
|
|
|
2010-08-24 07:50:33 +00:00
|
|
|
qfile = QFS_WOpen (tganame, 0);
|
|
|
|
if (!qfile) {
|
|
|
|
Sys_Printf ("Error opening %s", tganame);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Qwrite (qfile, &header, sizeof (header));
|
|
|
|
Qwrite (qfile, data, width * height * 3);
|
|
|
|
Qclose (qfile);
|
2001-05-11 01:01:27 +00:00
|
|
|
}
|