.PCX file has the following format: First, it has a 128-byte header, which looks like (in order in the file): SIZE in BYTES DECSRIPTION DEFINITION 1 Manufacturer 10 == PC-Paintbrush PCX 1 Version 0 == 2.5 2 == 2.8 with palette info 3 == 2.8 without palette info 5 == 3.0 with palette info 1 Encoding 1 == .PCX run-length encoding 1 Bits per pixel we use 1 for mono images 8 WINDOW (MinX, MinY) (MaxX, MaxY) 2 Horizontal Res Horizontal Resolution 2 Vertical Res Vertical Resolution 48 ( 16 * 3 ) ColourMap R, G, B for each of 16 colours The colour Map used is: 0, 0, 0 255, 255, 255 0, 170, 0 0, 170, 170 170, 0, 0 170, 0, 170 170, 170, 0 170, 170, 170 85, 85, 85 85, 85, 255 85, 255, 85 85, 255, 255 255, 85, 85 255, 85, 255 255, 255, 85 255, 255, 255 1 reserved (this puts in a 0) 1 Num Planes Number of colour planes, this uses 1 2 Bytes Per line 640 pixels/8 = 80 bytes per line The rest of the 128-byte header is padded with zeroes. The data in the file comes after the header. The data section contains a set of scan-lines, running by pixel from left-to-right, then top-to-bottom. Each scan-line contains bytes with 8 pixels each (since this is monochrome, each bit represents one pixel). PC-Paintbrush uses an encoding scheme to conserve on file space. Basically, the scheme compares adjacent bytes in one scan-line. If the adjacent bytes are the same, it the writes out a flag ( 0xC0 ) with a count ( actually 0xC0 | count ). The next byte is the data byte that is repeated. For example, if five bytes are the same 01 (in hex), 01 01 01 01 01 the .PCX output is only two bytes C5 01 where C5 means special flag (C) with a count of 5, and the 01 in the next position is the data byte. If there is no match in the next byte, .PCX files just use the plain data byte (implicit count of 1). For example, if the byte stream is 01 01 01 01 01 04 01 01 the .PCX output is C5 01 04 C2 01 Note: this .PCX format breaks down in two cases: i) If the byte value is >= 0xC0, (the flag code == top two bits are on), then the format must use a C1 (flag with count 1) ii) If there are more than 15 bytes the same, the count makes the 0xC0 become 0xD0 or higher. This confuses the decoding software in PC-Paintbrush. Instead, only use counts UNDER 15. If there are 17 of the same bytes in a row, use two count flags, e.g., 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 becomes CE 01 C3 01