/ Published in: C
Encode and decode RLE from files
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
int rle_encode_file (FILE * in, FILE * out) { int block_len, block_char, c; block_len = 0; block_char = -1; for(;;) { if (c == EOF) break; if (c == block_char && block_len < 255) { /* same block */ block_len++; } else { if (block_len) { /* write block */ } /* start new block */ block_len = 1; block_char = c; } } if (block_len) { /* last block */ } return 1; } int rle_decode_file (FILE * in, FILE * out) { int i, len, c; for(;;) { /* checking */ if (len == EOF) return 1; /* end of file */ if (c == EOF) return 0; /* bad format */ /* output */ } }
URL: http://en.wikipedia.org/wiki/Run-length_encoding