coder_o0.c

来自「小波变换算法」· C语言 代码 · 共 112 行

C
112
字号
#include <stdio.h>
#include <stdlib.h>
#include <crblib/inc.h>
#include <crblib/arithc.h>
#include <crblib/o0coder.h>

#define TOT_MAX		4000
#define ALPHABET	128
#define ESCAPE		(ALPHABET-1)

#include "coder.h"

void coderOzero_encodeBand(coder *me,int *band,int w,int h,int fullw,int *parent);
void coderOzero_decodeBand(coder *me,int *band,int w,int h,int fullw,int *parent);

#include <crblib/context.h>

struct O0coderInfoNew {
	struct FAI * arith;
	long numChars;
	context * order0;
  };


void coderOzero_init(coder *me)
{
	if ( (me->data = ozeroCreateMax(me->arith,ALPHABET,TOT_MAX)) == NULL )
		errexit("ozero init failed");
}
void coderOzero_free(coder *me)
{
ozero *oz;
	oz = (ozero *)me->data;
	if ( oz ) {
		int p,c;
		for(c=0;c<ALPHABET;c++) {
			p = contextGetProb(((struct O0coderInfoNew *)oz)->order0,c+1) - 1;
			if ( p > 0 ) printf("%3d : %d\n",c,p);
		}
		printf("tot : %d\n",contextGetCumProb(((struct O0coderInfoNew *)oz)->order0,ALPHABET)-ALPHABET);
		ozeroFree(oz);
	}
}

coder coderOzero = {
		"order 0",
		coderOzero_init,
		coderOzero_free,
		coderOzero_encodeBand,
		coderOzero_decodeBand
	};

void coderOzero_encodeBand(coder *me,int *band,int width,int height,int fullw,int *parent)
{
int x,y,v,sign;
int *dp;
ozero *oz = me->data;
arithInfo *ari = me->arith;

	dp = band;
	for(y=0;y<height;y++) {
		if ( coder_timetostop(me) ) { coder_didstop(me,y); return; }
		for(x=width;x--;) {
			v = *dp++;
			if ( v == 0 ) {
				ozeroEncode(oz,0);
				continue;
			} else if ( isneg(v) ) { sign = 1; v = -v; 
			} else sign = 0;

			while( v >= ESCAPE ) {
				ozeroEncode(oz,ESCAPE);
				v -= ESCAPE;
			}
			ozeroEncode(oz,v);
			arithBit(ari,sign);
		}
		dp += (fullw - width);
	}
}

void coderOzero_decodeBand(coder *me,int *band,int width,int height,int fullw,int *parent)
{
int x,y,v,got,sign;
int *dp;
arithInfo *ari = me->arith;
ozero *oz = me->data;

	dp = band;
	for(y=0;y<height;y++) {
		if ( coder_timetostopd(me,y) ) { dbf(); return; }
		for(x=width;x--;) {
			got = ozeroDecode(oz);
			if ( got == 0 ) {
				*dp++ = 0;
				continue;
			}
			v=0;
			while ( got == ESCAPE ) {
				v+=ESCAPE;
				got = ozeroDecode(oz);
			}
			v+=got;

			if ( arithGetBit(ari) ) v = -v;
			*dp++ = v;
		}
		dp += (fullw - width);
	}
}

⌨️ 快捷键说明

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