image_compress_squish.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*************************************************************************/
  2. /* image_compress_squish.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_compress_squish.h"
  31. #include <squish.h>
  32. void image_decompress_squish(Image *p_image) {
  33. int w = p_image->get_width();
  34. int h = p_image->get_height();
  35. Image::Format target_format = Image::FORMAT_RGBA8;
  36. PoolVector<uint8_t> data;
  37. int target_size = Image::get_image_data_size(w, h, target_format, p_image->has_mipmaps());
  38. int mm_count = p_image->get_mipmap_count();
  39. data.resize(target_size);
  40. PoolVector<uint8_t>::Read rb = p_image->get_data().read();
  41. PoolVector<uint8_t>::Write wb = data.write();
  42. int squish_flags = Image::FORMAT_MAX;
  43. if (p_image->get_format() == Image::FORMAT_DXT1) {
  44. squish_flags = squish::kDxt1;
  45. } else if (p_image->get_format() == Image::FORMAT_DXT3) {
  46. squish_flags = squish::kDxt3;
  47. } else if (p_image->get_format() == Image::FORMAT_DXT5) {
  48. squish_flags = squish::kDxt5;
  49. } else if (p_image->get_format() == Image::FORMAT_RGTC_R) {
  50. squish_flags = squish::kBc4;
  51. } else if (p_image->get_format() == Image::FORMAT_RGTC_RG) {
  52. squish_flags = squish::kBc5;
  53. } else {
  54. ERR_EXPLAIN("Squish: Can't decompress unknown format: " + itos(p_image->get_format()));
  55. ERR_FAIL_COND(true);
  56. return;
  57. }
  58. for (int i = 0; i <= mm_count; i++) {
  59. int src_ofs = 0, mipmap_size = 0, mipmap_w = 0, mipmap_h = 0;
  60. p_image->get_mipmap_offset_size_and_dimensions(i, src_ofs, mipmap_size, mipmap_w, mipmap_h);
  61. int dst_ofs = Image::get_image_mipmap_offset(p_image->get_width(), p_image->get_height(), target_format, i);
  62. squish::DecompressImage(&wb[dst_ofs], w, h, &rb[src_ofs], squish_flags);
  63. w >>= 1;
  64. h >>= 1;
  65. }
  66. p_image->create(p_image->get_width(), p_image->get_height(), p_image->has_mipmaps(), target_format, data);
  67. }
  68. #ifdef TOOLS_ENABLED
  69. void image_compress_squish(Image *p_image, float p_lossy_quality, Image::CompressSource p_source) {
  70. if (p_image->get_format() >= Image::FORMAT_DXT1)
  71. return; //do not compress, already compressed
  72. int w = p_image->get_width();
  73. int h = p_image->get_height();
  74. if (p_image->get_format() <= Image::FORMAT_RGBA8) {
  75. int squish_comp = squish::kColourRangeFit;
  76. if (p_lossy_quality > 0.85)
  77. squish_comp = squish::kColourIterativeClusterFit;
  78. else if (p_lossy_quality > 0.75)
  79. squish_comp = squish::kColourClusterFit;
  80. Image::Format target_format = Image::FORMAT_RGBA8;
  81. Image::DetectChannels dc = p_image->get_detected_channels();
  82. if (p_source == Image::COMPRESS_SOURCE_LAYERED) {
  83. //keep what comes in
  84. switch (p_image->get_format()) {
  85. case Image::FORMAT_L8: {
  86. dc = Image::DETECTED_L;
  87. } break;
  88. case Image::FORMAT_LA8: {
  89. dc = Image::DETECTED_LA;
  90. } break;
  91. case Image::FORMAT_R8: {
  92. dc = Image::DETECTED_R;
  93. } break;
  94. case Image::FORMAT_RG8: {
  95. dc = Image::DETECTED_RG;
  96. } break;
  97. case Image::FORMAT_RGB8: {
  98. dc = Image::DETECTED_RGB;
  99. } break;
  100. case Image::FORMAT_RGBA8:
  101. case Image::FORMAT_RGBA4444:
  102. case Image::FORMAT_RGBA5551: {
  103. dc = Image::DETECTED_RGBA;
  104. } break;
  105. default: {}
  106. }
  107. }
  108. p_image->convert(Image::FORMAT_RGBA8); //still uses RGBA to convert
  109. if (p_source == Image::COMPRESS_SOURCE_SRGB && (dc == Image::DETECTED_R || dc == Image::DETECTED_RG)) {
  110. //R and RG do not support SRGB
  111. dc = Image::DETECTED_RGB;
  112. }
  113. if (p_source == Image::COMPRESS_SOURCE_NORMAL) {
  114. //R and RG do not support SRGB
  115. dc = Image::DETECTED_RG;
  116. }
  117. switch (dc) {
  118. case Image::DETECTED_L: {
  119. target_format = Image::FORMAT_DXT1;
  120. squish_comp |= squish::kDxt1;
  121. } break;
  122. case Image::DETECTED_LA: {
  123. target_format = Image::FORMAT_DXT5;
  124. squish_comp |= squish::kDxt5;
  125. } break;
  126. case Image::DETECTED_R: {
  127. target_format = Image::FORMAT_RGTC_R;
  128. squish_comp |= squish::kBc4;
  129. } break;
  130. case Image::DETECTED_RG: {
  131. target_format = Image::FORMAT_RGTC_RG;
  132. squish_comp |= squish::kBc5;
  133. } break;
  134. case Image::DETECTED_RGB: {
  135. target_format = Image::FORMAT_DXT1;
  136. squish_comp |= squish::kDxt1;
  137. } break;
  138. case Image::DETECTED_RGBA: {
  139. //TODO, should convert both, then measure which one does a better job
  140. target_format = Image::FORMAT_DXT5;
  141. squish_comp |= squish::kDxt5;
  142. } break;
  143. default: {
  144. ERR_PRINT("Unknown image format, defaulting to RGBA8");
  145. break;
  146. }
  147. }
  148. PoolVector<uint8_t> data;
  149. int target_size = Image::get_image_data_size(w, h, target_format, p_image->has_mipmaps());
  150. int mm_count = p_image->has_mipmaps() ? Image::get_image_required_mipmaps(w, h, target_format) : 0;
  151. data.resize(target_size);
  152. int shift = Image::get_format_pixel_rshift(target_format);
  153. PoolVector<uint8_t>::Read rb = p_image->get_data().read();
  154. PoolVector<uint8_t>::Write wb = data.write();
  155. int dst_ofs = 0;
  156. for (int i = 0; i <= mm_count; i++) {
  157. int bw = w % 4 != 0 ? w + (4 - w % 4) : w;
  158. int bh = h % 4 != 0 ? h + (4 - h % 4) : h;
  159. int src_ofs = p_image->get_mipmap_offset(i);
  160. squish::CompressImage(&rb[src_ofs], w, h, &wb[dst_ofs], squish_comp);
  161. dst_ofs += (MAX(4, bw) * MAX(4, bh)) >> shift;
  162. w = MAX(w / 2, 1);
  163. h = MAX(h / 2, 1);
  164. }
  165. rb = PoolVector<uint8_t>::Read();
  166. wb = PoolVector<uint8_t>::Write();
  167. p_image->create(p_image->get_width(), p_image->get_height(), p_image->has_mipmaps(), target_format, data);
  168. }
  169. }
  170. #endif