Run-length encoding/C: Difference between revisions
no edit summary
m (→Unbuffered - Generative: minor reformat of code and comment) |
No edit summary |
||
(2 intermediate revisions by one other user not shown) | |||
Line 10:
Also: In this implementation, '''string'''s are assumed to be null terminated. Hence cannot contain the ''null'' character as data.
<
#include <stdlib.h>
#include <string.h>
const char *input = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW";
const char *output = "12W1B12W3B24W1B14W";
typedef void (*YIELDCHAR)(); /* yields via yield_init */
Line 45:
#define YIELDS(type) BOOL (*yield)(type) = yield_init
ITERATOR gen_char_seq (const char *s){
YIELDS(char);
fflush(stdout);
Line 79:
}
const char *zero2nine = "0123456789";
ITERATOR gen_decode (GENCHAR gen_char){
Line 94:
}
int main(){
/* iterate through input string */
printf("Encode input: ");
Line 108:
OD;
printf("\n");
return 0;
}</syntaxhighlight>
Output:
<pre>
Line 122 ⟶ 123:
Since repeat counter must fit a single byte in this implementation, it can't be greater than 255, so a byte repeated more than 255 times generates in the compressed stream more than 2 bytes (4 bytes if the length of the repeated byte sequence is less than 511 and so on)
<syntaxhighlight lang="c">
{
int dl, i;
Line 144 ⟶ 146:
*out++ = i; *out++ = cp; dl += 2;
return dl;
}
</syntaxhighlight>
'''Decoding function'''
<syntaxhighlight lang="c">
{
int i, j, tb;
Line 160 ⟶ 164:
}
return tb;
}
</syntaxhighlight>
'''Usage example'''
<
#include <stdlib.h>
#include <string.h>
Line 172 ⟶ 177:
int main()
{
char *d =
char *oc =
int rl = rle_encode(d, o, strlen(o));
Line 183 ⟶ 188:
free(d); free(oc);
return 0;
}
</syntaxhighlight>
In the following codes, encoding and decoding are
'''Encoding filter'''
<syntaxhighlight lang="c">
int main()
Line 205 ⟶ 212:
}
printf("%d%c", i, cp);
return 0;
}
</syntaxhighlight>
'''Decoding filter'''
<syntaxhighlight lang="c">
int main()
Line 218 ⟶ 228:
for(j=0; j < i; j++) printf("%c", c);
}
return 0;
}
</syntaxhighlight>
'''Final note''': since the repeat counter value 0 has no meaning, it could be used as it would be 256, so extending by one the maximum number of repetitions representable with a single byte; or instead it could be used as a special marker to encode in a more efficient way (long) sequences of ''isolated characters'', e.g. "ABCDE" would be encoded as "1A1B1C1D1E"; it could be instead encoded as "05ABCDE".
|