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

📄 coder_bitplane.c

📁 好东西呢
💻 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 <stdio.h>
#include <stdlib.h>
#include <crblib/inc.h>
#include <crblib/arithc.h>
#include <crblib/scontext.h>
#include <crblib/codeutil.h>

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;i<ORDER1_CONTEXTS;i++) {
		if ( (d->o1[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;i<ORDER1_CONTEXTS;i++) {
				if ( d->o1[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<height;y++) {
		for(x=0;x<width;x++) {
			val = *dp++; val = abs(val);
			if ( val > 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<<top_bitpn;

	nextmask= (int)0xFFFFFF - (top_val+top_val-1);

	for(bitmask = top_val;bitmask>=1;bitmask>>=1) {
		donemask = nextmask;
		nextmask = donemask + bitmask;

		dp = band;	pp = parent;
		for(y=0;y<height;y+=2) {
			if ( coder_timetostop(me) ) { dbf(); 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 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<height;y++) {
		for(x=0;x<width;x++) *dp++ = 0;
		dp += fullw - width;
	}
*********/

	top_bitpn = cu_getExpanding_ari(ari,8,8);
	top_val = 1<<top_bitpn;

	nextmask= (int)0xFFFFFF - (top_val+top_val-1);

	for(bitmask = top_val;bitmask>=1;bitmask>>=1) {
		donemask = nextmask;
		nextmask = donemask + bitmask;
		dp = band;	pp = parent;
		for(y=0;y<height;y+=2) {
			if ( coder_timetostopd(me,y) ) { dbf(); goto fix_signs; }
			dpn = dp + fullw;
			for(x=0;x<width;x+=2) {

				A = dp[x];	B = dp[x+1];
				C = dpn[x];	D = dpn[x+1];
				context = ((A & donemask)?1:0) + ((B & donemask)?1:0) + ((C & donemask)?1:0) + ((D & donemask)?1:0);
				val = abs(pp[x>>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<height;y++) {
		for(x=0;x<width;x++) {
			if ( *dp & SIGN_MASK ) {
				*dp -= SIGN_MASK;
				*dp = - *dp;
			}
			dp++;
		}
		dp += fullw - width;
	}
}

⌨️ 快捷键说明

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