Example #1
0
/*
 * huffman_encode_file huffman encodes in to out.
 */
int
huffman_encode_file(FILE *in, FILE *out)
{
    SymbolFrequencies sf;
    SymbolEncoder *se;
    huffman_node *root = NULL;
    int rc;
    unsigned int symbol_count;

    /* Get the frequency of each symbol in the input file. */
    symbol_count = get_symbol_frequencies(&sf, in);

    /* Build an optimal table from the symbolCount. */
    se = calculate_huffman_codes(&sf);
    root = sf[0];

    /* Scan the file again and, using the table
       previously built, encode it into the output file. */
    rewind(in);
    rc = write_code_table(out, se, symbol_count);
    if(rc == 0)
        rc = do_file_encode(in, out, se);

    /* Free the Huffman tree. */
    free_huffman_tree(root);
    free_encoder(se);
    return rc;
}
Example #2
0
/* 把 in 压缩成 out */
int huffman_encode_file(FILE *in, FILE *out)
{
    symfreq sf;
    symcode *sc;
    node *root = NULL;
    int rc;
    unsigned int syms;
    /* 获取输入文件中符号出现频率 */
    syms = get_symfreq(&sf, in);
    /* 生成编码表 */
    sc = calc_code(&sf);
    root = sf[0];
    /* 再次扫描文件,使用之前生成的编码表来编码 */
    rewind(in);
    rc = write_code_table(out, sc, syms);
    if (rc == 0)
        rc = do_file_encode(in, out, sc);
    /* 释放空间 */
    free_tree(root);
    free_encoder(sc);
    return rc;
}