码迷,mamicode.com
首页 > 其他好文 > 详细

huffman编码压缩和解压

时间:2016-01-17 11:01:17      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

typedef struct huffman_node_tag
{
    unsigned char        isLeaf; 
    unsigned long        count; 
    struct huffman_node_tag *parent; 
    union 
    {
       struct {
           struct huffman_node_tag *zero;
           struct huffman_node_tag *one;
       };
       unsigned char symbol;
    };
} huffman_node;

typedef struct huffman_code_tag
{
    unsigned long numbits;
    unsigned char *bits;
} huffman_code;

static unsigned long numbytes_from_numbits(unsigned long numbits)
{
    return numbits / 8 + (numbits % 8 ? 1 : 0);
}

static unsigned char get_bit(unsigned char *bits, unsigned long i)
{
    return ( bits[i/8] >> (i%8) ) & 1;
}

static void reverse_bits(unsigned char* bits, unsigned long numbits)
{
    unsigned long numbytes = numbytes_from_numbits(numbits);    // 所占字节数.
    unsigned char *tmp = (unsigned char*)calloc(numbytes, sizeof(unsigned char));    // 分配内存
    unsigned long curbit;           // index -- 当前位
    long          curbyte = 0;  // 当前是byte的index
      
    memset(tmp, 0, numbytes);   // 将tmp指向的buffer清零

    for(curbit=0; curbit<numbits; ++curbit) 
    {
       unsigned int bitpos = curbit % 8;  // 当前byte里的index
       if(curbit>0 && curbit%8==0) 
       {
           ++curbyte;
       }
       tmp[curbyte] |= (get_bit(bits, numbits-curbit-1) << bitpos);
    }
    memcpy(bits, tmp, numbytes);
}

static huffman_code* new_code(const huffman_node* leaf)
{
    unsigned long numbits = 0;
    unsigned char *bits = NULL;
    huffman_code  *p;
   
    while(leaf!=NULL && leaf->parent!=NULL) 
    {
       huffman_node *parent = leaf->parent;
       unsigned long cur_byte   = numbits / 8;
       unsigned char cur_bit    = (unsigned char)(numbits % 8);

       if(cur_bit == 0) 
       {
           size_t newSize = cur_byte + 1;
           bits = (unsigned char*)realloc(bits, newSize);
           bits[newSize - 1] = 0;
       }

       if(leaf == parent->one)
       {
           bits[cur_byte] |= (1<<cur_bit);
       }
      
       ++numbits;
       leaf = parent;
    }
   
    if( bits != 0) 
    {
       reverse_bits(bits, numbits);
    }
   
    p = (huffman_code*)malloc(sizeof(huffman_code));
    p->numbits = numbits; 
    p->bits    = bits; 

    return p;
}

static huffman_node* new_leaf_node(unsigned char symbol)
{
    huffman_node *p = (huffman_node*)malloc( sizeof(huffman_node) );

    p->isLeaf = 1;
    p->symbol = symbol;
    p->count = 0;
    p->parent = 0;
   
    return p;
}

static huffman_node* new_nonleaf_node(unsigned long count, huffman_node *zero, huffman_node *one)
{
    huffman_node *p = (huffman_node*)malloc( sizeof(huffman_node) );
    p->isLeaf = 0;
    p->count = count;
    p->zero = zero;
    p->one = one;
    p->parent = 0;
    return p;
}

static void free_huffman_tree(huffman_node *subtree)
{
    if(subtree == NULL)
       return;   

    if( !(subtree->isLeaf) ) 
    {
       free_huffman_tree( subtree->zero );
       free_huffman_tree( subtree->one );
    }
    free( subtree );
}

static void free_code(huffman_code* p)
{
    free(p->bits);
    free(p);
}
 
#define MAX_SYMBOLS 256
typedef huffman_node* SymbolFrequencies[MAX_SYMBOLS];   /*  */
typedef huffman_code* SymbolEncoder[MAX_SYMBOLS];       /*  */
/*  */
static void free_encoder(SymbolEncoder *pSE)
{
    unsigned long i;
    for(i = 0; i < MAX_SYMBOLS; ++i) {
       huffman_code *p = (*pSE)[i];
       if( p )    free_code(p);
    }
}
/*  */
static void
init_frequencies(SymbolFrequencies *pSF)
{
    memset(*pSF, 0, sizeof(SymbolFrequencies) );  /* 清零 */
}
 
 
// ----------------------------------------------------------------------------------------
typedef struct buf_cache_tag
{
    /*
     * 该结构主要描述了两个部分, 一个是cache, 一个是bufout.
     * cache是一个临时存储数据的buffer, cache会将数据写往bufout区间,
     * bufout类似一个仓库, 会一直存储cache写入的数据.
     * cache可以多次网bufout内写数据, bufout会一直保存这些数据.
     * bufout是一个动态的buffer, cache每一次往bufout内写数据的时候bufout都需要realloc一次.
     */
    unsigned char *cache;       // 指向真正存储数据的buffer
    unsigned int cache_len; // buffer的长度, 初始的时候就可以设置 cache的大小的
    unsigned int cache_cur; // 数据结尾处(或者说是光标位置)
    unsigned char **pbufout; /*
                             * cache要写数据就往这个空间内写(类似一个动态仓库, 一定是动态的)
                             * (*pbufout)就是真实的存储区
                             */
    unsigned int *pbufoutlen;  // 仓库的大小
} buf_cache;
/* 初始化一个buf_cache */
static int init_cache(buf_cache    *pc,
                    unsigned int    cache_size,
                    unsigned char **pbufout,
                    unsigned int    *pbufoutlen)
{
    assert(pc && pbufout && pbufoutlen);
    if(!pbufout || !pbufoutlen) return 1;
   
    pc->cache     = (unsigned char*)malloc(cache_size);  // 分配存储空间
    pc->cache_len = cache_size; //
    pc->cache_cur = 0;       // 光标从0开始
    pc->pbufout   = pbufout; //
    *pbufout      = NULL;       //
    pc->pbufoutlen    = pbufoutlen;
    *pbufoutlen   = 0;       //
   
    return (pc->cache==NULL ? 0 : 1);
}
 
/* 释放buf_cache */
static void free_cache(buf_cache* pc)
{
    assert( pc );
    if( pc->cache != NULL)
    {
       free( pc->cache );
       pc->cache = NULL;
    }
}

static int flush_cache(buf_cache* pc)
{
    assert( pc );
   
    if(pc->cache_cur > 0)
    {
       unsigned int newlen = pc->cache_cur + *(pc->pbufoutlen);
       unsigned char*    tmp = (unsigned char*)realloc(*(pc->pbufout), newlen);
       if( !tmp ) return 1;

       memcpy(tmp + *(pc->pbufoutlen), pc->cache, pc->cache_cur);
       *pc->pbufout = tmp;
       *pc->pbufoutlen = newlen;
       pc->cache_cur = 0;
    }
   
    return 0;
}

static int write_cache(buf_cache* pc,
                     const void *to_write,
                     unsigned int to_write_len)
{
    unsigned char* tmp;
   
    assert(pc && to_write);
    assert(pc->cache_len >= pc->cache_cur);

    if(to_write_len > pc->cache_len - pc->cache_cur)
    {
       unsigned int newlen;
      
       flush_cache( pc );
      
       newlen = *pc->pbufoutlen + to_write_len;
       tmp = (unsigned char*)realloc(*pc->pbufout, newlen);
       if( !tmp ) return 1;
       memcpy(tmp + *pc->pbufoutlen, to_write, to_write_len);
       *pc->pbufout = tmp;
       *pc->pbufoutlen = newlen;
    }
    else
    {
       memcpy(pc->cache+pc->cache_cur, to_write, to_write_len);
       pc->cache_cur += to_write_len;
    }
   
    return 0;
}

static unsigned int get_symbol_frequencies(SymbolFrequencies *pSF, FILE *in)
{
    int c;
    unsigned int total_count = 0;   // FILE对象内的字符总数
   
    init_frequencies( pSF ); /* Set all frequencies to 0. */
   
    /* Count the frequency of each symbol in the input file. */
    while( (c=fgetc(in)) != EOF )
    {
       unsigned char uc = c;
 
       if( !(*pSF)[uc] )
       {
           (*pSF)[uc] = new_leaf_node( uc );
       }
       ++( (*pSF)[uc]->count );
       ++total_count;
    }
 
    return total_count;
}
/* 计算buffer内各个字符的频率,和get_symbol_frequencies函数同理 */
static unsigned int get_symbol_frequencies_from_memory(SymbolFrequencies    *pSF,
                               const unsigned char    *bufin,
                               unsigned int       bufinlen)
{
    unsigned int i;
    unsigned int total_count = 0;
   
    /* Set all frequencies to 0. */
    init_frequencies(pSF);
   
    /* Count the frequency of each symbol in the input file. */
    for(i = 0; i < bufinlen; ++i)
    {
       unsigned char uc = bufin[i];
       if( !(*pSF)[uc] )
       {
           (*pSF)[uc] = new_leaf_node(uc);
       }
       ++(*pSF)[uc]->count;
       ++total_count;
    }
 
    return total_count;
}

static int SFComp(const void *p1, const void *p2)
{
    const huffman_node *hn1 = *(const huffman_node**)p1;
    const huffman_node *hn2 = *(const huffman_node**)p2;
   
    /* Sort all NULLs to the end. */
    if(hn1 == NULL && hn2 == NULL)     return 0;
    if(hn1 == NULL)                    return 1;
    if(hn2 == NULL)                    return -1;
   
    if(hn1->count > hn2->count)        return 1;
    else if(hn1->count < hn2->count)   return -1;
   
    return 0;
}

static void
build_symbol_encoder(huffman_node *subtree, SymbolEncoder *pSE)
{
    if(subtree == NULL)  return;
   
    if( subtree->isLeaf )
    {
       (*pSE)[subtree->symbol] = new_code( subtree );
    }
    else
    {
       build_symbol_encoder(subtree->zero, pSE);
       build_symbol_encoder(subtree->one, pSE);
    }
}
 
/*
 * calculate_huffman_codes turns pSF into an array
 * with a single entry that is the root of the
 * huffman tree. The return value is a SymbolEncoder,
 * which is an array of huffman codes index by symbol value.
 *
 * 为每个node编码. 这个函数比较重要, 精华就是在这个函数里头的for循环. 哈哈
 * 整个tree的建立全都依赖这个函数
 */
static SymbolEncoder*
calculate_huffman_codes(SymbolFrequencies * pSF)
{
    unsigned int i = 0;
    unsigned int n = 0;
    huffman_node *m1  = NULL, *m2 = NULL;
    SymbolEncoder *pSE = NULL;
   
    /*
     * Sort the symbol frequency array by ascending frequency.
     * 快速排序例程进行排序
     * 以symbol频率为关键字做升序排列
     * 有symbol的节点都会按升序排列, 没有symbol的节点会统一排在后面,
     * 通过一个for就能计算出symbol的个数了.
     */
    qsort((*pSF), MAX_SYMBOLS, sizeof((*pSF)[0]), SFComp);
   
    /*
     * Get the number of symbols.
     * 计算huffman树中的字符数, 这个实现可读性不够好
     */
    for(n = 0; (n<MAX_SYMBOLS) && (*pSF)[n]; ++n)
       ;
   
    /*
     * Construct a Huffman tree. This code is based
     * on the algorithm given in Managing Gigabytes
     * by Ian Witten et al, 2nd edition, page 34.
     * Note that this implementation uses a simple
     * count instead of probability.
     */
    for(i = 0; i < (n-1); ++i)
    {
       /* Set m1 and m2 to the two subsets of least probability. */
       m1 = (*pSF)[0];
       m2 = (*pSF)[1];
       /* Replace m1 and m2 with a set {m1, m2} whose probability
        * is the sum of that of m1 and m2.
        * 这个算法有优化的余地的, 因为n在一直减小.
        * 将最小的两个元素合并后得到一个一个节点为m12, 此时m1,m2已经建立起来了关系.
        * 这个m12的地址又被pSF[0]存储, 循环直至整个Tree建立成功.
        * 指针在这里运用的实在太巧妙了.
        * 这一行代码就是建树, 靠,NBA!
        */
       (*pSF)[0] = m1->parent = m2->parent = new_nonleaf_node(m1->count+m2->count, m1, m2);
       (*pSF)[1] = NULL;
       /*
        * Put newSet into the correct count position in pSF.
        * 这里应该可以再进行优化, 是否有必要再进行排序, 或者被排序的数组过长了.
        * 实际上每循环一次n都减少了一次
        */
       qsort((*pSF), n, sizeof((*pSF)[0]), SFComp);
    }/* for完毕的时候就求出了root, pSF[0]就是root, 后面的元素都是NULL
      * 而树通过for循环里头的
      * (*pSF)[0] = m1->parent = m2->parent = new_nonleaf_node(m1->count+m2->count, m1, m2);
      * 已经建立完成了*/
   
    /* Build the SymbolEncoder array from the tree. */
    pSE = (SymbolEncoder*)malloc(sizeof(SymbolEncoder));
    memset(pSE, 0, sizeof(SymbolEncoder));
    build_symbol_encoder((*pSF)[0], pSE);
   
    return pSE;
}
 
/*
 * Write the huffman code table. The format is:
 * 4 byte code count in network byte order.
 * 4 byte number of bytes encoded
 *   (if you decode the data, you should get this number of bytes)
 * code1
 * ...
 * codeN, where N is the count read at the begginning of the file.
 * Each codeI has the following format:
 * 1 byte symbol, 1 byte code bit length, code bytes.
 * Each entry has numbytes_from_numbits code bytes.
 * The last byte of each code may have extra bits, if the number of
 * bits in the code is not a multiple of 8.
 *
 * 编码后的格式 :
 * 0-3个byte是FILE内出现的不同字符个数(几不同的字符个数)
 * 4-7个byte是FILE内出现的全部字符个数(所有的字符)
 * 8-X是真正的编码后值
 *
 */
static int
write_code_table(FILE* out, SymbolEncoder *se, unsigned int symbol_count)
{
    unsigned long i, count = 0;
   
    /*
     * Determine the number of entries in se
     * 计算 SymbolEncoder 内具有编码值的元素个数.
     * 即有几种字符
     */
    for(i = 0; i < MAX_SYMBOLS; ++i)
       if( (*se)[i] )
           ++count;
   
    /*
     * Write the number of entries in network byte order.
     * 将字符种数写入到文件头部, 即[0, 3]一共4个字节
     */
    //i = htonl( count );
    i = count;
    if(fwrite(&i, sizeof(i), 1, out) != 1) return 1;
   
    /*
     * Write the number of bytes that will be encoded.
     * 将字符个数追加到[4,7]一共4个字节
     */
    //symbol_count = htonl(symbol_count);
    symbol_count = symbol_count;
    if(fwrite(&symbol_count, sizeof(symbol_count), 1, out) != 1)   return 1;
   
    /*
     * Write the entries.
     */
    for(i = 0; i < MAX_SYMBOLS; ++i)
    {
       huffman_code *p = (*se)[i];
       if( p != NULL )
       {   /*
            * 每个单元分为三个部分 : 
            * symbol  -- 字符
            * numbits  -- 叶子走到root需要的步数
            * bits    -- 叶子走到root的方式(即最终的编码, 比如说0101)
            */
           unsigned int numbytes;
           /* Write the 1 byte symbol. */
           fputc((unsigned char)i, out);  
           /* Write the 1 byte code bit length. */
           fputc(p->numbits, out);
           /* Write the code bytes. 她这个注释就没有说是几byte, 值得思考一下 */
           numbytes = numbytes_from_numbits( p->numbits );
           /* 将叶子走到root的方式写进去, 这个方式会被整理为byte格式, 不够就补0 */
           if(fwrite(p->bits, 1, numbytes, out) != numbytes)    return 1;
       }
    }
 
    return 0;
}
 
/*
 * Allocates memory and sets *pbufout to point to it. The memory
 * contains the code table.
 *
 * 以指定的格式将编码后的数据写入到cache中去, 实际是写到pbufout中去了.
 *
 */
static int
write_code_table_to_memory(buf_cache      *pc,
                        SymbolEncoder   *se,
                        unsigned int    symbol_count)
{
    unsigned long i, count = 0;
   
    /* Determine the number of entries in se. */
    for(i = 0; i < MAX_SYMBOLS; ++i)
    {
       if((*se)[i])
       {
           ++count;   // 计算不同字符的个数
       }
    }
 
    /* Write the number of entries in network byte order. */
    //i = htonl(count);
    i = count;
    if( write_cache(pc, &i, sizeof(i)) )   // 前四个字节是memory内所有字符数
       return 1;
   
    /* Write the number of bytes that will be encoded. */
    //symbol_count = htonl(symbol_count);
    symbol_count = symbol_count;
    if( write_cache(pc, &symbol_count, sizeof(symbol_count)) )  // 4-8字节是不同字符个数
       return 1;
 
    /* Write the entries. */
    for(i = 0; i < MAX_SYMBOLS; ++i)
    {
       huffman_code *p = (*se)[i];
       if( p )
       {
           /*
            * 对于每次循环来说, 如果p不为NULL, 则将该字符对应的编码写入到cache内.
            * 存储格式为三个字节作为一个单位.
            * byte0 --- 字符本身
            * byte1 --- 该字符编码后的码值长度(即2进制的位数)
            * byte2 --- 该字符对应的码值
            */
           unsigned int numbytes;
           /*
            * The value of i is < MAX_SYMBOLS (256), so it can
            * be stored in an unsigned char.
            * 将i转换为char型, 可以对应到字符集
            */
           unsigned char uc = (unsigned char)i;
           /*
            * Write the 1 byte symbol.
            * 将字符写到cache内
            */
           if(write_cache(pc, &uc, sizeof(uc)))   return 1;
           /*
            * Write the 1 byte code bit length.
            * 将叶子节点到root所需要经过的步数写到cache内, 也就是编码的长度
            * 这个数据是为了解码使用的.
            */
           uc = (unsigned char)p->numbits;
           if(write_cache(pc, &uc, sizeof(uc)))   return 1;
           /*
            * Write the code bytes.
            * 将编码值对齐并写如到cache内
            * 事先必须知道编码由几位组成, 如果编码为9位, 那么就需要2个byte来存储这个码值
            * 如果编码为4位, 那么就需要1个byte来存储了,
            */
           numbytes = numbytes_from_numbits(p->numbits);
           if(write_cache(pc, p->bits, numbytes)) return 1;
       }
    }
 
    return 0;
}
 
/*
 * read_code_table builds a Huffman tree from the code
 * in the in file. This function returns NULL on error.
 * The returned value should be freed with free_huffman_tree.
 *
 *
 */
static huffman_node*
read_code_table(FILE* in, unsigned int *pDataBytes)
{
    huffman_node *root = new_nonleaf_node(0, NULL, NULL);
    unsigned int count;
   
    /*
     * Read the number of entries.
     * (it is stored in network byte order).
     * 获得字符种数, 前2个byte就是出现的字符种数
     */
    if( fread(&count, sizeof(count), 1, in) != 1 )
    {
       free_huffman_tree( root );
       return NULL;
    }
 
    //count = ntohl(count);
    count = count;
    /*
     * Read the number of data bytes this encoding represents.
     * 一个有多少个字符
     */
    if( fread(pDataBytes, sizeof(*pDataBytes), 1, in) != 1 )
    {
       free_huffman_tree(root);
       return NULL;
    }
   
    //*pDataBytes = ntohl(*pDataBytes);
    *pDataBytes = *pDataBytes;
   
    /* Read the entries. */
    while(count-- > 0)
    {
       int           c;
       unsigned int curbit;
       unsigned char symbol;
       unsigned char numbits;
       unsigned char numbytes;
       unsigned char *bytes;
       huffman_node *p = root;
      
       if( (c=fgetc(in)) == EOF )
       {
           free_huffman_tree( root );
           return NULL;
       }
       symbol = (unsigned char)c;
      
       if( (c=fgetc(in)) == EOF )
       {
           free_huffman_tree( root );
           return NULL;
       }
      
       numbits    = (unsigned char)c;
       numbytes   = (unsigned char)numbytes_from_numbits( numbits );
       bytes      = (unsigned char*)malloc( numbytes );
       if( fread(bytes, 1, numbytes, in) != numbytes )
       {
           free(bytes);
           free_huffman_tree(root);
           return NULL;
       }
 
       /*
        * Add the entry to the Huffman tree. The value
        * of the current bit is used switch between
        * zero and one child nodes in the tree. New nodes
        * are added as needed in the tree.
        */
       for(curbit = 0; curbit < numbits; ++curbit)
       {
           if(get_bit(bytes, curbit))
           {
              if(p->one == NULL)
              {
                  p->one =
                     curbit == (unsigned char)(numbits-1) ?
                     new_leaf_node(symbol) : new_nonleaf_node(0, NULL, NULL);
                  p->one->parent = p;
              }
              p = p->one;
           }
           else
           {
              if(p->zero == NULL)
              {
                  p->zero =
                     curbit == (unsigned char)(numbits - 1) ?
                     new_leaf_node(symbol) : new_nonleaf_node(0, NULL, NULL);
                  p->zero->parent = p;
              }
              p = p->zero;
           }
       }
      
       free(bytes);
    }
   
    return root;
}
/*
 * 将数据从buf读到bufout中, 成功则返回0, 其他则返回1.
 * pindex  -- 拷贝的起点
 */
static int
memread(const unsigned char*    buf,
       unsigned int         buflen,
       unsigned int         *pindex,
       void*                bufout,
       unsigned int         readlen)
{
    assert(buf && pindex && bufout);
    assert(buflen >= *pindex);
   
    // 错误
    if(buflen < *pindex)        return 1;
    if(readlen + *pindex >= buflen) return 1;
   
    memcpy(bufout, buf + *pindex, readlen);
    *pindex += readlen;
   
    return 0;
}
/*
 * 从编码后的buf内读数据.
 */
static huffman_node*
read_code_table_from_memory(const unsigned char* bufin,
                         unsigned int         bufinlen,
                         unsigned int         *pindex,
                         unsigned int         *pDataBytes)
{
    huffman_node *root = new_nonleaf_node(0, NULL, NULL);
    unsigned int count;
   
    /*
     * Read the number of entries.
     * (it is stored in network byte order).
     * 读取
     */
    if( memread(bufin, bufinlen, pindex, &count, sizeof(count)) )
    {
       free_huffman_tree(root);
       return NULL;
    }
 
    //count = ntohl(count);
    count = count;
   
    /* Read the number of data bytes this encoding represents. */
    if(memread(bufin, bufinlen, pindex, pDataBytes, sizeof(*pDataBytes)))
    {
       free_huffman_tree(root);
       return NULL;
    }
   
    //*pDataBytes = ntohl(*pDataBytes);
    *pDataBytes = *pDataBytes;
   
    /* Read the entries. */
    while( (count--) > 0 )
    {
       unsigned int curbit;
       unsigned char symbol;
       unsigned char numbits;
       unsigned char numbytes;
       unsigned char *bytes;
       huffman_node *p = root;
 
       if(memread(bufin, bufinlen, pindex, &symbol, sizeof(symbol)))
       {
           free_huffman_tree(root);
           return NULL;
       }
 
       if(memread(bufin, bufinlen, pindex, &numbits, sizeof(numbits)))
       {
           free_huffman_tree(root);
           return NULL;
       }
      
       numbytes = (unsigned char)numbytes_from_numbits(numbits);
       bytes = (unsigned char*)malloc(numbytes);
       if(memread(bufin, bufinlen, pindex, bytes, numbytes))
       {
           free(bytes);
           free_huffman_tree(root);
           return NULL;
       }
 
       /*
        * Add the entry to the Huffman tree. The value
        * of the current bit is used switch between
        * zero and one child nodes in the tree. New nodes
        * are added as needed in the tree.
        */
       for(curbit = 0; curbit < numbits; ++curbit)
       {
           if(get_bit(bytes, curbit))
           {
              if(p->one == NULL)
              {
                  p->one = ( curbit==(unsigned char)(numbits - 1) ) ?
                  new_leaf_node(symbol) : new_nonleaf_node(0, NULL, NULL);
                  p->one->parent = p;
              }
              p = p->one;
           }
           else
           {
              if(p->zero == NULL)
              {
                  p->zero = curbit == (unsigned char)(numbits - 1)
                     ? new_leaf_node(symbol)
                     : new_nonleaf_node(0, NULL, NULL);
                  p->zero->parent = p;
              }
              p = p->zero;
           }
       }
      
       free(bytes);
    }
 
    return root;
}
/*
 * 依次将各个字符的编码写入到out中, 这次是直接写, 不对编码进行整齐工作
 * 也就是不将编码强制为byte类型了, 而是直接写入到out中.
 */
static int
do_file_encode(FILE* in, FILE* out, SymbolEncoder *se)
{
    unsigned char curbyte = 0;
    unsigned char curbit  = 0;
    int c;
   
    while( (c = fgetc(in)) != EOF)
    {
       unsigned char uc     = (unsigned char)c;
       huffman_code *code = (*se)[uc];
       unsigned long i;
      
       for(i = 0; i < code->numbits; ++i)
       {
           curbyte |= get_bit(code->bits, i) << curbit;
           if(++curbit == 8)
           {
              fputc(curbyte, out);
              curbyte = 0;
              curbit = 0;
           }
       }
    }

    if(curbit > 0)
       fputc(curbyte, out);
   
    return 0;
}

static int do_memory_encode(buf_cache *pc, const unsigned char *bufin,
               unsigned int         bufinlen,
               SymbolEncoder           *se)
{
    unsigned char curbyte    = 0;       //
    unsigned char curbit = 0;
    unsigned int i;
   
    /* 对 bufin 内的字符依次循环 */
    for(i = 0; i < bufinlen; ++i)
    {
       unsigned char uc = bufin[i];
       huffman_code *code = (*se)[uc]; // 取出第i个字符的编码
       unsigned long i;           
      
       /* 对第i个字符编码长度进行循环 */
       for(i = 0; i < code->numbits; ++i)
       {
           /*
            * Add the current bit to curbyte.
            * 依次取出
            */
           curbyte |= ( get_bit(code->bits, i) << curbit );
          
           /*
            * If this byte is filled up then write it
            * out and reset the curbit and curbyte
            */
           if(++curbit == 8)
           {
              /*
               * 满了一个字节则写cache
               *
               */
              if(write_cache(pc, &curbyte, sizeof(curbyte)))   return 1;
              curbyte = 0;
              curbit  = 0;
           }
       }
    }

    return curbit > 0 ? write_cache(pc, &curbyte, sizeof(curbyte)) : 0;
}
 
/*
 * huffman_encode_file huffman encodes in to out.
 * 对FILE对象进行编码, 将*in编码后写入*out.
 */
int
huffman_encode_file(FILE *in, FILE *out)
{
    SymbolFrequencies sf;
    SymbolEncoder *se;
    huffman_node *root = NULL;
    int rc;
    unsigned int symbol_count;

    symbol_count = get_symbol_frequencies(&sf, in);
      
    se = calculate_huffman_codes( &sf );
    root = sf[0];

    rewind( in ); // 将文件指针重新指向一个流的开头

    rc = write_code_table(out, se, symbol_count);
    if(rc == 0)
       rc = do_file_encode(in, out, se);

    free_huffman_tree( root );
    free_encoder(se);
   
    return rc;
}
 

int huffman_decode_file(FILE *in, FILE *out)
{
    huffman_node *root;
    huffman_node *p;
    int           c;
    unsigned int data_count;
   
    /* Read the Huffman code table. */
    root = read_code_table(in, &data_count);
    if( !root )   return 1;
 
    /* Decode the file. */
    p = root;
    while(data_count>0 && (c=fgetc(in))!=EOF)
    {
       unsigned char byte = (unsigned char)c;
       unsigned char mask = 1;
       while(data_count > 0 && mask)
       {
           p = ( (byte&mask)? p->one : p->zero );
           mask <<= 1;
          
           if( p->isLeaf )
           {
              fputc(p->symbol, out);
              p = root;
              --data_count;
           }
       }
    }
   
    free_huffman_tree( root );
   
    return 0;
}
 
 
// --------------------------------------------------------------------------------------
#define CACHE_SIZE 1024   
                          

int huffman_encode_memory(const unsigned char    *bufin,
                       unsigned int           bufinlen,
                       unsigned char       **pbufout,
                       unsigned int           *pbufoutlen)
{
    SymbolFrequencies    sf;
    SymbolEncoder     *se;
   
    huffman_node *root = NULL;
    int rc;
    unsigned int symbol_count;  // memory中的字符个数
   
    buf_cache cache;         //
   
    /* Ensure the arguments are valid. 检测参数合法性 */
    if(!pbufout || !pbufoutlen) return 1;
    if( init_cache(&cache, CACHE_SIZE, pbufout, pbufoutlen) )   return 1;
   
    /*
     * Get the frequency of each symbol in the input memory
     * 计算bufin内各个字符出现的频率, 并求得bufin内存储的字符个数.
     */
    symbol_count = get_symbol_frequencies_from_memory(&sf, bufin, bufinlen);
   
    /*
     * Build an optimal table from the symbolCount.
     * 为每个Node编码, 如果这个Node的symbol为NULL, 则不编码了.
     */
    se = calculate_huffman_codes( &sf );
    root = sf[0]; // root来拉, 哈哈, 逻辑树出来了.
   
    /*
     * Scan the memory again and, using the table
     * previously built, encode it into the output memory.
     * 将se内的数据统统的写入到cache中克.
     */
    rc = write_code_table_to_memory(&cache, se, symbol_count);
    if(rc == 0)
    {
       /*
        * 为什么write_code_table_to_memory成功之后还要要执行一次do_memory_encode?
        *
        */
       rc = do_memory_encode(&cache, bufin, bufinlen, se);
    }
   
    /* Flush the cache. */
    flush_cache( &cache );
   
    /* Free the Huffman tree. */
    free_huffman_tree( root );
    free_encoder( se );
    free_cache( &cache );
   
    return rc;
}
 
/**
 * 对bufin进行解码. 将解码后的数据写入到bufout中.
 */
int huffman_decode_memory(const unsigned char    *bufin,
                       unsigned int           bufinlen,
                       unsigned char       **pbufout,
                       unsigned int           *pbufoutlen)
{
    huffman_node *root, *p;
    unsigned int data_count;
    unsigned int i = 0;
    unsigned char *buf;
    unsigned int bufcur = 0;
 
    /* Ensure the arguments are valid. */
    if(!pbufout || !pbufoutlen) return 1;
      
    /* Read the Huffman code table. */
    root = read_code_table_from_memory(bufin, bufinlen, &i, &data_count);
    if(!root)  return 1;
 
    buf = (unsigned char*)malloc(data_count);
 
    /* Decode the memory. */
    p = root;
    for(; i < bufinlen && data_count > 0; ++i)
    {
       unsigned char byte = bufin[i];
       unsigned char mask = 1;
       while(data_count > 0 && mask)
       {
           p = byte & mask ? p->one : p->zero;
           mask <<= 1;
 
           if(p->isLeaf)
           {
              buf[bufcur++] = p->symbol;
              p = root;
              --data_count;
           }
       }
    }
 
    free_huffman_tree(root);
    *pbufout = buf;
    *pbufoutlen = bufcur;
   
    return 0;
}
 
/*
--------------------------------------------------------------------------------
 
不妨假设待编码的buffer为 "ABCAADC"
手工分析可得该树的形状  :
当然也可以也可以将这个树沿y方向翻转180度
               root
                /               /                A    *
                  / \                  
                 /                   C     *
                    /                     /                      B      D
 
现在我们知道的两个事实是 :
这个buffer内的字符数为         : symbol_count    = 7  ( "ABCAADC"一个有7个字符 )
这个buffer内出现的字符种数为    : count       = 4 ( 只出现了ABCD四种字符 )
 
接下来人工分析各个字符 :
symbol |   count  |       bits
--------|-----------|---------------------
  A     |     3     |   0000 0000
  B     |     1     |   0000 0110
  C     |     2     |   0000 0010
  D     |     1     |    0000 0111
我们设置左边为0, 右边为1. bits为从叶子节点走到root的路径.
 
分析完毕后, 需要实现整个编码过程. 编码过程暂时跳过.
假设成功编码完毕, 需要把编码后的数据写入到bufout内.
 
bufout内的
0-3个byte为字符种数 count
4-7个byte为字符个数 symbol_count
 
 
然后是遍历SymbolEncoder, 依次对每种字符进行编码(我们这个例子只进行4次编码)
我们对每种字符都会进行编码, 每个字符编码后的输出不妨称为frame
那么这个frame是由三个部分组成的:
 
(这个我们可以肯定一个char肯定是1byte的)
symbol (1byte)    -- 字符
 
(这个我们可以肯定就算这个树根本没有分支, 永远只有左/右孩子, 那也了不起是是256的深度)
numbits (1byte)   -- 叶子走到root需要的步数
 
bits   (1byte)    -- 叶子走到root的方式(即最终的编码, 比如说011)
开始我对这个bites到底占了多少个byte很是怀疑, 因为我不知道从叶子走到root
到底耗费了几步. 这里需要好好研究一下, 最好和最差情况. 暂时假设是个变化的byte吧.
但是有一点bites跟numbits是有关系的, 所以只要知道numbits还是可以知道bites占据了
多少byte的, 也知道bits到底是有几位.
    byte       content
---------------------------------------------
    0-3(4)     count
    4-7(4)     symbol_count
              
    A占xa个byte frame_struct
    B占xb个byte frame_struct
    C占xc个byte frame_struct
    D占xd个byte frame_struct
   
       X          X      
 
这个X是do_file_encode函数写到bufout中去的数据, 那么这个数据是什么呢?
实际上它是循环的把出现的字符的bits写到bufout中,
根据这个数据,解码的时候就可以依次的找到第0,1,2...个位置出现的是什么字符了
--------------------------------------------------------------------------------
*/
int
main(int argc, char** argv)
{
    FILE *origin  = fopen("main.cpp", "r+");
    FILE *out     = fopen("temp.txt", "w+");
    huffman_encode_file(origin, out);

    FILE *outfile = fopen("temp.txt", "r+");
    FILE *result  = fopen("out.cpp", "w+");
    huffman_decode_file(outfile, result);

    system("pause");
    return 1;
}

 

huffman编码压缩和解压

标签:

原文地址:http://www.cnblogs.com/xdyuklj/p/5136887.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!