Sunday, September 22, 2019 | Toby Opferman
 

PCX File Format

Toby Opferman
http://www.opferman.net
programming@opferman.net




                   How to display a 256 Colour PCX File

        PCX is the easiest compressed image data file there is to display.
The format consists of a 128 Byte Header, The Compressed Image, 768 Byte
pallete.


The Header:
The 128 Byte Header Consists of the following fields:

OFFSET    SIZE     DESCRIPTION
______________________________
0        1 Byte      Manufacturer Of The File
1        1 Byte      Version of PCX
2        1 Byte      Type of Encoding (RLE Mostly)
3        1 Byte      Number of Bits Per Pixel
4        2 Bytes     Starting X location of Image
6        2 Bytes     Starting Y location of Image
8        2 Bytes     Width Of Image
10       2 Bytes     Height Of Image
12       2 Bytes     Horziontal DPI
14       2 Bytes     Verticle DPI
16       48 Bytes    Old EGA Pallete (Mostly Unused)
64       1 Byte      RESERVED
65       1 Byte      Number of Color Planes
66       2 Bytes     Bytes Per Line
68       2 Bytes     Pallete Type (1 = Color, 0 = Grey Scale)
70       58 Bytes    Extra Padding


Of course, this header can usually be ignored if you already know your
data indefinate.  Only reason to want to read this is if you want to check
to make sure it's the right kind your reader will view or if you make a
generic reader that will read any and all resolutions.  Of course, if
you're making this for a program, and you make the image yourself, you
already know all the imformation you need and you can usually strip
this 128 bytes off the file to save some space.


Whether you skip this or not, we will move onto the Body of the PCX file.
The Body is of unknown size.  It uses RLE (Run Length Encoding), which will
be explained here.

RLE Coding is very simple compression.  You read a Byte.  If the byte is
Less than 192, you plot it and increment your byte counter (When your
byte counter is = to Height*Width of image, you are done).  If it is >= 192,
you take this byte and subtract 192 from it (Or and it with 63).  Now, you
read in the next byte.  No matter what the second byte is, you plot it
the number of times as the first byte & 63 or byte - 192 which ever you did.
Also, add the count to the Byte Counter.

Pseoudo Code:

BYTECOUNTER IS 0
PLOT IMAGE:
 IS BYTECOUNTER = HEIGHT*WIDTH ? IF YES, THEN GO TO DONE
   READ IN A BYTE TO F
   PUT A ONE INTO PLOT_TIMES

   IS F < 192 ? IF YES, THEN GO TO PLOT IT
     PLOT_TIMES = F - 192
     READ IN A BYTE TO F

   PLOT IT:
    PLOT F TO SCREEN PLOT_TIMES
    ADD PLOT_TIMES TO BYTECOUNTER

 GO TO PLOT IMAGE
DONE:

As you see, it's very simple.  

Next is the pallete.  The pallete is 768 bytes at the end of the file,
so you may want to go to the end of the file and seek -768.
Each Byte is Multipled by 4 before it is put into the file.  The
bytes are stored Red, Green Blue.  Here is simple Pseudo Code to
read in the pallete:


SEEK -768 BYTES FROM END OF FILE
LET X BE 0
LOAD PALLETE:
  IS X = 256 ? IF YES THEN DONE
   READ ONE BYTE INTO Y
   DIVIDE Y BY 4
   SET COLOR INDEX X RED TO Y

   READ ONE BYTE INTO Y
   DIVIDE Y BY 4
   SET COLOR INDEX X GREEN TO Y

   READ ONE BYTE INTO Y
   DIVIDE Y BY 4
   SET COLOR INDEX X BLUE TO Y

   ADD ONE TO X
  GO TO LOAD PALLETE
DONE:

Pretty simple stuff.  Instead of using Divide, you may want to just
shift right twice.

And now you're ready to use PCX files in your code!





       
 
About Toby Opferman

Professional software engineer with over 15 years...

Learn more »
Codeproject Articles

Programming related articles...

Articles »
Resume

Resume »
Contact

Email: codeproject(at)opferman(dot)com