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

📄 coder_bpb2.c

📁 用C++语言实现的基于小波分析的源代码,实现了小波分析的诸多算法
💻 C
字号:

// #define BIG_SIGN_CONTEXT , hurts !?
//#define BIG_SHAPE_CNTX

/*****

like CODER_BP_BIN but :

	1. we actually scan on bitplanes

	2. no weighting

512, l6, ts
Lena 4.163
Barb 4.496


*****/

#include <stdio.h>
#include <stdlib.h>
#include <crblib/inc.h>
#include <crblib/arithc.h>
#include <crblib/intmath.h>
#include "coder.h"
#include "subbands.h"

extern int tune_param;

#define	VAL_CONTEXTS		20
#define	VAL_CONTEXT_MAX		(VAL_CONTEXTS -1)
#define SHAPE_BASE			VAL_CONTEXTS
#define SHAPE(x)			(SHAPE_BASE<<(x))

#ifdef BIG_SHAPE_CNTX
#define NUM_SHAPES			4
#else
#define NUM_SHAPES			2
#endif

#define NUM_CONTEXTS		(VAL_CONTEXTS<<NUM_SHAPES)

#ifdef BIG_SIGN_CONTEXT
#define SIGN_CONTEXTS	729	// 3^6
#else
#define SIGN_CONTEXTS	81	// 3^4
#endif // BIG_SIGN_CONTEXT

#define TOTMAX			4000
#define INC				30

#define P0_INIT			8
#define P1_INIT			0

#define bitModel(bit,P0,PT)	do { PT += INC; if (!(bit)) P0 += INC;  if ( PT > TOTMAX ) { PT >>= 1; P0 >>= 1; P0++; PT += 2; } } while(0)
#define bitEnc(bit,ari,P0,PT)	do { arithEncBit(ari,P0,PT,bit);	bitModel(bit,P0,PT); } while(0)
#define bitDec(bit,ari,P0,PT)	do { bit = arithDecBit(ari,P0,PT);	bitModel(bit,P0,PT); } while(0)

#define AddSignContext(context,val,mask) do { context *= 3; if( abs(val)&(mask) ) { if ( (val) > 0 ) context ++; else context += 2; } } while(0)

typedef struct {
	int p0,pt;
} binContext;

void coderBPB2_init(coder *c);
void coderBPB2_free(coder *c);
void coderBPB2_encodeSubbandBP(coder *me,subband_leaf *sb,int);
void coderBPB2_decodeSubbandBP(coder *me,subband_leaf *sb,int);

typedef struct {
	binContext signs[SIGN_CONTEXTS];
	binContext stats_array[NUM_CONTEXTS];
} myInfo;

coder coderBPB2 = {
		"BPB 2",
		coderBPB2_init,
		coderBPB2_free,
		NULL,NULL,NULL,NULL,NULL,NULL,
		coderBPB2_encodeSubbandBP,
		coderBPB2_decodeSubbandBP
	};

void coderBPB2_init(coder *c)
{
myInfo *d;
int i;

	if ( (d = new(myInfo)) == NULL )
		errexit("ozero init failed");

	c->data = d;

	for(i=0;i<NUM_CONTEXTS;i++) {
		d->stats_array[i].p0 = P0_INIT+1; d->stats_array[i].pt = 2+P0_INIT+P1_INIT;
	}

	for(i=0;i<SIGN_CONTEXTS;i++) {
		d->signs[i].p0 = 100;
		d->signs[i].pt = 200;
	}

}

void coderBPB2_free(coder *c)
{
	if ( c->data ) {
		myInfo *d;
		d = c->data;
		free(d);
		c->data = NULL;
	}
}

/**********

lazy way to pass the state from getStats to fixStats
	and also interacts with the codeBand()

these are re-initialized at each codeBand() call, so this is
	quite re-entrant as long as we aren't multi-threaded
	(that is, no more than one call to codeBand() at a time)

*********/

static int VD;
static binContext *stats_array;
static binContext *statptr;
static int donemask,nextmask;
static int *sister_x,*sister_y,sister_trans;

static void getStats(int *dp,int *pp,int x,int y,int width,int height,int fullw)
{
int shapes;
int P,N,W,NE,NW,X1,X2,X3,X4;

	/*** elaborate context-making ***/

	VD	= abs(*dp)&donemask;	// current val already done

	P	= abs(*pp)&nextmask;

	if ( y == 0 ) {
		N = NW = NE = VD;
		if ( x == 0 ) W = VD; else W = abs(dp[-1]) & nextmask;
	} else if ( x == 0 ) {
		W = NW = 0;
		N  = abs(dp[-fullw])	& nextmask;
		NE = abs(dp[1-fullw])	& nextmask;
	} else {
		N = abs(dp[-fullw])		& nextmask;
		W = abs(dp[-1])			& nextmask;
		NW = abs(dp[-1-fullw])	& nextmask;
		if ( x == (width-1) ) NE = VD;
		else	NE = abs(dp[1-fullw]) & nextmask;
	}

	shapes = 0;
	if ( N > VD ) shapes += SHAPE(0);
	if ( W > VD ) shapes += SHAPE(1);
#ifdef BIG_SHAPE_CNTX
	X1 = abs(dp[fullw])	& donemask;		// S
	X2 = abs(dp[1])	& donemask;			// E
	if ( X1 > VD ) shapes += SHAPE(2);
	if ( X2 > VD ) shapes += SHAPE(3);
#endif

	if ( sister_x ) {
		if ( y < (height-1) )	X1 = abs(dp[fullw])	& donemask;	else X1 = VD;	//S
		if ( x < (width-1) )	X2 = abs(dp[1])		& donemask;	else X2 = VD;	//E
		switch(sister_trans) {
			case 0:
				X3 = abs( sister_x[ x + fullw*y ] ) & nextmask;
				X4 = abs( sister_y[ x + fullw*y ] ) & nextmask;
				break;
			case 1:
				X3 = abs( sister_x[ y + fullw*x ] ) & nextmask;
				X4 = abs( sister_y[ x + fullw*y ] ) & nextmask;
				break;
			case 2:
				X3 = abs( sister_x[ x + fullw*y ] ) & nextmask;
				X4 = abs( sister_y[ y + fullw*x ] ) & nextmask;
				break;
			case 3:
				X3 = abs( sister_x[ y + fullw*x ] ) & nextmask;
				X4 = abs( sister_y[ y + fullw*x ] ) & nextmask;
				break;
		}
	} else {

		/** band 0 has more vertical correlation than horizontal **/

		if ( y > 1 ) 			X1 = abs(dp[-fullw-fullw]) & nextmask; else X1 = VD;	//NN
		if ( y < (height-1) )	X2 = abs(dp[fullw]) & donemask; else X2 = VD;	//S
		if ( y > 2 ) 			X3 = abs(pp[-fullw])	& nextmask; else X3 = VD;		//PN
		if ( y < (height-2) )	X4 = abs(pp[fullw]) 	& nextmask; else X4 = VD;		//PS
	}

	statptr = &stats_array[ min(VAL_CONTEXT_MAX, ((VD + P + N + W + NW + NE + X1 + X2 + X3 + X4)>>2)) + shapes ];
}

static void codeBand_init(coder *me,subband_leaf *sb)
{
myInfo *d;

	d = (myInfo *)me->data;

	if ( sb->cntx1 && sb->cntx1shift == 0 && sb->cntx2 && sb->cntx2shift == 0 ) {
		assert(sb->width == sb->cntx1->width);
		assert(sb->width == sb->cntx2->width);
		sister_x = sb->cntx1->band;
		sister_y = sb->cntx2->band;
		sister_trans = 0;
		if ( sb->cntx1->transposed != sb->transposed ) sister_trans |= 1;
		if ( sb->cntx2->transposed != sb->transposed ) sister_trans |= 2;
	} else {
		sister_x = sister_y = NULL;
	}

	stats_array = d->stats_array;
}

void coderBPB2_encodeSubbandBP(coder *me,subband_leaf *sb,int bitmask)
{
int *band,width,height,fullw,*parent;
int x,y,bit;
int *dp,*pp,*dpn;
arithInfo *ari = me->arith;
binContext *signs = ((myInfo *)me->data)->signs;

	band = sb->band; width = sb->width; height = sb->height; fullw = sb->rowpad + width;
	parent = sb->parent->band;

	codeBand_init(me,sb);

	for(x= bitmask,nextmask=0; x<CODE_MAX_VAL ;x+=x) nextmask += x;
	donemask = nextmask - bitmask;

	dp = band;	pp = parent;
	for(y=0;y<height;y++) {
		if ( coder_timetostop(me) ) { coder_didstop(me,y); return; }
		dpn = dp + fullw;
		for(x=0;x<width;x++) {

			getStats(&dp[x],&pp[x>>1],x,y,width,height,fullw);

			bit = (abs(dp[x])&bitmask)?1:0;
			arithEncBit(ari,statptr->p0,statptr->pt,bit);
			bitModel(bit,statptr->p0,statptr->pt);

			if ( bit & !VD ) {
				int context;
				/** code the sign **/
				context = 0;
				if ( x < (width-1) )	AddSignContext(context,dp[x+1],donemask); else context *= 3;
				if ( x > 0 )			AddSignContext(context,dp[x-1],nextmask); else context *= 3;
				if ( y < (height-1) )	AddSignContext(context,dp[x+fullw],donemask); else context *= 3;
				if ( y > 0 )			AddSignContext(context,dp[x-fullw],nextmask); else context *= 3;
#ifdef BIG_SIGN_CONTEXT
				if ( y > 0 && x > 0 )	AddSignContext(context,dp[x-fullw-1],nextmask); else context *= 3;
				if ( y > 0 && x < (width-1) )	AddSignContext(context,dp[x-fullw+1],nextmask); else context *= 3;
#endif
				bitEnc( (isneg(dp[x])?1:0) ,ari,signs[context].p0,signs[context].pt);
			}
		}
		if ( y&1 ) pp += fullw;
		dp += fullw;
	}
}

void coderBPB2_decodeSubbandBP(coder *me,subband_leaf *sb,int bitmask)
{
int *band,width,height,fullw,*parent;
int x,y,bit;
int *dp,*pp,*dpn;
arithInfo *ari = me->arith;
binContext *signs = ((myInfo *)me->data)->signs;

	band = sb->band; width = sb->width; height = sb->height; fullw = sb->rowpad + width;
	parent = sb->parent->band;

	codeBand_init(me,sb);

	for(x= bitmask,nextmask=0; x<CODE_MAX_VAL ;x+=x) nextmask += x;
	donemask = nextmask - bitmask;

	dp = band;	pp = parent;
	for(y=0;y<height;y++) {
		if ( coder_timetostopd(me,y) ) return;
		dpn = dp + fullw;
		for(x=0;x<width;x++) {

			getStats(&dp[x],&pp[x>>1],x,y,width,height,fullw);

			bit = arithDecBit(ari,statptr->p0,statptr->pt);
			bitModel(bit,statptr->p0,statptr->pt);

			if ( bit ) {
				if ( isneg(dp[x]) ) dp[x] -= bitmask;
				else dp[x] += bitmask; 
				if ( ! VD ) {
					int context;
					/** code the sign **/
					context = 0;
					if ( x < (width-1) )	AddSignContext(context,dp[x+1],donemask); else context *= 3;
					if ( x > 0 )			AddSignContext(context,dp[x-1],nextmask); else context *= 3;
					if ( y < (height-1) )	AddSignContext(context,dp[x+fullw],donemask); else context *= 3;
					if ( y > 0 )			AddSignContext(context,dp[x-fullw],nextmask); else context *= 3;
#ifdef BIG_SIGN_CONTEXT
					if ( y > 0 && x > 0 )	AddSignContext(context,dp[x-fullw-1],nextmask); else context *= 3;
					if ( y > 0 && x < (width-1) )	AddSignContext(context,dp[x-fullw+1],nextmask); else context *= 3;
#endif
					bitDec(bit,ari,signs[context].p0,signs[context].pt);
					if ( bit ) dp[x] = - dp[x];
				}
			}
		}
		if ( y&1 ) pp += fullw;
		dp += fullw;
	}
}

⌨️ 快捷键说明

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