-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexampfile.c
More file actions
57 lines (44 loc) · 1.54 KB
/
Copy pathexampfile.c
File metadata and controls
57 lines (44 loc) · 1.54 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
50
51
52
53
54
55
56
57
// SPDX-License-Identifier: MIT
#include "../../amp.h"
#include "utils.h"
int main(int argc, char **argv) {
const char *error_message = nullptr;
if (argc < 2) {
error_message = "this program requires an AMP file argument\n";
write(2, error_message, strlen(error_message));
return EXIT_FAILURE;
}
auto input_file = load_file(argv[1]);
char *input_data = input_file.data;
size_t input_size = input_file.size;
if (!input_data) {
return EXIT_FAILURE;
}
uint32_t w, h;
size_t canvas_size = amp_doc_parse_size(input_data, input_size, &w, &h);
uint8_t *canvas_data = nullptr;
if (canvas_size) {
canvas_data = malloc(canvas_size);
if (canvas_data) {
struct amp_type amp;
if (amp_init(&, w, h, canvas_data, canvas_size) <= canvas_size) {
if (amp_decode(&, input_data, input_size)) {
amp_set_palette(&, AMP_PAL_24BIT);
amp_to_ans(&, nullptr, 0);
amp_stdout("\n", 1);
}
else error_message = "amp_decode: parse error\n";
}
else error_message = "amp_init: not enough memory provided\n";
}
else error_message = "malloc: allocation failed\n";
}
else error_message = "amp_parse_size: parse error\n";
free(input_data);
free(canvas_data);
if (error_message) {
write(2, error_message, strlen(error_message));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}