home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / GSPMSRC / SRC / GDEVTIFF.C < prev    next >
C/C++ Source or Header  |  1993-12-16  |  27KB  |  770 lines

  1. /* $Header: /usr/people/sam/fax/gs/RCS/gdevtiff.c,v 1.3 93/03/20 11:44:02 sam Exp $ */
  2.  
  3. /*
  4.  * Copyright (c) 1992, 1993 Sam Leffler
  5.  * Copyright (c) 1992, 1993 Silicon Graphics, Inc.
  6.  *
  7.  * Permission to use, copy, modify, distribute, and sell this software and 
  8.  * its documentation for any purpose is hereby granted without fee, provided
  9.  * that (i) the above copyright notices and this permission notice appear in
  10.  * all copies of the software and related documentation, and (ii) the names of
  11.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  12.  * publicity relating to the software without the specific, prior written
  13.  * permission of Sam Leffler and Silicon Graphics.
  14.  * 
  15.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  16.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  17.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  18.  * 
  19.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  20.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  21.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  23.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  24.  * OF THIS SOFTWARE.
  25.  */
  26. /*
  27.  * 5/19/93 modified by L. Peter Deutsch, Aladdin Enterprises,
  28.  *   for compatibility with Ghostscript 2.6.1.
  29.  */
  30. /* gdevtiff.c */
  31.  
  32. #include "gdevprn.h"
  33. #include "gdevdfg3.h"
  34. #include "gdevtiff.h"
  35.  
  36. #ifdef __PROTOTYPES__
  37. #    define PROTO_ARGS(X)    X
  38. #else
  39. #    define PROTO_ARGS(X)    ()
  40. #endif
  41.  
  42. /*
  43.  * TIFF fax output driver.
  44.  */
  45. typedef struct {
  46.     FILE*    fp;
  47.     long    prevdir;    /* file offset of previous directory offset */
  48.     long    diroff;        /* file offset of next write */
  49.     int        bigendian;    /* 1 if machine is big-endian, 0 otherwise */
  50.     unsigned long iwidth;    /* width of image data in pixels */
  51.     int        fax_byte;
  52.     int        fax_weight;
  53. } TIFFOUT;
  54.  
  55. private void    faxout_open_fp PROTO_ARGS((FILE *, TIFFOUT*));
  56. private int    faxout_begin_page PROTO_ARGS((TIFFOUT*, gx_device_printer*));
  57. private int    faxout_eolcode PROTO_ARGS((TIFFOUT *));
  58. private int    faxout_end_page PROTO_ARGS((TIFFOUT *));
  59. private void    tofax PROTO_ARGS((TIFFOUT*, unsigned char*));
  60.  
  61. private void putwhitespan();
  62. private void putblackspan();
  63. private void putcode();
  64. private void puteol();
  65. private void putbit();
  66. private void flushbits();
  67.  
  68. /*
  69.  * Redefine the device descriptor.
  70.  */
  71. struct gx_device_tiff_s {
  72.     gx_device_common;
  73.     gx_prn_device_common;
  74.     TIFFOUT    fax;
  75. };
  76. typedef struct gx_device_tiff_s gx_device_tiff;
  77.  
  78. /* The device descriptor */
  79. #define X_DPI 204
  80. #define Y_DPI 196
  81. #define LINE_SIZE ((X_DPI * 101 / 10 + 7) / 8)    /* bytes per line */
  82.  
  83. private dev_proc_open_device(tiff_prn_open);
  84. private dev_proc_print_page(tiff_print_page);
  85. private dev_proc_close_device(tiff_prn_close);
  86.  
  87. gx_device_procs tiff_std_procs =
  88.     prn_procs(tiff_prn_open, gdev_prn_output_page, tiff_prn_close);
  89.  
  90. gx_device_tiff far_data gs_tiffg3_device =
  91. {   prn_device_std_body(
  92.     gx_device_tiff,
  93.     tiff_std_procs,
  94.     "tiffg3",
  95.     85,            /* width_10ths, 8.5" */
  96.     110,            /* height_10ths, 11" */
  97.     X_DPI, Y_DPI,
  98.     0,0,0,0,        /* margins */
  99.     1,
  100.     tiff_print_page
  101.     )
  102. };
  103.  
  104.  
  105. static struct pageinfo {
  106.     short w, h;            /* page width and height in 10ths */
  107.     unsigned long iw;        /* image width */
  108. } pageinfo[] = {
  109. #define    PAPER_SIZE_LETTER    0
  110.     { 85,  110, 1728 },
  111. #define    PAPER_SIZE_A4        1
  112.     { 85,  117, 1728 },
  113. #define    PAPER_SIZE_B4        2
  114.     { 101, 143, 2048 }
  115. };
  116. #define    NPAGEINFO (sizeof (pageinfo) / sizeof (pageinfo[0]))
  117.  
  118. /* Get the paper size code, based on width and height. */
  119. static int
  120. papersize(gx_device *dev)
  121. {
  122.     return
  123.       (dev->height / dev->y_pixels_per_inch >= 11.8 ? PAPER_SIZE_B4 :
  124.        dev->height / dev->y_pixels_per_inch >= 11.1 ? PAPER_SIZE_A4 :
  125.        PAPER_SIZE_LETTER);
  126. }
  127.  
  128. /*
  129.  * Driver entry points.
  130.  */
  131.  
  132. /*
  133.  * Setup device according to output page.
  134.  */
  135. private int
  136. tiff_prn_open(gx_device *pdev)
  137. {
  138.     struct pageinfo* pi = &pageinfo[papersize(pdev)];
  139.     int    rc;
  140.  
  141.     pdev->width = (int)((pi->w * pdev->x_pixels_per_inch) / 10);
  142.     pdev->height = (int)((pi->h * pdev->y_pixels_per_inch) / 10);
  143.     rc = gdev_prn_open(pdev);
  144.     if (rc == 0) {
  145.     gx_device_tiff* ddev = (gx_device_tiff*) pdev;
  146.     faxout_open_fp(ddev->file, &ddev->fax);
  147.     ddev->fax.iwidth = pi->iw;
  148.     }
  149.     return (rc);
  150. }
  151.  
  152. private int
  153. tiff_print_page(gx_device_printer *pdev, FILE *prn_stream)
  154. {
  155.     gx_device_tiff* ddev = (gx_device_tiff*) pdev;
  156.     unsigned char data[LINE_SIZE + 4];
  157.     int    lnum, line_size;
  158.     TIFFOUT* fax = &ddev->fax;
  159.  
  160.     /* For some odd reason, the file isn't open until now */
  161.     fax->fp = prn_stream;    
  162.     faxout_begin_page(fax, pdev);
  163.     line_size = gdev_mem_bytes_per_scan_line((gx_device*)pdev);
  164.     for (lnum = 0; lnum < pdev->height; lnum++) {
  165.     gdev_prn_copy_scan_lines(pdev, lnum, (byte *)data, line_size);
  166.     tofax(fax, data);
  167.     }
  168.     faxout_end_page(fax);
  169.     return (0);
  170. }
  171.  
  172. private int
  173. tiff_prn_close(gx_device *pdev)
  174. {
  175.     gx_device_tiff* ddev = (gx_device_tiff*) pdev;
  176.     TIFFOUT* fax = &ddev->fax;
  177.  
  178.     if (fax->fp)
  179.     fflush(fax->fp);
  180.     return (gdev_prn_close(pdev));
  181. }
  182.  
  183. /*
  184.  * Internal routines.
  185.  */
  186. private void
  187. faxout_open_fp(FILE *fp, register TIFFOUT* faxp)
  188. {
  189.     faxp->fp = fp;
  190.     faxp->diroff = 0L;
  191.     faxp->prevdir = 0L;
  192.     faxp->bigendian = arch_is_big_endian;
  193.     faxp->fax_byte = 0;
  194.     faxp->fax_weight = 0x80;
  195. }
  196.  
  197. /* NB: this array is sorted by tag number (assumed below) */
  198. typedef struct {
  199.     TIFFDirEntry    subfiletype;
  200.     TIFFDirEntry    imagewidth;
  201.     TIFFDirEntry    imagelength;
  202.     TIFFDirEntry    bitspersample;
  203.     TIFFDirEntry    compression;
  204.     TIFFDirEntry    photometric;
  205.     TIFFDirEntry    fillorder;
  206. #ifdef notdef
  207.     TIFFDirEntry    documentname;
  208. #endif
  209.     TIFFDirEntry    stripoffsets;
  210.     TIFFDirEntry    orientation;
  211.     TIFFDirEntry    samplesperpixel;
  212.     TIFFDirEntry    rowsperstrip;
  213.     TIFFDirEntry    stripbytecounts;
  214.     TIFFDirEntry    xresolution;
  215.     TIFFDirEntry    yresolution;
  216.     TIFFDirEntry    planarconfig;
  217.     TIFFDirEntry    group3options;
  218.     TIFFDirEntry    resolutionunit;
  219. #ifdef notdef
  220.     TIFFDirEntry    software;
  221. #endif
  222.     TIFFDirEntry    cleanfaxdata;
  223.     unsigned long    diroff;            /* offset to next directory */
  224.     unsigned long    xresValue[2];        /* xresolution indirect value */
  225.     unsigned long    yresValue[2];        /* yresolution indirect value */
  226. } TIFFDirectory;
  227. private TIFFDirectory dirTemplate = {
  228.     { TIFFTAG_SUBFILETYPE,    TIFF_LONG,  1, FILETYPE_PAGE },
  229.     { TIFFTAG_IMAGEWIDTH,    TIFF_LONG,  1 },
  230.     { TIFFTAG_IMAGELENGTH,    TIFF_LONG,  1 },
  231.     { TIFFTAG_BITSPERSAMPLE,    TIFF_SHORT, 1, 1 },
  232.     { TIFFTAG_COMPRESSION,    TIFF_SHORT, 1, COMPRESSION_CCITTFAX3 },
  233.     { TIFFTAG_PHOTOMETRIC,    TIFF_SHORT, 1, PHOTOMETRIC_MINISWHITE },
  234.     { TIFFTAG_FILLORDER,    TIFF_SHORT, 1, FILLORDER_MSB2LSB },
  235. #ifdef notdef
  236.     { TIFFTAG_DOCUMENTNAME,    TIFF_ASCII, 1 },
  237. #endif
  238.     { TIFFTAG_STRIPOFFSETS,    TIFF_LONG,  1 },
  239.     { TIFFTAG_ORIENTATION,    TIFF_SHORT, 1, ORIENTATION_TOPLEFT },
  240.     { TIFFTAG_SAMPLESPERPIXEL,    TIFF_SHORT, 1, 1 },
  241.     { TIFFTAG_ROWSPERSTRIP,    TIFF_LONG,  1, -1L },
  242.     { TIFFTAG_STRIPBYTECOUNTS,    TIFF_LONG,  1, 1 },
  243.     { TIFFTAG_XRESOLUTION,    TIFF_RATIONAL, 1 },
  244.     { TIFFTAG_YRESOLUTION,    TIFF_RATIONAL, 1 },
  245.     { TIFFTAG_PLANARCONFIG,    TIFF_SHORT, 1, PLANARCONFIG_CONTIG },
  246.     { TIFFTAG_GROUP3OPTIONS,    TIFF_LONG,  1 },
  247.     { TIFFTAG_RESOLUTIONUNIT,    TIFF_SHORT, 1, RESUNIT_INCH },
  248. #ifdef notdef
  249.     { TIFFTAG_SOFTWARE,        TIFF_ASCII, 1 },
  250. #endif
  251.     { TIFFTAG_CLEANFAXDATA,    TIFF_SHORT, 1, CLEANFAXDATA_CLEAN },
  252.     0, { 0, 1 }, { 0, 1 },
  253. };
  254. #define    OFFSET(x)    ((unsigned)&(((TIFFDirectory*)0)->x))
  255. #define    NTAGS        (OFFSET(diroff) / sizeof (TIFFDirEntry))
  256.  
  257. /* correct tag values on bigendian machines */
  258. private void
  259. faxout_fixuptags(TIFFDirEntry* dp, int n)
  260. {
  261.     while (n-- > 0) {
  262.     if (dp->tdir_type == TIFF_SHORT || dp->tdir_type == TIFF_SSHORT)
  263.         dp->tdir_offset <<= 16;
  264.     else if (dp->tdir_type == TIFF_BYTE || dp->tdir_type == TIFF_SBYTE)
  265.         dp->tdir_offset <<= 24;
  266.     dp++;
  267.     }
  268. }
  269.  
  270. private int
  271. faxout_begin_page(TIFFOUT *faxp, gx_device_printer* pdev)
  272. {
  273.     gx_device_tiff* ddev = (gx_device_tiff*) pdev;
  274.     short dircount;
  275.     TIFFDirectory dir;
  276.  
  277.     /*
  278.      * Writing the header is delayed to here because the
  279.      * FILE* is not setup when faxout_open is called.
  280.      */
  281.     if (faxp->diroff == 0) {
  282.     TIFFHeader h;
  283.     h.tiff_magic = (faxp->bigendian ? TIFF_BIGENDIAN : TIFF_LITTLEENDIAN);
  284.     h.tiff_version = TIFF_VERSION;
  285.     h.tiff_diroff = sizeof (TIFFHeader);
  286.     fwrite((char*) &h, sizeof (h), 1, faxp->fp);
  287.     faxp->diroff = sizeof (TIFFHeader);    /* where next directory goes */
  288.     } else {
  289.     /* patch pointer to this directory */
  290.     fseek(faxp->fp, faxp->prevdir, 0);
  291.     fwrite((char*)&faxp->diroff, sizeof (faxp->diroff), 1, faxp->fp);
  292.     }
  293.     fseek(faxp->fp, faxp->diroff, 0);
  294.     /* write count of tags in directory */
  295.     dircount = NTAGS;
  296.     fwrite((char*)&dircount, sizeof (dircount), 1, faxp->fp);
  297.     faxp->diroff += sizeof (dircount);
  298.  
  299.     /* fill in directory tags and write them */
  300.     memcpy(&dir, &dirTemplate, sizeof (dirTemplate));
  301.     dir.imagewidth.tdir_offset = pageinfo[papersize((gx_device*) pdev)].iw;
  302.     dir.imagelength.tdir_offset = pdev->height;
  303.     dir.stripoffsets.tdir_offset = faxp->diroff + sizeof (TIFFDirectory);
  304.     dir.xresolution.tdir_offset = faxp->diroff + OFFSET(xresValue);
  305.     dir.yresolution.tdir_offset = faxp->diroff + OFFSET(yresValue);
  306.     dir.group3options.tdir_offset = 0;        /* XXX */
  307.     dir.xresValue[0] = ddev->x_pixels_per_inch;
  308.     dir.yresValue[0] = ddev->y_pixels_per_inch;
  309.     if (faxp->bigendian)
  310.     faxout_fixuptags(&dir.subfiletype, NTAGS);
  311.     fwrite((char*)&dir, sizeof (dir), 1, faxp->fp);
  312.  
  313.     puteol(faxp);
  314.     return (0);
  315. }
  316.  
  317. private int
  318. faxout_end_page(TIFFOUT *faxp)
  319. {
  320.     long diroff, cc;
  321.  
  322.     flushbits(faxp);
  323.     diroff = faxp->diroff;
  324.     faxp->prevdir = faxp->diroff + OFFSET(diroff);
  325.     faxp->diroff = ftell(faxp->fp);
  326.     /* patch strip byte counts value */
  327.     cc = faxp->diroff - (diroff + sizeof (TIFFDirectory));
  328.     fseek(faxp->fp, diroff + OFFSET(stripbytecounts.tdir_offset), 0);
  329.     fwrite(&cc, sizeof (cc), 1, faxp->fp);
  330.     return (0);
  331. }
  332.  
  333. private const byte far_data b_run_tbl[8][256] = {
  334.   {    /* START BIT 0 */
  335.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  336.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  337.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  338.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  339.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  340.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  341.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  342.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  343.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  344.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  345.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  346.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  347.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  348.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  349.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  350.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  351.   },
  352.   {    /* START BIT 1 */
  353.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  354.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  355.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  356.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  357.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  358.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  359.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  360.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  361.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  362.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  363.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  364.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  365.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  366.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  367.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  368.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  369.   },
  370.   {    /* START BIT 2 */
  371.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  372.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  373.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  374.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  375.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  376.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  377.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  378.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  379.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  380.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  381.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  382.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  383.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  384.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  385.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  386.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  387.   },
  388.   {    /* START BIT 3 */
  389.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  390.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  391.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  392.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  393.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  394.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  395.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  396.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  397.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  398.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  399.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  400.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  401.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  402.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  403.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  404.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  405.   },
  406.   {    /* START BIT 4 */
  407.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  408.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  409.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  410.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  411.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  412.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  413.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  414.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  415.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  416.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  417.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  418.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  419.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  420.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  421.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  422.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  423.   },
  424.   {    /* START BIT 5 */
  425.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  426.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  427.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  428.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  429.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  430.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  431.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  432.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  433.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  434.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  435.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  436.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  437.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  438.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  439.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  440.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  441.   },
  442.   {    /* START BIT 6 */
  443.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  444.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  445.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  446.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  447.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  448.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  449.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  450.     3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 7, 
  451.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  452.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  453.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  454.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  455.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  456.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  457.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  458.     3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 7, 
  459.   },
  460.   {    /* START BIT 7 */
  461.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  462.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  463.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  464.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  465.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  466.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  467.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  468.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  469.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  470.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  471.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  472.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  473.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  474.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  475.     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
  476.     4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, 
  477.   },
  478. };
  479.  
  480. private const byte far_data w_run_tbl[8][256] = {
  481.   {    /* START BIT 0 */
  482.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  483.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  484.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  485.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  486.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  487.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  488.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  489.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  490.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  491.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  492.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  493.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  494.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  495.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  496.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  497.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  498.   },
  499.   {    /* START BIT 1 */
  500.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  501.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  502.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  503.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  504.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  505.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  506.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  507.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  508.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  509.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  510.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  511.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  512.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  513.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  514.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  515.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  516.   },
  517.   {    /* START BIT 2 */
  518.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  519.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  520.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  521.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  522.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  523.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  524.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  525.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  526.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  527.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  528.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  529.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  530.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  531.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  532.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  533.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  534.   },
  535.   {    /* START BIT 3 */
  536.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  537.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  538.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  539.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  540.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  541.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  542.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  543.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  544.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  545.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  546.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  547.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  548.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  549.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  550.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  551.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  552.   },
  553.   {    /* START BIT 4 */
  554.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  555.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  556.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  557.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  558.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  559.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  560.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  561.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  562.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  563.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  564.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  565.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  566.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  567.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  568.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  569.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  570.   },
  571.   {    /* START BIT 5 */
  572.     6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
  573.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  574.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  575.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  576.     6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
  577.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  578.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  579.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  580.     6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
  581.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  582.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  583.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  584.     6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
  585.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  586.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  587.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  588.   },
  589.   {    /* START BIT 6 */
  590.     7, 6, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 
  591.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  592.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  593.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  594.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  595.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  596.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  597.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  598.     7, 6, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 
  599.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  600.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  601.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  602.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  603.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  604.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  605.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  606.   },
  607.   {    /* START BIT 7 */
  608.     8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 
  609.     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
  610.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  611.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  612.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  613.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  614.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  615.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  616.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  617.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  618.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  619.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  620.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  621.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  622.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  623.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  624.   },
  625. };
  626.  
  627. /*
  628.  * Macros to use tables in findrun.c
  629.  * Input is *p, bit
  630.  * Output is rl, *p, bit
  631.  */
  632. #define    find_black_run()                    \
  633.     for (rl = 0;;) {                        \
  634.     if (run = b_run_tbl[bit][*p]) {                \
  635.         rl += run;                        \
  636.         bit -= run;                        \
  637.         if (bit < 0 && ++p < ep) { bit=7; continue;}    \
  638.     }                            \
  639.     break;                            \
  640.     }
  641. #define    find_white_run()                    \
  642.     for (rl = 0;;) {                        \
  643.     if (run = w_run_tbl[bit][*p]) {                \
  644.         rl += run;                        \
  645.         bit -= run;                        \
  646.         if (bit < 0 && ++p < ep) { bit=7; continue;}    \
  647.     }                            \
  648.     break;                            \
  649.     }
  650.  
  651. private void
  652. tofax(TIFFOUT *faxp, unsigned char *p)
  653. {
  654.     unsigned char    *ep;
  655.     register int    bit;
  656.     register int    run;
  657.     register int    rl;
  658.  
  659.     ep = p + faxp->iwidth/8;
  660.     bit = 7;
  661.     for (;;) {
  662.     find_white_run();
  663.     putwhitespan(faxp, rl);
  664.     if (p >= ep) break;
  665.  
  666.     find_black_run();
  667.     putblackspan(faxp, rl);
  668.     if (p >= ep) break;
  669.     }
  670.     puteol(faxp);
  671. }
  672.  
  673.  
  674. /*************************************************************************
  675. **
  676. ** Copyright (C) 1989 by Paul Haeberli <paul@manray.sgi.com>.
  677. **
  678. ** Permission to use, copy, modify, and distribute this software and its
  679. ** documentation for any purpose and without fee is hereby granted, provided
  680. ** that the above copyright notice appear in all copies and that both that
  681. ** copyright notice and this permission notice appear in supporting
  682. ** documentation.  This software is provided "as is" without express or
  683. ** implied warranty.
  684.  *************************************************************************/
  685.  
  686. private void
  687. putwhitespan(register TIFFOUT *faxp, int c)
  688. {
  689.     register int tpos;
  690.     register tableentry* te;
  691.  
  692.     if(c>=64) {
  693.     tpos = (c/64)-1;
  694.     te = tpos < 27 ? mwtable+tpos : extable+tpos-27;
  695.     c -= te->count;
  696.     putcode(faxp, te);
  697.     }
  698.     tpos = c;
  699.     te = twtable+tpos;
  700.     putcode(faxp, te);
  701. }
  702.  
  703. private void
  704. putblackspan(register TIFFOUT *faxp, int c)
  705. {
  706.     register int tpos;
  707.     register tableentry* te;
  708.  
  709.     if(c>=64) {
  710.     tpos = (c/64)-1;
  711.     te = tpos < 27 ? mbtable+tpos : extable+tpos-27;
  712.     c -= te->count;
  713.     putcode(faxp, te);
  714.     }
  715.     tpos = c;
  716.     te = tbtable+tpos;
  717.     putcode(faxp, te);
  718. }
  719.  
  720. private void
  721. putcode(register TIFFOUT *faxp, tableentry *te)
  722. {
  723.     register unsigned int mask;
  724.     register int code;
  725.  
  726.     mask = 1<<(te->length-1);
  727.     code = te->code;
  728.     while(mask) {
  729.      if(code&mask)
  730.         putbit(faxp, 1);
  731.     else
  732.         putbit(faxp, 0);
  733.     mask >>= 1;
  734.     }
  735.  
  736. }
  737.  
  738. private void
  739. puteol(register TIFFOUT *faxp)
  740. {
  741.     register int i;
  742.  
  743.     for(i=0; i<11; ++i)
  744.     putbit(faxp, 0);
  745.     putbit(faxp, 1);
  746. }
  747.  
  748. private void
  749. putbit(register TIFFOUT *faxp, int d)
  750. {
  751.     if(d) 
  752.     faxp->fax_byte = faxp->fax_byte|faxp->fax_weight;
  753.     faxp->fax_weight = faxp->fax_weight>>1;
  754.     if((faxp->fax_weight&0xff) == 0) {
  755.     putc(faxp->fax_byte, faxp->fp);
  756.     faxp->fax_byte = 0;
  757.     faxp->fax_weight = 0x80;
  758.     }
  759. }
  760.  
  761. private void
  762. flushbits(register TIFFOUT *faxp)
  763. {
  764.     if (faxp->fax_weight != 0x80) {
  765.     putc(faxp->fax_byte, faxp->fp);
  766.     faxp->fax_byte = 0;
  767.     faxp->fax_weight = 0x80;
  768.     }
  769. }
  770.