THE PCX GRAPHIC FILE FORMAT By Mike Austin Last summer (1995), I had to choose a C++ programming project to complete my HNC. Being interested in graphics orientated programs and fed up of doing the usual boring database college type programs, I opted to do a conversion of my Amiga F1 Licenceware program `A Tour Through Time', minus any music, sfx or animations on the PC. However, I didn't know what I was letting myself in for as I didn't have a clue how to load and display bitmapped pictures on a PC. Anyway, after about three months of research, hair pulling and bad language I succeeded in completing the project and the purpose of this article is to share a small part of what I learned, which may be of interest to some of you. "What's this got to do with AMOS"? you might ask. Well, I'm a dedicated Amiga and AMOS user, and I realised after doing the PC project how much we are all spoiled by the power of AMOS and that often we forget what goes on behind the scenes. Because of this I decided it would be interesting to make life a bit harder and try to see if I could load a .PCX picture file from AMOS. There is no practical benefit to this because the PCX file is an old format and not used very much now and from AMOS we only need to code `Load Iff "Filename.Iff"' and the jobs done. Some of you might find it interesting to see what's involved in loading and decoding a picture the hard way. The AMOS source code I've provided is not perfect and can certainly be improved, I just put it together very quickly to test it out. Also the program will only load a sixteen colour 640*200 resolution picture properly. I didn't bother with the 256 colour images. Also, the program crashes if you try to load a low resolution picture. This is due to something stupid I've overlooked. As mentioned, this is not meant to be a practical program, but more of interest. Alter and improve it if you wish. I've supplied some demo pictures to load which I took directly from the PC version. The rest of this article is mainly about the PCX file format. Hope you enjoy it. GRAPHICS FILE FORMATS Graphics file formats fall into one of three classes: Raster, Vector and Metafiles. Despite the fact that there are three classes of file formats, there are only two ways to create a graphics image on a computer: Raster images involve the description of the colour of each possible dot on a given image. This can be compared to the pointillist technique of the impressionist painters. Here, images are represented (coded) as a large canvas (or monitor screen) full of dots. From the painter's point of view, this involved putting dots of unmixed colour side by side so that from a distance they fuse together in the viewer's eye into appropriate intermediate tones i.e. the image that you visualize. In the computer world these images are known as Raster images. An example of a non-computer Raster image could be a picture in a newspaper. If the image is examined close-up the coloured dots making up the image can be seen. Raster images are good for scanned images, paint packages, photo- realistic images, random patterns and textures. File size corresponds directly to the number of dots and colours in the image. Modification of the image is based on dot colour and location. Vector files on the other hand, contain no information about specific dots. Instead, vector files are an assimilation of mathematically described or coded objects used to create an image. In order for an image stored in a vector file to be viewed, the objects in the list must be rendered by a program on an output device. Thus, vector images are not linked to any particular piece of hardware. It is up to the program to properly interpret the file and drive the hardware on which the image is to be displayed or printed. Vector files are good for drawing objects such as lines, circles, squares and text and so are useful to engineers and architects. The size of the image is easily changed to accomodate resolution of viewing device. File size is related to number of objects in the image rather than size of image. Modification is based on individual objects present in the image. Metafiles can accomodate both raster and vector information and so are really an amalgam of both, although software packages rarely write both to the same file. As a rule, metafiles contain vector information only e.g. those used by MicrSoft Windows (Yuck and double Yuck Yuck). FILE DATA COMPRESSION SCHEMES. Since graphics files, especially raster files, require more storage space than alphanumeric files, various techniques have been used to reduce their size. Such data compression techniques, normally applied to the image data portion of the raster file, range from simple Run Length Encoding to the complex dynamically indexed data style used by TIFF files. Regardless of their individual complexity, these techniques all work on the principle of representing repeating data strings with codes or tags that are either stored in a table or generated on the fly, and since this is the case, the degree of success achieved by a given compression technique depends greatly on the content of the particular image file. The more common data compression schemes are as follows :- RUN LENGTH ENCODING ( RLE ). This is the simplest form of data compression. RLE is a way to avoid repeating consecutive occurrences of identical data items. An example of this could be as follows :- If you have an image consisting of a blue rectangle, an uncompressed data file would contain a data item for whatever the code was for blue, for each pixel. For an E.G.A screen with a resolution of 640 pixels width by 350 pixels height, this would require 224,000 identical data entries. Using RLE this value could be dramatically reduced by using a code that would represent the colour blue for 224,000 instances. This would mean that the data portion would only consist of a tag to indicate that RLE was being used, the code for the colour blue and the number 224,000 to indicate the number of consecutive instances. RLE works equally well with patterns as well as blocks of colours. VERTICAL REPLICATION. This technique is used by some file formats, particularly WordPerfect with the file extension of .WPG and GEM files with the extension of .IMG, to avoid having to describe identical horizontal rows more than once. It is similar to RLE in that the data item (one horizontal row of the image) is described once and then iterated a specific number of times. KEYED COMPRESSION TECHNIQUES. There are variations on the RLE technique that use sets of tags or tables for predefined patterns to achieve data compression. Most notable in the graphics area is the Huffman encoding scheme used by the CITT Group 3 FAX format. THE PCX RASTER IMAGE FILE FORMAT. The ZSoft .PCX raster image file format uses the Run Length Encoding technique to compress the image data portion of the file. PCX is one of the oldest and most common raster formats available on P.C.'s. It is simple to read and write, compared to other more complex file formats, and most graphics and desktop publishing programs that import raster graphics support the .PCX format. ZSoft are of course the publishers of PC Paintbrush, a raster graphics paint program. PCX file formats are also recognized by the Deluxe Paint program V2 (c) Electronic Arts on the PC ( Yuck). Since it has been in existence for such a long time, there have been many variations on the .PCX format. The most commonly used that I know of are listed below:- Version 2.5 This version does not support a flexible palette. The standard E.G.A. palette is used. Version 2.8 This version supports a modifiable palette for E.G.A. and V.G.A. sixteen colour files. Version 3.0 This version includes support for images containing 256 colours. The palette describing the RGB (Red, Green and Blue) content of those 256 colours is appended to the end of the bitmap data. The following table describes, byte by byte, the contents of a graphics file with the .PCX extension. The first 128 bytes are the Header and the bytes following are the data information using RLE. In the case of a 256 colour image the palette information is then appended to this. THE PCX FILE --------------------------------------------------------------- BYTE NUMBER DESCRIPTION NOTES AND VALUES --------------------------------------------------------------- 0 Manufacturer I.D. Always set to 0AHex --------------------------------------------------------------- 1 Version Number 0=Version 2.5 1=Version 2.8 3=Version 2.8 w/o palette 5=Version 3.0 --------------------------------------------------------------- 2 Encoding Technique Should always be set to 1. Simple RLE. All other values are non-standard. --------------------------------------------------------------- 3 Bits/Plane/Pixel Used with Byte 65 ( No. of planes). Valid entries are 1, 2 or 8. --------------------------------------------------------------- 4 - 5 Window-X Minimum 2 byte co-ordinate value --------------------------------------------------------------- 6 - 7 Window-Y Minimum 2 byte co-ordinate value --------------------------------------------------------------- 8 - 9 Window-X Maximum 2 byte co-ordinate value --------------------------------------------------------------- 10 - 11 Window-Y Maximum 2 byte co-ordinate value --------------------------------------------------------------- 12 - 13 Horizontal Resolution Measured in DPI --------------------------------------------------------------- 14 - 15 Vertical Resolution Measured in DPI --------------------------------------------------------------- 16 - 63 Colour Map Colours specified in RGB triplets with 1 byte per R.G.B. --------------------------------------------------------------- 64 Reserved Always set to Zero --------------------------------------------------------------- 65 No of Bit Planes Ties in with byte 3 --------------------------------------------------------------- 66 - 67 Bytes/Scanline/Plane Also known as image width. This value must be aligned on even byte boundaries. --------------------------------------------------------------- 68 - 69 Palette Information 1 = Colour or B&W 2 = Grey Scale --------------------------------------------------------------- 70 - 127 58 bytes of padding Used in storing info about fractal images only. In general filled with 0's. --------------------------------------------------------------- 128 Beginning of data area PCX compression map. --------------------------------------------------------------- If a 256 colour image, then C0Hex is written to byte 129 and the palette information is appended to the end of the file for the next 768 bytes. --------------------------------------------------------------- The .PCX file format table shown above starts with a 128 byte header. Except for the byte's 16 - 63 which contain the Colour Map Data, most of the contents are self-evident. The 58 bytes from byte 70 - 127 are not part of the original ZSoft format. They are used for storing information concerning fractal images such as Mandelbrot sets and are usually filled with zeroes. COLOR MAP The colour map is created by reading the palette register and then the colour register pointed to by the palette register. The red, green and blue values are then multiplied by four and the results are stored in the triple associated with that palette. To read and decode the colour map is essentially a reversal of this procedure, the difference being, to divide by four instead of multiplying. Contents of a .PCX File Colour Map. --------------------------------------------------------- BYTE PALETTE(INDEX) COLOUR DESCRIPTION ---------------------------------------- 16 0 Red 17 0 Green Value of each byte is 18 0 Blue the value of the ---------------------------------------- colour value from 19 1 Red the colour register 20 1 Green pointed to by the 21 1 Blue appropriate palette ---------------------------------------- register for each 22 2 Red colour of the triple 23 2 Green multiplied by four. 24 2 Blue ---------------------------------------- 25 3 Red 26 3 Green 27 3 Blue ---------------------------------------- 28 4 Red 29 4 Green 30 4 Blue ---------------------------------------- 31 5 Red -etc--------------------- Green 60 14 Blue ---------------------------------------- 61 15 Red 62 15 Green 63 15 Blue ---------------------------------------- The V.G.A. or AGA Amiga also has colour modes in which 256 different colours may be displayed simultaneously. This palette information is appended at the end of the .PCX file and to access it it is necessary to carry out the following steps: i. Ascertain that the version number data in the header, byte 1, is 5 which means version 3.0. ii. Read to the end of the file and then "seek" back 769 bytes. If the value in this byte position is 0CHex ( or 12 decimal ) then the following information is the 256-colour palette data. That's the end of this short article. If anyone wants to comment or wants any info regarding loading pics on a PC (why?), contact me at the address below. By the way, you may have noticed from reading the article and viewing my code, that I have not read the PCX file exactly as you may have thought in certain parts. It still works as I've done it though. Can anyone spot what I'm talking about? Also, you need the Craft extension to run the code as is unless you can figure how to load the Hex values of variables RED, GREEN and BLUE into the AMOS's Colour Function i.e Colour index,$RED GREEN BLUE. I've tried Hex$(), Instr(), Anding the values etc but I can't figure it out. Let me know if you do. By the way, there's some nice demo code supplied on AMOSZine #10 with the Intuition Extension demo on Loading an IFF file, check it out. Mike Austin, 10 Birch Grove, Parc Ave, Caerphilly, Mid Glam, CF83 3BG.