cammy/src/test.c

148 lines
3.5 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>
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-07 19:35:58 -05:00
fprintf(stderr, "usage: %s file.sav frame1..30 photo.png\n", argv[0]);
2016-05-07 16:11:34 -05:00
exit(1);
}
2016-05-07 19:35:58 -05:00
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;
2016-05-07 20:43:41 -05:00
memset(&frame->thumb, '\xff', sizeof(cammy_sram_thumb));
memset(&frame->photo, '\x00', sizeof(cammy_sram_photo));
2016-05-07 19:35:58 -05:00
for (y=0; y<CAMMY_SRAM_PHOTO_HEIGHT; y++) {
for (x=0; x<CAMMY_SRAM_PHOTO_WIDTH; x++) {
uint8_t r = buf[4*CAMMY_SRAM_PHOTO_WIDTH*y+4*x],
g = buf[4*CAMMY_SRAM_PHOTO_WIDTH*y+4*x+1],
b = buf[4*CAMMY_SRAM_PHOTO_WIDTH*y+4*x+2];
float avg = 255.0 - ((float)r + (float)g + (float)b) / 3;
uint8_t value = (uint8_t)(avg / 64.0);
cammy_sram_tile *tile = &frame->photo.tiles[y>>3][x>>3];
int tile_x = x & 7,
tile_y = y & 7;
tile->data[2*tile_y] |= (value & 0x01) << (7 - tile_x);
tile->data[2*tile_y+1] |= ((value & 0x02) >> 1) << (7 - tile_x);
2016-05-07 19:35:58 -05:00
}
}
}
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;
int frame = 0;
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) {
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");
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;
}
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;
}
2016-05-07 19:52:55 -05:00
image_copy(&sram->data->frames[frame-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;
}