Allow piping image to stdout

This commit is contained in:
2021-04-21 22:22:57 -04:00
parent 00aed907c6
commit 585d0bde25

View File

@@ -93,11 +93,15 @@ int writePNG(mazegrid_t const* g, char const* filename) {
png_structp png_ptr = NULL; png_structp png_ptr = NULL;
png_infop info_ptr = NULL; png_infop info_ptr = NULL;
f = fopen(filename, "wb"); if (strcmp(filename, "-") == 0) {
if (NULL == f) { f = stdout;
fprintf(stderr, "Could not open %s for writing.\n", filename); } else {
code = 0; f = fopen(filename, "wb");
goto exit; if (NULL == f) {
fprintf(stderr, "Could not open %s for writing.\n", filename);
code = 0;
goto exit;
}
} }
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
@@ -134,7 +138,9 @@ int writePNG(mazegrid_t const* g, char const* filename) {
png_write_end(png_ptr, NULL); png_write_end(png_ptr, NULL);
exit: exit:
if (f != NULL) fclose(f); if (strcmp(filename, "-") != 0) {
if (f != NULL) fclose(f);
}
if (info_ptr != NULL) { if (info_ptr != NULL) {
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
png_destroy_info_struct(png_ptr, &info_ptr); png_destroy_info_struct(png_ptr, &info_ptr);