LZW压缩算法 多媒体技术与应用.docx
- 文档编号:29911649
- 上传时间:2023-08-03
- 格式:DOCX
- 页数:13
- 大小:48.35KB
LZW压缩算法 多媒体技术与应用.docx
《LZW压缩算法 多媒体技术与应用.docx》由会员分享,可在线阅读,更多相关《LZW压缩算法 多媒体技术与应用.docx(13页珍藏版)》请在冰豆网上搜索。
LZW压缩算法多媒体技术与应用
多媒体技术与应用
--LZW压缩算法
学校:
江苏大学京江学院
班级:
J计算机1001
学号:
3101110001
姓名:
阎梦虹
辅导教师:
金华
一、LZW算法简介
字符串和编码的对应关系是在压缩过程中动态生成的,并且隐含在压缩数据中,解压的时候根据表来进行恢复,算是一种无损压缩.
根据Lempel-Ziv-WelchEncoding,简称LZW的压缩算法,用任何一种语言来实现它.
LZW压缩算法的基本概念:
LZW压缩有三个重要的对象:
数据流(CharStream)、编码流(CodeStream)和编译表(StringTable)。
在编码时,数据流是输入对象(文本文件的据序列),编码流就是输出对象(经过压缩运算的编码数据);在解码时,编码流则是输入对象,数据流是输出对象;而编译表是在编码和解码时都须要用借助的对象。
字符(Character):
最基础的数据元素,在文本文件中就是一个字节,在光栅数据中就是一个像素的颜色在指定的颜色列表中的索引值;字符串(String):
由几个连续的字符组成;前缀(Prefix):
也是一个字符串,不过通常用在另一个字符的前面,而且它的长度可以为0;根(Root):
一个长度的字符串;编码(Code):
一个数字,按照固定长度(编码长度)从编码流中取出,编译表的映射值;图案:
一个字符串,按不定长度从数据流中读出,映射到编译表条目.
LZW压缩算法的基本原理:
提取原始文本文件数据中的不同字符,基于这些字符创建一个编译表,然后用编译表中的字符的索引来替代原始文本文件数据中的相应字符,减少原始数据大小。
看起来和调色板图象的实现原理差不多,但是应该注意到的是,我们这里的编译表不是事先创建好的,而是根据原始文件数据动态创建的,解码时还要从已编码的数据中还原出原来的编译表.
二、LZW算法背景:
A.Lempel和J.Ziv在1978年首次发表了介绍字典编码方法的文章将其称之为LZ78。
在他们的研究基础上,TerryA.Welch在1984年发表了对这种编码算法进行了改进的文章,并首先在高速硬盘控制器上应用了这种算法。
因此后来把这种编码方法称为LZW(Lempel—ZivWalch)压缩编码。
近年来越来越多的研究人员注意到图像压缩的重要性,主要原因在于图像文件不仅占据内存空间,而且也占据大量的传输带宽,因此存储和传输前对图像进
行压缩就成为必然。
信息数字化的高速进行更是给数据压缩带来严峻的挑战,在不同的领域,出现了各种各样的压缩方法,例如基于熵的统计编码(Huffman编码、Shannon编码、算术编码等),基于字典的ARJ,LZH,PKZIP等压缩方法,分形编码、小波变换等变换编码,分块编码,以及JPEG,MPEG等。
LZW压缩算法是一种无损压缩算法,它主要是用一种代码来表示数据流中出现的重复串,以达到压缩的目的,并且LZW算法可以大大缩小对通讯带宽的需求,具有较高的实用价值。
LZW算法不仅可以用于文字数据的压缩,而且也可以成功地用于某些图像数据的压缩处理,如GIF和TIFF等图像格式,本文正是基于这种LZW编码的。
大量数据的图象信息会给存储器的存储容量,通信干线信道的宽度,以及计算机的处理速度增加极大的压力,会使得WorldWideWeb变成WorldWideWait。
单纯靠增加存储器容量,提高信道带宽以及计算机处理速度等方法来解决这个问题是不现实的。
很显然,在信道带宽、通信链路一定的前提下,采用编码压缩技术,减少传输数据量,是提高通信速度的重要手段。
没有图象编码压缩技术的发展,大容量图象信息的存储与传输是难以实现的,多媒体、信息高速公路等新技术在实际中的应用也会碰到很大的困难。
LZW压缩算法是一种常用的数据压缩算法,由于它不依赖于任何数据格式,而且具有很高的综合性能指标,因而得到广泛应用。
三、LZW算法流程:
步骤1:
开始时的词典包含所有可能的根(Root),而当前前缀P是空的;
步骤2:
当前字符(C):
=字符流中的下一个字符;
步骤3:
判断缀-符串P+C是否在词典中
(1)如果“是”:
P:
=P+C//(用C扩展P);
(2)如果“否”
①把代表当前前缀P的码字输出到码字流;
②把缀-符串P+C添加到词典;
③令P:
=C//(现在的P仅包含一个字符C);
步骤4:
判断码字流中是否还有码字要译
(1)如果“是”,就返回到步骤2;
(2)如果“否”
①把代表当前前缀P的码字输出到码字流;
②结束。
四、 LZW压缩的特点
LZW码能有效利用字符出现频率冗余度进行压缩,且字典是自适应生成的,但通常不能有效地利用位置冗余度。
具体特点如下:
l)LZW压缩技术对于可预测性不大的数据具有较好的处理效果,常用于TIF格式的图像压缩,其平均压缩比在2:
1以上,最高压缩比可达到3:
1。
2)对于数据流中连续重复出现的字节和字串,LZW压缩技术具有很高的压缩比。
3)除了用于图像数据处理以外,LZW压缩技术还被用于文本程序等数据压缩领域。
4)LZW压缩技术有很多变体,例如常见的ARC、RKARC、PKZIP高效压缩程序。
5)对于任意宽度和像素位长度的图像,都具有稳定的压缩过程。
压缩和解压缩速度较快。
6)对机器硬件条件要求不高,在Intel80386的计算机上即可进行压缩和解压缩。
代码:
#include
#include
#include
#defineBITS12
#defineHASHING_SHIFT(BITS-8)
#defineMAX_VALUE(1< #defineMAX_CODEMAX_VALUE-1 #ifBITS==14 #defineTABLE_SIZE18041 #endif #ifBITS==13 #defineTABLE_SIZE9029 #endif #ifBITS<=12 #defineTABLE_SIZE5021 #endif void*malloc(); int*code_value; unsignedint*prefix_code; unsignedchar*append_character; unsignedchardecode_stack[4000]; voidcompress(FILE*input,FILE*output); voidexpand(FILE*input,FILE*output); intfind_match(inthash_prefix,unsignedinthash_character); voidoutput_code(FILE*output,unsignedintcode); unsignedintinput_code(FILE*input); unsignedchar*decode_string(unsignedchar*buffer,unsignedintcode); main(intargc,char*argv[]) { FILE*input_file; FILE*output_file; FILE*lzw_file; charinput_file_name[81]; code_value=(int*)malloc(TABLE_SIZE*sizeof(int)); prefix_code=(unsignedint*)malloc(TABLE_SIZE*sizeof(unsignedint)); append_character=(unsignedchar*)malloc(TABLE_SIZE*sizeof(unsignedchar)); if(code_value==NULL||prefix_code==NULL||append_character==NULL) { printf("Fatalerrorallocatingtablespace! "); exit(-1); } if(argc>1) strcpy(input_file_name,argv[1]); else { printf("Inputfilename? "); scanf("%s",input_file_name); } input_file=fopen(input_file_name,"rb"); lzw_file=fopen("test.lzw","wb"); if(input_file==NULL||lzw_file==NULL) { printf("Fatalerroropeningfiles."); exit(-1); }; compress(input_file,lzw_file); fclose(input_file); fclose(lzw_file); free(code_value); lzw_file=fopen("test.lzw","rb"); output_file=fopen("test.out","wb"); if(lzw_file==NULL||output_file==NULL) { printf("Fatalerroropeningfiles."); exit(-2); }; expand(lzw_file,output_file); fclose(lzw_file); fclose(output_file); free(prefix_code); free(append_character); } voidcompress(FILE*input,FILE*output) { unsignedintnext_code; unsignedintcharacter; unsignedintstring_code; unsignedintindex; inti; next_code=256; for(i=0;i code_value[i]=-1; i=0; printf("Compressing..."); string_code=getc(input); while((character=getc(input))! =(unsigned)EOF) { if(++i==1000) { i=0; printf("*"); } index=find_match(string_code,character); if(code_value[index]! =-1) string_code=code_value[index]; else { if(next_code<=MAX_CODE) { code_value[index]=next_code++; prefix_code[index]=string_code; append_character[index]=character; } output_code(output,string_code); string_code=character; } } output_code(output,string_code); output_code(output,MAX_VALUE); output_code(output,0); printf(""); } intfind_match(inthash_prefix,unsignedinthash_character) { intindex; intoffset; index=(hash_character< if(index==0) offset=1; else offset=TABLE_SIZE-index; while (1) { if(code_value[index]==-1) return(index); if(prefix_code[index]==hash_prefix&& append_character[index]==hash_character) return(index); index-=offset; if(index<0) index+=TABLE_SIZE; } } voidexpand(FILE*input,FILE*output) { unsignedintnext_code; unsignedintnew_code; unsignedintold_code; intcharacter; intcounter; unsignedchar*string; next_code=256; counter=0; printf("Expanding..."); old_code=input_code(input); character=old_code; putc(old_code,output); while((new_code=input_code(input))! =(MAX_VALUE)) { if(++counter==1000) { counter=0; printf("*"); } if(new_code>=next_code) { *decode_stack=character; string=decode_string(decode_stack+1,old_code); } character=*string; while(string>=decode_stack) putc(*string--,output); if(next_code<=MAX_CODE) { prefix_code[next_code]=old_code; append_character[next_code]=character; next_code++; } old_code=new_code; } printf(""); } unsignedchar*decode_string(unsignedchar*buffer,unsignedintcode) { inti; i=0; while(code>255) { *buffer++=append_character[code]; code=prefix_code[code]; if(i++>=MAX_CODE) { printf("Fatalerrorduringcodeexpansion."); exit(-3); } } *buffer=code; return(buffer); } unsignedintinput_code(FILE*input) { unsignedintreturn_value; staticintinput_bit_count=0; staticunsignedlonginput_bit_buffer=0L; while(input_bit_count<=24) { input_bit_buffer|= (unsignedlong)getc(input)<<(24-input_bit_count); input_bit_count+=8; } return_value=input_bit_buffer>>(32-BITS); input_bit_buffer<<=BITS; input_bit_count-=BITS; return(return_value); } voidoutput_code(FILE*output,unsignedintcode) { staticintoutput_bit_count=0; staticunsignedlongoutput_bit_buffer=0L; output_bit_buffer|=(unsignedlong)code<<(32-BITS-output_bit_count); output_bit_count+=BITS; while(output_bit_count>=8) { putc(output_bit_buffer>>24,output); output_bit_buffer<<=8; output_bit_count-=8; } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- LZW压缩算法 多媒体技术与应用 LZW 压缩 算法 多媒体技术 应用