LZW压缩算法——简明原理与实现
来源:互联网 发布:学美术的软件 编辑:程序博客网 时间:2024/06/05 11:20
LZW和哈夫曼编码一样,是无损压缩中的一种。该算法通过建立字典,实现字符重用与编码,适用于source中重复率很高的文本压缩。本文首先讲下LZW的编解码原理,然后给出LZW的实现code。
*********************原理*********************
编码:
- 编码0-255用来存储Ascii码为[0,255]的字符,放在字典里。
- 编码从256开始,将出现过的字符计入字典
- 核心思想:利用字符的可重用性,每当往结果输出一个编码,就将一个新的string存入dictionary
算法流程:
举例:
解码:
编码的逆过程,若编码是string到int的映射,我们可以将解码过程描述为int到string的映射。
- LZW算法的解码无需在编码过程中存储字典(这样太浪费空间了)
- 解码初始化依旧用256个Ascii码,后面每读入一个编码(int),检查其在dictionary中的映射,并不断将新的映射加入字典
算法流程:
解码的例子建议读者用下面的代码直接调试吧~
*********************实现*********************
我用C++实现的,Compress和Decompress两个函数分别实现编解码
/************************************************************************//*File Name: LZW.cpp*@Function: Lossless Compression@Author: Sophia Zhang @Create Time: 2012-9-19 10:00@Last Modify: 2012-9-19 11:10*//************************************************************************/#include"iostream"#include "map"#include "string"#include "iterator"#include "vector"using namespace std;/************************************************************************//*Compress Module*input: str - the string need to be compressedresult - compress result*//************************************************************************/template<typename TypeIterator>TypeIterator Compress(string str, TypeIterator result){//Build the dictionarymap<string,int>dictionary;int Dictsize=256;for(int i=0;i<Dictsize;i++)dictionary[string(1,i)]=i;char z;string S;for(string::const_iterator it = str.begin(); it!=str.end(); it++){ z = *it; if(dictionary.count(S+z))//can find S S+=z; else//S is not in dictionary D { *result++ = dictionary[S];//output pointer (S,D) dictionary[S+z] = Dictsize++; //add to dictionary S = z; }}if(!S.empty())*result++ = dictionary[S];return result;}/************************************************************************//*Decompress Module*input: TypeIterator result - compression result, to be decompressed*//************************************************************************/template<typename TypeIterator>string Decompress(TypeIterator result){map<int,string>inv_dictionary;int Dictsize=256;for(int i=0;i<Dictsize;i++)inv_dictionary[i] = string(1,i);char z;string S;string entry;string res;Dictsize--;//because the first "Dictsize++" make no sense, it has only one char in [0,255]for(TypeIterator::iterator it = result.begin(); it!=result.end(); it++){int k = *it;if(inv_dictionary.count(k))entry = inv_dictionary[k];else if(k==Dictsize)entry = S+ S[0];elsethrow "Bad compression code";res += entry;inv_dictionary[Dictsize++] = S + entry[0];S = entry;}return res;}int main(){typedef vector<int> TypeIterator;TypeIterator compress_res;string S = "the/rain/in/Spain/falls/mainly/on/the/plain";Compress(S,std::back_inserter(compress_res));// copy(compress_res.begin(),compress_res.end(),std::ostream_iterator<int>(std::cout,","));// std::cout<<std::endl;//output the compressed resultfor( TypeIterator::iterator it= compress_res.begin(); it!=compress_res.end(); it++)cout<<(*it)<<endl;//decompress the compressed resultstring decompress = Decompress(compress_res);cout<<decompress<<endl;}
result:
Reference:
1. http://www.stringology.org/DataCompression/lzw-e/index_en.html
2. http://www.dspguide.com/ch27/5.htm
3. http://marknelson.us/1989/10/01/lzw-data-compression/
4. http://www.ccs.neu.edu/home/jnl22/oldsite/cshonor/jeff.html
关于Compression更多的学习资料将继续更新,敬请关注本博客和新浪微博Sophia_qing。
- LZW压缩算法——简明原理与实现
- LZW压缩算法——简明原理与实现
- LZW压缩算法——简明原理与实现
- LZW压缩算法——简明原理与实现
- lzw 压缩算法的原理与细节思考
- lzw字符串压缩算法实现
- lzw压缩算法的c语言实现
- LZW算法压缩c语言实现
- LZW算法压缩c语言实现
- java实现的LZW 压缩算法源码
- lzw压缩算法的c语言实现
- LZW 压缩算法的C++实现
- 关于LZW算法的压缩与解压缩
- JS LZW算法压缩与解压
- LZW编码—图像压缩
- LZW for GIF 算法原理和实现
- LZW for GIF 算法原理和实现
- LZW for GIF 算法原理和实现
- C语言标准库概览详述[1]-断言<assert.h>
- 从那一天起,你就成了我的全部
- 不要习惯于浪费——用技术提高回归测试精确度
- Android中使用Geocoding API
- Nginx 下缓存静态文件(如css js)
- LZW压缩算法——简明原理与实现
- JAVA 封装
- 从铁路订票系统问题看系统架构师的责任和水平
- iOS 定制应用程序字体
- Maven实战(二)构建简单Maven项目
- 为什么需要Spring
- Maven实战(三)Eclipse构建Maven项目
- 七夕,染红了我的相思
- Maven实战(四)生命周期