TIFF 6.0 Specification
Final—June 3, 1992
Read Pixel 1 into K. Does
ΩK
(<7><7>) exist in the string table? No, so we write
the code associated with
Ω
to output (write <7> to output) and add
ΩK
(<7><7>)
to the table as entry 258. Store K (<7>) into
Ω.
Note that although we have added
the string consisting of Pixel 0 and Pixel 1 to the table, we “re-use” Pixel 1 as the
beginning of the next string.
Back at the top of the loop, we read Pixel 2 into K. Does
ΩK
(<7><7>) exist in the
string table? Yes, the entry we just added, entry 258, contains exactly <7><7>. So
we add K to the end of
Ω
so that
Ω
is now <7><7>.
Back at the top of the loop, we read Pixel 3 into K. Does
ΩK
(<7><7><8>) exist
in the string table? No, so we write the code associated with
Ω
(<258>) to output
and then add
ΩK
to the table as entry 259. Store K (<8>) into
Ω.
Back at the top of the loop, we read Pixel 4 into K. Does
ΩK
(<8><8>) exist in the
string table? No, so we write the code associated with
Ω
(<8>) to output and then
add
ΩK
to the table as entry 260. Store K (<8>) into
Ω.
Continuing, we get the following results:
After reading:
Pixel 0
Pixel 1
Pixel 2
Pixel 3
Pixel 4
Pixel 5
Pixel 6
Pixel 7
Pixel 8
<7>
<258>
<8>
<8>
<258>
<6>
258: <7><7>
259: <7><7><8>
260:<8><8>
261: <8><7>
262: <7><7><6>
263: <6><6>
We write to output:
And add table entry:
WriteCode() also requires some explanation. In our example, the output code
stream, <7><258><8><8><258><6> should be written using as few bits as pos-
sible. When we are just starting out, we can use 9-bit codes, since our new string
table entries are greater than 255 but less than 512.
After adding table entry 511,
switch to 10-bit codes (i.e., entry 512 should be a 10-bit code.) Likewise, switch to
11-bit codes after table entry 1023, and 12-bit codes after table entry 2047.
We
will arbitrarily limit ourselves to 12-bit codes, so that our table can have at most
4096 entries. The table should not be any larger.
Whenever you add a code to the output stream, it “counts” toward the decision
about bumping the code bit length. This is important when writing the last code
word before an EOI code or ClearCode, to avoid code length errors.
What happens if we run out of room in our string table? This is where the
ClearCode comes in. As soon as we use entry 4094, we write out a (12-bit)
ClearCode. (If we wait any longer to write the ClearCode, the decompressor
might try to interpret the ClearCode as a 13-bit code.) At this point, the compres-
sor reinitializes the string table and then writes out 9-bit codes again.
Note that whenever you write a code and add a table entry,
Ω
is not left empty. It
contains exactly one character. Be careful not to lose it when you write an end-of-
table ClearCode. You can either write it out as a 12-bit code before writing the
ClearCode, in which case you need to do it right after adding table entry 4093, or
60