//定義CLZW類
#ifndef _LZW_H_INCLUDED
#define _LZW_H_INCLUDED
#include stdio.h//用于getc,getwc
#include string.h//用于memset
#include stdlib.h //用于malloc,free
#define MAX_LZW_BITS 12 //最大LZW代碼大小
#define LZW_TABLE_SIZE (1<#define HSIZE 5003 //hash表80%占有率大小
typedef int INT32;
typedef short UINT8;
typedef short code_int; //取值范圍為-1到2*MAX_LZE_BITS
typedef short hash_int; //取值范圍為-2到2*HSIZE
#define MAXCODE(n_bits) (((code_int)1<<(n_bits))-1)
class CLZW
{
public:
CLZW();//構造函數
~CLZW();//析構函數
FILE* outfile;
FILE* infile;
int GetDataBlock(char *buf);//一個零長度的塊標志數據塊序列的結束
void SkipDataBlocks();//找到數據塊結尾
void ReInitLZW();//初始化LZW狀態
void InitLZWCode (FILE* file,int in_size);//初始化文件對象
int GetCode();//從壓縮數據提取以后的code_size個比特
int LzwReadByte();//讀取一個LZW壓縮的字節
void CHAR_OUT(int c);//向現有緩沖區增加一個字節
void flush_packet();//清空緩沖區中的積累數據
void clear_hash();//清空hash表
void clear_block();//重置壓縮并發送一個清除碼
void output(code_int code);//發送一個n_bits比特的代碼并用cur_accum和cur_bits重組一個8位的字節
void compress_init(FILE* file,int ibits);//初始化LZW壓縮
void compress_byte(int c);//壓縮一個8位字節
void compress_term();//保存結尾
protected:
INT32 cur_accum;//用于保存還未輸出的bits
int cur_bits;//cur_accum中的bits
int n_bits;//當前bits/code數目
code_int maxcode;//n_bits數目中的最大的代碼
int code_counter;//輸出符號計數器
int init_bits;//初始n_bit..清除后重新恢復
code_int ClearCode;//清除代碼(保持不變)
code_int EOFCode;//EOF代碼(結束代碼)
bool first_byte;//判斷是否為第一個字節
//壓縮
code_int free_code;//備用代碼
code_int *hash_code;//符號代碼Hash表
code_int *hash_prefix;//前綴符號Hash表
UINT8 *hash_suffix;//后綴字節Hash表
code_int waiting_code;//還沒有輸出的代碼
int bytesinpkt;//在當前緩沖區中的bytes數目
char packetbuf[256];//用于壓縮的積累緩沖區
//解壓
char code_buf[256+4];//當前輸入數據塊
int last_byte;//code_buf中的bytes
int last_bit;//code_buf中的bits
bool out_of_blocks;//遇到結尾為真值
code_int *symbol_head;//前綴符號表
UINT8 *symbol_tail;//后綴字節表
UINT8 *symbol_stack;//用于符號展開的棧
UINT8 *sp;//棧指針
};
#endif