From c9f370996a90345d3a77649f5c144694f95edcc8 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Sat, 4 Dec 2021 17:14:47 -0500 Subject: [PATCH] Implement 'cammy split' command --- bin/Makefile | 2 +- bin/commands.h | 3 +- bin/main.c | 1 + bin/split.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 bin/split.c diff --git a/bin/Makefile b/bin/Makefile index df04f6d..19bd7ba 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -8,7 +8,7 @@ CFLAGS += -fPIC -Wall -O2 -I$(INCLUDE_PATH) LDFLAGS = CAMMY = cammy -CAMMY_OBJS = main.o export.o import.o dither.o +CAMMY_OBJS = main.o export.o import.o dither.o split.o CAMMY_HEADERS = commands.h diff --git a/bin/commands.h b/bin/commands.h index c500a43..b055f6b 100644 --- a/bin/commands.h +++ b/bin/commands.h @@ -8,8 +8,7 @@ void cammy_validate_photo_number(int argc, char **argv, int num); int cammy_import(int argc, char **argv); int cammy_export(int argc, char **argv); int cammy_dither(int argc, char **argv); -int cammy_rgb_split(int argc, char **argv); -int cammy_rgb_merge(int argc, char **argv); +int cammy_split(int argc, char **argv); int cammy_tile_import(int argc, char **argv); int cammy_tile_export(int argc, char **argv); int cammy_tile_convert(int argc, char **argv); diff --git a/bin/main.c b/bin/main.c index fe8a5c9..0af8204 100644 --- a/bin/main.c +++ b/bin/main.c @@ -14,6 +14,7 @@ static struct { { "import", cammy_import }, { "export", cammy_export }, { "dither", cammy_dither }, + { "split", cammy_split }, { NULL, NULL } }; diff --git a/bin/split.c b/bin/split.c new file mode 100644 index 0000000..ee6f6c6 --- /dev/null +++ b/bin/split.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "commands.h" + +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 split input.png output-r.png output-g.png output-b.png\n", argv[0]); + + exit(1); +} + +int cammy_split(int argc, char **argv) { + cammy_image *image_in, + *image_out_r, + *image_out_g, + *image_out_b; + + if (argc < 3) { + usage(argc, argv, "No PNG input file provided"); + } else if (argc < 6) { + usage(argc, argv, "Not enough PNG output filenames provided"); + } + + if ((image_in = cammy_image_open(argv[2])) == NULL) { + goto error_image_open; + } + + if (cammy_image_split(image_in, + &image_out_r, + &image_out_g, + &image_out_b) < 0) { + goto error_image_split; + } + + if (cammy_image_save(image_out_r, argv[3]) < 0) { + goto error_image_save; + } + + if (cammy_image_save(image_out_g, argv[4]) < 0) { + goto error_image_save; + } + + if (cammy_image_save(image_out_b, argv[5]) < 0) { + goto error_image_save; + } + + cammy_image_destroy(image_out_b); + cammy_image_destroy(image_out_g); + cammy_image_destroy(image_out_r); + cammy_image_destroy(image_in); + + return 0; + +error_image_save: + cammy_image_destroy(image_out_r); + cammy_image_destroy(image_out_g); + cammy_image_destroy(image_out_b); + +error_image_split: + cammy_image_destroy(image_in); + +error_image_open: + return 1; +}