[comp.graphics] PCX file manipulation

djk@ux1.cso.uiuc.edu (Doug Kelly) (01/05/91)

Hello, I need to manipulate an image file created by a color scanner.  I 
read the commonly asked questions file here, and downloaded the infor for
pcx, and tiff file formats.  The tiff is toooooo complicated for my task, so
i decided to use pcx.  I have one quick question concerning the compression
technique involved... As i understand it, the compression is like so:

read X
if X and 192 then
	read Y  
	duplicate Y (X and 63) times
else
	duplicate X one time
end if

Well this is fine but i have one problem, my scanner is 24 bit color, ie
8 bits in RGB.  So, how do i know if the X i read is really a duplicate coding
or just a real singular value of X?

Thanks for any help on this, and sorry if it's a stupid question...

Doug Kelly
djk@ux1.cso.uiuc.edu

josef@nixpbe.nixdorf.de (josef Moellers) (01/08/91)

In <1991Jan4.160540.19245@ux1.cso.uiuc.edu> djk@ux1.cso.uiuc.edu (Doug Kelly) writes:

>Hello, I need to manipulate an image file created by a color scanner.  I 
>read the commonly asked questions file here, and downloaded the infor for
>pcx, and tiff file formats.  The tiff is toooooo complicated for my task, so
>i decided to use pcx.  I have one quick question concerning the compression
>technique involved... As i understand it, the compression is like so:

>read X
>if X and 192 then
>	read Y  
>	duplicate Y (X and 63) times
>else
>	duplicate X one time
>end if

>Well this is fine but i have one problem, my scanner is 24 bit color, ie
>8 bits in RGB.  So, how do i know if the X i read is really a duplicate coding
>or just a real singular value of X?

>Thanks for any help on this, and sorry if it's a stupid question...

There are no stupid questions, only stupid answers (I sincerely hope
this is no stupid answer B-{)!

The decoding is as follows (I use C-notation):

X = nextbyte();
if ((X & 0xc0) == 0xc0)	/* NOTE: 2 MSBs must both be 1! */
{
	Y = nextbyte();
	for (i = 0; i < (X & 0x3f); i++)	/* sub-optimal */
		putbyte(Y);
}
else
	putbyte(X);

Now, when encoding, You must take care that You do not generate the
"repeat" code by mistake, so

X = nextbyte();
if ((X & 0xc0) == 0xc0)
	putbyte(0xc1);	/* repeat next byte 1 times */
putbyte(X);

The main idea is that whenever a repeat code is seen during decoding,
the next byte is repeated no matter what it's code, i.e. You cannot have
nested repeats! If You want more than 63 repeats, You'll have to do two
or more repeats, e.g.
		0xff 0x00 0xff 0x00
will repeat 0x00 126 times.

Hope this helps!
--
| Josef Moellers		| c/o Siemens Nixdorf Informationssysteme AG |
|  USA: mollers.pad@nixdorf.com	| Abt. STO-XS 113			     |
| !USA: mollers.pad@nixdorf.de	| Heinz-Nixdorf-Ring			     |
| Phone: (+49) 5251 104662	| D-4790 Paderborn			     |