File: HuffmanEncoder.h

package info (click to toggle)
apngasm 2.91-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,760 kB
  • sloc: ansic: 34,259; cpp: 7,258; makefile: 40
file content (60 lines) | stat: -rw-r--r-- 1,285 bytes parent folder | download | duplicates (20)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef __COMPRESSION_HUFFMANENCODER_H
#define __COMPRESSION_HUFFMANENCODER_H

namespace NCompression {
namespace NHuffman {

const int kNumBitsInLongestCode = 15;

struct CItem
{
  UINT32 Freq;
  UINT32 Code;
  UINT32 Dad;
  UINT32 Len;
};

class CEncoder
{
  UINT32 m_NumSymbols; // number of symbols in adwSymbol

  CItem *m_Items;
  UINT32 *m_Heap;
  UINT32 m_HeapSize;
  BYTE *m_Depth;
  const BYTE *m_ExtraBits;
  UINT32 m_ExtraBase;
  UINT32 m_MaxLength;

  UINT32 m_HeapLength;
  UINT32 m_BitLenCounters[kNumBitsInLongestCode + 1];

  UINT32 RemoveSmallest();
  bool Smaller(int n, int m); 
  void DownHeap(UINT32 k);
  void GenerateBitLen(UINT32 aMaxCode, UINT32 aHeapMax);
  void GenerateCodes(UINT32 aMaxCode);
  
  UINT32 m_BlockBitLength;
public:

  CEncoder(UINT32 aNumSymbols, const BYTE *anExtraBits, 
      UINT32 anExtraBase, UINT32 aMaxLength);
  ~CEncoder();
  void StartNewBlock();

  void AddSymbol(UINT32 aSymbol)
    {  m_Items[aSymbol].Freq++; }

  void SetFreqs(const UINT32 *aFreqs);
  void BuildTree(BYTE *aLevels);
  DWORD GetBlockBitLength() const { return m_BlockBitLength; }

  template <class TBitEncoder>
  void CodeOneValue(TBitEncoder *aStream, UINT32 aSymbol)
    { aStream->WriteBits(m_Items[aSymbol].Code, m_Items[aSymbol].Len); }
};

}}

#endif