|
6 | 6 | #include "read_write.h" |
7 | 7 |
|
8 | 8 |
|
9 | | -int main(int argc, char **argv) |
| 9 | +int main(int argc, char **argv) |
10 | 10 | { |
11 | | - char *input = NULL, *output = NULL; // input and output file |
12 | | - double angle = 0.0; // the angle used for pixelsorting |
13 | | - int threshold_value = 20; |
| 11 | + char *input = NULL, *output = NULL; // input and output file |
| 12 | + double angle = 0.0; // the angle used for pixelsorting |
| 13 | + int threshold_value = 20; |
14 | 14 |
|
15 | | - int c; |
16 | | - opterr = 0; |
| 15 | + int c; |
| 16 | + opterr = 0; |
17 | 17 |
|
18 | | - // Argument parsing |
19 | | - while ((c = getopt (argc, argv, "i:o:a:t:")) != -1) { |
20 | | - switch (c) { |
21 | | - case 'a': |
22 | | - angle = atof(optarg); |
23 | | - break; |
24 | | - case 'i': |
25 | | - input = optarg; |
26 | | - break; |
27 | | - case 'o': |
28 | | - output = optarg; |
29 | | - break; |
30 | | - case 't': |
31 | | - threshold_value = (int)(atof(optarg)); |
32 | | - break; |
33 | | - case '?': |
34 | | - if (optopt == 'i' || optopt == 'o') { |
35 | | - fprintf (stderr, "Option -%c requires an argument.\n", optopt); |
36 | | - } else if (isprint (optopt)) { |
37 | | - fprintf (stderr, "Unknown option `-%c'.\n", optopt); |
38 | | - } else { |
39 | | - fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt); |
40 | | - return 1; |
41 | | - } |
42 | | - abort(); |
43 | | - default: |
44 | | - abort(); |
45 | | - } |
46 | | - } |
| 18 | + // Argument parsing |
| 19 | + while ((c = getopt (argc, argv, "i:o:a:t:")) != -1) { |
| 20 | + switch (c) { |
| 21 | + case 'a': |
| 22 | + angle = atof(optarg); |
| 23 | + break; |
| 24 | + case 'i': |
| 25 | + input = optarg; |
| 26 | + break; |
| 27 | + case 'o': |
| 28 | + output = optarg; |
| 29 | + break; |
| 30 | + case 't': |
| 31 | + threshold_value = (int)(atof(optarg)); |
| 32 | + break; |
| 33 | + case '?': |
| 34 | + if (optopt == 'i' || optopt == 'o') { |
| 35 | + fprintf (stderr, "Option -%c requires an argument.\n", optopt); |
| 36 | + } else if (isprint (optopt)) { |
| 37 | + fprintf (stderr, "Unknown option `-%c'.\n", optopt); |
| 38 | + } else { |
| 39 | + fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt); |
| 40 | + return 1; |
| 41 | + } |
| 42 | + abort(); |
| 43 | + default: |
| 44 | + abort(); |
| 45 | + } |
| 46 | + } |
47 | 47 |
|
48 | | - |
49 | | - int is_jpg = 0; |
50 | | - int is_png = 0; |
51 | 48 |
|
52 | | - // Get filename |
53 | | - const char *dot = strrchr(input, '.'); |
| 49 | + int is_jpg = 0; |
| 50 | + int is_png = 0; |
| 51 | + |
| 52 | + // Get filename |
| 53 | + const char *dot = strrchr(input, '.'); |
54 | 54 | if(!dot || dot == input) abort(); |
55 | 55 | const char *ext = dot + 1; |
56 | | - if (strcmp(ext, "jpeg") == 0 |
57 | | - || strcmp(ext, "jpg") == 0) { |
58 | | - is_jpg = 1; |
59 | | - } |
60 | | - if (strcmp(ext, "png") == 0) { |
61 | | - is_png = 1; |
62 | | - } |
| 56 | + if (strcmp(ext, "jpeg") == 0 |
| 57 | + || strcmp(ext, "jpg") == 0) { |
| 58 | + is_jpg = 1; |
| 59 | + } |
| 60 | + if (strcmp(ext, "png") == 0) { |
| 61 | + is_png = 1; |
| 62 | + } |
| 63 | + |
| 64 | + if (!(is_png || is_jpg)) { |
| 65 | + abort(); |
| 66 | + } |
63 | 67 |
|
64 | | - if (!(is_png || is_jpg)) { |
65 | | - abort(); |
66 | | - } |
67 | 68 |
|
68 | | - |
69 | | - char *tmp = ".tmp.png"; |
70 | | - if (is_jpg) { |
71 | | - printf("Converting JPG to PNG…\n"); |
72 | | - char cmd[512]; |
73 | | - strcpy(cmd, "convert "); |
74 | | - strcat(cmd, input); |
75 | | - strcat(cmd, " "); |
76 | | - strcat(cmd, tmp); |
77 | | - system(cmd); |
78 | | - } |
| 69 | + char *tmp = ".tmp.png"; |
| 70 | + if (is_jpg) { |
| 71 | + printf("Converting JPG to PNG…\n"); |
| 72 | + char cmd[512]; |
| 73 | + strcpy(cmd, "convert "); |
| 74 | + strcat(cmd, input); |
| 75 | + strcat(cmd, " "); |
| 76 | + strcat(cmd, tmp); |
| 77 | + system(cmd); |
| 78 | + } |
79 | 79 |
|
80 | | - // If input and output are given |
81 | | - if (input && output) { |
82 | | - if(is_jpg) { |
83 | | - read_png_file(tmp); |
84 | | - } else if (is_png) { |
85 | | - read_png_file(input); |
86 | | - } |
87 | | - process_png_file(threshold_value); |
88 | | -// pixel_sort(); |
89 | | - write_png_file(output); |
90 | | - } |
| 80 | + // If input and output are given |
| 81 | + if (input && output) { |
| 82 | + if(is_jpg) { |
| 83 | + read_png_file(tmp); |
| 84 | + } else if (is_png) { |
| 85 | + read_png_file(input); |
| 86 | + } |
| 87 | + process_png_file(threshold_value); |
| 88 | +// pixel_sort(); |
| 89 | + write_png_file(output); |
| 90 | + } |
91 | 91 |
|
92 | | - if (is_jpg) { |
93 | | - char *cmd[512]; |
94 | | - strcpy(cmd, "rm "); |
95 | | - strcat(cmd, tmp); |
96 | | - system(cmd); |
97 | | - } |
| 92 | + if (is_jpg) { |
| 93 | + char *cmd[512]; |
| 94 | + strcpy(cmd, "rm "); |
| 95 | + strcat(cmd, tmp); |
| 96 | + system(cmd); |
| 97 | + } |
98 | 98 |
|
99 | | - |
100 | 99 |
|
101 | | - return 0; |
| 100 | + return 0; |
102 | 101 | } |
0 commit comments