一 算法分析及程序流程
1 数据结构
字典节点结构体
struct {
int suffix; //后缀字符
int parent, firstchild, nextsibling; //母节点,第一个孩子节点,下一个兄弟节点
} dictionary[MAX_CODE+1]; //数组下标即为编码
二进制文件结构体
typedef struct{
FILE *fp; //文件指针
unsigned char mask; //掩码
int rack;
}BITFILE;
2 算法流程图
3 代码分析
编码 ENCODING
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;//初始化前缀,p=-1
while( EOF!=(character=fgetc( fp))){//依次读入文件字符
//判断string+character是否在字典中,并返回相应编码(数组下标)
index = InDictionary( character, string_code);
if( 0<=index){ // string+character在字典中
string_code = index;//编码作为新前缀
}else{ // string+character不在字典中
output( bf, string_code);//输出前缀
if( MAX_CODE > next_code){ //字典空间充足
//将string+character添加至字典中
AddToDictionary( character, string_code);
}
string_code = character;//新字符作为新前缀
}
}
output( bf, string_code);//文件扫描完毕,输出最后的前缀
}
解码 DECODING
void LZWDecode( BITFILE *bf, FILE *fp){
/*当新读取的编码不存在于字典中时,character为旧编码last_code的首字母。当新读取的编码字典中存在时,character为新编码new_code的首字母*/
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(); //初始化字典,设置0-256根节点
last_code = -1; //开始时旧编码处空缺,故设置last_code为-1
while (0 < file_length) { //是否读取到最后一个编码
new_code = input(bf); //以16位读取新编码
if (new_code >= next_code) { //字典中没有新编码
d_stack[0] = character; /*character为旧编码last_code的首字母,将其放置在栈数组顶部d_stack[0]*/
/*遍历旧编码last_code所在树,将last_code对应字符串放置于d_stack栈数组中。前缀放于栈底方向,后缀位于d_stack[1]。得到字符总数,即要输出字符串的长度*/
phrase_length = DecodeString(1, last_code);
}
else { //字典中有新编码
/*遍历新编码new_code所在树,将new_code对应字符串放置于d_stack栈数组中。前缀放于栈底方向,后缀位于栈顶d_stack[0]。得到字符总数,即要输出字符串的长度*/
phrase_length = DecodeString(0, new_code);
}
/*当新读取的编码不存在于字典中时,character为旧编码last_code的首字母。当新读取的编码字典中存在时,character为新编码new_code的首字母*/
character = d_stack[phrase_length - 1];
/*当新读取的编码不存在于字典中时,输出(旧编码last_code对应字符串,last_code对应首字母)。当新读取的编码存在于字典中时,输出新编码new_code对应字符串*/
while (0 < phrase_length) {
phrase_length--;
fputc(d_stack[phrase_length], fp);
file_length--;
}
/*当新读取的编码不存在于字典中时,将(last_code对应字符串,last_code对应首字母)}添加到字典中。当新读取的编码存在于字典中时,将(last_code对应字符串,new_code对应首字母)添加到字典中。*/
if (MAX_CODE > next_code) {
AddToDictionary(character, last_code);
}
//新编码变为旧编码
last_code = new_code;
}
}
字典初始化
void InitDictionary( void){ //初始化字典,将0-255根节点初始化
int i;
for( i=0; i<256; i++){ //下标值为ASCII码值
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; //为全局变量,标记下一个编码为256
}
字典内查找
int InDictionary( int character, int string_code){
int sibling;
/*string_code小于0说明该字符是文件第一个字符,一定有对应根节点并存在于字典中,所以返回(string_code,character)的编码---character的ASCII码*/
if( 0>string_code) return character;
//自左向右遍历string_code节点的所有孩子(第一个孩子的所有兄弟)
//以string_code的第一个孩子为长兄
sibling = dictionary[string_code].firstchild;
while( -1<sibling){ //sibling等于-1说明所有兄弟遍历结束
/*如果找到一个兄弟的后缀是character,则返回(string_code,character)的编码¬---该兄弟的下标*/
if( character == dictionary[sibling].suffix) return sibling;
//如果该兄弟的后缀不是该字符,则寻找下一个兄弟
sibling = dictionary[sibling].nextsibling;
}
/*所有兄弟的后缀均不等于该字符,说明(string_code,character)不在字典中,返回-1*/
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; //新节点下一个兄弟暂时不存在,设为-1
dictionary[next_code].firstchild = -1; //新节点第一个孩子暂时不存在,设为-1
firstsibling = dictionary[string_code].firstchild;
//下面设置新节点的兄弟关系
if( -1<firstsibling){ //新节点的母亲原本有孩子
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 ++; //下一个编码增加1
}
二 解码时码字不存在的解决方案
当解码时遇到字典中不存在的编码时,栈底d_stack[0]为last_code前缀,遍历last_code所在树将last_code对应字符串放置于d_stack栈数组中,即位于栈顶d_stack[1]。栈中字符倒序存放(例如hello在栈中存放顺序即为olleh),其输出结果为last_code+last_code前缀。
例如字符流 97 98 98 256 259 99 (下表last_code=pw, new_code=cw)
步骤序号 | 编码 | 是否在字典中 | pw | cw | 输出字符 | 存入字典 | 返回的字符串长度 |
1 | 97 | 在字典中 | -1(无) | a | cw=a | / | 1 |
2 | 98 | 在字典中 | a | b | cw=b | pw+cw前缀=ab (256) | 1 |
3 | 98 | 在字典中 | ab | b | cw=b | pw+cw前缀=bb (257) | 1 |
4 | 256 | 在字典中 | b | ab | cw=ab | pw+cw前缀=ba (258) | 1 |
5 | 259 | 不在字典中 | ab | / | pw+pw前缀=aba | pw+pw前缀=aba (259) | 2 |
6 | 99 | 在字典中 | aba | c | cw=c | pw+cw前缀=abac (260) | 3 |
输出结果:abbababac
三 文件压缩及效率分析
文件类型 | 压缩前大小/kb | 压缩后大小/kb | 压缩效率(lzw) | 使用zip压缩 |
207 | 266 | -28.5% | 183 | |
txt | 1300 | 195 | 85.0% | 13 |
png | 22 | 35 | -59.1% | 21 |
bmp | 733 | 958 | -30.7% | 299 |
avi | 1162 | 1388 | -19.4% | 1124 |
docx | 15 | 24 | -60.0% | 13 |
xlsx | 24 | 37 | -51.2% | 21 |
html | 112 | 43 | 61.6% | 18 |
pptx | 410 | 500 | -22.0% | 378 |
jpg | 247 | 315 | -27.5% | 246 |
由此可知,LZW压缩算法对文本类压缩效果较好,对图片文档视频类基本不能压缩。
四 完整代码
lzw_E.c
/*
* Definition for LZW coding
*
* vim: ts=4 sw=4 cindent nowrap
*/
#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;
dictionary[i].parent = -1;
dictionary[i].firstchild = -1;
dictionary[i].nextsibling = i+1;
}
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;
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;
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 ++;
}
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;//初始化前缀,p=-1
while( EOF!=(character=fgetc( fp))){//依次读入文件字符
//判断string+character是否在字典中,并返回相应编码(数组下标)
index = InDictionary( character, string_code);
if( 0<=index){ // string+character在字典中
string_code = index;//编码作为新前缀
}else{ // string+character不在字典中
output( bf, string_code);//输出前缀
if( MAX_CODE > next_code){ //字典空间充足
//将string+character添加至字典中
AddToDictionary( character, string_code);
}
string_code = character;//新字符作为新前缀
}
}
output( bf, string_code);//文件扫描完毕,输出最后的前缀
}
void LZWDecode( BITFILE *bf, FILE *fp){
/*当新读取的编码不存在于字典中时,character为旧编码last_code的首字母。当新读取的编码字典中存在时,character为新编码new_code的首字母*/
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(); //初始化字典,设置0-256根节点
last_code = -1; //开始时旧编码处空缺,故设置last_code为-1
while (0 < file_length) { //是否读取到最后一个编码
new_code = input(bf); //以16位读取新编码
if (new_code >= next_code) { //字典中没有新编码
d_stack[0] = character; /*character为旧编码last_code的首字母,将其放置在栈数组顶部d_stack[0]*/
/*遍历旧编码last_code所在树,将last_code对应字符串放置于d_stack栈数组中。前缀放于栈底方向,后缀位于d_stack[1]。得到字符总数,即要输出字符串的长度*/
phrase_length = DecodeString(1, last_code);
}
else { //字典中有新编码
/*遍历新编码new_code所在树,将new_code对应字符串放置于d_stack栈数组中。前缀放于栈底方向,后缀位于栈顶d_stack[0]。得到字符总数,即要输出字符串的长度*/
phrase_length = DecodeString(0, new_code);
}
/*当新读取的编码不存在于字典中时,character为旧编码last_code的首字母。当新读取的编码字典中存在时,character为新编码new_code的首字母*/
character = d_stack[phrase_length - 1];
/*当新读取的编码不存在于字典中时,输出(旧编码last_code对应字符串,last_code对应首字母)。当新读取的编码存在于字典中时,输出新编码new_code对应字符串*/
while (0 < phrase_length) {
phrase_length--;
fputc(d_stack[phrase_length], fp);
file_length--;
}
/*当新读取的编码不存在于字典中时,将(last_code对应字符串,last_code对应首字母)}添加到字典中。当新读取的编码存在于字典中时,将(last_code对应字符串,new_code对应首字母)添加到字典中。*/
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;
}
bitio.c
/*
* Definitions for bitwise IO
*
* vim: ts=4 sw=4 cindent
*/
#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
五 REFERANCES
感谢指导