From cb6bdf531df9300eea1031d386ab8da87dd5fc75 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Fri, 3 Dec 2021 18:31:10 -0500 Subject: [PATCH] holy cognitohazard batman --- bin/dither.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 bin/dither.c diff --git a/bin/dither.c b/bin/dither.c new file mode 100644 index 0000000..e03ea79 --- /dev/null +++ b/bin/dither.c @@ -0,0 +1,116 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "commands.h" + +static cammy_image_color palette[4] = { + { 0, 0, 0, 255 }, + { 85, 85, 85, 255 }, + { 171, 171, 171, 255 }, + { 255, 255, 255, 255 } +}; + +static void usage(int argc, char **argv, const char *message, ...) { + if (message) { + va_list args; + + va_start(args, message); + vfprintf(stderr, message, args); + va_end(args); + fprintf(stderr, "\n"); + } + + fprintf(stderr, "usage: %1$s convert input.png output.png\n", argv[0]); + + exit(1); +} + +static cammy_image *image_dither(cammy_image *image) { + cammy_image *image_tile, + *dest; + + cammy_image_region from = { + 0, 0, image->width, image->height + }; + + cammy_image_point to = { + 0, 0 + }; + + if ((image_tile = cammy_image_new(CAMMY_IMAGE_TILE, + CAMMY_IMAGE_2BPP, + palette, + image->width, + image->height)) == NULL) { + goto error_image_new_2bpp; + } + + cammy_image_copy(image_tile, image, &to, &from); + + if ((dest = cammy_image_new(CAMMY_IMAGE_BITMAP, + CAMMY_IMAGE_24BPP_RGB, + palette, + image->width, + image->height)) == NULL) { + goto error_image_new_dest; + } + + cammy_image_copy(dest, image_tile, &to, &from); + + cammy_image_destroy(image_tile); + + return dest; + +error_image_new_dest: + cammy_image_destroy(image_tile); + +error_image_new_2bpp: + return NULL; +} + +int cammy_dither(int argc, char **argv) { + cammy_image *image_in, + *image_out; + + if (argc < 3) { + usage(argc, argv, "No PNG input file provided"); + } else if (argc < 4) { + usage(argc, argv, "No PNG output filename provided"); + } + + if ((image_in = cammy_image_open(argv[2])) == NULL) { + goto error_image_open; + } + + if ((image_out = image_dither(image_in)) == NULL) { + goto error_image_dither; + } + + if (cammy_image_save(image_out, argv[3]) < 0) { + goto error_image_save; + } + + cammy_image_destroy(image_out); + cammy_image_destroy(image_in); + + return 0; + +error_image_save: + cammy_image_destroy(image_out); + +error_image_dither: + cammy_image_destroy(image_in); + +error_image_open: + return 1; +}