求 LZ78 & LZW 压缩算法源码

sexisnothing 2008-02-22 05:08:48
跪求!
标准c调试通过的。谢谢。
...全文
606 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianzhuxiong 2011-10-06
  • 打赏
  • 举报
回复
回看一看答案。
sexisnothing 2008-02-28
  • 打赏
  • 举报
回复
同志,你贴的代码有点问题啊。我在linux下gcc没通过,暂时找不出原因。
laolaoliu2002 2008-02-27
  • 打赏
  • 举报
回复

/*
** This routine simply decodes a string from the string table, storing
** it in a buffer. The buffer can then be output in reverse order by
** the expansion program.
*/

char *decode_string(unsigned char *buffer,unsigned int code)
{
int i;

i=0;
while (code > 255)
{
*buffer++ = append_character[code];
code=prefix_code[code];
if (i++>=4094)
{
printf("Fatal error during code expansion.\n");
exit(0);
}
}
*buffer=code;
return(buffer);
}

/*
** The following two routines are used to output variable length
** codes. They are written strictly for clarity, and are not
** particularyl efficient.
*/

unsigned int input_code(FILE *input)
{
unsigned int return_value;
static int input_bit_count=0;
static unsigned long input_bit_buffer=0L;

while (input_bit_count <= 24)
{
input_bit_buffer |=
(unsigned long) 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);
}

void output_code(FILE *output,unsigned int code)
{
static int output_bit_count=0;
static unsigned long output_bit_buffer=0L;

output_bit_buffer |= (unsigned long) 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;
}
}
laolaoliu2002 2008-02-27
  • 打赏
  • 举报
回复

[code=C/C++]
/***********************************************************************************************************
LZW.c

本演示程序提供了LZW编码法的压缩和解压缩函数,并实现了对图象
文件的压缩和解压缩
**********************************************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define BITS 12 /* Setting the number of bits to 12, 13*/
#define HASHING_SHIFT BITS-8 /* or 14 affects several constants. */
#define MAX_VALUE (1 << BITS) - 1 /* Note that MS-DOS machines need to */
#define MAX_CODE MAX_VALUE - 1 /* compile their code in large model if*/
/* 14 bits are selected. */
#if BITS == 14
#define TABLE_SIZE 18041 /* The string table size needs to be a */
#endif /* prime number that is somewhat larger*/
#if BITS == 13 /* than 2**BITS. */
#define TABLE_SIZE 9029
#endif
#if BITS <= 12
#define TABLE_SIZE 5021
#endif

/* 函数原型 */
int LZW_Compression(char *in_filename, char *out_filename);
int LZW_Decompression(char *in_filename, char *out_filename);

/* 内部函数 */
int find_match(int hash_prefix,unsigned int hash_character);
char *decode_string(unsigned char *buffer,unsigned int code);
unsigned int input_code(FILE *input);
void output_code(FILE *output,unsigned int code);

/* 全局变量,编码/解码使用的内存缓冲区 */
int *code_value; /* This is the code value array */
unsigned int *prefix_code; /* This array holds the prefix codes */
unsigned char *append_character; /* This array holds the appended chars */
unsigned char decode_stack[4000]; /* This array holds the decoded string */


/* 主程序 */
void main(int argc, char *argv[])
{
printf("LZW compression and decompression utility\n");

if (4 != argc)
{
printf("\nUsage : lzw -c|d sourcefilename targetfilename\n");
exit(0);
}

if (! strcmp(argv[1], "-c"))
LZW_Compression(argv[2], argv[3]);
else if (! strcmp(argv[1], "-d"))
LZW_Decompression(argv[2], argv[3]);
else
printf("\nUnknow command.\n");
}

/**************************************************************************************************
LZW_Compression()

本函数用LZW算法压缩指定的文件,并将结构存储到新的文件中
***************************************************************************************************/
int LZW_Compression(char *in_filename, char *out_filename)
{
unsigned int next_code;
unsigned int character;
unsigned int string_code;
unsigned int index;
int i;
FILE *input;
FILE *output;

/* allocate memory for compression */
code_value=malloc(TABLE_SIZE*sizeof(unsigned int));
prefix_code=malloc(TABLE_SIZE*sizeof(unsigned int));
append_character=malloc(TABLE_SIZE*sizeof(unsigned char));
if (code_value==NULL || prefix_code==NULL || append_character==NULL)
{
printf("Fatal error allocating table space!\n");
return 0;
}

/* open files */
input=fopen(in_filename,"rb");
output=fopen(out_filename,"wb");
if (input==NULL || output==NULL)
{
printf("Fatal error opening files.\n");
return 0;
};

/* compressing... */

next_code=256; /* Next code is the next available string code*/
for (i=0;i<TABLE_SIZE;i++) /* Clear out the string table before starting */
code_value[i]=-1;

i=0;
printf("Compressing...\n");
string_code=getc(input); /* Get the first code */
/*
** This is the main loop where it all happens. This loop runs util all of
** the input has been exhausted. Note that it stops adding codes to the
** table after all of the possible codes have been defined.
*/
while ((character=getc(input)) != (unsigned)EOF)
{
if (++i==1000) /* Print a * every 1000 */
{ /* input characters. This */
i=0; /* is just a pacifier. */
printf(".");
}
index=find_match(string_code,character);/* See if the string is in */
if (code_value[index] != -1) /* the table. If it is, */
string_code=code_value[index]; /* get the code value. If */
else /* the string is not in the*/
{ /* table, try to add it. */
if (next_code <= MAX_CODE)
{
code_value[index]=next_code++;
prefix_code[index]=string_code;
append_character[index]=character;
}
output_code(output,string_code); /* When a string is found */
string_code=character; /* that is not in the table*/
} /* I output the last string*/
} /* after adding the new one*/
/*
** End of the main loop.
*/
output_code(output,string_code); /* Output the last code */
output_code(output,MAX_VALUE); /* Output the end of buffer code */
output_code(output,0); /* This code flushes the output buffer*/
printf("\n");

/* cleanup... */
fclose(input);
fclose(output);

free(code_value);
free(prefix_code);
free(append_character);

return 1;
}

[/code]
laolaoliu2002 2008-02-27
  • 打赏
  • 举报
回复

/***********************************************************************************************************
LZW.c

本演示程序提供了LZW编码法的压缩和解压缩函数,并实现了对图象
文件的压缩和解压缩
**********************************************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define BITS 12 /* Setting the number of bits to 12, 13*/
#define HASHING_SHIFT BITS-8 /* or 14 affects several constants. */
#define MAX_VALUE (1 << BITS) - 1 /* Note that MS-DOS machines need to */
#define MAX_CODE MAX_VALUE - 1 /* compile their code in large model if*/
/* 14 bits are selected. */
#if BITS == 14
#define TABLE_SIZE 18041 /* The string table size needs to be a */
#endif /* prime number that is somewhat larger*/
#if BITS == 13 /* than 2**BITS. */
#define TABLE_SIZE 9029
#endif
#if BITS <= 12
#define TABLE_SIZE 5021
#endif

/* 函数原型 */
int LZW_Compression(char *in_filename, char *out_filename);
int LZW_Decompression(char *in_filename, char *out_filename);

/* 内部函数 */
int find_match(int hash_prefix,unsigned int hash_character);
char *decode_string(unsigned char *buffer,unsigned int code);
unsigned int input_code(FILE *input);
void output_code(FILE *output,unsigned int code);

/* 全局变量,编码/解码使用的内存缓冲区 */
int *code_value; /* This is the code value array */
unsigned int *prefix_code; /* This array holds the prefix codes */
unsigned char *append_character; /* This array holds the appended chars */
unsigned char decode_stack[4000]; /* This array holds the decoded string */


/* 主程序 */
void main(int argc, char *argv[])
{
printf("LZW compression and decompression utility\n");

if (4 != argc)
{
printf("\nUsage : lzw -c|d sourcefilename targetfilename\n");
exit(0);
}

if (! strcmp(argv[1], "-c"))
LZW_Compression(argv[2], argv[3]);
else if (! strcmp(argv[1], "-d"))
LZW_Decompression(argv[2], argv[3]);
else
printf("\nUnknow command.\n");
}

/**************************************************************************************************
LZW_Compression()

本函数用LZW算法压缩指定的文件,并将结构存储到新的文件中
***************************************************************************************************/
int LZW_Compression(char *in_filename, char *out_filename)
{
unsigned int next_code;
unsigned int character;
unsigned int string_code;
unsigned int index;
int i;
FILE *input;
FILE *output;

/* allocate memory for compression */
code_value=malloc(TABLE_SIZE*sizeof(unsigned int));
prefix_code=malloc(TABLE_SIZE*sizeof(unsigned int));
append_character=malloc(TABLE_SIZE*sizeof(unsigned char));
if (code_value==NULL || prefix_code==NULL || append_character==NULL)
{
printf("Fatal error allocating table space!\n");
return 0;
}

/* open files */
input=fopen(in_filename,"rb");
output=fopen(out_filename,"wb");
if (input==NULL || output==NULL)
{
printf("Fatal error opening files.\n");
return 0;
};

/* compressing... */

next_code=256; /* Next code is the next available string code*/
for (i=0;i<TABLE_SIZE;i++) /* Clear out the string table before starting */
code_value[i]=-1;

i=0;
printf("Compressing...\n");
string_code=getc(input); /* Get the first code */
/*
** This is the main loop where it all happens. This loop runs util all of
** the input has been exhausted. Note that it stops adding codes to the
** table after all of the possible codes have been defined.
*/
while ((character=getc(input)) != (unsigned)EOF)
{
if (++i==1000) /* Print a * every 1000 */
{ /* input characters. This */
i=0; /* is just a pacifier. */
printf(".");
}
index=find_match(string_code,character);/* See if the string is in */
if (code_value[index] != -1) /* the table. If it is, */
string_code=code_value[index]; /* get the code value. If */
else /* the string is not in the*/
{ /* table, try to add it. */
if (next_code <= MAX_CODE)
{
code_value[index]=next_code++;
prefix_code[index]=string_code;
append_character[index]=character;
}
output_code(output,string_code); /* When a string is found */
string_code=character; /* that is not in the table*/
} /* I output the last string*/
} /* after adding the new one*/
/*
** End of the main loop.
*/
output_code(output,string_code); /* Output the last code */
output_code(output,MAX_VALUE); /* Output the end of buffer code */
output_code(output,0); /* This code flushes the output buffer*/
printf("\n");

/* cleanup... */
fclose(input);
fclose(output);

free(code_value);
free(prefix_code);
free(append_character);

return 1;
}
laolaoliu2002 2008-02-27
  • 打赏
  • 举报
回复
http://www.programsalon.com/downloads75/sourcecode/zip/detail277344.html
sexisnothing 2008-02-27
  • 打赏
  • 举报
回复
同志们!
sexisnothing 2008-02-26
  • 打赏
  • 举报
回复
网上有一些源码 windows下的很多 移植性很差
无心人 2008-02-22
  • 打赏
  • 举报
回复
下载7z源代码研究

但不知道7z是不是LZW编码

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧