Listing 1

Listing 1. The bit-wide technique: a straightforward but slow way
decode a Group 3 image

#define INVALID_CODE -1
#define EOL_CODE -2

decode_image () {
  ...
  while (!end_of_file) {
    /* loop for each row in the image */
    ...
    while (!end_of_row) {
      /* loop for each pair of runs in the row */
      int runlength;
      while (63 < (runlength = decode_white_run ())) {
        if (runlength == EOL_CODE)end_of_row = TRUE;
        else if (runlength == INVALID_CODE)
          ... error condition ...;
        else /* a legal white run */
          spit_out_white_pixels (runlength);
      }
      while (63 < (runlength = decode_black_run ())) {
        if (runlength == EOL_CODE) end_of_row = TRUE;
        else if (runlength == INVALID_CODE}
          ... error condition ...;
        else /* a legal black run */
          spit_out_black_pixels (runlength);
      }
    }
  }
}

int decode_white_run () {
  if (next_bit ()) /* 1... */ {
    if (next_bit ()) /* 11... */ {
      if (next_bit ()) /* 111... */ {
        if (next_bit ()) /* 1111 */ return (7);
        else /* 1110 */ return (6);
      }
      else /* 110... */ {
        if (next_bit ()) /* 1101... */ {
          if (next_bit ()) /* 1101 1 */ return (64);
          else /* 1101 0... */ {
            if (next_bit ()) /* 1101 01 */ return (15);
... omit a lot of ifs and elses and braces ...
      if (next_bit ()) /* 0000 0000 001 */ return (INVALID_CODE);
      else /* 0000 0000 000... */ {
        if (next_bit ()) /* 0000 0000 0001 */ return (EOL_CODE);
        else /* 0000 0000 0000 */ return (INVALID_CODE);
... omit a lot of braces ... 
}

int decode_black_run () {
... a lot like the preceding ... }