设为首页 加入收藏

TOP

用C语言LZW编解码算法(二)
2014-04-06 17:40:45 来源: 作者: 【 】 浏览:369
Tags:语言 LZW 解码 算法

 

  //------------------------------------------------------------------------------

  #endif

  hash.h 定义了压缩时所用的码表操作函数,为了快速查找使用了hash算法,还有处理hash冲突的函数

  #ifndef __HASH_H__

  #define __HASH_H__

  //------------------------------------------------------------------------------

  #include

  #include

  #include

  //------------------------------------------------------------------------------

  #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;

  }

  else

  {

  if( lzw->lp_prefix[ hash ] == lzw->prefix &&

  lzw->lp_suffix[ hash ] == lzw->suffix )

  {

  result = TRUE;

  }

  else

  {

  result = FALSE;

  while( lzw->lp_code[ hash ] != 0xFFFF )

  {

  if( lzw->lp_prefix[ hash ] == lzw->prefix &&

  lzw->lp_suffix[ hash ] == lzw->suffix )

  {

  result = TRUE;

  break;

  }

  hash = re_hash_index( hash );

  }

  }

  }

  return result;

  }

  //------------------------------------------------------------------------------

  WORD get_code( PLZW_DATA lzw )

  {

  WORD hash;

  WORD code;

  hash = get_hash_index( lzw );

  if( lzw->lp_prefix[ hash ] == lzw->prefix &&

  lzw->lp_suffix[ hash ] == lzw->suffix )

  {

  code = lzw->lp_code[ hash ];

  }

  else

  {

  while( lzw->lp_prefix[ hash ] != lzw->prefix ||

  lzw->lp_suffix[ hash ] != lzw->suffix )

  {

  hash = re_hash_index( hash );

  }

  code = lzw->lp_code[ hash ];

  }

  return code;

  }

  //------------------------------------------------------------------------------

  VOID insert_table( PLZW_DATA lzw )

  {

  WORD hash;

  hash = get_hash_index( lzw );

  if( lzw->lp_code[ hash ] == 0xFFFF )

  {

  lzw->lp_prefix[ hash ] = lzw->prefix;

  lzw->lp_suffix[ hash ] = lzw->suffix;

  lzw->lp_code[ hash ] = lzw->code;

  }

  else

  {

  while( lzw->lp_code[ hash ] != 0xFFFF )

  {

  hash = re_hash_index( hash );

  }

  lzw->lp_prefix[ hash ] = lzw->prefix;

  lzw->lp_suffix[ hash ] = lzw->suffix;

  lzw->lp_code[ hash ] = lzw->code;

  }

  }

  //------------------------------------------------------------------------------

  #endif

  encode.h 压缩程序主函数

  #ifndef __ENCODE_H__

  #define __ENCODE_H__

  //------------------------------------------------------------------------------

  #include

  #include

  #include

  //------------------------------------------------------------------------------

  VOID output_code( DWORD code ,PBUFFER_DATA out, PLZW_DATA lzw)

  {

  out->dw_buffer |= code << ( 32 - out->by_left - lzw->cur_code_len );

  out->by_left += lzw->cur_code_len;

  while( out->by_left >= 8 )

  {

  if( out->index == BUFFERSIZE )

  {

  empty_buffer( lzw,out);

  }

  out->lp_buffer[ out->index++ ] = (BYTE)( out->dw_buffer >> 24 );

  out->dw_buffer <<= 8;

  out->by_left -= 8;

  }

  }

  //------------------------------------------------------------------------------

  VOID do_encode( PBUFFER_DATA in, PBUFFER_DATA out, PLZW_DATA lzw)

  {

  WORD prefix;

  while( in->index != in->top )

  {

  if( !in_table(lzw) )

  {

  // current code not in code table

  // then add it to table and output prefix

  insert_table(lzw);

  prefix = lzw->suffix;

  output_code( lzw->prefix ,out ,lzw );

  lzw->code++;

  if( lzw->code == (WORD)1<< lzw->cur_code_len )

  {

  // code reached current code top(1<

  // then current code length add one

  lzw->cur_code_len++;

  if( lzw->cur_code_len == CODE_LEN + 1 )

  {

  re_init_lzw( lzw );

  }

  }

  }

  else

  {

  // current code already in code table

  // then output nothing

  prefix = get_code(lzw);

  }

  lzw->prefix = prefix;

  lzw->suffix = in->lp_buffer[ in->index++ ];

  }

  }

  //------------------------------------------------------------------------------

  VOID encode(HANDLE h_sour,HANDLE h_dest)

  {

  LZW_DATA lzw;

  BUFFER_DATA in ;

  BUFFER_DATA out;

  BOOL first_run = TRUE;

  lzw_create( &lzw ,h_sour,h_dest );

  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 );

  }

        

首页 上一页 1 2 3 4 5 下一页 尾页 2/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇数据结构实验叉树的建立 下一篇C++因子和阶乘

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: