-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathld2img.c
69 lines (55 loc) · 1.31 KB
/
ld2img.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdlib.h>
#include <stdio.h>
const char *get_in_file(int argc, char const *argv[]);
const char *get_out_file(int argc, char const *argv[]);
int main(int argc, char const *argv[]) {
const char* in_file = get_in_file(argc, argv);
const char* out_file = get_out_file(argc, argv);
FILE* data = fopen(in_file, "r");
FILE* image = out_file ? fopen(out_file, "w+") : stdout;
if (!data) {
perror("File opening failed");
return EXIT_FAILURE;
}
int c;
int p = 'A';
int pixels = 0;
fputs("P5\n", image);
fputs("160 144\n", image);
fputs("255\n", image);
while ((c = fgetc(data)) != EOF) {
if (p == '\'' && c >= '0' && c <= '3'){
pixels++;
c -= '0';
c = c ^ 3;
fputc(c * 85, image);
}
p = c;
}
fclose(data);
return EXIT_SUCCESS;
}
const char *get_in_file(int argc, char const *argv[]) {
switch (argc) {
case 4:
return argv[3];
break;
case 2:
return argv[1];
break;
default:
printf("USAGE: %s [-o <file>] <file>\n\n", argv[0]);
printf("OPTIONS:\n");
printf(" -o <file>\tWrite to <file> instead of stdout.");
exit(EXIT_SUCCESS);
}
}
const char *get_out_file(int argc, char const *argv[]) {
switch (argc) {
case 4:
return argv[2];
break;
default:
return NULL;
}
}