bool GdImageRenderer::saveAsPng( const char* filename, const int compression_level) const { bool success = true; FILE* output_file = fopen(filename, "wb"); if (output_file != nullptr) { output_stream << "Writing PNG file: " << filename << '\n'; gdImagePngEx(image_, output_file, compression_level); fclose(output_file); output_file = nullptr; } else { error_stream << "Failed to write PNG file: " << filename << '\n' << strerror(errno) << '\n'; success = false; } return success; }
int fswc_output(fswebcam_config_t *config, char *name, gdImage *image) { char filename[FILENAME_MAX]; gdImage *im; FILE *f; if(!name) return(-1); if(!strncmp(name, "-", 2) && config->background) { ERROR("stdout is unavailable in background mode."); return(-1); } fswc_strftime(filename, FILENAME_MAX, name, config->start, config->gmt); /* Create a temporary image buffer. */ im = fswc_gdImageDuplicate(image); if(!im) { ERROR("Out of memory."); return(-1); } /* Draw the underlay. */ fswc_draw_overlay(config, config->underlay, im); /* Draw the banner. */ if(config->banner != NO_BANNER) { char *err; /* Check if drawing text works */ err = gdImageStringFT(NULL, NULL, 0, config->font, config->fontsize, 0.0, 0, 0, ""); if(!err) fswc_draw_banner(config, im); else { /* Can't load the font - display a warning */ WARN("Unable to load font '%s': %s", config->font, err); WARN("Disabling the the banner."); } } /* Draw the overlay. */ fswc_draw_overlay(config, config->overlay, im); /* Write to a file if a filename was given, otherwise stdout. */ if(strncmp(name, "-", 2)) f = fopen(filename, "wb"); else f = stdout; if(!f) { ERROR("Error opening file for output: %s", filename); ERROR("fopen: %s", strerror(errno)); gdImageDestroy(im); return(-1); } /* Write the compressed image. */ switch(config->format) { case FORMAT_JPEG: MSG("Writing JPEG image to '%s'.", filename); gdImageJpeg(im, f, config->compression); break; case FORMAT_PNG: MSG("Writing PNG image to '%s'.", filename); gdImagePngEx(im, f, config->compression); break; } if(f != stdout) fclose(f); gdImageDestroy(im); return(0); }