diff --git a/bin/main.c b/bin/main.c index da1191b..e07f6ff 100644 --- a/bin/main.c +++ b/bin/main.c @@ -85,6 +85,54 @@ error_malloc_png: return -1; } +static uint8_t *read_png_file(const char *file, + size_t *width, + size_t *height, + int *depth) { + png_t *png; + uint8_t *buf; + + png_init(malloc, free); + + if ((png = malloc(sizeof(*png))) == NULL) { + goto error_malloc_png; + } + + if (png_open_file_read(png, file) < 0) { + goto error_png_open_file_read; + } + + *width = png->width; + *height = png->height; + *depth = png->bpp; + + if ((buf = malloc(*width * *height * *depth)) == NULL) { + goto error_malloc_buf; + } + + if (png_get_data(png, buf) < 0) { + goto error_png_get_data; + } + + png_close_file(png); + + free(png); + + return buf; + +error_png_get_data: + free(buf); + +error_malloc_buf: + png_close_file(png); + +error_png_open_file_read: + free(png); + +error_malloc_png: + return NULL; +} + static int import(int argc, char **argv) { cammy_sram *sram; png_t *png; @@ -239,10 +287,9 @@ error_sram_open: } static int dither(int argc, char **argv) { - png_t *in; uint8_t *bufin, *bufout; - - int error; + size_t width, height; + int depth; if (argc < 3) { usage(argc, argv, "No PNG input file provided"); @@ -250,75 +297,46 @@ static int dither(int argc, char **argv) { usage(argc, argv, "No PNG output filename provided"); } - png_init(malloc, free); - - if ((in = malloc(sizeof(*in))) == NULL) { - goto error_malloc_in; - } - - if ((error = png_open_file_read(in, argv[2])) < 0) { - fprintf(stderr, "%s: %s: %s: %s\n", - argv[0], "png_open_file_read()", argv[2], png_error_string(error)); - - goto error_png_open_file_read; - } - - if ((bufin = malloc(in->width * in->height * in->bpp)) == NULL) { + if ((bufin = read_png_file(argv[2], &width, + &height, + &depth)) == NULL) { fprintf(stderr, "%s: %s: %s\n", - argv[0], "malloc()", strerror(errno)); + argv[0], "read_png_file()", strerror(errno)); - goto error_malloc_bufin; + goto error_read_png_file; } - if ((bufout = malloc(in->width * in->height * 3)) == NULL) { + if ((bufout = malloc(width * height * 3)) == NULL) { fprintf(stderr, "%s: %s: %s\n", argv[0], "malloc()", strerror(errno)); goto error_malloc_bufout; } - if ((error = png_get_data(in, bufin)) < 0) { - fprintf(stderr, "%s: %s: %s: %s\n", - argv[0], "png_get_data()", argv[2], png_error_string(error)); - - goto error_png_get_data; - } - - cammy_image_dither(bufout, bufin, in->width, in->height, in->bpp, &palette); + cammy_image_dither(bufout, bufin, width, height, depth, &palette); if (save_buf_to_png_file(argv[3], bufout, - in->width, - in->height, 8, PNG_TRUECOLOR) < 0) { + width, + height, 8, PNG_TRUECOLOR) < 0) { fprintf(stderr, "%s: %s: %s: %s\n", argv[0], "save_buf_to_png_file()", argv[3], strerror(errno)); goto error_save_buf_to_png_file; } - png_close_file(in); - free(bufout); free(bufin); - free(in); - return 0; error_save_buf_to_png_file: -error_png_get_data: free(bufout); error_malloc_bufout: free(bufin); -error_malloc_bufin: - png_close_file(in); - -error_png_open_file_read: - free(in); - -error_malloc_in: +error_read_png_file: return 1; }