Reformat pcx.c

This commit is contained in:
Yamagi Burmeister 2012-03-12 09:56:27 +00:00
parent 57b06a8bcf
commit 643cb93e5f

View file

@ -22,26 +22,26 @@
* The PCX file format * The PCX file format
* *
* ======================================================================= * =======================================================================
*/ */
#include "../header/local.h" #include "../header/local.h"
void void
LoadPCX ( char *origname, byte **pic, byte **palette, int *width, int *height ) LoadPCX(char *origname, byte **pic, byte **palette, int *width, int *height)
{ {
byte *raw; byte *raw;
pcx_t *pcx; pcx_t *pcx;
int x, y; int x, y;
int len; int len;
int filelen; int filelen;
int dataByte, runLength; int dataByte, runLength;
byte *out, *pix; byte *out, *pix;
char filename[256]; char filename[256];
/* Add the extension */ /* Add the extension */
filelen = strlen( origname ); filelen = strlen(origname);
if ( strcmp( origname + filelen - 4, ".pcx" ) ) if (strcmp(origname + filelen - 4, ".pcx"))
{ {
strncpy(filename, origname, 256); strncpy(filename, origname, 256);
strncat(filename, ".pcx", 255); strncat(filename, ".pcx", 255);
@ -55,68 +55,63 @@ LoadPCX ( char *origname, byte **pic, byte **palette, int *width, int *height )
*palette = NULL; *palette = NULL;
/* load the file */ /* load the file */
len = ri.FS_LoadFile( filename, (void **) &raw ); len = ri.FS_LoadFile(filename, (void **)&raw);
if ( !raw ) if (!raw)
{ {
ri.Con_Printf( PRINT_DEVELOPER, "Bad pcx file %s\n", filename ); ri.Con_Printf(PRINT_DEVELOPER, "Bad pcx file %s\n", filename);
return; return;
} }
/* parse the PCX file */ /* parse the PCX file */
pcx = (pcx_t *) raw; pcx = (pcx_t *)raw;
pcx->xmin = LittleShort( pcx->xmin ); pcx->xmin = LittleShort(pcx->xmin);
pcx->ymin = LittleShort( pcx->ymin ); pcx->ymin = LittleShort(pcx->ymin);
pcx->xmax = LittleShort( pcx->xmax ); pcx->xmax = LittleShort(pcx->xmax);
pcx->ymax = LittleShort( pcx->ymax ); pcx->ymax = LittleShort(pcx->ymax);
pcx->hres = LittleShort( pcx->hres ); pcx->hres = LittleShort(pcx->hres);
pcx->vres = LittleShort( pcx->vres ); pcx->vres = LittleShort(pcx->vres);
pcx->bytes_per_line = LittleShort( pcx->bytes_per_line ); pcx->bytes_per_line = LittleShort(pcx->bytes_per_line);
pcx->palette_type = LittleShort( pcx->palette_type ); pcx->palette_type = LittleShort(pcx->palette_type);
raw = &pcx->data; raw = &pcx->data;
if ( ( pcx->manufacturer != 0x0a ) || if ((pcx->manufacturer != 0x0a) || (pcx->version != 5) || (pcx->encoding != 1) || (pcx->bits_per_pixel != 8) || (pcx->xmax >= 640) || (pcx->ymax >= 480))
( pcx->version != 5 ) ||
( pcx->encoding != 1 ) ||
( pcx->bits_per_pixel != 8 ) ||
( pcx->xmax >= 640 ) ||
( pcx->ymax >= 480 ) )
{ {
ri.Con_Printf( PRINT_ALL, "Bad pcx file %s\n", filename ); ri.Con_Printf(PRINT_ALL, "Bad pcx file %s\n", filename);
return; return;
} }
out = malloc( ( pcx->ymax + 1 ) * ( pcx->xmax + 1 ) ); out = malloc((pcx->ymax + 1) * (pcx->xmax + 1));
*pic = out; *pic = out;
pix = out; pix = out;
if ( palette ) if (palette)
{ {
*palette = malloc( 768 ); *palette = malloc(768);
memcpy( *palette, (byte *) pcx + len - 768, 768 ); memcpy(*palette, (byte *)pcx + len - 768, 768);
} }
if ( width ) if (width)
{ {
*width = pcx->xmax + 1; *width = pcx->xmax + 1;
} }
if ( height ) if (height)
{ {
*height = pcx->ymax + 1; *height = pcx->ymax + 1;
} }
for ( y = 0; y <= pcx->ymax; y++, pix += pcx->xmax + 1 ) for (y = 0; y <= pcx->ymax; y++, pix += pcx->xmax + 1)
{ {
for ( x = 0; x <= pcx->xmax; ) for (x = 0; x <= pcx->xmax; )
{ {
dataByte = *raw++; dataByte = *raw++;
if ( ( dataByte & 0xC0 ) == 0xC0 ) if ((dataByte & 0xC0) == 0xC0)
{ {
runLength = dataByte & 0x3F; runLength = dataByte & 0x3F;
dataByte = *raw++; dataByte = *raw++;
@ -126,39 +121,42 @@ LoadPCX ( char *origname, byte **pic, byte **palette, int *width, int *height )
runLength = 1; runLength = 1;
} }
while ( runLength-- > 0 ) while (runLength-- > 0)
{ {
pix [ x++ ] = dataByte; pix[x++] = dataByte;
} }
} }
} }
if ( raw - (byte *) pcx > len ) if (raw - (byte *)pcx > len)
{ {
ri.Con_Printf( PRINT_DEVELOPER, "PCX file %s was malformed", filename ); ri.Con_Printf(PRINT_DEVELOPER, "PCX file %s was malformed", filename);
free( *pic ); free(*pic);
*pic = NULL; *pic = NULL;
} }
ri.FS_FreeFile( pcx ); ri.FS_FreeFile(pcx);
} }
qboolean qboolean
GetPCXInfo (char *filename, int *width, int *height) GetPCXInfo(char *filename, int *width, int *height)
{ {
pcx_t *pcx; pcx_t *pcx;
byte *raw; byte *raw;
ri.FS_LoadFile(filename, (void **)&raw);
ri.FS_LoadFile (filename, (void **)&raw);
if (!raw) if (!raw)
{
return false; return false;
}
pcx = (pcx_t *)raw; pcx = (pcx_t *)raw;
*width = pcx->xmax + 1; *width = pcx->xmax + 1;
*height = pcx->ymax + 1; *height = pcx->ymax + 1;
ri.FS_FreeFile (raw); ri.FS_FreeFile(raw);
return true; return true;
} }