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;

}

 

程序运行结果如下:

  • 4
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值