github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/image/webp/libwebp/src/utils/huffman.h (about) 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 #ifdef __cplusplus 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 #define HUFF_LUT_BITS 7 32 #define HUFF_LUT (1U << HUFF_LUT_BITS) 33 typedef struct HuffmanTree HuffmanTree; 34 struct HuffmanTree { 35 // Fast lookup for short bit lengths. 36 uint8_t lut_bits_[HUFF_LUT]; 37 int16_t lut_symbol_[HUFF_LUT]; 38 int16_t lut_jump_[HUFF_LUT]; 39 // Complete tree for lookups. 40 HuffmanTreeNode* root_; // all the nodes, starting at root. 41 int max_nodes_; // max number of nodes 42 int num_nodes_; // number of currently occupied nodes 43 }; 44 45 // Returns true if the given node is not a leaf of the Huffman tree. 46 static WEBP_INLINE int HuffmanTreeNodeIsNotLeaf( 47 const HuffmanTreeNode* const node) { 48 return node->children_; 49 } 50 51 // Go down one level. Most critical function. 'right_child' must be 0 or 1. 52 static WEBP_INLINE const HuffmanTreeNode* HuffmanTreeNextNode( 53 const HuffmanTreeNode* node, int right_child) { 54 return node + node->children_ + right_child; 55 } 56 57 // Releases the nodes of the Huffman tree. 58 // Note: It does NOT free 'tree' itself. 59 void HuffmanTreeRelease(HuffmanTree* const tree); 60 61 // Builds Huffman tree assuming code lengths are implicitly in symbol order. 62 // Returns false in case of error (invalid tree or memory error). 63 int HuffmanTreeBuildImplicit(HuffmanTree* const tree, 64 const int* const code_lengths, 65 int code_lengths_size); 66 67 // Build a Huffman tree with explicitly given lists of code lengths, codes 68 // and symbols. Verifies that all symbols added are smaller than max_symbol. 69 // Returns false in case of an invalid symbol, invalid tree or memory error. 70 int HuffmanTreeBuildExplicit(HuffmanTree* const tree, 71 const int* const code_lengths, 72 const int* const codes, 73 const int* const symbols, int max_symbol, 74 int num_symbols); 75 76 // Utility: converts Huffman code lengths to corresponding Huffman codes. 77 // 'huff_codes' should be pre-allocated. 78 // Returns false in case of error (memory allocation, invalid codes). 79 int HuffmanCodeLengthsToCodes(const int* const code_lengths, 80 int code_lengths_size, int* const huff_codes); 81 82 83 #ifdef __cplusplus 84 } // extern "C" 85 #endif 86 87 #endif // WEBP_UTILS_HUFFMAN_H_