image_loader_webp.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/print_string.h"
  34. #include <stdlib.h>
  35. #include <webp/decode.h>
  36. #include <webp/encode.h>
  37. static PoolVector<uint8_t> _webp_lossy_pack(const Ref<Image> &p_image, float p_quality) {
  38. ERR_FAIL_COND_V(p_image.is_null() || p_image->empty(), PoolVector<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. Size2 s(img->get_width(), img->get_height());
  45. PoolVector<uint8_t> data = img->get_data();
  46. PoolVector<uint8_t>::Read r = data.read();
  47. uint8_t *dst_buff = NULL;
  48. size_t dst_size = 0;
  49. if (img->get_format() == Image::FORMAT_RGB8) {
  50. dst_size = WebPEncodeRGB(r.ptr(), s.width, s.height, 3 * s.width, CLAMP(p_quality * 100.0, 0, 100.0), &dst_buff);
  51. } else {
  52. dst_size = WebPEncodeRGBA(r.ptr(), s.width, s.height, 4 * s.width, CLAMP(p_quality * 100.0, 0, 100.0), &dst_buff);
  53. }
  54. ERR_FAIL_COND_V(dst_size == 0, PoolVector<uint8_t>());
  55. PoolVector<uint8_t> dst;
  56. dst.resize(4 + dst_size);
  57. PoolVector<uint8_t>::Write w = dst.write();
  58. w[0] = 'W';
  59. w[1] = 'E';
  60. w[2] = 'B';
  61. w[3] = 'P';
  62. copymem(&w[4], dst_buff, dst_size);
  63. free(dst_buff);
  64. w = PoolVector<uint8_t>::Write();
  65. return dst;
  66. }
  67. static Ref<Image> _webp_lossy_unpack(const PoolVector<uint8_t> &p_buffer) {
  68. int size = p_buffer.size() - 4;
  69. ERR_FAIL_COND_V(size <= 0, Ref<Image>());
  70. PoolVector<uint8_t>::Read r = p_buffer.read();
  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_EXPLAIN("Error unpacking WEBP image:");
  75. ERR_FAIL_V(Ref<Image>());
  76. }
  77. /*
  78. print_line("width: "+itos(features.width));
  79. print_line("height: "+itos(features.height));
  80. print_line("alpha: "+itos(features.has_alpha));
  81. */
  82. PoolVector<uint8_t> dst_image;
  83. int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
  84. dst_image.resize(datasize);
  85. PoolVector<uint8_t>::Write dst_w = dst_image.write();
  86. bool errdec = false;
  87. if (features.has_alpha) {
  88. errdec = WebPDecodeRGBAInto(&r[4], size, dst_w.ptr(), datasize, 4 * features.width) == NULL;
  89. } else {
  90. errdec = WebPDecodeRGBInto(&r[4], size, dst_w.ptr(), datasize, 3 * features.width) == NULL;
  91. }
  92. //ERR_EXPLAIN("Error decoding webp! - "+p_file);
  93. ERR_FAIL_COND_V(errdec, Ref<Image>());
  94. dst_w = PoolVector<uint8_t>::Write();
  95. Ref<Image> img = memnew(Image(features.width, features.height, 0, features.has_alpha ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8, dst_image));
  96. return img;
  97. }
  98. Error webp_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p_buffer_len) {
  99. ERR_FAIL_NULL_V(p_image, ERR_INVALID_PARAMETER);
  100. WebPBitstreamFeatures features;
  101. if (WebPGetFeatures(p_buffer, p_buffer_len, &features) != VP8_STATUS_OK) {
  102. // ERR_EXPLAIN("Error decoding WEBP image");
  103. ERR_FAIL_V(ERR_FILE_CORRUPT);
  104. }
  105. PoolVector<uint8_t> dst_image;
  106. int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
  107. dst_image.resize(datasize);
  108. PoolVector<uint8_t>::Write dst_w = dst_image.write();
  109. bool errdec = false;
  110. if (features.has_alpha) {
  111. errdec = WebPDecodeRGBAInto(p_buffer, p_buffer_len, dst_w.ptr(), datasize, 4 * features.width) == NULL;
  112. } else {
  113. errdec = WebPDecodeRGBInto(p_buffer, p_buffer_len, dst_w.ptr(), datasize, 3 * features.width) == NULL;
  114. }
  115. dst_w = PoolVector<uint8_t>::Write();
  116. //ERR_EXPLAIN("Error decoding webp!");
  117. ERR_FAIL_COND_V(errdec, ERR_FILE_CORRUPT);
  118. p_image->create(features.width, features.height, 0, features.has_alpha ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8, dst_image);
  119. return OK;
  120. }
  121. static Ref<Image> _webp_mem_loader_func(const uint8_t *p_png, int p_size) {
  122. Ref<Image> img;
  123. img.instance();
  124. Error err = webp_load_image_from_buffer(img.ptr(), p_png, p_size);
  125. ERR_FAIL_COND_V(err, Ref<Image>());
  126. return img;
  127. }
  128. Error ImageLoaderWEBP::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) {
  129. PoolVector<uint8_t> src_image;
  130. int src_image_len = f->get_len();
  131. ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
  132. src_image.resize(src_image_len);
  133. PoolVector<uint8_t>::Write w = src_image.write();
  134. f->get_buffer(&w[0], src_image_len);
  135. f->close();
  136. Error err = webp_load_image_from_buffer(p_image.ptr(), w.ptr(), src_image_len);
  137. w = PoolVector<uint8_t>::Write();
  138. return err;
  139. }
  140. void ImageLoaderWEBP::get_recognized_extensions(List<String> *p_extensions) const {
  141. p_extensions->push_back("webp");
  142. }
  143. ImageLoaderWEBP::ImageLoaderWEBP() {
  144. Image::_webp_mem_loader_func = _webp_mem_loader_func;
  145. Image::lossy_packer = _webp_lossy_pack;
  146. Image::lossy_unpacker = _webp_lossy_unpack;
  147. }