cammy/bin/main.c
XANTRONIX Development 109a3a9efb meh wip
2016-06-07 00:07:58 -05:00

656 lines
16 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 "pnglite.h"
#include <cammy/image.h>
#include <cammy/screen.h>
#include <cammy/photo.h>
#include <cammy/sram.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 import file.sav 1..30 input.png\n"
" %1$s export file.sav 1..30 output.png\n"
" %1$s dither input.png output.png\n"
" %1$s split file.sav rn gn bn input.png\n"
" %1$s merge file.sav rn gn bn output.png\n"
" %1$s import-tile file.sav 1..30 printer.tile\n"
" %1$s export-tile file.sav 1..30 output.png\n"
" %1$s convert-tile printer.tile output.png\n",
argv[0]);
exit(1);
}
static int save_buf_to_png_file(const char *file,
void *buf,
size_t width,
size_t height,
int depth,
int format) {
png_t *png;
png_init(malloc, free);
if ((png = malloc(sizeof(*png))) == NULL) {
goto error_malloc_png;
}
if (png_open_file_write(png, file) < 0) {
goto error_png_open_file_write;
}
if (png_set_data(png, width, height, depth, format, buf) < 0) {
goto error_png_set_data;
}
png_close_file(png);
free(png);
return 0;
error_png_set_data:
png_close_file(png);
error_png_open_file_write:
free(png);
error_malloc_png:
return -1;
}
static int import(int argc, char **argv) {
cammy_sram *sram;
png_t *png;
int photo = 0;
uint8_t *buf;
int error;
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 photo 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;
}
png_init(malloc, free);
if ((png = malloc(sizeof(*png))) == NULL) {
goto error_malloc_png;
}
if ((error = png_open_file_read(png, argv[4])) < 0) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "png_open_file_read()", argv[4], png_error_string(error));
goto error_png_open_file_read;
}
if (png->width != CAMMY_PHOTO_WIDTH
|| png->height != CAMMY_PHOTO_HEIGHT) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], argv[4], "Invalid image dimensions");
goto error_invalid_dimensions;
}
if ((buf = malloc(png->width * png->height * png->bpp)) == NULL) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], "malloc()", strerror(errno));
goto error_malloc_buf;
}
if ((error = png_get_data(png, buf)) < 0) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "png_get_data()", argv[4], png_error_string(error));
goto error_png_get_data;
}
cammy_photo_import(&sram->data->photos[photo-1], buf, (int)png->bpp);
free(buf);
png_close_file(png);
free(png);
cammy_sram_close(sram);
return 0;
error_png_get_data:
free(buf);
error_malloc_buf:
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;
}
static int 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 (save_buf_to_png_file(argv[4], buf,
CAMMY_PHOTO_WIDTH,
CAMMY_PHOTO_HEIGHT, 8, PNG_TRUECOLOR) < 0) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "save_buf_to_png_file()", argv[4], strerror(errno));
goto error_save_buf_to_png_file;
}
free(buf);
cammy_sram_close(sram);
return 0;
error_save_buf_to_png_file:
free(buf);
error_malloc_buf:
cammy_sram_close(sram);
error_sram_open:
return 1;
}
static int dither(int argc, char **argv) {
png_t *in;
uint8_t *bufin, *bufout;
int error;
if (argc < 3) {
usage(argc, argv, "No PNG input file provided");
} else if (argc < 4) {
usage(argc, argv, "No PNG output filename provided");
}
png_init(malloc, free);
if ((in = malloc(sizeof(*in))) == NULL) {
goto error_malloc_in;
}
if ((error = png_open_file_read(in, argv[2])) < 0) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "png_open_file_read()", argv[2], png_error_string(error));
goto error_png_open_file_read;
}
if ((bufin = malloc(in->width * in->height * in->bpp)) == NULL) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], "malloc()", strerror(errno));
goto error_malloc_bufin;
}
if ((bufout = malloc(in->width * in->height * 3)) == NULL) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], "malloc()", strerror(errno));
goto error_malloc_bufout;
}
if ((error = png_get_data(in, bufin)) < 0) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "png_get_data()", argv[2], png_error_string(error));
goto error_png_get_data;
}
cammy_image_dither(bufout, bufin, in->width, in->height, in->bpp, &palette);
if (save_buf_to_png_file(argv[3], bufout,
in->width,
in->height, 8, PNG_TRUECOLOR) < 0) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "save_buf_to_png_file()", argv[3], strerror(errno));
goto error_save_buf_to_png_file;
}
png_close_file(in);
free(bufout);
free(bufin);
free(in);
return 0;
error_save_buf_to_png_file:
error_png_get_data:
free(bufout);
error_malloc_bufout:
free(bufin);
error_malloc_bufin:
png_close_file(in);
error_png_open_file_read:
free(in);
error_malloc_in:
return 1;
}
static void validate_photo_number(int argc, char **argv, int num) {
if (num < 1 || num > CAMMY_SRAM_PHOTO_COUNT) {
usage(argc, argv, "Invalid photo number");
}
}
static int split(int argc, char **argv) {
cammy_sram *sram;
png_t *png;
int photo_r = 0,
photo_g = 0,
photo_b = 0;
uint8_t *buf;
int error;
if (argc < 3) {
usage(argc, argv, "No save file provided");
} else if (argc < 4) {
usage(argc, argv, "No red photo number provided");
} else if (argc < 5) {
usage(argc, argv, "No green photo number provided");
} else if (argc < 6) {
usage(argc, argv, "No blue photo number provided");
} else if (argc < 7) {
usage(argc, argv, "No photo provided");
}
validate_photo_number(argc, argv, photo_r = atoi(argv[3]));
validate_photo_number(argc, argv, photo_g = atoi(argv[4]));
validate_photo_number(argc, argv, photo_b = atoi(argv[5]));
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;
}
png_init(malloc, free);
if ((png = malloc(sizeof(*png))) == NULL) {
goto error_malloc_png;
}
if ((error = png_open_file_read(png, argv[6])) < 0) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "png_open_file_read()", argv[6], png_error_string(error));
goto error_png_open_file_read;
}
if (png->width != CAMMY_PHOTO_WIDTH
|| png->height != CAMMY_PHOTO_HEIGHT) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], argv[6], "Invalid image dimensions");
goto error_invalid_dimensions;
}
if ((buf = malloc(png->width * png->height * png->bpp)) == NULL) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], "malloc()", strerror(errno));
goto error_malloc;
}
if ((error = png_get_data(png, buf)) < 0) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "png_get_data()", argv[4], png_error_string(error));
goto error_png_get_data;
}
cammy_photo_import_rgb(&sram->data->photos[photo_r-1],
&sram->data->photos[photo_g-1],
&sram->data->photos[photo_b-1], buf, (int)png->bpp);
free(buf);
png_close_file(png);
free(png);
cammy_sram_close(sram);
return 0;
error_png_get_data:
free(buf);
error_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;
}
static int merge(int argc, char **argv) {
cammy_sram *sram;
png_t *png;
int photo_r = 0,
photo_g = 0,
photo_b = 0;
uint8_t *buf;
int error;
if (argc < 3) {
usage(argc, argv, "No save file provided");
} else if (argc < 4) {
usage(argc, argv, "No red photo number provided");
} else if (argc < 5) {
usage(argc, argv, "No green photo number provided");
} else if (argc < 6) {
usage(argc, argv, "No blue photo number provided");
} else if (argc < 7) {
usage(argc, argv, "No photo provided");
}
validate_photo_number(argc, argv, photo_r = atoi(argv[3]));
validate_photo_number(argc, argv, photo_g = atoi(argv[4]));
validate_photo_number(argc, argv, photo_b = atoi(argv[5]));
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;
}
png_init(malloc, free);
if ((png = malloc(sizeof(*png))) == NULL) {
goto error_malloc_png;
}
if ((error = png_open_file_write(png, argv[6])) < 0) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "png_open_file_write()", argv[6], png_error_string(error));
goto error_png_open_file_write;
}
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_merge(&sram->data->photos[photo_r-1],
&sram->data->photos[photo_g-1],
&sram->data->photos[photo_b-1], buf, 3);
if ((error = png_set_data(png,
CAMMY_PHOTO_WIDTH,
CAMMY_PHOTO_HEIGHT, 8, PNG_TRUECOLOR, buf)) < 0) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "png_set_data()", argv[6], png_error_string(error));
goto error_png_set_data;
}
png_close_file(png);
free(buf);
free(png);
cammy_sram_close(sram);
return 0;
error_png_set_data:
free(buf);
error_malloc_buf:
png_close_file(png);
error_png_open_file_write:
free(png);
error_malloc_png:
cammy_sram_close(sram);
error_sram_open:
return 1;
}
static int import_tile(int argc, char **argv) {
cammy_sram *sram;
int fd;
if (argc < 2) {
usage(argc, argv, "No camera SRAM file provided");
} else if (argc < 3) {
usage(argc, argv, "No picture number provided");
} else if (argc < 4) {
usage(argc, argv, "No Game Boy screen tile data file provided");
}
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 ((fd = open(argv[4], O_RDONLY)) < 0) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "open()", argv[2], strerror(errno));
goto error_open;
}
return 0;
error_sram_open:
cammy_sram_close(sram);
return 1;
}
static int convert_tile(int argc, char **argv) {
int fd;
void *in, *out;
if (argc < 3) {
usage(argc, argv, "No input Game Boy screen tile data file provided");
} else if (argc < 4) {
usage(argc, argv, "No output PNG filename provided");
}
if ((fd = open(argv[2], O_RDONLY)) < 0) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "open()", argv[2], strerror(errno));
goto error_open;
}
if ((in = malloc(CAMMY_SCREEN_SIZE)) == NULL) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], "malloc()", strerror(errno));
goto error_malloc_in;
}
if (read(fd, in, CAMMY_SCREEN_SIZE) < 0) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "read()", argv[2], strerror(errno));
goto error_read;
}
if ((out = malloc(CAMMY_SCREEN_WIDTH * CAMMY_SCREEN_HEIGHT * 3)) == NULL) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], "malloc()", strerror(errno));
goto error_malloc_out;
}
cammy_image_copy_from_tile(out, in,
CAMMY_SCREEN_WIDTH,
CAMMY_SCREEN_HEIGHT, 3, &palette);
if (save_buf_to_png_file(argv[3], out,
CAMMY_SCREEN_WIDTH,
CAMMY_SCREEN_HEIGHT, 8, PNG_TRUECOLOR) < 0) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "save_buf_to_png_file()", argv[3], strerror(errno));
goto error_save_buf_to_png_file;
}
return 0;
error_save_buf_to_png_file:
free(out);
error_malloc_out:
error_read:
free(in);
error_malloc_in:
close(fd);
error_open:
return errno;
}
static struct {
char *name;
int (*fun)(int, char **);
} commands[] = {
{ "import", import },
{ "export", export },
{ "dither", dither },
{ "split", split },
{ "merge", merge },
{ "import-tile", import_tile },
{ "convert-tile", convert_tile },
{ NULL, NULL }
};
int main(int argc, char **argv) {
int i;
if (argc < 2) {
usage(argc, argv, "No command specified");
}
for (i=0; commands[i].name; i++) {
if (strcmp(argv[1], commands[i].name) == 0) {
return commands[i].fun(argc, argv);
}
}
usage(argc, argv, "Unknown command '%s'", argv[1]);
return 0;
}