算术编码用c++的实现

王朝c/c++·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

算术编码在图象数据压缩标准(如jpeg,jbig)中扮演了重要的角色。在算术编码中,消息用0到1之间的实数进行编码。算术编码用到了两个基本的参数:符号的概率和它的编码间隔。信源符号的概率决定压缩编码的效率,也决定编码过程中信源符号的间隔,而这些间隔包含在0到1之间。编码过程中的间隔决定了符号压缩后的输出。

算术编码需要输入的是符号,各个符号的概率还有需要编码的符号序列,根据概率可以算出初始编码间隔,先设几个变量在后面可用:High——当前编码的上限,Low——当前编码的下限,high——中间变量,用来计算下一个编码符号的当前间隔的上限,low——中间变量,用来计算下一个编码符号的当前间隔的下限,d——当前间隔之间的距离。第1个编码符号的当前间隔为其初始的编码间隔,第i个编码符号的当前间隔为第i-1个编码后的[Low,High),第i+1个编码符号的当前间隔算法如下:high=Low+d*第i+1个初始编码符号对应的上限,low=Low+d*第i+1个编码符号对应的下限,然后High=high,Low=low,d=d*第i个编码符号的概率。

编码程序如下:

#include <iostream.h>

#define M 100

#define N 4

class suanshu

{

int count,length;

char number[N],n;

long double chance[N],c;

char code[M];

long double High,Low,high,low,d;

public:

suanshu()

{High=0;Low=0;}

void get_number();

void get_code();

void coding();

~suanshu(){}

};

void suanshu::get_number()

{

cout<<"please input the number and its chance."<<endl;

for(int i=0;i<N;i++)

{

cin>>n>>c;

number[i]=n;

chance[i]=c;

}

if(i==20)

cout<<"the number is full."<<endl;

count=i;

}

void suanshu::get_code()

{

cout<<"please input the code's length:";

cin>>length;

while(length>=M)

{

cout<<"the length is too larger,please input a smaller one.";

cin>>length;

}

for(int i=0;i<length;i++)

{

cin>>code[i];

}

}

void suanshu::coding()

{

int i,j=0;

for(i=0;i<count;i++)

if(code[0]==number[i]) break;

while(j<i)

Low+=chance[j++];

d=chance[j];

High=Low+d;

for(i=1;i<length;i++)

for(j=0;j<count;j++)

{

if(code[i]==number[j])

{

if(j==0)

{

low=Low;

high=Low+chance[j]*d;

High=high;

d*=chance[j];

}

else

{

float chance_l=0.0;

for(int k=0;k<=j-1;k++)

chance_l+=chance[k];

low=Low+d*chance_l;

high=Low+d*(chance_l+chance[j]);

Low=low;

High=high;

d*=chance[j];

}

}

else continue;

}

cout<<"the result is:"<<Low<<endl;

}

int main()

{

suanshu a;

a.get_number();

a.get_code();

a.coding();

return 0;

}

本程序在VC6.0和xp专业版下运行通过,这是我个人第一次用c++写的比较完整的程序,还有些不尽人意的地方,比如变量和函数命名不太专业,以后会注意,慢慢也会好的。呵呵~

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航