webp_common.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**************************************************************************/
  2. /* webp_common.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 "webp_common.h"
  31. #include "core/config/project_settings.h"
  32. #include <webp/decode.h>
  33. #include <webp/encode.h>
  34. #include <string.h>
  35. namespace WebPCommon {
  36. Vector<uint8_t> _webp_lossy_pack(const Ref<Image> &p_image, float p_quality) {
  37. ERR_FAIL_COND_V(p_image.is_null() || p_image->is_empty(), Vector<uint8_t>());
  38. return _webp_packer(p_image, CLAMP(p_quality * 100.0f, 0.0f, 100.0f), false);
  39. }
  40. Vector<uint8_t> _webp_lossless_pack(const Ref<Image> &p_image) {
  41. ERR_FAIL_COND_V(p_image.is_null() || p_image->is_empty(), Vector<uint8_t>());
  42. float compression_factor = GLOBAL_GET("rendering/textures/webp_compression/lossless_compression_factor");
  43. compression_factor = CLAMP(compression_factor, 0.0f, 100.0f);
  44. return _webp_packer(p_image, compression_factor, true);
  45. }
  46. Vector<uint8_t> _webp_packer(const Ref<Image> &p_image, float p_quality, bool p_lossless) {
  47. int compression_method = GLOBAL_GET("rendering/textures/webp_compression/compression_method");
  48. compression_method = CLAMP(compression_method, 0, 6);
  49. Ref<Image> img = p_image->duplicate();
  50. if (img->is_compressed()) {
  51. Error error = img->decompress();
  52. ERR_FAIL_COND_V_MSG(error != OK, Vector<uint8_t>(), "Couldn't decompress image.");
  53. }
  54. if (img->detect_alpha()) {
  55. img->convert(Image::FORMAT_RGBA8);
  56. } else {
  57. img->convert(Image::FORMAT_RGB8);
  58. }
  59. Size2 s(img->get_width(), img->get_height());
  60. Vector<uint8_t> data = img->get_data();
  61. const uint8_t *r = data.ptr();
  62. // we need to use the more complex API in order to access specific flags...
  63. WebPConfig config;
  64. WebPPicture pic;
  65. if (!WebPConfigInit(&config) || !WebPPictureInit(&pic)) {
  66. ERR_FAIL_V(Vector<uint8_t>());
  67. }
  68. WebPMemoryWriter wrt;
  69. if (p_lossless) {
  70. config.lossless = 1;
  71. config.exact = 1;
  72. }
  73. config.method = compression_method;
  74. config.quality = p_quality;
  75. config.use_sharp_yuv = 1;
  76. pic.use_argb = 1;
  77. pic.width = s.width;
  78. pic.height = s.height;
  79. pic.writer = WebPMemoryWrite;
  80. pic.custom_ptr = &wrt;
  81. WebPMemoryWriterInit(&wrt);
  82. bool success_import = false;
  83. if (img->get_format() == Image::FORMAT_RGB8) {
  84. success_import = WebPPictureImportRGB(&pic, r, 3 * s.width);
  85. } else {
  86. success_import = WebPPictureImportRGBA(&pic, r, 4 * s.width);
  87. }
  88. bool success_encode = false;
  89. if (success_import) {
  90. success_encode = WebPEncode(&config, &pic);
  91. }
  92. WebPPictureFree(&pic);
  93. if (!success_encode) {
  94. WebPMemoryWriterClear(&wrt);
  95. ERR_FAIL_V_MSG(Vector<uint8_t>(), "WebP packing failed.");
  96. }
  97. // copy from wrt
  98. Vector<uint8_t> dst;
  99. dst.resize(wrt.size);
  100. uint8_t *w = dst.ptrw();
  101. memcpy(w, wrt.mem, wrt.size);
  102. WebPMemoryWriterClear(&wrt);
  103. return dst;
  104. }
  105. Ref<Image> _webp_unpack(const Vector<uint8_t> &p_buffer) {
  106. int size = p_buffer.size();
  107. ERR_FAIL_COND_V(size <= 0, Ref<Image>());
  108. const uint8_t *r = p_buffer.ptr();
  109. // A WebP file uses a RIFF header, which starts with "RIFF____WEBP".
  110. ERR_FAIL_COND_V(r[0] != 'R' || r[1] != 'I' || r[2] != 'F' || r[3] != 'F' || r[8] != 'W' || r[9] != 'E' || r[10] != 'B' || r[11] != 'P', Ref<Image>());
  111. WebPBitstreamFeatures features;
  112. if (WebPGetFeatures(r, size, &features) != VP8_STATUS_OK) {
  113. ERR_FAIL_V_MSG(Ref<Image>(), "Error unpacking WebP image.");
  114. }
  115. Vector<uint8_t> dst_image;
  116. int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
  117. dst_image.resize(datasize);
  118. uint8_t *dst_w = dst_image.ptrw();
  119. bool errdec = false;
  120. if (features.has_alpha) {
  121. errdec = WebPDecodeRGBAInto(r, size, dst_w, datasize, 4 * features.width) == nullptr;
  122. } else {
  123. errdec = WebPDecodeRGBInto(r, size, dst_w, datasize, 3 * features.width) == nullptr;
  124. }
  125. ERR_FAIL_COND_V_MSG(errdec, Ref<Image>(), "Failed decoding WebP image.");
  126. Ref<Image> img = memnew(Image(features.width, features.height, false, features.has_alpha ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8, dst_image));
  127. return img;
  128. }
  129. Error webp_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p_buffer_len) {
  130. ERR_FAIL_NULL_V(p_image, ERR_INVALID_PARAMETER);
  131. WebPBitstreamFeatures features;
  132. if (WebPGetFeatures(p_buffer, p_buffer_len, &features) != VP8_STATUS_OK) {
  133. ERR_FAIL_V(ERR_FILE_CORRUPT);
  134. }
  135. Vector<uint8_t> dst_image;
  136. int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
  137. dst_image.resize(datasize);
  138. uint8_t *dst_w = dst_image.ptrw();
  139. bool errdec = false;
  140. if (features.has_alpha) {
  141. errdec = WebPDecodeRGBAInto(p_buffer, p_buffer_len, dst_w, datasize, 4 * features.width) == nullptr;
  142. } else {
  143. errdec = WebPDecodeRGBInto(p_buffer, p_buffer_len, dst_w, datasize, 3 * features.width) == nullptr;
  144. }
  145. ERR_FAIL_COND_V_MSG(errdec, ERR_FILE_CORRUPT, "Failed decoding WebP image.");
  146. p_image->set_data(features.width, features.height, false, features.has_alpha ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8, dst_image);
  147. return OK;
  148. }
  149. } // namespace WebPCommon