#include #include #include #include #include #include "pnglite.h" #include 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: %s file.sav frame1..30 photo.png\n", argv[0]); exit(1); } static uint8_t *buf_malloc(png_t *png) { return malloc(4 * png->width * png->height); } static void image_copy(cammy_sram_frame *frame, uint8_t *buf) { size_t x, y; memset(&frame->photo, '\0', sizeof(cammy_sram_photo)); for (y=0; yphoto.tiles[y>>3][x>>3]; int tile_x = x & 7, tile_y = y & 7; tile->data[2*tile_y] |= ((value & 0x02) >> 1) << (7 - tile_x); tile->data[2*tile_y+1] |= (value & 0x01) << (7 - tile_x); } } } int main(int argc, char **argv) { cammy_sram *sram; png_t *png; int frame = 0; uint8_t *buf; int error; if (argc < 2) { usage(argc, argv, "No save file provided"); } else if (argc < 3) { usage(argc, argv, "No frame number provided"); } else if (argc < 4) { usage(argc, argv, "No photo provided"); } frame = atoi(argv[2]); if (frame < 1 || frame > CAMMY_SRAM_FRAME_COUNT) { usage(argc, argv, "Invalid frame number"); } 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; } 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; } if (png->width != CAMMY_SRAM_PHOTO_WIDTH || png->height != CAMMY_SRAM_PHOTO_HEIGHT) { 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; } image_copy(&sram->data->frames[frame], buf); free(buf); png_close_file(png); free(png); cammy_sram_close(sram); return 0; 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); error_sram_open: return 1; }