image_compress_squish.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "print_string.h"
  32. #if defined(__SSE2__)
  33. #define SQUISH_USE_SSE 2
  34. #elif defined(__SSE__)
  35. #define SQUISH_USE_SSE 1
  36. #endif
  37. #include <squish.h>
  38. void image_decompress_squish(Image *p_image) {
  39. int w = p_image->get_width();
  40. int h = p_image->get_height();
  41. Image::Format target_format = Image::FORMAT_RGBA8;
  42. PoolVector<uint8_t> data;
  43. int target_size = Image::get_image_data_size(w, h, target_format, p_image->has_mipmaps() ? -1 : 0);
  44. int mm_count = p_image->get_mipmap_count();
  45. data.resize(target_size);
  46. PoolVector<uint8_t>::Read rb = p_image->get_data().read();
  47. PoolVector<uint8_t>::Write wb = data.write();
  48. int squish_flags = Image::FORMAT_MAX;
  49. if (p_image->get_format() == Image::FORMAT_DXT1) {
  50. squish_flags = squish::kDxt1;
  51. } else if (p_image->get_format() == Image::FORMAT_DXT3) {
  52. squish_flags = squish::kDxt3;
  53. } else if (p_image->get_format() == Image::FORMAT_DXT5) {
  54. squish_flags = squish::kDxt5;
  55. } else if (p_image->get_format() == Image::FORMAT_RGTC_R) {
  56. squish_flags = squish::kBc4;
  57. } else if (p_image->get_format() == Image::FORMAT_RGTC_RG) {
  58. squish_flags = squish::kBc5;
  59. } else {
  60. print_line("Can't decompress unknown format: " + itos(p_image->get_format()));
  61. ERR_FAIL_COND(true);
  62. return;
  63. }
  64. int dst_ofs = 0;
  65. for (int i = 0; i <= mm_count; i++) {
  66. int src_ofs = 0, mipmap_size = 0, mipmap_w = 0, mipmap_h = 0;
  67. p_image->get_mipmap_offset_size_and_dimensions(i, src_ofs, mipmap_size, mipmap_w, mipmap_h);
  68. squish::DecompressImage(&wb[dst_ofs], mipmap_w, mipmap_h, &rb[src_ofs], squish_flags);
  69. }
  70. p_image->create(p_image->get_width(), p_image->get_height(), p_image->has_mipmaps(), target_format, data);
  71. }
  72. void image_compress_squish(Image *p_image, Image::CompressSource p_source) {
  73. if (p_image->get_format() >= Image::FORMAT_DXT1)
  74. return; //do not compress, already compressed
  75. int w = p_image->get_width();
  76. int h = p_image->get_height();
  77. if (p_image->get_format() <= Image::FORMAT_RGBA8) {
  78. int squish_comp = squish::kColourRangeFit;
  79. Image::Format target_format = Image::FORMAT_RGBA8;
  80. Image::DetectChannels dc = p_image->get_detected_channels();
  81. p_image->convert(Image::FORMAT_RGBA8); //still uses RGBA to convert
  82. if (p_source == Image::COMPRESS_SOURCE_SRGB && (dc == Image::DETECTED_R || dc == Image::DETECTED_RG)) {
  83. //R and RG do not support SRGB
  84. dc = Image::DETECTED_RGB;
  85. }
  86. if (p_source == Image::COMPRESS_SOURCE_NORMAL) {
  87. //R and RG do not support SRGB
  88. dc = Image::DETECTED_RG;
  89. }
  90. switch (dc) {
  91. case Image::DETECTED_L: {
  92. target_format = Image::FORMAT_DXT1;
  93. squish_comp |= squish::kDxt1;
  94. } break;
  95. case Image::DETECTED_LA: {
  96. target_format = Image::FORMAT_DXT5;
  97. squish_comp |= squish::kDxt5;
  98. } break;
  99. case Image::DETECTED_R: {
  100. target_format = Image::FORMAT_RGTC_R;
  101. squish_comp |= squish::kBc4;
  102. } break;
  103. case Image::DETECTED_RG: {
  104. target_format = Image::FORMAT_RGTC_RG;
  105. squish_comp |= squish::kBc5;
  106. } break;
  107. case Image::DETECTED_RGB: {
  108. target_format = Image::FORMAT_DXT1;
  109. squish_comp |= squish::kDxt1;
  110. } break;
  111. case Image::DETECTED_RGBA: {
  112. //TODO, should convert both, then measure which one does a better job
  113. target_format = Image::FORMAT_DXT5;
  114. squish_comp |= squish::kDxt5;
  115. } break;
  116. default: {
  117. ERR_PRINT("Unknown image format, defaulting to RGBA8");
  118. break;
  119. }
  120. }
  121. PoolVector<uint8_t> data;
  122. int target_size = Image::get_image_data_size(w, h, target_format, p_image->has_mipmaps() ? -1 : 0);
  123. int mm_count = p_image->has_mipmaps() ? Image::get_image_required_mipmaps(w, h, target_format) : 0;
  124. data.resize(target_size);
  125. int shift = Image::get_format_pixel_rshift(target_format);
  126. PoolVector<uint8_t>::Read rb = p_image->get_data().read();
  127. PoolVector<uint8_t>::Write wb = data.write();
  128. int dst_ofs = 0;
  129. for (int i = 0; i <= mm_count; i++) {
  130. int bw = w % 4 != 0 ? w + (4 - w % 4) : w;
  131. int bh = h % 4 != 0 ? h + (4 - h % 4) : h;
  132. int src_ofs = p_image->get_mipmap_offset(i);
  133. squish::CompressImage(&rb[src_ofs], w, h, &wb[dst_ofs], squish_comp);
  134. dst_ofs += (MAX(4, bw) * MAX(4, bh)) >> shift;
  135. w >>= 1;
  136. h >>= 1;
  137. }
  138. rb = PoolVector<uint8_t>::Read();
  139. wb = PoolVector<uint8_t>::Write();
  140. p_image->create(p_image->get_width(), p_image->get_height(), p_image->has_mipmaps(), target_format, data);
  141. }
  142. }