home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 317_01 / g3sdecod.c < prev    next >
C/C++ Source or Header  |  1990-06-18  |  30KB  |  1,162 lines

  1. /*    $Id: g3sdecod.c 1.2 90/06/09 18:22:19 marking Exp $
  2.  *
  3.  NAME
  4.  *    g3sdecod.c -- decode group 3 data using nested if statements
  5.  *
  6.  TYPE
  7.  *    C procedures
  8.  *
  9.  SYNOPSIS
  10.  *    char    g3i_initialize (short image_width, short image_length);
  11.  *    char    g3i_decode (void);    -- for standard group 3
  12.  *    char    g3i_decode_T (void);    -- for TIFF g3 with no EOL chars
  13.  *
  14.  DESCRIPTION
  15.  *    In order to acquire data from the image and to return run lengths and
  16.  *    new line information, these routines invoke procedures provided by the
  17.  *    caller. These caller-provided procedures are invoked throught pointers
  18.  *    which have been stuffed by the caller with the procedure addresses.
  19.  *    To acquire a new data byte, g3i_decode () and g3i_decode_T () call
  20.  *    (*p_decode_next_byte) (). To report the decoding of a black or white
  21.  *    run, the routines (*p_decode_black) () or (*p_decode_white) () are
  22.  *    called.
  23.  *
  24.  RETURNS
  25.  *    Initialization always returns zero.
  26.  *
  27.  *    For decoding,
  28.  *        0    end of image reached
  29.  *        -1    on error (bad data)
  30.  *    The decode loop will be prematurely terminated if decode_return is
  31.  *    set to not zero, and the value of decode_return will be returned.
  32.  *    No code here does this, but it might be useful under certain
  33.  *    circumstances.
  34.  *
  35.  LEGAL
  36.  *    Copyright 1989, 1990 Michael P. Marking, Post Office Box 8039,
  37.  *    Scottsdale, Arizona 85252-8039. All rights reserved.
  38.  *
  39.  *    License is granted by the copyright holder to distribute and use this
  40.  *    code without payment of royalties or the necessity of notification as
  41.  *    long as this notice (all the text under "LEGAL") is included.
  42.  *
  43.  *    Reference: $Id: g3sdecod.c 1.2 90/06/09 18:22:19 marking Exp $
  44.  *
  45.  *    This program is offered without any warranty of any kind. It includes
  46.  *    no warranty of merchantability or fitness for any purpose. Testing and
  47.  *    suitability for any use are the sole responsibility of the user.
  48.  * 
  49.  HISTORY
  50.  *    $Log:    g3sdecod.c $
  51.  * Revision 1.2  90/06/09  18:22:19  marking
  52.  * clean up comments for release
  53.  * 
  54.  * Revision 1.1  89/06/30  17:00:00  marking
  55.  * Initial revision
  56.  * 
  57.  *
  58.  NOTES
  59.  *
  60.  PORTABILITY
  61.  *    Tested using Microsoft C 5.1. Some memory models may not work due to
  62.  *    the large decoding arrays.
  63.  *
  64.  *    There is a non-portable use of "global" variables in the file g3g4.h,
  65.  *    about which a minority of compilers will justifiably complain. Certain
  66.  *    variables are declared in g3g4.h without extern keywords. Strictly
  67.  *    speaking, they should be declared extern in all but one module, but
  68.  *    that would require complication of g3g4.h. If it gets past your
  69.  *    compiler and linker, you can probably ignore it.
  70.  *
  71.  SEE ALSO
  72.  *    g4sdecod.c -- decode group 4 image using nested if statements
  73.  *
  74.  INFORMATION
  75.  *    Although there is no support offered with this program, the author will
  76.  *    endeavor to correct errors. Updates will also be made available from
  77.  *    time to time.
  78.  *
  79.  *    Contact: Michael P. Marking, Post Office Box 8039, Scottsdale, Arizona
  80.  *    85252-8039 USA. Replies are not guaranteed to be swift. Beginning
  81.  *    July 1990, e-mail may be sent to uunet!ipel!marking.
  82.  *
  83.  *    Also beginning in July 1990, this code will be archived at the
  84.  *    ipel!phoenix BBS in file g3g4.zoo. The 24-hour telephone number
  85.  *    for 300/1200/2400 is (602)274-0462. When logging in, specify user
  86.  *    "public", system "bbs", and password "public".
  87.  *
  88.  *    This code is also available from the C Users Group in volume 317.
  89.  *
  90.  */
  91.  
  92. #include "g3g4.h"
  93.  
  94. /* #define TRACE 1 */
  95. #define TRACE_BEGIN 0
  96. #define TRACE_END 30000
  97.  
  98. static short bit_number, code_byte;
  99. static unsigned char color, current_row, mode, next_state;
  100. static short column_limit, row_limit;
  101. static short row_number = 0, column_number;
  102.  
  103. extern unsigned char horiz_mode [] [256];
  104. extern unsigned char horiz_mode_next_state [] [256];
  105.  
  106. static short new_row (void);
  107. static short decode_white_run (void);
  108. static short decode_black_run (void);
  109. static short decode_white_word (void);
  110. static short decode_black_word (void);
  111. static char next_bit (void);
  112.  
  113. static char decode_return;
  114.  
  115. /* g3i_decode () successively invokes (*p_decode_next_byte) () for each byte
  116.    of the encoded image, and calls (*p_decode_white) () or (*p_decode_black) ()
  117.    as required to return the image contents on a run-by-run basis. */
  118.  
  119. char g3i_decode ()
  120. {
  121.   short runlength;
  122.   while (!decode_return)
  123.   {
  124.     if (color == WHITE)
  125.     {
  126.       runlength = decode_white_run ();
  127.       if (runlength == -2) return (-1);
  128.       else if (runlength == -1)
  129.       {
  130.     if (new_row ()) return (0);
  131.       }
  132.       else
  133.       {
  134.     column_number += runlength;
  135.     (*p_decode_white) ((short) runlength);
  136.     color = BLACK;
  137.       }
  138.     }
  139.     else
  140.     {
  141.       runlength = decode_black_run ();
  142.       if (runlength == -2) return (-1);
  143.       else if (runlength == -1)
  144.       {
  145.     if (new_row ()) return (0);
  146.       }
  147.       else
  148.       {
  149.     column_number += runlength;
  150.     (*p_decode_black) ((short) runlength);
  151.     color = WHITE;
  152.       }
  153.     }
  154.   }
  155.   return (decode_return);
  156. }
  157.  
  158. /* special version for TIFF files with no EOL characters */
  159. char g3i_decode_T ()
  160. {
  161.   short runlength;
  162.   while (!decode_return)
  163.   {
  164.     if (color == WHITE)
  165.     {
  166.       runlength = decode_white_run ();
  167.       if (runlength == -2) return (-1);
  168.       else if (runlength == -1)
  169.       {
  170.     if (new_row ()) return (0);
  171.       }
  172.       else
  173.       {
  174.     column_number += runlength;
  175.     (*p_decode_white) ((short) runlength);
  176.     color = BLACK;
  177.     if (column_number >= column_limit)
  178.     {
  179.       if (new_row ()) return (0);
  180.       bit_number = 0;
  181.     }
  182.       }
  183.     }
  184.     else
  185.     {
  186.       runlength = decode_black_run ();
  187.       if (runlength == -2) return (-1);
  188.       else if (runlength == -1)
  189.       {
  190.     if (new_row ()) return (0);
  191.       }
  192.       else
  193.       {
  194.     column_number += runlength;
  195.     (*p_decode_black) ((short) runlength);
  196.     color = WHITE;
  197.     if (column_number >= column_limit)
  198.     {
  199.       if (new_row ()) return (0);
  200.       bit_number = 0;
  201.     }
  202.       }
  203.     }
  204.   }
  205.   return (decode_return);
  206. }
  207.  
  208. /* g3i_initialize () is called to set up to decode a new image.  All of the
  209.    static data (flags, etc) for g3i_decode () are initialized, allowing the
  210.    decoding of multiple images as long as g3i_initialize () is
  211.    called before each. */
  212. char g3i_initialize (short image_width, short image_length)
  213. {
  214.   color = WHITE;
  215.   bit_number= 0;
  216.   column_limit = image_width;
  217.   row_limit = image_length;
  218.   row_number = 0;
  219.   column_number = 0;
  220.   decode_return = 0;
  221.   return (0);
  222. }
  223.  
  224. static short new_row ()
  225. {
  226.   if (column_number)
  227.   {
  228.     (*p_decode_new_row) ();
  229.     color = WHITE;
  230.     if (++row_number >= row_limit) return (-1);
  231.     column_number = 0;
  232.     return (0);
  233.   }
  234.   else return (0);
  235. }
  236.  
  237. static char next_bit (void)
  238. {
  239.   char value;
  240.   static unsigned char decode_mask [8] =
  241.     { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
  242.   if (!bit_number) code_byte = (*p_decode_next_byte) ();
  243.   if ((unsigned char) code_byte & decode_mask [bit_number]) value = 1;
  244.   else value = 0;
  245.   if (++bit_number > 7) bit_number = 0;
  246.   return (value);
  247. }
  248.  
  249. /*
  250.  *    return the length of the next run, assumed to be black
  251.  */
  252.  
  253. static short decode_black_run ()
  254. {
  255.   short cumulative = 0, code_value;
  256.   for (;;)
  257.   {
  258.     code_value = decode_black_word ();
  259.     cumulative += code_value;
  260.     if (code_value < 0) return (code_value);
  261.     if (code_value < 64) return (cumulative);
  262.   }
  263. }
  264.  
  265. static short decode_black_word ()
  266. {
  267.   if (next_bit ()) /* 1... */
  268.   {
  269.     if (next_bit ()) /* 11 */ return (2);
  270.     else /* 10 */ return (3);
  271.   }
  272.   else /* 0... */
  273.   {
  274.     if (next_bit ()) /* 01... */
  275.     {
  276.       if (next_bit ()) /* 011 */ return (4);
  277.       else /* 010 */ return (1);
  278.     }
  279.     else /* 00... */
  280.     {
  281.       if (next_bit ()) /* 001... */
  282.       {
  283.     if (next_bit ()) /* 0011 */ return (5);
  284.     else /* 0010 */ return (6);
  285.       }
  286.       else /* 000... */
  287.       {
  288.     if (next_bit ()) /* 0001... */
  289.     {
  290.       if (next_bit ()) /* 0001 1 */ return (7);
  291.       else /* 0001 0... */
  292.       {
  293.         if (next_bit ()) /* 0001 01 */ return (8);
  294.         else /* 0001 00 */ return (9);
  295.       }
  296.     }
  297.     else /* 0000... */
  298.     {
  299.       if (next_bit ()) /* 0000 1... */
  300.       {
  301.         if (next_bit ()) /* 0000 11... */
  302.         {
  303.           if (next_bit ()) /* 0000 111 */ return (12);
  304.           else /* 0000 110... */
  305.           {
  306.         if (next_bit ()) /* 0000 1101... */
  307.         {
  308.           if (next_bit ()) /* 0000 1101 1... */
  309.           {
  310.             if (next_bit ()) /* 0000 1101 11 */ return (0);
  311.             else /* 0000 1101 10... */
  312.             {
  313.               if (next_bit ()) /* 0000 1101 101... */
  314.               {
  315.             if (next_bit ()) /* 0000 1101 1011 */ return (43);
  316.             else /* 0000 1101 1010 */ return (42);
  317.               }
  318.               else /* 0000 1101 100 */ return (21);
  319.             }
  320.           }
  321.           else /* 0000 1101 0... */
  322.           {
  323.             if (next_bit ()) /* 0000 1101 01... */
  324.             {
  325.               if (next_bit ()) /* 0000 1101 011... */
  326.               {
  327.             if (next_bit ()) /* 0000 1101 0111 */ return (39);
  328.             else /* 0000 1101 0110 */ return (38);
  329.               }
  330.               else /* 0000 1101 010... */
  331.               {
  332.             if (next_bit ()) /* 0000 1101 0101 */ return (37);
  333.             else /* 0000 1101 0100 */ return (36);
  334.               }
  335.             }
  336.             else /* 0000 1101 00... */
  337.             {
  338.               if (next_bit ()) /* 0000 1101 001... */
  339.               {
  340.             if (next_bit ()) /* 0000 1101 0011 */ return (35);
  341.             else /* 0000 1101 0010 */ return (34);
  342.               }
  343.               else /* 0000 1101 000 */ return (20);
  344.             }
  345.           }
  346.         }
  347.         else /* 0000 1100... */
  348.         {
  349.           if (next_bit ()) /* 0000 1100 1... */
  350.           {
  351.             if (next_bit ()) /* 0000 1100 11... */
  352.             {
  353.               if (next_bit ()) /* 0000 1100 111 */ return (19);
  354.               else /* 0000 1100 110... */
  355.               {
  356.             if (next_bit ()) /* 0000 1100 1101 */ return (29);
  357.             else /* 0000 1100 1100 */ return (28);
  358.               }
  359.             }
  360.             else /* 0000 1100 10... */
  361.             {
  362.               if (next_bit ()) /* 0000 1100 101.. */
  363.               {
  364.             if (next_bit ()) /* 0000 1100 1011 */ return (27);
  365.             else /* 0000 1100 1010 */ return (26);
  366.               }
  367.               else /* 0000 1100 100... */
  368.               {
  369.             if (next_bit ()) /* 0000 1100 1001 */ return (192);
  370.             else /* 0000 1100 1000 */ return (128);
  371.               }
  372.             }
  373.           }
  374.           else /* 0000 1100 0 */ return (15);
  375.         }
  376.           }
  377.         }
  378.         else /* 0000 10... */
  379.         {
  380.           if (next_bit ()) /* 0000 101 */ return (11);
  381.           else /* 0000 100 */ return (10);
  382.         }
  383.       }
  384.       else /* 0000 0... */
  385.       {
  386.         if (next_bit ()) /* 0000 01... */
  387.         {
  388.           if (next_bit ()) /* 0000 011... */
  389.           {
  390.         if (next_bit ()) /* 0000 0111 */ return (14);
  391.         else /* 0000 0110... */
  392.         {
  393.           if (next_bit ()) /* 0000 0110 1... */
  394.           {
  395.             if (next_bit ()) /* 0000 0110 11... */
  396.             {
  397.               if (next_bit ()) /* 0000 0110 111 */ return (22);
  398.               else /* 0000 0110 110... */
  399.               {
  400.             if (next_bit ()) /* 0000 0110 1101 */ return (41);
  401.             else /* 0000 0110 1100 */ return (40);
  402.               }
  403.             }
  404.             else /* 0000 0110 10... */
  405.             {
  406.               if (next_bit ()) /* 0000 0110 101... */
  407.               {
  408.             if (next_bit ()) /* 0000 0110 1011 */ return (33);
  409.             else /* 0000 0110 1010 */ return (32);
  410.               }
  411.               else /* 0000 0110 100... */
  412.               {
  413.             if (next_bit ()) /* 0000 0110 1001 */ return (31);
  414.             else /* 0000 0110 1000 */ return (30);
  415.               }
  416.             }
  417.           }
  418.           else /* 0000 0110 0... */
  419.           {
  420.             if (next_bit ()) /* 0000 0110 01... */
  421.             {
  422.               if (next_bit ()) /* 0000 0110 011... */
  423.               {
  424.             if (next_bit ()) /* 0000 0110 0111 */ return (63);
  425.             else /* 0000 0110 0110 */ return (62);
  426.               }
  427.               else /* 0000 0110 010... */
  428.               {
  429.             if (next_bit ()) /* 0000 0110 0101 */ return (49);
  430.             else /* 0000 0110 0100 */ return (48);
  431.               }
  432.             }
  433.             else /* 0000 0110 00 */ return (17);
  434.           }
  435.         }
  436.           }
  437.           else /* 0000 010... */
  438.           {
  439.         if (next_bit ()) /* 0000 0101... */
  440.         {
  441.           if (next_bit ()) /* 0000 0101 1... */
  442.           {
  443.             if (next_bit ()) /* 0000 0101 11 */ return (16);
  444.             else /* 0000 0101 10... */
  445.             {
  446.               if (next_bit ()) /* 0000 0101 101... */
  447.               {
  448.             if (next_bit ()) /* 0000 0101 1011 */ return (256);
  449.             else /* 0000 0101 1010 */ return (61);
  450.               }
  451.               else /* 0000 0101 100... */
  452.               {
  453.             if (next_bit ()) /* 0000 0101 1001 */ return (58);
  454.             else /* 0000 0101 1000 */ return (57);
  455.               }
  456.             }
  457.           }
  458.           else /* 0000 0101 0... */
  459.           {
  460.             if (next_bit ()) /* 0000 0101 01... */
  461.             {
  462.               if (next_bit ()) /* 0000 0101 011... */
  463.               {
  464.             if (next_bit ()) /* 0000 0101 0111 */ return (47);
  465.             else /* 0000 0101 0110 */ return (46);
  466.               }
  467.               else /* 0000 0101 010... */
  468.               {
  469.             if (next_bit ()) /* 0000 0101 0101 */ return (45);
  470.             else /* 0000 0101 0100 */ return (44);
  471.               }
  472.             }
  473.             else /* 0000 0101 00... */
  474.             {
  475.               if (next_bit ()) /* 0000 0101 001... */
  476.               {
  477.             if (next_bit ()) /* 0000 0101 0011 */ return (51);
  478.             else /* 0000 0101 0010 */ return (50);
  479.               }
  480.               else /* 0000 0101 000 */ return (23);
  481.             }
  482.           }
  483.         }
  484.         else /* 0000 0100 */ return (13);
  485.           }
  486.         }
  487.         else /* 0000 00... */
  488.         {
  489.           if (next_bit ()) /* 0000 001... */
  490.           {
  491.         if (next_bit ()) /* 0000 0011... */
  492.         {
  493.           if (next_bit ()) /* 0000 0011 1... */
  494.           {
  495.             if (next_bit ()) /* 0000 0011 11 */ return (64);
  496.             else /* 0000 0011 10... */
  497.             {
  498.               if (next_bit ()) /* 0000 0011 101... */
  499.               {
  500.             if (next_bit ()) /* 0000 0011 1011... */
  501.             {
  502.               if (next_bit ()) /* 0000 0011 1011 1 */ return (1216);
  503.               else /* 0000 0011 1011 0 */ return (1152);
  504.             }
  505.             else /* 0000 0011 1010... */
  506.             {
  507.               if (next_bit ()) /* 0000 0011 1010 1 */ return (1088);
  508.               else /* 0000 0011 1010 0 */ return (1024);
  509.             }
  510.               }
  511.               else /* 0000 0011 100... */
  512.               {
  513.             if (next_bit ()) /* 0000 0011 1001... */
  514.             {
  515.               if (next_bit ()) /* 0000 0011 1001 1 */ return (960);
  516.               else /* 0000 0011 1001 0 */ return (896);
  517.             }
  518.             else /* 0000 0011 1000 */ return (54);
  519.               }
  520.             }
  521.           }
  522.           else /* 0000 0011 0... */
  523.           {
  524.             if (next_bit ()) /* 0000 0011 01... */
  525.             {
  526.               if (next_bit ()) /* 0000 0011 011... */
  527.               {
  528.             if (next_bit ()) /* 0000 0011 0111 */ return (53);
  529.             else /* 0000 0011 0110... */
  530.             {
  531.               if (next_bit ()) /* 0000 0011 0110 1 */ return (576);
  532.               else /* 0000 0011 0110 0 */ return (512);
  533.             }
  534.               }
  535.               else /* 0000 0011 010... */
  536.               {
  537.             if (next_bit ()) /* 0000 0011 0101 */ return (448);
  538.             else /* 0000 0011 0100 */ return (384);
  539.               }
  540.             }
  541.             else /* 0000 0011 00... */
  542.             {
  543.               if (next_bit ()) /* 0000 0011 001... */
  544.               {
  545.             if (next_bit ()) /* 0000 0011 0011 */ return (320);
  546.             else /* 0000 0011 0010... */
  547.             {
  548.               if (next_bit ()) /* 0000 0011 0010 1 */ return (1728);
  549.               else /* 0000 0011 0010 0 */ return (1664);
  550.             }
  551.               }
  552.               else /* 0000 0011 000 */ return (25);
  553.             }
  554.           }
  555.         }
  556.         else /* 0000 0010... */
  557.         {
  558.           if (next_bit ()) /* 0000 0010 1... */
  559.           {
  560.             if (next_bit ()) /* 0000 0010 11... */
  561.             {
  562.               if (next_bit ()) /* 0000 0010 111 */ return (24);
  563.               else /* 0000 0010 110... */
  564.               {
  565.             if (next_bit ()) /* 0000 0010 1101... */
  566.             {
  567.               if (next_bit ()) /* 0000 0010 1101 1 */ return (1600);
  568.               else /* 0000 0010 1101 0 */ return (1536);
  569.             }
  570.             else /* 0000 0010 1100 */ return (60);
  571.               }
  572.             }
  573.             else /* 0000 0010 10... */
  574.             {
  575.               if (next_bit ()) /* 0000 0010 101... */
  576.               {
  577.             if (next_bit ()) /* 0000 0010 1011 */ return (59);
  578.             else /* 0000 0010 1010... */
  579.             {
  580.               if (next_bit ()) /* 0000 0010 1010 1 */ return (1472);
  581.               else /* 0000 0010 1010 0 */ return (1408);
  582.             }
  583.               }
  584.               else /* 0000 0010 100... */
  585.               {
  586.             if (next_bit ()) /* 0000 0010 1001... */
  587.             {
  588.               if (next_bit ()) /* 0000 0010 1001 1 */ return (1344);
  589.               else /* 0000 0010 1001 0 */ return (1280);
  590.             }
  591.             else /* 0000 0010 1000 */ return (56);
  592.               }
  593.             }
  594.           }
  595.           else /* 0000 0010 0... */
  596.           {
  597.             if (next_bit ()) /* 0000 0010 01... */
  598.             {
  599.               if (next_bit ()) /* 0000 0010 011... */
  600.               {
  601.             if (next_bit ()) /* 0000 0010 0111 */ return (55);
  602.             else /* 0000 0010 0110... */
  603.             {
  604.               if (next_bit ()) /* 0000 0010 0110 1 */ return (832);
  605.               else /* 0000 0010 0110 0 */ return (768);
  606.             }
  607.               }
  608.               else /* 0000 0010 010... */
  609.               {
  610.             if (next_bit ()) /* 0000 0010 0101... */
  611.             {
  612.               if (next_bit ()) /* 0000 0010 0101 1 */ return (704);
  613.               else /* 0000 0010 0101 0 */ return (640);
  614.             }
  615.             else /* 0000 0010 0100 */ return (52);
  616.               }
  617.             }
  618.             else /* 0000 0010 00 */ return (18);
  619.           }
  620.         }
  621.           }
  622.           else /* 0000 000... */
  623.           {
  624.         if (next_bit ()) /* 0000 0001... */
  625.         {
  626.           if (next_bit ()) /* 0000 0001 1... */
  627.           {
  628.             if (next_bit ()) /* 0000 0001 11... */
  629.             {
  630.               if (next_bit ()) /* 0000 0001 111... */
  631.               {
  632.             if (next_bit ()) /* 0000 0001 1111 */ return (2560);
  633.             else /* 0000 0001 1110 */ return (2496);
  634.               }
  635.               else /* 0000 0001 110... */
  636.               {
  637.             if (next_bit ()) /* 0000 0001 1101 */ return (2432);
  638.             else /* 0000 0001 1100 */ return (2368);
  639.               }
  640.             }
  641.             else /* 0000 0001 10... */
  642.             {
  643.               if (next_bit ()) /* 0000 0001 101 */ return (1920);
  644.               else /* 0000 0001 100 */ return (1856);
  645.             }
  646.           }
  647.           else /* 0000 0001 0... */
  648.           {
  649.             if (next_bit ()) /* 0000 0001 01... */
  650.             {
  651.               if (next_bit ()) /* 0000 0001 011... */
  652.               {
  653.             if (next_bit ()) /* 0000 0001 0111 */ return (2304);
  654.             else /* 0000 0001 0110 */ return (2240);
  655.               }
  656.               else /* 0000 0001 010... */
  657.               {
  658.             if (next_bit ()) /* 0000 0001 0101 */ return (2176);
  659.             else /* 0000 0001 0100 */ return (2112);
  660.               }
  661.             }
  662.             else /* 0000 0001 00... */
  663.             {
  664.               if (next_bit ()) /* 0000 0001 001... */
  665.               {
  666.             if (next_bit ()) /* 0000 0001 0011 */ return (2048);
  667.             else /* 0000 0001 0010 */ return (1984);
  668.               }
  669.               else /* 0000 0001 000 */ return (1792);
  670.             }
  671.           }
  672.         }
  673.         else /* 0000 0000... */
  674.         {
  675.           if (next_bit ()) /* 0000 0000 1 */ return (INVALID_CODE);
  676.           else /* 0000 0000 0... */
  677.           {
  678.             if (next_bit ()) /* 0000 0000 01 */ return (INVALID_CODE);
  679.             else /* 0000 0000 00... */
  680.             {
  681.               if (next_bit ()) /* 0000 0000 001 */
  682.                             return (INVALID_CODE);
  683.               else /* 0000 0000 000... */
  684.               {
  685.             if (next_bit ()) /* 0000 0000 0001 */ return (EOL_CODE);
  686.             else /* 0000 0000 0000 */ /* return (INVALID_CODE); */
  687.               /* normally this is an invalid code, but *if* we
  688.                 assume the file has no errors, then we can
  689.                 greatly simplify pad stripping with the
  690.                 following... */
  691.               for (;;) if (next_bit ()) return (EOL_CODE);
  692.               }
  693.             }
  694.           }
  695.         }
  696.           }
  697.         }
  698.       }
  699.     }
  700.       }
  701.     }
  702.   }
  703. }
  704.  
  705. /*
  706.  *    return the length of the next run, assumed to be white 
  707.  */
  708.  
  709. static short decode_white_run ()
  710. {
  711.   short cumulative = 0, code_value;
  712.   for (;;)
  713.   {
  714.     code_value = decode_white_word ();
  715.     cumulative += code_value;
  716.     if (code_value < 0) return (code_value);
  717.     if (code_value < 64) return (cumulative);
  718.   }
  719. }
  720.  
  721. static short decode_white_word ()
  722. {
  723.   if (next_bit ()) /* 1... */
  724.   {
  725.     if (next_bit ()) /* 11... */
  726.     {
  727.       if (next_bit ()) /* 111... */
  728.       {
  729.     if (next_bit ()) /* 1111 */ return (7);
  730.     else /* 1110 */ return (6);
  731.       }
  732.       else /* 110... */
  733.       {
  734.     if (next_bit ()) /* 1101... */
  735.     {
  736.       if (next_bit ()) /* 1101 1 */ return (64);
  737.       else /* 1101 0... */
  738.       {
  739.         if (next_bit ()) /* 1101 01 */ return (15);
  740.         else /* 1101 00 */ return (14);
  741.       }
  742.     }
  743.     else /* 1100 */ return (5);
  744.       }
  745.     }
  746.     else /* 10... */
  747.     {
  748.       if (next_bit ()) /* 101... */
  749.       {
  750.     if (next_bit ()) /* 1011 */ return (4);
  751.     else /* 1010... */
  752.     {
  753.       if (next_bit ()) /* 10101... */
  754.       {
  755.         if (next_bit ()) /* 101011 */ return (17);
  756.         else /* 101010 */ return (16);
  757.       }
  758.       else /* 10100 */ return (9);
  759.     }
  760.       }
  761.       else /* 100... */
  762.       {
  763.     if (next_bit ()) /* 1001... */
  764.     {
  765.       if (next_bit ()) /* 10011 */ return (8);
  766.       else /* 10010 */ return (128);
  767.     }
  768.     else /* 1000 */ return (3);
  769.       }
  770.     }
  771.   }
  772.   else /* 0... */
  773.   {
  774.     if (next_bit ()) /* 01... */
  775.     {
  776.       if (next_bit ()) /* 011... */
  777.       {
  778.     if (next_bit ()) /* 0111 */ return (2);
  779.     else /* 0110... */
  780.     {
  781.       if (next_bit ()) /* 01101... */
  782.       {
  783.         if (next_bit ()) /* 011011... */
  784.         {
  785.           if (next_bit ()) /* 0110111 */ return (256);
  786.           else /* 0110110... */
  787.           {
  788.         if (next_bit ()) /* 01101101... */
  789.         {
  790.           if (next_bit ()) /* 011011011 */ return (1408);
  791.           else /* 011011010 */ return (1344);
  792.         }
  793.             else /* 01101100... */
  794.         {
  795.           if (next_bit ()) /* 011011001 */ return (1280);
  796.           else /* 011011000 */ return (1216);
  797.         }
  798.           }
  799.         }
  800.         else /* 011010... */
  801.         {
  802.           if (next_bit ()) /* 0110101... */
  803.           {
  804.         if (next_bit ()) /* 01101011... */
  805.         {
  806.           if (next_bit ()) /* 011010111 */ return (1152);
  807.           else /* 011010110 */ return (1088);
  808.         }
  809.         else /* 01101010... */
  810.         {
  811.           if (next_bit ()) /* 011010101 */ return (1024);
  812.           else /* 011010100 */ return (960);
  813.         }
  814.           }
  815.           else /* 0110100... */
  816.           {
  817.         if (next_bit ()) /* 01101001... */
  818.         {
  819.           if (next_bit ()) /* 011010011 */ return (896);
  820.           else /* 011010010 */ return (832);
  821.         }
  822.         else /* 01101000 */ return (576);
  823.           }
  824.         }
  825.       }
  826.       else /* 01100... */
  827.       {
  828.         if (next_bit ()) /* 011001... */
  829.         {
  830.           if (next_bit ()) /* 0110011... */
  831.           {
  832.         if (next_bit ()) /* 01100111 */ return (640);
  833.         else /* 01100110 */
  834.         {
  835.           if (next_bit ()) /* 011001101 */ return (768);
  836.           else /* 011001100 */ return (704);
  837.         }
  838.           }
  839.           else /* 0110010 */
  840.           {
  841.         if (next_bit ()) /* 01100101 */ return (512);
  842.         else /* 01100100 */ return (448);
  843.           }
  844.         }
  845.         else /* 011000 */ return (1664);
  846.       }
  847.     }
  848.       }
  849.       else /* 010... */
  850.       {
  851.     if (next_bit ()) /* 0101... */
  852.     {
  853.       if (next_bit ()) /* 01011... */
  854.       {
  855.         if (next_bit ()) /* 010111 */ return (192);
  856.         else /* 010110... */
  857.         {
  858.           if (next_bit ()) /* 0101101... */
  859.           {
  860.         if (next_bit ()) /* 01011011 */ return (58);
  861.         else /* 01011010 */ return (57);
  862.           }
  863.           else /* 0101100... */
  864.           {
  865.         if (next_bit ()) /* 01011001 */ return (56);
  866.         else /* 01011000 */ return (55);
  867.           }
  868.         }
  869.       }
  870.       else /* 01010... */
  871.       {
  872.         if (next_bit ()) /* 010101... */
  873.         {
  874.           if (next_bit ()) /* 0101011 */ return (25);
  875.           else /* 0101010... */
  876.           {
  877.         if (next_bit ()) /* 01010101 */ return (52);
  878.         else /* 01010100 */ return (51);
  879.           }
  880.         }
  881.         else /* 010100... */
  882.         {
  883.           if (next_bit ()) /* 0101001... */
  884.           {
  885.         if (next_bit ()) /* 01010011 */ return (50);
  886.         else /* 01010010 */ return (49);
  887.           }
  888.           else /* 0101000 */ return (24);
  889.         }
  890.       }
  891.     }
  892.     else /* 0100... */
  893.     {
  894.       if (next_bit ()) /* 01001... */
  895.       {
  896.         if (next_bit ()) /* 010011... */
  897.         {
  898.           if (next_bit ()) /* 0100111 */ return (18);
  899.           else /* 0100110... */
  900.           {
  901.         if (next_bit ()) /* 01001101... */
  902.         {
  903.           if (next_bit ()) /* 010011011 */ return (1728);
  904.           else /* 010011010 */ return (1600);
  905.         }
  906.         else /* 01001100... */
  907.         {
  908.           if (next_bit ()) /* 010011001 */ return (1536);
  909.           else /* 010011000 */ return (1472);
  910.         }
  911.           }
  912.         }
  913.         else /* 010010... */
  914.         {
  915.           if (next_bit ()) /* 0100101... */
  916.           {
  917.         if (next_bit ()) /* 01001011 */ return (60);
  918.         else /* 01001010 */ return (59);
  919.           }
  920.           else /* 0100100 */ return (27);
  921.         }
  922.       }
  923.       else /* 01000 */ return (11);
  924.     }
  925.       }
  926.     }
  927.     else /* 00... */
  928.     {
  929.       if (next_bit ()) /* 001... */
  930.       {
  931.     if (next_bit ()) /* 0011... */
  932.     {
  933.       if (next_bit ()) /* 00111 */ return (10);
  934.       else /* 00110... */
  935.       {
  936.         if (next_bit ()) /* 001101... */
  937.         {
  938.           if (next_bit ()) /* 0011011... */
  939.           {
  940.         if (next_bit ()) /* 00110111 */ return (384);
  941.         else /* 00110110 */ return (320);
  942.           }
  943.           else /* 0011010... */
  944.           {
  945.         if (next_bit ()) /* 00110101 */ return (0);
  946.         else /* 00110100 */ return (63);
  947.           }
  948.         }
  949.         else /* 001100... */
  950.         {
  951.           if (next_bit ()) /* 0011001... */
  952.           {
  953.         if (next_bit ()) /* 00110011 */ return (62);
  954.         else /* 00110010 */ return (61);
  955.           }
  956.           else /* 0011000 */ return (28);
  957.         }
  958.       }
  959.     }
  960.     else /* 0010... */
  961.     {
  962.       if (next_bit ()) /* 00101... */
  963.       {
  964.         if (next_bit ()) /* 001011... */
  965.         {
  966.           if (next_bit ()) /* 0010111 */ return (21);
  967.           else /* 0010110... */
  968.           {
  969.         if (next_bit ()) /* 00101101 */ return (44);
  970.         else /* 00101100 */ return (43);
  971.           }
  972.         }
  973.         else /* 001010... */
  974.         {
  975.           if (next_bit ()) /* 0010101... */
  976.           {
  977.         if (next_bit ()) /* 00101011 */ return (42);
  978.         else /* 00101010 */ return (41);
  979.           }
  980.           else /* 0010100... */
  981.           {
  982.         if (next_bit ()) /* 00101001 */ return (40);
  983.         else /* 00101000 */ return (39);
  984.           }
  985.         }
  986.       }
  987.       else /* 00100... */
  988.       {
  989.         if (next_bit ()) /* 001001... */
  990.         {
  991.           if (next_bit ()) /* 0010011 */ return (26);
  992.           else /* 0010010... */
  993.           {
  994.         if (next_bit ()) /* 00100101 */ return (54);
  995.         else /* 00100100 */ return (53);
  996.           }
  997.         }
  998.         else /* 001000 */ return (12);
  999.       }
  1000.     }
  1001.       }
  1002.       else /* 000... */
  1003.       {
  1004.     if (next_bit ()) /* 0001... */
  1005.     {
  1006.       if (next_bit ()) /* 00011... */
  1007.       {
  1008.         if (next_bit ()) /* 000111 */ return (1);
  1009.         else /* 000110... */
  1010.         {
  1011.           if (next_bit ()) /* 0001101... */
  1012.           {
  1013.         if (next_bit ()) /* 00011011 */ return (32);
  1014.         else /* 00011010 */ return (31);
  1015.           }
  1016.           else /* 0001100 */ return (19);
  1017.         }
  1018.       }
  1019.       else /* 00010... */
  1020.       {
  1021.         if (next_bit ()) /* 000101... */
  1022.         {
  1023.           if (next_bit ()) /* 0001011... */
  1024.           {
  1025.         if (next_bit ()) /* 00010111 */ return (38);
  1026.         else /* 00010110 */ return (37);
  1027.           }
  1028.           else /* 0001010... */
  1029.           {
  1030.         if (next_bit ()) /* 00010101 */ return (36);
  1031.         else /* 00010100 */ return (35);
  1032.           }
  1033.         }
  1034.         else /* 000100... */
  1035.         {
  1036.           if (next_bit ()) /* 0001001... */
  1037.           {
  1038.         if (next_bit ()) /* 00010011 */ return (34);
  1039.         else /* 00010010 */ return (33);
  1040.           }
  1041.           else /* 0001000 */ return (20);
  1042.         }
  1043.       }
  1044.     }
  1045.     else /* 0000... */
  1046.     {
  1047.       if (next_bit ()) /* 00001... */
  1048.       {
  1049.         if (next_bit ()) /* 000011 */ return (13);
  1050.         else /* 000010... */
  1051.         {
  1052.           if (next_bit ()) /* 0000101... */
  1053.           {
  1054.         if (next_bit ()) /* 00001011 */ return (48);
  1055.         else /* 00001010 */ return (47);
  1056.           }
  1057.           else /* 0000100 */ return (23);
  1058.         }
  1059.       }
  1060.       else /* 00000... */
  1061.       {
  1062.         if (next_bit ()) /* 000001... */
  1063.         {
  1064.           if (next_bit ()) /* 0000011 */ return (22);
  1065.           else /* 0000010... */
  1066.           {
  1067.         if (next_bit ()) /* 00000101 */ return (46);
  1068.         else /* 00000100 */ return (45);
  1069.           }
  1070.         }
  1071.         else /* 000000... */
  1072.         {
  1073.           if (next_bit ()) /* 0000001... */
  1074.           {
  1075.         if (next_bit ()) /* 00000011 */ return (30);
  1076.         else /* 00000010 */ return (29);
  1077.           }
  1078.           else /* 0000 000... */
  1079.           {
  1080.         if (next_bit ()) /* 0000 0001... */
  1081.         {
  1082.           if (next_bit ()) /* 0000 0001 1... */
  1083.           {
  1084.             if (next_bit ()) /* 0000 0001 11... */
  1085.             {
  1086.               if (next_bit ()) /* 0000 0001 111... */
  1087.               {
  1088.             if (next_bit ()) /* 0000 0001 1111 */ return (2560);
  1089.             else /* 0000 0001 1110 */ return (2496);
  1090.               }
  1091.               else /* 0000 0001 110... */
  1092.               {
  1093.             if (next_bit ()) /* 0000 0001 1101 */ return (2432);
  1094.             else /* 0000 0001 1100 */ return (2368);
  1095.               }
  1096.             }
  1097.             else /* 0000 0001 10... */
  1098.             {
  1099.               if (next_bit ()) /* 0000 0001 101 */ return (1920);
  1100.               else /* 0000 0001 100 */ return (1856);
  1101.             }
  1102.           }
  1103.           else /* 0000 0001 0... */
  1104.           {
  1105.             if (next_bit ()) /* 0000 0001 01... */
  1106.             {
  1107.               if (next_bit ()) /* 0000 0001 011... */
  1108.               {
  1109.             if (next_bit ()) /* 0000 0001 0111 */ return (2304);
  1110.             else /* 0000 0001 0110 */ return (2240);
  1111.               }
  1112.               else /* 0000 0001 010... */
  1113.               {
  1114.             if (next_bit ()) /* 0000 0001 0101 */ return (2176);
  1115.             else /* 0000 0001 0100 */ return (2112);
  1116.               }
  1117.             }
  1118.             else /* 0000 0001 00... */
  1119.             {
  1120.               if (next_bit ()) /* 0000 0001 001... */
  1121.               {
  1122.             if (next_bit ()) /* 0000 0001 0011 */ return (2048);
  1123.             else /* 0000 0001 0010 */ return (1984);
  1124.               }
  1125.               else /* 0000 0001 000 */ return (1792);
  1126.             }
  1127.           }
  1128.         }
  1129.         else /* 0000 0000... */
  1130.         {
  1131.           if (next_bit ()) /* 0000 0000 1 */ return (INVALID_CODE);
  1132.           else /* 0000 0000 0... */
  1133.           {
  1134.             if (next_bit ()) /* 0000 0000 01 */ return (INVALID_CODE);
  1135.             else /* 0000 0000 00... */
  1136.             {
  1137.               if (next_bit ()) /* 0000 0000 001 */
  1138.                             return (INVALID_CODE);
  1139.               else /* 0000 0000 000... */
  1140.               {
  1141.             if (next_bit ()) /* 0000 0000 0001 */ return (EOL_CODE);
  1142.             else /* 0000 0000 0000 */ /* return (INVALID_CODE); */
  1143.               /* normally this is an invalid code, but *if* we
  1144.                 assume the file has no errors, then we can
  1145.                 greatly simplify pad stripping with the
  1146.                 following... */
  1147.               for (;;) if (next_bit ()) return (EOL_CODE);
  1148.               }
  1149.             }
  1150.           }
  1151.         }
  1152.           }
  1153.         }
  1154.       }
  1155.     }
  1156.       }
  1157.     }
  1158.   }
  1159. }
  1160.  
  1161. /*    end $RCSfile: g3sdecod.c $ */
  1162.