/**************************************************************************/1/* image_loader_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 "image_loader_png.h"3132#include "drivers/png/png_driver_common.h"3334Error ImageLoaderPNG::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) {35const uint64_t buffer_size = f->get_length();36Vector<uint8_t> file_buffer;37Error err = file_buffer.resize(buffer_size);38if (err) {39return err;40}41{42uint8_t *writer = file_buffer.ptrw();43f->get_buffer(writer, buffer_size);44}45const uint8_t *reader = file_buffer.ptr();46return PNGDriverCommon::png_to_image(reader, buffer_size, p_flags & FLAG_FORCE_LINEAR, p_image);47}4849void ImageLoaderPNG::get_recognized_extensions(List<String> *p_extensions) const {50p_extensions->push_back("png");51}5253Ref<Image> ImageLoaderPNG::load_mem_png(const uint8_t *p_png, int p_size) {54Ref<Image> img;55img.instantiate();5657// the value of p_force_linear does not matter since it only applies to 16 bit58Error err = PNGDriverCommon::png_to_image(p_png, p_size, false, img);59ERR_FAIL_COND_V(err, Ref<Image>());6061return img;62}6364Ref<Image> ImageLoaderPNG::unpack_mem_png(const uint8_t *p_png, int p_size) {65ERR_FAIL_COND_V(p_size < 4, Ref<Image>());66ERR_FAIL_COND_V(p_png[0] != 'P' || p_png[1] != 'N' || p_png[2] != 'G' || p_png[3] != ' ', Ref<Image>());67return load_mem_png(&p_png[4], p_size - 4);68}6970Ref<Image> ImageLoaderPNG::lossless_unpack_png(const Vector<uint8_t> &p_data) {71return unpack_mem_png(p_data.ptr(), p_data.size());72}7374Vector<uint8_t> ImageLoaderPNG::lossless_pack_png(const Ref<Image> &p_image) {75Vector<uint8_t> out_buffer;7677// add Godot's own "PNG " prefix78if (out_buffer.resize(4) != OK) {79ERR_FAIL_V(Vector<uint8_t>());80}8182// scope for writer lifetime83{84// must be closed before call to image_to_png85uint8_t *writer = out_buffer.ptrw();86memcpy(writer, "PNG ", 4);87}8889Error err = PNGDriverCommon::image_to_png(p_image, out_buffer);90if (err) {91ERR_FAIL_V(Vector<uint8_t>());92}9394return out_buffer;95}9697ImageLoaderPNG::ImageLoaderPNG() {98Image::_png_mem_loader_func = load_mem_png;99Image::_png_mem_unpacker_func = unpack_mem_png;100Image::png_unpacker = lossless_unpack_png;101Image::png_packer = lossless_pack_png;102}103104105