• Aucun résultat trouvé

Converting Length

Dans le document File Formats (Page 79-82)

JPEG Huffman Coding

Algorithm 6.3 Converting Length

Counts to Code Lengths

Procedure CountsToLengths (LENGTHCOUNTS [1..MAXIMUMLENGTH], MAXIMUMLENGTH,

CODELENGTHS [0..255]) Begin

INDEX = 1

For II = 1 To MAXIMUMLENGTH Do Begin

For JJ = 1 To LENGTHCOUNTS [II] Do Begin

CODELENGTHS [INDEX] = II INDEX = INDEX + 1 End

End End

Huffman Coding in JPEG

In the JPEG modes we will be using in this book, the possible Huffman values are the integers 0 to 255. We have seen that, depending upon how the Huffman coding algorithm is applied, different codes can be generated from the same sym-bol values and frequency data. The JPEG standard does not specify exactly how Huffman codes are generated. The Huffman codes for values in JPEG files do not have to be optimal. In fact, as we shall see in a moment, there are times when they cannot be. It is possible to define the Huffman tables so that the least fre-quently used values have the shortest code. The result would be terrible com-pression, but it is not illegal.

The JPEG standard puts two restrictions on Huffman codes. A Huffman-encoded bit-string consisting of all 1-bits must be a prefix to a longer code. This means that no Huffman code can consist of all 1-bits. In Table 6.4 and Figure 6.1 you can see that the only code consisting of all 1-bits is the one generated for the period. If we got rid of the period, the problem with all 1-bit codes would go away.

Using Algorithm 6.1, the only code that will consist of all 1-bits is the one for the last value when the values are sorted by code length. If we insert a dummy value with a usage frequency of 1 at the start of Huffman coding and sort the

val-72 JPEG Huffman Coding

ues so that the dummy value winds up at the end of the list, the dummy value will be the only one assigned a code consisting of all 1-bits. At the end of the coding process we simply discard the dummy code. In the palindrome example, our dummy code is the period.

Limiting Code Lengths

Limiting code lengths to 16 bits is a little more difficult, even though in practice the Huffman code generation process does not often result in codes longer than that. On those rare occasions when the Huffman algorithm generates longer codes you must use a less than optimal coding.

The longest Huffman code length we generated for the palindrome was 5, but suppose we wanted to limit the maximum code length to 4. We could easily accomplish this by shifting nodes in the tree. Figure 6.2 is a representation of the tree generated for the results in Figure 6.1, showing how nodes could be moved to reduce the maximum code length to 4.

It is much simpler to do the transform shown in Figure 6.2 on an array of length counts than on an actual tree structure. The basic method is to find a sym-bol with a code length of at least 2 less than the code you want to reduce. You

Figure 6.2

Shifting Tree Nodes to Reduce Code Length

Decoding Huffman Codes

When we read a JPEG image file we get a set of counts for each Huffman code and a list of symbol values sorted by Huffman code. From this information we need to set up tables for converting the Huffman codes in the input stream to the correct symbol. Since Huffman codes vary in length, we have to read them one bit at a time. Our first problem is how to tell when we have read a complete Huffman code.

The obvious method for decoding Huffman values is to create a binary tree containing the values arranged according to their codes. Start at the root of the tree and, using the value of bits read from the input stream to determine the path, search for the values in the tree.

A simpler method to implement is to use the list of values sorted by Huffman code in conjunction with an array of data structures with one element per Huffman code length. Each structure contains the minimum and maximum Huffman code for a given length and the index of the first value with a Huffman code of that length in the sorted value array. Figure 6.3 shows how these struc-tures would look for the Huffman table in Figure 6.1.

Algorithm 6.5 shows how to decode Huffman-encoded values using this data structure. This algorithm relies on the Huffman codes for a given length being consecutive.

Procedure LimitTo16Bits (LENGTHCOUNTS [1..32]) Begin

For II = 32 DownTo 17 Begin

While LENGTHCOUNTS [II] <> 0 Do Begin

JJ = II - 2

While LENGTHCOUNTS [JJ] = 0 Do JJ = JJ - 1

// Replace a tree node with a value.

LENGTHCOUNTS [II] = LENGTHCOUNTS [II] - 2 LENGTHCOUNTS [II - 1] = LENGTHCOUNTS [II-1] + 1 // Replace a value with a tree node.

LENGTHCOUNTS [JJ + 1] = LENGTHCOUNTS [JJ + 1] + 2 LENGTHCOUNTS [JJ] = LENGTHCOUNTS [JJ] - 1

End End End Algorithm 6.4

Limiting Code Lengths to 16 Bits

replace the symbol with a branch and then shift all the longer codes across. The complete process is shown in Algorithm 6.4.

Decoding Huffman Codes 73

74 JPEG Huffman Coding

While CODELENGTH <= 16 Do Begin

CODE = CODE LeftShift 1

CODE = CODE Or NextBitFromInput () CODELENGTH = CODELENGTH + 1

If CODE <= MAXCODE [CODELENGTH] Then Begin

INDEX = FIRSTCODE [CODELENGTH] + CODE - MINCODE [CODELENGTH]

Return VALUES [INDEX]

End End

// Error If We Get Here End

Using the Huffman codes in Figure 6.3, suppose the next bits in the input stream are 110100. During the next passes through the loop, the code value and maximum code lengths will be:

Length

Dans le document File Formats (Page 79-82)