| lzw压缩算法的c语言实现 |
|
|
|
|
| 来源: 作者: 添加日期:2006-7-17 6:53:04 点击次数: |
|
buffer_create( &in ); buffer_create( &out );
while( load_buffer( h_sour, &in ) ) { if( first_run ) {// File length should be considered but here we simply // believe file length bigger than 2 bytes. lzw.prefix = in.lp_buffer[ in.index++ ]; lzw.suffix = in.lp_buffer[ in.index++ ]; first_run = FALSE; } do_encode(&in , &out, &lzw); } output_code(lzw.prefix, &out , &lzw); output_code(lzw.suffix, &out , &lzw); out.end_flag = TRUE; empty_buffer( &lzw,&out);
lzw_destory( &lzw ); buffer_destory( &in ); buffer_destory( &out ); }
//------------------------------------------------------------------------------
#endif
(5) decode.h 解压函数主函数
#ifndef __DECODE_H__ #define __DECODE_H__ //------------------------------------------------------------------------------ #include <stdio.h> #include <stdlib.h> #include <windows.h> //------------------------------------------------------------------------------ VOID out_code( WORD code ,PBUFFER_DATA buffer,PLZW_DATA lzw,PSTACK_DATA stack) { WORD tmp; if( code < 0x100 ) { stack->lp_stack[ stack->index++ ] = code; } else { stack->lp_stack[ stack->index++ ] = lzw->lp_suffix[ code ]; tmp = lzw->lp_prefix[ code ]; while( tmp > 0x100 ) { stack->lp_stack[ stack->index++ ] = lzw->lp_suffix[ tmp ]; tmp = lzw->lp_prefix[ tmp ]; } stack->lp_stack[ stack->index++ ] = (BYTE)tmp;
}
while( stack->index ) { if( buffer->index == BUFFERSIZE ) { empty_buffer(lzw,buffer); } buffer->lp_buffer[ buffer->index++ ] = stack->lp_stack[ --stack->index ] ; } } //------------------------------------------------------------------------------ VOID insert_2_table(PLZW_DATA lzw ) {
lzw->lp_code[ lzw->code ] = lzw->code; lzw->lp_prefix[ lzw->code ] = lzw->prefix; lzw->lp_suffix[ lzw->code ] = lzw->suffix; lzw->code++;
if( lzw->code == ((WORD)1<<lzw->cur_code_len)-1 ) { lzw->cur_code_len++; if( lzw->cur_code_len == CODE_LEN+1 ) |
|
| |