A bit less boilerplate-y
This commit is contained in:
parent
5c8783819a
commit
726e3999f2
1 changed files with 60 additions and 84 deletions
144
bin/main.c
144
bin/main.c
|
@ -44,6 +44,44 @@ static void usage(int argc, char **argv, const char *message, ...) {
|
|||
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;
|
||||
|
@ -139,12 +177,9 @@ error_sram_open:
|
|||
|
||||
static int export(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) {
|
||||
|
@ -166,19 +201,6 @@ static int export(int argc, char **argv) {
|
|||
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[4])) < 0) {
|
||||
fprintf(stderr, "%s: %s: %s: %s\n",
|
||||
argv[0], "png_open_file_write()", argv[4], 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));
|
||||
|
@ -188,33 +210,25 @@ static int export(int argc, char **argv) {
|
|||
|
||||
cammy_photo_export(&sram->data->photos[photo-1], buf, 3, &palette);
|
||||
|
||||
if ((error = png_set_data(png, CAMMY_PHOTO_WIDTH, CAMMY_PHOTO_HEIGHT, 8, PNG_TRUECOLOR, buf)) < 0) {
|
||||
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], "png_set_data()", argv[4], png_error_string(error));
|
||||
argv[0], "save_buf_to_png_file()", argv[4], strerror(errno));
|
||||
|
||||
goto error_png_set_data;
|
||||
goto error_save_buf_to_png_file;
|
||||
}
|
||||
|
||||
png_close_file(png);
|
||||
|
||||
free(buf);
|
||||
|
||||
free(png);
|
||||
|
||||
cammy_sram_close(sram);
|
||||
|
||||
return 0;
|
||||
|
||||
error_png_set_data:
|
||||
error_save_buf_to_png_file:
|
||||
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:
|
||||
|
@ -222,7 +236,7 @@ error_sram_open:
|
|||
}
|
||||
|
||||
static int dither(int argc, char **argv) {
|
||||
png_t *in, *out;
|
||||
png_t *in;
|
||||
uint8_t *bufin, *bufout;
|
||||
|
||||
int error;
|
||||
|
@ -239,10 +253,6 @@ static int dither(int argc, char **argv) {
|
|||
goto error_malloc_in;
|
||||
}
|
||||
|
||||
if ((out = malloc(sizeof(*in))) == NULL) {
|
||||
goto error_malloc_out;
|
||||
}
|
||||
|
||||
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));
|
||||
|
@ -250,13 +260,6 @@ static int dither(int argc, char **argv) {
|
|||
goto error_png_open_file_read;
|
||||
}
|
||||
|
||||
if ((error = png_open_file_write(out, argv[3])) < 0) {
|
||||
fprintf(stderr, "%s: %s: %s: %s\n",
|
||||
argv[0], "png_open_file_write()", argv[3], png_error_string(error));
|
||||
|
||||
goto error_png_open_file_write;
|
||||
}
|
||||
|
||||
if ((bufin = malloc(in->width * in->height * in->bpp)) == NULL) {
|
||||
fprintf(stderr, "%s: %s: %s\n",
|
||||
argv[0], "malloc()", strerror(errno));
|
||||
|
@ -280,28 +283,26 @@ static int dither(int argc, char **argv) {
|
|||
|
||||
cammy_image_dither(bufout, bufin, in->width, in->height, in->bpp, &palette);
|
||||
|
||||
if ((error = png_set_data(out, in->width, in->height, 8, PNG_TRUECOLOR, bufout)) < 0) {
|
||||
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], "png_set_data()", argv[4], png_error_string(error));
|
||||
argv[0], "save_buf_to_png_file()", argv[3], strerror(errno));
|
||||
|
||||
goto error_png_set_data;
|
||||
goto error_save_buf_to_png_file;
|
||||
}
|
||||
|
||||
png_close_file(out);
|
||||
|
||||
png_close_file(in);
|
||||
|
||||
free(bufout);
|
||||
|
||||
free(bufin);
|
||||
|
||||
free(out);
|
||||
|
||||
free(in);
|
||||
|
||||
return 0;
|
||||
|
||||
error_png_set_data:
|
||||
error_save_buf_to_png_file:
|
||||
error_png_get_data:
|
||||
free(bufout);
|
||||
|
||||
|
@ -309,15 +310,9 @@ error_malloc_bufout:
|
|||
free(bufin);
|
||||
|
||||
error_malloc_bufin:
|
||||
png_close_file(out);
|
||||
|
||||
error_png_open_file_write:
|
||||
png_close_file(in);
|
||||
|
||||
error_png_open_file_read:
|
||||
free(out);
|
||||
|
||||
error_malloc_out:
|
||||
free(in);
|
||||
|
||||
error_malloc_in:
|
||||
|
@ -490,7 +485,9 @@ static int merge(int argc, char **argv) {
|
|||
&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) {
|
||||
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));
|
||||
|
||||
|
@ -526,8 +523,6 @@ error_sram_open:
|
|||
static int convert(int argc, char **argv) {
|
||||
int fd;
|
||||
void *in, *out;
|
||||
png_t *png;
|
||||
int error;
|
||||
|
||||
if (argc < 3) {
|
||||
usage(argc, argv, "No input Game Boy screen tile data file provided");
|
||||
|
@ -563,41 +558,22 @@ static int convert(int argc, char **argv) {
|
|||
goto error_malloc_out;
|
||||
}
|
||||
|
||||
png_init(malloc, free);
|
||||
|
||||
if ((png = malloc(sizeof(*png))) == NULL) {
|
||||
goto error_malloc_png;
|
||||
}
|
||||
|
||||
if ((error = png_open_file_write(png, argv[3])) < 0) {
|
||||
fprintf(stderr, "%s: %s: %s: %s\n",
|
||||
argv[0], "png_open_file_write()", argv[3], png_error_string(error));
|
||||
|
||||
goto error_png_open_file_write;
|
||||
}
|
||||
|
||||
cammy_image_copy_from_tile(out, in,
|
||||
CAMMY_SCREEN_WIDTH,
|
||||
CAMMY_SCREEN_HEIGHT, 3, &palette);
|
||||
|
||||
if ((error = png_set_data(png, CAMMY_SCREEN_WIDTH, CAMMY_SCREEN_HEIGHT, 8, PNG_TRUECOLOR, out)) < 0) {
|
||||
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], "png_set_data()", argv[3], png_error_string(error));
|
||||
argv[0], "save_buf_to_png_file()", argv[3], strerror(errno));
|
||||
|
||||
goto error_png_set_data;
|
||||
goto error_save_buf_to_png_file;
|
||||
}
|
||||
|
||||
png_close_file(png);
|
||||
|
||||
return 0;
|
||||
|
||||
error_png_set_data:
|
||||
png_close_file(png);
|
||||
|
||||
error_png_open_file_write:
|
||||
free(png);
|
||||
|
||||
error_malloc_png:
|
||||
error_save_buf_to_png_file:
|
||||
free(out);
|
||||
|
||||
error_malloc_out:
|
||||
|
|
Loading…
Add table
Reference in a new issue