image_loader_webp.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*************************************************************************/
  2. /* image_loader_webp.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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_webp.h"
  31. #include "core/io/marshalls.h"
  32. #include "core/os/os.h"
  33. #include "core/string/print_string.h"
  34. #include <stdlib.h>
  35. #include <webp/decode.h>
  36. #include <webp/encode.h>
  37. static Vector<uint8_t> _webp_lossy_pack(const Ref<Image> &p_image, float p_quality) {
  38. ERR_FAIL_COND_V(p_image.is_null() || p_image->is_empty(), Vector<uint8_t>());
  39. Ref<Image> img = p_image->duplicate();
  40. if (img->detect_alpha()) {
  41. img->convert(Image::FORMAT_RGBA8);
  42. } else {
  43. img->convert(Image::FORMAT_RGB8);
  44. }
  45. Size2 s(img->get_width(), img->get_height());
  46. Vector<uint8_t> data = img->get_data();
  47. const uint8_t *r = data.ptr();
  48. uint8_t *dst_buff = nullptr;
  49. size_t dst_size = 0;
  50. if (img->get_format() == Image::FORMAT_RGB8) {
  51. dst_size = WebPEncodeRGB(r, s.width, s.height, 3 * s.width, CLAMP(p_quality * 100.0, 0, 100.0), &dst_buff);
  52. } else {
  53. dst_size = WebPEncodeRGBA(r, s.width, s.height, 4 * s.width, CLAMP(p_quality * 100.0, 0, 100.0), &dst_buff);
  54. }
  55. ERR_FAIL_COND_V(dst_size == 0, Vector<uint8_t>());
  56. Vector<uint8_t> dst;
  57. dst.resize(4 + dst_size);
  58. uint8_t *w = dst.ptrw();
  59. w[0] = 'W';
  60. w[1] = 'E';
  61. w[2] = 'B';
  62. w[3] = 'P';
  63. copymem(&w[4], dst_buff, dst_size);
  64. free(dst_buff);
  65. return dst;
  66. }
  67. static Ref<Image> _webp_lossy_unpack(const Vector<uint8_t> &p_buffer) {
  68. int size = p_buffer.size() - 4;
  69. ERR_FAIL_COND_V(size <= 0, Ref<Image>());
  70. const uint8_t *r = p_buffer.ptr();
  71. ERR_FAIL_COND_V(r[0] != 'W' || r[1] != 'E' || r[2] != 'B' || r[3] != 'P', Ref<Image>());
  72. WebPBitstreamFeatures features;
  73. if (WebPGetFeatures(&r[4], size, &features) != VP8_STATUS_OK) {
  74. ERR_FAIL_V_MSG(Ref<Image>(), "Error unpacking WEBP image.");
  75. }
  76. /*
  77. print_line("width: "+itos(features.width));
  78. print_line("height: "+itos(features.height));
  79. print_line("alpha: "+itos(features.has_alpha));
  80. */
  81. Vector<uint8_t> dst_image;
  82. int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
  83. dst_image.resize(datasize);
  84. uint8_t *dst_w = dst_image.ptrw();
  85. bool errdec = false;
  86. if (features.has_alpha) {
  87. errdec = WebPDecodeRGBAInto(&r[4], size, dst_w, datasize, 4 * features.width) == nullptr;
  88. } else {
  89. errdec = WebPDecodeRGBInto(&r[4], size, dst_w, datasize, 3 * features.width) == nullptr;
  90. }
  91. ERR_FAIL_COND_V_MSG(errdec, Ref<Image>(), "Failed decoding WebP image.");
  92. Ref<Image> img = memnew(Image(features.width, features.height, 0, features.has_alpha ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8, dst_image));
  93. return img;
  94. }
  95. Error webp_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p_buffer_len) {
  96. ERR_FAIL_NULL_V(p_image, ERR_INVALID_PARAMETER);
  97. WebPBitstreamFeatures features;
  98. if (WebPGetFeatures(p_buffer, p_buffer_len, &features) != VP8_STATUS_OK) {
  99. ERR_FAIL_V(ERR_FILE_CORRUPT);
  100. }
  101. Vector<uint8_t> dst_image;
  102. int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
  103. dst_image.resize(datasize);
  104. uint8_t *dst_w = dst_image.ptrw();
  105. bool errdec = false;
  106. if (features.has_alpha) {
  107. errdec = WebPDecodeRGBAInto(p_buffer, p_buffer_len, dst_w, datasize, 4 * features.width) == nullptr;
  108. } else {
  109. errdec = WebPDecodeRGBInto(p_buffer, p_buffer_len, dst_w, datasize, 3 * features.width) == nullptr;
  110. }
  111. ERR_FAIL_COND_V_MSG(errdec, ERR_FILE_CORRUPT, "Failed decoding WebP image.");
  112. p_image->create(features.width, features.height, false, features.has_alpha ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8, dst_image);
  113. return OK;
  114. }
  115. static Ref<Image> _webp_mem_loader_func(const uint8_t *p_png, int p_size) {
  116. Ref<Image> img;
  117. img.instance();
  118. Error err = webp_load_image_from_buffer(img.ptr(), p_png, p_size);
  119. ERR_FAIL_COND_V(err, Ref<Image>());
  120. return img;
  121. }
  122. Error ImageLoaderWEBP::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) {
  123. Vector<uint8_t> src_image;
  124. int src_image_len = f->get_len();
  125. ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
  126. src_image.resize(src_image_len);
  127. uint8_t *w = src_image.ptrw();
  128. f->get_buffer(&w[0], src_image_len);
  129. f->close();
  130. Error err = webp_load_image_from_buffer(p_image.ptr(), w, src_image_len);
  131. return err;
  132. }
  133. void ImageLoaderWEBP::get_recognized_extensions(List<String> *p_extensions) const {
  134. p_extensions->push_back("webp");
  135. }
  136. ImageLoaderWEBP::ImageLoaderWEBP() {
  137. Image::_webp_mem_loader_func = _webp_mem_loader_func;
  138. Image::lossy_packer = _webp_lossy_pack;
  139. Image::lossy_unpacker = _webp_lossy_unpack;
  140. }