📄 jpeg_ls.cpp
字号:
}
if(RUNindex>0)
RUNindex--;
for(j=0;j<output;j++){
*(f+LinX*(y+2)+RowX)=Ra;
RowX++;}
if(RowX>y){
RowX=RowX-y;
LinX++;
}
if(decode!=0)
{
while(((1<<(size-1))&decode)==0){
size--;
cnt++;
}
size--;
if(size==0)
decode=0;
else
decode=(decode<<(32-size))>>(32-size);
}
else{
while(decode==0)
{
fread(&buffer,1,1,fp);
decode=buffer;
size=size+8;
cnt=size;
}
int q=7;
while(((1<<q)&decode)==0)
q--;
cnt=cnt-8+7-q;
if(q==0)
decode=0;
else
decode=(decode<<(32-q))>>(32-q);
size=q;
}
Rb=*(f+(LinX-1)*(y+2)+RowX);
if(abs(Ra-Rb)<=NEAR)
RItype=1;
else
RItype=0;
if(RItype==1)
Px=Ra;
else
Px=Rb;
if((RItype==0)&&(Ra>Rb))
{
SIGN=-1;
}
else
SIGN=1;
if(RItype==0)
TEMP=A[365];
else
TEMP=A[366]+(N[366]>>1);
Q=RItype+365;
AQ=A[Q];
A[Q]=TEMP;
k=LG(Q);
if(cnt<(LIMIT-J[RUNindex]-qbpp-2))
{
if(k<=size)
{
output=decode>>(size-k);
if(size==k)
decode=0;
else
decode=((decode<<(32-size+k)))>>(32-size+k);
size=size-k;
}
else
{
fread(&buffer,1,1,fp);
decode1=0;
if(k-size<=8)
decode1=decode<<(k-size);
while(k-size>8)
{
decode1=decode1+(decode<<(k-size))+(buffer<<(k-size-8));
k=k-8;
fread(&buffer,1,1,fp);
decode=0;
}
output=decode1+(buffer>>(8+size-k));
if(8-k+size==0)
decode=0;
else
decode=(buffer<<(32-8+k-size))>>(32-8+k-size);
size=8-k+size;
}
EMErrval=(cnt<<k)+output;
}
else
{
if(qbpp<=size)
{
output=decode>>(size-qbpp);
if(size-qbpp==0)
decode=0;
else
decode=((decode<<(32-size+qbpp)))>>(32-size+qbpp);
size=size-qbpp;
}
else
{
fread(&buffer,1,1,fp);
decode1=0;
if(qbpp-size<=8)
decode1=decode<<(qbpp-size);
while(qbpp-size>8)
{
decode1=decode1+(decode<<(qbpp-size))+(buffer<<(qbpp-size-8));
qbpp=qbpp-8;
fread(&buffer,1,1,fp);
decode=0;
}
output=decode1+(buffer>>(8+size-qbpp));
if(8-qbpp+size==0)
decode=0;
else
decode=(buffer<<(32-8+qbpp-size))>>(32-8+qbpp-size);
size=8-qbpp+size;
}
EMErrval=output+1;
}
// **************************************
// * 由EMErrval求解Errval *
// **************************************
if(((EMErrval&1)==0)&&(RItype==1))
map=1;
else if(((EMErrval&1)==1)&&(RItype==1))
map=0;
else if(((EMErrval&1)==1)&&(RItype==0))
map=1;
else if(((EMErrval&1)==0)&&(RItype==0))
map=0;
if((map==0&&k!=0)||(map==0&&k==0&&2*Nn[Q-365]>=N[Q])||(map==1&&k==0&&2*Nn[Q-365]<N[Q]))
Errval=(EMErrval+RItype+map)>>1;
else
Errval=-((EMErrval+RItype+map)>>1);
flag=Errval;
Errval=Errval*(2*NEAR+1);
if(SIGN==-1)
Errval=-Errval;
Rx=Errval+Px;
if(Rx<(-NEAR))
Rx=Rx+RANGE*(2*NEAR+1);
if(Rx>(MAXVAL+NEAR))
Rx=Rx-RANGE*(2*NEAR+1);
if(Rx<0)
Rx=0;
else if(Rx>MAXVAL)
Rx=MAXVAL;
*(f+LinX*(y+2)+RowX)=Rx;
// **********************************
// * 更新其变量 *
// **********************************
if(flag<0)
Nn[Q-365]=Nn[Q-365]+1;
A[Q]=AQ;
A[Q]=A[Q]+((EMErrval+1-RItype)>>1);
if(N[Q]==RESET)
{
A[Q]=A[Q]>>1;
N[Q]=N[Q]>>1;
Nn[Q-365]=Nn[Q-365]>>1;
}
N[Q]=N[Q]+1;
}
void JPEG_LS::RegularModeProcessing(int y, int Ra, int Rb, int Rc, int Rd, int Ix, int D1, int D2, int D3, float T1, float T2, float T3)
{
// ********************
// * 当地梯度的量化 *
// ********************
int Di[4]={0,D1,D2,D3};
int Qi[4],Errval,MErrval;
int i,k,SIGN,Rx,Px;
unsigned interim;
output=0;
for(i=1;i<4;i++)
{
if(Di[i]<=-T3) Qi[i]=-4;
else if(Di[i]<=-T2) Qi[i]=-3;
else if(Di[i]<=-T1) Qi[i]=-2;
else if(Di[i]<-NEAR)Qi[i]=-1;
else if(Di[i]<=NEAR)Qi[i]=0;
else if(Di[i]<T1) Qi[i]=1;
else if(Di[i]<T2) Qi[i]=2;
else if(Di[i]<T3) Qi[i]=3;
else Qi[i]=4;
}
// **********************************************
// * 完成从矢量(Q1,Q2,Q3)到Q的一一对应的映射 *
// **********************************************
if((Qi[1]<0)||((Qi[1]==0)&&(Qi[2]<0))||(((Qi[1]==0)&&(Qi[2]==0))&&(Qi[3]<0)))
SIGN=-1;
else SIGN=1;
if(SIGN==-1)
{
for(i=1;i<4;i++)
Qi[i]=Qi[i]*SIGN;
}
Q=(Qi[1]*9+Qi[2])*9+Qi[3];/*人为设定*/
// *****************************
// * 计算Px的值(预测) *
// *****************************
if(Rc>=__max(Ra,Rb))
Px=__min(Ra,Rb);
else{
if(Rc<=__min(Ra,Rb))
Px=__max(Ra,Rb);
else
Px=Ra+Rb-Rc;
}/*边缘检测*/
/* if(Rc<=__max(Ra,Rb)){
if(Rc<=__min(Ra,Rb)){
if((10<=(Rd-Rb))&&(abs(Ra-Rb))<=10&&(__min(Ra,Rb)-Rc)>=5&&(Rd-Rb)<=50)
Px=Rd/2+__max(Ra,Rb)/2;
else
Px=__max(Ra,Rb);}
else
Px=Ra+Rb-Rc;}
else{
if(Rc-Ra>=10&&Rd<Rb&&Ra-Rb<=5)
Px=Rd/2+__min(Ra,Rb)/2;
else
Px=__min(Ra,Rb);
} */
if(SIGN==1)
Px=Px+C[Q];
else
Px=Px-C[Q];
// **********************************
// * 将Px规整到(0..MAXVAL)的范围 *
// **********************************
if(Px>MAXVAL)
Px=MAXVAL;
else if(Px<0)
Px=0;/*预测修正*/
// ********************
// * 计算Errval *
// ********************
Errval=Ix-Px;
if(SIGN==-1)
Errval=-Errval;
if(Errval>0)
Errval=(Errval+NEAR)/(2*NEAR+1);
else
Errval=-(NEAR-Errval)/(2*NEAR+1);/*量化预测误差*/
Rx=Px+SIGN*Errval*(2*NEAR+1);/*重建*/
if(Rx<0)
Rx=0;
else if(Rx>MAXVAL)
Rx=MAXVAL;
*(f+LinX*(y+2)+RowX)=Rx;/*用重建值代替实际值*/
// ******************************************************
// * 将Errval规整到[-(RANGE-1)/2..+RANGE/2)的范围 *
// ******************************************************
Errval=ModRange(Errval);
// ***********************************
// * 将Errval映射到MErrval *
// ***********************************
k=LG(Q);
if((NEAR==0)&&(k==0)&&(2*B[Q]<=-N[Q]))
{
if(Errval>=0)
MErrval=2*Errval+1;
else
MErrval=-2*(Errval+1);
}
else{
if(Errval>=0)
MErrval=2*Errval;
else
MErrval=-2*Errval-1;
}
// **********************************
// * 对MErrval进行编码 *
// **********************************
interim=MErrval;
interim=interim>>k;/*除最低k位以外的高位*/
if(interim<((unsigned)(LIMIT-qbpp-1)))
{
unsigned b,c;
c=~(~0<<k);
b=c&MErrval;/*截取MErrval的最低k位*/
output=(output<<(interim+1))+1;/*左移interim+1得到interim+1个零,加一后即interim个零接一个1*/
output=output<<k;
output=output+b;
// *******************************
// * 向二进制文件写码流 *
// *******************************
pp=cnt;
cnt=cnt+interim+1+k;
writecode(&cnt,&pp,&output,&code);
}
else
{
unsigned b,c;
output=(output<<(LIMIT-qbpp))+1;/*当前码流为:LIMIT-qbpp-1个零,一个1*/
output=output<<qbpp;/*后接qbpp位的数字*/
c=~(~0<<qbpp);
b=c&(MErrval-1);/*取MErrval-1的后qbpp位*/
output=output+b;
pp=cnt;
cnt=cnt+LIMIT;
writecode(&cnt,&pp,&output,&code);
}
// *********************************
// * 更新各个变量 *
// *********************************
B[Q]=B[Q]+Errval*(2*NEAR+1);
A[Q]=A[Q]+abs(Errval);
if(N[Q]==RESET)
{
A[Q]=A[Q]>>1;
B[Q]=B[Q]>>1;
N[Q]=N[Q]>>1;
}
N[Q]=N[Q]+1;/*N[Q]指此种上下文出现的次数,最多为64次*/
// Nt[Q]=Nt[Q]+1;
if(B[Q]<=-N[Q]){
B[Q]=B[Q]+N[Q];
if(C[Q]>MIN_C)
C[Q]=C[Q]-1;
if(B[Q]<=-N[Q])
B[Q]=-N[Q]+1;
}
else if(B[Q]>0){
B[Q]=B[Q]-N[Q];
if(C[Q]<MAX_C)
C[Q]=C[Q]+1;
if(B[Q]>0)
B[Q]=0;
}
}
void JPEG_LS::JPEG_LSDeCompress()
{
int x,y,i,j;
int Ra,Rb,Rc,Rd,D1,D2,D3;
int BASIC_T1=3,BASIC_T2=7,BASIC_T3=21;
float T1,T2,T3;
int qbpp,filemode;
struct timeb start_time,end_time;
int second_d;
x=y=256;
ftime(&start_time);
Initial();
//计算各参数的值
RANGE=((int)((MAXVAL+2*NEAR)/(2*NEAR+1)))+1;
qbpp=-(int)(floor(-log(RANGE)/log(2.0)));
bpp=__max(2,-(int)(floor(-log(MAXVAL+1)/log(2.0))));
LIMIT=2*(bpp+__max(8,bpp));
if(MAXVAL>=128){
T1=CLAMP_1((float)((int)(__min(MAXVAL,4095)+128)/256)*(BASIC_T1-2)+2+3*NEAR);
T2=CLAMP_2((float)((int)(__min(MAXVAL,4095)+128)/256)*(BASIC_T2-3)+3+5*NEAR,T1);
T3=CLAMP_3((float)((int)(__min(MAXVAL,4095)+128)/256)*(BASIC_T3-4)+4+7*NEAR,T2);}
else{
T1=CLAMP_1((float)__max(2,BASIC_T1/(int)(256/(MAXVAL+1))+3*NEAR));
T2=CLAMP_2((float)__max(3,BASIC_T2/(int)(256/(MAXVAL+1))+5*NEAR),T1);
T3=CLAMP_3((float)__max(4,BASIC_T3/(int)(256/(MAXVAL+1))+7*NEAR),T2);
}
f=(int *)calloc((x+1)*(y+2),sizeof(int));
if((fp=fopen("E:\\cuprite1.raw","rb"))==NULL)
{
AfxMessageBox("Can not Open File!");
return;
}
// 对各数组的初始化
for(j=0;j<365;j++)
{
A[j]=__max(2,(RANGE+(1<<5))/(1<<6));
N[j]=1;
B[j]=0;
C[j]=0;
}
A[365]=A[0];
A[366]=A[0];
N[365]=1;
N[366]=1;
// 读入变量
for(i=0;i<(x+1)*(y+2);i++)
*(f+i)=0;
while(RowX<=y&&LinX<=x)
{
Ra=(*(f+LinX*(y+2)+RowX-1));
Rb=(*(f+(LinX-1)*(y+2)+RowX));
Rc=(*(f+(LinX-1)*(y+2)+RowX-1));
Rd=(*(f+(LinX-1)*(y+2)+RowX+1));
D1=Rd-Rb;
D2=Rb-Rc;
D3=Rc-Ra;
// 选择解码方式
if((abs(D1)<=NEAR)&&(abs(D2)<=NEAR)&&(abs(D3)<=NEAR))
RunModeProcessing(qbpp,Ra,Rb,y);
else
DeModeProcessing(qbpp,Ra,Rb,Rc,Rd,D1,D2,D3,y,T1,T2,T3);
if(RowX==y)
{
*(f+LinX*(y+2)+y+1)=*(f+LinX*(y+2)+y);
*(f+(LinX+1)*(y+2))=*(f+LinX*(y+2)+1);
}
RowX++;
if(RowX>y)
{
RowX=RowX-y;
LinX++;
}
}
fclose(fp);
// 文件输出格式选择
if((fp=fopen("E:\\decuprite.raw","wb"))==NULL)
{
AfxMessageBox("Can not Open File!");
return;
}
for(i=1;i<x+1;i++)
for(j=1;j<y+1;j++)
fwrite((f+i*(y+2)+j),1,1,fp);
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);
}
void JPEG_LS::DeModeProcessing(int qbpp, int Ra, int Rb, int Rc, int Rd, int D1, int D2, int D3, int y, float T1, float T2, float T3)
{
// ********************
// * 当地梯度的量化 *
// ********************
int Di[4]={0,D1,D2,D3};
int Qi[4],Q;
int SIGN,k,q,decode1,Rx,i;
int Px,Errval,MErrval;
int cnt=0;
output=0;
for(i=1;i<4;i++)
{
if(Di[i]<=-T3) Qi[i]=-4;
else if(Di[i]<=-T2) Qi[i]=-3;
else if(Di[i]<=-T1) Qi[i]=-2;
else if(Di[i]<(-NEAR)) Qi[i]=-1;
else if(Di[i]<=NEAR) Qi[i]=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -