数据压缩实验3——LZW编解码算法的实现

一、原理概述
LZW的编码思想是不断地从字符流中提取新的字符串,通俗地理解为新“词条”,然后用“代号”也就是码字表示这个“词条”。这样一来,对字符流的编码就变成了用码字去替换字符流,生成码字流,从而达到压缩数据的目的。LZW编码是围绕称为词典的转换表来完成的。LZW编码器通过管理这个词典完成输入与输出之间的转换。LZW编码器的输入是字符流,字符流可以是用8位ASCII字符组成的字符串,而输出是用n位(例如12位)表示的码字流。
二.LZW解码原理
在开始译码时词典包含所有可能的前缀根
令CW=码字流中的第一个码字
输出当前缀-符串string.CW到码字流
先前码字PW=当前码字CW
当前码字CW=码字流的下一个码字;
判断当前缀-符串string.CW 是否在词典中;
(1)是——把当前缀-符串string.CW输出到字符流:
当前前缀P=先前缀-符串string.PW;
当前字符C=当前前缀-符串string.CW的第一个字符;
把缀-符串P+C添加到词典;
(2)否——当前前缀P=先前缀-符串string.PW。
当前字符C=当前缀-符串string.CW的第一个字符。
输出缀-符串P+C到字符流,然后把它添加到词典中。
判断码字流中是否还有码字要译:
(1)是——返回步骤4;
(2)否——结束。
三.LZW解码原理:
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)如果”是”,就返回步骤4。

(2)如果”否”,结束。
四.代码实现
LZW主函数代码:

/*
 * Definition for LZW coding
 *
 * vim: ts=4 sw=4 cindent nowrap
 */
#include <stdlib.h>
#include <stdio.h>
#include "bitio.h"
#pragma warning(disable:4996)  
#pragma warning(disable:4703) 
#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;
}
void InitDictionary(void) {
	int i;
	//初始字典中每一个节点的根节点都是自己,兄弟姐妹就是自己+1,只有第256个没有兄弟姐妹
	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;//下一个要插入的suffix
}
/*
 * 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;//前缀为string_code的first_child
	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;//尾缀字符添加为character
	dictionary[next_code].parent = string_code;//前缀为string_code
	dictionary[next_code].nextsibling = -1;//没有nextsibling
	dictionary[next_code].firstchild = -1;//没有firstchild
	firstsibling = dictionary[string_code].firstchild;//firstsibling是查找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);//文件位置位于SEEK_END,偏移量是0
	file_length = ftell(fp);//文件的长度,ftell函数用于获得文件当前位置相对于文件首的偏移字节
	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);
}
 
void LZWDecode(BITFILE* bf, FILE* 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;
	if (file_length == -1)file_length = 0;
	InitDictionary();//初始化字典树
	last_code = -1;
	while (0 < file_length) {
		new_code = input(bf);
		if (new_code >= next_code) { // this is the case CSCSC( not in dict)
			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) {// add the new phrase to dictionary
			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//第一个字符为E开始编码
		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//第一个字符为D开始解码
		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是原文件 2 是编码后文件
在这里插入图片描述
六.实验结果及分析
对于部分类型的文件,经过LZW编码后,文件反而变大了,但对于.yuv文件来说压缩效率很高。我觉得可能是有些文件的内容重复率过低,通过LZW编码后反而增加了冗余。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值