数据压缩【实验三】lzw词典编码

一、实验步骤

(1)首先调试LZW的编码程序,以一个文本文件作为输入,得到输出的LZW编码文件。
(2)将得到的编码文件作为输入文件,编写LZW的解码程序。
(3)选择至少十种不同格式类型的文件,使用LZW编码器进行压缩得到输出的压缩比特流文件,并对各种不同格式的文件进行压缩效率的分析。

二、LZW编码原理和实现算法

在这里插入图片描述

1) LZW编码

在这里插入图片描述

(1)原理

LZW编码是围绕称为词典的转换表来完成的。LZW编码器通过管理这个词典完成输入与输出之间的转换。LZW编码器的输入是字符流,字符流可以是用8位ASCII字符组成的字符串,而输出是用n位(例如12位)表示的码字流。
步骤:
步骤1:将词典初始化为包含所有可能的单字符,当前前缀P初始化为空。
步骤2:当前字符C=字符流中的下一个字符。
步骤3:判断P+C是否在词典中
(1)如果“是”,则用C扩展P,即让P=P+C,返回到步骤2。
(2)如果“否”,则
输出与当前前缀P相对应的码字W;
将P+C添加到词典中;
令P=C,并返回到步骤2

(2)代码

LZW编码算法可用下述函数实现。首先初始化词典,然后顺序从待压缩文件中读入字符并按照上述算法执行编码。最后将编得的码字流输出至文件中。

void LZWEncode( FILE *fp, BITFILE *bf){//编码
	int character;//c
	int string_code;//p
	int index;//pc
	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)))//往后读一个字符,判断pc是否在字典里
	{
		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);
}

2)LZW解码

在这里插入图片描述

(1)原理

步骤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)如果”是”,就返回步骤4。
(2)如果”否”,结束。
LZW解码算法可用下述函数实现。首先初始化词典,然后顺序从压缩文件中读入码字并按照上述算法执行解码。最后将解得的字符串输出至文件中。
【注意】对于步骤6进行判断的第二种情况,即当前前缀-符串string.CW并不在词典中的情况,我们依然可以正确实现解码输出。

出现这种情况的原因:在编码时,上一个刚创建的新词条直接被下一个词组所使用。
算法的实现表明,解码端的词典建立会比编码端晚一步。
处理这种情况的做法:由于下一个词条的尾缀必定是该词条的第一个字符,所以直接输出这个字符,然后再用先前一条词条的code进行译码即可。

(2)代码

void LZWDecode( BITFILE *bf, FILE *fp){//解码
	int character;
	int new_code, last_code;
	int phrase_length;
	unsigned long file_length;

	file_length = BitsInput( bf, 4*8);
	if( -1 == file_length) file_length = 0;
	/*需填充*/
		file_length = 0;
		InitDictionary();
	last_code = -1;//同编码
	while( 0<file_length)//未没读到文件末尾
	 {
	  new_code = input( bf);
	  if( new_code >= next_code)// 不在字典里,需要解码
	  { 
	   d_stack[0] = character;
	   phrase_length = DecodeString( 1, last_code);
	  }
	  else
	  {
	   phrase_length = DecodeString( 0, new_code);
	  }
	  character = d_stack[phrase_length-1];
	  while( 0<phrase_length)
	  {
	   phrase_length --;
	   fputc( d_stack[ phrase_length], fp);
	   file_length--;
	  }
	  if( MAX_CODE>next_code)//字典加入新phrase
	  { 
	   AddToDictionary( character, last_code);
	  }
	  last_code = new_code;
	 }
}

三、实验代码注释

/*
 * Definition for LZW coding 
 *
 * vim: ts=4 sw=4 cindent nowrap
 */
#include <stdlib.h>
#include <stdio.h>
#include "bitio.h"
#define MAX_CODE 65535

struct {
	int suffix;
	int parent, firstchild, nextsibling;
} dictionary[MAX_CODE+1];
int next_code;
int d_stack[MAX_CODE]; // stack for decoding a phrase

#define input(f) ((int)BitsInput( f, 16))
#define output(f, x) BitsOutput( f, (unsigned long)(x), 16)

int DecodeString( int start, int code);
void InitDictionary( void);
void PrintDictionary( void){//定义了每个字符串存储时的结构
	int n;
	int count;
	for( n=256; n<next_code; n++){
		count = DecodeString( 0, n);
		printf( "%4d->", n);
		while( 0<count--) printf("%c", (char)(d_stack[count]));
		printf( "\n");
	}
}

int DecodeString( int start, int code){
	//通过 for 循环实现了查找词典与输出字符串

	int count;
	count = start;
	while( 0<=code){
		d_stack[ count] = dictionary[code].suffix;
		code = dictionary[code].parent;
		count ++;
	}
	return count;
}
void InitDictionary( void){ //初始化词典,将256以内的ASCII码初始化到表中

	int i;

	for( i=0; i<256; i++){
		dictionary[i].suffix = i;//码字的ASCII码
		dictionary[i].parent = -1;//因为是256以内所以没有parent
		dictionary[i].firstchild = -1;//因为是256以内所以没有firstchild
		dictionary[i].nextsibling = i+1;//nextsibling即是下一个ASCII码
	}
	dictionary[255].nextsibling = -1;
	next_code = 256;
}
/*
 * Input: string represented by string_code in dictionary,
 * Output: the index of character+string in the dictionary
 * 		index = -1 if not found
 */
int InDictionary( int character, int string_code){//初始化词典
	int sibling;
	if( 0>string_code) return character;//若为单个字符,直接返回c
	sibling = dictionary[string_code].firstchild;//slibling为前缀的第一个孩子节点
	while( -1<sibling){//找到
		if( character == dictionary[sibling].suffix) return sibling;//如果c为p的孩子节点,返回孩子
		sibling = dictionary[sibling].nextsibling;//否则p指向孩子节点的兄弟节点,继续判断c是否是p的子节点
	}
	return -1;//表中没有PC返回-1
}

//通过 while 循环寻找与character相同的节点。
//若有,则返回节点值;若没有,则返回 -1
void AddToDictionary( int character, int string_code){//将新串加入词典
	int firstsibling, nextsibling;
	if( 0>string_code) return;
	dictionary[next_code].suffix = character;//c
	dictionary[next_code].parent = string_code;//p
	dictionary[next_code].nextsibling = -1;//新加入还没nextsibling
	dictionary[next_code].firstchild = -1;//新加入还没有firstchild
	firstsibling = dictionary[string_code].firstchild;//找到P的第一个child
	if( -1<firstsibling){	// the parent has child
		nextsibling = firstsibling;
		while( -1<dictionary[nextsibling].nextsibling ) 
			nextsibling = dictionary[nextsibling].nextsibling;//顺着从第一个child一个一个找兄弟
		dictionary[nextsibling].nextsibling = next_code;//找到最后一个,将nextcode放到最后一位
	}
	else
	{// no child before, modify it to be the first
		dictionary[string_code].firstchild = next_code;//若没找到firstchild,nextcode就是firstchild
	}
	next_code ++;
}

void LZWEncode( FILE *fp, BITFILE *bf){//编码
	int character;//c
	int string_code;//p
	int index;//pc
	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)))//往后读一个字符,判断pc是否在字典里
	{
		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);
}

void LZWDecode( BITFILE *bf, FILE *fp){//解码
	int character;
	int new_code, last_code;
	int phrase_length;
	unsigned long file_length;

	file_length = BitsInput( bf, 4*8);
	if( -1 == file_length) file_length = 0;
	/*需填充*/
		file_length = 0;
		InitDictionary();
	last_code = -1;//同编码
	while( 0<file_length)//未没读到文件末尾
	 {
	  new_code = input( bf);
	  if( new_code >= next_code)// 不在字典里,需要解码
	  { 
	   d_stack[0] = character;
	   phrase_length = DecodeString( 1, last_code);
	  }
	  else
	  {
	   phrase_length = DecodeString( 0, new_code);
	  }
	  character = d_stack[phrase_length-1];
	  while( 0<phrase_length)
	  {
	   phrase_length --;
	   fputc( d_stack[ phrase_length], fp);
	   file_length--;
	  }
	  if( MAX_CODE>next_code)//字典加入新phrase
	  { 
	   AddToDictionary( character, last_code);
	  }
	  last_code = new_code;
	 }
}


int main( int argc, char **argv){
	FILE *fp;
	BITFILE *bf;

	if( 4>argc){
		fprintf( stdout, "usage: \n%s <o> <ifile> <ofile>\n", argv[0]);
		fprintf( stdout, "\t<o>: E or D reffers encode or decode\n");
		fprintf( stdout, "\t<ifile>: input file name\n");
		fprintf( stdout, "\t<ofile>: output file name\n");
		return -1;
	}
	if( 'E' == argv[1][0]){ // do encoding
		fp = fopen( argv[2], "rb");
		bf = OpenBitFileOutput( argv[3]);
		if( NULL!=fp && NULL!=bf){
			LZWEncode( fp, bf);
			fclose( fp);
			CloseBitFileOutput( bf);
			fprintf( stdout, "encoding done\n");
		}
	}else if( 'D' == argv[1][0]){	// do decoding
		bf = OpenBitFileInput( argv[2]);
		fp = fopen( argv[3], "wb");
		if( NULL!=fp && NULL!=bf){
			LZWDecode( bf, fp);
			fclose( fp);
			CloseBitFileInput( bf);
			fprintf( stdout, "decoding done\n");
		}
	}else{	// otherwise
		fprintf( stderr, "not supported operation\n");
	}
	return 0;
}

四、实验结果

原文本文件test.txt
在这里插入图片描述

编码文本文件test_encode.txt
在这里插入图片描述

解码文本文件test_decode.txt
在这里插入图片描述

五、压缩效率

选择十种不同格式类型的文件,使用LZW编码器进行压缩得到输出的压缩比特流文件,并对各种不同格式的文件进行压缩效率的分析。

在这里插入图片描述

六、实验反思

(1) LZW算法适合用于压缩较大的文字类型文件,且内容重复率越高,压缩效率越大。LZW在字符串重复概率低时,影响压缩效率。
(2)LZW算法的实现基础在于编解码两端词典表的创建。
(3)LZW算法不需要将词典表传送至解码端,从而避免了因词典表的内容过大,导致压缩失效。
(4)LZW只需要一编扫描,具有自适应的特点。算法简单,便于快速实现(数字查找树/建树)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值