0.2.0-dev Download ZIP
100 lines ·4.3 KB Raw
| 1 | /**************************************************************************/ |
| 2 | /* image_loader_png.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "image_loader_png.h" |
| 32 | |
| 33 | #include "drivers/png/png_driver_common.h" |
| 34 | |
| 35 | Error ImageLoaderPNG::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) { |
| 36 | const uint64_t buffer_size = f->get_length(); |
| 37 | Vector<uint8_t> file_buffer; |
| 38 | RETURN_IF_ERROR(file_buffer.resize(buffer_size)); |
| 39 | { |
| 40 | uint8_t *writer = file_buffer.ptrw(); |
| 41 | f->get_buffer(writer, buffer_size); |
| 42 | } |
| 43 | const uint8_t *reader = file_buffer.ptr(); |
| 44 | return PNGDriverCommon::png_to_image(reader, buffer_size, p_flags & FLAG_FORCE_LINEAR, p_image); |
| 45 | } |
| 46 | |
| 47 | void ImageLoaderPNG::get_recognized_extensions(List<String> *p_extensions) const { |
| 48 | p_extensions->push_back("png"); |
| 49 | } |
| 50 | |
| 51 | Ref<Image> ImageLoaderPNG::load_mem_png(const uint8_t *p_png, int p_size) { |
| 52 | Ref<Image> img; |
| 53 | img.instantiate(); |
| 54 | |
| 55 | // the value of p_force_linear does not matter since it only applies to 16 bit |
| 56 | Error err = PNGDriverCommon::png_to_image(p_png, p_size, false, img); |
| 57 | ERR_FAIL_COND_V(err, Ref<Image>()); |
| 58 | |
| 59 | return img; |
| 60 | } |
| 61 | |
| 62 | Ref<Image> ImageLoaderPNG::unpack_mem_png(const uint8_t *p_png, int p_size) { |
| 63 | ERR_FAIL_COND_V(p_size < 4, Ref<Image>()); |
| 64 | ERR_FAIL_COND_V(p_png[0] != 'P' || p_png[1] != 'N' || p_png[2] != 'G' || p_png[3] != ' ', Ref<Image>()); |
| 65 | return load_mem_png(&p_png[4], p_size - 4); |
| 66 | } |
| 67 | |
| 68 | Ref<Image> ImageLoaderPNG::lossless_unpack_png(const Vector<uint8_t> &p_data) { |
| 69 | return unpack_mem_png(p_data.ptr(), p_data.size()); |
| 70 | } |
| 71 | |
| 72 | Vector<uint8_t> ImageLoaderPNG::lossless_pack_png(const Ref<Image> &p_image) { |
| 73 | Vector<uint8_t> out_buffer; |
| 74 | |
| 75 | // add Godot's own "PNG " prefix |
| 76 | if (out_buffer.resize(4) != OK) { |
| 77 | ERR_FAIL_V(Vector<uint8_t>()); |
| 78 | } |
| 79 | |
| 80 | // scope for writer lifetime |
| 81 | { |
| 82 | // must be closed before call to image_to_png |
| 83 | uint8_t *writer = out_buffer.ptrw(); |
| 84 | memcpy(writer, "PNG ", 4); |
| 85 | } |
| 86 | |
| 87 | Error err = PNGDriverCommon::image_to_png(p_image, out_buffer); |
| 88 | if (err) { |
| 89 | ERR_FAIL_V(Vector<uint8_t>()); |
| 90 | } |
| 91 | |
| 92 | return out_buffer; |
| 93 | } |
| 94 | |
| 95 | ImageLoaderPNG::ImageLoaderPNG() { |
| 96 | Image::_png_mem_loader_func = load_mem_png; |
| 97 | Image::_png_mem_unpacker_func = unpack_mem_png; |
| 98 | Image::png_unpacker = lossless_unpack_png; |
| 99 | Image::png_packer = lossless_pack_png; |
| 100 | } |