cammy/src/test.c

122 lines
2.6 KiB
C
Raw Normal View History

2016-05-07 16:11:34 -05:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
2016-05-07 19:35:58 -05:00
#include "pnglite.h"
2016-05-07 16:11:34 -05:00
#include <cammy/sram.h>
2016-05-14 23:40:28 -05:00
#include <cammy/photo.h>
2016-05-07 16:11:34 -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");
}
2016-05-14 23:40:28 -05:00
fprintf(stderr, "usage: %s file.sav photo1..30 photo.png\n", argv[0]);
2016-05-07 16:11:34 -05:00
exit(1);
}
2016-05-11 22:34:01 -05:00
static inline uint8_t *buf_malloc(png_t *png) {
2016-05-07 19:35:58 -05:00
return malloc(4 * png->width * png->height);
}
2016-05-07 16:11:34 -05:00
int main(int argc, char **argv) {
cammy_sram *sram;
2016-05-07 19:35:58 -05:00
png_t *png;
2016-05-14 23:40:28 -05:00
int photo = 0;
2016-05-07 19:35:58 -05:00
uint8_t *buf;
int error;
2016-05-07 16:11:34 -05:00
2016-05-07 19:35:58 -05:00
if (argc < 2) {
2016-05-07 16:11:34 -05:00
usage(argc, argv, "No save file provided");
2016-05-07 19:35:58 -05:00
} else if (argc < 3) {
2016-05-14 23:40:28 -05:00
usage(argc, argv, "No photo number provided");
2016-05-07 19:35:58 -05:00
} else if (argc < 4) {
usage(argc, argv, "No photo provided");
}
2016-05-14 23:40:28 -05:00
photo = atoi(argv[2]);
2016-05-07 19:35:58 -05:00
2016-05-14 23:40:28 -05:00
if (photo < 1 || photo > CAMMY_SRAM_PHOTO_COUNT) {
usage(argc, argv, "Invalid photo number");
2016-05-07 16:11:34 -05:00
}
if ((sram = cammy_sram_open(argv[1])) == NULL) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "cammy_sram_open()", argv[1], strerror(errno));
goto error_sram_open;
}
2016-05-07 19:35:58 -05:00
png_init(malloc, free);
if ((png = malloc(sizeof(*png))) == NULL) {
goto error_malloc_png;
}
if ((error = png_open_file_read(png, argv[3])) < 0) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "png_open_file_read()", argv[3], png_error_string(error));
goto error_png_open_file_read;
}
2016-05-14 23:40:28 -05:00
if (png->width != CAMMY_PHOTO_WIDTH
|| png->height != CAMMY_PHOTO_HEIGHT) {
2016-05-07 19:35:58 -05:00
fprintf(stderr, "%s: %s: %s\n",
argv[0], argv[3], "Invalid image dimensions");
goto error_invalid_dimensions;
}
if ((buf = buf_malloc(png)) == NULL) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], "malloc()", strerror(errno));
goto error_buf_malloc;
}
if ((error = png_get_data(png, buf)) < 0) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "png_get_data()", argv[3], png_error_string(error));
goto error_png_get_data;
}
2016-05-14 23:40:28 -05:00
cammy_photo_import(&sram->data->photos[photo-1], buf);
2016-05-07 19:35:58 -05:00
free(buf);
png_close_file(png);
free(png);
2016-05-07 16:11:34 -05:00
cammy_sram_close(sram);
return 0;
2016-05-07 19:35:58 -05:00
error_png_get_data:
free(buf);
error_buf_malloc:
error_invalid_dimensions:
png_close_file(png);
error_png_open_file_read:
free(png);
error_malloc_png:
cammy_sram_close(sram);
2016-05-07 16:11:34 -05:00
error_sram_open:
return 1;
}