<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://moddingwiki.shikadi.net/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mra</id>
	<title>ModdingWiki - User contributions [en-gb]</title>
	<link rel="self" type="application/atom+xml" href="https://moddingwiki.shikadi.net/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mra"/>
	<link rel="alternate" type="text/html" href="https://moddingwiki.shikadi.net/wiki/Special:Contributions/Mra"/>
	<updated>2026-05-25T22:40:56Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.11</generator>
	<entry>
		<id>https://moddingwiki.shikadi.net/w/index.php?title=PCX_Format&amp;diff=11944</id>
		<title>PCX Format</title>
		<link rel="alternate" type="text/html" href="https://moddingwiki.shikadi.net/w/index.php?title=PCX_Format&amp;diff=11944"/>
		<updated>2024-09-18T12:47:06Z</updated>

		<summary type="html">&lt;p&gt;Mra: Correct the order of VertDPI and HorzDPI&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Image Infobox&lt;br /&gt;
 | Hardware1 = CGA&lt;br /&gt;
 | Hardware2 = EGA&lt;br /&gt;
 | Hardware3 = VGA&lt;br /&gt;
 | Depth = Multiple&lt;br /&gt;
 | MinSize = 0&amp;amp;times;0&lt;br /&gt;
 | MaxSize = 65536&amp;amp;times;65536 &amp;lt;!-- Ymin=0, Ymax=65535 = height 65536 --&amp;gt;&lt;br /&gt;
 | Palette = Internal (optional)&lt;br /&gt;
 | NumPlanes = 1-255&lt;br /&gt;
 | HasTransparency = No&lt;br /&gt;
 | HasHitmap = No&lt;br /&gt;
&amp;lt;!-- All the games that use this file format --&amp;gt;&lt;br /&gt;
 | Games = &lt;br /&gt;
   {{Game|Bubble Bobble featuring Rainbow Islands}}&lt;br /&gt;
   {{Game|Command &amp;amp; Conquer}} (Win95 version only)&lt;br /&gt;
   {{Game|Command &amp;amp; Conquer: Red Alert}}&lt;br /&gt;
   {{Game|Crystal Caves}}&lt;br /&gt;
   {{Game|F1 Manager Professional}}&lt;br /&gt;
   {{Game|Halloween Harry}}&lt;br /&gt;
   {{Game|Hocus Pocus}}&lt;br /&gt;
   {{Game|Hugo&#039;s House of Horrors}}&lt;br /&gt;
   {{Game|Hugo II, Whodunit?}}&lt;br /&gt;
   {{Game|Hugo III, Jungle of Doom!}}&lt;br /&gt;
   {{Game|Math Rescue}}&lt;br /&gt;
   {{Game|The Oregon Trail}}&lt;br /&gt;
   {{Game|Wacky Wheels}}&lt;br /&gt;
   {{Game|Word Rescue}}&lt;br /&gt;
}}&lt;br /&gt;
The &#039;&#039;&#039;PCX Format&#039;&#039;&#039; is an image format used by many games, usually to store full screen (320x200) 16-colour EGA, and later 256-colour VGA (mode 13h), graphics.  It was, for a time, also a general picture format like .bmp or .png, and was the primary format used by PC Paintbrush.  It declined in popularity after support for 24-bit true colour images was added too late, by which time many people had switched to competing formats like .png and JPEG (the latter offering far better compression for photos.)  It also lacks support for transparency, which resulted in it losing some ground to the GIF format which otherwise provided a similar feature set.&lt;br /&gt;
&lt;br /&gt;
== Header ==&lt;br /&gt;
&lt;br /&gt;
The PCX file is composed of two parts, the header and the image data, which is usually compressed. The header is as follows:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Data type !! Name !! Description&lt;br /&gt;
|-&lt;br /&gt;
| [[UINT8]] || Manufacturer || Always 0x0A&lt;br /&gt;
|-&lt;br /&gt;
| [[UINT8]] || Version || PC Paintbrush version. Acts as file format version. &amp;lt;br/&amp;gt;0 = v2.5&amp;lt;br/&amp;gt;2 = v2.8 with palette&amp;lt;br/&amp;gt;3 = v2.8 without palette&amp;lt;br/&amp;gt;4 = Paintbrush for Windows&amp;lt;br/&amp;gt;5 = v3.0 or higher&lt;br /&gt;
|-&lt;br /&gt;
| [[UINT8]] || Encoding || Should be 0x01&amp;lt;br/&amp;gt;0 = uncompressed image (not officially allowed, but some software supports it)&amp;lt;br/&amp;gt;1 = PCX run length encoding&lt;br /&gt;
|-&lt;br /&gt;
| [[UINT8]] || BitsPerPlane || Number of bits per pixel in each entry of the colour planes (1, 2, 4, 8, 24)&lt;br /&gt;
|-&lt;br /&gt;
| [[UINT16LE]] || WindowXmin ||rowspan=4| Window (image dimensions):&amp;lt;br/&amp;gt;ImageWidth = &amp;lt;tt&amp;gt;Xmax - Xmin + 1&amp;lt;/tt&amp;gt;&amp;lt;br/&amp;gt;ImageHeight = &amp;lt;tt&amp;gt;Ymax - Ymin + 1&amp;lt;/tt&amp;gt;&amp;lt;br/&amp;gt;Normally &amp;lt;tt&amp;gt;Xmin&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;Ymin&amp;lt;/tt&amp;gt; should be set to zero. Note that these field values are valid rows and columns, which is why you have to add one to get the actual dimension (so a 200 pixel high image would have Ymin=0 and Ymax=199, or Ymin=100 and Ymax=299, etc.)&lt;br /&gt;
|-&lt;br /&gt;
| [[UINT16LE]] || WindowYmin&lt;br /&gt;
|-&lt;br /&gt;
| [[UINT16LE]] || WindowXmax&lt;br /&gt;
|-&lt;br /&gt;
| [[UINT16LE]] || WindowYmax&lt;br /&gt;
|-&lt;br /&gt;
| [[UINT16LE]] || HorzDPI ||rowspan=2| This is supposed to specify the image&#039;s vertical and horizontal resolution in DPI (dots per inch), but it is rarely reliable. It often contains the image dimensions, or nothing at all.&lt;br /&gt;
|-&lt;br /&gt;
| [[UINT16LE]] || VertDPI&lt;br /&gt;
|-&lt;br /&gt;
| [[UINT8]] || Palette[48] || Palette for 16 colors or less, in three-byte RGB entries. Padded with 0x00 to 48 bytes in total length. See below for more details on palette handling.&lt;br /&gt;
|-&lt;br /&gt;
| [[UINT8]] || Reserved || Should be set to 0, but can sometimes contain junk.&lt;br /&gt;
|-&lt;br /&gt;
| [[UINT8]] || ColorPlanes || Number of colour planes. Multiply by &amp;lt;tt&amp;gt;BitsPerPlane&amp;lt;/tt&amp;gt; to get the actual colour depth.&lt;br /&gt;
|-&lt;br /&gt;
| [[UINT16LE]] || BytesPerPlaneLine || Number of bytes to read for a single plane&#039;s scanline, i.e. at least &amp;lt;tt&amp;gt;ImageWidth&amp;lt;/tt&amp;gt; &amp;amp;divide; 8 bits per byte &amp;amp;times; &amp;lt;tt&amp;gt;BitsPerPlane&amp;lt;/tt&amp;gt;. Must be an even number. Do &#039;&#039;&#039;not&#039;&#039;&#039; calculate from Xmax-Xmin. Normally a multiple of the machine&#039;s native word length (2 or 4)&lt;br /&gt;
|-&lt;br /&gt;
| [[UINT16LE]] || PaletteInfo || How to interpret palette:&amp;lt;br/&amp;gt;1 = Color/BW&amp;lt;br/&amp;gt;2 = Grayscale (ignored in PC Paintbrush IV and later)&lt;br /&gt;
|-&lt;br /&gt;
| [[UINT16LE]] || HorScrSize ||rowspan=2| Only supported by PC Paintbrush IV or higher; deals with scrolling. Best to just ignore it.&lt;br /&gt;
|-&lt;br /&gt;
| [[UINT16LE]] || VerScrSize&lt;br /&gt;
|-&lt;br /&gt;
| [[BYTE]][54] || Padding || Filler to bring header up to 128 bytes total. Can contain junk.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Image Data ==&lt;br /&gt;
&lt;br /&gt;
Image data comes after the header (starting at offset 0x80 in a PCX file), and will be RLE compressed if the header indicated so. The way the data is stored depends on how many colour planes are specified. Each row has its color planes stored sequentially, similar to [[raw EGA data]].&lt;br /&gt;
&lt;br /&gt;
For one plane of eight bits (256-colour), each byte will represent one pixel. For one plane of four bits (16-colour), each byte will represent two pixels. The bits within the byte are in big-endian order, so the most significant bit belongs to the left-most pixel. In other words, in two bits per pixel mode, a byte of value 0xE4 (binary 11 10 01 00) will have left-to-right pixel values of 3, 2, 1, 0.&lt;br /&gt;
&lt;br /&gt;
EGA 16-colour images are often stored with four colour planes instead of one, with each plane being one-bit-per-pixel (think of four black and white images, one each for red, green, blue and intensity.)  The planes are stored sequentially for each line (see [[Raw EGA data#Row-planar EGA data|Row-planar EGA data]] for the exact details), thus a 320x200 EGA image will store at least 40 bytes for each scanline&#039;s colour plane (320 pixels &amp;amp;divide; 8 bits per byte &amp;amp;times; 1 bit per pixel), with each scanline being at least 160 bytes long (320 pixels &amp;amp;divide; 8 bits per byte &amp;amp;times; 1 bit per pixel &amp;amp;times; 4 planes). Note that the scanline length can be larger than expected (40 bytes in this example), especially for images whose width is not a multiple of four. This is because each scanline in a plane is padded to a multiple of two or four bytes, depending on the architecture of the machine used to create the file. The actual size is stored in the &amp;lt;tt&amp;gt;BytesPerPlaneLine&amp;lt;/tt&amp;gt; field in the header, which should always be used instead of calculating the value from the other image attributes.&lt;br /&gt;
&lt;br /&gt;
True colour PCX files are not common, and could be either three planes (R, G and B) of eight bits each (24-bit RGB) or one plane of 24-bits. Technically, the same is applicable for alpha-capable 32-bit images (with four planes in planar mode), though no official format specs ever included such a thing.&lt;br /&gt;
&lt;br /&gt;
The split into planes is generally governed by what is most convenient for the game at the time, which in turn depends on which video mode is being used to display the image. Since EGA video memory is split into planes, 16-colour PCX files are frequently split into matching planes so that no processing is required when loading an image directly into video memory.&lt;br /&gt;
&lt;br /&gt;
=== RLE Compression ===&lt;br /&gt;
&lt;br /&gt;
The PCX format uses a form of [[RLE Compression]] that is rather unique. It compresses on the byte level and uses a flag system, where the flag is the two highest bits of a byte. If this flag is set (i.e. the two upper bits are set, or in other words the value is &amp;gt;= 192) then the lower six bits are the number of times to repeat the following byte.&lt;br /&gt;
&lt;br /&gt;
Thus, for a  byte pair &amp;lt;tt&amp;gt;C7 28&amp;lt;/tt&amp;gt;, the 0xC7 can be decomposed into the flag value 192 (128 + 64) plus 7, meaning the full byte pair means &#039;7 bytes of 0x28&#039;. So to get the amount, you subtract 192 from the flag, or, using faster logic operations, you can do &amp;lt;code&amp;gt;byte &amp;amp; 0x3F&amp;lt;/code&amp;gt; to retain only the lowest six bits.&lt;br /&gt;
&lt;br /&gt;
This means that the six-bit length values have a maximum of 63. It also means that any value larger than 191 &#039;&#039;must&#039;&#039; be stored as a length/value pair, which can actually &#039;&#039;increase&#039;&#039; the size of the file in some cases. For instance, if you have a single byte of color 192, then it must be represented by two bytes - one of 193 (C1, a repeat of one time) followed by one of 192 (C0, color byte 192).&lt;br /&gt;
&lt;br /&gt;
It is also worth noting that the byte value C0 does not have a clearly defined effect. Depending on the implementation in the decoding program, this could do any of the following:&lt;br /&gt;
&lt;br /&gt;
* treat &amp;lt;tt&amp;gt;C0&amp;lt;/tt&amp;gt; as a literal byte.&lt;br /&gt;
* ignore &amp;lt;tt&amp;gt;C0&amp;lt;/tt&amp;gt; and continue with the following byte.&lt;br /&gt;
* &#039;repeat the following byte zero times&#039;, effectively ignoring any byte following &amp;lt;tt&amp;gt;C0&amp;lt;/tt&amp;gt;. This could conceivably be used to embed non-image data in the PCX file which would be ignored by any program displaying the image.&lt;br /&gt;
* &#039;repeat the following byte 65536 times&#039;, which is basically a bugged implementation using a &amp;quot;&amp;lt;tt&amp;gt;while (--count != 0)&amp;lt;/tt&amp;gt;&amp;quot; style loop with a 16 bit variable/CPU register.&lt;br /&gt;
&lt;br /&gt;
At any rate, the best way to handle a &amp;lt;tt&amp;gt;C0&amp;lt;/tt&amp;gt; when encoding (compressing) is to write the sequence &amp;lt;tt&amp;gt;C1 C0&amp;lt;/tt&amp;gt;. When decoding (decompressing), a value of &amp;lt;tt&amp;gt;C0&amp;lt;/tt&amp;gt; almost always indicates an error in the file.&lt;br /&gt;
&lt;br /&gt;
Note that each scanline is compressed independently - an RLE sequence may span multiple planes, but it will never span more than one row of pixels. Thus when decompressing an image, the RLE algorithm will produce at most &amp;lt;tt&amp;gt;BytesPerPlaneLine&amp;lt;/tt&amp;gt; bytes at a time. Even where the RLE coding could have continued over to the next scanline, it will stop and start again fresh for each line.&amp;lt;ref&amp;gt;[http://www.drdobbs.com/pcx-graphics/184402396#01ED_0105 PCX Graphics | Dr Dobb&#039;s]&amp;lt;/ref&amp;gt;  For example, if the input image is 8&amp;amp;times;4 pixels EGA 16-colour, and the first two lines of pixels are black (color 0) and the last two are white (color 15), they must be compressed as &amp;lt;tt&amp;gt;C4 00 | C4 00 | C4 FF | C4 FF&amp;lt;/tt&amp;gt; and not as &amp;lt;tt&amp;gt;C8 00 | C8 FF&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Palettes ===&lt;br /&gt;
&lt;br /&gt;
Most of the game&#039;s supported formats are indexed, and thus require a colour palette. There are, however, three distinct ways of handling those palettes; CGA, EGA and VGA. For images with 16 colours or less, the palette is stored in the header. For images with more colours (i.e. 256-colour images) the header palette is ignored, and the 768-byte palette is stored after the image data. Some sources seem to indicate that there are formats where even the smaller palettes are added behind the image, so it is best to always check for it there. To determine if this is the case, check if the decompressed image data is followed by a &amp;lt;tt&amp;gt;0C&amp;lt;/tt&amp;gt; byte, and if there is enough data left in the file behind that for the palette data.&lt;br /&gt;
&lt;br /&gt;
As a general rule, the palettes are lists of 3-byte blocks. These blocks normally contain 8-bit RGB data, but even if they are used differently (as is the case for CGA palettes), they will still retain the 3-byte block structure.&lt;br /&gt;
&lt;br /&gt;
It is difficult to correctly determine the palette for 1-bit and 2-bit images, since they can be stored either as CGA or as EGA, and there is no real way to identify which method to use. Some sources suggest checking for 640x200 dimensions on 1-bit images, or 320x200 on 2-bit, since those are standard CGA sizes for respectively its monochrome and 4-colour mode, but 320x200 is a very common size for non-CGA images as well, and there has most likely never been anything preventing people on CGA hardware from saving PCX images in different sizes than a full screen. It is also suggested, for two bit per pixel images, that those with a single 2-bit plane would use a CGA palette, while those with two 1-bit planes would use EGA&amp;lt;ref&amp;gt;[http://www.fysnet.net/pcxfile.htm PCX graphics files explained] - section &amp;quot;Interpretation of the PCX data&amp;quot;&amp;lt;/ref&amp;gt;. All of this makes fully automatic identification for these image types very difficult.&lt;br /&gt;
&lt;br /&gt;
==== CGA palette ====&lt;br /&gt;
&lt;br /&gt;
CGA palette handling is a bit peculiar. As with other colour palettes, the data is seen as blocks of three bytes. However, in CGA mode, these blocks do not contain normal RGB colour data.&lt;br /&gt;
&lt;br /&gt;
The first triplet contains a specifically-defined colour. Only the highest four bits of the first byte are used, meaning the actual colour value can be found by taking that byte and shifting it four bits to the right. The result is a value from 0..15, which matches [[EGA Palette|one of the standard CGA/EGA text-mode colours]].&lt;br /&gt;
&lt;br /&gt;
For monochrome CGA images, this colour is put on index 1 on the palette, and index 0 is filled in with black (EGA palette entry #0).&lt;br /&gt;
&lt;br /&gt;
For actual 4-colour palettes, this colour becomes the background colour at index 0. The other colours are determined by three status bits fed into the CGA hardware; &amp;lt;tt&amp;gt;ColorBurst&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;Palette&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;Intensity&amp;lt;/tt&amp;gt;. For the full explanation on which colours this produces, see the [[CGA Palette]] article.&lt;br /&gt;
&lt;br /&gt;
However, the way these three bits are determined changed in PC Paintbrush version IV, and this change is not reflected in a change in the &amp;lt;tt&amp;gt;Version&amp;lt;/tt&amp;gt; value in the header. The only decent way to distinguish the files from before and after version IV is to check the &amp;lt;tt&amp;gt;PaletteInfo&amp;lt;/tt&amp;gt; byte in the header, which is 0 on files using the old method, and filled in (with 1 or 2) on files using the new method.&lt;br /&gt;
&lt;br /&gt;
===== Older method =====&lt;br /&gt;
&lt;br /&gt;
In the original way the status bits were saved, the second palette entry&#039;s first byte (byte 3) contains the three status bits.&amp;lt;ref name=&amp;quot;fileformat&amp;quot;&amp;gt;[https://www.fileformat.info/format/pcx/egff.htm PCX specs on fileformat.info]&amp;lt;/ref&amp;gt; They can be extracted by taking the highest three bits of that byte.&lt;br /&gt;
&lt;br /&gt;
* Bit 8 (&amp;lt;tt&amp;gt;10000000&amp;lt;/tt&amp;gt;) is &amp;lt;tt&amp;gt;ColorBurst&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* Bit 7 (&amp;lt;tt&amp;gt;01000000&amp;lt;/tt&amp;gt;) is the &amp;lt;tt&amp;gt;Palette&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* Bit 6 (&amp;lt;tt&amp;gt;00100000&amp;lt;/tt&amp;gt;) is the &amp;lt;tt&amp;gt;Intensity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Newer method =====&lt;br /&gt;
&lt;br /&gt;
For the newer files, with the &amp;lt;tt&amp;gt;PaletteInfo&amp;lt;/tt&amp;gt; byte filled in, the following&amp;lt;ref&amp;gt;[https://github.com/samuel/go-pcx/blob/d9c017170db4e016377181263f7e4cbb6bf1d8cc/pcx/decoder.go#L299 PCX image encoder and decoder for Go] - &#039;&#039;PC Paintbush 4.0 encodes the CGA palettes differently than 3.0.&#039;&#039;&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[https://github.com/wjaguar/mtPaint/blob/3884b6d6ebc18511df7bf1485a19fe774dd61dcf/src/png.c#L5416 Mark Tyler&#039;s Painting Program] - &#039;&#039;CGA palette is evil: what the PCX spec describes is the way it was handled by PC Paintbrush 3.0, while 4.0 was using an entirely different, undocumented encoding for palette selection.&#039;&#039;&amp;lt;/ref&amp;gt; should be done to determine the palette:&lt;br /&gt;
&lt;br /&gt;
* Take the green and blue values of the second palette entry. These are the bytes at index 4 and 5 in the palette data.&lt;br /&gt;
* If the green value is strictly-greater than the blue value, take Palette 0, otherwise take Palette 1.&lt;br /&gt;
* If the largest of these two values is greater than 200, the Intensity bit is enabled.&lt;br /&gt;
&lt;br /&gt;
The new method seemed to have been intended to convert a fully-saved colour palette back to CGA status bits. Sadly, it seems people figured out exactly what it checked, and started writing PCX files which contained only exactly enough data to derive those bits instead of saving an actual full palette.&lt;br /&gt;
&lt;br /&gt;
As a side effect of this, there is no real check for the color burst, meaning it is always considered to be enabled, and there is no support for palette 2. If an actual palette is saved in the file, such support &#039;&#039;could&#039;&#039; be added. A viable check for detecting palette 2 (meaning, a disabled colour burst bit) would be that the original logic matches palette 1, and the third entry&#039;s red value is greater than its blue value.&lt;br /&gt;
&lt;br /&gt;
==== EGA palette ====&lt;br /&gt;
&lt;br /&gt;
EGA images are those with eight or sixteen colours. Typically, they have &amp;lt;tt&amp;gt;BitsPerPlane&amp;lt;/tt&amp;gt; set to 1 and &amp;lt;tt&amp;gt;ColorPlanes&amp;lt;/tt&amp;gt; set to 3 or 4, though variations like two 2-bit planes or one 4-bit plane are possible as well. For these images, there are two ways of handling the colours:&lt;br /&gt;
&lt;br /&gt;
* If the version in the header is 0 or 3, then the [[EGA Palette|standard EGA palette]] is used, since version 0 does not support a modified palette, and version 3 specifically indicates that no palette information is present in the file.&amp;lt;ref name=&amp;quot;fileformat&amp;quot; /&amp;gt;&lt;br /&gt;
* In any other version, the palette is read from the header. The data is structured the same way as [[VGA Palette#The_.22Modern.22_format|8-bit VGA palettes]], except that it&#039;s less long.&lt;br /&gt;
&lt;br /&gt;
Most EGA images will be 4 bits per pixel, meaning the palette will be 16 colours long, but 3 bit per pixel planar format is supported as well. In that case, only 8 entries are read, or if it uses the default EGA palette, only the darker first eight colours will be available. 2 bit per pixel images could use EGA palette handling as well, but these are hard to distinguish from the CGA ones.&lt;br /&gt;
&lt;br /&gt;
Note that technically, EGA images are limited to 2 bit colour components, meaning every component would need to be rounded to the nearest multiple of &amp;lt;tt&amp;gt;0x55&amp;lt;/tt&amp;gt;, though, practically, there is little reason to not let the images retain the more accurate colours as they are specified in the palette.&lt;br /&gt;
&lt;br /&gt;
==== VGA palette ====&lt;br /&gt;
&lt;br /&gt;
For 8-bit images, the 256-colour palette can be found behind the image data. It is a [[VGA Palette#The_.22Modern.22_format|standard VGA palette in 8-bit RGB format]]. A single signature byte of &amp;lt;tt&amp;gt;0C&amp;lt;/tt&amp;gt; is included before the palette data begins.&lt;br /&gt;
&lt;br /&gt;
Some PCX readers simplify the start offset of the 256-colour palette to &amp;lt;code&amp;gt;EndOfFile - 768&amp;lt;/code&amp;gt;, though technically, the correct way to find it is to take the offset you end up at after decompressing the image, checking for the &amp;lt;tt&amp;gt;0C&amp;lt;/tt&amp;gt; byte after that, and then reading the 768-byte palette.&lt;br /&gt;
&lt;br /&gt;
== Source Code ==&lt;br /&gt;
&lt;br /&gt;
=== ASM ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;asm&amp;quot;&amp;gt;&lt;br /&gt;
EXTERN  kbdin, dosxit           ; LIB291 functions&lt;br /&gt;
&lt;br /&gt;
SEGMENT ScratchSeg&lt;br /&gt;
ScratchPad      resb 65535&lt;br /&gt;
&lt;br /&gt;
SEGMENT stkseg STACK&lt;br /&gt;
        resb    64*8&lt;br /&gt;
stacktop:&lt;br /&gt;
        resb    0&lt;br /&gt;
&lt;br /&gt;
SEGMENT code&lt;br /&gt;
&lt;br /&gt;
PCX1    db      &#039;my_pcx1.pcx&#039;, 0        ; Filenames&lt;br /&gt;
PCX2    db      &#039;my_pcx2.pcx&#039;, 0        ; (Must end with 0 byte)&lt;br /&gt;
&lt;br /&gt;
..start:&lt;br /&gt;
        mov     ax, cs          ; Set up data and stack segments&lt;br /&gt;
        mov     ds, ax&lt;br /&gt;
        mov     ax, stkseg&lt;br /&gt;
        mov     ss, ax&lt;br /&gt;
        mov     sp, stacktop&lt;br /&gt;
&lt;br /&gt;
MAIN:&lt;br /&gt;
        ; Sets up mode 13h and clears screen&lt;br /&gt;
        mov     ax, 0013h&lt;br /&gt;
        int     10h&lt;br /&gt;
&lt;br /&gt;
        mov     dx, pcx1        ; Filename to display&lt;br /&gt;
        call    ShowPCX         ; Display PCX file to screen&lt;br /&gt;
&lt;br /&gt;
        ; Wait for keypress&lt;br /&gt;
        call    kbdin&lt;br /&gt;
&lt;br /&gt;
        ; Go back to text mode&lt;br /&gt;
        mov     ax, 0003h&lt;br /&gt;
        int     10h&lt;br /&gt;
&lt;br /&gt;
        ; Return to DOS&lt;br /&gt;
        call    dosxit&lt;br /&gt;
&lt;br /&gt;
;-----------------------------------------------------------------------------&lt;br /&gt;
; ShowPCX procedure by Brandon Long,&lt;br /&gt;
;   modified by Eric Meidel and Nathan Jachimiec,&lt;br /&gt;
;   converted to NASM, cleaned up, and better commented by Peter Johnson&lt;br /&gt;
; Inputs: DX has the offset of PCX filename to show.&lt;br /&gt;
; Output: PCX file displayed (all registers unchanged)&lt;br /&gt;
; Notes:  Assumes PCX file is 320x200x256.&lt;br /&gt;
;         Uses ScratchSeg for temporary storage.&lt;br /&gt;
;         The PCX file must be in the same directory as this executable.&lt;br /&gt;
;-----------------------------------------------------------------------------&lt;br /&gt;
ShowPCX&lt;br /&gt;
        push    ax              ; Save registers&lt;br /&gt;
        push    bx&lt;br /&gt;
        push    cx&lt;br /&gt;
        push    si&lt;br /&gt;
        push    di&lt;br /&gt;
        push    es&lt;br /&gt;
&lt;br /&gt;
        mov     ax, 3D00h&lt;br /&gt;
        int     21h             ; Open file&lt;br /&gt;
        jc      .error          ; Exit if open failed&lt;br /&gt;
&lt;br /&gt;
        mov     bx, ax          ; File handle&lt;br /&gt;
        mov     cx, 65535       ; Number of bytes to read&lt;br /&gt;
        mov     ax, ScratchSeg  ; DS:DX -&amp;gt; buffer for data&lt;br /&gt;
        mov     ds, ax&lt;br /&gt;
        mov     dx, ScratchPad&lt;br /&gt;
        mov     si, dx&lt;br /&gt;
        mov     ah, 3Fh&lt;br /&gt;
        int     21h             ; Read from file&lt;br /&gt;
&lt;br /&gt;
        mov     ax, 0A000h      ; Start writing to upper-left corner&lt;br /&gt;
        mov     es, ax          ; of graphics display&lt;br /&gt;
        xor     di, di&lt;br /&gt;
&lt;br /&gt;
        add     si, 128         ; Skip header information&lt;br /&gt;
&lt;br /&gt;
        xor     ch, ch          ; Clear high part of CX for string copies&lt;br /&gt;
&lt;br /&gt;
.nextbyte:&lt;br /&gt;
        mov     cl, [si]        ; Get next byte&lt;br /&gt;
        cmp     cl, 0C0h        ; Is it a length byte?&lt;br /&gt;
        jb      .normal         ;  No, just copy it&lt;br /&gt;
        and     cl, 3Fh         ; Strip upper two bits from length byte&lt;br /&gt;
        inc     si              ; Advance to next byte - color byte&lt;br /&gt;
        lodsb                   ; Get color byte into AL from [SI]&lt;br /&gt;
        rep stosb               ; Store to [ES:DI] and inc DI, CX times&lt;br /&gt;
        jmp     short .tst&lt;br /&gt;
&lt;br /&gt;
.normal:&lt;br /&gt;
        movsb                   ; Copy color value from [SI] to [ES:DI]&lt;br /&gt;
&lt;br /&gt;
.tst:&lt;br /&gt;
        cmp     di, 320*200     ; End of file? (written 320x200 bytes)&lt;br /&gt;
        jb      .nextbyte&lt;br /&gt;
&lt;br /&gt;
        mov     cl, [si]&lt;br /&gt;
        cmp     cl, 0Ch         ; Palette available?&lt;br /&gt;
        jne     .close&lt;br /&gt;
&lt;br /&gt;
        ; Set palette using port I/O&lt;br /&gt;
        mov     dx, 3C8h&lt;br /&gt;
        mov     al, 0&lt;br /&gt;
        out     dx, al&lt;br /&gt;
        inc     dx              ; Port 3C9h&lt;br /&gt;
        mov     cx, 256*3       ; Copy 256 entries, 3 bytes (RGB) apiece&lt;br /&gt;
        inc     si              ; Skip past padding byte&lt;br /&gt;
&lt;br /&gt;
.palette:&lt;br /&gt;
        lodsb&lt;br /&gt;
        shr     al, 1           ; PCX stores color values as 0-255&lt;br /&gt;
        shr     al, 1           ;  but VGA DAC is only 0-63&lt;br /&gt;
        out     dx, al&lt;br /&gt;
        dec     cx&lt;br /&gt;
        jnz     .palette&lt;br /&gt;
&lt;br /&gt;
.close:&lt;br /&gt;
        mov     ah, 3Eh&lt;br /&gt;
        int     21h             ; Close file&lt;br /&gt;
&lt;br /&gt;
.error:&lt;br /&gt;
        pop     es              ; Restore registers&lt;br /&gt;
        pop     di&lt;br /&gt;
        pop     si&lt;br /&gt;
        pop     cx&lt;br /&gt;
        pop     bx&lt;br /&gt;
        pop     ax&lt;br /&gt;
        ret&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Tools ==&lt;br /&gt;
&lt;br /&gt;
PCX files can be read, and occasionally converted by several programs, notably, the Microsoft Photo Editor included in Windows XP can do so.&lt;br /&gt;
&lt;br /&gt;
{{BeginFileFormatTools|Type=image}}&lt;br /&gt;
{{FileFormatTool&lt;br /&gt;
| Name = [http://www.xnview.com XnView]&lt;br /&gt;
| Platform = Windows/MacOSX/Linux&lt;br /&gt;
| canView = Yes&lt;br /&gt;
| canExport = Yes&lt;br /&gt;
| canImport = Yes&lt;br /&gt;
| editHidden = N/A&lt;br /&gt;
| editMetadata = N/A&lt;br /&gt;
| notes = Freeware for private non-commercial or educational use.&lt;br /&gt;
}}&lt;br /&gt;
{{FileFormatTool&lt;br /&gt;
| Name = [http://www.imagemagick.com ImageMagick]&lt;br /&gt;
| Platform = Cross-platform&lt;br /&gt;
| canView = Yes&lt;br /&gt;
| canExport = Yes&lt;br /&gt;
| canImport = Yes&lt;br /&gt;
| editHidden = N/A&lt;br /&gt;
| editMetadata = N/A&lt;br /&gt;
| notes = Is unable to correctly write 16-colour PCX files.&lt;br /&gt;
}}&lt;br /&gt;
{{FileFormatTool&lt;br /&gt;
| Name = [http://www.gimp.org GNU Image Manipulation Program]&lt;br /&gt;
| Platform = Cross-platform&lt;br /&gt;
| canView = Yes&lt;br /&gt;
| canExport = Yes&lt;br /&gt;
| canImport = Yes&lt;br /&gt;
| editHidden = N/A&lt;br /&gt;
| editMetadata = N/A&lt;br /&gt;
| notes = Does not handle CGA or default-palette EGA images correctly.&lt;br /&gt;
}}&lt;br /&gt;
{{FileFormatTool&lt;br /&gt;
| Name = [[Engie File Converter]]&lt;br /&gt;
| Platform = Windows&lt;br /&gt;
| canView = Yes&lt;br /&gt;
| canExport = Yes&lt;br /&gt;
| canImport = No&lt;br /&gt;
| editHidden = N/A&lt;br /&gt;
| editMetadata = N/A&lt;br /&gt;
| notes = Relies on common image dimensions for CGA detection. No save functionality so far.&lt;br /&gt;
}}&lt;br /&gt;
{{FileFormatTool&lt;br /&gt;
| Name = [https://github.com/libsdl-org/SDL_image SDL_image]&lt;br /&gt;
| Platform = Cross-platform&lt;br /&gt;
| canView = Yes&lt;br /&gt;
| canExport = No&lt;br /&gt;
| canImport = No&lt;br /&gt;
| editHidden = N/A&lt;br /&gt;
| editMetadata = N/A&lt;br /&gt;
| notes = &lt;br /&gt;
}}&lt;br /&gt;
{{EndFileFormatTools}}&lt;br /&gt;
&lt;br /&gt;
== Useful links ==&lt;br /&gt;
&lt;br /&gt;
* [http://bespin.org/~qz/pc-gpe/pcx.txt Official PCX documentation]&lt;br /&gt;
* [http://web.archive.org/web/20030111010058/http://www.nist.fss.ru/hr/doc/spec/pcx.htm Official PCX documentation in HTML (archived version)]&lt;br /&gt;
* [http://www.fileformat.info/format/pcx/egff.htm PCX at the File Format Encyclopaedia] - very detailed&lt;br /&gt;
* [http://fileformats.archiveteam.org/wiki/PCX PCX on the &#039;Just Solve the File Format Problem&#039; wiki] - includes useful sample files.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mra</name></author>
	</entry>
</feed>