RLE壓縮演算法

來源:互聯網
上載者:User

 

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;

}

 

程式運行結果如下:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.