⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rle2.cpp

📁 zlib-1.2.3.tar是新的zlib库藏 用于压缩 等等
💻 CPP
字号:
/* File: codrle2.c
   Author: David Bourgin
   Creation date: 1/2/94
   Last update: 24/7/95
   Purpose: Example of RLE type 2 encoding with a file source to compress.
*/

/* Modified 06/02/2000 by Jeff Connelly to work with ComprLib */
/* Modified 06/02/2000 by Jeff Connelly to conform to ANSI C++ */
#include "codec.h"

#include <stdio.h>
/* For routines printf,fgetc,fputc and rewind */
#include <memory.h>
#include <string.h>
/* For routine memset */
#include <stdlib.h>
/* For routine exit */

/* Error codes sent to the caller */
#define NO_ERROR      0
#define BAD_FILE_NAME 1
#define BAD_ARGUMENT  2

/* Useful constants */
#define FALSE 0
#define TRUE  1

                             /* Being that fgetc=EOF only after an access
                                then 'byte_stored_status' is 'TRUE' if a byte has been stored by 'fgetc'
                                or 'FALSE' if there's no valid byte not already read and not handled in 'val_byte_stored' */
static void rle2write_rep(unsigned char header_byte,
    unsigned char repeated_byte, unsigned int repetition_number)
/* Returned parameters: None
   Action: Writes in the output compression stream the encoding of 'repetition_number' times 'repeated_byte'.
   'header_byte' is used as marker as defined in RLE 2 method
   Errors: An input/output error could disturb the running of the program
*/
{
 if (repetition_number<4)
     if (repeated_byte==header_byte)
        { write_byte(header_byte);
          write_byte(repetition_number-1);
        }
     else { register unsigned int i;

            for (i=1;i<=repetition_number;i++)
                write_byte(repeated_byte);
          }
  else { write_byte(header_byte);
         write_byte(repetition_number-1);
         write_byte(repeated_byte);
       }
}

static void rle2write_non_rep(unsigned char header_byte,
    unsigned char non_repeated_byte)
/* Returned parameters: None
   Action: Writes in the output compression stream the encoding of 'non_repeated_byte'
   'header_byte' is used as marker as defined in RLE 2 method
   Errors: An input/output error could disturb the running of the program
*/
{
 if (non_repeated_byte==header_byte)
     { write_byte(header_byte);
       write_byte(0);
     }
  else write_byte(non_repeated_byte);
}

int encode_rle2() /* void rle2encoding() */
/* Returned parameters: None
   Action: Compresses with RLE type 2 method all bytes read by the function read_byte
   Errors: An input/output error could disturb the running of the program
*/
{ unsigned char byte1,byte2,header_byte;
  unsigned int frame_size;
  register unsigned int i;
  unsigned long int lookup_table[256];

  /* File signature */
  write_byte ('R');
  write_byte ('L');
  write_byte ('2');

  if (!end_of_data())        /* Is there at least a byte to analyze? */
     {                       /* Sets up the occurrence numbers of all bytes to 0 */
       (void)memset((char *)lookup_table,0,sizeof(lookup_table));
                             /* This is the same to fill 'lookup_table' to 0.
                                It's fastest than to loop 256 times */
       while (!end_of_data())/* Valids the occurrences in 'lookup_table' in regard to the data to compress */
             { byte1=read_byte();
               lookup_table[byte1]++;
             }
       header_byte=0;
       for (i=1;i<=255;i++)
           if (lookup_table[i]<lookup_table[header_byte])
              header_byte=i;
       write_byte(header_byte);
       beginning_of_data();  /* New data analysis */
       byte1=read_byte();
       frame_size=1;
       if (!end_of_data())
                             /* Are there at least two bytes? */
          { byte2=read_byte();
            frame_size=2;
            do {             /* Real beginning of the compression */
                 if (byte1==byte2)
                             /* Do we meet only a sequence of identical bytes? */
                    { while ((!end_of_data())&&(byte1==byte2)&&(frame_size<256))
                            { byte2=read_byte();
                              frame_size++;
                            }
                      if (byte1==byte2)
                         { rle2write_rep(header_byte,byte1,frame_size);
                           if (!end_of_data())
                              { byte1=read_byte();
                                frame_size=1;
                              }
                           else frame_size=0;
                         }
                      else { rle2write_rep(header_byte,byte1,frame_size-1);
                             byte1=byte2;
                             frame_size=1;
                           }
                    }
                 else {      /* No, then don't handle the last byte */
                        rle2write_non_rep(header_byte,byte1);
                        byte1=byte2;
                        frame_size=1;
                      }
                 if (!end_of_data())
                    { byte2=read_byte();
                      frame_size=2;
                    }
               }
            while ((!end_of_data())||(frame_size>=2));
          }
       if (frame_size==1)  /* Was there a last byte to analyze? */
          rle2write_non_rep(header_byte,byte1);
     }
     return 0;
}


/* File: dcodrle2.c
   Author: David Bourgin
   Creation date: 1/2/94
   Last update: 22/5/94
   Purpose: Example of RLE type 2 decoding with a file source to decompress.
*/

/* header code/initialization deleted */

int decode_rle2() /* rle2decoding() */
/* Returned parameters: None
   Action: Decompresses with RLE type 2 method all bytes read by the function read_byte
   Errors: An input/output error could disturb the running of the program
*/
{ unsigned char header_byte,byte_read,byte_to_repeat;
  register unsigned int i;

  if (!end_of_data())
     { header_byte=read_byte();
       do {                  /* Being that header byte is present, then there are bytes to decompress */
            if ((byte_read=read_byte())==header_byte)
               { byte_read=read_byte();
                 if (byte_read<3)
                    byte_to_repeat=header_byte;
                 else byte_to_repeat=read_byte();
                 for (i=0;i<=byte_read;i++)
                     write_byte(byte_to_repeat);
               }
            else write_byte(byte_read);
          }
       while (!end_of_data());
     }
     return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -