2000-03-04 16:57:34 +00:00
|
|
|
/*
|
|
|
|
pcx.c
|
2000-03-19 15:59:51 +00:00
|
|
|
|
2000-03-04 16:57:34 +00:00
|
|
|
PCX loading/saving functions
|
2000-03-19 15:59:51 +00:00
|
|
|
|
2000-03-04 16:57:34 +00:00
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
Copyright (C) 1999,2000 contributors of the QuakeForge project
|
|
|
|
Please see the file "AUTHORS" for a list of contributors
|
|
|
|
|
|
|
|
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
|
2000-03-19 15:59:51 +00:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
2000-03-04 16:57:34 +00:00
|
|
|
|
|
|
|
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:
|
|
|
|
|
2000-03-24 12:43:07 +00:00
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
$Id$
|
2000-03-04 16:57:34 +00:00
|
|
|
*/
|
|
|
|
|
2000-03-14 05:11:50 +00:00
|
|
|
#include <string.h>
|
2000-03-04 16:57:34 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <qtypes.h>
|
|
|
|
#include <quakeio.h>
|
|
|
|
#include <console.h>
|
|
|
|
#include <image.h>
|
2000-03-04 20:32:38 +00:00
|
|
|
#include <zone.h>
|
|
|
|
#include <quakefs.h>
|
|
|
|
#include <sys.h>
|
2000-03-04 16:57:34 +00:00
|
|
|
|
2000-03-19 17:01:35 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
2000-03-04 16:57:34 +00:00
|
|
|
/*
|
|
|
|
LoadPCX
|
|
|
|
*/
|
2000-08-20 13:33:48 +00:00
|
|
|
byte *
|
|
|
|
LoadPCX ( char *file, cache_user_t *cache, int buf_x, int buf_y)
|
|
|
|
{
|
2000-03-04 20:32:38 +00:00
|
|
|
pcx_t *pcx;
|
2000-03-04 16:57:34 +00:00
|
|
|
byte *pix;
|
2000-03-04 20:32:38 +00:00
|
|
|
byte *raw, *out;
|
2000-03-04 16:57:34 +00:00
|
|
|
int x, y;
|
|
|
|
int dataByte, runLength;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Parse PCX file
|
|
|
|
*/
|
2000-03-04 20:32:38 +00:00
|
|
|
out = Cache_Check (cache);
|
|
|
|
if (out)
|
|
|
|
{
|
|
|
|
Con_Printf("Using cached version of %s\n", file);
|
|
|
|
return out;
|
|
|
|
}
|
2000-03-19 15:59:51 +00:00
|
|
|
|
2000-03-04 20:32:38 +00:00
|
|
|
raw = COM_LoadTempFile (file);
|
|
|
|
if (!raw) {
|
|
|
|
Con_Printf("Can not open %s\n", file);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-03-04 16:57:34 +00:00
|
|
|
|
2000-03-04 20:32:38 +00:00
|
|
|
pcx = (pcx_t *)raw;
|
|
|
|
raw = &pcx->data;
|
2000-03-04 16:57:34 +00:00
|
|
|
|
2000-03-19 15:59:51 +00:00
|
|
|
if (pcx->manufacturer != 0x0a
|
2000-03-04 20:32:38 +00:00
|
|
|
|| pcx->version != 5
|
|
|
|
|| pcx->encoding != 1
|
|
|
|
|| pcx->bits_per_pixel != 8
|
|
|
|
|| pcx->xmax >= 320
|
|
|
|
|| pcx->ymax >= 200)
|
|
|
|
{
|
2000-03-04 16:57:34 +00:00
|
|
|
Con_Printf ("Bad PCX file\n");
|
2000-03-04 20:32:38 +00:00
|
|
|
return NULL;
|
2000-03-04 16:57:34 +00:00
|
|
|
}
|
|
|
|
|
2000-03-04 20:32:38 +00:00
|
|
|
if (!buf_x)
|
|
|
|
buf_x = pcx->ymax;
|
|
|
|
if (!buf_y)
|
|
|
|
buf_y = pcx->ymax;
|
|
|
|
|
2000-03-31 22:46:39 +00:00
|
|
|
Con_DPrintf("PCX file %s %dx%d\n", file, buf_x, buf_y);
|
2000-03-04 20:32:38 +00:00
|
|
|
out = Cache_Alloc (cache, buf_x * buf_y, file);
|
|
|
|
if (!out)
|
|
|
|
Sys_Error("LoadPCX: couldn't allocate.");
|
|
|
|
|
|
|
|
pix = out;
|
|
|
|
memset(out, 0, buf_x * buf_y);
|
|
|
|
|
|
|
|
for (y=0 ; y<=pcx->ymax ; y++, pix += buf_x) {
|
|
|
|
for (x=0 ; x<=pcx->xmax ; ) {
|
|
|
|
if (raw - (byte*)pcx > com_filesize) {
|
|
|
|
Cache_Free(cache);
|
|
|
|
Con_Printf("PCX file %s was malformed. You should delete it.\n", file);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
dataByte = *raw++;
|
2000-03-04 16:57:34 +00:00
|
|
|
|
2000-03-04 20:32:38 +00:00
|
|
|
if ((dataByte & 0xC0) == 0xC0) {
|
2000-03-04 16:57:34 +00:00
|
|
|
runLength = dataByte & 0x3F;
|
2000-03-04 20:32:38 +00:00
|
|
|
if (raw - (byte*)pcx > com_filesize) {
|
|
|
|
Cache_Free(cache);
|
|
|
|
Con_Printf("PCX file %s was malformed. You should delete it.\n", file);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
dataByte = *raw++;
|
|
|
|
} else
|
2000-03-04 16:57:34 +00:00
|
|
|
runLength = 1;
|
|
|
|
|
2000-03-04 20:32:38 +00:00
|
|
|
if (runLength + x > pcx->xmax + 2) {
|
|
|
|
Cache_Free(cache);
|
|
|
|
Con_Printf("PCX file %s was malformed. You should delete it.\n", file);
|
|
|
|
return NULL;
|
2000-03-04 16:57:34 +00:00
|
|
|
}
|
2000-03-04 20:32:38 +00:00
|
|
|
|
|
|
|
while(runLength-- > 0)
|
|
|
|
pix[x++] = dataByte;
|
2000-03-04 16:57:34 +00:00
|
|
|
}
|
|
|
|
}
|
2000-03-04 20:32:38 +00:00
|
|
|
if (raw - (byte*)pcx > com_filesize) {
|
|
|
|
Cache_Free(cache);
|
|
|
|
Con_Printf("PCX file %s was malformed. You should delete it.\n", file);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-03-05 00:56:25 +00:00
|
|
|
return out;
|
2000-03-04 16:57:34 +00:00
|
|
|
}
|