2001-05-11 01:01:27 +00:00
|
|
|
/*
|
|
|
|
pcx.c
|
|
|
|
|
|
|
|
pcx 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
|
|
|
|
|
2003-09-04 18:59:39 +00:00
|
|
|
#include "QF/image.h"
|
2001-05-11 01:01:27 +00:00
|
|
|
#include "QF/pcx.h"
|
|
|
|
#include "QF/qendian.h"
|
|
|
|
#include "QF/qtypes.h"
|
2002-08-27 07:16:28 +00:00
|
|
|
#include "QF/quakefs.h"
|
2001-09-21 04:22:46 +00:00
|
|
|
#include "QF/sys.h"
|
2001-05-11 01:01:27 +00:00
|
|
|
#include "QF/zone.h"
|
|
|
|
|
2001-05-31 18:11:05 +00:00
|
|
|
#include "compat.h"
|
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE tex_t *
|
2023-06-13 09:06:11 +00:00
|
|
|
LoadPCX (QFile *f, bool convert, const byte *pal, int load)
|
2001-05-11 01:01:27 +00:00
|
|
|
{
|
|
|
|
pcx_t *pcx;
|
2021-07-29 02:12:37 +00:00
|
|
|
size_t pcx_mark;
|
2001-05-11 01:01:27 +00:00
|
|
|
byte *palette;
|
2011-12-29 02:27:54 +00:00
|
|
|
byte *end;
|
2001-05-11 01:01:27 +00:00
|
|
|
byte *pix;
|
|
|
|
byte *dataByte;
|
|
|
|
int runLength = 1;
|
|
|
|
int count;
|
|
|
|
tex_t *tex;
|
2021-01-19 01:15:57 +00:00
|
|
|
int fsize = sizeof (pcx_t);
|
2001-05-11 01:01:27 +00:00
|
|
|
|
2021-01-19 01:15:57 +00:00
|
|
|
if (load) {
|
|
|
|
fsize = Qfilesize(f);
|
|
|
|
}
|
2001-05-11 01:01:27 +00:00
|
|
|
// parse the PCX file
|
2021-07-28 06:01:45 +00:00
|
|
|
pcx_mark = Hunk_LowMark (0);
|
|
|
|
pcx = Hunk_AllocName (0, fsize, "PCX");
|
2011-01-09 05:55:40 +00:00
|
|
|
Qread (f, pcx, fsize);
|
2001-05-11 01:01:27 +00:00
|
|
|
|
|
|
|
pcx->xmax = LittleShort (pcx->xmax);
|
|
|
|
pcx->xmin = LittleShort (pcx->xmin);
|
|
|
|
pcx->ymax = LittleShort (pcx->ymax);
|
|
|
|
pcx->ymin = LittleShort (pcx->ymin);
|
|
|
|
pcx->hres = LittleShort (pcx->hres);
|
|
|
|
pcx->vres = LittleShort (pcx->vres);
|
|
|
|
pcx->bytes_per_line = LittleShort (pcx->bytes_per_line);
|
|
|
|
pcx->palette_type = LittleShort (pcx->palette_type);
|
|
|
|
|
|
|
|
if (pcx->manufacturer != 0x0a
|
|
|
|
|| pcx->version != 5
|
|
|
|
|| pcx->encoding != 1
|
2001-12-21 21:12:01 +00:00
|
|
|
|| pcx->bits_per_pixel != 8) {
|
2011-06-14 17:13:33 +00:00
|
|
|
Sys_Printf ("Bad pcx file: %x %d %d %d\n",
|
2021-01-19 01:15:57 +00:00
|
|
|
pcx->manufacturer, pcx->version, pcx->encoding,
|
|
|
|
pcx->bits_per_pixel);
|
2021-07-28 06:01:45 +00:00
|
|
|
Hunk_FreeToLowMark (0, pcx_mark);
|
2001-05-11 01:01:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-29 02:27:54 +00:00
|
|
|
end = palette = ((byte *) pcx) + fsize - 768;
|
2001-12-21 21:12:01 +00:00
|
|
|
dataByte = (byte *) &pcx[1];
|
2001-05-11 01:01:27 +00:00
|
|
|
|
2021-01-19 01:15:57 +00:00
|
|
|
count = load ? (pcx->xmax + 1) * (pcx->ymax + 1) : 0;
|
2001-05-11 01:01:27 +00:00
|
|
|
if (convert) {
|
2021-07-28 06:01:45 +00:00
|
|
|
tex = Hunk_TempAlloc (0, sizeof (tex_t) + count * 3);
|
2021-01-24 15:54:41 +00:00
|
|
|
tex->data = (byte *) (tex + 1);
|
2003-10-09 19:16:14 +00:00
|
|
|
tex->format = tex_rgb;
|
2001-05-11 01:01:27 +00:00
|
|
|
tex->palette = 0;
|
|
|
|
} else {
|
2021-07-28 06:01:45 +00:00
|
|
|
tex = Hunk_TempAlloc (0, sizeof (tex_t) + count);
|
2021-01-24 15:54:41 +00:00
|
|
|
tex->data = (byte *) (tex + 1);
|
2001-12-21 21:12:01 +00:00
|
|
|
tex->format = tex_palette;
|
2003-10-09 19:16:14 +00:00
|
|
|
if (pal)
|
|
|
|
tex->palette = pal;
|
|
|
|
else
|
|
|
|
tex->palette = palette;
|
2001-05-11 01:01:27 +00:00
|
|
|
}
|
2003-03-20 21:08:04 +00:00
|
|
|
tex->width = pcx->xmax + 1;
|
|
|
|
tex->height = pcx->ymax + 1;
|
2021-01-19 01:15:57 +00:00
|
|
|
tex->loaded = load;
|
|
|
|
if (!load) {
|
2021-07-28 06:01:45 +00:00
|
|
|
Hunk_FreeToLowMark (0, pcx_mark);
|
2021-01-19 01:15:57 +00:00
|
|
|
return tex;
|
|
|
|
}
|
2001-05-11 01:01:27 +00:00
|
|
|
pix = tex->data;
|
|
|
|
|
2002-05-28 06:05:36 +00:00
|
|
|
while (count) {
|
2011-12-29 02:27:54 +00:00
|
|
|
if (dataByte >= end)
|
2002-05-28 06:05:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
if ((*dataByte & 0xC0) == 0xC0) {
|
|
|
|
runLength = *dataByte++ & 0x3F;
|
2011-12-29 02:27:54 +00:00
|
|
|
if (dataByte >= end)
|
2001-05-11 01:01:27 +00:00
|
|
|
break;
|
2002-05-28 06:05:36 +00:00
|
|
|
} else {
|
|
|
|
runLength = 1;
|
|
|
|
}
|
2001-05-11 01:01:27 +00:00
|
|
|
|
2002-05-28 06:05:36 +00:00
|
|
|
if (runLength > count)
|
|
|
|
runLength = count;
|
|
|
|
count -= runLength;
|
2001-05-11 01:01:27 +00:00
|
|
|
|
2002-05-28 06:05:36 +00:00
|
|
|
if (convert) {
|
2002-05-28 23:34:02 +00:00
|
|
|
for (; runLength > 0; runLength--) {
|
2002-05-28 06:05:36 +00:00
|
|
|
*pix++ = palette[*dataByte * 3];
|
|
|
|
*pix++ = palette[*dataByte * 3 + 1];
|
|
|
|
*pix++ = palette[*dataByte * 3 + 2];
|
|
|
|
}
|
|
|
|
} else {
|
2002-05-28 23:34:02 +00:00
|
|
|
for (; runLength > 0; runLength--) {
|
2002-05-28 06:05:36 +00:00
|
|
|
*pix++ = *dataByte;
|
2001-05-11 01:01:27 +00:00
|
|
|
}
|
|
|
|
}
|
2002-05-28 06:05:36 +00:00
|
|
|
dataByte++;
|
2001-05-11 01:01:27 +00:00
|
|
|
}
|
2021-07-28 06:01:45 +00:00
|
|
|
Hunk_FreeToLowMark (0, pcx_mark);
|
2001-05-11 01:01:27 +00:00
|
|
|
if (count || runLength) {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("PCX was malformed. You should delete it.\n");
|
2001-05-11 01:01:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return tex;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE pcx_t *
|
2013-01-27 10:57:40 +00:00
|
|
|
EncodePCX (const byte *data, int width, int height,
|
2023-06-13 09:06:11 +00:00
|
|
|
int rowbytes, const byte *palette, bool flip, int *length)
|
2001-05-11 01:01:27 +00:00
|
|
|
{
|
2013-01-27 10:57:40 +00:00
|
|
|
int i, run, pix, size;
|
|
|
|
pcx_t *pcx;
|
|
|
|
byte *pack;
|
|
|
|
const byte *dataend;
|
2001-05-11 01:01:27 +00:00
|
|
|
|
2004-05-11 07:27:27 +00:00
|
|
|
size = width * height * 2 + 1000;
|
2021-07-28 06:01:45 +00:00
|
|
|
if (!(pcx = Hunk_TempAlloc (0, size))) {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("EncodePCX: not enough memory\n");
|
2001-05-11 01:01:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2004-05-11 07:27:27 +00:00
|
|
|
memset (pcx, 0, size);
|
2001-05-11 01:01:27 +00:00
|
|
|
|
|
|
|
pcx->manufacturer = 0x0a; // PCX id
|
|
|
|
pcx->version = 5; // 256 color
|
2011-01-09 09:06:50 +00:00
|
|
|
pcx->encoding = 1; // RLE
|
2001-05-11 01:01:27 +00:00
|
|
|
pcx->bits_per_pixel = 8; // 256 color
|
|
|
|
pcx->xmin = 0;
|
|
|
|
pcx->ymin = 0;
|
|
|
|
pcx->xmax = LittleShort ((short) (width - 1));
|
|
|
|
pcx->ymax = LittleShort ((short) (height - 1));
|
|
|
|
pcx->hres = LittleShort ((short) width);
|
|
|
|
pcx->vres = LittleShort ((short) height);
|
|
|
|
pcx->color_planes = 1; // chunky image
|
|
|
|
pcx->bytes_per_line = LittleShort ((short) width);
|
2011-01-09 09:34:35 +00:00
|
|
|
pcx->palette_type = LittleShort (1); // not a grey scale
|
2001-05-11 01:01:27 +00:00
|
|
|
|
|
|
|
// pack the image
|
2001-12-21 21:12:01 +00:00
|
|
|
pack = (byte *) &pcx[1];
|
2001-05-11 01:01:27 +00:00
|
|
|
|
|
|
|
if (flip)
|
|
|
|
data += rowbytes * (height - 1);
|
|
|
|
|
|
|
|
for (i = 0; i < height; i++) {
|
2011-01-09 08:06:25 +00:00
|
|
|
// from darkplaces lmp2pcx
|
|
|
|
for (dataend = data + width; data < dataend; /* empty */) {
|
2012-05-21 23:23:22 +00:00
|
|
|
for (pix = *data++, run = 0xC1;
|
2011-01-09 08:06:25 +00:00
|
|
|
data < dataend && run < 0xFF && *data == pix;
|
|
|
|
data++, run++)
|
|
|
|
/* empty */;
|
|
|
|
if (run > 0xC1 || pix >= 0xC0)
|
|
|
|
*pack++ = run;
|
|
|
|
*pack++ = pix;
|
2001-05-11 01:01:27 +00:00
|
|
|
}
|
|
|
|
|
2011-01-09 08:06:25 +00:00
|
|
|
if (width & 1)
|
|
|
|
*pack++ = 0;
|
|
|
|
|
2001-05-11 01:01:27 +00:00
|
|
|
data += rowbytes - width;
|
|
|
|
if (flip)
|
|
|
|
data -= rowbytes * 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// write the palette
|
|
|
|
*pack++ = 0x0c; // palette ID byte
|
|
|
|
for (i = 0; i < 768; i++)
|
|
|
|
*pack++ = *palette++;
|
|
|
|
|
2012-05-21 23:23:22 +00:00
|
|
|
// write output file
|
2001-05-11 01:01:27 +00:00
|
|
|
*length = pack - (byte *) pcx;
|
|
|
|
return pcx;
|
|
|
|
}
|