home *** CD-ROM | disk | FTP | other *** search
/ ftp.infoflex.se / 2015-02-10.ftp.infoflex.se.tar / ftp.infoflex.se / pub / xfax / ghostscript / gdevxfax.c next >
C/C++ Source or Header  |  2004-07-22  |  35KB  |  1,359 lines

  1. /*
  2.  * gdevxfax.c
  3.  * By Nordic Messaging Tech. AB, Sweden, 2001-2002.
  4.  * Hopefully you find this XFAX driver for Ghostscript
  5.  * useful. However, with provide it WITHOUT ANY
  6.  * WARRANTY. You use it at YOUR OWN RISK.
  7.  * Visit us at http://www.nordicmessaging.se or send
  8.  * e-mail at info@nordicmessaging.se.
  9.  *
  10.  * We are helpful but do NOT guarantee support for
  11.  * this Ghostscript driver.
  12.  *
  13.  * Jan 20, 2004, version 1.2, be sure lines end with blank space
  14.  * Oct 23, 2002, version 1.1, added support for appending
  15.  * when output file already exists.
  16.  */
  17.  
  18. #include "gdevprn.h"
  19. #include "std.h"
  20. #include <sys/stat.h>
  21. #include "gdevxfax.h"
  22.  
  23. unsigned char huffman_white[130] = {
  24.     8,    /* ;0 */
  25.     6,    /* ;1 */
  26.     4,    /* ;2 */
  27.     4,    /* ;3 */
  28.     4,    /* ;4 */
  29.     4,    /* ;5 */
  30.     4,    /* ;6 */
  31.     4,    /* ;7 */
  32.     5,    /* ;8 */
  33.     5,    /* ;9 */
  34.     5,    /* ;10 */
  35.     5,    /* ;11 */
  36.     6,    /* ;12 */
  37.     6,    /* ;13 */
  38.     6,    /* ;14 */
  39.     6,    /* ;15 */
  40.  
  41.     6,    /* ;16 */
  42.     6,    /* ;17 */
  43.     7,    /* ;18 */
  44.     7,    /* ;19 */
  45.     7,    /* ;20 */
  46.     7,    /* ;21 */
  47.     7,    /* ;22 */
  48.     7,    /* ;23 */
  49.     7,    /* ;24 */
  50.     7,    /* ;25 */
  51.     7,    /* ;26 */
  52.     7,    /* ;27 */
  53.     7,    /* ;28 */
  54.     8,    /* ;29 */
  55.     8,    /* ;30 */
  56.     8,    /* ;31 */
  57.  
  58.     8,    /* ;32 */
  59.     8,    /* ;33 */
  60.     8,    /* ;34 */
  61.     8,    /* ;35 */
  62.     8,    /* ;36 */
  63.     8,    /* ;37 */
  64.     8,    /* ;38 */
  65.     8,    /* ;39 */
  66.     8,    /* ;40 */
  67.     8,    /* ;41 */
  68.     8,    /* ;42 */
  69.     8,    /* ;43 */
  70.     8,    /* ;44 */
  71.     8,    /* ;45 */
  72.     8,    /* ;46 */
  73.     8,    /* ;47 */
  74.  
  75.     8,    /* ;48 */
  76.     8,    /* ;49 */
  77.     8,    /* ;50 */
  78.     8,    /* ;51 */
  79.     8,    /* ;52 */
  80.     8,    /* ;53 */
  81.     8,    /* ;54 */
  82.     8,    /* ;55 */
  83.     8,    /* ;56 */
  84.     8,    /* ;57 */
  85.     8,    /* ;58 */
  86.     8,    /* ;59 */
  87.     8,    /* ;60 */
  88.     8,    /* ;61 */
  89.     8,    /* ;62 */
  90.     8,    /* ;63 */
  91.  
  92. /*
  93. ; White make-up code lengths:
  94. */
  95.     5,    /* ;64 */
  96.     5,    /* ;128 */
  97.     6,    /* ;192 */
  98.     7,    /* ;256 */
  99.     8,    /* ;320 */
  100.     8,    /* ;384 */
  101.     8,    /* ;448 */
  102.     8,    /* ;512 */
  103.     8,    /* ;576 */
  104.     8,    /* ;640 */
  105.     9,    /* ;704 */
  106.     9,    /* ;768 */
  107.     9,    /* ;832 */
  108.     9,    /* ;896 */
  109.     9,    /* ;960 */
  110.     9,    /* ;1024 */
  111.     9,    /* ;1088 */
  112.     9,    /* ;1152 */
  113.     9,    /* ;1216 */
  114.     9,    /* ;1280 */
  115.     9,    /* ;1344 */
  116.     9,    /* ;1408 */
  117.     9,    /* ;1472 */
  118.     9,    /* ;1536 */
  119.     9,    /* ;1600 */
  120.     6,    /* ;1664 */
  121.     9,    /* ;1728 */
  122.  
  123.     11,    /* ;1792 */
  124.     11,    /* ;1856 */
  125.     11,    /* ;1920 */
  126.     12,    /* ;1984 */
  127.     12,    /* ;2048 */
  128.     12,    /* ;2112 */
  129.     12,    /* ;2176 */
  130.     12,    /* ;2240 */
  131.     12,    /* ;2304 */
  132.     12,    /* ;2368 */
  133.     12,    /* ;2432 */
  134.     12,    /* ;2496 */
  135.     12     /* ;2560 */
  136. };
  137.  
  138. /*
  139. ; White data:
  140. */
  141. unsigned short int huffman1[] = {
  142.     0x3500,    /* B        ;0 */
  143.     0x1c00,    /* B        ;1 */
  144.     0x7000,    /* B        ;2 */
  145.     0x8000,    /* B        ;3 */
  146.     0xb000,    /* B        ;4 */
  147.     0xc000,    /* B        ;5 */
  148.     0xe000,    /* B        ;6 */
  149.     0xf000,    /* B        ;7 */
  150.     0x9800,    /* B        ;8 */
  151.     0xa000,    /* B        ;9 */
  152.     0x3800,    /* B        ;10 */
  153.     0x4000,    /* B        ;11 */
  154.     0x2000,    /* B        ;12 */
  155.     0x0c00,    /* B        ;13 */
  156.     0xd000,    /* B        ;14 */
  157.     0xd400,    /* B        ;15 */
  158.  
  159.     0xa800,    /* B        ;16 */
  160.     0xac00,    /* B        ;17 */
  161.     0x4e00,    /* B        ;18 */
  162.     0x1800,    /* B        ;19 */
  163.     0x1000,    /* B        ;20 */
  164.     0x2e00,    /* B        ;21 */
  165.     0x0600,    /* B        ;22 */
  166.     0x0800,    /* B        ;23 */
  167.     0x5000,    /* B        ;24 */
  168.     0x5600,    /* B        ;25 */
  169.     0x2600,    /* B        ;26 */
  170.     0x4800,    /* B        ;27 */
  171.     0x3000,    /* B        ;28 */
  172.     0x0200,    /* B        ;29 */
  173.     0x0300,    /* B        ;30 */
  174.     0x1a00,    /* B        ;31 */
  175.  
  176.     0x1b00,    /* B        ;32 */
  177.     0x1200,    /* B        ;33 */
  178.     0x1300,    /* B        ;34 */
  179.     0x1400,    /* B        ;35 */
  180.     0x1500,    /* B        ;36 */
  181.     0x1600,    /* B        ;37 */
  182.     0x1700,    /* B        ;38 */
  183.     0x2800,    /* B        ;39 */
  184.     0x2900,    /* B        ;40 */
  185.     0x2a00,    /* B        ;41 */
  186.     0x2b00,    /* B        ;42 */
  187.     0x2c00,    /* B        ;43 */
  188.     0x2d00,    /* B        ;44 */
  189.     0x0400,    /* B        ;45 */
  190.     0x0500,    /* B        ;46 */
  191.     0x0a00,    /* B        ;47 */
  192.  
  193.     0x0b00,    /* B        ;48 */
  194.     0x5200,    /* B        ;49 */
  195.     0x5300,    /* B        ;50 */
  196.     0x5400,    /* B        ;51 */
  197.     0x5500,    /* B        ;52 */
  198.     0x2400,    /* B        ;53 */
  199.     0x2500,    /* B        ;54 */
  200.     0x5800,    /* B        ;55 */
  201.     0x5900,    /* B        ;56 */
  202.     0x5a00,    /* B        ;57 */
  203.     0x5b00,    /* B        ;58 */
  204.     0x4a00,    /* B        ;59 */
  205.     0x4b00,    /* B        ;60 */
  206.     0x3200,    /* B        ;61 */
  207.     0x3300,    /* B        ;62 */
  208.     0x3400,    /* B        ;63 */
  209. /*
  210. ; White make-up data:
  211. */
  212.     0xd800,    /* B        ;64 */
  213.     0x9000,    /* B        ;128 */
  214.     0x5c00,    /* B        ;192 */
  215.     0x6e00,    /* B        ;256 */
  216.     0x3600,    /* B        ;320 */
  217.     0x3700,    /* B        ;384 */
  218.     0x6400,    /* B        ;448 */
  219.     0x6500,    /* B        ;512 */
  220.     0x6800,    /* B        ;576 */
  221.     0x6700,    /* B        ;640 */
  222.     0x6600,    /* B        ;704 */
  223.     0x6680,    /* B        ;768 */
  224.     0x6900,    /* B        ;832 */
  225.     0x6980,    /* B        ;896 */
  226.     0x6a00,    /* B        ;960 */
  227.     0x6a80,    /* B        ;1024 */
  228.     0x6b00,    /* B        ;1088 */
  229.     0x6b80,    /* B        ;1152 */
  230.     0x6c00,    /* B        ;1216 */
  231.     0x6c80,    /* B        ;1280 */
  232.     0x6d00,    /* B        ;1344 */
  233.     0x6d80,    /* B        ;1408 */
  234.     0x4c00,    /* B        ;1472 */
  235.     0x4c80,    /* B        ;1536 */
  236.     0x4d00,    /* B        ;1600 */
  237.     0x6000,    /* B        ;1664 */
  238.     0x4d80,    /* B        ;1728 */
  239.  
  240.     0x0100,    /* B        ;1792 */
  241.     0x0180,    /* B        ;1856 */
  242.     0x01a0,    /* B        ;1920 */
  243.     0x0120,    /* B        ;1984 */
  244.     0x0130,    /* B        ;2048 */
  245.     0x0140,    /* B        ;2112 */
  246.     0x0150,    /* B        ;2176 */
  247.     0x0160,    /* B        ;2240 */
  248.     0x0170,    /* B        ;2304 */
  249.     0x01c0,    /* B        ;2368 */
  250.     0x01d0,    /* B        ;2432 */
  251.     0x01e0,    /* B        ;2496 */
  252.     0x01f0    /* B        ;2560 */
  253. };
  254.  
  255. unsigned char huffman_black[] = {
  256.     10,    /* ;0 */
  257.     3,    /* ;1 */
  258.     2,    /* ;2 */
  259.     2,    /* ;3 */
  260.     3,    /* ;4 */
  261.     4,    /* ;5 */
  262.     4,    /* ;6 */
  263.     5,    /* ;7 */
  264.     6,    /* ;8 */
  265.     6,    /* ;9 */
  266.     7,    /* ;10 */
  267.     7,    /* ;11 */
  268.     7,    /* ;12 */
  269.     8,    /* ;13 */
  270.     8,    /* ;14 */
  271.     9,    /* ;15 */
  272.  
  273.     10,    /* ;16 */
  274.     10,    /* ;17 */
  275.     10,    /* ;18 */
  276.     11,    /* ;19 */
  277.     11,    /* ;20 */
  278.     11,    /* ;21 */
  279.     11,    /* ;22 */
  280.     11,    /* ;23 */
  281.     11,    /* ;24 */
  282.     11,    /* ;25 */
  283.     12,    /* ;26 */
  284.     12,    /* ;27 */
  285.     12,    /* ;28 */
  286.     12,    /* ;29 */
  287.     12,    /* ;30 */
  288.     12,    /* ;31 */
  289.  
  290.     12,    /* ;32 */
  291.     12,    /* ;33 */
  292.     12,    /* ;34 */
  293.     12,    /* ;35 */
  294.     12,    /* ;36 */
  295.     12,    /* ;37 */
  296.     12,    /* ;38 */
  297.     12,    /* ;39 */
  298.     12,    /* ;40 */
  299.     12,    /* ;41 */
  300.     12,    /* ;42 */
  301.     12,    /* ;43 */
  302.     12,    /* ;44 */
  303.     12,    /* ;45 */
  304.     12,    /* ;46 */
  305.     12,    /* ;47 */
  306.  
  307.     12,    /* ;48 */
  308.     12,    /* ;49 */
  309.     12,    /* ;50 */
  310.     12,    /* ;51 */
  311.     12,    /* ;52 */
  312.     12,    /* ;53 */
  313.     12,    /* ;54 */
  314.     12,    /* ;55 */
  315.     12,    /* ;56 */
  316.     12,    /* ;57 */
  317.     12,    /* ;58 */
  318.     12,    /* ;59 */
  319.     12,    /* ;60 */
  320.     12,    /* ;61 */
  321.     12,    /* ;62 */
  322.     12,    /* ;63 */
  323. /*
  324. ; Black make-up code lengths:
  325. */
  326.     10,    /* ;64 */
  327.     12,    /* ;128 */
  328.     12,    /* ;192 */
  329.     12,    /* ;256 */
  330.     12,    /* ;320 */
  331.     12,    /* ;384 */
  332.     12,    /* ;448 */
  333.     13,    /* ;512 */
  334.     13,    /* ;576 */
  335.     13,    /* ;640 */
  336.     13,    /* ;704 */
  337.     13,    /* ;768 */
  338.     13,    /* ;832 */
  339.     13,    /* ;896 */
  340.     13,    /* ;960 */
  341.     13,    /* ;1024 */
  342.     13,    /* ;1088 */
  343.     13,    /* ;1152 */
  344.     13,    /* ;1216 */
  345.     13,    /* ;1280 */
  346.     13,    /* ;1344 */
  347.     13,    /* ;1408 */
  348.     13,    /* ;1472 */
  349.     13,    /* ;1536 */
  350.     13,    /* ;1600 */
  351.     13,    /* ;1664 */
  352.     13,    /* ;1728 */
  353.  
  354.     11,    /* ;1792 */
  355.     11,    /* ;1856 */
  356.     11,    /* ;1920 */
  357.     12,    /* ;1984 */
  358.     12,    /* ;2048 */
  359.     12,    /* ;2112 */
  360.     12,    /* ;2176 */
  361.     12,    /* ;2240 */
  362.     12,    /* ;2304 */
  363.     12,    /* ;2368 */
  364.     12,    /* ;2432 */
  365.     12,    /* ;2496 */
  366.     12    /* ;2560 */
  367. };
  368.  
  369. /*
  370. ; Black data:
  371. */
  372. unsigned short int huffman2[] = {
  373.     0x0dc0,    /* ;0 */
  374.     0x4000,    /* ;1 */
  375.     0xc000,    /* ;2 */
  376.     0x8000,    /* ;3 */
  377.     0x6000,    /* ;4 */
  378.     0x3000,    /* ;5 */
  379.     0x2000,    /* ;6 */
  380.     0x1800,    /* ;7 */
  381.     0x1400,    /* ;8 */
  382.     0x1000,    /* ;9 */
  383.     0x0800,    /* ;10 */
  384.     0x0a00,    /* ;11 */
  385.     0x0e00,    /* ;12 */
  386.     0x0400,    /* ;13 */
  387.     0x0700,    /* ;14 */
  388.     0x0c00,    /* ;15 */
  389.  
  390.     0x05c0,    /* ;16 */
  391.     0x0600,    /* ;17 */
  392.     0x0200,    /* ;18 */
  393.     0x0ce0,    /* ;19 */
  394.     0x0d00,    /* ;20 */
  395.     0x0d80,    /* ;21 */
  396.     0x06e0,    /* ;22 */
  397.     0x0500,    /* ;23 */
  398.     0x02e0,    /* ;24 */
  399.     0x0300,    /* ;25 */
  400.     0x0ca0,    /* ;26 */
  401.     0x0cb0,    /* ;27 */
  402.     0x0cc0,    /* ;28 */
  403.     0x0cd0,    /* ;29 */
  404.     0x0680,    /* ;30 */
  405.     0x0690,    /* ;31 */
  406.  
  407.     0x06a0,    /* ;32 */
  408.     0x06b0,    /* ;33 */
  409.     0x0d20,    /* ;34 */
  410.     0x0d30,    /* ;35 */
  411.     0x0d40,    /* ;36 */
  412.     0x0d50,    /* ;37 */
  413.     0x0d60,    /* ;38 */
  414.     0x0d70,    /* ;39 */
  415.     0x06c0,    /* ;40 */
  416.     0x06d0,    /* ;41 */
  417.     0x0da0,    /* ;42 */
  418.     0x0db0,    /* ;43 */
  419.     0x0540,    /* ;44 */
  420.     0x0550,    /* ;45 */
  421.     0x0560,    /* ;46 */
  422.     0x0570,    /* ;47 */
  423.  
  424.     0x0640,    /* ;48 */
  425.     0x0650,    /* ;49 */
  426.     0x0520,    /* ;50 */
  427.     0x0530,    /* ;51 */
  428.     0x0240,    /* ;52 */
  429.     0x0370,    /* ;53 */
  430.     0x0380,    /* ;54 */
  431.     0x0270,    /* ;55 */
  432.     0x0280,    /* ;56 */
  433.     0x0580,    /* ;57 */
  434.     0x0590,    /* ;58 */
  435.     0x02b0,    /* ;59 */
  436.     0x02c0,    /* ;60 */
  437.     0x05a0,    /* ;61 */
  438.     0x0660,    /* ;62 */
  439.     0x0670,    /* ;63 */
  440. /*
  441. ; Black make-up data:
  442. */
  443.     0x03c0,    /* ;64 */
  444.     0x0c80,    /* ;128 */
  445.     0x0c90,    /* ;192 */
  446.     0x05b0,    /* ;256 */
  447.     0x0330,    /* ;320 */
  448.     0x0340,    /* ;384 */
  449.     0x0350,    /* ;448 */
  450.     0x0360,    /* ;512 */
  451.     0x0368,    /* ;576 */
  452.     0x0250,    /* ;640 */
  453.     0x0258,    /* ;704 */
  454.     0x0260,    /* ;768 */
  455.     0x0268,    /* ;832 */
  456.     0x0390,    /* ;896 */
  457.     0x0398,    /* ;960 */
  458.     0x03a0,    /* ;1024 */
  459.     0x03a8,    /* ;1088 */
  460.     0x03b0,    /* ;1152 */
  461.     0x03b8,    /* ;1216 */
  462.     0x0290,    /* ;1280 */
  463.     0x0298,    /* ;1344 */
  464.     0x02a0,    /* ;1408 */
  465.     0x02a8,    /* ;1472 */
  466.     0x02d0,    /* ;1536 */
  467.     0x02d8,    /* ;1600 */
  468.     0x0320,    /* ;1664 */
  469.     0x0328,    /* ;1728 */
  470.  
  471.     0x0100,    /* ;1792 */
  472.     0x0180,    /* ;1856 */
  473.     0x01a0,    /* ;1920 */
  474.     0x0120,    /* ;1984 */
  475.     0x0130,    /* ;2048 */
  476.     0x0140,    /* ;2112 */
  477.     0x0150,    /* ;2176 */
  478.     0x0160,    /* ;2240 */
  479.     0x0170,    /* ;2304 */
  480.     0x01c0,    /* ;2368 */
  481.     0x01d0,    /* ;2432 */
  482.     0x01e0,    /* ;2496 */
  483.     0x01f0    /* ;2560 */
  484. };
  485.  
  486. unsigned char rev_tab[] = {
  487.     0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
  488.     0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
  489.     0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
  490.     0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
  491.     0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
  492.     0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
  493.     0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
  494.     0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
  495.     0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
  496.     0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
  497.     0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
  498.     0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
  499.     0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
  500.     0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
  501.     0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
  502.     0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
  503.     0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
  504.     0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
  505.     0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
  506.     0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
  507.     0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
  508.     0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
  509.     0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
  510.     0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
  511.     0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
  512.     0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
  513.     0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
  514.     0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
  515.     0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
  516.     0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
  517.     0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
  518.     0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
  519. };
  520.  
  521. #ifdef __PROTOTYPES__
  522. #    define PROTO_ARGS(X)    X
  523. #else
  524. #    define PROTO_ARGS(X)    ()
  525. #endif
  526.  
  527. /*
  528.  *    This appears before each page in a FAX file
  529.  */
  530.  
  531. /**********************************************************************/
  532. /*
  533.  *    Generic fax output library
  534.  */
  535. /**********************************************************************/
  536.  
  537. static long curpos=-1;
  538.  
  539. private FAXOUT    *faxout_open_fp PROTO_ARGS((FILE *));
  540. private int    faxout_begin_page PROTO_ARGS((FAXOUT *, int));
  541. private int    faxout_end_page PROTO_ARGS((FAXOUT *));
  542. private void    tofax(FAXOUT *, unsigned char *);
  543.  
  544. void xstart_line(FAXOUT *fax);
  545. void xend_line(FAXOUT *fax);
  546.  
  547. /*************************************************************************
  548.     Redefine the device descriptor
  549.  *************************************************************************/
  550. struct gx_device_xfax_s {
  551.     gx_device_common;
  552.     gx_prn_device_common;
  553.     FAXOUT    *fax;
  554. };
  555. typedef struct gx_device_xfax_s gx_device_xfax;
  556.  
  557. /* The device descriptor */
  558. #define X_DPI 204
  559. #define Y_DPI 196
  560. #define LINE_SIZE ((X_DPI * 85 / 10 + 7) / 8)    /* bytes per line */
  561.  
  562. private dev_proc_open_device(xfax_prn_open);
  563. private dev_proc_print_page(xfax_print_page);
  564. private dev_proc_close_device(xfax_prn_close);
  565.  
  566. gx_device_procs xfax_std_procs =
  567.     prn_procs(xfax_prn_open, gdev_prn_output_page, xfax_prn_close);
  568.  
  569. gx_device_xfax gs_xfaxhigh_device =
  570. {    prn_device_std_body(
  571.         gx_device_xfax,
  572.         xfax_std_procs,
  573.         "xfaxhigh",
  574.         85,            /* width_10ths, 8.5" */
  575.         110,            /* height_10ths, 11" */
  576.         X_DPI, Y_DPI,
  577.         0,0,0,0,        /* margins */
  578.         1,
  579.         xfax_print_page
  580.     )
  581. };
  582.  
  583. gx_device_xfax gs_xfaxlow_device =
  584. {    prn_device_std_body(
  585.         gx_device_xfax,
  586.         xfax_std_procs,
  587.         "xfaxlow",
  588.         85,            /* width_10ths, 8.5" */
  589.         110,            /* height_10ths, 11" */
  590.         X_DPI, Y_DPI/2,
  591.         0,0,0,0,        /* margins */
  592.         1,
  593.         xfax_print_page
  594.     )
  595. };
  596.  
  597. /*************************************************************************
  598.     Driver entry points
  599.  *************************************************************************/
  600.  
  601. private int
  602. xfax_prn_open(gx_device *pdev)
  603. {
  604.     gx_device_xfax    *ddev = (gx_device_xfax *) pdev;
  605.     gx_device_printer    *ppdev = (gx_device_printer *) pdev;
  606.     int rc;
  607.  
  608.     rc = gdev_prn_open(pdev);
  609.     if((ddev->file=fopen(ppdev->fname, "rb+"))==NULL)
  610.         ddev->file=fopen(ppdev->fname, "wb+");
  611.     ddev->fax = faxout_open_fp(ddev->file);
  612.     return (rc);
  613. }
  614.  
  615. private int
  616. xfax_print_page(gx_device_printer *pdev, FILE *prn_stream)
  617. {
  618.     gx_device_xfax    *ddev = (gx_device_xfax *) pdev;
  619.     char data[LINE_SIZE + 4];
  620.     int lnum;
  621.     int line_size;
  622.     FAXOUT *fax = ddev->fax;
  623.  
  624.     /* For some odd reason, the file isn't open until now */
  625.     fax->fp = prn_stream;    
  626.     faxout_begin_page(fax, ddev->y_pixels_per_inch > 120);
  627.  
  628.     line_size = gdev_mem_bytes_per_scan_line( (gx_device *) pdev);
  629.     for ( lnum = 0; lnum < pdev->height; lnum++ )
  630.     {
  631.         memset(data, 0, LINE_SIZE + 4);
  632.         gdev_prn_copy_scan_lines(pdev, lnum, (byte *)data, line_size);
  633.         tofax(fax, data);
  634.     }
  635.  
  636.     /* Make sure each page is 2238 scanlines */
  637.     memset(data, 0, LINE_SIZE + 4);
  638.     for ( ; lnum < 2238; lnum++ ) {
  639.         tofax(fax, data);
  640.     }
  641.  
  642.     faxout_end_page(fax);
  643.  
  644.     return 0;
  645. }
  646.  
  647. private int
  648. xfax_prn_close(gx_device *pdev)
  649. {
  650.     return(gdev_prn_close(pdev));
  651. }
  652.  
  653. /*************************************************************************
  654.     Internal routines
  655.  *************************************************************************/
  656.  
  657. /************************Coding FAX Routines*************************/
  658. /*
  659.  *    faxp = faxout_open_fp(fp);
  660.  *    faxout_begin_page(faxp, resolution);
  661.  *    for(;;) tofax(faxp, linebuf);
  662.  *    faxout_end_page(faxp);
  663.  */
  664.  
  665. long
  666. read_header(FILE *fp)
  667. {
  668.     int i;
  669.     char header[16];
  670.     long offset=0;
  671.  
  672.     if(fp==NULL) return -1;
  673.  
  674.     if(fread(header, sizeof(header), 1, fp)!=sizeof(header)) {
  675.         fseek(fp, 0L, SEEK_SET);
  676.         return -1;
  677.     }
  678.     for(i=7;i>=4;i--) {
  679.         offset=offset<<8;
  680.         offset+=header[i];
  681.     }
  682.  
  683.     return(offset);
  684. }
  685.  
  686. private FAXOUT    *
  687. faxout_open_fp(FILE *fp)
  688. {
  689.     long startpos;
  690.     register FAXOUT    *faxp;
  691.  
  692.     faxp = (FAXOUT *) malloc(sizeof(*faxp));
  693.  
  694.     faxp->fp = fp;
  695.     faxp->fax_byte = 0;
  696.     faxp->fax_weight = 0x80;
  697.     faxp->pages = 0;
  698.  
  699.     if((startpos=read_header(fp))>=0) {
  700.         fseek(faxp->fp, startpos, SEEK_SET);
  701.     }
  702.  
  703.     return (faxp);
  704. }
  705.  
  706. private int
  707. faxout_begin_page(FAXOUT *faxp, int resolution)
  708. {
  709.     char header[16];
  710.  
  711.     if(curpos==-1) {
  712.         fseek(faxp->fp, 0L, SEEK_END);
  713.     }
  714.  
  715.     memset(header, 0, sizeof(header));
  716.     header[0]=0x10; header[1]=0x10; header[2]=0x10;
  717.     /* High resolution */
  718.     header[3]=0x01;
  719.     header[4]=0; header[5]=0; header[6]=0; header[7]=0;
  720.     header[8]='X'; header[9]='F'; header[10]='A'; header[11]='X';
  721.  
  722.     curpos=ftell(faxp->fp);
  723.  
  724.     fwrite((char*)header, 1, 16, faxp->fp);
  725.     fflush(faxp->fp);
  726.  
  727.     return (0);
  728. }
  729.  
  730. private int
  731. faxout_end_page(FAXOUT *faxp)
  732. {
  733.     char header[16];
  734.     int i, n;
  735.     long pos;
  736.     long offset;
  737.  
  738.     pos=ftell(faxp->fp);
  739.     offset=pos;
  740.  
  741.     memset(header, 0, sizeof(header));
  742.     header[0]=0x10; header[1]=0x10; header[2]=0x10;
  743.     /* High resolution */
  744.     header[3]=0x01;
  745.     header[4]=0; header[5]=0; header[6]=0; header[7]=0;
  746.     header[8]='X'; header[9]='F'; header[10]='A'; header[11]='X';
  747.  
  748.     for(i=4;i<=7;i++) {
  749.         header[i]=offset&0xff;
  750.         offset>>=8;
  751.     }
  752.  
  753.     fseek(faxp->fp, curpos, SEEK_SET);
  754.     fwrite((char *)header, 1, 16, faxp->fp);
  755.     fseek(faxp->fp, pos, SEEK_SET);
  756.  
  757.     return (0);
  758. }
  759.  
  760. private unsigned char    b_run_tbl[8][256] =
  761. {
  762.   {    /* START BIT 0 */
  763.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  764.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  765.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  766.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  767.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  768.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  769.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  770.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  771.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  772.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  773.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  774.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  775.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  776.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  777.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  778.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  779.   },
  780.   {    /* START BIT 1 */
  781.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  782.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  783.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  784.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  785.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  786.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  787.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  788.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  789.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  790.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  791.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  792.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  793.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  794.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  795.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  796.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  797.   },
  798.   {    /* START BIT 2 */
  799.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  800.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  801.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  802.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  803.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  804.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  805.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  806.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  807.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  808.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  809.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  810.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  811.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  812.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  813.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  814.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  815.   },
  816.   {    /* START BIT 3 */
  817.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  818.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  819.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  820.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  821.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  822.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  823.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  824.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  825.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  826.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  827.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  828.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  829.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  830.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  831.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  832.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  833.   },
  834.   {    /* START BIT 4 */
  835.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  836.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  837.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  838.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  839.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  840.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  841.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  842.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  843.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  844.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  845.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  846.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  847.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  848.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  849.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  850.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  851.   },
  852.   {    /* START BIT 5 */
  853.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  854.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  855.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  856.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  857.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  858.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  859.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  860.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  861.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  862.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  863.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  864.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  865.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  866.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  867.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  868.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  869.   },
  870.   {    /* START BIT 6 */
  871.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  872.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  873.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  874.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  875.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  876.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  877.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  878.     3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 7, 
  879.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  880.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  881.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  882.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  883.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  884.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  885.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  886.     3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 7, 
  887.   },
  888.   {    /* START BIT 7 */
  889.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  890.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  891.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  892.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  893.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  894.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  895.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  896.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  897.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  898.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  899.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  900.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  901.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  902.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  903.     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
  904.     4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, 
  905.   },
  906. };
  907.  
  908. private unsigned char    w_run_tbl[8][256] =
  909. {
  910.   {    /* START BIT 0 */
  911.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  912.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  913.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  914.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  915.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  916.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  917.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  918.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  919.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  920.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  921.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  922.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  923.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  924.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  925.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  926.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  927.   },
  928.   {    /* START BIT 1 */
  929.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  930.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  931.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  932.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  933.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  934.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  935.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  936.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  937.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  938.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  939.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  940.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  941.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  942.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  943.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  944.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  945.   },
  946.   {    /* START BIT 2 */
  947.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  948.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  949.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  950.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  951.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  952.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  953.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  954.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  955.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  956.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  957.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  958.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  959.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  960.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  961.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  962.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  963.   },
  964.   {    /* START BIT 3 */
  965.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  966.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  967.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  968.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  969.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  970.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  971.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  972.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  973.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  974.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  975.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  976.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  977.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  978.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  979.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  980.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  981.   },
  982.   {    /* START BIT 4 */
  983.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  984.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  985.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  986.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  987.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  988.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  989.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  990.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  991.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  992.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  993.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  994.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  995.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  996.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  997.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  998.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  999.   },
  1000.   {    /* START BIT 5 */
  1001.     6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
  1002.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  1003.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1004.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1005.     6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
  1006.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  1007.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1008.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1009.     6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
  1010.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  1011.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1012.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1013.     6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
  1014.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  1015.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1016.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1017.   },
  1018.   {    /* START BIT 6 */
  1019.     7, 6, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 
  1020.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  1021.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  1022.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  1023.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1024.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1025.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1026.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1027.     7, 6, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 
  1028.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  1029.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  1030.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  1031.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1032.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1033.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1034.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1035.   },
  1036.   {    /* START BIT 7 */
  1037.     8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 
  1038.     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
  1039.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  1040.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  1041.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  1042.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  1043.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  1044.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  1045.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1046.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1047.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1048.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1049.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1050.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1051.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1052.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1053.   },
  1054. };
  1055.  
  1056. /*
  1057.  *    Macros to use tables in findrun.c
  1058.  *    Input is *p, bit
  1059.  *    Output is rl, *p, bit
  1060.  */
  1061.  
  1062. #define    find_black_run() \
  1063. for (rl = 0;;) \
  1064. { \
  1065.     if (run = b_run_tbl[bit][*p]) \
  1066.     { \
  1067.         rl += run; \
  1068.         bit -= run; \
  1069.         if (bit < 0 && ++p < ep) { bit=7; continue;} \
  1070.     } \
  1071.     break; \
  1072. }
  1073.  
  1074. #define    find_white_run() \
  1075. for (rl = 0;;) \
  1076. { \
  1077.     if (run = w_run_tbl[bit][*p]) \
  1078.     { \
  1079.         rl += run; \
  1080.         bit -= run; \
  1081.         if (bit < 0 && ++p < ep) { bit=7; continue;} \
  1082.     } \
  1083.     break; \
  1084. }
  1085.  
  1086. extern void store_run(unsigned int length);
  1087.  
  1088. private void
  1089. tofax(FAXOUT *faxp, unsigned char *p)
  1090. {
  1091.     unsigned char        *ep;
  1092.     register int        bit;
  1093.     register int        run;
  1094.     register int        rl;
  1095.  
  1096.     ep = p + FAX_WIDTH/8;
  1097.     bit = 7;
  1098.  
  1099.     xstart_line(faxp);
  1100.     for (;;)
  1101.     {
  1102.         find_white_run();
  1103.         store_run(rl);
  1104.         if (p >= ep) break;
  1105.  
  1106.         find_black_run();
  1107.         store_run(rl);
  1108.         if (p >= ep) break;
  1109.     }
  1110.     xend_line(faxp);
  1111. }
  1112.  
  1113.  
  1114. #define    MH_BUFFER_SIZE        0x800
  1115.  
  1116. #define    out_buffer_size        0x400
  1117.  
  1118. #define    MAKE_UP_OFFSET        64
  1119. #define    RUN_DATA_OFFSET        104
  1120. #define    MAKE_UP_DATA_OFFSET    232
  1121.  
  1122. /*
  1123. ; External variables
  1124. ;-------------------
  1125. */
  1126.  
  1127. char wbuf[2048];
  1128. int inx=0;
  1129.  
  1130. extern PAGE *Page;
  1131. extern UCHAR huffman_black[];
  1132. extern USHORT huffman1[];
  1133. extern UCHAR huffman_white[];
  1134. extern USHORT huffman2[];
  1135. extern UCHAR rev_tab[];
  1136.  
  1137. UCHAR *huffman_table;    /*    DW    ?            ;Pointer to table in use */
  1138. unsigned short int *huffman_codes;
  1139. UCHAR huffman_color = 0; /*    DB    0            ;White at start of line */
  1140.  
  1141. UCHAR shifts = 0; /*        DB    0 */
  1142. UCHAR mh_left_over = 0; /*    DB    0 */
  1143.  
  1144. UCHAR *mh_buff_ptr; /*    DW    ?            ;Pointer to buffer */
  1145.  
  1146. unsigned int xpos = 0; /*        DW    0            ;X offset along line */
  1147.  
  1148. int in_line = 0; /*    DW    0            ;Set when doing line */
  1149.  
  1150. unsigned int filekbytes = 0; /*    DW    0            ;Size of the file in Kbytes */
  1151. unsigned int filebytes = 0; /* DW 0   ;plus the remainder */
  1152.  
  1153. int line_bits = 0; /*    DW    0            ;Number of bits in the MH */
  1154.  
  1155. UCHAR *mh_buffer=NULL; /* DUP (?)    ;Buffer for MH */
  1156.  
  1157. UCHAR out_buffer[out_buffer_size]; /* DUP (?)    ;Fax file output buffer */
  1158. UCHAR *out_buffer_ptr = 0; /*    DW    0            ;Pointer to output buffer */
  1159.  
  1160. /*
  1161. ;-----------------------------------------------------------------------------
  1162. ;  store_run (length) - Store run as MH
  1163. ;-----------------------------------------------------------------------------
  1164. */
  1165.  
  1166. #if arch_is_big_endian
  1167. struct _gnu { UCHAR h; UCHAR l; };
  1168. #else
  1169. struct _gnu { UCHAR l; UCHAR h; };
  1170. #endif
  1171.  
  1172. union aq1 {
  1173.     unsigned short x;
  1174.     struct _gnu oo;
  1175. } aq1, aq2, aq3;
  1176.  
  1177. #define ax    aq1.x
  1178. #define ah    aq1.oo.h
  1179. #define al    aq1.oo.l
  1180.  
  1181. #define cx    aq2.x
  1182. #define ch    aq2.oo.h
  1183. #define cl    aq2.oo.l
  1184.  
  1185. #define bx    aq3.x
  1186. #define bh    aq3.oo.h
  1187. #define bl    aq3.oo.l
  1188.  
  1189. static UCHAR *di, *si;
  1190. static unsigned int dx;
  1191.  
  1192. /*
  1193. ;-----------------------------------------------------------------------------
  1194. ;  DO_STORE_RUN
  1195. ;
  1196. ;  Stores the next MH code (in DX) in the line buffer pointed to by DI.  The
  1197. ;  number of bits in the code are given in AL and the number of shifts
  1198. ;  required to align the code are given in CL.
  1199. ;-----------------------------------------------------------------------------
  1200. */
  1201.  
  1202. void
  1203. do_store_run()
  1204. {
  1205.     line_bits += al;
  1206.     bh=(dx&255);
  1207.     dx >>= cl;
  1208.     dx |= (mh_left_over << 8);
  1209. /*
  1210. ; (AH will hold the next incomplete byte to be incorporated).
  1211. */
  1212.     ah = (dx >> 8) & 255;
  1213.     al += cl;
  1214.  
  1215.     if (al > 7) {
  1216.     ah = dx & 255;
  1217.     *di++ = (dx >> 8) & 255;
  1218.     if (al > 15) {
  1219.         /* ???? */
  1220.         bx=bx&(0xff00);
  1221.         bx>>=cl;
  1222.         /* ???? */
  1223.         ah = bx & 255;
  1224.         *di++ = dx & 255;
  1225.     }
  1226.     }
  1227.  
  1228.     mh_left_over = ah;
  1229.     cl = al & 7;
  1230. }
  1231.  
  1232. /* * * * * * * * * * * * * * * * * * * * * * * * *
  1233. STORE_RUN
  1234. * * * * * * * * * * * * * * * * * * * * * * * * */
  1235. void
  1236. store_run(unsigned int length)
  1237. {
  1238.     register unsigned bx2;
  1239.  
  1240.     di = mh_buff_ptr;
  1241.     si = huffman_table;
  1242.     bx = length;
  1243.  
  1244. /*
  1245. ; We check that this run does not go beyond the right edge of the paper -
  1246. ; fax_page_width contains the number of pixels across the page.  If it
  1247. ; does, we clip this run to the width of the page.
  1248. */
  1249.  
  1250.     if (FAX_WIDTH<= xpos) return;
  1251.  
  1252.     ax = bx + xpos;
  1253.     if (ax >= FAX_WIDTH) {
  1254.     ax = FAX_WIDTH - xpos;
  1255.     bx = ax;
  1256.    }
  1257.  
  1258.     cl = shifts;
  1259.     xpos += bx;
  1260.     if (bx > 63) {
  1261.     bx2 = bx;
  1262.     bx >>= 6;
  1263.     bx--;
  1264.     al = huffman_table[bx + MAKE_UP_OFFSET];
  1265.     dx = huffman_codes[bx + MAKE_UP_OFFSET];
  1266.     do_store_run();
  1267.     bx = bx2 & 0x3f;
  1268.     }
  1269.  
  1270.     al = huffman_table[bx];
  1271.     dx = huffman_codes[bx];
  1272.     do_store_run();
  1273.     shifts = cl;
  1274. /*
  1275. ; (Finally, we change colour in prepartion for the next run length we're
  1276. ; given).
  1277. */
  1278.     huffman_color = ~huffman_color;
  1279.     if (huffman_color) {
  1280.         si = huffman_black;
  1281.         huffman_codes = huffman2;
  1282.     } else {
  1283.         si = huffman_white;
  1284.         huffman_codes = huffman1;
  1285.     }
  1286.  
  1287.     huffman_table = si;
  1288.     mh_buff_ptr = di;
  1289. }
  1290.  
  1291. /*
  1292. ;-----------------------------------------------------------------------------
  1293. ;  start_line () - Start a new line of MH
  1294. ;-----------------------------------------------------------------------------
  1295. */
  1296.  
  1297. void
  1298. xstart_line(FAXOUT *fax)
  1299. {
  1300.     if(mh_buffer==NULL) {
  1301.         mh_buffer=malloc(MH_BUFFER_SIZE+10);
  1302.         mh_buffer+=2;
  1303.     }
  1304.     huffman_table = huffman_white;
  1305.     huffman_codes = huffman1;
  1306.     mh_buff_ptr = mh_buffer;
  1307.     mh_left_over = 0;
  1308.     huffman_color = 0;
  1309.     shifts = 0;
  1310.     line_bits = 0;
  1311.     xpos = 0;
  1312.     in_line = ~0;
  1313. }
  1314.  
  1315. /*
  1316. ;-----------------------------------------------------------------------------
  1317. ;  end_line () - Ends line of MH
  1318. ;
  1319. ;  At the end of the a line of MH, any incomplete bytes must be written to
  1320. ;  the output stream, each byte in the line reversed and the checksum
  1321. ;  calculated.
  1322. ;-----------------------------------------------------------------------------
  1323. */
  1324.  
  1325. void
  1326. xend_line(FAXOUT *fax)
  1327. {
  1328.     UCHAR *dxp;
  1329.     short int *shp, n;
  1330.  
  1331.     if (!in_line) return;
  1332.  
  1333.     di = mh_buff_ptr;
  1334.  
  1335.     if (line_bits & 7)
  1336.     *di++ = mh_left_over;
  1337.  
  1338.     dxp = di;
  1339.     di = mh_buffer;
  1340.     cx = 0; cl = 0; ch = 0; bh = 0;
  1341.  
  1342.     do {
  1343.         *di = rev_tab[*di];
  1344.         cl -= *di++;
  1345.     } while (di < dxp);
  1346.  
  1347.     shp = (short int *)(mh_buffer - 2);
  1348.  
  1349.     dxp = mh_buffer - 2;
  1350.     *dxp++ = 0x00;
  1351.     *dxp++ = 0x80;
  1352.  
  1353.     n=(int)(di-(UCHAR *)shp);
  1354.  
  1355.     fwrite(shp, n, 1, fax->fp);
  1356.  
  1357.     in_line = 0;
  1358. }
  1359.