| lzw压缩算法的c语言实现 |
|
|
|
|
| 来源: 作者: 添加日期:2006-7-17 6:53:04 点击次数: |
|
buffer->index = 0; buffer->top = (WORD)ret; return (WORD)ret; } //------------------------------------------------------------------------------ WORD empty_buffer( PLZW_DATA lzw, PBUFFER_DATA buffer)// Output buffer to file { DWORD ret; if(buffer->end_flag) // The flag mark the end of decode { if( buffer->by_left ) { buffer->lp_buffer[ buffer->index++ ] = (BYTE)( buffer->dw_buffer >> 32-buffer->by_left )<<(8-buffer->by_left); } } WriteFile(lzw->h_dest, buffer->lp_buffer,buffer->index,&ret,NULL); buffer->index = 0; buffer->top = ret; return (WORD)ret; } //------------------------------------------------------------------------------ #endif
(3) hash.h 定义了压缩时所用的码表操作函数,为了快速查找使用了hash算法,还有处理hash冲突的函数
#ifndef __HASH_H__ #define __HASH_H__ //------------------------------------------------------------------------------ #include <stdio.h> #include <stdlib.h> #include <windows.h> //------------------------------------------------------------------------------ #define DIV TABLE_LEN #define HASHSTEP 13 // It should bigger than 0. //------------------------------------------------------------------------------ WORD get_hash_index( PLZW_DATA lzw ) { DWORD tmp; WORD result; DWORD prefix; DWORD suffix; prefix = lzw->prefix; suffix = lzw->suffix; tmp = prefix<<8 | suffix; result = tmp % DIV; return result; } //------------------------------------------------------------------------------ WORD re_hash_index( WORD hash ) // If hash conflict occured we must recalculate { // hash index . WORD result; result = hash + HASHSTEP; result = result % DIV; return result; } //------------------------------------------------------------------------------ BOOL in_table( PLZW_DATA lzw ) // To find whether current code is already in table. { BOOL result; WORD hash;
hash = get_hash_index( lzw ); if( lzw->lp_code[ hash ] == 0xFFFF ) { result = FALSE; |
|
| |