📄 jpeg-ls-decode.cpp
字号:
// ******************************************************************
// * JPEG_LS_decode源程序清单 *
// ******************************************************************
// ************
// *包含库文件*
// ************
#include <iostream.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys\types.h>
#include <sys\timeb.h>
// ************
// * 宏定义 *
// ************
//RESET: threshold value at which A,B,and N are halved
//NEAR: difference bound for near-lossless coding
//MIN_C: minimum allowed value of C[0..364],equal to -128
//MAX_C: maximum allowed value of C[0..364],equal to 127
#define RESET 64
#define NEAR 0
#define MIN_C -128
#define MAX_C 127
#define MAXVAL 255
// ************************************
// * 全局变量初始化 *
// ************************************
//MAXVAL: maximum possible image sample value ove all component of a scan
//RUNindex: index for run mode order
//qbpp: number of bits needed to represent a mapped error value
//bpp: number of bits needed to represent MAXVAL,with a minimum of 2
//LIMIT: the value of glimit for a sample encoded in regular mode
//Q: context determined from Q1,Q2,Q3
//RANGE: range of prediction error representation
//A[0..366]:367 counters for the accumulated preditection error magnitude
//B[0..364]:365 counters for computing the bias
//C[0..364]:365 counters storing prediction coreection values
//J[0..31]: 32 variables indicating order of run-length codes
//N[0..366]:367 counters for frequency of occurrence of each context
//Nn[365..366]:2 counters for negative prediction error for run interruption
//EOLine: end of line indicator,used in run mode
//Errval: prediction error
//EMErrval: Errval mapped to non-negative integers in run interruption mode
//MErrval: Errval mapped to non-negative integers in regular mode
//--------------------------------------------------------------------------------
// LinX:当前行号
// RowX:当前列号
// *f:指向码流的指针
// *fp:文件指针
// buffer:数值缓冲器
// output:解码的数值
// size:当前码流的长度
// decode:当前码流的数值
// decode2:临时变量
// flag1:标志位
unsigned long buffer=0,output=0,decode=0,decode2=0;
int size=0;
int LinX=1,RowX=1;
int RANGE;
static int RUNindex=0;
int *f;
int flag1=0,bpp;
int B[365],C[365];
int LIMIT;
int J[32]={0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,5,5,6,6,7,7,8,9,10,11,12,13,14,15};
int A[367],N[367];
int Nn[2]={0};
int EOLine=0;
FILE *fp;
// **********************************
// * 计算T1,T2,T3值的子程序 *
// **********************************
float CLAMP_1(float i)
{
if(i>MAXVAL||i<NEAR+1)
return(NEAR+1);
else
return(i);
}
float CLAMP_2(float i,float T1)
{
if(i>MAXVAL||i<T1)
return(T1);
else
return(i);
}
float CLAMP_3(float i,float T2)
{
if(i>MAXVAL||i<T2)
return(T2);
else
return(i);
}
// ******************************
// * 求算术编码的长度的子程序 *
// ******************************
int LG(int Q)
{
int i;
for(i=0;(N[Q]<<i)<A[Q];i++);
return(i);
}
// ****************************************
// * 常规解码过程的子程序 *
// ****************************************
void RegularModeProcessing(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;
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);
c=1;
if(Rc-__max(Ra,Rb)>=10)
Px--; }
else{
if(Rc<=__min(Ra,Rb)){
Px=__max(Ra,Rb);
c=1;
if(__min(Ra,Rb)-Rc>=10)
Px++;}
else{
Px=Ra+Rb-Rc;
c=0;}
}
if(c==0)
Px=(Px+Ra+Rb)/3;*/
/*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(Rc>=__max(Ra,Rb))
Px=__min(Ra,Rb);
else{
if(Rc<=__min(Ra,Rb))
Px=__max(Ra,Rb);
else
Px=Ra+Rb-Rc;
}
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;
// ******************************************************
// * 从码流中恢复重建值Rx *
// ******************************************************
k=LG(Q);
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;/*以8bit为单位处理*/
cnt=size;
}
q=7;/*预设高几位形成的q=7(是处理单元能达到的最大值)*/
while(((1<<q)&decode)==0)
q--;
cnt=cnt-8+7-q;/*0的个数*/
if(q==0)
decode=0;
else
decode=(decode<<(32-q))>>(32-q);/*处理后k位,之前置零*/
size=q;
}
if(cnt<(LIMIT-qbpp-1))/*情况1*/
{
if(k<=size)
{
output=decode>>(size-k);/*得到后k位*/
if(size-k==0)
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));/*第二项表示在k<size的单元里,得到当前的k位数*/
if(8-k+size==0)
decode=0;
else
decode=(buffer<<(32-8+k-size))>>(32-8+k-size);/*处理结束,置零*/
size=8-k+size;
}
MErrval=(cnt<<k)+output;
}
else/*情况2*/
{
if(qbpp<=size)
{
output=decode>>(size-qbpp);
if(size==qbpp)
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;
}
MErrval=output+1;
}
if((NEAR==0)&&(k==0)&&(2*B[Q]<=-N[Q]))
{
if((MErrval&1)==1)
Errval=(MErrval-1)>>1;
else
Errval=(-(MErrval>>1))-1;
}
else
{
if((MErrval&1)==0)
Errval=((MErrval)>>1);
else
Errval=-((MErrval+1)>>1);
}
// *********************************
// * 更新各个变量 *
// *********************************
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;
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*(y+2)+RowX)=Rx;
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 RunModeProcessing(int qbpp,int Ra,int Rb,int y)
{
int decode1=0;
int cnt=0,i;
unsigned long j;
int RItype,TEMP,AQ,Errval,EMErrval,flag,map,SIGN,k,Px,Rx,Q;
// **********************************
// * 从码流中恢复EMErrval *
// **********************************
if(size==0){
fread(&buffer,1,1,fp);
decode=buffer;
size=8;}/*以8bit为一个处理单位*/
size--;
while((1&(decode>>size))==1)
{
size--;
i=0;
while(i<(1<<J[RUNindex]))/*写一个Ra*/
{
*(f+LinX*(y+2)+RowX)=Ra;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -