image_loader_png.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include "image_loader_png.h"
  31. #include "core/os/os.h"
  32. #include "core/print_string.h"
  33. #include "drivers/png/png_driver_common.h"
  34. #include <string.h>
  35. Error ImageLoaderPNG::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) {
  36. const uint64_t buffer_size = f->get_len();
  37. PoolVector<uint8_t> file_buffer;
  38. Error err = file_buffer.resize(buffer_size);
  39. if (err) {
  40. f->close();
  41. return err;
  42. }
  43. {
  44. PoolVector<uint8_t>::Write writer = file_buffer.write();
  45. f->get_buffer(writer.ptr(), buffer_size);
  46. f->close();
  47. }
  48. PoolVector<uint8_t>::Read reader = file_buffer.read();
  49. return PNGDriverCommon::png_to_image(reader.ptr(), buffer_size, p_force_linear, p_image);
  50. }
  51. void ImageLoaderPNG::get_recognized_extensions(List<String> *p_extensions) const {
  52. p_extensions->push_back("png");
  53. }
  54. Ref<Image> ImageLoaderPNG::load_mem_png(const uint8_t *p_png, int p_size) {
  55. Ref<Image> img;
  56. img.instance();
  57. // the value of p_force_linear does not matter since it only applies to 16 bit
  58. Error err = PNGDriverCommon::png_to_image(p_png, p_size, false, img);
  59. ERR_FAIL_COND_V(err, Ref<Image>());
  60. return img;
  61. }
  62. Ref<Image> ImageLoaderPNG::lossless_unpack_png(const PoolVector<uint8_t> &p_data) {
  63. const int len = p_data.size();
  64. ERR_FAIL_COND_V(len < 4, Ref<Image>());
  65. PoolVector<uint8_t>::Read r = p_data.read();
  66. ERR_FAIL_COND_V(r[0] != 'P' || r[1] != 'N' || r[2] != 'G' || r[3] != ' ', Ref<Image>());
  67. return load_mem_png(&r[4], len - 4);
  68. }
  69. PoolVector<uint8_t> ImageLoaderPNG::lossless_pack_png(const Ref<Image> &p_image) {
  70. PoolVector<uint8_t> out_buffer;
  71. // add Godot's own "PNG " prefix
  72. if (out_buffer.resize(4) != OK) {
  73. ERR_FAIL_V(PoolVector<uint8_t>());
  74. }
  75. // scope for writer lifetime
  76. {
  77. // must be closed before call to image_to_png
  78. PoolVector<uint8_t>::Write writer = out_buffer.write();
  79. memcpy(writer.ptr(), "PNG ", 4);
  80. }
  81. Error err = PNGDriverCommon::image_to_png(p_image, out_buffer);
  82. if (err) {
  83. ERR_FAIL_V(PoolVector<uint8_t>());
  84. }
  85. return out_buffer;
  86. }
  87. ImageLoaderPNG::ImageLoaderPNG() {
  88. Image::_png_mem_loader_func = load_mem_png;
  89. Image::png_unpacker = lossless_unpack_png;
  90. Image::png_packer = lossless_pack_png;
  91. }