-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
67 lines (51 loc) · 1.17 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
61
62
63
64
65
66
67
#include <stdio.h>
#include <stdlib.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image/stb_image_2016.h"
int main(int argc, char **argv)
{
FILE *f;
char *buf = NULL;
long siz_buf;
if (argc < 2)
{
fprintf(stderr, "No input file submitted\n");
goto err;
}
f = fopen(argv[1], "rb");
if (f == NULL)
{
fprintf(stderr, "Error opening input file %s\n", argv[1]);
goto err;
}
fseek(f, 0, SEEK_END);
siz_buf = ftell(f);
rewind(f);
if (siz_buf < 1)
goto err;
buf = (char *)malloc((size_t)siz_buf);
if (buf == NULL)
{
fprintf(stderr, "malloc() failed\n");
goto err;
}
if (fread(buf, (size_t)siz_buf, 1, f) != 1)
{
fprintf(stderr, "fread() failed\n");
goto err;
}
const uint8_t *data = (uint8_t *)buf;
size_t size = siz_buf;
int x, y, channels;
if (!stbi_info_from_memory(data, size, &x, &y, &channels))
{
goto err;
}
if (y && x > (80000000 / 4) / y)
return 0;
unsigned char *img = stbi_load_from_memory(data, size, &x, &y, &channels, 4);
free(img);
err:
free(buf);
return 0;
}