📄 rle1.cpp
字号:
/* File: codrle1.c
Author: David Bourgin
Creation date: 28/1/94
Last update: 22/5/94
Purpose: Example of RLE type 1 encoding with a file source to compress.
*/
#include <stdio.h>
/* For routines printf,fgetc,fputc and fwrite */
#include <stdlib.h>
/* For routine exit */
/* For ComprLib codec support */
#include "codec.h"
/* 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
/* Global variables */
FILE *source_file,*dest_file;
/* 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' */
int byte_stored_status=FALSE;
int val_byte_stored;
/* Pseudo procedures */
/*
#define end_of_data() (byte_stored_status?FALSE:!(byte_stored_status=((val_byte_stored=fgetc(source_file))!=EOF)))
#define read_byte() (byte_stored_status?byte_stored_status=FALSE,(unsigned char)val_byte_stored:(unsigned char)fgetc(source_file))
#define write_byte(byte) ((void)fputc((byte),dest_file))
#define write_array(array,byte_nb_to_write) ((void)fwrite((array),1,(byte_nb_to_write),dest_file))
*/
int encode_rle1() /* was rle1encoding() */
/* Returned parameters: None
Action: Compresses with RLE type 1 method all bytes read by the function 'read_byte'
Errors: An input/output error could disturb the running of the program
*/
{ register unsigned char byte1,byte2,frame_size,
array[129];
if (!end_of_data())
{ byte1=read_byte(); /* Is there at least a byte to analyze? */
frame_size=1;
if (!end_of_data())
/* Are there at least two bytes to analyze? */
{ byte2=read_byte();
frame_size=2;
do { if (byte1==byte2)
/* Is there a repetition? */
{ while ((!end_of_data())&&(byte1==byte2)&&(frame_size<129))
{ byte2=read_byte();
frame_size++;
}
if (byte1==byte2)
/* Do we meet only a sequence of bytes? */
{ write_byte(126+frame_size);
write_byte(byte1);
if (!end_of_data())
{ byte1=read_byte();
frame_size=1;
}
else frame_size=0;
}
else /* No, then don't handle the last byte */
{ write_byte(125+frame_size);
write_byte(byte1);
byte1=byte2;
frame_size=1;
}
if (!end_of_data())
{ byte2=read_byte();
frame_size=2;
}
}
else /* Prepare the array of comparisons
where will be stored all the identical bytes */
{ *array = byte1;
array[1]=byte2;
while ((!end_of_data())&&(array[frame_size-2]!=array[frame_size-1])&&(frame_size<128))
{ array[frame_size]=read_byte();
frame_size++;
}
if (array[frame_size-2]==array[frame_size-1])
/* Do we meet a sequence of all different bytes followed by identical byte? */
{ /* Yes, then don't count the two last bytes */
write_byte(frame_size-3);
write_array(array,frame_size-2);
byte1=array[frame_size-2];
byte2=byte1;
frame_size=2;
}
else { write_byte(frame_size-1);
write_array(array,frame_size);
if (end_of_data())
frame_size=0;
else { byte1=read_byte();
if (end_of_data())
frame_size=1;
else { byte2=read_byte();
frame_size=2;
}
}
}
}
}
while ((!end_of_data())||(frame_size>=2));
}
if (frame_size==1)
{ write_byte(0);
write_byte(byte1);
}
}
return 0;
}
/* From dcodrle1.c */
int decode_rle1() /* void rle1decoding() */
/* Returned parameters: None
Action: Decompresses with RLE type 1 method all bytes read by the function read_byte
Erreurs: An input/output error could disturb the running of the program
*/
{ unsigned char header;
register unsigned char i;
while (!end_of_data())
{ header=read_byte();
switch (header & 128)
{ case 0:for (i=0;i<=header;i++)
write_byte(read_byte());
break;
case 128:write_block(read_byte(),(header & 127)+2);
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -