• 大小: 4KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-09
  • 语言: C/C++
  • 标签: LZW  压缩  mfc  封装  

资源简介

LZW压缩算法的C++源码,已经封装成class

资源截图

代码片段和文件信息

// LZW.cpp: implementation of the CLZW class.
//
//////////////////////////////////////////////////////////////////////
#include “stdafx.h“
#include “LZW.h“

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

void CLZW::InitGlobalVar()
{
num_bits = INIT_BITS;
bytes_in = 0;
bytes_out = 0;
checkpoint = CHECK_TIME;
max_code = MAXVAL(num_bits);
}

CLZW::CLZW()
{
InitGlobalVar();
code_value = NULL;
prefix_code = NULL;
append_character = NULL;
code_value=(int *)malloc(TABLE_SIZE*sizeof(unsigned int));
prefix_code=(unsigned int *)malloc(TABLE_SIZE*sizeof(unsigned int));
append_character=(unsigned char *)malloc(TABLE_SIZE*sizeof(unsigned char));
}

CLZW::~CLZW()
{
if(code_value)
free(code_value);
if(prefix_code)
free(prefix_code);
if(append_character)
free(append_character);
}

/* UNCHANGED from original
 * This is the hashing routine.
 */
unsigned int CLZW::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;
}
}
/* UNCHANGED from original
 * Decode a string from the string table storing it in a buffer.
 * The buffer can then be output in reverse order by the expansion
 * program.
 */
unsigned char *CLZW::decode_string(unsigned char *buffer unsigned int code)
{
int i=0;

while(code > 255 )
{
*buffer++ = append_character[code];
code=prefix_code[code];
if (i++ >= 4000 )
{
// printf(“Error during code expansion\n“);
exit(1);
}
}
*buffer=code;
return(buffer);
}

/* UNCHANGED from original
 * Input a variable length code.
 */
unsigned int CLZW::input_code(FILE *input)
{
unsigned int return_value;

while (input_bit_count <= 24 ) {
input_bit_buffer |= (unsigned long) getc(input) << (24 - input_bit_count);
input_bit_count += 8;
}
return_value=input_bit_buffer >> (32-num_bits);
input_bit_buffer <<= num_bits;
input_bit_count -= num_bits;
return(return_value);
}
/* MODIFIED Output a variable length code.
 */
void CLZW::output_code(FILE *output 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) {
      putc(output_bit_buffer >> 24 output);
      output_bit_buffer <<= 8;
      output_bit_count -= 8;
      bytes_out++

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       9266  2008-11-08 20:02  LZW\LZW.cpp

     文件       2431  2008-11-08 11:50  LZW\LZW.h

     目录          0  2009-01-03 14:01  LZW

----------- ---------  ---------- -----  ----

                11697                    3


评论

共有 条评论