资源简介
算数编码程序,可以实现对文本的算数编码压缩。
代码片段和文件信息
//------------------------------------------------------------
// arith-n.cpp - order-n arithmetic coding module
#include
#include
#include
#include “errhand.h“
#include “bitio.h“
// low_count 和 high_count 唯一地定义了在 0 到 1 范围中符号的所在位置
// scale 指 0 到 1 范围内的总量程,即有多少字符要记数
typedef struct
{
unsigned short int low_count;
unsigned short int high_count;
unsigned short int scale;
}SYMBOL;
void build_model(FILE *inputFILE *output);
void scale_counts(unsigned long counts[]unsigned char scaled_counts[]);
void build_totals(unsigned char scaled_counts[]);
void count_bytes(FILE *inputunsigned long counts[]);
void output_counts(FILE *outputunsigned char scaled_counts[]);
void input_counts(FILE *stream);
void convert_int_to_symbol(int symbolSYMBOL *s);
void get_symbol_scale(SYMBOL *s);
int convert_symbol_to_int(int countSYMBOL *s);
void initialize_arithmetic_encoder(void);
void encode_symbol(BIT_FILE *streamSYMBOL *s);
void flush_arithmetic_encoder(BIT_FILE *stream);
short int get_current_count(SYMBOL *s);
void initialize_arithmetic_decoder(BIT_FILE *stream);
void remove_symbol_from_stream(BIT_FILE *streamSYMBOL *s);
#define END_OF_STREAM 256
short int totals[258];
char* CompressionName = “Adaptive order n moder with arithmetic coding“;
char* Usage = “in-file out-file [-o order]\n\n“;
void CompressFile( FILE* input BIT_FILE* output int argc char * argv[] )
{
int c;
SYMBOL s;
build_model(inputoutput->file);
initialize_arithmetic_encoder();
while((c=getc(input))!=EOF){
convert_int_to_symbol(c&s);
encode_symbol(output&s);
}
convert_int_to_symbol(END_OF_STREAM&s);
encode_symbol(output&s);
flush_arithmetic_encoder(output);
OutputBits(output0L16);
while(argc-->0){
printf(“Unused argument:%s\n“*argv);
argv++;
}
}
void ExpandFile( BIT_FILE* input FILE* output int argc char* argv[])
{
SYMBOL s;
int c;
int count;
input_counts(input->file);
initialize_arithmetic_decoder(input);
for (;;)
{
get_symbol_scale( &s );
count = get_current_count( &s );
c = convert_symbol_to_int( count &s );
if(c==END_OF_STREAM)
break;
remove_symbol_from_stream( input &s );
putc((char)coutput);
}
while(argc-->0)
{
printf(“Unused argument:%s\n“*argv);
argv++;
}
}
void build_model(FILE *inputFILE *output)
{
unsigned long counts[256];
unsigned char scaled_counts[256];
count_bytes(inputcounts);
scale_counts(countsscaled_counts);
output_counts(outputscaled_counts);
build_totals(scaled_counts);
}
#ifndef SEEK_SET
#define SEEK_SET 0
#endif
void count_bytes(FILE *inputunsigned long counts[])
{
long input_marker;
int i;
int c;
for(i=0;i<256;i++)
counts[i]=0;
input_marker=ftell(input);
while((c=getc(input))!=EOF)
counts[c]++;
fseek(inputinput_markerSEEK_SET);
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 541 2009-06-08 18:49 encoding1\encoding1.dsw
文件 41984 2009-06-08 18:55 encoding1\encoding1.ncb
文件 267 2004-11-14 12:02 encoding1\errhand.cpp
文件 142 2004-11-14 12:03 encoding1\errhand.h
文件 311 2004-11-14 12:03 encoding1\main.h
文件 2066 2004-11-14 12:04 encoding1\main-c.cpp
文件 8915 2009-06-02 15:43 encoding1\arith.cpp
文件 3389 2004-11-14 12:02 encoding1\bitio.cpp
文件 815 2004-11-14 12:02 encoding1\bitio.h
文件 1342 2009-06-08 18:50 encoding1\encoding1.plg
文件 49664 2009-06-08 18:55 encoding1\encoding1.opt
文件 4688 2009-06-08 18:55 encoding1\encoding1.dsp
文件 41984 2009-06-08 18:50 encoding1\Debug\vc60.idb
文件 53248 2009-06-08 18:50 encoding1\Debug\vc60.pdb
文件 7101 2009-06-08 18:50 encoding1\Debug\main-c.obj
文件 19149 2009-06-08 18:50 encoding1\Debug\arith.obj
文件 10373 2009-06-08 18:50 encoding1\Debug\bitio.obj
文件 220808 2009-06-08 18:50 encoding1\Debug\encoding1.pch
文件 2144 2009-06-08 18:50 encoding1\Debug\errhand.obj
文件 199904 2009-06-08 18:50 encoding1\Debug\encoding1.ilk
文件 192561 2009-06-08 18:50 encoding1\Debug\encoding1.exe
文件 394240 2009-06-08 18:50 encoding1\Debug\encoding1.pdb
目录 0 2009-06-08 19:01 encoding1\Debug
目录 0 2009-06-08 19:01 encoding1
----------- --------- ---------- ----- ----
1255636 24
评论
共有 条评论