-
Notifications
You must be signed in to change notification settings - Fork 27
/
png2c.c
51 lines (42 loc) · 1.22 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 <= 1) {
fprintf(stderr, "Usage: png2c <filepath.png> <name>\n");
fprintf(stderr, "ERROR: expected file path and name\n");
exit(1);
}
const char *filepath = shift(&argc, &argv);
const char *name = 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);
}
printf("#ifndef PNG_%s_H_\n", name);
printf("#define PNG_%s_H_\n", name);
printf("size_t %s_width = %d;\n", name, x);
printf("size_t %s_height = %d;\n", name, y);
printf("uint32_t %s_data[] = {", name);
for (size_t i = 0; i < (size_t)(x * y); ++i) {
printf("0x%x, ", data[i]);
}
printf("};\n");
printf("#endif // PNG_%s_H_\n", name);
return 0;
}