Viewing a PCX-file

Posted on 1998-06-23
Last Modified: 2010-04-15
I'm looking for a C/C++-code that will load a PCX-file in my program. I'm using BC4.5 and the program must run under DOS.
I want easy-to-understand sourcecode since I am quite new to this.
Question by:mape
  • 2

Accepted Solution

jers7 earned 50 total points
ID: 1251373
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.

typedef BYTE unsigned char;

struct PCXheader{

    BYTE Version;
    BYTE Encoding;
    BYTE BitsPerPixel;
    int Xmin,Ymin,Xmax,Ymax;
    int  HRes;
    int VRes;
    BYTE Colormap[48];        
    BYTE Reserved;
    BYTE  NPlanes;        
    int  BytesPerLine;
    int  PaletteInfo;
    BYTE  Filler[58];


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.

FILE *pcx;
unsigned char buffer[1000];


    /* initialize graphics */
    /*Open PCX file*/
    /* read file header*/
    /*Compute width and height of the image w/ respect to its Xmin,Ymin,Xmax,Ymax */


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*/
         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 */

          .... /* interpret and write the data from the buffer to the screen */


   /*Close the pcx file */


I'll just send you my code latter, just give me you'r e-mail ad.

Author Comment

ID: 1251374
my email is:
if you send me your code, I'll check it out.
do you have routines for writing the data from the buffer to the screen too, it would be appreciated

Author Comment

ID: 1251375
I will be using VGA 640x400, 256 colors

Featured Post

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question