The Decoding Algorithm
Upon first glance at the finite state diagram in Figure 1, it may seem that it would be difficult to create a decoding procedure. For example, if you are in the WW state, and the next bit is a 1, how do you know whether to do a transition to WB or BW? The answer to this is to recognize that every arc out of the BW state begins with 0 and every arc out of WB begins with 1. Thus, if you are in the WW state, and the next bit is 1, followed by a 0, you know to go to the BW state. If the next bit is 1, followed by a 1, you know to go to the WB state.
In writing the decoding program it was necessary to have two ways of reading the next bit in the data stream. The first way reads the bit and "consumes" it, i.e. increments the bit pointer to point at the next bit. The other way does not "consume" it. Below are four statements which show how to decode fax data. The numbers in parentheses are not to be consumed, that is to say they will be read again in making the next transition.
If I am in state BW (2) and the next bits are:
0 (0): go to BW
0111: go to BB
010 (1): go to WB
0100: go to WW
If I am in state WB (1) and the next bits are:
1 (1): go to WB
1000: go to WW
101 (0): go to BW
1011: go to BB
If I am in state WW (0), then first go through the run length
algorithm, then if the next bits are:
0: go to BB
1 (0): go to BW
1 (1): go to WB
If I am in state BB (3), then first go through the run length
algorithm, then if the next bits are:
0: go to WW
1 (0): go to BW
1 (1): go to WB
For the run length algorithm, remember, look at the next n bits, where n is the length of either the black or white run length word, REVERSE the bits, and output that many BB's or WW's (depending on whether black or white run). If the field is full, increment the size of the word, and get that many bits more, i.e. get the next n+1 bits, etc. Also, the run length word length can be decremented according to the rules given in section III.
You always go through the run length even if there is only one WW or BB, in this case, the run field will be 0.
Let us look at the first example given in section III. Suppose we want to decode the bits: 110111100010100100100101000... (we have already reversed the run lengths to make things easier).
We are in state 1 (WB) and the black run length word length is 2 and the white length is 3. We get these initial values either from the block header, or by remembering them from the previous transitions if this is not the start of the block. According to our rules, we would parse this string as follows:
1(1) 1011 11 000 1(0) 0100 100 1(0) 0(0) 010(1) 1000...
The numbers in parentheses are numbers that were read but not "consumed", thus the next number in the sequence is the same as the one in parentheses. First, we see a 1 and that the next bit is a 1, this means that we go to WB. Then we have a 1011 which means to go to BB. Then we do a run, we have a 11 followed by a 000 which means the black run length gets incremented by 1 (it is now 3) and we get 3 MORE BB's. Now we have a 1 followed by 0 which means go to BW. Next a 0100 which is WW. Then we have a run, 100, which means four more WW's. We keep going like this and we get the original bit pattern given in the first example of section III.
It is important to always start fresh when dealing with each block. There are many reasons for this. The first is that sometimes blocks are dropped, and you can recover from this if you resynchronize at the start of each block. Also, if at the end of the previous block, there is about to be a transition, instead of making it at the beginning of the next block, the fax machine gives the new state in the header of the next block and goes from there. Thus it is important to always start at whatever state is given in the header, and to align yourself at the current X position given there also.
Sometimes, while decoding a block, a bit pattern will occur which does not correspond to any transition. If this happens, the rest of the block may be bad and should be discarded.
The decoding program decodes the fax data block by block until it comes to an END command in the data, or runs out of data.