diff options
Diffstat (limited to 'pximg.c')
-rw-r--r-- | pximg.c | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -0,0 +1,40 @@ +#include <stdint.h> +#include <stdio.h> +#define STB_IMAGE_IMPLEMENTATION +#include "stb_image.h" + +int main(int argc, char **argv) { + int img_w, img_h, img_col_chans; + uint8_t *img_data; + + if (argc != 3) { + fprintf(stderr, "usage: %s image.png name\n", argv[0]); + return 1; + } + + img_data = stbi_load(argv[1], &img_w, &img_h, &img_col_chans, 0); + + if (img_data == NULL) { + fputs("error: failed to load image\n", stderr); + return 1; + } + if (img_col_chans != 1) { + fputs("error: image isn't grayscale\n", stderr); + stbi_image_free(img_data); + return 1; + } + if (img_h * img_w == 0) { + fputs("error: image size is 0\n", stderr); + stbi_image_free(img_data); + return 1; + } + + printf("#include \"px.h\"\nconst PxSpr %s={%d,%d,(const uint8_t[]){", + argv[2], img_w, img_h); + for (int i = 0; i < img_w * img_h; i++) + printf("%#x,", (unsigned)img_data[i]); + printf("}};\n"); + + stbi_image_free(img_data); + return 0; +} |