-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrle.c
More file actions
49 lines (43 loc) · 1.14 KB
/
rle.c
File metadata and controls
49 lines (43 loc) · 1.14 KB
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
46
47
48
49
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
long fileSize;
fp = fopen("test.bmp", "rb");
fseek(fp, 0, SEEK_END);
fileSize = ftell(fp);
rewind(fp);
unsigned char * buffer;
buffer = (unsigned char *)malloc(sizeof(unsigned char) * fileSize);
fread(buffer, 1, fileSize, fp);
unsigned char current = buffer[0];
int count = 1;
unsigned char *output = (unsigned char *)malloc(sizeof(unsigned char) * fileSize);
int output_index = 0;
for (int i = 1; i < fileSize; i++) {
printf("%x ", current);
if (current == buffer[i]) {
count++;
}
else {
if (count > 1) {
output[output_index++] = current;
output[output_index++] = current;
output[output_index++] = count;
}
else {
output[output_index++] = current;
}
count = 1;
}
current = buffer[i];
}
fclose(fp);
printf("\n\n");
printf("%d\n", output_index);
for (int i = 0; i < output_index; i++) {
printf("%x ", output[i]);
}
return 0;
}