3

I'm making jpeg encoder in C++. I successfully create Huffman tree, but how do I generate huffman codes from tree? One way I tried was to assign 0 to left branch and 1 to right branch, just like in the picture, but there is a problem in this approach, because one element will be coded with all ones (like sibol E in picture bellow which is coded with 11), but jpeg standard doesn't allow huffman codes with all ones.

enter image description here

MrD
  • 2,423
  • 3
  • 33
  • 57
  • Why you cannot use `libjpeg` and "interrupt" process of creating image? You would be able to check your results with original results step by step than. *(anyway, respect for trying to implement this)* – Vyktor Jan 31 '12 at 15:14
  • I have already made decoder, and encoder (this is my task) but when I test my picture in JPEG snoop I see that my huffman codes in my picture are not good. So this step is final step :) – MrD Jan 31 '12 at 15:21
  • If you need to extract only DC elements, then you need few functions, I can help you do that, or I can send you my functions. – MrD Jan 31 '12 at 15:42
  • I'll be glad, it's school project and I cannot help them because it's all working on my linux desktop. – Vyktor Jan 31 '12 at 15:44
  • @Vyktor please can you write your email again? Sry – MrD Jan 31 '12 at 17:11

3 Answers3

1

If you can't use a string of all 1s, then you can't necessarily get an optimal code (optimal within certain constraints, I mean).

If you append a "0" to the string that's all "1"s, then it won't be all "1"s any more. You'll still have a prefix code, just not an optimal one. So that might suffice, but I don't know whether that's what jpeg encoders are supposed to do. I'd have thought there would be a standard solution.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • it would be complicated to use strings, rather I use int or short int variable. – MrD Jan 31 '12 at 15:43
  • I mean string in the general sense that Huffman code points are strings of symbols from an alphabet (usually a binary alphabet). I don't care how your program stores them :-) – Steve Jessop Jan 31 '12 at 15:55
  • you can interpret an integral variable as a _string_ of 16 or 32 bits for the purpose of this answer: so appending a zero is identical to left-shifting your integer (low bit is zero-filled). – Useless Jan 31 '12 at 15:56
  • Maybe I misunderstood you, but if I code it like so, decoder won't use that algorithm, and I will not get good result. – MrD Jan 31 '12 at 16:11
  • 1
    @Mr.M: I don't know anything about the jpeg format, I'm just telling you one way to generate a Huffman-like code that has the additional restriction you mention "doesn't use an all-1 code point". If jpeg requires that the code be generated from the tree in a particular way, then presumably one of the JPEG standard documents describes it. – Steve Jessop Jan 31 '12 at 16:38
1

JPEG is using a fixed tree based on statistics. So you'll never get an optimal code. The fixed tree has to be used because it is the only way of distributing the Huffman tree in an efficient way (otherwise you would have to keep the tree within the file and this makes the file much bigger). I assume the tree is described within the standard documents.

Sebastian
  • 8,046
  • 2
  • 34
  • 58
  • In jpeg file there is no exactly huffman tree, but there is huffman table, starting from FFC4 marker, from which you can easily make huffman tree. I need inverse process. – MrD Jan 31 '12 at 15:51
  • But anyway the generated Huffman tree should not lead to binary strings with all ones. – Sebastian Jan 31 '12 at 16:18
0

I found that this is totally wrong approach in generating jpeg huffman codes. A Huffman table is generated from a collection of statistics in two steps, which are given in Annex K of JPEG standard. Also, few details are given in Annex C of the same document.

MrD
  • 2,423
  • 3
  • 33
  • 57