home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / formats / pcx / spec / pcx.txt
Text File  |  1994-06-20  |  20KB  |  564 lines

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