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

📄 advancejpeg-lscode.h

📁 改进的JPEG-LS算法
💻 H
字号:

//编码器操作方式定义
#define MaxMTestErrMeLimen(a)			(((a)/3))
//#define MinMTestErrMeLimen(a)			(((a)/8))
#define MinMTestErrMeLimen(a)			(((a)/6))
#define MTestErrMeLimen(a)				(((a)/4))
#define MAdjustErrMe(a)					((a)+(a))
#define MAdjustErrMe2(a)				(4*(a))
#define MAX_RUN_TEST_THRESHOLD_NUMBER	3
#define MIN_MINCONTEXT(a)					(a+(a)/6)
#define MIN_MDoubleMaxErrAdjust(a)			(a+1)
#define MIN_MDoubleMaxErrAdjust2(a)			(a+(a)/2+1)
#define MIN_MDoubleMaxErr(a)				(a+a)

#define MID_MINCONTEXT(a)					(a+((a)/3))
#define MID_MDoubleMaxErrAdjust(a)			(a+((a)/6)+1)
#define MID_MDoubleMaxErrAdjust2(a)			(a+(a)/3+1)
#define MID_MDoubleMaxErr(a)				(a+a+((a)/3))

#define MAX_MINCONTEXT(a)					(a+((a)*2/3))
#define MAX_MDoubleMaxErrAdjust(a)			(a+((a)/3)+1)
#define MAX_MDoubleMaxErrAdjust2(a)			(a+(a))
#define MAX_MDoubleMaxErr(a)				(a+a+a)

#define MFilterGradeLimen(a)			(((a)*3)/3)
//#define MFilterGradeLimen(a)			(((a)*2)/3)
#define DEFAULT_ADJUST_WIN_LENGTH		6

#define RUN_APPEND_FILTER_CONTROL_MIN_RUNL	3//附加游程方式下的滤波控制最小检测游程长度

#define BASIC_T1	3				// basic default threshold values
#define BASIC_T2	7
#define BASIC_T3	21

#define DEFAULT_RESET 64
//用于多波段编码
#define UNIFORM_FLOAT_TO_INT_MUL	32767
#define STATISTIC_LINE_NUMBER	3
#define PIX_ADD					8192
#define MAX_PREDICT_ERR			PIX_ADD
#define MAX_A_MUL				131068//4*32767
#define MIN_A_MUL				6553//0.2*32767
#define MAX_BAND_CONTEXT_NUMBER	64
#define BAND_CONTEXT_RANGE		64
#define MIN_STATISTIC_NUMBER	64
#define UPDATE_RELATIVE_LINE_NUMBER		2
#define ALLOW_PREDICTION_MIN_RELATIVE	26541//0.90*0.9*32767
#define NOT_PREDICTION_MAX_RELATIVE		22033//0.82*0.82*32767
//该类仅完成一次连续的图像行编码过程,在调用过程中,只要不复位,图像的压缩就是连续的。
//由于部分参数的优化更新与行数有关,类内有当前行指示。
/*

      编码调用办法


	一、初始化
		  1、定义编码器对象(这是一个数据结构,提供图像行压缩的基本控制数据和图像的上下文缓冲)
		  2、复位并初始化编码器。(主要的图像的上下文及压缩控制数据,为压缩新的图像作准备)
	二、压缩过程调用
		  1、加载图像数据。(要求图像行数据是存在于某缓冲中,如果是可波段预测,则求行预测误差数据。)
		  2、压缩一行图像数据,压缩比特流存放于指定地址缓冲区内。(有比特流输出)
		  3、压缩控制参数的优化更新。(内部操作)
	三、压缩过程的结束
		  1、释放编码器申请的存贮空间。

		译码调用办法


	一、初始化
		  1、定义译码器对象(这是一个数据结构,提供图像行解压缩的基本控制数据和图像的上下文缓冲)
		  2、复位并初始化译码器。(主要的图像的上下文及压缩控制数据,为解压缩新的图像作准备)
	二、解压缩过程调用
		  1、加载图像压缩数据。(要求图像行数据是存在于某缓冲中,如果是可波段预测,则求行预测误差数据。)
		  2、解压缩一行图像数据,输出一个图像行。
		  3、压缩控制参数的优化更新。(内部操作)
	三、压缩过程的结束
		  1、释放译码器申请的存贮空间。

*/

struct AdvanceJPEGLSMaxErrList
{
	short int PixSeat,Sign;
};
class	CDoubleErrorNumberFastCoding;
class	CDoubleErrorRunFastCoding;
class CAdvanceJepgLSOneLineCode
{
private:
public:
	//数据单元
	int *lpImage;//图象数据指针,波段预测时应为预测误差图像
	int OneSampleBits;
	int AndCode;
	int Pixel_ADD;
	LPBYTE lpCodeStream;//编码数据流
	int CodeBitcp;
	int LineN;//行计数

	int A[367];//accumulated prediction error magnitude
	int B[365];//computing the bias
//	int CC[10];
	int C[365];//storing prediction correction values
	int N[367];//frequency of occurrence of each context
	char qbpp;//	number of bits needed to represent a mapped error value
	char bpp;//number of bits needed to represent MAXVAL, with a minimum of 2
	unsigned short int MAXVAL;//maximum possible image sample value over all components of a scan
	short int RANGE;//	range of prediction error representation
	short int RESET;//	threshold value at which A, B, and N are halved

	BOOL EOLine;//end of line indicator, used in run mode
	unsigned short int X;//number of samples per line in the component with largest horizontal dimension

	short int MAX_C;//	maximum allowed value of C[0..364], equal to 127
	short int MIN_C;//	minimum allowed value of C[0..364], equal to -128
	short int LIMIT;//	the value of glimit for a sample encoded in regular mode

	unsigned short int qNEAR;//	difference bound for near-lossless coding

	int x;
	int Ix;//the value of the current sample in the input image
	int Ra,Rb,Rc,Rd,Raa;//reconstructed values of samples in the causal template
	int *lpPrecedingLine,*lpCurrentLine,*lpThirdLine,*lpBuf1,*lpBuf2,*lpBuf3;
	int *lpReconstructPrecedingLine,*lpReconstructCurrentLine,*lpBuf4,*lpBuf5;
	int Rx;//reconstructed value of the current sample
	int Px;//predicted value for the sample x

	int T1, T2, T3;//	thresholds for local gradients

	int SIGN;//temporary variable used to hold the sign of a context
	short int Q;//context determined from Q1, Q2, Q3
	short int Q1,Q2,Q3;//region numbers of quantized local gradients

	int Errval;//prediction error (quantized or unquantized, before and after modulo reduction)
	int MErrval;//Errval mapped  to non-negative integers in regular mode
	int K;//Golomb coding variable for regular mode
	int glimit;//	number of bits to which the length of a Golomb code word is limited

	short int J[32];//	32 variables indicating order of run-length codes
	int RUNval;//repetitive reconstructed sample value in a run
	int RUNcnt;//repetitive sample count for run mode
	int RUNindex;//index for run mode order

	int RItype;//index for run interruption coding
	int TEMP;//auxiliary variable used in the calculation of the Golomb variable in run interruption coding
	int map;//auxiliary variable for error mapping at run interruption
	int EMErrval;//Errval mapped to non-negative integers in run interruption mode
	short int Nn[2];// (365,366) 2 counters for negative prediction error for run interruption

	int DoubleMaxError;//游程方式的检测门限
	int RunContext;//游程方式下的上下文控制门限
	int MaxErrAdjust1;//第一级大误差调节量
	int MaxErrAdjust2;//第二级大误差调节量
	int RunTestThresholdStep;//游程方式下门限参数组指示。
	int SetDoubleMaxError[MAX_RUN_TEST_THRESHOLD_NUMBER];//游程方式的检测门限,预定值。
	int SetRunContext[MAX_RUN_TEST_THRESHOLD_NUMBER];//游程方式下的上下文控制门限,预定值。
	int SetMaxErrAdjust1[MAX_RUN_TEST_THRESHOLD_NUMBER];//第一级大误差调节量,预定值。
	int SetMaxErrAdjust2[MAX_RUN_TEST_THRESHOLD_NUMBER];//第二级大误差调节量,预定值。

	AdvanceJPEGLSMaxErrList *lpDoubleErrList;//存放每行大误差位置和状态
	int DoubleErrN,DErrN,DDErrN;//大误差数,一级大误差数,二级大误差数。

	int AdjustErrorMe,TestErrorMeLimen;//平坦区单元均值调节值及调节检测门限。
	int AdjustWinLength,BakAdjustWinLength;//调节窗口长度。
	int AdjustNumber,AllTestNumber;//单元调节数及总的测试单元数,用于求单元调节率并用以控制单元调节检测门限。
	int RunPredication;//游程方式下的预测值

	CDoubleErrorNumberFastCoding *lpDErrNumberFastCoding;//一级大误差数编码器
	CDoubleErrorRunFastCoding *lpDErrRunFastCoding;//一级大误差游程编码器
	CDoubleErrorNumberFastCoding *lpDDErrNumberFastCoding;//二级大误差数编码器
	CDoubleErrorRunFastCoding *lpDDErrRunFastCoding;//二级大误差游程编码器
	CDoubleErrorNumberFastCoding *lpTwoAdjustNumberFastCoding;//一级单元调节数编码器
	CDoubleErrorRunFastCoding *lpTwoAdjustRunFastCoding;//一级单元调节游程编码器
	CDoubleErrorNumberFastCoding *lpAdjustNumberFastCoding;//二级单元调节数编码器
	CDoubleErrorRunFastCoding *lpAdjustRunFastCoding;//二级单元调节游程编码器

	//用于多波段编码
	//AllowBandPrediction用在单象素编码方式的切换,预波段预测可随时启用或关闭。

	CAdvanceJepgLSOneLineCode *lpPredictionBandCode;//用于波段预测,可从该数据结构中获得预测波段的重建图像数据
	int A_Mul,B_Add,p;//用于波段预测的参数(线性预测)
	//相关系数控制是否预测,当相关系数小于某门限时:取消预测,此时,前行及前前行换成重建象素值(都加上PIX_ADD)。
	//波段预测系数及相关系数还要更新,前行及前前行的预测系数要另行保存,以备预测开启时用以求预测误差重建值。
	//开启预测时:前行及前前行换成预测误差重建值,设置开启标志
	BOOL OpenOffBandPrediction,AllowBandPrediction;//初值为TRUE
	int Last_A_Mul,Last_B_Add;//前行波段预测的参数(线性预测),初值与A_Mul,B_Add同
	__int64 D_Cun,D_Pre,E_Cun,E_Pre,R_CunPre;
	int PredictErr_D;//预测误差的方差
	int StatisticLinN;
public:
	void UpdateBandPrediction();//更新波段预测参数,更新变量A_Mul,A_Div,B_Add。
	void InitBandPrediction();
	void OpenBandPrediction();
	void CloseBandPrediction();
	void PrepareCode(int LineL,CAdvanceJepgLSOneLineCode *lpPredictionBand=NULL);
	//用于多波段编码

	//函数
	CAdvanceJepgLSOneLineCode();
	~CAdvanceJepgLSOneLineCode();
	BOOL CanEnterRUN();
	BOOL GetNextSample();
	void AppendToBitStream(int a,int b);//Function: appends the non-negative number a in binary form 
		//to the encoded bit stream, using b bits.  Most significant bits are appended first.  
		//The process guarantees that b bits are sufficient to represent a exactly.
	int Quantize(int a,int pNEAR);//Function: returns the quantized value of a following the procedure
		//applied to Errval in Figure A.8 ("if" statement).  This function is used to quantize 
		//the prediction error in near-lossless coding.
	int ModRange(int a,int pRANGE);//Function: returns the value of a modulo RANGE as described in A.4.5.
	int ComputeRx(int pNEAR);//Function: returns the reconstructed value Rx of the current sample
		//as described in Figure A.8 (after the "if" statement).  This function reconstructs 
		//the value of Rx from the quantized prediction error.
	void InitialisationsCode(int OnePixelBit,int AllowMaxError);//初始化编码器,一般在图象数据加载之后
	void EndCode();//主要是清除申请的缓冲区,要在编码数据处理完后方可结束
	int RunPredicate();//游程方式下的预测
	void ResetContext(int Seat);
	int DoEncodeLine(LPBYTE lpImageBuf,int OneSampleBytes,LPBYTE lpCompressBuf,int CompressBitcp,double *lpErrorMse=NULL);//执行一行图像编码操作
	void AppendRunEncode(int Bgx,int Runl);
	void EncodeDoubleError();//编码一级大误差
	void EncodeDDoubleError();//编码二级大误差
	void EncodeTwoAdjust(int *lpAdjustSequence,int AdjustLength,int TwoAdjustLength);//编码二级调节序列
	void EncodeAdjust(int *lpAdjustSequence,int AllAdjustLength,int AdjustLength);//编码一级调节序列
	//与译码有关的函数
	int DoDecodeLine(LPBYTE lpImageBuf,int OneSampleBytes,LPBYTE lpCompressBuf,int CompressBitcp);//执行一行图像译码操作
	BOOL SetNextSample();
	int GetFromBitStream(int b);
	int ReturnZeroBitNumber();
	void AppendRunDecode(int Bgx,int Runl);
	void DecodeDoubleError();
	void DecodeDDoubleError();
	int DecodeTwoAdjust(int *lpAdjustSequence,int AdjustLength);
	int DecodeAdjust(int *lpAdjustSequence,int AllAdjustLength);
	CString GetString();
};
//不含预测的单幅图像压缩实例
int OneImageAdvanceJPEGLSEncode(LPBYTE lpOneImageData,LPBYTE lpCompressBuf,
								int Width,
								int Height,
								int AllowMaxError,
								int OnePixelBits,
								int OnePixelBytes);
int OneImageAdvanceJPEGLSDecode(LPBYTE lpOneImageData,LPBYTE lpCompressBuf,
								int Width,
								int Height,
								int AllowMaxError,
								int OnePixelBits,
								int OnePixelBytes);

⌨️ 快捷键说明

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