目录
(一)实验名称
LZW 编解码算法实现与分析
(二)实验目的
掌握词典编码的基本原理,用C/C++/Python等语言编程实现LZW解码器并分析编解码算 法。
(三)主要设备
安装 Windows 和 Visual Studio 等编程平台的个人计算机
(四)实验内容
一、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编码流程图如下:
2. LZW编码代码分析
(1)数据结构
树用数组dict[ ]表示,数组下标用pointer 表示,所以dict[pointer]表示一个节点
dict[pointer].suffix
dict[pointer].parent
dict[pointer].firstchild
dict[pointer].nextsibling
struct {
int suffix;
int parent, firstchild, nextsibling;
} dictionary[MAX_CODE+1];
(2)初始化词典
将词典初始化为包含所有可能的单字符,当前前缀P初始化为空。
void InitDictionary( void){//初始化词典,将0-255根节点初始化
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;//新词条编码为256
}
(3)查找词典中是否有字符串
/*
* 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;//string_code==-1,当前词条无前缀,为单个字符,初始化后已经在词典中,返回此字符
sibling = dictionary[string_code].firstchild;//如果不是单个字符,找当前前缀的第一个孩子节点
while( -1<sibling){
if( character == dictionary[sibling].suffix) return sibling;//如果此孩子节点的尾缀字符等于character,则当前词条在词典中,返回此孩子节点
sibling = dictionary[sibling].nextsibling;//否则,找当前前缀的下一个孩子节点
}
return -1;//没有找到,返回-1
}
(4)将新串加入词典
void AddToDictionary( int character, int string_code){//将新串加入词典
int firstsibling, nextsibling;
if( 0>string_code) return;//string_code==-1,当前词条无前缀,为单个字符,初始化后已经在词典中
dictionary[next_code].suffix = character;//添加新节点dictionary[next_code],其尾缀字符为character
dictionary[next_code].parent = string_code;//新节点的母节点为string_code
dictionary[next_code].nextsibling = -1;//新节点还没有下一个兄弟
dictionary[next_code].firstchild = -1;//新节点还没有孩子
firstsibling = dictionary[string_code].firstchild;//firstsibling为当前前缀的第一个孩子
if( -1<firstsibling){ // 如果当前前缀有孩子
nextsibling = firstsibling;
while( -1<dictionary[nextsibling].nextsibling ) //只要nextsibling还有下一个兄弟
nextsibling = dictionary[nextsibling].nextsibling;//让nextsibling替换为它的下一个兄弟,这样可以找到当前前缀的最后一个孩子
dictionary[nextsibling].nextsibling = next_code;//当前前缀的最后一个孩子的下一个兄弟为新节点
}else{// 当前前缀无孩子,则新节点为它的第一个孩子
dictionary[string_code].firstchild = next_code;
}
next_code ++;//为下一个新词条所用
}
(5)LZW编码
LZW编码算法可用下述函数实现。首先初始化词典,然后顺序从待压缩文件中读入字符并按照上述算法执行编码。最后将编得的码字流输出至文件中。
void LZWEncode( FILE *fp, BITFILE *bf){//LZW编码
int character;//当前字符C
int string_code;//当前前缀P
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))){//EOF:END OF FILE,每次从文件中读取一个字符,直到文件的最后一个字符
index = InDictionary( character, string_code);//查找当前字符串是否在词典中