aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: b4fea1707e7c437096b898a249212646b59b715e (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#include "image.h"

#define ARRAY_LEN(arr) sizeof(arr) / sizeof(arr[0])

#define FORMAT_ERROR "%s is not in a supported format\n"

typedef size_t (*splash_func)(uint8_t**, size_t);
typedef struct {
    char* extension;
    splash_func func;
} splash_types;

const splash_types in_conversions[] = {
    { .extension = "bin", .func = bin_to_rgba },
    { .extension = "png", .func = png_to_rgba },
};

const splash_types out_conversions[] = {
    { .extension = "bin", .func = rgba_to_bin },
    { .extension = "png", .func = rgba_to_png },
};

int main(int argc, char** argv)
{
    FILE* in_fd = NULL;
    FILE* out_fd = NULL;

    int retval = EXIT_SUCCESS;
    if (argc < 3)
    {
        fprintf(stderr, "%s [input] [output]\n", argv[0]);
        retval = EXIT_FAILURE;
        goto exit;
    }

    char* in_path = argv[1];
    size_t in_path_len = strlen(in_path);
    splash_func in_func = NULL;

    char* out_path = argv[2];
    size_t out_path_len = strlen(out_path);
    splash_func out_func = NULL;

    for (size_t i = 0; i <= ARRAY_LEN(in_conversions); ++i)
    {
        if (i == ARRAY_LEN(in_conversions))
        {
            fprintf(stderr, FORMAT_ERROR, in_path);
            retval = EXIT_FAILURE;
            goto exit;
        }

        char* extension = in_conversions[i].extension;
        if (!strcmp(in_path+(in_path_len-strlen(extension)), extension))
        {
            in_func = in_conversions[i].func;
            break;
        }
    }

    for (size_t i = 0; i <= ARRAY_LEN(out_conversions); ++i)
    {
        if (i == ARRAY_LEN(out_conversions))
        {
            fprintf(stderr, FORMAT_ERROR, out_path);
            retval = EXIT_FAILURE;
            goto exit;
        }

        char* extension = out_conversions[i].extension;
        if (!strcmp(out_path+(out_path_len-strlen(extension)), extension))
        {
            out_func = out_conversions[i].func;
            break;
        }
    }


    in_fd = fopen(in_path, "rb");
    if (!in_fd)
    {
        fprintf(stderr, "cannot open %s\n", in_path);
        retval = EXIT_FAILURE;
        goto exit;
    }

    fseek(in_fd, 0L, SEEK_END);
    size_t in_size = (size_t)ftell(in_fd);
    rewind(in_fd);

    if (!in_size)
    {
        fprintf(stderr, "%s is empty\n", in_path);
        retval = EXIT_FAILURE;
        goto exit;
    }

    out_fd = fopen(out_path, "wb");
    if (!out_fd)
    {
        fprintf(stderr, "cannot open %s\n", out_path);
        retval = EXIT_FAILURE;
        goto exit;
    }

    uint8_t* buf = malloc(in_size+1);
    fread(buf, 1, in_size, in_fd);
    buf[in_size] = '\0';

    if (in_func)
    {
        in_size = in_func(&buf, in_size);
    }

    if (out_func)
    {
        in_size = out_func(&buf, in_size);
    }

    fwrite(buf, 1, in_size, out_fd);

    exit:

    if (in_fd) fclose(in_fd);
    if (out_fd) fclose(out_fd);

    return retval;
}