2
0

webp_common.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*************************************************************************/
  2. /* webp_common.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "webp_common.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/os/os.h"
  33. #include <string.h>
  34. #include <webp/decode.h>
  35. #include <webp/encode.h>
  36. namespace WebPCommon {
  37. 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.0f, 0.0f, 100.0f), &dst_buff);
  52. } else {
  53. dst_size = WebPEncodeRGBA(r, s.width, s.height, 4 * s.width, CLAMP(p_quality * 100.0f, 0.0f, 100.0f), &dst_buff);
  54. }
  55. ERR_FAIL_COND_V(dst_size == 0, Vector<uint8_t>());
  56. Vector<uint8_t> dst;
  57. dst.resize(dst_size);
  58. uint8_t *w = dst.ptrw();
  59. memcpy(w, dst_buff, dst_size);
  60. WebPFree(dst_buff);
  61. return dst;
  62. }
  63. Vector<uint8_t> _webp_lossless_pack(const Ref<Image> &p_image) {
  64. ERR_FAIL_COND_V(p_image.is_null() || p_image->is_empty(), Vector<uint8_t>());
  65. int compression_level = GLOBAL_GET("rendering/textures/lossless_compression/webp_compression_level");
  66. compression_level = CLAMP(compression_level, 0, 9);
  67. Ref<Image> img = p_image->duplicate();
  68. if (img->detect_alpha()) {
  69. img->convert(Image::FORMAT_RGBA8);
  70. } else {
  71. img->convert(Image::FORMAT_RGB8);
  72. }
  73. Size2 s(img->get_width(), img->get_height());
  74. Vector<uint8_t> data = img->get_data();
  75. const uint8_t *r = data.ptr();
  76. // we need to use the more complex API in order to access the 'exact' flag...
  77. WebPConfig config;
  78. WebPPicture pic;
  79. if (!WebPConfigInit(&config) || !WebPConfigLosslessPreset(&config, compression_level) || !WebPPictureInit(&pic)) {
  80. ERR_FAIL_V(Vector<uint8_t>());
  81. }
  82. WebPMemoryWriter wrt;
  83. config.exact = 1;
  84. pic.use_argb = 1;
  85. pic.width = s.width;
  86. pic.height = s.height;
  87. pic.writer = WebPMemoryWrite;
  88. pic.custom_ptr = &wrt;
  89. WebPMemoryWriterInit(&wrt);
  90. bool success_import = false;
  91. if (img->get_format() == Image::FORMAT_RGB8) {
  92. success_import = WebPPictureImportRGB(&pic, r, 3 * s.width);
  93. } else {
  94. success_import = WebPPictureImportRGBA(&pic, r, 4 * s.width);
  95. }
  96. bool success_encode = false;
  97. if (success_import) {
  98. success_encode = WebPEncode(&config, &pic);
  99. }
  100. WebPPictureFree(&pic);
  101. if (!success_encode) {
  102. WebPMemoryWriterClear(&wrt);
  103. ERR_FAIL_V_MSG(Vector<uint8_t>(), "WebP packing failed.");
  104. }
  105. // copy from wrt
  106. Vector<uint8_t> dst;
  107. dst.resize(wrt.size);
  108. uint8_t *w = dst.ptrw();
  109. memcpy(w, wrt.mem, wrt.size);
  110. WebPMemoryWriterClear(&wrt);
  111. return dst;
  112. }
  113. Ref<Image> _webp_unpack(const Vector<uint8_t> &p_buffer) {
  114. int size = p_buffer.size();
  115. ERR_FAIL_COND_V(size <= 0, Ref<Image>());
  116. const uint8_t *r = p_buffer.ptr();
  117. // A WebP file uses a RIFF header, which starts with "RIFF____WEBP".
  118. 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>());
  119. WebPBitstreamFeatures features;
  120. if (WebPGetFeatures(r, size, &features) != VP8_STATUS_OK) {
  121. ERR_FAIL_V_MSG(Ref<Image>(), "Error unpacking WEBP image.");
  122. }
  123. Vector<uint8_t> dst_image;
  124. int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
  125. dst_image.resize(datasize);
  126. uint8_t *dst_w = dst_image.ptrw();
  127. bool errdec = false;
  128. if (features.has_alpha) {
  129. errdec = WebPDecodeRGBAInto(r, size, dst_w, datasize, 4 * features.width) == nullptr;
  130. } else {
  131. errdec = WebPDecodeRGBInto(r, size, dst_w, datasize, 3 * features.width) == nullptr;
  132. }
  133. ERR_FAIL_COND_V_MSG(errdec, Ref<Image>(), "Failed decoding WebP image.");
  134. Ref<Image> img = memnew(Image(features.width, features.height, 0, features.has_alpha ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8, dst_image));
  135. return img;
  136. }
  137. Error webp_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p_buffer_len) {
  138. ERR_FAIL_NULL_V(p_image, ERR_INVALID_PARAMETER);
  139. WebPBitstreamFeatures features;
  140. if (WebPGetFeatures(p_buffer, p_buffer_len, &features) != VP8_STATUS_OK) {
  141. ERR_FAIL_V(ERR_FILE_CORRUPT);
  142. }
  143. Vector<uint8_t> dst_image;
  144. int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
  145. dst_image.resize(datasize);
  146. uint8_t *dst_w = dst_image.ptrw();
  147. bool errdec = false;
  148. if (features.has_alpha) {
  149. errdec = WebPDecodeRGBAInto(p_buffer, p_buffer_len, dst_w, datasize, 4 * features.width) == nullptr;
  150. } else {
  151. errdec = WebPDecodeRGBInto(p_buffer, p_buffer_len, dst_w, datasize, 3 * features.width) == nullptr;
  152. }
  153. ERR_FAIL_COND_V_MSG(errdec, ERR_FILE_CORRUPT, "Failed decoding WebP image.");
  154. p_image->set_data(features.width, features.height, false, features.has_alpha ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8, dst_image);
  155. return OK;
  156. }
  157. } // namespace WebPCommon