diff options
author | Rangi <remy.oukaour+rangi42@gmail.com> | 2021-09-02 00:21:10 -0400 |
---|---|---|
committer | Rangi <remy.oukaour+rangi42@gmail.com> | 2021-09-02 00:21:10 -0400 |
commit | 313deab55253ee49ef3872491f6805a03b5ea36b (patch) | |
tree | 855534742230d659350dd72afded9a353fb44ccd /tools/png_dimensions.c | |
parent | 8f88e04401083d847d8f234645a91067c9c70e1b (diff) |
Rewrite tool png_dimensions.c, and start using common.h more
Diffstat (limited to 'tools/png_dimensions.c')
-rw-r--r-- | tools/png_dimensions.c | 58 |
1 files changed, 13 insertions, 45 deletions
diff --git a/tools/png_dimensions.c b/tools/png_dimensions.c index 900485bdf..f46f42b8b 100644 --- a/tools/png_dimensions.c +++ b/tools/png_dimensions.c @@ -1,57 +1,25 @@ -#include <stdio.h> -#include <stdint.h> -#include <stdlib.h> +#include "common.h" -void usage(void) { - fprintf(stderr, "Usage: png_dimensions infile outfile\n"); - exit(1); +void usage() { + fprintf(stderr, "Usage: png_dimensions in.png out.dimensions\n"); } -void output_dimensions(char* png_filename, char* out_filename) { - FILE* f; - int width, height; - int i; - uint8_t bytes[4]; - uint8_t output; - - f = fopen(png_filename, "rb"); - if (f == NULL) { - fprintf(stderr, "failed to open file %s\n", png_filename); - exit(1); - } - - // width - fseek(f, 16, SEEK_SET); - int size = fread(bytes, 1, 4, f); - fclose(f); - if (size != 4) { - fprintf(stderr, "failed to read at offset 0x10 in file %s\n", png_filename); +uint8_t read_dimensions(const char *filename) { + uint32_t width_px = read_png_width_verbose(filename); + if (width_px != 40 && width_px != 48 && width_px != 56) { + fprintf(stderr, "Not a valid width for \"%s\": %" PRIu32 " px\n", filename, width_px); exit(1); } - - width = 0; - for (i = 0; i < 4; i++) { - width |= bytes[i] << (8 * (3 - i)); - } - width >>= 3; - height = width; - - output = width & 0xf; - output |= (height & 0xf) << 4; - - f = fopen(out_filename, "wb"); - if (f == NULL) { - fprintf(stderr, "failed to open file %s\n", out_filename); - exit(1); - } - fwrite(&output, 1, 1, f); - fclose(f); + uint8_t width_tiles = (uint8_t)(width_px / 8); + return (width_tiles << 4) | width_tiles; } -int main(int argc, char* argv[]) { +int main(int argc, char *argv[]) { if (argc < 3) { usage(); + exit(1); } - output_dimensions(argv[1], argv[2]); + uint8_t output_byte = read_dimensions(argv[1]); + write_u8(argv[2], &output_byte, 1); return 0; } |