Jump to content

PCX

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 83.216.149.7 (talk) at 12:46, 8 August 2011 (→‎Color palette). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

PCX
Filename extension
.pcx
Internet media type
image/x-pcx
Developed byZSoft Corporation
Type of formatlossless bitmap image format

PCX is an image file format developed by the now-defunct ZSoft Corporation of Marietta, Georgia. It was the native file format for PC Paintbrush (PCX = "Personal Computer eXchange") and became one of the first widely accepted DOS imaging standards, although it has since been succeeded by more sophisticated image formats, such as GIF, JPEG and PNG.

The PCX is a device-independent raster image format; the file header stores metadata about the image (pixel resolution, color bit depth and number of bitplanes, color palettes, etc.) separately from the image bitmap, allowing the image to be properly transferred and displayed on computer systems with different software applications and arbitrary hardware. PCX files commonly store palette-indexed images ranging from 2 or 4 colors to 16 and 256 colors, although the format has been extended to record true-color (24-bit) images as well.

File structure

A PCX file has three main sections, in the following order

  1. 128-byte header
  2. image data
  3. (optional) 256-color palette

PCX files were designed for use on IBM-compatible PCs and always use little endian byte ordering.

File header

Table A. Typical PCX Image Formats
Planes Bit Depth Display Type
1 1 Monochrome
1 2 CGA (4 colour palleted)
3 1 EGA (8 color palleted)
4 1 EGA or high-res. (S)VGA (16 color palleted)
1 8 XGA or low-res. VGA

(256 color palleted/grayscale)

3 or 4 8 SVGA/XGA and above (24/32-bit "true color",

3x grayscale planes with optional 8-bit alpha channel)

The PCX file header contains an identifier byte (value 10), a version number, image dimensions, a 16 color palette, number color planes and the bit depth of each plane. While the file header could describe a wide variety of image formats, many are not of practical use. Convention has limited the supported combinations of plane count and bit depth to match specific PC display hardware. Many image editing programs will not read PCX files not matching one of these conventions. Table A shows the most commonly supported combinations.

The header also contains a value for compression method. All PCX files are written with the same compression scheme and this value is always 1. No other values have been defined and there are no uncompressed PCX files.

PCX version numbers range from 0 to 5, though the file format does not change between versions.

The header is always 128 bytes long, though only 74 bytes are used. The rest of the 128 bytes is padded, and the image data always begins 128 bytes after the start of the file.

Image data

Table B. PCX Image Data Arranged into Color Planes
Row 0 R R R R R R R R R
G G G G G G G G
B B B B B B B B B
A A A A A A A A A
Row 1 R R R R R R R R R
G G G G G G G G
B B B B B B B B B
A A A A A A A A A
Row 2 etc. ....

PCX image data are stored in rows or scan lines in top-down order. Where the image has multiple planes, these are stored by plane within row, such that all the red data for row 0 are followed by all the green data for row 0, then all the blue data, then alpha (or intensity) data. This pattern is repeated for each line as shown in Table B.

When an image is less than 8 bits per pixel, each line is padded to the next byte boundary. For example, if an image has 1 plane of 1-bit data (monochrome) with a width of 22 pixels, each row will be 3 bytes long, having 24 bits per row with 2 bits unused.

PCX image data are compressed using run-length encoding (RLE), a simple lossless compression algorithm that collapses a series of three or more consecutive bytes with identical colors into a two-byte pair. As the file is processed, the two most-significant bits of a byte are used to determine whether the given data represent a single pixel of a given palette index or color value, or an RLE pair representing a series of several pixels of a single value. This RLE scheme makes a trade-off; it can have more single-pixel data, but the maximum run length is 63 (compared to the 128 possible with TGA RLE compression).

Due to the use of the two most-significant bits as flags, pixel values from 192 to 255 (with their most-significant bit already set) must be stored in an RLE byte pair, even when they only occur one or two pixels in succession, whereas color indexes 0 to 191 can be stored directly or in RLE byte pairs (whichever is more space-efficient); therefore, the actual compression ratio could be optimized with proper sorting of palette entries, though this is not feasible where the file must share its color palette with other images. For example, a palette could be optimized with the most commonly used colors occurring in palette positions 0 to 191 and the least common colors allocated to the remaining quarter of the palette.

Another inefficiency with the RLE algorithm is that it is possible to store chunks with a length of 0, which allows whitespace in the file. This does, however, allow it to be decoded a cycle quicker on some processors, namely on the DOS machines for which it was originally intended. This quirk could be used for steganography.

The PCX compression algorithm requires very little processor power or memory to apply, a significant concern with the computer systems of the time, but as computers and display hardware grow more sophisticated, the PCX algorithm becomes less space-efficient. Compression algorithms used by newer image formats are more efficient when compressing dithered images, such as photographs or complex computer graphics.

Color palette

When VGA display hardware was introduced, the designers of PCX had a problem. The PCX file has space in its header for a 16 color palette. Even with the unused 54 bytes of header there was not enough space for the 256 colors available in VGA mode. The solution was to put the palette at the end of the file, along with a marker byte to confirm its existence.

If a PCX file has a 256 color palette, it is found 768 bytes from the end of the file. In this case the value in the byte preceding the palette should be 12 (0x0C). Unfortunately, not all programmers kept to this scheme precisely. PCX files have been created with 2-, 4-, 8-, or 16-color palettes at the end of the file rather than in the header. The palette is stored as a sequence of RGB triples; its usable length is defined by the number of colors in the image.

Colors values in PCX palettes always use 8 bits, regardless of the bit depth of the image.

Multipage PCX

There is a multi-page version of PCX, used by some computer FAX and document management programs, using the file extension DCX.

Further reading