Implement 'cammy split' command

This commit is contained in:
XANTRONIX Development 2021-12-04 17:14:47 -05:00
parent 8c80aaf5c8
commit c9f370996a
4 changed files with 86 additions and 3 deletions

View file

@ -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

View file

@ -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);

View file

@ -14,6 +14,7 @@ static struct {
{ "import", cammy_import },
{ "export", cammy_export },
{ "dither", cammy_dither },
{ "split", cammy_split },
{ NULL, NULL }
};

83
bin/split.c Normal file
View file

@ -0,0 +1,83 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <cammy/image.h>
#include <cammy/photo.h>
#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;
}