PCX
PCX, standing for PiCture eXchange, is an image file format developed by the now-defunct ZSoft Corporation of Marietta, Georgia, United States. It was the native file format for PC Paintbrush and became one of the first widely accepted DOS imaging standards, although it has since been succeeded by more sophisticated image formats, such as BMP, JPEG, and PNG. 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 images as well.
PCX image formats
PCX was designed during the early development of PC display hardware and most of the formats it supported are no longer used. The table below shows a list of the most commonly used PCX formats. Contemporary image editing programs may not read PCX files that match older hardware.| Bit Depth | Planes | Number of Colors |
| 4 | 1 | 16 colors from a palette |
| 8 | 1 | 256 colors from a palette |
| 8 | 1 | 256 shades of gray |
| 4 | 4 | 4096 colors with 16 levels of transparency |
| 8 | 3 | 16.7 million, 24-bit "true color" |
| 8 | 4 | 16.7 million with 256 levels of transparency |
| 1 | 1 | 2 colors monochrome |
| 1 | 4 | 16 colors RGBi in 4 planes |
PCX is supported by common image processing software including ACDSee, FastStone, GIMP, ImageMagick, IrfanView, LView, Netpbm, PaintShop Pro, Photoshop, Visio, PMview, XnView and GraphicConverter. In version 2.1.4 FFmpeg could encode and decode the PCX pixel formats rgb24, rgb8, bgr8, rgb4_byte, bgr4_byte, gray, pal8, and monob.
There is a multi-page version of PCX, used by some computer fax and document management programs, with file extension
.dcx. A DCX file consists of a header introducing a set of following PCX files.PCX file format
PCX files were designed for use on IBM-compatible PCs and always use little endian byte ordering. A PCX file has three main sections, in the following order- 128-byte header
- image data
- 256-color palette
| Offset hex | Offset dec | Size | Purpose |
| 00 | 0 | 1 byte | The fixed header field valued at a hexadecimal 0x0A. |
| 01 | 1 | 1 byte | The version number referring to the Paintbrush software release, which might be: ;0: PC Paintbrush version 2.5 using a fixed EGA palette ;2: PC Paintbrush version 2.8 using a modifiable EGA palette ;3: PC Paintbrush version 2.8 using no palette ;4: PC Paintbrush for Windows ;5: PC Paintbrush version 3.0, including 24-bit images |
| 02 | 2 | 1 byte | The method used for encoding the image data. Can be: ;0: No encoding ;1: Run-length encoding |
| 03 | 3 | 1 byte | The number of bits constituting one plane. Most often 1, 2, 4 or 8. |
| 04 | 4 | 2 bytes | The minimum x co-ordinate of the image position. |
| 06 | 6 | 2 bytes | The minimum y co-ordinate of the image position. |
| 08 | 8 | 2 bytes | The maximum x co-ordinate of the image position. |
| 0A | 10 | 2 bytes | The maximum y co-ordinate of the image position. |
| 0C | 12 | 2 bytes | The horizontal image resolution in DPI. |
| 0E | 14 | 2 bytes | The vertical image resolution in DPI. |
| 10 | 16 | 48 bytes | The EGA palette for 16-color images. |
| 40 | 64 | 1 byte | The first reserved field, usually set to zero. |
| 41 | 65 | 1 byte | The number of color planes constituting the pixel data. Mostly chosen to be 1, 3, or 4. |
| 42 | 66 | 2 bytes | The number of bytes of one color plane representing a single scan line. |
| 44 | 68 | 2 bytes | The mode in which to construe the palette: ; 1: The palette contains monochrome or color information ; 2: The palette contains grayscale information |
| 46 | 70 | 2 bytes | The horizontal resolution of the source system's screen. |
| 48 | 72 | 2 bytes | The vertical resolution of the source system's screen. |
| 4A | 74 | 54 bytes | The second reserved field, intended for future extensions, and usually set to zero bytes. |
All PCX files use the same compression scheme and the compression value is always 1. No other values have been defined and there are no uncompressed PCX files. One source claims that 0 is allowed, but not much software supports it.
Image data layout
PCX image data is stored in rows or scan lines in top-down order. If 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 data. This pattern is repeated for each line as shown in the next table:| Row 0 | R R R R R R R R R |
| Row 0 | G G G G G G G G G |
| Row 0 | B B B B B B B B B |
| Row 0 | A A A A A A A A A |
| Row 1 | R R R R R R R R R |
| Row 1 | G G G G G G G G G |
| Row 1 | B B B B B B B B B |
| Row 1 | A A A A A A A A A |
| Row 2 etc. | .... |
When an image is less than 8 bits per pixel, each line is padded to the next even byte boundary. For example, if an image has 1 plane of 1-bit data with a width of 22 pixels, each row will be 4 bytes long, having 32 bits per row with 10 bits unused.
Image data compression
PCX image data are compressed using run-length encoding, a simple lossless compression algorithm that collapses a series of three or more consecutive bytes with identical values into a two-byte pair. 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:- If both bits are 1, the byte is interpreted as the run length. This leaves 6 bits for the actual run length value, a range of 0-63.
- In any other case, the byte is interpreted as a single pixel value. This leaves all values for which bit #7 and bit #8 are not 1 at the same time. This allows all values under 192 to be encoded in a single byte.
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 is because otherwise the length would have to be incremented once which would slow down decompression slightly, this allowed PCX files to be decompressed faster on the processors it was originally intended for.
The PCX compression algorithm requires very little processor power or memory to apply, a significant concern with computer systems when it was designed. Compression algorithms used by newer image formats are more efficient when compressing images such as photographs, and dithered or otherwise complex graphics.
Color palette
A PCX file has space in its header for a 16 color palette. When 256-color VGA hardware became available there was not enough space for the palette in a PCX file; even the 54 unused bytes after the header would not be enough. The solution chosen 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. The palette is stored as a sequence of RGB triples; its usable length is defined by the number of colors in the image. Color values in a PCX palette always use 8 bits, regardless of the bit depth of the image.