• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 Google Inc. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the COPYING file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 // -----------------------------------------------------------------------------
9 //
10 // Utilities for building and looking up Huffman trees.
11 //
12 // Author: Urvang Joshi (urvang@google.com)
13 
14 #ifndef WEBP_UTILS_HUFFMAN_H_
15 #define WEBP_UTILS_HUFFMAN_H_
16 
17 #include <assert.h>
18 #include "../webp/types.h"
19 
20 #if defined(__cplusplus) || defined(c_plusplus)
21 extern "C" {
22 #endif
23 
24 // A node of a Huffman tree.
25 typedef struct {
26   int symbol_;
27   int children_;  // delta offset to both children (contiguous) or 0 if leaf.
28 } HuffmanTreeNode;
29 
30 // Huffman Tree.
31 typedef struct HuffmanTree HuffmanTree;
32 struct HuffmanTree {
33   HuffmanTreeNode* root_;   // all the nodes, starting at root.
34   int max_nodes_;           // max number of nodes
35   int num_nodes_;           // number of currently occupied nodes
36 };
37 
38 // Returns true if the given node is a leaf of the Huffman tree.
HuffmanTreeNodeIsLeaf(const HuffmanTreeNode * const node)39 static WEBP_INLINE int HuffmanTreeNodeIsLeaf(
40     const HuffmanTreeNode* const node) {
41   return (node->children_ == 0);
42 }
43 
44 // Go down one level. Most critical function. 'right_child' must be 0 or 1.
HuffmanTreeNextNode(const HuffmanTreeNode * node,int right_child)45 static WEBP_INLINE const HuffmanTreeNode* HuffmanTreeNextNode(
46     const HuffmanTreeNode* node, int right_child) {
47   return node + node->children_ + right_child;
48 }
49 
50 // Releases the nodes of the Huffman tree.
51 // Note: It does NOT free 'tree' itself.
52 void HuffmanTreeRelease(HuffmanTree* const tree);
53 
54 // Builds Huffman tree assuming code lengths are implicitly in symbol order.
55 // Returns false in case of error (invalid tree or memory error).
56 int HuffmanTreeBuildImplicit(HuffmanTree* const tree,
57                              const int* const code_lengths,
58                              int code_lengths_size);
59 
60 // Build a Huffman tree with explicitly given lists of code lengths, codes
61 // and symbols. Verifies that all symbols added are smaller than max_symbol.
62 // Returns false in case of an invalid symbol, invalid tree or memory error.
63 int HuffmanTreeBuildExplicit(HuffmanTree* const tree,
64                              const int* const code_lengths,
65                              const int* const codes,
66                              const int* const symbols, int max_symbol,
67                              int num_symbols);
68 
69 // Utility: converts Huffman code lengths to corresponding Huffman codes.
70 // 'huff_codes' should be pre-allocated.
71 // Returns false in case of error (memory allocation, invalid codes).
72 int HuffmanCodeLengthsToCodes(const int* const code_lengths,
73                               int code_lengths_size, int* const huff_codes);
74 
75 
76 #if defined(__cplusplus) || defined(c_plusplus)
77 }    // extern "C"
78 #endif
79 
80 #endif  // WEBP_UTILS_HUFFMAN_H_
81