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

📄 arcode.h

📁 此代码是C编的EWZ的测试程序,大家可以尝试对这个包里lena.pgm进行图像压缩处理.
💻 H
字号:
#ifndef __ARCODE_H_
#define __ARCODE_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>
#include <math.h>
#include "memchk.h"

/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
/* original copyright */
/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
/* The code is taken from the MC-EZBC package files 
 * "ar_code.h" and "ar_code.cpp", C++ version 3.02  -  02/01/96
 *
 * The authors are:
 * Amir Said, William A. Pearlman, S.T. Hsiang and P.Chen at RPI.
 * It is an implementation of the arithmetic-coding algorithm by I.H. Witten,
 * R.M. Neal, and J.G. Cleary, published in ``Arithmetic coding for data
 * compression,'' {\em Commun. ACM}, vol.~30, pp.~520--540, June 1987.
 *
 * Remark:
 * =======
 * - Only binary alphabet is supported.
 * - The file i/o is tied to the coding engine.
 *
 * - 28 Jan 2004 : Fixed the AREncoderEncodeBits bug that occurs when output 32 bits word
 *                 Use unsigned integer and make the mask shift after output bit.
 */
/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
/* Binary histogram */

/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
typedef struct HistoBi_{
  int MaxCount;
  int c0;
  int c1;

} HistoBi;

HistoBi* HistoBiAlloc(int MaxCount, int c0, int c1);
void HistoBiDealloc(HistoBi *h);
void  HistoBiReset(HistoBi *h, int c0, int c1);
void HistoBiTaubScale(HistoBi *h, int MinThre, int TotalThre, int TruncFlag);
void HistoBiScale(HistoBi *h);
void HistoBiUpdate(HistoBi *h, int bit);

/*--------------------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------------------*/
/* M-ary histogram
 * The statistical table is implemented using Binary Index Tree as described in
 * P. M. Fenwick, "A new data structure for cumulative frequency tables,"
 * Softw. Pract. Exper. 24, 3 (Mar. 1994), 327-336.
 */
/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
typedef struct Histo_{
	int nSymbols;	
	int *Tree;	
	int p2half;
   int TotalFreq;
	int MaxCount;
	int ResetCount;
} Histo;

Histo *HistoAlloc(int MaxCount, int nSymbols);
int HistoCopy(Histo *s, Histo *d);
void HistoDealloc(Histo *h);
int HistoGetCumul(Histo *h, int ix);
void HistoPutValue(Histo *h, int val, int ix);
int HistoGetProb(Histo *h, int ix);
void HistoScaleDown(Histo *h);
int HistoGetSymbol(Histo *h, int CumFreq);
void HistoUpdate(Histo *h, int val, int ix);
void HistoReset(Histo *h);

/*--------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
/* Encoder */
/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
/* - About the number of bits output by the encoder is obtained as follow
 *
 *			number of bits output = 8*ByteCounter+(8-BitIndex)+BitsToFollow
 *
 * - Note that this is the actual bits that has to be output for correct decoding.
 */
typedef struct AREncoder_{
	FILE *f;
	int Closed;
	int BitBuffer, BitIndex;
	int ByteCounter, SymbolCounter;
	long Low, High, BitsToFollow;
} AREncoder;

AREncoder* AREncoderAlloc(void);
void AREncoderDealloc(AREncoder *ar);
void AREncoderOpenFile(AREncoder *ar, char* filename);
void AREncoderOpenAppendFile(AREncoder *ar, char* filename);
void AREncoderCloseFile(AREncoder *ar);
void AREncoderFlush(AREncoder *ar);

/* coding function */
void AREncoderEncodeBit(AREncoder *ar, int bit);
void AREncoderEncodeSymbol(AREncoder *ar, int bit, HistoBi *h);
void AREncoderEncodeBits(AREncoder *ar, int bits, int word);

int AREncoderBitsOutput(AREncoder *ar);



/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
/* Decoder */
/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
/*
 * - About the number of bits input by the decoder
 * 
 *			number of bits output = 8*ByteCounter+(8-BitIndex)
 *
 */
typedef struct ARDecoder_{
	FILE *f;
	int Closed;
	int BitBuffer, BitIndex, ExtraBits;
	int ByteCounter, SymbolCounter;
	long Low, High, Value;
} ARDecoder;

ARDecoder* ARDecoderAlloc(void);
void ARDecoderDealloc(ARDecoder *ar);
void ARDecoderOpenFile(ARDecoder *ar, char* filename);
void ARDecoderOpenFileOffset(ARDecoder *ar, char* filename, int OffsetBytes);
void ARDecoderCloseFile(ARDecoder *ar);
/* coding function */
int ARDecoderDecodeBit(ARDecoder *ar);
int ARDecoderDecodeSymbol(ARDecoder *ar, HistoBi *h);
int ARDecoderDecodeBits(ARDecoder *ar, int bits);

int ARDecoderBitsInput(ARDecoder *ar);

/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
/* function for m-ary arithmetic coding */
void AREncoderEncodeMSymbol(AREncoder *ar, int s, Histo *h);
int ARDecoderDecodeMSymbol(ARDecoder *ar, Histo *h);

/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
void ARCoderWarning(char *fmt, ...);
void ARCoderError(char *fmt, ...);

#ifdef __cplusplus
}
#endif

#endif

⌨️ 快捷键说明

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