#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 dither 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; }