Changes between Initial Version and Version 1 of Develop/FileFormats/PCX


Ignore:
Timestamp:
Jan 19, 2018, 10:42:34 AM (6 years ago)
Author:
Thomas Bernard
Comment:

Soft PCX File Format Technical Reference Manual

Legend:

Unmodified
Added
Removed
Modified
  • Develop/FileFormats/PCX

    v1 v1  
     1PCX is the format used by ZSoft PC Paintbrush.
     2Below is the Reference manual from ZSoft :
     3{{{
     4Soft PCX File Format Technical Reference Manual       
     5       
     6       
     7       
     8Introduction                            2
     9Image  File (.PCX) Format               3
     10ZSoft .PCX FILE HEADER FORMAT           4
     11Decoding .PCX Files                     6
     12Palette Information Description         7
     13EGA/VGA 16 Color Palette Information    7
     14VGA 256 Color Palette Information       7
     1524-Bit .PCX Files                       8
     16CGA Color Palette Information           8
     17CGA Color Map                           8
     18PC Paintbrush Bitmap Character Format   9
     19Sample "C" Routines                    10
     20FRIEZE Technical Information           14
     21General FRIEZE Information             14
     227.00 and Later FRIEZE                  14
     23FRIEZE Function Calls                  15
     24FRIEZE Error Codes                     18
     25
     26
     27
     28
     29
     30Introduction
     31
     32This booklet was designed to aid developers and users in understanding
     33the technical aspects of the .PCX file format and the use of FRIEZE.
     34Any comments, questions or suggestions about this booklet should be
     35sent to:
     36
     37        ZSoft Corporation
     38        Technical Services
     39        ATTN: Code Librarian
     40        450 Franklin Rd. Suite 100
     41        Marietta, GA  30067
     42
     43
     44
     45Technical Reference Manual information compiled by:
     46Dean Ansley
     47
     48
     49Revision 5
     50
     51To down load additional information and the source for a complete
     52Turbo Pascal program to show .PCX files on a CGA/EGA/VGA graphics
     53display, call our BBS at (404)427-1045.  You may use a 9600 baud
     54modem or a 2400 baud standard modem.  Your modem should be set for
     558 data bits, 1 stop bit, and NO parity.
     56
     57Image  File (.PCX) Format
     58
     59If you have technical questions on the format, please do not call
     60technical support.  ZSoft provides this document as a courtesy to
     61its users and developers.  It is not the function of Technical Support
     62to provide programming assistance.  If something is not clear, leave a
     63message on our BBS, Compuserve, or write us a letter at the above address.
     64
     65The information in this section will be useful if you want to write a
     66program to read or write PCX files (images).  If you want to write a
     67special case program for one particular image format you should be able
     68to produce something that runs twice as fast as "Load from..." in
     69PC Paintbrush. 
     70
     71Image files used by PC Paintbrush product family and FRIEZE (those with a
     72.PCX extension) begin with a 128 byte header.  Usually you can ignore this
     73header, since your images will probably all have the same resolution.  If
     74you want to process different resolutions or colors, you will need to
     75interpret the header correctly.  The remainder of the image file consists
     76of encoded graphic data.  The encoding method is a simple byte oriented
     77run-length technique.  We reserve the right to change this method to
     78improve space efficiency.  When more than one color plane is stored in
     79the file, each line of the image is stored by color plane (generally ordered
     80red, green, blue, intensity), As shown below.
     81
     82Scan line 0:         RRR...        (Plane 0)
     83                     GGG...        (Plane 1)
     84                     BBB...        (Plane 2)
     85                     III...        (Plane 3)
     86Scan line 1:         RRR...
     87                     GGG...
     88                     BBB...
     89                     III...        (etc.)
     90
     91The encoding method is:
     92    FOR  each  byte,  X,  read from the file
     93        IF the top two bits of X are  1's then
     94            count = 6 lowest bits of X
     95            data = next byte following X
     96        ELSE
     97            count = 1
     98            data = X
     99
     100Since the overhead this technique requires is, on average,  25% of
     101the non-repeating data and is at least offset whenever bytes are repeated,
     102the file storage savings are usually considerable.
     103
     104ZSoft .PCX FILE HEADER FORMAT
     105
     106Byte      Item          Size   Description/Comments
     107 0         Manufacturer 1      Constant Flag, 10 = ZSoft .pcx
     108 1         Version      1      Version information
     109                               0 = Version 2.5 of PC Paintbrush
     110                               2 = Version 2.8 w/palette information
     111                               3 = Version 2.8 w/o palette information
     112                               4 = PC Paintbrush for Windows(Plus for
     113                                  Windows uses Ver 5)
     114                               5 = Version 3.0 and > of PC Paintbrush
     115                                  and PC Paintbrush +, includes
     116                                  Publisher's Paintbrush . Includes
     117                                  24-bit .PCX files
     118 2         Encoding      1     1 = .PCX run length encoding
     119 3         BitsPerPixel  1     Number of bits to represent a pixel
     120                                  (per Plane) - 1, 2, 4, or 8
     121 4         Window        8     Image Dimensions: Xmin,Ymin,Xmax,Ymax
     12212         HDpi          2     Horizontal Resolution of image in DPI*
     12314         VDpi          2     Vertical Resolution of image in DPI*
     12416         Colormap     48     Color palette setting, see text
     12564         Reserved      1     Should be set to 0.
     12665         NPlanes       1     Number of color planes
     12766         BytesPerLine  2     Number of bytes to allocate for a scanline
     128                                  plane.  MUST be an EVEN number.  Do NOT
     129                                  calculate from Xmax-Xmin.
     13068         PaletteInfo   2     How to interpret palette- 1 = Color/BW,
     131                                  2 = Grayscale (ignored in PB IV/ IV +)
     13270         HscreenSize   2     Horizontal screen size in pixels. New field
     133                                  found only in PB IV/IV Plus
     13472         VscreenSize   2     Vertical screen size in pixels. New field
     135                                  found only in PB IV/IV Plus
     13674         Filler       54     Blank to fill out 128 byte header.  Set all
     137                                  bytes to 0
     138
     139NOTES:
     140
     141All sizes are measured in BYTES.
     142
     143All variables of SIZE 2 are integers. 
     144
     145*HDpi and VDpi represent the Horizontal and Vertical resolutions which the
     146image was created (either printer or scanner); i.e. an image which was
     147scanned might have 300 and 300 in each of these fields.
     148
     149Decoding .PCX Files
     150
     151First, find the pixel dimensions of the image by calculating
     152[XSIZE = Xmax - Xmin + 1] and [YSIZE = Ymax - Ymin + 1].  Then calculate
     153how many bytes are required to hold one complete uncompressed scan line:
     154
     155TotalBytes = NPlanes * BytesPerLine
     156
     157Note that since there are always an even number of bytes per scan line,
     158there will probably be unused data at the end of each scan line.  TotalBytes
     159shows how much storage must be available to decode each scan line, including
     160any blank area on the right side of the image.  You can now begin decoding
     161the first scan line - read the first byte of data from the file.  If the
     162top two bits are set, the remaining six bits in the byte show how many times
     163to duplicate the next byte in the file.  If the top two bits are not set,
     164the first byte is the data itself, with a count of one.
     165
     166Continue decoding the rest of the line.  Keep a running subtotal of how
     167many bytes are moved and duplicated into the output buffer.  When the
     168subtotal equals TotalBytes, the scan line is complete.  There should always
     169be a decoding break at the end of each scan line.  But there will not be a
     170decoding break at the end of each plane within each scan line.  When the
     171scan line is completed, there may be extra blank data at the end of each
     172plane within the scan line.  Use the XSIZE and YSIZE values to find where
     173the valid image data is.  If the data is multi-plane, BytesPerLine shows
     174where each plane ends within the scan line.
     175
     176Continue decoding the remainder of the scan lines (do not just read to
     177end-of-file).  There may be additional data after the end of the image
     178(palette, etc.)
     179
     180Palette Information Description
     181
     182EGA/VGA 16 Color Palette Information
     183
     184In standard RGB format (IBM EGA, IBM VGA) the data is stored as 16 triples.
     185Each triple is a 3 byte quantity of Red, Green, Blue values.  The values can
     186range from 0-255, so some interpretation may be necessary.  On an IBM EGA,
     187for example, there are 4 possible levels of RGB for each color.  Since
     188256/4 = 64, the following is a list of the settings and levels:
     189
     190Setting                Level
     191   0-63                0
     192 64-127                1
     193128-192                2
     194193-254                3
     195
     196VGA 256 Color Palette Information
     197
     198ZSoft has recently added the capability to store palettes containing more
     199than 16 colors in the .PCX image file.  The 256 color palette is formatted
     200and treated the same as the 16 color palette, except that it is substantially
     201longer.  The palette (number of colors x 3 bytes in length) is appended to
     202the end of the .PCX file, and is preceded by a 12 decimal.  Since the VGA
     203device expects a palette value to be 0-63 instead of 0-255, you need to
     204divide the values read in the palette by 4.
     205
     206To access a 256 color palette:
     207
     208First, check the version number in the header; if it contains a 5 there is
     209a palette.
     210
     211Second, read to the end of the file and count back 769 bytes.  The value
     212you find should be a 12 decimal, showing the presence of a 256 color palette.
     213
     21424-Bit .PCX Files
     215
     21624 bit images are stored as version 5 or above as 8 bit, 3 plane images.
     217
     21824 bit images do not contain a palette.
     219
     220Bit planes are ordered as lines of red, green, blue in that order.
     221
     222CGA Color Palette Information
     223
     224NOTE: This is no longer supported for PC Paintbrush IV/IV Plus.
     225
     226For a standard IBM CGA board, the palette settings are a bit more complex.
     227Only the first byte of the triple is used.  The first triple has a valid
     228first byte which represents the background color.  To find the background,
     229take the (unsigned) byte value and divide by 16.  This will give a result
     230between 0-15, hence the background color.  The second triple has a valid
     231first byte, which represents the foreground palette.  PC Paintbrush supports
     2328 possible CGA palettes, so when the foreground setting is encoded between
     2330 and 255, there are 8 ranges of numbers and the divisor is 32.
     234
     235CGA Color Map
     236
     237Header Byte #16
     238
     239Background color is determined in the upper four bits.
     240
     241Header Byte #19
     242
     243Only upper 3 bits are used, lower 5 bits are ignored.  The first three bits
     244that are used are ordered C, P, I.  These bits are interpreted as follows:
     245
     246c: color burst enable - 0 = color; 1 = monochrome
     247
     248p: palette - 0 = yellow; 1 = white
     249
     250i: intensity - 0 = dim; 1 = bright
     251
     252PC Paintbrush Bitmap Character Format
     253
     254NOTE: This format is for PC Paintbrush (up to Vers 3.7) and PC Paintbrush
     255Plus (up to Vers 1.65)
     256
     257The bitmap character fonts are stored in a particularly simple format.  The
     258format of these characters is as follows:
     259
     260
     261Header
     262
     263font width         byte                0xA0 + character width  (in pixels)
     264font height        byte                character height  (in pixels)
     265
     266Character Width Table
     267
     268char widths        (256 bytes)         each char's width + 1 pixel of kerning
     269
     270Character Images
     271
     272(remainder of the file)                starts at char 0  (Null)
     273
     274The characters are stored in ASCII order and as many as 256 may be provided.
     275Each character is left justified in the character block, all characters take
     276up the same number of bytes.
     277
     278Bytes are organized as N strings, where each string is one scan line of the
     279character.
     280
     281For example, each character in a 5x7 font requires 7 bytes.  A 9x14 font
     282uses 28 bytes per character (stored two bytes per scan line in 14 sets of
     2832 byte packets).  Custom fonts may be any size up to the current maximum of
     28410K bytes allowed for a font file.  There is a maximum of 4 bytes per scan
     285line.
     286
     287Sample "C" Routines
     288
     289The following is a simple set of C subroutines to read data from a .PCX file.
     290
     291/* This procedure reads one encoded block from the image file and stores a
     292count and data byte.
     293
     294Return result:  0 = valid data stored, EOF = out of data in file */
     295
     296encget(pbyt, pcnt, fid)
     297int *pbyt;        /* where to place data */
     298int *pcnt;        /* where to place count */
     299FILE *fid;        /* image file handle */
     300{
     301int i;
     302        *pcnt = 1;        /* assume a "run" length of one */
     303        if (EOF == (i = getc(fid)))
     304                return (EOF);
     305        if (0xC0 == (0xC0 & i))
     306                {
     307                *pcnt = 0x3F & i;
     308                if (EOF == (i = getc(fid)))
     309                        return (EOF);
     310                }
     311        *pbyt = i;
     312        return (0);
     313}
     314/* Here's a program fragment using encget.  This reads an entire file and
     315stores it in a (large) buffer, pointed to by the variable "bufr". "fp" is
     316the file pointer for the image */
     317
     318int i;
     319long l, lsize;
     320     lsize = (long )hdr.BytesPerLine * hdr.Nplanes * (1 + hdr.Ymax - hdr.Ymin);
     321     for (l = 0; l < lsize; )             /* increment by cnt below */
     322                {
     323                if (EOF == encget(&chr, &cnt, fp))
     324                        break;
     325                for (i = 0; i < cnt; i++)
     326                        *bufr++ = chr;
     327                l += cnt;
     328                }
     329
     330The following is a set of C subroutines to write data to a .PCX file.
     331
     332/* Subroutine for writing an encoded byte pair (or single byte if it
     333doesn't encode) to a file. It returns the count of bytes written, 0 if error */
     334
     335encput(byt, cnt, fid)
     336unsigned char byt, cnt;
     337FILE *fid;
     338{
     339  if (cnt) {
     340        if ((cnt == 1) && (0xC0 != (0xC0 & byt)))
     341                {
     342                if (EOF == putc((int )byt, fid))
     343                        return(0);     /* disk write error (probably full) */
     344                return(1);
     345                }
     346        else
     347                {
     348                if (EOF == putc((int )0xC0 | cnt, fid))
     349                        return (0);      /* disk write error */
     350                if (EOF == putc((int )byt, fid))
     351                        return (0);      /* disk write error */
     352                return (2);
     353                }
     354        }
     355   return (0);
     356}
     357
     358/* This subroutine encodes one scanline and writes it to a file.
     359It returns number of bytes written into outBuff, 0 if failed. */
     360
     361encLine(inBuff, inLen, fp)
     362unsigned char *inBuff;    /* pointer to scanline data */
     363int inLen;                        /* length of raw scanline in bytes */
     364FILE *fp;                        /* file to be written to */
     365{
     366unsigned char this, last;
     367int srcIndex, i;
     368register int total;
     369register unsigned char runCount;     /* max single runlength is 63 */
     370  total = 0;
     371  runCount = 1;
     372  last = *(inBuff);
     373
     374/* Find the pixel dimensions of the image by calculating
     375[XSIZE = Xmax - Xmin + 1] and [YSIZE = Ymax - Ymin + 1]. 
     376Then calculate how many bytes are in a "run" */
     377
     378  for (srcIndex = 1; srcIndex < inLen; srcIndex++)
     379        {
     380        this = *(++inBuff);
     381        if (this == last)     /* There is a "run" in the data, encode it */
     382                {
     383                runCount++;
     384                if (runCount == 63)
     385                        {
     386                        if (! (i = encput(last, runCount, fp)))
     387                                return (0);
     388                        total += i;
     389                        runCount = 0;
     390                        }
     391                }
     392        else                /* No "run"  -  this != last */
     393                {
     394                if (runCount)
     395                        {
     396                        if (! (i = encput(last, runCount, fp)))
     397                                return(0);
     398                        total += i;
     399                        }
     400                last = this;
     401                runCount = 1;
     402                }
     403        }        /* endloop */
     404  if (runCount)        /* finish up */
     405        {
     406        if (! (i = encput(last, runCount, fp)))
     407                return (0);
     408        return (total + i);
     409        }
     410  return (total);
     411}
     412
     413FRIEZE Technical Information
     414
     415General FRIEZE Information
     416
     417FRIEZE is a memory-resident utility that allows you to capture and save
     418graphic images from other programs.  You can then bring these images into
     419PC Paintbrush for editing and enhancement.
     420
     421FRIEZE 7.10 and later can be removed from memory (this can return you up
     422to 90K of DOS RAM, depending on your configuration). To remove FRIEZE from
     423memory, change directories to your paintbrush directory and type the word
     424"FRIEZE".
     425
     4267.00 and Later FRIEZE
     427
     428The FRIEZE command line format is:
     429
     430FRIEZE {PD} {Xn[aarr]} {flags} {video} {hres} {vres} {vnum}
     431Where:
     432{PD}        Printer driver filename (without the .PDV extension)
     433{Xn[aarr]}
     434                X=S for Serial Printer, P for Parallel Printer, D for disk file.
     435                        (file is always named FRIEZE.PRN)
     436                n = port number
     437                aa = Two digit hex code for which return bits cause
     438                         an abort (optional)
     439                rr = Two digit hex code for which return bits cause
     440                        a retry (optional)
     441                NOTE:  These codes represent return values from serial or
     442                       parallel port  BIOS calls.  For values see and IBM
     443                       BIOS reference (such as Ray Duncan's Advanced MS-DOS
     444                       Programming).
     445{flags}Four digit hex code
     446        First Digit controls Length Flag
     447        Second Digit controls Width Flag
     448                Third Digit controls Mode Flag
     449                Fourth Digit controls BIOS Flag
     450                        0 - None
     451                        1 - Dual Monitor Present
     452                        2 - Use internal (true) B/W palette for dithering
     453                                2 color images
     454                        4 - Capture palette along with screen IN VGA ONLY
     455                                Frieze 8.08 & up ONLY)
     456
     457NOTE: The length, width and mode flags are printer driver specific.
     458See PRINTERS.DAT on disk 1 (or Setup Disk) for correct use.  In general
     459width flag of 1 means wide carriage, and 0 means standard width.  Length
     460flag of 0 and mode flag of 0 means use default printer driver settings.
     461
     462If you need to use more than one BIOS flag option, add the needed flag values
     463and use the sum as the flag value.
     464
     465{video}       Video driver combination, where the leading digit signifies the
     466                high level video driver and the rest signifies the low
     467                level video driver
     468                Example = 1EGA - uses DRIVE1 and EGA.DEV
     469{hres}        Horizontal resolution of the desired graphics mode
     470{vres}        Vertical resolution of the desired graphics mode
     471{vnum}        Hardware specific parameter (usually number of color planes)
     472
     473Note: The last four parameters can be obtained from the CARDS.DAT file,
     474in your PC Paintbrush product directory.
     475
     476
     477FRIEZE Function Calls
     478
     479FRIEZE is operated using software interrupt number 10h (the video interrupt
     480call).
     481
     482To make a FRIEZE function call, load 75 (decimal) into the  AH register and
     483the function number into the CL register, then either load AL with the
     484function argument or load ES and BX with a segment and offset which point
     485to the function argument. Do an int 10h. FRIEZE will return a result code
     486number in AX.  All other registers are preserved.  In general, a result
     487code of 0 means success and other values indicate errors.  However, function
     48820 (get Frieze Version) behaves differently; see below.
     489No.      Definition         Arguments
     4900        Reserved
     4911        Load Window
     492                            ES:BX - string  (filename to read from)
     4932        Save Window
     494                            ES:BX - string  (filename to write to)
     4953        Reserved
     4964        Reserved       
     4976        Reserved       
     4987        Set Window Size
     499                            ES:BX - 4 element word vector of window settings:
     500                            Xmin, Ymin, Xmax, Ymax
     5018        Reserved
     5029        Set Patterns       
     503                            ES:BX - 16 element vector of byte values
     504                            containing the screen-to-printer color                                          correspondence
     50510        Get Patterns
     506                            ES:BX - room for 16 bytes as above
     50711        Set Mode
     50812,13,14  Reserved
     50915        Get Window
     510                            ES:BX - room for 4 words of the current window
     511                            settings
     51216         Set Print Options
     513                            ES:BX - character string of printer options.
     514                            Same format as for the FRIEZE command.
     51517, 18, 19        Reserved
     51620        Get FRIEZE Version.
     517                            AH gets the whole number portion and AL gets the
     518                            decimal portion of the version number.  (eg. for
     519                            Freize vesion 7.41, AH will contain 7 and AL will
     520                            contain 41.  If AH =0, you are calling a pre-7.0
     521                            version of FRIEZE).
     52221        Set Parameters
     523                            ES:BX points to an 8 word table  (16 bytes) of
     524                            parameter settings: TopMargin, LeftMargin,
     525                            HSize,VSize, Quality/Draft Mode, PrintHres,
     526                            PrintVres, Reserved.
     527                            Margins and sizes are specified in hundredths
     528                            of inches.
     529                                    Q/D mode parameter values:
     530                                    0 - draft print mode
     531                                    1 - quality print mode
     532                                    Print resolutions are specified in DPI.
     533                            Any parameter which should be left unchanged may
     534                            be filled with a (-1) (0FFFF hex).  The reserved
     535                            settings should be filled with a (-1).
     53622        Get Parameters
     537                            ES:BX points to an 8 word table (16 bytes) where
     538                            parameter settings are held.
     53923        Get Printer Res
     540                            ES:BX points to a 12 word table (24 bytes) that
     541                            holds six printer resolution pairs.
     54224        Reserved (versions 8.00 & up)
     543
     544FRIEZE Error Codes
     545
     546When FRIEZE is called using interrupt 10 hex, it will return an error code
     547in the AX register.  A value of zero shows that there was no error.  A
     548nonzero result means there was an error.  These error codes are explained
     549below.
     550
     551 0        No Error
     552 1        Printout was stopped by user with the ESC key
     553 2        Reserved
     554 3        File read error
     555 4        File write error
     556 5        File not found
     557 6        Invalid Header - not an image, wrong screen mode
     558 7        File close error
     559 8        Disk error - usually drive door open
     560 9        Printer error - printer is off or out of paper
     56110        Invalid command - CL was set to call a nonexistent  FRIEZE function
     56211        Can't create file - write protect tab or disk is full
     56312        Wrong video mode - FRIEZE cannot capture text screens.
     564
     565Technical Reference Manual
     566
     567Including information for:
     568Publisher's Paintbrushr
     569PC Paintbrush IVTM
     570PC Paintbrush IV PlusTM
     571PC Paintbrush PlusTM
     572PC Paintbrushr
     573FRIEZETM Graphics
     574PaintbrushTM
     575Revision 5
     576
     577ZSoft Corporation
     578450 Franklin Rd. Suite 100
     579Marietta, GA  30067
     580(404) 428-0008
     581(404) 427-1150 Fax
     582(404) 427-1045 BBS
     583
     584Copyright c 1985, 1987, 1988, 1990, 1991, ZSoft Corporation
     585All Rights Reserved
     586
     587
     588
     589}}}