LZW编解码算法的实现

一、实验目的

掌握词典编码的基本原理,用C/C++等语言编程实现LZW解码器并分析编解码算法

二、实验内容

1.LZW编码原理和实现算法

LZW的编码思想是不断地从字符流中提取新的字符串,通俗地理解为新“词条”,然后用“代号”也就是码字表示这个“词条”。这样一来,对字符流的编码就变成了用码字去替换字符流,生成码字流,从而达到压缩数据的目的。

LZW编码是围绕称为词典的转换表来完成的。LZW编码器通过管理这个词典完成输入与输出之间的转换。LZW编码器的输
入是字符流,字符流可以是用8位ASCII字符组成的字符串,而输出是用n位(例如12位)表示的码字流。

LZW编码算法的步骤如下:

  • 步骤1:将词典初始化为包含所有可能的单字符,当前前缀P初始化为空。
  • 步骤2:当前字符C=字符流中的下一个字符。
  • 步骤3:判断P+C是否在词典中
  • (1)如果“是”,则用C扩展P,即让P=P+C,返回到步骤2。
  • (2)如果“否”,则输出与当前前缀P相对应的码字W; 将P+C添加到词典中; 令P=C,并返回到步骤2

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

编码实现函数:
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);
}
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解码算法可用下述函数实现。首先初始化词典,然后顺序从压缩文件中读入码字并按照上述算法执行解码。最后将解得的字符串输出至文件中。

解码实现函数:
void LZWDecode( BITFILE *bf, FILE *fp)
{	
	int character;  //字符
	int new_code, last_code; //先前码字pw、当前码字cw 
	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)  //当前码字不存在或错误
		//判断解码端收到的码字是否已经存在于字典中
		{
			//解码端收到的码字不在字典中
			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)  //词典还有空间时
		{
			AddToDictionary(character, last_code);
		}
		last_code = new_code;  //更新词典
	}
}

三、实验步骤

1.首先调试LZW的编码程序,以一个文本文件作为输入,得到输出的LZW编码文件。
2. 以实验步骤一得到的编码文件作为输入,编写LZW的解码程序。在写解码程序时需要对关键语句加上注释,并说明进行何操作。在实验报告中重点说明当前码字在词典中不存在时应如何处理并解释原因。
3. 选择至少十种不同格式类型的文件,使用LZW编码器进行压缩得到输出的压缩比特流文件。对各种不同格式的文件进行压缩效率的分析。

四、其他代码及结果

1.初始化词典
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;
}
2.查找字符串
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;
}
3.在词典中写入新词
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 ++;
}

原先的文件内容:
在这里插入图片描述
编码后得到的文件内容:
在这里插入图片描述
解码后得到的文件内容:
在这里插入图片描述
可以看到得到的OUT_D和最开始的IN文件相同,编解码正确

选取十个不同格式的文件:

在这里插入图片描述
运行程序后的文件:
(2为编码后的文件)
在这里插入图片描述

文件格式压缩效率
avi1.208
bmp0.058
docx1.538
jpeg1.318
mp41.257
pdf1.296
png1.379
pptx1.406
txt1.000
xlsx1.488

只有bmp文件编码后效率提高,且提高较多
编码效率取决于文件格式和文件内容及文件大小

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LZW压缩算法是一种经典的无损压缩算法,使用字典进行码和解码,可以有效地减小数据的存储空间。下面是一个简单的Verilog实现LZW压缩算法的示例。 ``` module lzw_compress ( input clk, input rst, input [7:0] data_in, input valid_in, output [15:0] code_out, output valid_out ); // define constants localparam MAX_DICT_SIZE = 256; localparam MAX_CODE_WIDTH = 9; localparam MAX_CODE_VALUE = 512; // define states typedef enum logic [2:0] { IDLE, SEARCH, ADD, UPDATE } state_t; // define signals reg [7:0] input_char; reg [7:0] prev_char; reg [7:0] search_char; reg [8:0] code_value; reg [8:0] next_code; reg [8:0] dict[MAX_DICT_SIZE]; reg [MAX_CODE_WIDTH-1:0] code_width; state_t state; // initialize variables initial begin input_char = 0; prev_char = 0; search_char = 0; code_value = 0; next_code = 256; code_width = 9; state = IDLE; end // state machine always @(posedge clk) begin if (rst) begin input_char <= 0; prev_char <= 0; search_char <= 0; code_value <= 0; next_code <= 256; code_width <= 9; state <= IDLE; valid_out <= 0; code_out <= 0; end else begin case (state) IDLE: begin if (valid_in) begin input_char <= data_in; state <= SEARCH; end end SEARCH: begin search_char <= prev_char; prev_char <= input_char; if (code_value == 0) begin code_value <= input_char; valid_out <= 1; code_out <= input_char; state <= ADD; end else if (dict[code_value] == 0) begin dict[code_value] <= next_code; next_code <= next_code + 1; valid_out <= 1; code_out <= code_value; state <= ADD; end else if (dict[code_value] == (code_value << 8) + input_char) begin code_value <= dict[code_value]; state <= SEARCH; end else begin code_value <= dict[code_value]; state <= UPDATE; end end ADD: begin if (next_code >= MAX_CODE_VALUE) begin code_width <= code_width + 1; end prev_char <= input_char; code_value <= prev_char; state <= SEARCH; end UPDATE: begin valid_out <= 1; code_out <= code_value; dict[code_value] <= next_code; next_code <= next_code + 1; code_value <= input_char; state <= ADD; end endcase end end endmodule ``` 这个Verilog代码实现了一个LZW压缩模块,可以压缩输入的数据流并输出压缩后的代码流。模块使用一个字典来存储已经出现的字符和对应的码值,每次读取一个字符并检查是否在字典中出现过,如果出现了则更新当前码值,否则将当前字符加入字典中并输出上一个码值。输出的代码流是固定长度的,可以根据需要调整最大码值和代码宽度。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值