jubilant-funicular
Compressor.h
1 #ifndef COMPRESSOR_H_INCLUDED
2 #define COMPRESSOR_H_INCLUDED
3 
4 #include <vector>
5 #include <map>
6 
7 #include "MyEngine.h"
8 
9 typedef unsigned char GLubyte;
10 
11 namespace nta {
13  class HuffmanNode {
14  private:
16  HuffmanNode *m_lChild, *m_rChild;
17  protected:
19  int m_freq;
20  public:
22  HuffmanNode();
26  virtual ~HuffmanNode();
28  auto getEncodings(crstring enc = "") const -> std::map<GLubyte,std::string>;
30  HuffmanNode* getLeft() const;
31  HuffmanNode* getRight() const;
33  bool hasChildren() const;
35  int getFrequency() const;
36  };
38  class HuffmanLeaf : public HuffmanNode {
39  private:
41  GLubyte m_data;
42  public:
44  HuffmanLeaf();
46  HuffmanLeaf(GLubyte data, int freq);
48  ~HuffmanLeaf();
50  GLubyte getData() const;
51  };
53  class Compressor {
54  private:
56  static std::string toBinary(int num, int numBits = 8);
58  static int fromBinary(crstring num);
60  static void createTree(const std::vector<GLubyte>& data);
62  static std::map<GLubyte,std::string> m_encodings;
65  public:
67  static std::vector<GLubyte> decompress(const std::vector<GLubyte>& data);
69  static std::vector<GLubyte> compress(const std::vector<GLubyte>& data);
70  };
71 }
72 
73 #endif // COMPRESSOR_H_INCLUDED
nta::HuffmanNode
A node in a Huffman tree.
Definition: Compressor.h:13
nta::HuffmanNode::getEncodings
auto getEncodings(crstring enc="") const -> std::map< GLubyte, std::string >
returns map of all the bytes and how they are encoded
Definition: HuffmanNode.cpp:17
nta::HuffmanNode::HuffmanNode
HuffmanNode()
basic constructor
Definition: HuffmanNode.cpp:4
nta::HuffmanNode::hasChildren
bool hasChildren() const
returns whether or not the node has children
Definition: HuffmanNode.cpp:34
nta::HuffmanNode::~HuffmanNode
virtual ~HuffmanNode()
recursively destroys node
Definition: HuffmanNode.cpp:8
nta::Compressor
Static class for compressing byte buffers.
Definition: Compressor.h:53
nta
Definition: Animation2D.h:6
nta::Compressor::m_root
static HuffmanNode * m_root
the root of the created Huffman tree
Definition: Compressor.h:64
nta::HuffmanNode::getFrequency
int getFrequency() const
returns the frequency of the node
Definition: HuffmanNode.cpp:37
nta::HuffmanLeaf
represents a leaf in a Huffman tree
Definition: Compressor.h:38
nta::Compressor::m_encodings
static std::map< GLubyte, std::string > m_encodings
the encodings generated from the tree
Definition: Compressor.h:62
nta::HuffmanNode::getLeft
HuffmanNode * getLeft() const
returns children
Definition: HuffmanNode.cpp:28
nta::HuffmanNode::m_lChild
HuffmanNode * m_lChild
the children of the node
Definition: Compressor.h:16
nta::HuffmanLeaf::m_data
GLubyte m_data
the byte associated with the leaf
Definition: Compressor.h:41
nta::HuffmanNode::m_freq
int m_freq
the frequency of the nodes associated bytes
Definition: Compressor.h:19