?? dpcmprocess.cpp
字號:
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;
}
int CDpcmProcess::Get_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;
}
void * CDpcmProcess::Alloc_One_Row(j_compress_ptr cinfo, size_t size_object)
{
void *buffer_ptr;
buffer_ptr=cinfo->buffer[cinfo->buffer_count++]=(void *)
malloc(size_object);
return buffer_ptr;
}
JSAMPARRAY CDpcmProcess::Alloc_Sarray(JDIMENSION samplesperrow, JDIMENSION numrows, j_compress_ptr cinfo)
{
JSAMPARRAY result;
JSAMPROW workspace;
int currow;
/* Get space for row pointers (small object) */
result = (JSAMPARRAY)Alloc_One_Row(cinfo,((size_t) numrows) * SIZEOF(JSAMPROW));
/* Get the rows themselves (large objects) */
currow = 0;
while (currow < (int)numrows) {
workspace = (JSAMPROW)Alloc_One_Row(cinfo,((size_t) (samplesperrow)
* SIZEOF(JSAMPLE)));
result[currow++] = workspace;
}
return result;
}
void CDpcmProcess::Free_Mem(j_compress_ptr cinfo)
{
int i;
for (i=0;i<cinfo->buffer_count;i++)
free(cinfo->buffer[i]);
}
void CDpcmProcess::Input_Image_Data(j_compress_ptr cinfo)
{
int j,input_lines;
JSAMPARRAY in_array=cinfo->inbuffer;
input_lines=1;
for(j=0;j<input_lines;j++)
JFREAD(cinfo->inputfile,in_array[j],cinfo->image_width);
}
void CDpcmProcess::Compress_Data(j_compress_ptr cinfo, int i)
{
JSAMPARRAY in_array=cinfo->inbuffer;
/*dipose a row of image*/
Encode_Row (cinfo,in_array[0],i);
}
void CDpcmProcess::Initial_Decoder(j_compress_ptr cinfo)
{
arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
int data;
data=Get_Byte(cinfo);
e->c +=(data<<8);
e->c <<=8;
data=Get_Byte(cinfo);
e->c +=(data<<8);
e->c <<=8;
}
void CDpcmProcess::Decode_Row(j_compress_ptr cinfo , int i)
{
arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
unsigned char * st;
unsigned char context_a=0;
int num;
int v, m,sign,Ra=0,Rc=0,predic_val=0;
entropy->context=0;
/* Encode the MCU data blocks */
for (num = 0; num < cinfo->image_width; num++) {
/* Sections F.1.4.1 & F.1.4.4.1: Encoding of DC coefficients */
/*printf("%x ",entropy->context);*/
/* Table F.4: Point to statistics bin S0 for DC coefficient coding */
st =entropy->dc_stats + entropy->context*5+entropy->context_b[num];
/*predic_val=last_val+((entropy->val_b[num]-Rc)>>1);*/
/*predic_val=entropy->val_b[num]+RIGHT_SHIFT(last_val-Rc,1);*/
/*predic_val=RIGHT_SHIFT(Ra+entropy->val_b[num],1);*/
/* if (i==0)
printf("num:%d bin:%d *st:%x laval:%d pre:%d\n",num,context_a,*st,last_val,predic_val);*/
/* Figure F.4: Encode_DC_DIFF */
if ((Arith_Decode(cinfo,st)) == 0) {
entropy->context = 0; /* zero diff category */
v=0;
} else {
sign=Arith_Decode(cinfo, st+1);
st +=2; st +=sign;
if ((m = Arith_Decode(cinfo, st)) != 0) {
if ( entropy->context_b[num]>8 )
st=entropy->dc_stats+129;
else
st = entropy->dc_stats + 100; /* Table H.3: X1 = X1_context(Db)*/
/*st=entropy->dc_stats+20;*/
while (Arith_Decode(cinfo, st)) {
m <<=1;
st += 1;
}
}
if (m < (int) (((INT32) 1 << cinfo->arith_dc_L) >> 1))
entropy->context = 0; /* zero diff category */
else if (m > (int) (((INT32) 1 << cinfo->arith_dc_U)>>1 ))
entropy->context = 12 + (sign * 4); /* large diff category */
else
entropy->context = 4 + (sign * 4); /* small diff category */
/* Figure F.24: Decoding the magnitude bit pattern of v */
v = m;
st += 14;
while (m >>= 1)
if (Arith_Decode(cinfo, st)) v |= m;
/* Section F.1.4.4.1.2: Establish dc_context conditioning category */
v += 1; if (sign) v = -v;
}
predic_val=Return_Pixel_Val(cinfo,Ra,entropy->val_b[num],Rc,entropy->val_b[num+1],v);
/*if (i==0)
printf("row[%d]:%x v:%d\n",num,predic_val,v);*/
entropy->context_b[num]=entropy->context;
Rc=entropy->val_b[num];
Ra=entropy->val_b[num]=predic_val;
Emit_Byte(cinfo,predic_val);
}
}
/*
* The core arithmetic decoding routine (common in JPEG and JBIG).
* This needs to go as fast as possible.
* Machine-dependent optimization facilities
* are not utilized in this portable implementation.
* However, this code should be fairly efficient and
* may be a good base for further optimizations anyway.
*
* Return value is 0 or 1 (binary decision).
*
* Note: I've changed the handling of the code base & bit
* buffer register C compared to other implementations
* based on the standards layout & procedures.
* While it also contains both the actual base of the
* coding interval (16 bits) and the next-bits buffer,
* the cut-point between these two parts is floating
* (instead of fixed) with the bit shift counter CT.
* Thus, we also need only one (variable instead of
* fixed size) shift for the LPS/MPS decision, and
* we can get away with any renormalization update
* of C (except for new data insertion, of course).
*
* I've also introduced a new scheme for accessing
* the probability estimation state machine table,
* derived from Markus Kuhn's JBIG implementation.
*/
int CDpcmProcess::Arith_Decode(j_compress_ptr cinfo, unsigned char * st)
{
extern const INT32 jaritab[];
register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
register unsigned char nl, nm;
register INT32 qe, temp;
register int sv, data,cx;
/* Fetch values from our compact representation of Table D.2:
* Qe values and probability estimation state machine
*/
cx=(e->c>>16)&0x0000ffff;
/*printf("%x",cx);*/
sv = *st;
qe = jaritab[sv & 0x7F]; /* => Qe_Value */
nl = qe & 0xFF; qe =(qe>>8)&0x00ffffff ; /* Next_Index_LPS + Switch_MPS */
nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */
/* Decode & estimation procedures per sections D.2.4 & D.2.5 */
temp = e->a - qe;
e->a = temp;
/*temp <<= e->ct;*/
if (cx >= temp) {
cx -= temp;
/* Conditional LPS (less probable symbol) exchange */
if (e->a < qe) {
e->a = qe;
*st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
} else {
e->a = qe;
*st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
sv ^= 0x80; /* Exchange LPS/MPS */
}
} else if (e->a < 0x8000L) {
/* Conditional MPS (more probable symbol) exchange */
if (e->a < qe) {
*st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
sv ^= 0x80; /* Exchange LPS/MPS */
} else {
*st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
}
}
e->c =(e->c & 0xFFFF)|(cx<<16);
/* Renormalization & data input per section D.2.6 */
while (e->a < 0x8000L) {
if (e->ct == 0) {
/* Need to fetch next data byte */
e->ct=8;
if (cinfo->unread_marker){
printf("%x \n",cinfo->unread_marker);
data = 0;
}
else {
data = Get_Byte(cinfo); /* read next input byte */
if (data==0xff) {
if ((data=Get_Byte(cinfo))==0)
data=0xff;
else {
cinfo->unread_marker=data;
data=0;
}
}
e->c +=(data<<8);
}
}
e->a <<=1;
e->c <<=1;
e->ct--;
}
return (sv >> 7);
}
int CDpcmProcess::Get_Byte(j_compress_ptr cinfo)
{
jpeg_source_mgr * src = cinfo->src;
int data;
if (src->bytes_in_buffer == 0){
printf("yes\n");
Fill_Input_Buffer(cinfo);
}
src->bytes_in_buffer--;
data=*src->next_input_byte++;
return (data);
}
void CDpcmProcess::Fill_Input_Buffer(j_compress_ptr cinfo)
{
jpeg_source_mgr *src = cinfo->src;
size_t nbytes;
nbytes = JFREAD(cinfo->inputfile, cinfo->inbuffer[0], 4096);
if (nbytes <= 0) {
/* Insert a fake EOI marker to jump out the decoding programe*/
cinfo->inbuffer[0][0] = (JOCTET) 0xFF;
cinfo->inbuffer[0][1] = (JOCTET) 0xD9;
nbytes = 2;
}
src->next_input_byte = cinfo->inbuffer[0];
src->bytes_in_buffer = nbytes;
}
int CDpcmProcess::Return_Pixel_Val(j_compress_ptr cinfo , int Ra, PIXEL Rb, int Rc, PIXEL Rd, int errval)
{
UNI_TYPE D1,D2,D3;
UNI_TYPE Q1,Q2,Q3,Q;
int x;
int px,nNear;
UNI_TYPE sign=1,errval_coded;
predic_structure_ptr pre=&cinfo->pre;
nNear=pre->nNear;
/*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_coded=errval;
errval_coded *= (2*nNear+1);
px=errval_coded*sign+px;
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*nNear+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 x;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -