📄 calic1decode.cpp
字号:
for (i = 1; i <= CodeValueBits; i++) {
if (!D->bit_index) Input_Byte(D);
D->value += D->value + (D->bit_buffer & 1);
D->bit_buffer >>= 1; --D->bit_index; }
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
void Stop_Decoder(Decoder * D)
{
fclose(D->in_file);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
int Read_Symbol(Decoder * D, Adaptive_Model * M)
{
int symbol = Select_Symbol(M, D->value, &(D->low), &(D->high));
Update_Decoder(D);
return symbol;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
int Read_Bits(Decoder * D, int bits)
{
long lm1 = D->low - 1, range = D->high - lm1, prod = range;
int word = (int) ((((D->value - lm1) << bits) - 1) / range);
prod *= word;
D->high = lm1 + ((prod + range) >> bits);
D->low += prod >> bits;
Update_Decoder(D);
return word;
}
/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
// ****************************************
// * 常规解码过程的子程序 *
// ****************************************
void RegularModeProcessing(int y,int Rw,int Rww,int Rn,int Rnw,int Rne,int Rnn,int Rnne,int q1,int q2,int q3,int q4,int q5,int q6,int q7)
{
// ********************
// * 当地梯度的量化 *
// ********************
int b0,b1,b2,b3,b4,b5,b6,b7;
int dh,dv,B,e,L;
int SIGN,Rx,Px;
int Errval,MErrval;
b0=b1=b2=b3=b4=b5=b6=b7=0;
dh=abs(Rw-Rww)+abs(Rn-Rnw)+abs(Rn-Rne);
dv=abs(Rw-Rnw)+abs(Rn-Rnn)+abs(Rne-Rnne);
// ******************************
// * 预测器 *
// ******************************
if(dv-dh>80)
Px=Rw;
else if(dv-dh<-80)
Px=Rn;
else{
Px=(Rw+Rn)/2+(Rne-Rnw)/4;
if(dv-dh>32)
Px=(Px+Rw)/2;
else if(dv-dh>8)
Px=(3*Px+Rw)/4;
else if(dv-dh<-32)
Px=(Px+Rn)/2;
else if(dv-dh<-8)
Px=(3*Px+Rn)/4;
}
// *****************************
// * 结构上下文 *
// *****************************
if(Rn<Px)
b0=1;
if(Rw<Px)
b1=1;
if(Rnw<Px)
b2=1;
if(Rne<Px)
b3=1;
if(Rnn<Px)
b4=1;
if(Rww<Px)
b5=1;
if(2*Rn-Rnn<Px)
b6=1;
if(2*Rw-Rww<Px)
b7=1;
B=(b7<<7)+(b6<<6)+(b5<<5)+(b4<<4)+(b3<<3)+(b2<<2)+(b1<<1)+b0;
// *********************************
// * 误差能量量化 *
// *********************************
e=dh+dv+2*abs(Ew);
if(e<q1) L=0;
else if(e<q2) L=1;
else if(e<q3) L=2;
else if(e<q4) L=3;
else if(e<q5) L=4;
else if(e<q6) L=5;
else if(e<q7) L=6;
else L=7;
if(Offset[L/2][B]<0)
SIGN=-1;
else
SIGN=1;
// *********************
// * 计算Px的值 *
// *********************
Px=Px+Offset[L/2][B];
// **********************************
// * 将Px规整到(0..MAXVAL)的范围 *
// **********************************
if(Px>MAXVAL)
Px=MAXVAL;
else if(Px<0)
Px=0;
// ******************************************************
// * 从码流中恢复重建值Rx *
// ******************************************************
MErrval=Read_Symbol(&decod, &model[L]);
if((MErrval&1)==0)
Errval=((MErrval)>>1);
else
Errval=-((MErrval+1)>>1);
Ew=Errval;
// *********************************
// * 更新各个变量 *
// *********************************
C[L/2][B]=C[L/2][B]+SIGN*Errval;
if(N[L/2][B]==RESET)
{
C[L/2][B]=C[L/2][B]>>1;
N[L/2][B]=N[L/2][B]>>1;
}
N[L/2][B]=N[L/2][B]+1;
Offset[L/2][B]=C[L/2][B]/N[L/2][B];
Errval=Errval*(2*NEAR+1);
if(SIGN==-1)
Errval=-Errval;
Rx=Errval+Px;
if(Rx<(-NEAR))
Rx=Rx+RANGE*(2*NEAR+1);
else if(Rx>(MAXVAL+NEAR))
Rx=Rx-RANGE*(2*NEAR+1);
if(Rx<0)
Rx=0;
else if(Rx>MAXVAL)
Rx=MAXVAL;
*(f+(LinX+1)*(y+3)+RowX+1)=Rx;
}
// **********************************
// * 游程解码子程序的定义 *
// **********************************
void RunModeProcessing(int B,int s1,int s2,int y){
int T;
T=Read_Symbol(&decod, &model[B+8]);
while(T==1||T==0){
if(T==0)
*(f+(LinX+1)*(y+3)+RowX+1)=s1;
if(T==1)
*(f+(LinX+1)*(y+3)+RowX+1)=s2;
RowX++;
if(RowX==y){
*(f+(LinX+2)*(y+3))=*(f+(LinX+1)*(y+3)+1);
*(f+(LinX+2)*(y+3)+1)=*(f+(LinX+1)*(y+3)+2);
}
if(RowX>y){
*(f+(LinX+1)*(y+3)+y+2)=*(f+(LinX+1)*(y+3)+y+1);
RowX=RowX-y;
LinX++;}
T=Read_Symbol(&decod, &model[B+8]);
}
}
// *************************************************
// * 主程序 *
// *************************************************
// i,j:循环变量计数;
// _______________________
// | | Rc| Rb| Rd| |
// -----------------------
// | | Ra| Ix| | |
// -----------------------
// D1,D2,D3:当前梯度
// x,y:图像的长度和宽度
void main()
{
int x,y,i,j;
int Rw,Rww,Rnw,Rn,Rne,Rnn,Rnne;
int q1=5,q2=15,q3=25,q4=42,q5=60,q6=85,q7=140;
int filemode;
struct timeb start_time,end_time;
int second_d;
int s1,s2;
int flag=1;
printf("图象长度和高度\n");
scanf("%d,%d",&x,&y);
printf("选择文件格式(0:Adata5.dat,1:Adata5.raw)\n");
scanf("%d",&filemode);
ftime(&start_time);
// *********************************
// * 计算各参数的值 *
// *********************************
RANGE=((int)((MAXVAL+2*NEAR)/(2*NEAR+1)))+1;
f=(int *)calloc((x+2)*(y+3),sizeof(int));
// ******************************
// * 对各数组的初始化 *
// ******************************
for(i=0;i<4;i++)
for(j=0;j<256;j++)
{
N[i][j]=1;
C[i][j]=0;
Offset[i][j]=0;
}
// ************************
// * 读入变量 *
// ************************
for(i=0;i<(x+2)*(y+3);i++)
*(f+i)=0;
Start_Decoder(&decod, "E:\\felicia\\test.cod"); /* initialize decododer, open file */
for(i=0;i<8;i++)
Create_Model(&model[i], RANGE);
for(i=8;i<40;i++)
Create_Model(&model[i], 3);
while(RowX<=y&&LinX<=x)
{
Rw=(*(f+(LinX+1)*(y+3)+RowX));
Rww=(*(f+(LinX+1)*(y+3)+RowX-1));
Rn=(*(f+(LinX)*(y+3)+RowX+1));
Rnw=(*(f+(LinX)*(y+3)+RowX));
Rne=(*(f+(LinX)*(y+3)+RowX+2));
Rnn=(*(f+(LinX-1)*(y+3)+RowX+1));
Rnne=(*(f+(LinX-1)*(y+3)+RowX+2));
s1=Rw;
s2=-1;
if(Rww!=Rw) s2=Rww;
if(Rnw!=Rw) s2=Rnw;
if(Rn!=Rw) s2=Rn;
if(Rne!=Rw) s2=Rne;
if(Rnn!=Rw) s2=Rnn;
if(flag==0||(Rww!=s1&&Rww!=s2)||(Rnw!=s1&&Rnw!=s2)||(Rn!=s1&&Rn!=s2)||(Rne!=s1&&Rne!=s2)||(Rnn!=s1&&Rnn!=s2)){
RegularModeProcessing(y,Rw,Rww,Rn,Rnw,Rne,Rnn,Rnne,q1,q2,q3,q4,q5,q6,q7);
flag=1;
}
else{
int b0=1,b1=1,b2=1,b3=1,b4=1,B;
if(Rnn==s1)
b4=0;
if(Rww==s1)
b3=0;
if(Rne==s1)
b2=0;
if(Rnw==s1)
b1=0;
if(Rn==s1)
b0=0;
B=b4*16+b3*8+b2*4+b1*2+b0;
RunModeProcessing(B,s1,s2,y);
flag=0;
RowX--;
if(RowX==0){
LinX--;
RowX=y;
*(f+(LinX+2)*(y+3))=*(f+(LinX+1)*(y+3)+1);
*(f+(LinX+2)*(y+3)+1)=*(f+(LinX+1)*(y+3)+2);
}
}
RowX++;
if(RowX==y)
{
*(f+(LinX+2)*(y+3))=*(f+(LinX+1)*(y+3)+1);
*(f+(LinX+2)*(y+3)+1)=*(f+(LinX+1)*(y+3)+2);
}
if(RowX>y)
{
*(f+(LinX+1)*(y+3)+y+2)=*(f+(LinX+1)*(y+3)+y+1);
RowX=RowX-y;
LinX++;
}
}
Stop_Decoder(&decod);
// *************************************
// * 文件输出格式选择 *
// *************************************
if(filemode==0)
{
if((fp=fopen("D:\\Adata5.dat","wt"))==NULL)
{
printf("cannot open the file\n");
exit(0);
}
for(i=1;i<x+1;i++)
{
for(j=1;j<y+1;j++)
fprintf(fp,"%5d",*(f+(i+1)*(y+3)+j+1));
fprintf(fp,"\n");
}
}
else
{
if((fp=fopen("D:\\Adata5.raw","wb"))==NULL)
{
printf("cannot open the file\n");
exit(0);
}
for(i=1;i<x+1;i++)
for(j=1;j<y+1;j++)
fprintf(fp,"%c",(unsigned char)*(f+(i+1)*(y+3)+j+1));
}
fclose(fp);
// **************************
// * 计算编码时间 *
// **************************
ftime(&end_time);
second_d=end_time.millitm-start_time.millitm;
second_d=second_d+(end_time.time-start_time.time)*1000;
printf("The encoding costs:%.3f seconds.\n",(float)second_d/1000.0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -