-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrle.c
45 lines (40 loc) · 977 Bytes
/
rle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
void main(int argc, char **argv) {
FILE *ifp;
FILE *ofp;
int count;
int bytesread;
uint8_t lastbyte;
uint8_t nextbyte;
uint8_t odata[2];
if (argc < 3) {
printf("Usage: %s [raw input] [LRE compressed output]\n", argv[0]);
return;
}
ifp = fopen(argv[1], "r");
if (ifp == NULL) {
printf("Error opening %s for reading\n", argv[1]);
return;
}
ofp = fopen(argv[2], "w");
if (ofp == NULL) {
printf("Error opening %s for writing\n", argv[2]);
return;
}
fread(&lastbyte,1,1,ifp);
count = 1;
while (!feof(ifp)) {
bytesread = fread(&nextbyte,1,1,ifp);
if ((count == 255) || (nextbyte != lastbyte) || (bytesread == 0)) {
odata[0] = count;
odata[1] = lastbyte;
fwrite(odata,1,2,ofp);
lastbyte = nextbyte;
count = 1;
} else {
count++;
}
}
}