LZW Data Compression Algorithm
2016-01-04 14:04
429 查看
LZW is a “dictionary”-based data compression algorithm, created by Lempel and Ziv in 1978 (LZ78) and further refined by Welch in 1984. LZW algorithm uses a dynamically generated dictionary and and encodes strings by a reference to the dictionary.
Step 2. Read the next byte from the input stream.
Step 3. If the byte is an EOF goto step 6.
Step 4. If concatenating the byte to the encoded string produces a string that is in the dictionary, then concatenate the the byte to the encoded string and go to step 2.
Step 5. If concatenating the byte to the encoded string produces a string that is not in the dictionary, then add the new sting to the dictionary. write the code for the encoded string to the output stream. set the encoded string equal to the new byte and go to step 2.
Step 6. Write out code for encoded string and exit.
Step 2. Read the first code word from the input stream and write out the byte it encodes.
Step 3. Read the next code word from the input stream.
Step 4. If the code word is an EOF exit.
Step 5. Write out the string encoded by the code word.
Step 6. Concatenate the first character in the new code word to the string produced by the previous code word and add the resulting string to the dictionar
4000
y.
Step 7. Go to step 3.
The Basic Encoding Algorithm
Step 1. Initialize dictionary to contain one entry for each byte. Initialize the encoded string with the first byte of the input stream.Step 2. Read the next byte from the input stream.
Step 3. If the byte is an EOF goto step 6.
Step 4. If concatenating the byte to the encoded string produces a string that is in the dictionary, then concatenate the the byte to the encoded string and go to step 2.
Step 5. If concatenating the byte to the encoded string produces a string that is not in the dictionary, then add the new sting to the dictionary. write the code for the encoded string to the output stream. set the encoded string equal to the new byte and go to step 2.
Step 6. Write out code for encoded string and exit.
void compress(ifstream& ifile, ofstream& ofile){ unordered_map<string,unsigned short>dictionary; //Build the dictionary unsigned short Dictsize=256; for(unsigned short i=0;i<Dictsize;i++) dictionary[string(1,i)]=i; char z; string S; //empty string ifile.seekg(0, ifile.beg); while(!ifile.eof()){ ifile.read((char*)&z,sizeof(char)); if(dictionary.count(S+z))//can find S S+=z; else{//S is not in dictionary D //ofile<<dictionary[S]; //output pointer (S,D) unsigned short tmp = dictionary[S]; ofile.write((char*)&tmp, sizeof(unsigned short)); dictionary[S+z] = Dictsize++; //add to dictionary S = z; } } if(!S.empty()){ unsigned short tmp = dictionary[S]; ofile.write((char*)&tmp, sizeof(unsigned short)); //ofile<<dictionary[S]; } ofile.flush(); }
The Basic Decoding Algorithm
Step 1. Initialize dictionary to contain one entry for each byte.Step 2. Read the first code word from the input stream and write out the byte it encodes.
Step 3. Read the next code word from the input stream.
Step 4. If the code word is an EOF exit.
Step 5. Write out the string encoded by the code word.
Step 6. Concatenate the first character in the new code word to the string produced by the previous code word and add the resulting string to the dictionar
4000
y.
Step 7. Go to step 3.
void decompress(ifstream& ifile, ofstream& ofile){ //Build the dictionary unordered_map<unsigned short,string>inv_dictionary; unsigned short Dictsize=256; for(unsigned short i=0;i<Dictsize;i++) inv_dictionary[i] = string(1,i); unsigned short k; string S; string entry; Dictsize--; ifile.seekg(0, ifile.beg); while(!ifile.eof()){ ifile.read((char*)&k,sizeof(unsigned short)); if(inv_dictionary.count(k)) entry = inv_dictionary[k]; else if(k==Dictsize) entry = S+ S[0]; else throw "Bad compression code"; ofile<<entry; inv_dictionary[Dictsize++] = S + entry[0]; S = entry; } ofile.flush(); }
相关文章推荐
- 谷歌的网页排序算法(PageRank Algorithm)
- MIT Introduction to Algorithms 学习笔记(七)
- CF Good Bye 2015 E. New Year and Three Musketeers(贪心+枚举)
- GO语言可以直接输出数组
- 迪士尼黑科技:爬墙机器人 VertiGo
- Genymotion模拟器无法开启的解决方法——Unable to start the virtual device,The virtual device got no IP address
- 详解WordPress中分类函数wp_list_categories的使用
- 在WordPress中加入Google搜索功能的简单步骤讲解
- 让Samba支持软连接
- Django如何搭建服务器
- Django ORM 中的批量操作
- Google是不是真的不能用了?非常奇怪的问题
- cfgoodbye2015 B
- #include <algorithm> 常用函数
- django之创建第6-2个项目-过滤器列表
- django session
- SKUA-GOCAD建模常见问题汇总
- LSGO软件技术团队2015~2016学年第十七周(1221~1227)总结
- LSGO软件技术团队2015~2016学年第十六周(1214~1220)总结
- LSGO软件技术团队2015~2016学年第十五周(1207~1213)总结