cammy/bin/slice.c

68 lines
1.3 KiB
C
Raw Permalink Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
2016-07-06 17:12:47 -05:00
#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 "png.h"
#include "commands.h"
2016-07-06 17:12:47 -05:00
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 slice x-pad y-pad input.png\n", argv[0]);
exit(1);
}
int cammy_slice(int argc, char **argv) {
uint8_t *buf;
size_t width, height,
x_pad, y_pad;
int depth;
if (argc < 3) {
2016-07-06 17:12:47 -05:00
usage(argc, argv, "No X padding provided");
} else if (argc < 4) {
2016-07-06 17:12:47 -05:00
usage(argc, argv, "No Y padding provided");
} else if (argc < 5) {
2016-07-06 17:12:47 -05:00
usage(argc, argv, "No input PNG image provided");
}
x_pad = atoi(argv[2]);
y_pad = atoi(argv[3]);
if ((buf = cammy_png_load(argv[4], &width, &height, &depth)) == NULL) {
goto error_read_png_file;
}
if (cammy_image_slice(buf, width, height, x_pad, y_pad, depth) < 0) {
goto error_image_slice;
}
free(buf);
return 0;
error_image_slice:
free(buf);
error_read_png_file:
return 1;
}