forked from derpibooru/cli_intensities
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
60 lines (48 loc) · 1.23 KB
/
main.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
#include "definitions.h"
#include <magic.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static char *mime_type(const char *filename)
{
magic_t magic;
const char *mime;
char *ret;
magic = magic_open(MAGIC_MIME_TYPE);
magic_load(magic, NULL);
mime = magic_file(magic, filename);
if (mime) {
ret = strdup(mime);
}
magic_close(magic);
return ret;
}
static int print_data(intensity_data data)
{
if (data.error) {
fprintf(stderr, "Decoder library returned an error\n");
return 1;
}
printf("%f\t%f\t%f\t%f\n", data.nw, data.ne, data.sw, data.se);
return 0;
}
int main(int argc, char *argv[])
{
char *file_mime;
intensity_data data;
if (argc != 2) {
fprintf(stderr, "Expected to be called with 2 arguments\n");
return 1;
}
file_mime = mime_type(argv[1]);
if (file_mime != NULL && strcmp(file_mime, "image/png") == 0) {
data = png_intensities(argv[1]);
} else if (file_mime != NULL && strcmp(file_mime, "image/jpeg") == 0) {
data = jpeg_intensities(argv[1]);
} else {
fprintf(stderr, "Unrecognized file type\n");
return 1;
}
free(file_mime);
return print_data(data);
}