Pcx file format begins with a 128 byte header information on how the succeeding data should be interpreted and the nature on how it should be displayed to the screen. The method varies from one type of video mode to another but the algorithm is only a simple run-count.

Here Is a structure that you can use to read or write a PCX file header.

after an execution of the fread() function in C/C++, the succeeding data are data for the image. Example is, if you are to work in a standard VGA mode 640x480 resolution, you should detect how many video planes it have. Commonly it has 4 in 16-color mode. Try to read the value "NPlanes" in the structure, this tells you on what video mode the PCX was designed for, though you can try to translate it to other modes doing some additional programming, I've tried to modify it to fit a single plane 800x600 256-color mode.

In Decoding the image you should set up first a buffer to hold the first scanline for the image. It is given by the formula

TotalBytes = NPlanes * BytesPerLine;

create an array of unsigned char of size TotalBytes. An 800x600 256-color format of a PCX is easier to decode. I'll just disccuss here the 640x480 16-color mode, since we are dealing with 16 -colors and if NPlanes is 4, each plane contains a bit for the whole pixel color.

bcount=0;
for(j=0; j<PictureWidth && !feof(pcx);j++)
{
runbyte=getc(pcx); /* Get the first byte after the header */

if( 0xC0 ==( runbyte & 0xC0)){ /*Test if the two significant bits are "11"*/
runcount = (runbyte & 0x3F); /* If true the number of bytes should be written is strored on the lower 6-bits of the byte read */
runbyte = getc(pcx); /*Read the byte of the value to be wrttien as color*/
}else
runcount =1; /*Else the byte to be written in the video is the lower 6-bits itself from the first read-byte
and runcount is set to 1 */
for(i=0;i<runcount;i++) /* Writes the byte to the buffer */
buffer[bcount++] = runbyte;
} /* end of j-loop */

Featured Post

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net. To view more iPhone tutorials, visit www.sdkexpert.net.
This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…