c JPEG Huffman coding understanding

Table 1:

6e48c86766244a5890ad09b10c8ce8a9.jpeg

 

014ada73a1e44b1a9c4a5580e3c732b8.jpeg18333d7da0a54986b01f5efc105d4cfe.jpegd7a061295f59474ab1658977307c03a5.jpeg

 0 ,0 ,1 ,4 ,3 ,1 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,6 ,0 ,4 ,5 ,7 ,1 ,2 ,3 ,8 ,
----------------
16 ,0 ,2 ,1 ,2 ,5 ,2 ,4 ,3 ,5 ,6 ,4 ,4 ,5 ,5 ,0 ,0 ,1 ,2 ,3 ,4 ,17 ,0 ,5 ,18 ,33 ,49 ,6 ,65 ,19 ,34 ,81 ,97 ,7 ,113 ,-127 ,20 ,50 ,-111 ,-95 ,-16 ,35 ,66 ,82 ,114 ,-79 ,-63 ,21 ,-126 ,-47 ,- 15 ,22 ,51 ,67 ,98 ,8 ,36 ,-110 ,-94 ,-62 ,23 ,52 ,53 ,-93 ,-78 ,
------ ----------
1 ,0 ,2 ,3 ,1 ,1 ,1 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,3 ,4 ,0 ,2 ,5 ,1 ,6 ,7 ,
----------------
17 ,0 ,2 ,2 ,1 ,3 ,2 ,4 ,4 ,4 ,5 ,5 ,1 ,0 ,0 ,0 ,0 ,1 ,2 ,0 ,3 ,17 ,4 ,18 ,33 ,5 ,49 ,19 ,34 ,65 ,81 ,50 ,97 ,-111 ,-95 ,6 ,113 ,-47 ,-16 ,20 ,35 ,51 ,-127 ,-79 ,21 ,66 ,114 ,-63 ,-31 ,-15 ,
----------------
This is the finished product jpg Fragment of Huofuman

0: is the brightness dc table, 16: is the brightness ac table

1: is the chromaticity dc table, 17: is the chromaticity ac table

As you can see from the table above, the demo images online may be incorrect. For example, the 16 table: How to know that there are 2 two-digit binary numbers by looking up the table. One out of three. The more important evidence is that the converted numbers starting from bit 17 should be increasing, rather than being arranged in different sizes.

I understand:

1. Coding must first collect all values ​​of b in (a, b) and their frequency of occurrence. (a,b) is the Z-sorted number pair. Don't worry about a.

2. Sort all b values ​​from high to low according to frequency of occurrence

3. Generate Huffman code 

Such as: 0 ,0 ,1 ,4 ,3 ,1 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,6 ,0 ,4 ,5 ,7 ,1 ,2 , 3,8,

       The first 0 is the table id, which is not encoded. The remaining 0 is: 0,1,4,3,1,0,0,0,0,0,0,0,0,0,0,0,6,0 ,4 ,5 ,7 ,1 ,2 ,3 ,8 . The first 16 bits represent the number of binary digits: 0,1,4,3,1,0,0,0,0,0,0,0,0,0,0,0. How many digits are converted in each digit? number. If the first bit is 0, it means there is no one bit; the second bit is 1, which means there is one binary xx, xxx has 4, xxxx has 3, and xxxxx has 1. There are a total of 1+4+3+1=9 numbers. These 9 numbers are the 9 numbers starting from the 17th digit: 6, 0, 4,5,7,1,2,3,8. These 9 numbers are b in the pair (a, b).

4: Encoded binary: 2 bits: 1 00 It is stipulated that it must start from 0, 00 represents 6

                              3 digits 4 (00+1) × 2 = 010, 011, 100, 101 // 010 represent 0, 011 == 4

                                        //   100==5 , 101== 7

                              4 digits: 3  (101+1)×2=1100(1), 1101(2), 1110(3)

                              5 digits: 1  (1110+1)×2=11110 (8)

5: So after conversion: the code for 6 is 2:00

                              0   ==        3:010

                              4   ==       3:011

                               5  ==        3:100

                               7   ==       3:101

                                1  ==      4:1100

                               2   ==      4:1101

                               3  ==      4:1110

                               8  ==     5 :11110

Therefore, most articles on the Internet are incorrect.

7. Then check the (0 numbers/binary digits) table defined by Huffman, which is the table in Figure 3. This table is provided on the official website of Huffman, and does not need to be provided with the jpg file header.

    For example, after Z sorting (0, 5), the binary calculated according to step 5 5==3:100

        Then (0, 5) ---> (0, 3) 100, then check the 0 number/binary digit table defined by Huffman, (0, 3) is

        100, Finally (0, 5) is converted to 100 100. These 6 bits represent (0, 5).

  Of course, with this calculation table, decoding is very easy. If you read a three-digit binary number as 100, it means 5

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/m0_59802969/article/details/134972089