【VC开源代码栏目提醒】:网学会员,鉴于大家对VC开源代码十分关注,论文会员在此为大家搜集整理了“LZWCompress.cpp”一文,供大家参考学习!
// LZWCompress.cpp: implementation of the LZWCompress.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "LZWCompress.h"
///变量定义///////////////////////////////////////////////////////////
int *code_value=NULL;
unsigned int *prefix_code=NULL;
unsigned char *append_character=NULL;
int num_bits=INIT_BITS;
unsigned long bytes_in=0,bytes_out=0;
unsigned int max_code=0;
unsigned long checkpoint=CHECK_TIME;
unsigned long incounts=0;
unsigned long outcounts=0;
unsigned long counts=0;
unsigned char *input=NULL,*output=NULL;
int output_bit_count=0;
unsigned long output_bit_buffer=0;
//////////////////////////////////////////////////////////////////////
//进行LZW
压缩 unsigned long LZWCompress(unsigned char *inputs,unsigned long lens,unsigned char *outputs)
{
unsigned int next_code=FIRST_CODE;
unsigned int character;
unsigned int string_code;
unsigned int index;
int i, /* All purpose integer */
ratio_new, /* New compression ratio as a percentage */
ratio_old=100; /* Original ratio at 100% */
///初始化变量
num_bits=INIT_BITS;
bytes_in=0;bytes_out=0;
max_code=0;
checkpoint=CHECK_TIME;
incounts=0;
outcounts=0;
counts=lens;
input=inputs;
output=outputs;
output_bit_count=0;
output_bit_buffer=0;
max_code = MAXVAL(num_bits);
////////////////////////////
for (i=0;i<TABLE_SIZE;i++){ /* Initialize the string table first */
code_value[i]=-1;
}
string_code=*input++; /* Get the first code */
incounts++;
/* This is the main compression loop. Notice when the table is full we try
* to increment the code size. Only when num_bits == MAX_BITS and the code
* value table is full do we start to monitor the compression ratio.
*/
while(incounts<counts ) {
character=*input++;
incounts++;
++bytes_in;
index=find_match(string_code,character);
if (code_value[index] != -1)
string_code=code_value[index];
else {
if (next_code <= max_code ) {
code_value[index]=next_code++;
prefix_code[index]=string_code;
append_character[index]=character;
}
output_code(string_code); /* Send out current code */
string_code=character;
if (next_code > max_code) { /* Is table Full? */
if ( num_bits < MAX_BITS) { /* Any more bits? */
max_code = MAXVAL(++num_bits); /* Increment code size then */
}
else if (bytes_in > checkpoint) { /* At checkpoint? */
if (num_bits == MAX_BITS ) {
ratio_new = bytes_out*100/bytes_in; /* New compression ratio */
if (ratio_new > ratio_old) { /* Has ratio degraded? */
output_code(CLEAR_TABLE); /* YES,flush string table */
num_bits=INIT_BITS;
next_code=FIRST_CODE; /* Reset to FIRST_CODE */
max_code = MAXVAL(num_bits); /* Re-Initialize this stuff */
bytes_in = bytes_out = 0;
ratio_old=100; /* Reset compression ratio */
for (i=0;i<TABLE_SIZE;i++) /* Reset code value array */
code_value[i]=-1;
}
else /* NO, then save new */
ratio_old = ratio_new; /* compression ratio */
}
checkpoint = bytes_in + CHECK_TIME; /* Set new checkpoint */
}
}
}
}
output_code(string_code); /* Output the last code */
if (next_code == max_code) { /* Handles special case for bit */
++num_bits; /* increment on EOF */
}
output_code(TERMINATOR); /* Output the end of buffer code */
output_code(0); /* Flush the output buffer */
output_code(0);
output_code(0);
return outcounts;
}
//匹配表中数据
int find_match(unsigned int hash_prefix, unsigned int hash_character)
{
int index, offset;
index = (hash_character << HASHING_SHIFT ) ^ hash_prefix;
if (index == 0 )
offset=1;
else
offset = TABLE_SIZE - index;
while(1) {
if (code_value[index] == -1 )
return(index);
if (prefix_code[index] == hash_prefix &&
append_character[index] == hash_character)
return(index);
index -= offset;
if (index < 0)
index += TABLE_SIZE;
}
}
//输出编码
void output_code(unsigned int code)
{
output_bit_buffer |= (unsigned long) code << (32 - num_bits -
output_bit_count);
output_bit_count += num_bits;
while (output_bit_count >= 8) {
*output++=(unsigned char)(output_bit_buffer >> 24);
output_bit_buffer <<= 8;
output_bit_count -= 8;
bytes_out++; /* ADDED for compression monitoring */
outcounts++;
}
}
//为各表分配内存空间
bool LZWAllocTables()
{
bool flag=true;
__try{
code_value=(int*)GlobalAlloc(GPTR,TABLE_SIZE*sizeof(un