100 lines
2.2 KiB
C
100 lines
2.2 KiB
C
#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/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 }
|
|
}
|
|
};
|
|
|
|
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 export file.sav 1..30 output.png\n", argv[0]);
|
|
|
|
exit(1);
|
|
}
|
|
|
|
int cammy_export(int argc, char **argv) {
|
|
cammy_sram *sram;
|
|
int photo = 0;
|
|
uint8_t *buf;
|
|
|
|
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 output filename 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 ((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;
|
|
}
|