image_loader_webp.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*************************************************************************/
  2. /* image_loader_webp.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "image_loader_webp.h"
  30. #include "print_string.h"
  31. #include "os/os.h"
  32. #include "drivers/webp/decode.h"
  33. #include "drivers/webp/encode.h"
  34. #include "io/marshalls.h"
  35. #include <stdlib.h>
  36. static DVector<uint8_t> _webp_lossy_pack(const Image& p_image,float p_quality) {
  37. ERR_FAIL_COND_V(p_image.empty(),DVector<uint8_t>());
  38. Image img=p_image;
  39. if (img.detect_alpha())
  40. img.convert(Image::FORMAT_RGBA);
  41. else
  42. img.convert(Image::FORMAT_RGB);
  43. Size2 s(img.get_width(),img.get_height());
  44. DVector<uint8_t> data = img.get_data();
  45. DVector<uint8_t>::Read r = data.read();
  46. uint8_t *dst_buff=NULL;
  47. size_t dst_size=0;
  48. if (img.get_format()==Image::FORMAT_RGB) {
  49. dst_size = WebPEncodeRGB(r.ptr(),s.width,s.height,3*s.width,CLAMP(p_quality*100.0,0,100.0),&dst_buff);
  50. } else {
  51. dst_size = WebPEncodeRGBA(r.ptr(),s.width,s.height,4*s.width,CLAMP(p_quality*100.0,0,100.0),&dst_buff);
  52. }
  53. ERR_FAIL_COND_V(dst_size==0,DVector<uint8_t>());
  54. DVector<uint8_t> dst;
  55. dst.resize(4+dst_size);
  56. DVector<uint8_t>::Write w = dst.write();
  57. w[0]='W';
  58. w[1]='E';
  59. w[2]='B';
  60. w[3]='P';
  61. copymem(&w[4],dst_buff,dst_size);
  62. free(dst_buff);
  63. w=DVector<uint8_t>::Write();
  64. return dst;
  65. }
  66. static Image _webp_lossy_unpack(const DVector<uint8_t>& p_buffer) {
  67. int size = p_buffer.size()-4;
  68. ERR_FAIL_COND_V(size<=0,Image());
  69. DVector<uint8_t>::Read r = p_buffer.read();
  70. ERR_FAIL_COND_V(r[0]!='W' || r[1]!='E' || r[2]!='B' || r[3]!='P',Image());
  71. WebPBitstreamFeatures features;
  72. if (WebPGetFeatures(&r[4],size,&features)!=VP8_STATUS_OK) {
  73. ERR_EXPLAIN("Error unpacking WEBP image:");
  74. ERR_FAIL_V(Image());
  75. }
  76. //print_line("width: "+itos(features.width));
  77. //print_line("height: "+itos(features.height));
  78. //print_line("alpha: "+itos(features.has_alpha));
  79. DVector<uint8_t> dst_image;
  80. int datasize = features.width*features.height*(features.has_alpha?4:3);
  81. dst_image.resize(datasize);
  82. DVector<uint8_t>::Write dst_w = dst_image.write();
  83. bool errdec=false;
  84. if (features.has_alpha) {
  85. errdec = WebPDecodeRGBAInto(&r[4],size,dst_w.ptr(),datasize,4*features.width)==NULL;
  86. } else {
  87. errdec = WebPDecodeRGBInto(&r[4],size,dst_w.ptr(),datasize,3*features.width)==NULL;
  88. }
  89. //ERR_EXPLAIN("Error decoding webp! - "+p_file);
  90. ERR_FAIL_COND_V(errdec,Image());
  91. dst_w = DVector<uint8_t>::Write();
  92. return Image(features.width,features.height,0,features.has_alpha?Image::FORMAT_RGBA:Image::FORMAT_RGB,dst_image);
  93. }
  94. Error ImageLoaderWEBP::load_image(Image *p_image,FileAccess *f) {
  95. uint32_t size = f->get_len();
  96. DVector<uint8_t> src_image;
  97. src_image.resize(size);
  98. WebPBitstreamFeatures features;
  99. DVector<uint8_t>::Write src_w = src_image.write();
  100. f->get_buffer(src_w.ptr(),size);
  101. ERR_FAIL_COND_V(f->eof_reached(), ERR_FILE_EOF);
  102. if (WebPGetFeatures(src_w.ptr(),size,&features)!=VP8_STATUS_OK) {
  103. f->close();
  104. //ERR_EXPLAIN("Error decoding WEBP image: "+p_file);
  105. ERR_FAIL_V(ERR_FILE_CORRUPT);
  106. }
  107. print_line("width: "+itos(features.width));
  108. print_line("height: "+itos(features.height));
  109. print_line("alpha: "+itos(features.has_alpha));
  110. src_w = DVector<uint8_t>::Write();
  111. DVector<uint8_t> dst_image;
  112. int datasize = features.width*features.height*(features.has_alpha?4:3);
  113. dst_image.resize(datasize);
  114. DVector<uint8_t>::Read src_r = src_image.read();
  115. DVector<uint8_t>::Write dst_w = dst_image.write();
  116. bool errdec=false;
  117. if (features.has_alpha) {
  118. errdec = WebPDecodeRGBAInto(src_r.ptr(),size,dst_w.ptr(),datasize,4*features.width)==NULL;
  119. } else {
  120. errdec = WebPDecodeRGBInto(src_r.ptr(),size,dst_w.ptr(),datasize,3*features.width)==NULL;
  121. }
  122. //ERR_EXPLAIN("Error decoding webp! - "+p_file);
  123. ERR_FAIL_COND_V(errdec,ERR_FILE_CORRUPT);
  124. src_r = DVector<uint8_t>::Read();
  125. dst_w = DVector<uint8_t>::Write();
  126. *p_image = Image(features.width,features.height,0,features.has_alpha?Image::FORMAT_RGBA:Image::FORMAT_RGB,dst_image);
  127. return OK;
  128. }
  129. void ImageLoaderWEBP::get_recognized_extensions(List<String> *p_extensions) const {
  130. p_extensions->push_back("webp");
  131. }
  132. ImageLoaderWEBP::ImageLoaderWEBP() {
  133. Image::lossy_packer=_webp_lossy_pack;
  134. Image::lossy_unpacker=_webp_lossy_unpack;
  135. }