Listing 1 Decode PCX Image File Scan Line Function

/* Read an encoded scan line (all color planes) from a          */
/* PCX-format image file and write the decoded data to a scan   */
/* line buffer                                                  */

void pcx_read_line
(
  unsigned char *linep, /* PCX scan line buffer pointer         */
  FILE *fp,             /* PCX image file pointer               */
  int bpline            /* # bytes per line (all color planes)  */
)
{
  int data;        /* Image data byte                           */
  int count;       /* Image data byte repeat count              */
  int offset = 0;  /* Scan line buffer offset                   */

  while (offset < bpline)  /* Decode scan line                  */
  {
    data = getc(fp);          /* Get next byte                  */

    /* If top two bits of byte are set, lower six bits show how */
    /* many times to duplicate next byte                        */

    if ((data & 0xc0) == 0xc0)
    {
      count = data & 0x3f;          /* Mask off repeat count    */
      data = getc(fp);              /* Get next byte            */
      memset(linep, data, count);   /* Duplicate byte           */
      linep += count;
      offset += count;
    }
    else
    {
      *linep++= (unsigned char) data;  /* Copy byte             */
      offset++;
    }
  }
}

/* End of File */