newtree/source/pcx.c

111 lines
2.3 KiB
C
Raw Normal View History

/*
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
$Id$
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "console.h"
#include "pcx.h"
2000-12-01 08:59:44 +00:00
#include "qendian.h"
#include "qtypes.h"
#include "quakefs.h"
byte *pcx_rgb;
/*
LoadPCX
*/
void LoadPCX (QFile *f)
{
pcx_t *pcx, pcxbuf;
byte palette[768];
byte *pix;
int x, y;
int dataByte, runLength;
int count;
//
// parse the PCX file
//
2000-12-01 08:59:44 +00:00
Qread (f, &pcxbuf, sizeof (pcxbuf));
pcx = &pcxbuf;
2000-12-01 08:59:44 +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
|| pcx->bits_per_pixel != 8
|| pcx->xmax >= 320
|| pcx->ymax >= 256) {
Con_Printf ("Bad pcx file\n");
return;
}
// seek to palette
Qseek (f, -768, SEEK_END);
Qread (f, palette, 768);
2000-12-01 08:59:44 +00:00
Qseek (f, sizeof (pcxbuf) - 4, SEEK_SET);
2000-12-01 08:59:44 +00:00
count = (pcx->xmax + 1) * (pcx->ymax + 1);
pcx_rgb = malloc (count * 4);
2000-12-01 08:59:44 +00:00
for (y = 0; y <= pcx->ymax; y++) {
pix = pcx_rgb + 4*y*(pcx->xmax+1);
2000-12-01 08:59:44 +00:00
for (x = 0; x <= pcx->ymax;) {
dataByte = Qgetc (f);
if((dataByte & 0xC0) == 0xC0) {
runLength = dataByte & 0x3F;
2000-12-01 08:59:44 +00:00
dataByte = Qgetc (f);
} else {
runLength = 1;
2000-12-01 08:59:44 +00:00
}
2000-12-01 08:59:44 +00:00
while (runLength-- > 0) {
pix[0] = palette[dataByte * 3];
pix[1] = palette[dataByte * 3 + 1];
pix[2] = palette[dataByte * 3 + 2];
pix[3] = 255;
pix += 4;
x++;
}
}
}
}