Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Write(const std::string& filename) {
- FILE* fp = fopen(filename.c_str(), "wb");
- if (!fp) {
- throw std::runtime_error("Can't open file " + filename);
- }
- png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
- if (!png) {
- throw std::runtime_error("Can't create png write struct");
- }
- png_infop info = png_create_info_struct(png);
- if (!info) {
- throw std::runtime_error("Can't create png info struct");
- }
- if (setjmp(png_jmpbuf(png))) {
- abort();
- }
- png_init_io(png, fp);
- // Output is 8bit depth, RGBA format.
- png_set_IHDR(png, info, width_, height_, 8, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE,
- PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
- png_write_info(png, info);
- // To remove the alpha channel for PNG_COLOR_TYPE_RGB format,
- // Use png_set_filler().
- // png_set_filler(png, 0, PNG_FILLER_AFTER);
- png_write_image(png, bytes_);
- png_write_end(png, nullptr);
- fclose(fp);
- png_destroy_write_struct(&png, &info);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement