image_loader_webp.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**************************************************************************/
  2. /* image_loader_webp.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_webp.h"
  31. #include "core/io/marshalls.h"
  32. #include "core/os/os.h"
  33. #include "core/print_string.h"
  34. #include "core/project_settings.h"
  35. #include <stdlib.h>
  36. #include <webp/decode.h>
  37. #include <webp/encode.h>
  38. static PoolVector<uint8_t> _webp_lossy_pack(const Ref<Image> &p_image, float p_quality) {
  39. ERR_FAIL_COND_V(p_image.is_null() || p_image->empty(), PoolVector<uint8_t>());
  40. Ref<Image> img = p_image->duplicate();
  41. if (img->detect_alpha()) {
  42. img->convert(Image::FORMAT_RGBA8);
  43. } else {
  44. img->convert(Image::FORMAT_RGB8);
  45. }
  46. Size2 s(img->get_width(), img->get_height());
  47. PoolVector<uint8_t> data = img->get_data();
  48. PoolVector<uint8_t>::Read r = data.read();
  49. uint8_t *dst_buff = nullptr;
  50. size_t dst_size = 0;
  51. if (img->get_format() == Image::FORMAT_RGB8) {
  52. dst_size = WebPEncodeRGB(r.ptr(), s.width, s.height, 3 * s.width, CLAMP(p_quality * 100.0, 0, 100.0), &dst_buff);
  53. } else {
  54. dst_size = WebPEncodeRGBA(r.ptr(), s.width, s.height, 4 * s.width, CLAMP(p_quality * 100.0, 0, 100.0), &dst_buff);
  55. }
  56. ERR_FAIL_COND_V(dst_size == 0, PoolVector<uint8_t>());
  57. PoolVector<uint8_t> dst;
  58. dst.resize(4 + dst_size);
  59. PoolVector<uint8_t>::Write w = dst.write();
  60. w[0] = 'W';
  61. w[1] = 'E';
  62. w[2] = 'B';
  63. w[3] = 'P';
  64. memcpy(&w[4], dst_buff, dst_size);
  65. WebPFree(dst_buff);
  66. w.release();
  67. return dst;
  68. }
  69. static PoolVector<uint8_t> _webp_lossless_pack(const Ref<Image> &p_image) {
  70. ERR_FAIL_COND_V(p_image.is_null() || p_image->empty(), PoolVector<uint8_t>());
  71. int compression_level = ProjectSettings::get_singleton()->get("rendering/misc/lossless_compression/webp_compression_level");
  72. compression_level = CLAMP(compression_level, 0, 9);
  73. Ref<Image> img = p_image->duplicate();
  74. if (img->detect_alpha()) {
  75. img->convert(Image::FORMAT_RGBA8);
  76. } else {
  77. img->convert(Image::FORMAT_RGB8);
  78. }
  79. Size2 s(img->get_width(), img->get_height());
  80. PoolVector<uint8_t> data = img->get_data();
  81. PoolVector<uint8_t>::Read r = data.read();
  82. // we need to use the more complex API in order to access the 'exact' flag...
  83. WebPConfig config;
  84. WebPPicture pic;
  85. if (!WebPConfigInit(&config) || !WebPConfigLosslessPreset(&config, compression_level) || !WebPPictureInit(&pic)) {
  86. ERR_FAIL_V(PoolVector<uint8_t>());
  87. }
  88. WebPMemoryWriter wrt;
  89. config.exact = 1;
  90. pic.use_argb = 1;
  91. pic.width = s.width;
  92. pic.height = s.height;
  93. pic.writer = WebPMemoryWrite;
  94. pic.custom_ptr = &wrt;
  95. WebPMemoryWriterInit(&wrt);
  96. bool success_import = false;
  97. if (img->get_format() == Image::FORMAT_RGB8) {
  98. success_import = WebPPictureImportRGB(&pic, r.ptr(), 3 * s.width);
  99. } else {
  100. success_import = WebPPictureImportRGBA(&pic, r.ptr(), 4 * s.width);
  101. }
  102. bool success_encode = false;
  103. if (success_import) {
  104. success_encode = WebPEncode(&config, &pic);
  105. }
  106. WebPPictureFree(&pic);
  107. if (!success_encode) {
  108. WebPMemoryWriterClear(&wrt);
  109. ERR_FAIL_V_MSG(PoolVector<uint8_t>(), "WebP packing failed.");
  110. }
  111. // copy from wrt
  112. PoolVector<uint8_t> dst;
  113. dst.resize(4 + wrt.size);
  114. PoolVector<uint8_t>::Write w = dst.write();
  115. w[0] = 'W';
  116. w[1] = 'E';
  117. w[2] = 'B';
  118. w[3] = 'P';
  119. memcpy(&w[4], wrt.mem, wrt.size);
  120. w.release();
  121. WebPMemoryWriterClear(&wrt);
  122. return dst;
  123. }
  124. static Ref<Image> _webp_lossy_unpack(const PoolVector<uint8_t> &p_buffer) {
  125. int size = p_buffer.size() - 4;
  126. ERR_FAIL_COND_V(size <= 0, Ref<Image>());
  127. PoolVector<uint8_t>::Read r = p_buffer.read();
  128. ERR_FAIL_COND_V(r[0] != 'W' || r[1] != 'E' || r[2] != 'B' || r[3] != 'P', Ref<Image>());
  129. WebPBitstreamFeatures features;
  130. if (WebPGetFeatures(&r[4], size, &features) != VP8_STATUS_OK) {
  131. ERR_FAIL_V_MSG(Ref<Image>(), "Error unpacking WEBP image.");
  132. }
  133. /*
  134. print_line("width: "+itos(features.width));
  135. print_line("height: "+itos(features.height));
  136. print_line("alpha: "+itos(features.has_alpha));
  137. */
  138. PoolVector<uint8_t> dst_image;
  139. int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
  140. dst_image.resize(datasize);
  141. PoolVector<uint8_t>::Write dst_w = dst_image.write();
  142. bool errdec = false;
  143. if (features.has_alpha) {
  144. errdec = WebPDecodeRGBAInto(&r[4], size, dst_w.ptr(), datasize, 4 * features.width) == nullptr;
  145. } else {
  146. errdec = WebPDecodeRGBInto(&r[4], size, dst_w.ptr(), datasize, 3 * features.width) == nullptr;
  147. }
  148. ERR_FAIL_COND_V_MSG(errdec, Ref<Image>(), "Failed decoding WebP image.");
  149. dst_w.release();
  150. Ref<Image> img = memnew(Image(features.width, features.height, 0, features.has_alpha ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8, dst_image));
  151. return img;
  152. }
  153. Error webp_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p_buffer_len) {
  154. ERR_FAIL_NULL_V(p_image, ERR_INVALID_PARAMETER);
  155. WebPBitstreamFeatures features;
  156. if (WebPGetFeatures(p_buffer, p_buffer_len, &features) != VP8_STATUS_OK) {
  157. ERR_FAIL_V(ERR_FILE_CORRUPT);
  158. }
  159. PoolVector<uint8_t> dst_image;
  160. int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
  161. dst_image.resize(datasize);
  162. PoolVector<uint8_t>::Write dst_w = dst_image.write();
  163. bool errdec = false;
  164. if (features.has_alpha) {
  165. errdec = WebPDecodeRGBAInto(p_buffer, p_buffer_len, dst_w.ptr(), datasize, 4 * features.width) == nullptr;
  166. } else {
  167. errdec = WebPDecodeRGBInto(p_buffer, p_buffer_len, dst_w.ptr(), datasize, 3 * features.width) == nullptr;
  168. }
  169. dst_w.release();
  170. ERR_FAIL_COND_V_MSG(errdec, ERR_FILE_CORRUPT, "Failed decoding WebP image.");
  171. p_image->create(features.width, features.height, false, features.has_alpha ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8, dst_image);
  172. return OK;
  173. }
  174. static Ref<Image> _webp_mem_loader_func(const uint8_t *p_png, int p_size) {
  175. Ref<Image> img;
  176. img.instance();
  177. Error err = webp_load_image_from_buffer(img.ptr(), p_png, p_size);
  178. ERR_FAIL_COND_V(err, Ref<Image>());
  179. return img;
  180. }
  181. Error ImageLoaderWEBP::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) {
  182. PoolVector<uint8_t> src_image;
  183. uint64_t src_image_len = f->get_len();
  184. ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
  185. src_image.resize(src_image_len);
  186. PoolVector<uint8_t>::Write w = src_image.write();
  187. f->get_buffer(&w[0], src_image_len);
  188. f->close();
  189. Error err = webp_load_image_from_buffer(p_image.ptr(), w.ptr(), src_image_len);
  190. return err;
  191. }
  192. void ImageLoaderWEBP::get_recognized_extensions(List<String> *p_extensions) const {
  193. p_extensions->push_back("webp");
  194. }
  195. ImageLoaderWEBP::ImageLoaderWEBP() {
  196. Image::_webp_mem_loader_func = _webp_mem_loader_func;
  197. Image::webp_lossy_packer = _webp_lossy_pack;
  198. Image::webp_lossless_packer = _webp_lossless_pack;
  199. Image::webp_unpacker = _webp_lossy_unpack;
  200. }