⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 predic.c

📁 JPEG压缩和解压程序和一些相关的说明文档 内容比较全
💻 C
字号:
/*This file include programe accomplish the prediction discribed in JPEG_LS standard. *The author is Fujian Shi,email fieagle@yahoo.com.cn. */#include "commondecls.h"#define   PIXEL  unsigned char #define   UNI_TYPE int#define T1 3#define T2 7#define T3 21#define NEAR 6#define PREDICTION(Rb, Ra, Rc,Px)	\{	\	register PIXEL minx;	\	register PIXEL maxx;	\	\	if (Rb > Ra) {	\		minx = Ra;	\		maxx = Rb;	\	} else {	\		maxx = Ra;	\		minx = Rb;	\	}	\	if (Rc >= maxx)	\		Px = minx;	\	else if (Rc <= minx)	\		Px = maxx;	\	else	\		Px = Ra + Rb - Rc;	\}#define   QUAN_GRAD(D,q) if (D<=-T3)q=-4;\else if (D<=-T2) q=-3;\else if (D<=-T1) q=-2;\else if (D< -NEAR) q=-1;\else if (D<=NEAR) q=0;\else if (D<T1) q=1;\else if (D<T2) q=2;\else if (D<T3) q=3;\else q=4#define PO_OR_NE(Q)  *sign_ptr= Q >= 0 ? 1 : (-1)#define MAKE_FIRST_POSITIVE(Q1,Q2,Q3)       Q1 *=*sign_ptr;Q2 *=*sign_ptr;Q3 *=*sign_ptr#define ABS(Q) Q >= 0 ? Q : -QLOCAL(int) find_bin(UNI_TYPE Q1,UNI_TYPE Q2,UNI_TYPE Q3,UNI_TYPE *sign_ptr){  int position=0;    if (Q1 != 0) {    PO_OR_NE(Q1);    MAKE_FIRST_POSITIVE(Q1,Q2,Q3);    position +=41;    Q1--;    if (Q2 < 0) {      position +=45;      Q2 = ABS(Q2)-1;    }    if (Q3 <0) {      position +=5;      Q3 =ABS(Q3)-1;    }    position +=81*Q1 + 9*Q2 + Q3;  }else if (Q2 != 0) {    PO_OR_NE(Q2);    MAKE_FIRST_POSITIVE(Q1,Q2,Q3);    position +=5;    Q2--;    if (Q3 < 0) {      position +=5;      Q3 =ABS(Q3)-1;    }    position +=9*Q2 + Q3;  }else {    PO_OR_NE(Q3);    MAKE_FIRST_POSITIVE(Q1,Q2,Q3);    position +=Q3;  }  return position;  } intget_errval(j_compress_ptr cinfo,int Ra, PIXEL Rb, int Rc,PIXEL  Rd,PIXEL *x){  UNI_TYPE D1,D2,D3;  UNI_TYPE Q1,Q2,Q3,Q;  int px;  UNI_TYPE sign=1,errval,errval_coded;  predic_structure_ptr pre=&cinfo->pre;  /*Caculate the context bin and the predic value px.*/  D1=Rd-Rb;  D2=Rb-Rc;  D3=Rc-Ra;  QUAN_GRAD(D1,Q1);  QUAN_GRAD(D2,Q2);  QUAN_GRAD(D3,Q3);  Q=find_bin(Q1,Q2,Q3,&sign);  PREDICTION(Rb,Ra,Rc,px)  /*Correct the prediction*/  px +=sign*pre->C[Q];  if (px >255) px=255;  if (px<0) px=0;  errval=(*x - px)*sign;  if (errval >=0) errval=(errval+NEAR)/(2*NEAR+1);  else errval=-(NEAR-errval)/(2*NEAR+1);  errval_coded=errval;  px=px+sign*errval*(2*NEAR+1);  if (px<0) px=0;  if (px>255) px=255;  *x=px;  if (errval<0 ) errval +=256;  if (errval >= 128) errval -=256;  /*Update the N[Q], B[Q],C[Q]to accomplish  context self-adaption*/  pre->B[Q] +=errval*(2*NEAR+1);  if (pre->N[Q]==pre->reset) {    pre->B[Q] >>=1;    pre->N[Q] >>=1;  }  pre->N[Q] +=1;  /* Do bias estimation for NEXT pixel */  /* Bias cancelation tries to put error in (-1,0] (A.6.2)*/	  if  ( pre->B[Q] <= -pre->N[Q] ) {    if (pre->C[Q] > -128)      --pre->C[Q];    if ( (pre->B[Q] += pre->N[Q]) <= -pre->N[Q] ) 			pre->B[Q] = -pre->N[Q]+1;  } else if ( pre->B[Q] > 0 ) {           if (pre->C[Q] < 127)             ++pre->C[Q];	   if ( (pre->B[Q] -= pre->N[Q]) > 0 )	     pre->B[Q] = 0;  }  /*printf("C[%d]=%d\n",Q,pre->C[Q]);*/  return errval_coded;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -