实现LZARI压缩算法的C++类

实现LZARI压缩算法的C++类
作者: 阙荣文(querw)
  这是一个基于LZARI算法的数据压缩的类.Haruhiko Okumura 于1989年7月4日用c语言写实现了这个算法.但是上面用到了一些全局或静态的变量,在MFC下用起来很不方便.我把它改写成了一个c++类,使它可以方便的压缩和解压缩,更重要的是,我新增加了两个接口,这个类可以压缩/解压缩一段内存缓冲区,而不仅仅是文件.
一共提供了5个对外接口:
1.压缩/解压缩文件
void Compress(const char *lpszInfile,const char *lpszOutfile);
void UnCompress(const char *lpszInfile,const char *lpszOutfile);
参数一目了然,可以像下面这样使用这两个接口:
LZARI Lzari;
Lzari.Compress("show.bmp","show.liz");	//压缩文件 show.bmp 到 show.liz
// Lzari.UnCompress("show.liz","show.bmp"); // 解压缩文件 show.liz 到 show.bmp
就这么简单.
2.压缩/解压缩一段内存缓冲区
void Compress(const BYTE *pInBuffer,int nInLength,const BYTE * &pOutBuffer ,int &nOutLength);
void UnCompress(const BYTE *pInBuffer,int nInLength,const BYTE * &pOutBuffer,int &nOutLength);
这两个接口的参数也不难理解,分别传入输入指针和长度,LZARI会返回一个只读的输出指针和长度.使用者不用担心内存分配的问题,当不需要使用输出结果时调用Release()就行了,以下是使用示例:
LZARI Lzari;
BYTE *pOutBuffer = NULL;
int nOutSize = 0;
char szInBuffer[] = "This is a class for compress and uncompress";
Lzari.Compress(szInBuffer,strlen(szInBuffer),pOutBuffer,nOutSize);//压缩pInBuffer
//
// 用pOutBuffer 做一些事情
//
Lzari.Release();
3.释放内存,并清空标志.	
void Release();
若想让一个LZARI类实例既进行压缩操作又进行解压缩操作,请在后一个操作调用之前调用一下Release(); 如下所示:
LZARI Lzari;
Lzari.Compress(pInBuffer,nInsize,pOutBuffer,nOutSize);//压缩pInBuffer
//
// 用pOutBuffer 做一些事情
//
Lzari.Release();
Lzari.UnCompress(pInBuffer2,nInsize2,pOutBuffer2,nOutSize2); //解压缩pInBuffer2
//
// ...
//
Lzari.Release();
请注意千万不要这样调用:
Lzari.Compress(pInBuffer,nInsize,pOutBuffer,nOutSize);//压缩pInBuffer
//
// 用pOutBuffer 做一些事情
//
Lzari.Release();
Lzari.UnCompress(pOutBuffer,nOutSize,pOutBuffer2,nOutSize2); //解压缩第一次压缩的结果
  因为Release()后pOutBuffer的指针就无效了.而如果不调用Release()又会导致pOutBuffer和pOutBuffer2指向同一段内存从而导致混乱.碰到这种情况最好使用两个类实例来完成.如下:
LZARI Lzari;
LZARI UnLzari;
Lzari.Compress(pInBuffer,nInsize,pOutBuffer,nOutSize);//压缩pInBuffer
//
// ...
//

UnLzari.UnCompress(pOutBuffer,nOutSize,pOutBuffer2,nOutSize2); //解压缩第一次压缩的结果
//
// ...
//
Lzari.Release();
UnLzari.Release();
由于程序中用到了STL的vector模板,请在stdafx.h中加入以下一行:
#include .当然,这个类并不依赖于MFC,可以使用在任何C++程序中.
另外,LZARI压缩的效果比zip差一些,差距大约是5%~10%,压缩速度则基本相当.
注:与算法有关的问题请不要问我,我也不知道 :) 其他问题欢迎指教 querw@sina.com


/**************************************************************
 LZARI.C -- A Data Compression Program
 (tab = 4 spaces)
***************************************************************
 4/7/1989 Haruhiko Okumura
 Use, distribute, and modify this program freely.
 Please send me your improved versions.
  PC-VAN  SCIENCE
  NIFTY-Serve PAF01022
  CompuServe 74050,1022
**************************************************************/

/********** Bit I/O **********/
#ifndef _FILE_H_COMPRESSION_LZARI_
#define  _FILE_H_COMPRESSION_LZARI_
//#pragma warning(disable:4786)
//#include <VECTOR>

#define _OUTPUT_STATUS

#define N   4096 /* size of ring buffer */
#define F     60 /* upper limit for match_length */
#define THRESHOLD 2   /* encode string into position and length
         if match_length is greater than this */
#define NIL   N /* index for root of binary search trees */
/********** Arithmetic Compression **********/

/*  If you are not familiar with arithmetic compression, you should read
  I. E. Witten, R. M. Neal, and J. G. Cleary,
   Communications of the ACM, Vol. 30, pp. 520-540 (1987),
 from which much have been borrowed.  */

#define M   15

/* Q1 (= 2 to the M) must be sufficiently large, but not so
 large as the unsigned long 4 * Q1 * (Q1 - 1) overflows.  */

#define Q1  (1UL << M)
#define Q2  (2 * Q1)
#define Q3  (3 * Q1)
#define Q4  (4 * Q1)
#define MAX_CUM (Q1 - 1)

#define N_CHAR  (256 - THRESHOLD + F)

class LZARI
{
public: 
 LZARI();
 virtual ~LZARI();
protected:
 FILE  *infile, *outfile;
 unsigned long textsize;
 unsigned long codesize;
 unsigned long printcount;
 unsigned char  text_buf[N + F - 1]; /* ring buffer of size N,with extra F-1 bytes to facilitate string comparison */
 int match_position;
 int match_length;  /* of longest match.  These areset by the InsertNode() procedure. */
 int lson[N + 1];
 int rson[N + 257];
 int dad[N + 1];  /* left & right children &parents -- These constitute binary search trees. */

 /* character code = 0, 1, ..., N_CHAR - 1 */

 unsigned long low;
 unsigned long high;
 unsigned long value;
 int  shifts;  /* counts for magnifying low and high around Q2 */
 int  char_to_sym[N_CHAR];
 int sym_to_char[N_CHAR + 1];
 unsigned int sym_freq[N_CHAR + 1];  /* frequency for symbols */
 unsigned int sym_cum[N_CHAR + 1];   /* cumulative freq for symbols */
 unsigned int position_cum[N + 1];   /* cumulative freq for positions */

 // Compress in memory;
 bool m_bMem;

 std::vector<BYTE> m_OutBuffer;
 //BYTE *m_pOutBuffer;
 int m_nOutLength;
 //int m_nOutCur;

 const BYTE *m_pInBuffer;
 int m_nInLength;
 int m_nInCur;

 unsig

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值