forked from tsoding/sowon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
png2c.c
51 lines (42 loc) · 1.18 KB
/
png2c.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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define STB_IMAGE_IMPLEMENTATION
#include "./stb_image.h"
const char *shift(int *argc, char ***argv)
{
assert(*argc > 0);
const char *result = *argv[0];
*argc -= 1;
*argv += 1;
return result;
}
int main(int argc, char *argv[])
{
shift(&argc, &argv); // skip program name
if (argc <= 0) {
fprintf(stderr, "Usage: png2c <filepath.png>\n");
fprintf(stderr, "ERROR: expected file path\n");
exit(1);
}
const char *filepath = shift(&argc, &argv);
int x, y, n;
uint32_t *data = (uint32_t *)stbi_load(filepath, &x, &y, &n, 4);
if (data == NULL) {
fprintf(stderr, "Could not load file `%s`\n", filepath);
exit(1);
}
// TODO: inclusion guards and the array name are not customizable
printf("#ifndef PNG_H_\n");
printf("#define PNG_H_\n");
printf("size_t png_width = %d;\n", x);
printf("size_t png_height = %d;\n", y);
printf("uint32_t png[] = {");
for (size_t i = 0; i < (size_t)(x * y); ++i) {
printf("0x%x, ", data[i]);
}
printf("};\n");
printf("#endif // PNG_H_\n");
return 0;
}