LZW 编解码算法实现与分析
一、实验目的
掌握词典编码的基本原理,用C/C++/Python等语言编程实现LZW解码器并分析编解码算法
1.LZW编码原理和实现算法
步骤1:将词典初始化为包含所有可能的单字符,当前前缀P初始化为空。
步骤2:当前字符C=字符流中的下一个字符。
步骤3:判断P+C是否在词典中
(1)如果“是”,则用C扩展P,即让P=P+C,返回到步骤2。
(2)如果“否”,则
输出与当前前缀P相对应的码字W;
将P+C添加到词典中;
令P=C,并返回到步骤2
2.LZW解码原理和实现算法
步骤1:在开始译码时词典包含所有可能的前缀根。
步骤2:令CW:=码字流中的第一个码字。
步骤3:输出当前缀-符串string.CW到码字流。
步骤4:先前码字PW:=当前码字CW。
步骤5:当前码字CW:=码字流的下一个码字。
步骤6:判断当前缀-符串string.CW 是否在词典中。 (1)如果”是”,则把当前缀-符串string.CW输出到字符流。
当前前缀P:=先前缀-符串string.PW。
当前字符C:=当前前缀-符串string.CW的第一个字符。
把缀-符串P+C添加到词典。 (2)如果”否”,则当前前缀P:=先前缀-符串string.PW。
当前字符C:=当前缀-符串string.CW的第一个字符。
输出缀-符串P+C到字符流,然后把它添加到词典中。
步骤7:判断码字流中是否还有码字要译。 (1)如果”是”,就返回步骤(2)如果”否”,结束
- 码字在词典中不存在时候,则把先前码字PW译出的P+先前码字PW译出的第一个字符进行输出,并写入词典中。
例如:aba不在词典中
二、实验步骤
1.首先调试LZW的编码程序,以一个文本文件作为输入,得到输出的LZW编码文件。
2. 以实验步骤一得到的编码文件作为输入,编写LZW的解码程序。在写解码程序时需要对关键语句加上注释,并说明进行何操作。在实验报告中重点说明当前码字在词典中不存在时应如何处理并解释原因。
3. 选择至少十种不同格式类型的文件,使用LZW编码器进行压缩得到输出的压缩比特流文件。对各种不同格式的文件进行压缩效率的分析。
三、实验分析
1、代码部分:
- 根据给定程序的主函数可知参数设置:
E 输入文件名 输出文件名——编码
D 输入文件名 输出文件名——解码
由此可从原文件R.txt编码为R_out.txt
得到的编码文件R_out.txt作为输入 解码出R_out1.txt
- 初始化:
void InitDictionary( void){
int i;
for( i=0; i<256; i++){
dictionary[i].suffix = i;
dictionary[i].parent = -1;
dictionary[i].firstchild = -1;
dictionary[i].nextsibling = i+1;
}
dictionary[255].nextsibling = -1;
next_code = 256;
}
- 查找词典中是否有字符串:
int InDictionary( int character, int string_code){
int sibling;
if( 0>string_code) return character;
sibling = dictionary[string_code].firstchild;
while( -1<sibling){
if( character == dictionary[sibling].suffix) return sibling;
sibling = dictionary[sibling].nextsibling;
}
return -1;
}
- 在词典中加入新串:
void AddToDictionary( int character, int string_code){
int firstsibling, nextsibling;
if( 0>string_code) return;
dictionary[next_code].suffix = character;
dictionary[next_code].parent = string_code;
dictionary[next_code].nextsibling = -1;
dictionary[next_code].firstchild = -1;
firstsibling = dictionary[string_code].firstchild;
if( -1<firstsibling){ // the parent has child
nextsibling = firstsibling;
while( -1<dictionary[nextsibling].nextsibling )
nextsibling = dictionary[nextsibling].nextsibling;
dictionary[nextsibling].nextsibling = next_code;
}else{// no child before, modify it to be the first
dictionary[string_code].firstchild = next_code;
}
next_code ++;
}
- 编码程序段:
void LZWEncode( FILE *fp, BITFILE *bf){
int character;
int string_code;
int index;
unsigned long file_length;
fseek( fp, 0, SEEK_END);
file_length = ftell( fp);
fseek( fp, 0, SEEK_SET);
BitsOutput( bf, file_length, 4*8);
InitDictionary();
string_code = -1;
while( EOF!=(character=fgetc( fp))){
index = InDictionary( character, string_code);
if( 0<=index){ // string+character in dictionary
string_code = index;
}else{ // string+character not in dictionary
output( bf, string_code);
if( MAX_CODE > next_code){ // free space in dictionary
// add string+character to dictionary
AddToDictionary( character, string_code);
}
string_code = character;
}
}
output( bf, string_code);
}
- 解码程序注释:
//解码函数 先前码字PW 当前码字CW
void LZWDecode( BITFILE *bf, FILE *fp){ //bf是编码形成的文件指针 fp是原字符文件指针
int character; //字符
int new_code, last_code; //定义当前码字CW和先前码字PW
int phrase_length;//字符串长度
unsigned long file_length;//文件长度
file_length = BitsInput( bf, 4*8); //读出输入的编码文件的长度
if( -1 == file_length) file_length = 0;//文件长度为零
InitDictionary();//初始化字典
last_code = -1;//上一个码字初始化为-1 因为现在从第一个码字开始读,没有上一个
while( 0<file_length)//输入文件还存在(未读/未读完)的情况下开始解码
{
new_code = input( bf);//当前码字为输入的编码文件
if( new_code >= next_code){ // 当前码字CW在词典中不存在
d_stack[0] = character;//当前字符C写入字符串堆栈
phrase_length = DecodeString( 1, last_code);//解码PW得到字符
}else{
phrase_length = DecodeString( 0, new_code);//解码CW得到字符
}
character = d_stack[phrase_length-1];//将解码后的字符流P+C更新至堆栈
while( 0<phrase_length){//字符串写入文件
phrase_length --;
fputc( d_stack[ phrase_length], fp);
file_length--; }
if( MAX_CODE>next_code){//字典有空间的情况下在字典中添加新词条
AddToDictionary( character, last_code);//字符P+C写入字典
}
last_code = new_code;//当前码字CW成为先前码字PW
}
}
2、十种不同格式类型的文件使用LZW编码器进行压缩
文件格式 | 压缩效率 |
---|---|
jpg | 1.33 |
doc | 0.34 |
1.15 | |
mp4 | 1.24 |
png | 1.58 |
dat | 0.91 |
tga | 0.80 |
wav | 0.18 |
bmp | 0.33 |
xls | 0.42 |
- jpg、pdf、mp4、png等格式进行LZW编码后效率并未提高
- LZW编码并不适用于每种格式的文件