home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_06 / 8n06051a < prev    next >
Text File  |  1989-06-11  |  2KB  |  80 lines

  1.  
  2. Listing 2.  The byte-wide decoding routines
  3.  
  4. /* decode routines for group 3 files
  5.  
  6.   char g3_init (void) - initializes the g3 decode routines
  7.   char g3_reset (void) - may be called from within one of
  8.     the routines g3_white (), g3_black (), g3_error (), or
  9.     g3_EOL () to set the decoding state to zero
  10.   char g3_decode (void) - decodes a group 3 file
  11.  
  12.   the above routines return nonzero on error, else zero
  13.  
  14. USER MUST PROVIDE THE FOLLOWING ROUTINES:
  15.  
  16.   short g3_next_byte (void) - returns the next byte in the
  17.     image, -1 at EOF
  18.   void g3_white (short runlength) - writes a white run of
  19.     the specified length to the output file
  20.   void g3_black (short runlength) - writes a black run of
  21.     the specified length to the output file
  22.   void g3_EOL (void) - processes the end-of-line character
  23.   void g3_error (void) - process an invalid code
  24. */
  25.  
  26. static char initialized = 0, reset = 0;
  27. static unsigned char action_table [220299];
  28. static long branch_table [216] [256];
  29. static short state = 0;
  30. static short column, row;
  31.  
  32. extern unsigned char reverse_byte [];
  33.  
  34. char g3_init () {
  35.   if (!initialized) /* if this is the first time this
  36.     routine is called */ {
  37.     long bytes_read;
  38.     int action_handle, jump_handle;
  39.     ... open the file containing action_table and read
  40.       into memory ...
  41.     ... open the file containing branch_table and read
  42.        into memory ...
  43.     initialized = 1;
  44.   }
  45.   state = 0; reset = 0;
  46.   return (0);
  47. }
  48.  
  49. char g3_reset () {
  50.   state = 0; reset = 1;
  51.   return (0);
  52. }
  53.  
  54. char g3_decode () {
  55.   char rcode;
  56.   unsigned char *p_action;
  57.   short g3byte;
  58.   if (!initialized)
  59.     if (rcode = g3_init ()) return (rcode);
  60.   while ((g3byte = g3_next_byte ()) != -1)
  61.     /* while not end-of-image */ {
  62.     unsigned char code_byte;
  63.     p_action = &action_table [branch_table [state] [g3byte]];
  64.     while ((code_byte = *(p_action++)) != 255 && !reset)
  65.       /* until state change code */ {
  66.       if (!code_byte) g3_EOL ();
  67.       else if (code_byte == 1) g3_error ();
  68.       else if (code_byte < 67) g3_white (code_byte-2);
  69.       else if (code_byte < 106) g3_white ((code_byte-65)*64);
  70.       else if (code_byte < 171) g3_black (code_byte-106);
  71.       else g3_black ((code_byte-169)*64);
  72.     }
  73.     state = *p_action;
  74.     if (reset) /* if reset flag set during above */ {
  75.       state = 0; reset = 0;
  76.     }
  77.   }
  78. }
  79.  
  80.