cammy/bin/split.c

84 lines
1.9 KiB
C
Raw Permalink Normal View History

2021-12-04 17:14:47 -05:00
#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;
}