cammy/bin/import.c
XANTRONIX Development 05fd969619 Kickass
2021-11-30 14:10:12 -05:00

84 lines
1.8 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <cammy/photo.h>
#include <cammy/sram.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 import file.sav 1..30 input.png\n", argv[0]);
exit(1);
}
int cammy_import(int argc, char **argv) {
cammy_sram *sram;
cammy_image *image;
int photo = 0;
if (argc < 3) {
usage(argc, argv, "No save file provided");
} else if (argc < 4) {
usage(argc, argv, "No photo number provided");
} else if (argc < 5) {
usage(argc, argv, "No photo provided");
}
photo = atoi(argv[3]);
if (photo < 1 || photo > CAMMY_SRAM_PHOTO_COUNT) {
usage(argc, argv, "Invalid photo number");
}
if ((sram = cammy_sram_open(argv[2])) == NULL) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "cammy_sram_open()", argv[2], strerror(errno));
goto error_sram_open;
}
if ((image = cammy_image_open(argv[4])) == NULL) {
goto error_image_open;
}
if (image->width != CAMMY_PHOTO_WIDTH
|| image->height != CAMMY_PHOTO_HEIGHT) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], argv[4], "Invalid image dimensions");
goto error_invalid_dimensions;
}
if (cammy_photo_import(&sram->data->photos[photo-1], image) < 0) {
goto error_photo_import;
}
cammy_image_destroy(image);
cammy_sram_close(sram);
return 0;
error_photo_import:
error_invalid_dimensions:
cammy_image_destroy(image);
error_image_open:
cammy_sram_close(sram);
error_sram_open:
return 1;
}