数据压缩实验报告3 LZW编解码

数据压缩实验一实验报告 阅读详情

写在前面:本次实验由老师写好了LZW编码的代码,先理解了已有代码之后,自己再写解码代码时比较容易。下面给出完整的代码以及实验结果

bitio.h(全部由老师给出,自己写了一些注释)

/*
 * Declaration for bitwise IO
 *
 * vim: ts=4 sw=4 cindent
 */
#ifndef __BITIO__
#define __BITIO__

#include <stdio.h>

typedef struct{
	FILE *fp;//输出文件指针
	unsigned char mask;//按位写入字节时掩码
	int rack;//每写完8位,将rack输出到文件中
}BITFILE;

BITFILE *OpenBitFileInput( char *filename);
BITFILE *OpenBitFileOutput( char *filename);
void CloseBitFileInput( BITFILE *bf);
void CloseBitFileOutput( BITFILE *bf);
int BitInput( BITFILE *bf);
unsigned long BitsInput( BITFILE *bf, int count);
void BitOutput( BITFILE *bf, int bit);
void BitsOutput( BITFILE *bf, unsigned long code, int count);
#endif	// __BITIO__

bitio.cpp(全部由老师给出)

/*
 * Definitions for bitwise IO
 *
 * vim: ts=4 sw=4 cindent
 */
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include "bitio.h"
BITFILE *OpenBitFileInput( char *filename){
	BITFILE *bf;
	bf = (BITFILE *)malloc( sizeof(BITFILE));
	if( NULL == bf) return NULL;
	if( NULL == filename)	bf->fp = stdin;
	else bf->fp = fopen( filename, "rb");
	if( NULL == bf->fp) return NULL;
	bf->mask = 0x80;
	bf->rack = 0;
	return bf;
}

BITFILE *OpenBitFileOutput( char *filename){
	BITFILE *bf;
	bf = (BITFILE *)malloc( sizeof(BITFILE));
	if( NULL == bf) return NULL;
	if( NULL == filename)	bf->fp = stdout;
	else bf->fp = fopen( filename, "wb");
	if( NULL == bf->fp) return NULL;
	bf->mask = 0x80;
	bf->rack = 0;
	return bf;
}

void CloseBitFileInput( BITFILE *bf){
	fclose( bf->fp);
	free( bf);
}

void CloseBitFileOutput( BITFILE *bf){
	// Output the remaining bits
	if( 0x80 != bf->mask) fputc( bf->rack, bf->fp);
	fclose( bf->fp);
	free( bf);
}

int BitInput( BITFILE *bf){
	int value;

	if( 0x80 == bf->mask){
		bf->rack = fgetc( bf->fp);
		if( EOF == bf->rack){
			fprintf(stderr, "Read after the end of file reached\n");
			exit( -1);
		}
	}
	value = bf->mask & bf->rack;
	bf->mask >>= 1;
	if( 0==bf->mask) bf->mask = 0x80;
	return( (0==value)?0:1);
}

unsigned long BitsInput( BITFILE *bf, int count){
	unsigned long mask;
	unsigned long value;
	mask = 1L << (count-1);
	value = 0L;
	while( 0!=mask){
		if( 1 == BitInput( bf))
			value |= mask;
		mask >>= 1;
	}
	return value;
}

void BitOutput( BITFILE *bf, int bit){
	if( 0 != bit) bf->rack |= bf->mask;
	bf->mask >>= 1;
	if( 0 == bf->mask){	// eight bits in rack
		fputc( bf->rack, bf->fp);
		bf->rack = 0;
		bf->mask = 0x80;
	}
}

void BitsOutput( BITFILE *bf, unsigned long code, int count){
	unsigned long mask;

	mask = 1L << (count-1);
	while( 0 != mask){
		BitOutput( bf, (int)(0==(code&mask)?0:1));
		mask >>= 1;
	}
}
#if 0
int main( int argc, char **argv){
	BITFILE *bfi, *bfo;
	int bit;
	int count = 0;

	if( 1<argc){
		if( NULL==OpenBitFileInput( bfi, argv[1])){
			fprintf( stderr, "fail open the file\n");
			return -1;
		}
	}else{
		if( NULL==OpenBitFileInput( bfi, NULL)){
			fprintf( stderr, "fail open stdin\n");
			return -2;
		}
	}
	if( 2<argc){
		if( NULL==OpenBitFileOutput( bfo, argv[2])){
			fprintf( stderr, "fail open file for output\n");
			return -3;
		}
	}else{
		if( NULL==OpenBitFileOutput( bfo, NULL)){
			fprintf( stderr, "fail open stdout\n");
			return -4;
		}
	}
	while( 1){
		bit = BitInput( bfi);
		fprintf( stderr, "%d", bit);
		count ++;
		if( 0==(count&7))fprintf( stderr, " ");
		BitOutput( bfo, bit);
	}
	return 0;
}
#endif

lzw_E.cpp(解码部分由自己编写,其他部分由老师给出,自己写了一部分注释)

/*
 * Definition for LZW coding 
 *
 * vim: ts=4 sw=4 cindent nowrap
 */
#define _CRT_SECURE_NO_WARNINGS
#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;
}
void InitDictionary( void){
	int i;

	for( i=0; i<256; i++){
		dictionary[i].suffix = i;//根的后缀字符为对应ASCII码
		dictionary[i].parent = -1;//前缀字符长度为0(没有前缀)
		dictionary[i].firstchild = -1;//没有第一个孩子
		dictionary[i].nextsibling = i+1;//下一个兄弟根节点下标为下一个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;
	sibling = dictionary[string_code].firstchild;//以string_code的第一个孩子为兄弟
	while( -1<sibling){
		if( character == dictionary[sibling].suffix) return sibling; //如果找到一个兄弟的后缀是character,则返回(string_code,character)的编码
		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{
		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);//将输入文件的大小写入输出文件中,file_lengt为32位数字
	InitDictionary();//初始化字典,设置根节点
	string_code = -1;//初始化前缀
	while( EOF!=(character=fgetc( fp))){//扫描输入文件
		index = InDictionary( character, string_code);
		//判断(string_code,character)是否在字典中,如果在则返回对应编码,否则返回-1
		if( 0<=index){	// (string_code,character)在字典中
			string_code = index; //将(string_code,character)对应的编码作为前缀
		}else{	//(string_code,character)不在字典中
			output( bf, string_code);//输出前缀
			if( MAX_CODE > next_code){	//字典空间充足时将(string_code,character)添加到字典中
				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;
	InitDictionary();
	last_code = -1;
	while (file_length > 0)
	{
		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 (phrase_length > 0)
		{
			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;
}

测试代码:先写一个a.dat,将abbababac写入其中。经过LZW编码后生成b.dat,解码后生成c.dat,可见编码再解码后的文件与原文件一致。
在这里插入图片描述
选择10种不同格式的文件进行LZW编码并比较压缩效率:
在这里插入图片描述
除了bdf(Quartus II原理图文件)和vwf(Quartus II仿真波形文件)之外,其他文件在进行LZW编码后大小反而增加了。

LZW编解码详解 最近在看LZW编码和解码,正好看到一篇好文章,在此记录: 转自:https://segmentfault.com/a/1190000011425787 最近整理Github上以前胡乱写的代码,发现自己还写过压缩算法,大概是不知道什么时候用来练手的。里面我实现了哈夫曼树,LZW字典和算数编码三种压缩算法,时隔几年几乎没什么印象了,尤其是后两种连原理都基本忘了,所以把它们拎出来整理一下,也算是逼自... 阅读详情

相关推荐

【数据压缩】LZW编解码原理及算法实现

一、LZW简介 LZW压缩(LZW compression)是一种由Abraham Lempel、Jacob Ziv和Terry Welch发明的基于表查寻算法把文件压缩成小文件的无损压缩方法。 二、LZW编解码举例 1、LZW编码算法思想 举例:输入流 a b b a b a b a c 首先,以ASCII码为基础词典 已编码:a b 遇到a,用97表示,编码为97。 遇到b,用98表示,编码为98。 发现ab,加入词典,ab索引为256。 已编码:a b b a b 遇到b,编码为98,发

spadeK2020的博客 1万+

《数据压缩》实验报告一·YUV2RGB实验

yuv转rgb格式实验报告

ininw的博客 486

【数据压缩实验】LZW编解码算法实现与分析

LZW编解码算法实现

m0_52082331的博客 651

数据压缩实验三——LZW编解码

数据压缩实验三——LZW编解码 一、 实验目的 掌握词典编码的基本原理,用C/C++/Python等语言编程实现LZW解码器并分析编解码算法。 二、 实验内容 1.LZW编码原理和实现算法 LZW的编码思想是不断地从字符流中提取新的字符串,通俗地理解为新“词条”,然后用“代号”也就是码字表示这个“词条”。这样一来,对字符流的编码就变成了用码字去替换字符流,生成码字流,从而达到压缩数据的目的。LZW编码是围绕称为词典的转换表 来完成的。LZW编码器通过管理这个词典完成输入与输出之间的转换。LZW编码器的输入是

weixin_50971817的博客 366

数据压缩实验三|LZW 编解码算法实现与分析

实验三:LZW 编解码算法实现与分析 一、实验课程编码:103050 二、实验课程名称:数据压缩原理与应用 A 三、实验项目名称:LZW 编解码算法实现与分析 四、实验目的 掌握词典编码的基本原理,用C/C++/Python等语言编程实现LZW解码器并分析编解码算 法。 五、主要设备 安装 Windows 和 Visual Studio 等编程平台的个人计算机 六、实验内容 1.LZW编码原理和实现算法 LZW的编码思想是不断地从字符流中提取新的字符串,通俗地理解为新“词条”,然后用“代号”也就是码字表示这

m0_55764256的博客 255

【数据压缩(五)】LZW编解码c语言实现和分析

c语言实现LZW

qq_60187577的博客 1469

实验三 LZW编解码实验

LZW编解码实验

weixin_47590942的博客 971

哈夫曼实现文件压缩的实验报告

报告中不仅有完整的哈夫曼树实现文件的压缩和解压缩功能,还有算法的结构流程图和主函数的简单结构图,程序中有中文注释,可读性比较强,文末还附有本人的心得体会

LZW编解码算法原理及分析

第二类词典编码——LZW编解码算法原理及分析

Haolin_Liu的博客 5562

数据压缩实验报告

数据压缩实验报告 内容完整 实验目的 实验步骤 实验结果

信息论编码报告含哈夫曼及LZW

信息论编码报告包含哈夫曼与lzw的实验程序c++的完整版的实验报告,适合学生下载。

【数据压缩(六)】基于C++实现LZW编解码算法

基于C++实现LZW编解码算法一、实验目的二、实验要求三、实验原理1、LZW编码2、LZW解码三、实验代码1、`bitio.h`头文件2、`main`函数3、`OpenBitFileInput`函数和`OpenBitFileOutput`函数4、LZW编码部分5、LZW解码部分6、`CloseBitFileOutput`函数 一、实验目的 掌握词典编码的基本原理。 在已有LZW编码器的基础上用C++语言编程实现LZW解码器。 分析编解码算法。 二、实验要求 首先调试LZW的编码程序,以一个文本文件作为

zyzcuczyu的博客 1493

LZW编解码实验报告(含程序和注释)

信息论与编码的实验题目,注释详细,程序可以运行,结果正确。

[数据压缩作业5]LZW编解码

LZW编解码

m0_59620643的博客 864

信息论课程设计——LZW编码

信息论课程设计,LZW编码方案,C++实现LZW编码。使用txt文本保存和输入输出。

《用哈夫曼编码实现文件压缩》实验报告

《用哈夫曼编码实现文件压缩》实验报告 只是一个报告~~~

“数据压缩实验之LZW 编解码算法实现与分析”实验报告_201810413045_陈诚

还是更好的风格的话

qq_45639597的博客 1474

动手学Moveit2 | 运行配置好的机械臂功能demo

通过这个小demo你可以直观的感受moveit2的强大,但还有很多需要我们探索的,比如机械臂是如何规划的,规划的过程中如何结合环境信息,规划的时候机械臂如何求解,如何和真实的机械臂对接等等,这些都需要你来进一步探索,今天就到这里,明天给大家秀一下小仙女焊的fishbot运控板。

生活不止眼前的苟且 8880

【数据压缩】C++实现LZW算法编解码

一、实验目的 掌握词典编码的基本原理,用C/C++/Python等语言编程实现LZW解码器并分析编解码算法。 选择十种不同格式类型的文件,使用LZW编码器进行压缩得到输出的压缩比特流文件。对各种不同格式的文件进行压缩效率的分析。 二、实验内容 (1)实验思路 1.LZW编码原理和实现算法 LZW的编码思想是不断地从字符流中提取新的字符串,通俗地理解为新“词条”,然后用“代号”也就是码字表示这个“词条”。这样一来,对字符流的编码就变成了用码字去替换字符流,生成码字流,从而达到压缩数据的目的。 LZW编码是

hdkks的博客 2231

基于POI-TL实现动态Word模板数据填充(含图表):从需求到落地的完整开发实践

本文聚焦企业级报告生成场景中 “在线编辑模板 + 动态数据填充” 的核心需求,详细阐述基于 POI-TL、OnlyOffice 与 MinIO 的全栈实现方案。通过代码示例拆解模板加载、文字填充、图表匹配渲染(含通过关系 ID 关联图表与段落的关键逻辑)、报告存储全步骤。最终通过技术组合实现业务人员可视化编辑模板、后端高效填充数据的目标,为同类动态 Word 生成需求提供可复用的实践参考。

imHanweihu的博客 1866

PSIM+PIL+CCS三剑客:手把手教你搞定DSP28335的软硬件联合调试

本文详细介绍了如何利用PSIM、PIL与CCS三款工具,实现DSP28335的软硬件联合调试。通过搭建兼容环境、从PSIM仿真生成PIL代码,再到实战联合调试与问题诊断,手把手指导开发者构建从虚拟模型到实际硬件的完整验证闭环,有效提升电力电子与电机驱动等领域的开发效率与可靠性。

weixin_29183637的博客 186
上一篇: 数据压缩实验报告2 TGA转YUV
下一篇: 数据压缩实验报告4 DPCM编码
タン
博客等级 码龄8年 15粉丝 14原创
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值