RLE压缩算法

 

RLE算法实现数据压缩

游程编码(Run-Length Encoding, RLE)又称行程长度编码或者变动长度编码法,在控制理论中对于二值图像而言是一种编码方法,对连续的黑,白向像素以不同的码字进行编码。游程编码是一种简单的无损压缩方法,其特点是压缩和解压缩都非常快。该方法是用重复字节和重复次数来简单的描述重复的字节,也就是将一串连续的相同数据转换为特定的格式来达到压缩的目的。

       RLE是一种简单的压缩算法,主要用于压缩图像中连续的重复的颜色块。当然RLE并不是只能应用于图像压缩上,RLE能压缩任何二进制数据。原始图像文件的数据有一个特点,那就是有大量连续重复的颜色数据,RLE正好是用来压缩有大量连续重复数据的压缩编码,但对于其他二进制文件而言,由于文件中相同的数据出现概率较少,使用RLE压缩这些数据重复性不强的文件效果不太理想,有时候压缩后的数据反而变大了。

       RLE压缩方案是一种极其成熟的压缩方案,其特点是无损失压缩。

程序设计如下:

#include <stdio.h>

#include <stdlib.h>

#pragma hdrstop

#include <tchar.h>

#pragma argsused

void compress(char *sourcefile,char *destinfile)

{

       FILE *source,*destin;

       char cur_char,cur_seq;

       register int seq_len;

       if((source=fopen(sourcefile,"rb"))==NULL)

       {

              printf("Unable to open %s.",sourcefile);

              exit(0);

       }

       if((destin=fopen(destinfile,"wb"))==NULL)

       {

              printf("Unable to open %s.",destinfile);

              exit(0);

       }

       cur_char=fgetc(source);

       cur_seq=cur_char;

       seq_len=1;

       while(!feof(source))

       {

              cur_char=fgetc(source);

              if(cur_char==cur_seq)

              {

                     seq_len++;

              }

              else

              {

                     fputc(seq_len,destin);

                     fputc(cur_seq,destin);

                     cur_seq=cur_char;

                     seq_len=1;

              }

       }

       fclose(source);

       fclose(destin);

}

 

int _tmain(int argc, _TCHAR* argv[])

{

       char source[20],destin[20];

       printf("the file to compress:\n");

       scanf("%s",source);

       printf("the compressed file is located at:\n");

       scanf("%s",destin);

       compress(source,destin);

       return 0;

}

解压缩时,需要编写另外一个解压缩的程序,完整的程序如下:

#include <stdio.h>

#include <stdlib.h>

#pragma hdrstop

#include <tchar.h>

#pragma argsused

 

void compress(char *sourcefile,char *destinfile)

{

       FILE *source,*destin;

       char cur_char,cur_seq;

       register int seq_len;

       if((source=fopen(sourcefile,"rb"))==NULL)

       {

              printf("Unable to open %s.",sourcefile);

              exit(0);

       }

       if((destin=fopen(destinfile,"wb"))==NULL)

       {

              printf("Unable to open %s.",destinfile);

              exit(0);

       }

       cur_char=fgetc(source);

       cur_seq=cur_char;

       seq_len=1;

       while(!feof(source))

       {

              cur_char=fgetc(source);

              if(cur_char==cur_seq)

              {

                     seq_len++;

              }

              else

              {

                     fputc(seq_len,destin);

                     fputc(cur_seq,destin);

                     cur_seq=cur_char;

                     seq_len=1;

              }

       }

       fclose(source);

       fclose(destin);

}

 

void decompress(char *sourcefile,char *destinfile)

{

       FILE *source,*destin;

       char cur_char;

       register int i,seq_len;

       if((source=fopen(sourcefile,"rb"))==NULL)

       {

        printf("Unable to open %s.",sourcefile);

              exit(0);

       }

       if((destin=fopen(destinfile,"wb"))==NULL)

       {

              printf("Unable to open %s.",destinfile);

              exit(0);

       }

       while(!feof(source))

       {

              seq_len=fgetc(source);

              cur_char=fgetc(source);

              for(i=0;i<seq_len;i++)

              {

                     fputc(cur_char,destin);

              }

       }

       fclose(source);

       fclose(destin);

}

 

int _tmain(int argc, _TCHAR* argv[])

{

       char source[20],destin[20];

       printf("the file to compress:\n");

       scanf("%s",source);

       printf("the compressed file is located at:\n");

       scanf("%s",destin);

       compress(source,destin);

       printf("the file to decompress:\n");

       scanf("%s",source);

       printf("the decompressed file is located at:\n");

       scanf("%s",destin);

       decompress(source,destin);

       return 0;

}

 

程序运行结果如下:

相关推荐
©️2020 CSDN 皮肤主题: 程序猿惹谁了 设计师:白松林 返回首页