huffman_encode.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2011 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. // Author: Jyrki Alakuijala (jyrki@google.com)
  11. //
  12. // Entropy encoding (Huffman) for webp lossless
  13. #ifndef WEBP_UTILS_HUFFMAN_ENCODE_H_
  14. #define WEBP_UTILS_HUFFMAN_ENCODE_H_
  15. #include "../webp/types.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. // Struct for holding the tree header in coded form.
  20. typedef struct {
  21. uint8_t code; // value (0..15) or escape code (16,17,18)
  22. uint8_t extra_bits; // extra bits for escape codes
  23. } HuffmanTreeToken;
  24. // Struct to represent the tree codes (depth and bits array).
  25. typedef struct {
  26. int num_symbols; // Number of symbols.
  27. uint8_t* code_lengths; // Code lengths of the symbols.
  28. uint16_t* codes; // Symbol Codes.
  29. } HuffmanTreeCode;
  30. // Struct to represent the Huffman tree.
  31. // TODO(vikasa): Add comment for the fields of the Struct.
  32. typedef struct {
  33. uint32_t total_count_;
  34. int value_;
  35. int pool_index_left_; // Index for the left sub-tree.
  36. int pool_index_right_; // Index for the right sub-tree.
  37. } HuffmanTree;
  38. // Turn the Huffman tree into a token sequence.
  39. // Returns the number of tokens used.
  40. int VP8LCreateCompressedHuffmanTree(const HuffmanTreeCode* const tree,
  41. HuffmanTreeToken* tokens, int max_tokens);
  42. // Create an optimized tree, and tokenize it.
  43. // 'buf_rle' and 'huff_tree' are pre-allocated and the 'tree' is the constructed
  44. // huffman code tree.
  45. void VP8LCreateHuffmanTree(uint32_t* const histogram, int tree_depth_limit,
  46. uint8_t* const buf_rle, HuffmanTree* const huff_tree,
  47. HuffmanTreeCode* const tree);
  48. #ifdef __cplusplus
  49. }
  50. #endif
  51. #endif // WEBP_UTILS_HUFFMAN_ENCODE_H_