super sahar
super sahar

Reputation: 425

JPG huffman DECODE stucks

I'm trying to decode the JPG file, the entire header part is correctly read. During the reading of the photo body itself (SOS, 0xFFDA) at some point, the function for finding the correspondence in the Huffman table goes into an infinite loop. If you look at the file in the hex editor, you can find the following sequence of bytes at the error location:

7F FF 00 61

FF 00 => FF

7F FF 61

that in binary code

0111 1111 1111 1111 0110 0001

The first bit has already been used by the past MCU, now it's 15 'ones' in a row and then zero. In the corresponding Huffman table, the maximum code is 8 ones and one zero. I concluded that the byte 7F was just filled with ones until the end. But this is not the end of the file. How can I find out when I need to skip a byte, and when not?

Upvotes: 1

Views: 424

Answers (1)

super sahar
super sahar

Reputation: 425

Algorithm of decode AC coefficient was not very clear. I changed loop condition from (index != 63) to (index <= 63) because of EOB that hit the 64th element of MCU and everything began to work. https://www.w3.org/Graphics/JPEG/itu-t81.pdf (page 106) enter image description here

Upvotes: 1

Related Questions