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

📄 coder_sm_o0.c

📁 小波变换算法
💻 C
字号:
/*****

SigMap : send "significance map" then residual values

order zero version

--------

slightly better than plain order 0

******/

#include <stdio.h>
#include <stdlib.h>
#include <crblib/inc.h>
#include <crblib/arithc.h>
#include <crblib/scontext.h>
#include <crblib/o0coder.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 SMo0_ALPHABET		5	/** 0,1,-1,pos,neg **/

#define SMo0_TOTMAX		5000
#define SMo0_INC			30

#define ORDER0_TOTMAX		500
#define ORDER0_ALPHABET		25
#define ORDER0_ESCAPE		(ORDER0_ALPHABET-1)

#include "coder.h"

void coderSMo0_encodeBand(coder *me,int *band,int w,int h,int fullw,int *parent);
void coderSMo0_decodeBand(coder *me,int *band,int w,int h,int fullw,int *parent);

typedef struct {
	ozero * o0;
	scontext * o1;
} myInfo;

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

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

	c->data = d;

	if ( (d->o0 = ozeroCreateMax(c->arith,ORDER0_ALPHABET,ORDER0_TOTMAX)) == NULL )
		errexit("ozero init failed");

	if ( (d->o1 = scontextCreate(c->arith,SMo0_ALPHABET,0,
			SMo0_TOTMAX,SMo0_INC,true)) == NULL )
		errexit("context creation failed!");

}

void coderSMo0_free(coder *c)
{
	if ( c->data ) {
		myInfo *d;
		d = c->data;
		if ( d->o0 ) ozeroFree(d->o0);
		if ( d->o1 )  scontextFree(d->o1);
		free(d);
		c->data = NULL;
	}
}

coder coderSMo0 = {
		"SigMap o0",
		coderSMo0_init,
		coderSMo0_free,
		coderSMo0_encodeBand,
		coderSMo0_decodeBand
	};

void coderSMo0_encodeBand(coder *me,int *band,int width,int height,int fullw,int *parent)
{
int x,y,val;
int *dp;
ozero *o0 = ((myInfo *)me->data)->o0;
scontext *o1 = ((myInfo *)me->data)->o1;
arithInfo *ari = me->arith;

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

			val = dp[x];	
			switch(val) {
				case 0:		scontextEncode(o1,0); break;
				case 1:		scontextEncode(o1,1); break;
				case -1:	scontextEncode(o1,2); break;
				default:
					if ( isneg(val) ){	scontextEncode(o1,3); val = -val; }
					else 			scontextEncode(o1,4);
					val -= 2;
					if ( val < ORDER0_ESCAPE ) ozeroEncode(o0,val);
					else {
						ozeroEncode(o0,ORDER0_ESCAPE);	
						encode_m1(ari,val - ORDER0_ESCAPE);
					}
					break;
			}
		}
		dp += fullw;
	}
}

void coderSMo0_decodeBand(coder *me,int *band,int width,int height,int fullw,int *parent)
{
int x,y,val,sign;
int *dp;
ozero *o0 = ((myInfo *)me->data)->o0;
scontext *o1 = ((myInfo *)me->data)->o1;
arithInfo *ari = me->arith;

	dp = band;
	for(y=0;y<height;y++) {
		if ( coder_timetostopd(me,y) ) return;
		for(x=0;x<width;x++) {
			switch( scontextDecode(o1) ) {
				case 0:		dp[x] = 0; break;
				case 1:		dp[x] = 1; break;
				case 2:		dp[x] = -1; break;
				case 3:		sign = -1; goto decode_val;
				case 4:		sign = 1;
				decode_val:
					val = ozeroDecode(o0);
					if( val == ORDER0_ESCAPE ) val += decode_m1(ari);
					dp[x] = sign * (val + 2);
					break;
				default: 	
					errexit("decoded past alphabet!");
			}
		}
		dp += fullw;
	}
}

⌨️ 快捷键说明

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