实验 LZW编解码

1.LZW编码原理和实现

LZW算法基于一个转换表(词典),将输入的字符串映射成定长的码字。LZW串表具有前缀性:表中任何一个字符串的前缀字符串也在表中。

步骤1:编码前将词典初始化以包含所有的单字符串,当前前缀P初始化为空

步骤2:将字符流中的下一个字符赋给当前字符C

步骤3:判断P+C是否在词典中

——在,则扩展P,P=P+C,并返回步骤2

——不在,则输出与当前前缀P对应的码字W,将P+C添加到词典中,更新前缀令P=C并返回步骤2

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; //将character赋给string_code,及更新前缀,准备读入下一字符
		}
	}
	output( bf, string_code);
}

用一个txt文件进行编码测试,修改命令行参数

 

 编码后的txt文件占用内存变小了。

2.LZW解码

编码过程建立的词典,实际上并不与码流一起传输。那么在解码端就需要按照编码端相同的规则同步建立词典。因此解码开始前仍需要初始化词典,使其包含所有的单字符。

解码过程中两个核心变量:PW(码流中的上一个码字)和CW(当前码字)

解码流程:

初始化词典

CW=数据流的第一个码字

PW=CW

输出CW

CW=数据流的下一码字

if CW在词典中
{
    输出CW对应字符串
    将PW+CW的第一个字符写入词典
}
else
{
    输出PW+PW的第一个字符
    将PW+PW的第一个字符写入词典
}

CW=数据流的下一码字
......
重复如此,直到文件内容全部解码

当CW(当前码字)不在词典中时:

解码总是慢编码一步,如果CW不在词典中,说明CW是由刚解完的码字+此码字的第一个字符组成。

int DecodeString( int start, int code){  //解码序列
	//需填充
	int count;
	count = start;
	while (0 <= code)  //一直读入,直到为空,得到此序列的长度
	{
		d_stack[count] = dictionary[code].suffix; //构造树
		code = dictionary[code].parent;
		count++;
	}
	return count; //count记录了字符数,即串长
}
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;
	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]; //将字符串当前位置的值赋给character
		while (0 < phrase_length)
		{
			phrase_length--;
			fputc(d_stack[phrase_length], fp);
			file_length--;
		}
	}
	if (MAX_CODE > next_code) //输出解码码字到词典
	{
		AddToDictionary(character, last_code);  //并更新词典
	}
	last_code = new_code; //更新前缀
}

 3.不同格式类型文件的压缩情况(共十种,其中有些文件编码后所在内存反而变大了,我不是很理解)其中example为原文件,encode为编码后文件

(1)TXT文本文件——19/23,约为0.826

(2)JPG文件——37/22,约为1.681

(3)PNG文件—— 56/34,约为1.647

(4)M4A文件—— 80.9/60.5,约为1.337

(5)AAC文件—— 798/645,约为1.237

(6)WAV文件——543/584,约为0.930

(7)WORD文件——18/12=1.5

(8)PDF文件—— 26/19,约为1.368

(9)PowerPoint文件—— 47/35,约为1.343

(10)Excel文件——13/10=1.3

 在我选取的10种文件格式中,仅有txt文件和wav文件编码后所占存储空间变小。相比之下,txt文件的压缩效率更高。

LZW.c的完整代码如下:

ps:编译时系统对fopen函数用法的报错,在添加#define _CRT_SECURE_NO_DEPRECATE后可得到消除)

#define _CRT_SECURE_NO_DEPRECATE
#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){  //解码序列
	//需填充
	int count;
	count = start;
	while (0 <= code)  //一直读入,直到为空,得到此序列的长度
	{
		d_stack[count] = dictionary[code].suffix; //构造树
		code = dictionary[code].parent;
		count++;
	}
	return count; //count记录了字符数,即串长
}

void InitDictionary( void){  //初始化词典
	int i;

	for( i=0; i<256; i++){  //将256个单字符及对应编码写入词典中
		dictionary[i].suffix = i;
		dictionary[i].parent = -1;  //初始时没有母树也没有孩子节点,随着for循环的进行
		dictionary[i].firstchild = -1;  //更新树及节点
		dictionary[i].nextsibling = i+1;
	}
	dictionary[255].nextsibling = -1;  //全部单字符录入后,最后一个单字符没有兄弟节点,为-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;
	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; //将character赋给string_code,及更新前缀,准备读入下一字符
		}
	}
	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;
	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]; //将字符串当前位置的值赋给character
		while (0 < phrase_length)
		{
			phrase_length--;
			fputc(d_stack[phrase_length], fp);
			file_length--;
		}
	}
	if (MAX_CODE > next_code) //输出解码码字到词典
	{
		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;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值