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

📄 ezw.h

📁 单独的ezw编解码
💻 H
字号:
/*--------------------------------------------------------------------------------------------*/
/* ezw.h */
#ifndef __EZW_H__
#define __EZW_H__

/* disable unused variabled warning 避免一种变量重定义的warning*/
#pragma warning (disable : 4101)

#include <math.h>
#include <conio.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/stat.h>

/*--------------------------------------------------------------------------------------------*/
/* encoded stream filename extension */
typedef float Real;
typedef float REAL;
typedef unsigned long  ULONG;
typedef unsigned long  uLong;
typedef unsigned char uchar;
typedef int	INT32;

#define MaxReal ((float)3.40282346638528860e+38)
#define MinReal ((float)1.17549435082228750e-38)

/*--------------------------------------------------------------------------------------------*/	
/*--------------------------------------------------------------------------------------------*/
/* Codec mode options */
#define ENCODE						0
#define DECODE						1
/*--------------------------------------------------------------------------------------------*/
#define SCAN_CTX_BIT				0x00000008
#define CSIG_CTX_BIT				0x00000004
#define SIGN_CTX_BIT				0x00000002
#define PARE_CTX_BIT				0x00000001

/************************************************************************/
/*量化时的参数                                                          */     
/************************************************************************/
/* Scalar quantization */
/* At certain bitrate, uniform quantizer is better than deadzone. 
 * Note that the dequantization equation is for deadzone only. This is mathematically 
 * incorrect.
 */
/* EZW default : 1 */
#define _BIAS_DEQUANTIZE_			0
#if _BIAS_DEQUANTIZE_
#define MSB_GAMMA					0.38
#define GAMMA						0.48
#else
#define MSB_GAMMA					0.5
#define GAMMA						0.5
#endif

#define _DEADZONE_   1

#if _DEADZONE_
   #define EZWQuantize(Value, StepSize) SIGN((Value))*((int)(fabs((Value))/(StepSize)));
#else
   #define EZWQuantize(Value, StepSize) SIGN((Value))*(ROUND(fabs((Value))/(StepSize)));
#endif

#define EZWDequantize(index, StepSize, Gamma) \
	((index)==0? 0 : SIGN((index))*(abs((index)) + (Gamma))*(StepSize));

/************************************************************************/
/*DlistElement、Dlist有关树型结构中用到,例如需要进行副扫描的位置       */
/************************************************************************/
/* double-linked list element */
typedef struct DListElementStruct{
	void   *data;
	struct DListElementStruct *prev;
	struct DListElementStruct *next;
} DListElement;
/*--------------------------------------------------------------------------------------------*/
/* double-linked list struct */
typedef struct DListStruct{
	int  size;
	int  (*match)(const void *key1, const void *key2);
	void (*destroy)(void *data);
	void (*print)(int i, const void *data);
	DListElement *head;
	DListElement *tail;
} DList;

/************************************************************************/
/*主要的编解码部分                                                                      */
/************************************************************************/	
typedef struct NodeStruct{
	int i;				/* coeff linear location in the subband */
	int x;				/* coeff horizontal location in the subband */
	int y;				/* coeff vertical location in the subband */
	char n;				/* subband index */
} ListData;

typedef struct CoderStruct {
	
	int Mode;//ENCODE or DECODE
	int FXSize;
	int FYSize;
	int ImageXSize;
	int ImageYSize;
	int NScale;//分辨率级数
	int nSubbands;//子带数
	int CurrentThreshold;
	int MaxThreshold;
	int CurrentBitPlane;
	int MaxBitPlane;

	
	int *Coeff;
	int **SubbandPtr;
	int *SubbandXSize;
	int *SubbandYSize;
	int *SubbandSize;

	int **Magnitude;
	int **MaxMagnitude;
	unsigned int **State;
	
	DListElement *RefinementMark[32];

   /* List based */
 	DList LSP;
	
	char EncodeImageFileName[_MAX_PATH];
	double TargetBitRate;
	int TargetBits;
	
	int nPS;
	int nNG;
	int nIZ;
	int nZS;
	int nRF;

	int CodedSymbols;
	FILE *file;

} Coder;

/*--------------------------------------------------------------------------------------------*/	
/* General interface */
int EZWEncodeInterface(int xsize,int ysize,int level,double rate,int &maxbitplane);
int EZWDecodeInterface(int xsize,int ysize,int level,double rate,int maxbitplane);
int EZWEncode(Coder *coder,int *ezwcomp);
int EZWDecode(Coder *coder);

/*--------------------------------------------------------------------------------------------*/
/* Structures setup */ 
void EZWBuildCodingArrays(Coder *coder,int *ezwcomp);
void EZWDeBuildCodingArrays(Coder *coder);
void EZWDestroyCodingArrays(Coder *coder);
void EZWComputeMaxMagnitudeTree(Coder *coder);

/*--------------------------------------------------------------------------------------------*/
/* I/O */
//void EZWWriteHeader(Coder *coder);
//void EZWReadHeader(Coder *coder);
int EZWWSymbol(Coder *coder,int symbol);
int EZWRSymbol(Coder *coder,int *symbol);

/*--------------------------------------------------------------------------------------------*/
/* Main algorithms */
void EZWMainCodingPass(Coder *coder);
int EZWRefinementPass(Coder *coder);
int EZWSortingPass(Coder *coder);

/*--------------------------------------------------------------------------------------------*/
/* List manupulations */
void EZWDeleteData(void *data);
ListData *EZWCreateData(int i, int x, int y, char n);

/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
#define TEST_MALLOC(p) \
	if (p==NULL){ \
		Error("Memory allocation fail.\n"); \
	}

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//一些常用的数学函数
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
#ifdef __cplusplus
extern "C" {
#endif
/*---------------------------------------------------------------------------*/
/*
 * Mow-Song, Ng 2/9/2002
 * msng@mmu.edu.my
 * http://www.pesona.mmu.edu.my/~msng
 *
 */
/*---------------------------------------------------------------------------*/
// note : (int)(-2.3) gives -2
// floor of -2.3 is -3
// so we do have to minus -0.5 for negative numbers to get their floor
#ifndef MIN
#define MIN(x,y)	(((x)<(y))?(x):(y))
#endif
#ifndef MAX
#define MAX(x,y)	(((x)>(y))?(x):(y))
#endif

#define ROUND(a)	(((a)<0)?(int)((a)-0.5):(int)((a)+0.5))
#define SIGN(x)		(x>0? 1: (x<0? -1: 0))

int sign(Real x);
int Log2(int x);

void Error(char *fmt, ...);
/* code from A. Said and W. Pearlman (SPIHT) */
#ifdef __cplusplus
}
#endif

/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
//////////////////////////////////////////////////////////////////////////
//关于树型结构
//////////////////////////////////////////////////////////////////////////
#ifdef __cplusplus
extern "C" {
#endif
/*--------------------------------------------------------------------------------------------*/	
/*--------------------------------------------------------------------------------------------*/
/* Copyright notice:
* Code comes from the book "Mastering Algorithms with C"
* by Kyle Loudon,  
* published by O'Reilly & Associates
*
/*	
/*--------------------------------------------------------------------------------------------*/
/* 
* Adapted/reformated to suit my own coding style 
* Mow-Song, Ng
* 18-02-2003
/*
/*--------------------------------------------------------------------------------------------*/
	
/*--------------------------------------------------------------------------------------------*/
/* Note
* Never pass free() as the function pointer for destroy. This has to be  
* followed strictly, especialy when the memchk module is used.
/*
/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
/* Public Interface */
void EZWDListInit(DList *dlist, void (*destroy)(void *data),
				int  (*match)(const void *key1, const void *key2),
				void (*print)(int i, const void *data));
void EZWDListDestroy(DList *dlist);
int  EZWDListInsertAsHead(DList *dlist, const void *data);
int  EZWDListAppend(DList *dlist, const void *data);
int  EZWDListInsertNext(DList *dlist, DListElement *element, const void *data);
int  EZWDListInsertPrev(DList *dlist, DListElement *element, const void *data);
int  EZWDListRemove(DList *dlist, DListElement *element, void **data);

int  EZWDListInsertListPrev(DList *mainlist, DList *sublist, DListElement *element);
int  EZWDListInsertListNext(DList *mainlist, DList *sublist, DListElement *element);

/* Macros */
#define DListSize(dlist) ((dlist)->size)
#define DListHead(dlist) ((dlist)->head)
#define DListTail(dlist) ((dlist)->tail)
#define DListIsHead(element) ((element)->prev == NULL ? 1 : 0)
#define DListIsTail(element) ((element)->next == NULL ? 1 : 0)
#define DListData(element) ((element)->data)
#define DListNext(element) ((element)->next)
#define DListPrev(element) ((element)->prev)

#ifdef __cplusplus
}
#endif

/*--------------------------------------------------------------------------------------------*/
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//关于内存分配的函数
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/* Adapted/reformated to suit my own coding style. Some changes to the 
* printing. Also the defintion that enable/disable the memory checker.
* Added a few functions and definitions.
*
* Mow-Song, Ng 2/9/2002
* msng@mmu.edu.my
* http://www.pesona.mmu.edu.my/~msng
*
*
*/
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/* Original code from LiftPack */
/*
*  -*- Mode: ANSI C -*-
*  $Id: memchk.h,v 1.4 1996/11/06 20:52:23 fernande Exp $
*  $Header: /sgi.acct/sweldens/cvs/liftpack/include/memchk.h,v 1.4 
*           1996/11/06 20:52:23 fernande Exp $
*  Author: Sridhar, M. A.
*  Modified: Gabriel Fernandez
*
*  Contains declarations of memory management functions to control
*  memory leaks in a C source code.
*/
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
#ifdef __cplusplus
extern "C" {
#endif
	
//#define __MEMCHK_ENABLE_
	
/* allocate memory and initialize it with zeros */
extern void *
	Calloc (size_t __number, size_t __size, int __line_no, char* __file_name);
	
/* allocate a new memory block for a given pointer */
extern void *
	Realloc (void* __ptr, size_t __size, int __line_no, char* __file_name);
	
/* allocate memory and keep track of important information */
extern void *
	Malloc (size_t __size, int __line_no, char* __file_name);
	
/* free memory and delete entry in Malloc list */
extern void
	Free (void* __p);
	
	
/* print content of Malloc list of allocations */
extern int
	PrintLeaks (void);
	
/* free memory pointed out by the Malloc list */
extern void
	FreeLeaks (void);
	
/* return the maximum memory used */
extern long 
	MaxMemory(void);
	
/* return the current memory allocated */
extern long 
	CurrentMemoryAllocated(void);
	
#ifdef __MEMCHK_ENABLE_
/* definitions for all programs to use Realloc, Calloc, Malloc, and Free */
#define calloc(n, s)  Calloc(n, s, __LINE__, __FILE__)
#define realloc(p, s) Realloc(p, s, __LINE__, __FILE__)
#define malloc(s)     Malloc(s, __LINE__, __FILE__)
#define free(s)       Free(s)
#endif /* __MEMCHK_ENABLE_ */
	
#ifdef __cplusplus
}
#endif

#endif

⌨️ 快捷键说明

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