简体   繁体   English

如何从霍夫曼树生成霍夫曼码

[英]How to generate Huffman codes from huffman tree

I'm making jpeg encoder in C++. 我正在用C ++制作jpeg编码器。 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. 我尝试过的一种方法是将0分配给左分支,1分配给右分支,就像在图片中一样,但是这种方法存在一个问题,因为一个元素将被编码为全部(如图中的sibol E)用11)编码,但jpeg标准不允许所有的霍夫曼代码。

在此输入图像描述

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). 如果你不能使用全1的字符串,那么你不一定能获得最佳代码(在某些约束条件下是最佳的,我的意思是)。

If you append a "0" to the string that's all "1"s, then it won't be all "1"s any more. 如果你在字符串中追加一个“0”,那么它就不再是“1”了。 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. 这样就足够了,但我不知道这是不是jpeg编码器应该做的。 I'd have thought there would be a standard solution. 我原以为会有一个标准的解决方案。

JPEG is using a fixed tree based on statistics. JPEG使用基于统计信息的固定树。 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. 我假设树在标准文档中描述。

I found that this is totally wrong approach in generating jpeg huffman codes. 我发现这是生成jpeg霍夫曼代码的完全错误的方法。 A Huffman table is generated from a collection of statistics in two steps, which are given in Annex K of JPEG standard. 霍夫曼表是通过两个步骤的统计集合生成的,这些步骤在JPEG标准的附录K中给出。 Also, few details are given in Annex C of the same document. 此外,同一文件的附件C中没有提供详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM