cammy/bin/export.c
2016-07-04 19:48:48 -05:00

85 lines
1.9 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <cammy/photo.h>
#include <cammy/sram.h>
#include "pnglite.h"
#include "png.h"
#include "commands.h"
static cammy_tile_palette palette = {
.colors = {
{ 0, 0, 0 },
{ 85, 85, 85 },
{ 171, 171, 171 },
{ 255, 255, 255 }
}
};
int cammy_command_export(int argc, char **argv) {
cammy_sram *sram;
int photo = 0;
uint8_t *buf;
if (argc < 3) {
cammy_command_usage(argc, argv, "No save file provided");
} else if (argc < 4) {
cammy_command_usage(argc, argv, "No photo number provided");
} else if (argc < 5) {
cammy_command_usage(argc, argv, "No output filename provided");
}
photo = atoi(argv[3]);
if (photo < 1 || photo > CAMMY_SRAM_PHOTO_COUNT) {
cammy_command_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 ((buf = malloc(CAMMY_PHOTO_WIDTH * CAMMY_PHOTO_HEIGHT * 3)) == NULL) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], "malloc()", strerror(errno));
goto error_malloc_buf;
}
cammy_photo_export(&sram->data->photos[photo-1], buf, 3, &palette);
if (cammy_png_save(argv[4], buf,
CAMMY_PHOTO_WIDTH,
CAMMY_PHOTO_HEIGHT, 8, PNG_TRUECOLOR) < 0) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "cammy_png_save()", argv[4], strerror(errno));
goto error_png_save;
}
free(buf);
cammy_sram_close(sram);
return 0;
error_png_save:
free(buf);
error_malloc_buf:
cammy_sram_close(sram);
error_sram_open:
return 1;
}