⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 huffman.h

📁 huffman 压缩算法 支持文件夹的压缩与解压
💻 H
字号:

#ifndef Huffman_h
#define Huffman_h


//      哈夫曼树
typedef struct hufftree
{
    unsigned int freq;       //s1 freq
    unsigned char s1;        //0...255:codes       left symb
    unsigned char s2;        //                    right symb
    hufftree* pleft;         //0
    hufftree* pright;        //1
    hufftree* parent;        //parent node
} *PHTREE;
//哈夫曼编码
typedef struct huffsymbol 
{
    unsigned int size;       //symbol size in bits
    __int64 symb;            //max 256 bits
} *PSYMB;


//   哈夫曼文件头
typedef struct huffheader
{
    char magic[4];           //'HUFF'
    int symnum;              //total symbnum
    int uncmpsize;           //uncompressed file size
    int cmpsize;             //compressed file size
} *PHUFFHDR;

//哈夫曼编码表
typedef struct symbheader
{
    unsigned int freq;       //its freq
    unsigned char symb;      //symbol
} *PSYMBHDR;


//哈夫曼压缩类
class Huffman
{
public:
    Huffman(){};
    //Huffman(const Huffman& huffman);
    //~Huffman();

// Operators
    //const Huffman& operator=(const Huffman& huffman);

// Operations
    void encode(unsigned char *dest, int &csize, unsigned char *sour, int usize);
    void decode(unsigned char *dest, int &usize, unsigned char *sour);

// Access
// Inquiry
    inline int get_uncompressed_size(unsigned char *sour);

protected:
private:
    Huffman(const Huffman& huffman);
    const Huffman& operator=(const Huffman& huffman);


    PHTREE temptree;                              //tmass1 first tree
    PHTREE htree;                                 //tmass2 second huff tree
    PSYMB hsymbol;                                //smass symbols codebook

    unsigned char tmass1[256*sizeof(hufftree)];             //first tree with freq codes
    unsigned char tmass2[256*sizeof(hufftree)];             //last built tree
    unsigned char smass[256*sizeof(huffsymbol)];            //symbols codebook buffer [code][size] pairs

    unsigned char *psour;                                  //source pointer
    unsigned char *pdest;                                  //dest pointer

    PHUFFHDR pheader;                              //huffman file header
    PSYMBHDR psheader;                             //[ freq ][C] pairs

    int filesize;                                 //uncompressed file size
    int symnum;                                   //total different symbols
    int bitslast;


    static int __cdecl compare(const void *a, const void *b);    //for sorting routine

    void storetree(void);                         //memory and huff headers init
    void buildtree(void);                         //htree building
    void buildbook(void);                         //build codebook [code][size] pairs
    void writesymb(int c);                        //write codebook symbol

    void readtree(void);                          //read headers and get freqs
    unsigned char readsymb();                     //read symbol from htree
    inline int getnextbit();                      //get bits from sour

};

inline int Huffman::get_uncompressed_size(unsigned char *sour)
{
    pheader = (PHUFFHDR)sour;
    return pheader->uncmpsize;
}

/*             get bits from sour             */
inline int Huffman::getnextbit()
{
    if (bitslast)
    {
        return 0x1 & (*psour >> (--bitslast));
    }
    else
    {
        psour++;
        bitslast = 8;
        return 0x1 & (*psour >> (--bitslast));
    }
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -