home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 June
/
SIMTEL_0692.cdr
/
msdos
/
ddjmag
/
ddj8905.arc
/
RLE.ASC
< prev
next >
Wrap
Text File
|
1989-05-12
|
5KB
|
156 lines
_RLE Revisited_
by Phil Daley
[LISTING ONE]
/*****************************************************************************
* PROGRAM RLE.C *
* written by Phil Daley *
* February 3, 1989 *
*****************************************************************************/
int main(void);
int uncompress(unsigned char *,int,unsigned char *) ;
int compress(unsigned char *,int,unsigned char *) ;
int process_comp(unsigned char *,int,int) ;
int process_uncomp(unsigned char *,int,int) ;
#include <stdio.h>
#include <memory.h>
unsigned char screen[24][80] = {
"╔══════════════════════════════════════════════════════════════════════════════╗",
"║ ║",
"║ ║",
"║ ║",
"║ o This is a sample screen that would be typical of the type ║",
"║ that would present information to a user for instructions ║",
"║ or a help screen, etc. ║",
"║ ║",
"║ ║",
"║ o While it contains a lot of unique characters in the text ║",
"║ lines, it also contains a lot of white space in empty lines ║",
"║ and margins. ║",
"║ ║",
"║ ║",
"║ o It would be unusual for the compression algorithm used ║",
"║ here to find any repeated characters other than spaces ║",
"║ and the border. ║",
"║ ║",
"║ ║",
"║ ║",
"║ ║",
"║ ║",
"║ ║",
"╚══════════════════════════════════════════════════════════════════════════════╝"} ;
unsigned char new[2000] ;
/********************** main ***********************/
int main() /* this is a demo main */
{
int orig_length = 1920 ;
int compressed_length ;
int i, j ;
compressed_length = compress(screen[0],orig_length,new) ;
printf("The original screen (1920) compressed to %d bytes\n",compressed_length) ;
memset(screen,0,1920) ; /* erase the orig */
orig_length = uncompress(new,compressed_length,screen[0]) ;
printf("And back to the original (%d) length\n",orig_length) ;
for (i = 0; i < 24; i++)
for (j = 0; j < 80; j++) /* show it */
printf("%c",screen[i][j]) ;
return(0) ;
}
/*********************** compress ****************************/
compress(in_array,in_size,out_array)
unsigned char *in_array ;
int in_size ;
unsigned char *out_array ;
{
register int i = 0 ;
register int j = 0 ;
register int k ;
register int l ;
while (i < in_size) {
if (in_array[i] == in_array[i + 1] && in_array[i + 1] == in_array[i + 2]) {
k = process_comp(in_array,i,in_size) ;
out_array[j++] = (unsigned char)k | 0x80 ;
out_array[j++] = in_array[i] ;
i += k ;
}
else {
k = process_uncomp(in_array,i,in_size) ;
out_array[j++] = (unsigned char)k ;
for (l = 0; l < k; l++)
out_array[j++] = in_array[i++] ;
}
}
return(j) ;
}
/*********************** process_comp ****************************/
process_comp(in_array,i,in_size)
unsigned char *in_array ;
int i ;
int in_size ;
{
register int len = 0 ;
while (in_array[i] == in_array[i + 1] && i < in_size) {
len++ ;
i++ ;
if (len == 126)
break ;
}
return(len + 1) ;
}
/*********************** process_uncomp ****************************/
process_uncomp(in_array,i,in_size)
unsigned char *in_array ;
int i ;
int in_size ;
{
register int len = 0 ;
while ((in_array[i] != in_array[i + 1] || in_array[i] != in_array[i + 2]) && i < in_size) {
len++ ;
i++ ;
if (len == 127)
break ;
}
return(len) ;
}
/********************** uncompress ***********************/
uncompress(in_array,in_size,out_array)
unsigned char *in_array ;
int in_size ;
unsigned char *out_array ;
{
register int i ;
register int j ;
register int k=0 ;
register int l ;
for (i = 0; i < in_size;) {
j = in_array[i++] ;
if (j > 128) {
for (j -= 128; j > 0; j--)
out_array[k++] = in_array[i] ;
i++ ;
}
else
for (l = 0; l < j; l++)
out_array[k++] = in_array[i++] ;
}
return(k) ;
}
-30-