/**************************************************************************/1/* resource_saver_png.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "resource_saver_png.h"3132#include "core/io/file_access.h"33#include "core/io/image.h"34#include "drivers/png/png_driver_common.h"35#include "scene/resources/image_texture.h"3637Error ResourceSaverPNG::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {38Ref<ImageTexture> texture = p_resource;3940ERR_FAIL_COND_V_MSG(texture.is_null(), ERR_INVALID_PARAMETER, "Can't save invalid texture as PNG.");41ERR_FAIL_COND_V_MSG(!texture->get_width(), ERR_INVALID_PARAMETER, "Can't save empty texture as PNG.");4243Ref<Image> img = texture->get_image();4445Error err = save_image(p_path, img);4647return err;48}4950Error ResourceSaverPNG::save_image(const String &p_path, const Ref<Image> &p_img) {51Vector<uint8_t> buffer;52Error err = PNGDriverCommon::image_to_png(p_img, buffer);53ERR_FAIL_COND_V_MSG(err, err, "Can't convert image to PNG.");54Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);55ERR_FAIL_COND_V_MSG(err, err, vformat("Can't save PNG at path: '%s'.", p_path));5657const uint8_t *reader = buffer.ptr();5859file->store_buffer(reader, buffer.size());60if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) {61return ERR_CANT_CREATE;62}6364return OK;65}6667Vector<uint8_t> ResourceSaverPNG::save_image_to_buffer(const Ref<Image> &p_img) {68Vector<uint8_t> buffer;69Error err = PNGDriverCommon::image_to_png(p_img, buffer);70ERR_FAIL_COND_V_MSG(err, Vector<uint8_t>(), "Can't convert image to PNG.");71return buffer;72}7374bool ResourceSaverPNG::recognize(const Ref<Resource> &p_resource) const {75return (p_resource.is_valid() && p_resource->is_class("ImageTexture"));76}7778void ResourceSaverPNG::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const {79if (Object::cast_to<ImageTexture>(*p_resource)) {80p_extensions->push_back("png");81}82}8384ResourceSaverPNG::ResourceSaverPNG() {85Image::save_png_func = &save_image;86Image::save_png_buffer_func = &save_image_to_buffer;87}888990