[Gtkradiant] [Bug 371] propagate PCX fix code?

gtkradiant@zerowing.idsoftware.com gtkradiant@zerowing.idsoftware.com
Tue, 05 Mar 2002 13:36:34 -0600


http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=371





------- Additional Comments From ydnar@rasterproductions.com  2002-03-05 13:36 -------
/*
==============
LoadPCX
==============
*/

/* RR2DO2 */
#define DECODEPCX( b, d, r ) d=*b++;if((d&0xC0)==0xC0){r=d&0x3F;d=*b++;}else
{r=1;}

void LoadPCX( const char *filename, byte **pic, byte **palette, int *width, int 
*height )
{
	byte	*raw;
	pcx_t	*pcx;
	int		x, y, lsize;
	int		len;
	int		dataByte, runLength;
	byte	*out, *pix;
	

	/* load the file */
	len = vfsLoadFile (filename, (void **)&raw, 0);
    if( len == -1 ) 
		Error( "LoadPCX: Couldn't read %s", filename );

	
	/* parse the PCX file */
	pcx = (pcx_t *)raw;
	raw = &pcx->data;

	pcx->xmin = LittleShort(pcx->xmin);
	pcx->ymin = LittleShort(pcx->ymin);
	pcx->xmax = LittleShort(pcx->xmax);
	pcx->ymax = LittleShort(pcx->ymax);
	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 >= 640
		|| pcx->ymax >= 480)
		Error ("Bad pcx file %s", filename);
	
	if (palette)
	{
		*palette = safe_malloc(768);
		memcpy (*palette, (byte *)pcx + len - 768, 768);
	}

	if (width)
		*width = pcx->xmax+1;
	if (height)
		*height = pcx->ymax+1;

	if (!pic)
		return;

	out = safe_malloc ( (pcx->ymax+1) * (pcx->xmax+1) );
	if (!out)
		Error( "LoadPCX: couldn't allocate");

	*pic = out;
	pix = out;
	
	/* RR2DO2: pcx fix  */
	lsize = pcx->color_planes * pcx->bytes_per_line;
	
	/* go scanline by scanline */
	for( y = 0; y <= pcx->ymax; y++, pix += pcx->xmax + 1 )
	{
		/* do a scanline */
		for( x=0; x <= pcx->xmax; )
		{
			/* RR2DO2 */
			DECODEPCX( raw, dataByte, runLength );
			while( runLength-- > 0 )
				pix[ x++ ] = dataByte;
		}

		/* RR2DO2: discard any other data */
		while( x < lsize )
		{
			DECODEPCX( raw, dataByte, runLength );
			x++;
		}
		while( runLength-- > 0 )
			x++;
	}
	
	/* validity check */
	if( raw - (byte *) pcx > len)
		Error( "PCX file %s was malformed", filename );
	free( pcx );
}



------- You are receiving this mail because: -------
Whoops!  I have no idea!