home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / GDEVDFAX.C < prev    next >
C/C++ Source or Header  |  1992-04-25  |  25KB  |  784 lines

  1. /*
  2.  *    Copyright 1992 DigiBoard, Inc. All rights reserved
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software and its
  5.  * documentation for any purpose and without fee is hereby granted.
  6.  * This software is provided "as is" without express or implied warranty.
  7.  */
  8.  
  9. /* gdevdfax.c */
  10. /* DigiBoard, Inc. DigiFAX driver for Ghostscript. */
  11.  
  12. #include "gdevprn.h"
  13. #include "gdevdfg3.h"
  14.  
  15. #ifdef __PROTOTYPES__
  16. #    define PROTO_ARGS(X)    X
  17. #else
  18. #    define PROTO_ARGS(X)    ()
  19. #endif
  20.  
  21. /*************************************************************************
  22.  *    Format of page header in PC Research "group_3" FAX file
  23.  *
  24.  *    Note that all "shorts" are stored LSB in lowest byte address
  25.  *************************************************************************/
  26. typedef struct
  27. {
  28.     char    magic[24];
  29. } FAXBREAK;
  30.  
  31. #define    FAXMAGIC    "\000PC Research, Inc\000\000\000\000\000\000"
  32.     
  33. typedef struct
  34. {
  35. /*24*/    short    pages;        /* Number of pages in file */
  36. /*26*/    short    pagenum;
  37. /*28*/    char    msbfirst;    /* Coding used in file for codewords */
  38.                 /* MUST be set to 1, meaning the codewords */
  39.                 /* are filled starting with the most */
  40.                 /* significant bit */
  41. /*29*/    char    hires;        /* Set to 1 for Hi resolution, else 0 */
  42. /*30*/    char    dirty;        /* File may contain T.4 errors if 1, else 0 */
  43.                 /* e.g. it was received by FAX, not created */
  44.                 /* by a program */
  45. /*31*/    char    reservedc;
  46. /*32*/    short    reserved[12];
  47. } FAXINFO;
  48.  
  49. #define    OFFSET_PAGES    24
  50.  
  51. typedef struct
  52. {
  53.     char    jt0;
  54.     char    jthires;    /* Set to 0x40 for Hi resolution, else 0 */
  55.     char    jt2;
  56.     char    jt3;
  57.     char    jt4;
  58.     char    jt5;
  59.     char    jt6;
  60.     char    jt7;
  61. } JTINFO;
  62.  
  63. /*
  64.  *    This appears before each page in a FAX file
  65.  */
  66. typedef struct
  67. {
  68.     FAXBREAK    id;
  69.     FAXINFO        info;
  70.     JTINFO        jtinfo;
  71. } FAXHDR;
  72.  
  73. /**********************************************************************/
  74. /*
  75.  *    Generic fax output library
  76.  */
  77. /**********************************************************************/
  78.  
  79. typedef struct
  80. {
  81.     FILE        *fp;
  82.     int        fax_byte;
  83.     int        fax_weight;
  84.     int        pages;
  85. } FAXOUT;
  86.  
  87. private FAXOUT    *faxout_open PROTO_ARGS((char *));
  88. private FAXOUT    *faxout_open_fp PROTO_ARGS((FILE *));
  89. private int    faxout_begin_page PROTO_ARGS((FAXOUT *, int));
  90. private int    faxout_eolcode PROTO_ARGS((FAXOUT *));
  91. private int    faxout_end_page PROTO_ARGS((FAXOUT *));
  92. private int    faxout_close PROTO_ARGS((FAXOUT *));
  93. private unsigned short fax_ushort PROTO_ARGS((unsigned short));
  94. private void    tofax();
  95.  
  96. private void putwhitespan();
  97. private void putblackspan();
  98. private void putcode();
  99. private void puteol();
  100. private void putbit();
  101. private void flushbits();
  102.  
  103. /*************************************************************************
  104.     Redefine the device descriptor
  105.  *************************************************************************/
  106. struct gx_device_dfax_s {
  107.     gx_device_common;
  108.     gx_prn_device_common;
  109.     FAXOUT    *fax;
  110. };
  111. typedef struct gx_device_dfax_s gx_device_dfax;
  112.  
  113. /*    Too bad the prn_device() macro doesn't let you specify the
  114.  *    size of the gx_device structure -- we have to repeat it here
  115.  */
  116. #define generic_prn_device(Struct, procs, dev_name, width_10ths, height_10ths, x_dpi, y_dpi, l_margin, b_margin, r_margin, t_margin, color_bits, print_page) {\
  117.     sizeof(Struct),\
  118.     &procs,\
  119.     dev_name,\
  120.     (int)((long)width_10ths * x_dpi / 10),    /* width */\
  121.     (int)((long)height_10ths * y_dpi / 10),    /* height */\
  122.     x_dpi,\
  123.     y_dpi,\
  124.     l_margin, b_margin, r_margin, t_margin,\
  125.        {    (color_bits > 1 ? 3 : 1),    /* num_components */\
  126.         ((color_bits > 1) & (color_bits < 8) ? 8 : color_bits),    /* depth */\
  127.         (color_bits >= 8 ? 255 : 1),    /* max_gray */\
  128.         (color_bits >= 8 ? 255 : color_bits > 1 ? 1 : 0),    /* max_rgb */\
  129.         (color_bits >= 8 ? 5 : 2),    /* dither_gray */\
  130.         (color_bits >= 8 ? 5 : color_bits > 1 ? 2 : 0),    /* dither_rgb */\
  131.        },\
  132.     0,        /* not initialized yet */\
  133.       { 0 },    /* skip */\
  134.     print_page,\
  135.     PRN_MAX_BITMAP,\
  136.     PRN_BUFFER_SPACE,\
  137.       { 0 }        /* fname */\
  138. }
  139.  
  140. /* The device descriptor */
  141. #define X_DPI 204
  142. #define Y_DPI 196
  143. #define LINE_SIZE ((X_DPI * 85 / 10 + 7) / 8)    /* bytes per line */
  144.  
  145. private dev_proc_open_device(dfax_prn_open);
  146. private dev_proc_print_page(dfax_print_page);
  147. private dev_proc_close_device(dfax_prn_close);
  148.  
  149. gx_device_procs dfax_std_procs =
  150.     prn_procs(dfax_prn_open, gdev_prn_output_page, dfax_prn_close);
  151.  
  152. gx_device_dfax gs_dfaxhigh_device =
  153.     generic_prn_device(
  154.         gx_device_dfax,
  155.         dfax_std_procs,
  156.         "dfaxhigh",
  157.         85,            /* width_10ths, 8.5" */
  158.         110,            /* height_10ths, 11" */
  159.         X_DPI, Y_DPI,
  160.         0,0,0,0,        /* margins */
  161.         1,
  162.         dfax_print_page
  163.     );
  164.  
  165. gx_device_dfax gs_dfaxlow_device =
  166.     generic_prn_device(
  167.         gx_device_dfax,
  168.         dfax_std_procs,
  169.         "dfaxlow",
  170.         85,            /* width_10ths, 8.5" */
  171.         110,            /* height_10ths, 11" */
  172.         X_DPI, Y_DPI/2,
  173.         0,0,0,0,        /* margins */
  174.         1,
  175.         dfax_print_page
  176.     );
  177.  
  178. /*************************************************************************
  179.     Driver entry points
  180.  *************************************************************************/
  181.  
  182. private int
  183. dfax_prn_open(gx_device *pdev)
  184. {
  185.     gx_device_dfax    *ddev = (gx_device_dfax *) pdev;
  186.     int        rc;
  187.  
  188.     rc = gdev_prn_open(pdev);
  189.     ddev->fax = faxout_open_fp(ddev->file);
  190.     return (rc);
  191. }
  192.  
  193. private int
  194. dfax_print_page(gx_device_printer *pdev, FILE *prn_stream)
  195. {
  196.     gx_device_dfax    *ddev = (gx_device_dfax *) pdev;
  197.     char        data[LINE_SIZE + 4];
  198.     int        lnum;
  199.     int        line_size;
  200.     FAXOUT        *fax = ddev->fax;
  201.  
  202.     /* For some odd reason, the file isn't open until now */
  203.     fax->fp = prn_stream;    
  204.     faxout_begin_page(fax, ddev->y_pixels_per_inch > 120);
  205.     
  206.     line_size = gdev_mem_bytes_per_scan_line( (gx_device *) pdev);
  207.     for ( lnum = 0; lnum < pdev->height; lnum++ )
  208.     {
  209.         gdev_prn_copy_scan_lines(pdev, lnum, (byte *)data, line_size);
  210.         tofax(fax, data, 1728);
  211.     }
  212.     faxout_end_page(fax);
  213.     return 0;
  214. }
  215.  
  216. private int
  217. dfax_prn_close(gx_device *pdev)
  218. {
  219.     gx_device_dfax    *ddev = (gx_device_dfax *) pdev;
  220.     int        rc;
  221.     FAXOUT        *fax = ddev->fax;
  222.     unsigned short     p = fax_ushort(fax->pages);
  223.  
  224.     if (fax->fp)
  225.     {
  226.         fflush(fax->fp);
  227.         if (fseek(fax->fp, (long) OFFSET_PAGES, 0) == 0)
  228.             fwrite((char*)&p, sizeof(p), 1, fax->fp);
  229.     }
  230.     rc = gdev_prn_close(pdev);
  231.     return(rc);
  232. }
  233.  
  234. /*************************************************************************
  235.     Internal routines
  236.  *************************************************************************/
  237.  
  238. /************************Coding FAX Routines*************************/
  239. /*
  240.  *    faxp = faxout_open(filename);
  241.  *    faxp = faxout_open_fp(fp);
  242.  *    faxout_begin_page(faxp, resolution);
  243.  *    for(;;) tofax(faxp, linebuf);
  244.  *    faxout_end_page(faxp);
  245.  *    faxout_close(faxp);
  246.  */
  247.  
  248. private FAXOUT    *
  249. faxout_open_fp(FILE *fp)
  250. {
  251.     register FAXOUT    *faxp;
  252.  
  253.     faxp = (FAXOUT *) malloc(sizeof(*faxp));
  254.  
  255.     faxp->fp = fp;
  256.     faxp->fax_byte = 0;
  257.     faxp->fax_weight = 0x80;
  258.     faxp->pages = 0;
  259.  
  260.     return (faxp);
  261. }
  262.  
  263. private FAXOUT    *
  264. faxout_open(char *filename)
  265. {
  266.     register FILE    *fp;
  267.     register FAXOUT    *faxp;
  268.  
  269.     if (filename)
  270.         fp = fopen(filename, "wb");
  271.     else
  272.         fp = stdout;
  273.     if (!fp) return (NULL);
  274.  
  275.     return(faxout_open_fp(fp));
  276. }
  277.  
  278. private int
  279. faxout_begin_page(FAXOUT *faxp, int resolution)
  280. {
  281.     FAXHDR    hdr;
  282.  
  283.     memset(&hdr, 0, sizeof(hdr));
  284.     memcpy(hdr.id.magic, FAXMAGIC, sizeof(hdr.id.magic));
  285.     hdr.info.pagenum = fax_ushort(++faxp->pages);
  286.     hdr.info.msbfirst = 1;
  287.     hdr.info.hires = resolution;
  288.     hdr.jtinfo.jthires = resolution ? 0x40 : 0;
  289.     fwrite((char*)&hdr, sizeof(hdr), 1, faxp->fp);
  290.  
  291.     puteol(faxp);
  292.  
  293.     return (0);
  294. }
  295.  
  296. private int
  297. faxout_end_page(FAXOUT *faxp)
  298. {
  299.     flushbits(faxp);
  300.     puteol(faxp);
  301.     puteol(faxp);
  302.     puteol(faxp);
  303.     puteol(faxp);
  304.     puteol(faxp);
  305.     flushbits(faxp);
  306.     return (0);
  307. }
  308.  
  309. private int
  310. faxout_close(FAXOUT *faxp)
  311. {
  312.     unsigned short p = fax_ushort(faxp->pages);
  313.  
  314.     fflush(faxp->fp);
  315.     if (fseek(faxp->fp, (long) OFFSET_PAGES, 0) == 0)
  316.         fwrite((char*)&p, sizeof(p), 1, faxp->fp);
  317.     fclose(faxp->fp);
  318.     free(faxp);
  319.  
  320.     return (0);
  321. };
  322.  
  323. private unsigned short
  324. fax_ushort(unsigned short v)
  325. {
  326.     static unsigned short    x = 0x1122;
  327.     static unsigned short    *xp = &x;
  328.     
  329.     if ( *((unsigned char *)xp) == 0x22)
  330.         return (v);
  331.     else
  332.         return ( ((v>>8)&255) + (v<<8) );
  333. }
  334.  
  335. private unsigned char    b_run_tbl[8][256] =
  336. {
  337.   {    /* START BIT 0 */
  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.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  352.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  353.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  354.   },
  355.   {    /* START BIT 1 */
  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.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  370.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  371.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  372.   },
  373.   {    /* START BIT 2 */
  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.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  388.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  389.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  390.   },
  391.   {    /* START BIT 3 */
  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.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  406.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  407.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  408.   },
  409.   {    /* START BIT 4 */
  410.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  411.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  412.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  413.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  414.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  415.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  416.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  417.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  418.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  419.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  420.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  421.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  422.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  423.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  424.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  425.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  426.   },
  427.   {    /* START BIT 5 */
  428.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  429.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  430.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  431.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  432.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  433.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  434.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  435.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  436.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  437.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  438.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  439.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  440.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  441.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  442.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  443.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  444.   },
  445.   {    /* START BIT 6 */
  446.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  447.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  448.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  449.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  450.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  451.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  452.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  453.     3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 7, 
  454.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  455.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  456.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  457.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  458.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  459.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  460.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  461.     3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 7, 
  462.   },
  463.   {    /* START BIT 7 */
  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.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  470.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  471.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  472.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  473.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  474.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  475.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  476.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  477.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  478.     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
  479.     4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, 
  480.   },
  481. };
  482.  
  483. private unsigned char    w_run_tbl[8][256] =
  484. {
  485.   {    /* START BIT 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.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  499.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  500.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  501.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  502.   },
  503.   {    /* START BIT 1 */
  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.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  517.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  518.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  519.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  520.   },
  521.   {    /* START BIT 2 */
  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.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  535.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  536.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  537.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  538.   },
  539.   {    /* START BIT 3 */
  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.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  553.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  554.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  555.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  556.   },
  557.   {    /* START BIT 4 */
  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.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  571.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  572.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  573.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  574.   },
  575.   {    /* START BIT 5 */
  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.     6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
  589.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  590.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  591.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  592.   },
  593.   {    /* START BIT 6 */
  594.     7, 6, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 
  595.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  596.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  597.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  598.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  599.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  600.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  601.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  602.     7, 6, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 
  603.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  604.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  605.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  606.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  607.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  608.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  609.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  610.   },
  611.   {    /* START BIT 7 */
  612.     8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 
  613.     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
  614.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  615.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  616.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  617.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  618.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  619.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  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.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  625.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  626.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  627.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  628.   },
  629. };
  630.  
  631. /*
  632.  *    Macros to use tables in findrun.c
  633.  *    Input is *p, bit
  634.  *    Output is rl, *p, bit
  635.  */
  636.  
  637. #define    find_black_run() \
  638. for (rl = 0;;) \
  639. { \
  640.     if (run = b_run_tbl[bit][*p]) \
  641.     { \
  642.         rl += run; \
  643.         bit -= run; \
  644.         if (bit < 0 && ++p < ep) { bit=7; continue;} \
  645.     } \
  646.     break; \
  647. }
  648.  
  649. #define    find_white_run() \
  650. for (rl = 0;;) \
  651. { \
  652.     if (run = w_run_tbl[bit][*p]) \
  653.     { \
  654.         rl += run; \
  655.         bit -= run; \
  656.         if (bit < 0 && ++p < ep) { bit=7; continue;} \
  657.     } \
  658.     break; \
  659. }
  660.  
  661. private void
  662. tofax(FAXOUT *faxp, unsigned char *p)
  663. {
  664.     unsigned char        *ep;
  665.     register int        bit;
  666.     register int        run;
  667.     register int        rl;
  668.  
  669.     ep = p + 1728/8;
  670.     bit = 7;
  671.     for (;;)
  672.     {
  673.         find_white_run();
  674.         putwhitespan(faxp, rl);
  675.         if (p >= ep) break;
  676.  
  677.         find_black_run();
  678.         putblackspan(faxp, rl);
  679.         if (p >= ep) break;
  680.     }
  681.     puteol(faxp);
  682. }
  683.  
  684.  
  685. /*************************************************************************
  686.     The rest of this file is a FAX encoding algorithm
  687.     derived from pbmplus.  It is not the normal DigiFAX algorithm.
  688.     The following copyright applies.
  689. **
  690. ** Copyright (C) 1989 by Paul Haeberli <paul@manray.sgi.com>.
  691. **
  692. ** Permission to use, copy, modify, and distribute this software and its
  693. ** documentation for any purpose and without fee is hereby granted, provided
  694. ** that the above copyright notice appear in all copies and that both that
  695. ** copyright notice and this permission notice appear in supporting
  696. ** documentation.  This software is provided "as is" without express or
  697. ** implied warranty.
  698.  *************************************************************************/
  699.  
  700. private void
  701. putwhitespan(register FAXOUT *faxp, int c)
  702. {
  703.     register int tpos;
  704.     register tableentry* te;
  705.  
  706.     if(c>=64) {
  707.     tpos = (c/64)-1;
  708.     te = mwtable+tpos;
  709.     c -= te->count;
  710.     putcode(faxp, te);
  711.     }
  712.     tpos = c;
  713.     te = twtable+tpos;
  714.     putcode(faxp, te);
  715. }
  716.  
  717. private void
  718. putblackspan(register FAXOUT *faxp, int c)
  719. {
  720.     register int tpos;
  721.     register tableentry* te;
  722.  
  723.     if(c>=64) {
  724.     tpos = (c/64)-1;
  725.     te = mbtable+tpos;
  726.     c -= te->count;
  727.     putcode(faxp, te);
  728.     }
  729.     tpos = c;
  730.     te = tbtable+tpos;
  731.     putcode(faxp, te);
  732. }
  733.  
  734. private void
  735. putcode(register FAXOUT *faxp, tableentry *te)
  736. {
  737.     register unsigned int mask;
  738.     register int code;
  739.  
  740.     mask = 1<<(te->length-1);
  741.     code = te->code;
  742.     while(mask) {
  743.      if(code&mask)
  744.         putbit(faxp, 1);
  745.     else
  746.         putbit(faxp, 0);
  747.     mask >>= 1;
  748.     }
  749.  
  750. }
  751.  
  752. private void
  753. puteol(register FAXOUT *faxp)
  754. {
  755.     register int i;
  756.  
  757.     for(i=0; i<11; ++i)
  758.     putbit(faxp, 0);
  759.     putbit(faxp, 1);
  760. }
  761.  
  762. private void
  763. putbit(register FAXOUT *faxp, int d)
  764. {
  765.     if(d) 
  766.     faxp->fax_byte = faxp->fax_byte|faxp->fax_weight;
  767.     faxp->fax_weight = faxp->fax_weight>>1;
  768.     if((faxp->fax_weight&0xff) == 0) {
  769.     putc(faxp->fax_byte, faxp->fp);
  770.     faxp->fax_byte = 0;
  771.     faxp->fax_weight = 0x80;
  772.     }
  773. }
  774.  
  775. private void
  776. flushbits(register FAXOUT *faxp)
  777. {
  778.     if (faxp->fax_weight != 0x80) {
  779.     putc(faxp->fax_byte, faxp->fp);
  780.     faxp->fax_byte = 0;
  781.     faxp->fax_weight = 0x80;
  782.     }
  783. }
  784.