diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..665d3c8 --- /dev/null +++ b/src/main.c @@ -0,0 +1,59 @@ +#define STB_IMAGE_IMPLEMENTATION +#include "stb_image.h" +#include <stdio.h> + +#define RGB(r, g, b) (((r) << 11) | ((g) << 5) | (b)) + +static void +output_tile(const uint8_t *data, int x, int y, int w) +{ + for (int iy = 0; iy < 16; iy += 1) { + putchar('\t'); + for (int ix = 0; ix < 16; ix += 1) { + const int i = (ix + x + (iy + y) * w) * 3; + const int v = RGB(data[i]/8, data[i+1]/4, data[i+2]/8); + printf("%04x ", v); + } + putchar('\n'); + } +} + +int +main(int argc, char **argv) +{ + if (argc != 1) { + fprintf(stderr, "usage: %s < tileset.png\n", argv[0]); + return 1; + } + + /* Load image. */ + int w, h, chans; + uint8_t *const data = stbi_load_from_file(stdin, &w, &h, &chans, 3); + if (data == NULL) { + fprintf(stderr, "stbi_load_from_file failed\n"); + return 1; + } + + if ((w & 15) || (h & 15)) { + fprintf(stderr, "invalid dimensions %dx%d, " + "expected dimensions divisible by 16\n", + w, h); + goto panic; + } + if (chans != 3) { + fprintf(stderr, "my lame utility only supports " + "images using 3 color channels, " + "yours has %d channels. too bad\n", chans); + goto panic; + } + + for (int y = 0; y < h; y += 16) + for (int x = 0; x < w; x += 16) + output_tile(data, x, y, w); + + stbi_image_free(data); + return 0; +panic: + stbi_image_free(data); + return 0; +} |