image_loader_webp.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "io/marshalls.h"
  32. #include "os/os.h"
  33. #include "print_string.h"
  34. #include <stdlib.h>
  35. #include <webp/decode.h>
  36. #include <webp/encode.h>
  37. static DVector<uint8_t> _webp_lossy_pack(const Image &p_image, float p_quality) {
  38. ERR_FAIL_COND_V(p_image.empty(), DVector<uint8_t>());
  39. Image img = p_image;
  40. if (img.detect_alpha())
  41. img.convert(Image::FORMAT_RGBA);
  42. else
  43. img.convert(Image::FORMAT_RGB);
  44. Size2 s(img.get_width(), img.get_height());
  45. DVector<uint8_t> data = img.get_data();
  46. DVector<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_RGB) {
  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, DVector<uint8_t>());
  55. DVector<uint8_t> dst;
  56. dst.resize(4 + dst_size);
  57. DVector<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 = DVector<uint8_t>::Write();
  65. return dst;
  66. }
  67. static Image _webp_lossy_unpack(const DVector<uint8_t> &p_buffer) {
  68. int size = p_buffer.size() - 4;
  69. ERR_FAIL_COND_V(size <= 0, Image());
  70. DVector<uint8_t>::Read r = p_buffer.read();
  71. ERR_FAIL_COND_V(r[0] != 'W' || r[1] != 'E' || r[2] != 'B' || r[3] != 'P', 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(Image());
  76. }
  77. //print_line("width: "+itos(features.width));
  78. //print_line("height: "+itos(features.height));
  79. //print_line("alpha: "+itos(features.has_alpha));
  80. DVector<uint8_t> dst_image;
  81. int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
  82. dst_image.resize(datasize);
  83. DVector<uint8_t>::Write dst_w = dst_image.write();
  84. bool errdec = false;
  85. if (features.has_alpha) {
  86. errdec = WebPDecodeRGBAInto(&r[4], size, dst_w.ptr(), datasize, 4 * features.width) == NULL;
  87. } else {
  88. errdec = WebPDecodeRGBInto(&r[4], size, dst_w.ptr(), datasize, 3 * features.width) == NULL;
  89. }
  90. //ERR_EXPLAIN("Error decoding webp! - "+p_file);
  91. ERR_FAIL_COND_V(errdec, Image());
  92. dst_w = DVector<uint8_t>::Write();
  93. return Image(features.width, features.height, 0, features.has_alpha ? Image::FORMAT_RGBA : Image::FORMAT_RGB, dst_image);
  94. }
  95. Error ImageLoaderWEBP::load_image(Image *p_image, FileAccess *f) {
  96. uint32_t size = f->get_len();
  97. DVector<uint8_t> src_image;
  98. src_image.resize(size);
  99. WebPBitstreamFeatures features;
  100. DVector<uint8_t>::Write src_w = src_image.write();
  101. f->get_buffer(src_w.ptr(), size);
  102. ERR_FAIL_COND_V(f->eof_reached(), ERR_FILE_EOF);
  103. if (WebPGetFeatures(src_w.ptr(), size, &features) != VP8_STATUS_OK) {
  104. f->close();
  105. //ERR_EXPLAIN("Error decoding WEBP image: "+p_file);
  106. ERR_FAIL_V(ERR_FILE_CORRUPT);
  107. }
  108. print_line("width: " + itos(features.width));
  109. print_line("height: " + itos(features.height));
  110. print_line("alpha: " + itos(features.has_alpha));
  111. src_w = DVector<uint8_t>::Write();
  112. DVector<uint8_t> dst_image;
  113. int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
  114. dst_image.resize(datasize);
  115. DVector<uint8_t>::Read src_r = src_image.read();
  116. DVector<uint8_t>::Write dst_w = dst_image.write();
  117. bool errdec = false;
  118. if (features.has_alpha) {
  119. errdec = WebPDecodeRGBAInto(src_r.ptr(), size, dst_w.ptr(), datasize, 4 * features.width) == NULL;
  120. } else {
  121. errdec = WebPDecodeRGBInto(src_r.ptr(), size, dst_w.ptr(), datasize, 3 * features.width) == NULL;
  122. }
  123. //ERR_EXPLAIN("Error decoding webp! - "+p_file);
  124. ERR_FAIL_COND_V(errdec, ERR_FILE_CORRUPT);
  125. src_r = DVector<uint8_t>::Read();
  126. dst_w = DVector<uint8_t>::Write();
  127. *p_image = Image(features.width, features.height, 0, features.has_alpha ? Image::FORMAT_RGBA : Image::FORMAT_RGB, dst_image);
  128. return OK;
  129. }
  130. void ImageLoaderWEBP::get_recognized_extensions(List<String> *p_extensions) const {
  131. p_extensions->push_back("webp");
  132. }
  133. ImageLoaderWEBP::ImageLoaderWEBP() {
  134. Image::lossy_packer = _webp_lossy_pack;
  135. Image::lossy_unpacker = _webp_lossy_unpack;
  136. }