www.pudn.com > wavecode.rar > coder_bitplane.c


/***** 
 
coder_bitplane : send bitplanes ; this is an "EZ" coder, 
	except that our calling structures ruins the embedding 
 
old style : call be Band order ; compare to coder_bp 
 
*****/ 
 
#include  
#include  
#include  
#include  
#include  
#include  
 
extern int tune_param; 
 
/*** some convenient debug routines: 
** #define SEND_TAG()	if(0); else { arithEncode(ari,43,44,77); } 
** #define GET_TAG()	if(0); else { int got; got = arithGet(ari,77); arithDecode(ari,43,44,77); if ( got != 43 ) errexit("tag failure"); } 
********/ 
 
#define ORDER1_CONTEXTS		10 
#define ORDER1_ALPHABET		16	//4 bits 
 
#define ORDER1_TOTMAX		15000 
#define ORDER1_INC			30 
 
#include "coder.h" 
 
void coderBitPlane_encodeBand(coder *me,int *band,int w,int h,int fullw,int *parent); 
void coderBitPlane_decodeBand(coder *me,int *band,int w,int h,int fullw,int *parent); 
 
typedef struct { 
	scontext ** o1; 
} myInfo; 
 
void coderBitPlane_init(coder *c) 
{ 
myInfo *d; 
int i; 
 
	if ( (d = new(myInfo)) == NULL ) 
		errexit("ozero init failed"); 
 
	c->data = d; 
 
	if ( (d->o1 = newarray(void *,ORDER1_CONTEXTS)) == NULL ) 
		errexit("Order1_Init failed!"); 
 
	for(i=0;io1[i] = scontextCreate(c->arith,ORDER1_ALPHABET,0, 
				ORDER1_TOTMAX,ORDER1_INC,true)) == NULL ) 
			errexit("context creation failed!"); 
	} 
} 
 
void coderBitPlane_free(coder *c) 
{ 
	if ( c->data ) { 
		myInfo *d; 
		d = c->data; 
		if ( d->o1 ) { 
			int i; 
			for(i=0;io1[i] ) scontextFree(d->o1[i]); 
			} 
		} 
		free(d); 
		c->data = NULL; 
	} 
} 
 
coder coderBitPlane = { 
		"BP by Band", 
		coderBitPlane_init, 
		coderBitPlane_free, 
		coderBitPlane_encodeBand, 
		coderBitPlane_decodeBand 
	}; 
 
void coderBitPlane_encodeBand(coder *me,int *band,int width,int height,int fullw,int *parent) 
{ 
int x,y,val,cntx; 
int *dp,*pp,*dpn; 
int top_val,top_bitpn; 
int context,block,par,A,B,C,D; 
int bitmask,donemask,nextmask; 
scontext **o1 = ((myInfo *)me->data)->o1; 
arithInfo *ari = me->arith; 
 
	dp = band;	top_val = 0; 
	for(y=0;y top_val ) top_val = val;  
		} 
		dp += fullw - width; 
	} 
	for(top_bitpn=0;(1<<(top_bitpn+1))<=top_val;top_bitpn++) ; 
 
	cu_putExpanding_ari(top_bitpn,ari,8,8); 
	top_val = 1<=1;bitmask>>=1) { 
		donemask = nextmask; 
		nextmask = donemask + bitmask; 
 
		dp = band;	pp = parent; 
		for(y=0;y>1]); 
				A = abs(dp[x]);		B = abs(dp[x+1]); 
				C = abs(dpn[x]);	D = abs(dpn[x+1]); 
 
				context = ((A & donemask)?1:0) + ((B & donemask)?1:0) + ((C & donemask)?1:0) + ((D & donemask)?1:0); 
				if (par&nextmask) context += 5; 
 
				block = 0; // 4 bits 
				if ( A & bitmask ) block += 1; 
				if ( B & bitmask ) block += 2; 
				if ( C & bitmask ) block += 4; 
				if ( D & bitmask ) block += 8; 
 
				scontextEncode(o1[context],block); 
 
				/** send signs when we see the first 'on' bit **/ 
 
				if ( (A & nextmask) == bitmask ) arithBit(ari, signbit(dp[x]   )); 
				if ( (B & nextmask) == bitmask ) arithBit(ari, signbit(dp[x+1] )); 
				if ( (C & nextmask) == bitmask ) arithBit(ari, signbit(dpn[x]  )); 
				if ( (D & nextmask) == bitmask ) arithBit(ari, signbit(dpn[x+1]));	 
 
			} 
			pp += fullw; 
			dp += fullw + fullw; 
		} 
	} 
} 
 
void coderBitPlane_decodeBand(coder *me,int *band,int width,int height,int fullw,int *parent) 
{ 
int x,y,val,cntx,sign; 
int *dp,*pp,*dpn; 
int top_val,top_bitpn; 
int context,block,A,B,C,D; 
int bitmask,donemask,nextmask; 
scontext **o1 = ((myInfo *)me->data)->o1; 
arithInfo *ari = me->arith; 
 
#define SIGN_MASK (1<<30) 
 
/**** done in mainline now 
	dp = band; 
	for(y=0;y=1;bitmask>>=1) { 
		donemask = nextmask; 
		nextmask = donemask + bitmask; 
		dp = band;	pp = parent; 
		for(y=0;y>1]); 
				if (val&nextmask) context += 5; 
 
				block = scontextDecode(o1[context]); 
 
				if ( block & 1 ) { dp[x]	+= bitmask; if ( ! (A&donemask) ) dp[x] += arithGetBit(ari)?SIGN_MASK:0; } 
				if ( block & 2 ) { dp[x+1]	+= bitmask; if ( ! (B&donemask) ) dp[x+1] += arithGetBit(ari)?SIGN_MASK:0; } 
				if ( block & 4 ) { dpn[x]  	+= bitmask; if ( ! (C&donemask) ) dpn[x] += arithGetBit(ari)?SIGN_MASK:0; } 
				if ( block & 8 ) { dpn[x+1]	+= bitmask; if ( ! (D&donemask) ) dpn[x+1] += arithGetBit(ari)?SIGN_MASK:0; } 
			} 
			pp += fullw; 
			dp += fullw + fullw; 
		} 
	} 
 
	/** signs **/		fix_signs: 
 
	dp = band; 
	for(y=0;y