LZW compression class definition and conception

Publisher:心有所属Latest update time:2015-05-15 Source: 51heiKeywords:LZW Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//Define the CLZW class

#ifndef _LZW_H_INCLUDED
#define _LZW_H_INCLUDED
#include stdio.h // for getc, getwc
#include string.h // for memset
#include stdlib.h // for malloc, free

#define MAX_LZW_BITS 12 //Maximum LZW code size
#define LZW_TABLE_SIZE (1<#define HSIZE 5003 //Hash table 80% occupancy size

typedef int INT32;
typedef short UINT8;
typedef short code_int; //The value range is -1 to 2*MAX_LZE_BITS
typedef short hash_int; //The value range is -2 to 2*HSIZE

#define MAXCODE(n_bits) (((code_int)1<<(n_bits))-1)

class CLZW
{
public:
CLZW(); //Constructor
~ CLZW(); // Destructor

FILE* outfile;
FILE* infile;

int GetDataBlock(char *buf);//A zero-length block marks the end of the data block sequence
void SkipDataBlocks();//Find the end of the data block
void ReInitLZW();//Initialize LZW state
void InitLZWCode (FILE* file,int in_size);//Initialize the file object
int GetCode();//Extract the next code_size bits from the compressed data
int LzwReadByte();//Read a LZW compressed byte

void CHAR_OUT(int c);//Add a byte to the existing buffer
void flush_packet();//Clear the accumulated data in the buffer
void clear_hash();//Clear the hash table
void clear_block();//Reset compression and send a clear code
void output(code_int code);//Send a n_bits bit code and reassemble an 8-bit byte with cur_accum and cur_bits
void compress_init(FILE* file,int ibits);//Initialize LZW compression
void compress_byte(int c);//Compress an 8-bit byte
void compress_term();//Save the end

protected:
INT32 cur_accum;//Used to save bits that have not been output yet
int cur_bits;//bits in cur_accum
int n_bits;//Current bits/code number
code_int maxcode;//The largest code in n_bits
int code_counter;//Output symbol counter
int init_bits;//Initial n_bit..restore after clearing
code_int ClearCode;//Clear code (remain unchanged)
code_int EOFCode;//EOF code (end code)
bool first_byte;//Judge whether it is the first byte

//Compression
code_int free_code; //Alternate code
code_int *hash_code; //Symbol code Hash table
code_int *hash_prefix; //Prefix symbol Hash table
UINT8 *hash_suffix; //Suffix byte Hash table
code_int waiting_code; //Code not yet output
int bytesinpkt; //Number of bytes in the current buffer char packetbuf[256]; //Accumulation buffer
for compression

//Decompress
char code_buf[256+4]; //Current input data block
int last_byte; //Bytes in code_buf
int last_bit; //Bits in code_buf
bool out_of_blocks; //True value when the end is encountered
code_int *symbol_head; //Prefix symbol table
UINT8 *symbol_tail; //Suffix byte table
UINT8 *symbol_stack; //Stack for symbol expansion
UINT8 *sp; //Stack pointer

};

 

#endif

Keywords:LZW Reference address:LZW compression class definition and conception

Previous article:DEV C++ Nine-grid solution source program
Next article:Data structure->stack (C implementation)

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号