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

📄 coder_bp.c

📁 好东西呢
💻 C
字号:
/*****

coder_BP : send BPs ; this is an "EZ" coder,

Checa lossless :
	band-then-bp : 3.43
	bp-then-band : 3.39

Lena -qu10 -l5 (37.61 psnr)
	band-then-bp : 0.607
	bp-then-band : 0.598

*****/

#include <stdio.h>
#include <stdlib.h>
#include <crblib/inc.h>
#include <crblib/arithc.h>
#include <crblib/scontext.h>

extern int tune_param;

#define ORDER1_CONTEXTS		10
#define ORDER1_ALPHABET		16	//4 bits

#define ORDER1_TOTMAX		15000
#define ORDER1_INC			30

#include "coder.h"

void coderBP_encodeBandBP(coder *me,int *band,int w,int h,int fullw,int *parent,int);
void coderBP_decodeBandBP(coder *me,int *band,int w,int h,int fullw,int *parent,int);

typedef struct {
	scontext ** o1;
} myInfo;

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

	d = new(myInfo);
	assert(d);

	c->data = d;

	d->o1 = newarray(void *,ORDER1_CONTEXTS);
	assert(d->o1);

	for(i=0;i<ORDER1_CONTEXTS;i++) {
		d->o1[i] = scontextCreate(c->arith,ORDER1_ALPHABET,0,
				ORDER1_TOTMAX,ORDER1_INC,true);
		assert( d->o1[i] );
	}
}

void coderBP_free(coder *c)
{
	if ( c->data ) {
		myInfo *d;
		d = c->data;
		if ( d->o1 ) {
			int i;
			for(i=0;i<ORDER1_CONTEXTS;i++) {
				if ( d->o1[i] ) scontextFree(d->o1[i]);
			}
		}
		free(d);
		c->data = NULL;
	}
}

coder coderBP = {
		"BitPlane",
		coderBP_init,
		coderBP_free,
		NULL,NULL,
		coderBP_encodeBandBP,
		coderBP_decodeBandBP
	};

void coderBP_encodeBandBP(coder *me,int *band,int width,int height,int fullw,int *parent,int bitmask)
{
int x,y;
int *dp,*pp,*dpn;
int context,block,par,A,B,C,D;
scontext **o1 = ((myInfo *)me->data)->o1;
arithInfo *ari = me->arith;
int donemask,nextmask;
	
	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+=2) {
		if ( coder_timetostop(me) ) { coder_didstop(me,y); return; }
		dpn = dp + fullw;
		for(x=0;x<width;x+=2) {	/** x & y are the parent's location *2 **/
			par = abs(pp[x>>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 coderBP_decodeBandBP(coder *me,int *band,int width,int height,int fullw,int *parent,int bitmask)
{
int x,y,par;
int *dp,*pp,*dpn;
int context,block,A,B,C,D;
scontext **o1 = ((myInfo *)me->data)->o1;
arithInfo *ari = me->arith;
int donemask,nextmask;

	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+=2) {
		if ( coder_timetostopd(me,y) ) return;
		dpn = dp + fullw;
		for(x=0;x<width;x+=2) {
			par = abs(pp[x>>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 = scontextDecode(o1[context]);

			if ( block & 1 ) { 
				if ( !A ) dp[x] = arithGetBit(ari)? -bitmask: bitmask;
				else if ( isneg(dp[x]) ) dp[x] -= bitmask; else dp[x] += bitmask;
			}
			if ( block & 2 ) { 
				if ( !B ) dp[x+1] = arithGetBit(ari)? -bitmask: bitmask;
				else if ( isneg(dp[x+1]) ) dp[x+1] -= bitmask; else dp[x+1] += bitmask;
			}
			if ( block & 4 ) { 
				if ( !C ) dpn[x] = arithGetBit(ari)? -bitmask: bitmask;
				else if ( isneg(dpn[x]) ) dpn[x] -= bitmask; else dpn[x] += bitmask;
			}
			if ( block & 8 ) { 
				if ( !D ) dpn[x+1] = arithGetBit(ari)? -bitmask: bitmask;
				else if ( isneg(dpn[x+1]) ) dpn[x+1] -= bitmask; else dpn[x+1] += bitmask;
			}
		}
		pp += fullw;
		dp += fullw + fullw;
	}

}

⌨️ 快捷键说明

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