image_decompress_bcdec.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /**************************************************************************/
  2. /* image_decompress_bcdec.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_decompress_bcdec.h"
  31. #include "core/os/os.h"
  32. #include "core/string/print_string.h"
  33. #define BCDEC_IMPLEMENTATION
  34. #include "thirdparty/misc/bcdec.h"
  35. inline void bcdec_bc6h_half_s(const void *compressedBlock, void *decompressedBlock, int destinationPitch) {
  36. bcdec_bc6h_half(compressedBlock, decompressedBlock, destinationPitch, true);
  37. }
  38. inline void bcdec_bc6h_half_u(const void *compressedBlock, void *decompressedBlock, int destinationPitch) {
  39. bcdec_bc6h_half(compressedBlock, decompressedBlock, destinationPitch, false);
  40. }
  41. static void decompress_image(BCdecFormat format, const void *src, void *dst, const uint64_t width, const uint64_t height) {
  42. const uint8_t *src_blocks = reinterpret_cast<const uint8_t *>(src);
  43. uint8_t *dec_blocks = reinterpret_cast<uint8_t *>(dst);
  44. #define DECOMPRESS_LOOP(func, block_size, color_bytesize, color_components) \
  45. for (uint64_t y = 0; y < height; y += 4) { \
  46. for (uint64_t x = 0; x < width; x += 4) { \
  47. func(&src_blocks[src_pos], &dec_blocks[dst_pos], width *color_components); \
  48. src_pos += block_size; \
  49. dst_pos += 4 * color_bytesize; \
  50. } \
  51. dst_pos += 3 * width * color_bytesize; \
  52. }
  53. #define DECOMPRESS_LOOP_SAFE(func, block_size, color_bytesize, color_components, output) \
  54. for (uint64_t y = 0; y < height; y += 4) { \
  55. for (uint64_t x = 0; x < width; x += 4) { \
  56. const uint32_t yblock = MIN(height - y, 4ul); \
  57. const uint32_t xblock = MIN(width - x, 4ul); \
  58. \
  59. const bool incomplete = yblock < 4 || xblock < 4; \
  60. uint8_t *dec_out = incomplete ? output : &dec_blocks[y * 4 * width + x * color_bytesize]; \
  61. \
  62. func(&src_blocks[src_pos], dec_out, 4 * color_components); \
  63. src_pos += block_size; \
  64. \
  65. if (incomplete) { \
  66. for (uint32_t cy = 0; cy < yblock; cy++) { \
  67. for (uint32_t cx = 0; cx < xblock; cx++) { \
  68. memcpy(&dec_blocks[(y + cy) * 4 * width + (x + cx) * color_bytesize], &output[cy * 4 + cx * color_bytesize], color_bytesize); \
  69. } \
  70. } \
  71. } \
  72. } \
  73. }
  74. if (width % 4 != 0 || height % 4 != 0) {
  75. uint64_t src_pos = 0;
  76. uint8_t r8_output[4 * 4];
  77. uint8_t rg8_output[4 * 4 * 2];
  78. uint8_t rgba8_output[4 * 4 * 4];
  79. uint8_t rgbh_output[4 * 4 * 6];
  80. switch (format) {
  81. case BCdec_BC1: {
  82. DECOMPRESS_LOOP_SAFE(bcdec_bc1, BCDEC_BC1_BLOCK_SIZE, 4, 4, rgba8_output)
  83. } break;
  84. case BCdec_BC2: {
  85. DECOMPRESS_LOOP_SAFE(bcdec_bc2, BCDEC_BC2_BLOCK_SIZE, 4, 4, rgba8_output)
  86. } break;
  87. case BCdec_BC3: {
  88. DECOMPRESS_LOOP_SAFE(bcdec_bc3, BCDEC_BC3_BLOCK_SIZE, 4, 4, rgba8_output)
  89. } break;
  90. case BCdec_BC4: {
  91. DECOMPRESS_LOOP_SAFE(bcdec_bc4, BCDEC_BC4_BLOCK_SIZE, 1, 1, r8_output)
  92. } break;
  93. case BCdec_BC5: {
  94. DECOMPRESS_LOOP_SAFE(bcdec_bc5, BCDEC_BC5_BLOCK_SIZE, 2, 2, rg8_output)
  95. } break;
  96. case BCdec_BC6U: {
  97. DECOMPRESS_LOOP_SAFE(bcdec_bc6h_half_u, BCDEC_BC6H_BLOCK_SIZE, 6, 3, rgbh_output)
  98. } break;
  99. case BCdec_BC6S: {
  100. DECOMPRESS_LOOP_SAFE(bcdec_bc6h_half_s, BCDEC_BC6H_BLOCK_SIZE, 6, 3, rgbh_output)
  101. } break;
  102. case BCdec_BC7: {
  103. DECOMPRESS_LOOP_SAFE(bcdec_bc7, BCDEC_BC7_BLOCK_SIZE, 4, 4, rgba8_output)
  104. } break;
  105. }
  106. } else {
  107. uint64_t src_pos = 0, dst_pos = 0;
  108. switch (format) {
  109. case BCdec_BC1: {
  110. DECOMPRESS_LOOP(bcdec_bc1, BCDEC_BC1_BLOCK_SIZE, 4, 4)
  111. } break;
  112. case BCdec_BC2: {
  113. DECOMPRESS_LOOP(bcdec_bc2, BCDEC_BC2_BLOCK_SIZE, 4, 4)
  114. } break;
  115. case BCdec_BC3: {
  116. DECOMPRESS_LOOP(bcdec_bc3, BCDEC_BC3_BLOCK_SIZE, 4, 4)
  117. } break;
  118. case BCdec_BC4: {
  119. DECOMPRESS_LOOP(bcdec_bc4, BCDEC_BC4_BLOCK_SIZE, 1, 1)
  120. } break;
  121. case BCdec_BC5: {
  122. DECOMPRESS_LOOP(bcdec_bc5, BCDEC_BC5_BLOCK_SIZE, 2, 2)
  123. } break;
  124. case BCdec_BC6U: {
  125. DECOMPRESS_LOOP(bcdec_bc6h_half_u, BCDEC_BC6H_BLOCK_SIZE, 6, 3)
  126. } break;
  127. case BCdec_BC6S: {
  128. DECOMPRESS_LOOP(bcdec_bc6h_half_s, BCDEC_BC6H_BLOCK_SIZE, 6, 3)
  129. } break;
  130. case BCdec_BC7: {
  131. DECOMPRESS_LOOP(bcdec_bc7, BCDEC_BC7_BLOCK_SIZE, 4, 4)
  132. } break;
  133. }
  134. }
  135. #undef DECOMPRESS_LOOP
  136. #undef DECOMPRESS_LOOP_SAFE
  137. }
  138. void image_decompress_bcdec(Image *p_image) {
  139. uint64_t start_time = OS::get_singleton()->get_ticks_msec();
  140. int width = p_image->get_width();
  141. int height = p_image->get_height();
  142. // Compressed images' dimensions should be padded to the upper multiple of 4.
  143. // If they aren't, they need to be realigned (the actual data is correctly padded though).
  144. const bool need_width_realign = width % 4 != 0;
  145. const bool need_height_realign = height % 4 != 0;
  146. if (need_width_realign || need_height_realign) {
  147. int new_width = need_width_realign ? width + (4 - (width % 4)) : width;
  148. int new_height = need_height_realign ? height + (4 - (height % 4)) : height;
  149. print_verbose(vformat("Compressed image's dimensions are not multiples of 4 (%dx%d), aligning to (%dx%d)", width, height, new_width, new_height));
  150. width = new_width;
  151. height = new_height;
  152. }
  153. Image::Format source_format = p_image->get_format();
  154. Image::Format target_format = Image::FORMAT_MAX;
  155. BCdecFormat bcdec_format = BCdec_BC1;
  156. switch (source_format) {
  157. case Image::FORMAT_DXT1:
  158. bcdec_format = BCdec_BC1;
  159. target_format = Image::FORMAT_RGBA8;
  160. break;
  161. case Image::FORMAT_DXT3:
  162. bcdec_format = BCdec_BC2;
  163. target_format = Image::FORMAT_RGBA8;
  164. break;
  165. case Image::FORMAT_DXT5:
  166. case Image::FORMAT_DXT5_RA_AS_RG:
  167. bcdec_format = BCdec_BC3;
  168. target_format = Image::FORMAT_RGBA8;
  169. break;
  170. case Image::FORMAT_RGTC_R:
  171. bcdec_format = BCdec_BC4;
  172. target_format = Image::FORMAT_R8;
  173. break;
  174. case Image::FORMAT_RGTC_RG:
  175. bcdec_format = BCdec_BC5;
  176. target_format = Image::FORMAT_RG8;
  177. break;
  178. case Image::FORMAT_BPTC_RGBFU:
  179. bcdec_format = BCdec_BC6U;
  180. target_format = Image::FORMAT_RGBH;
  181. break;
  182. case Image::FORMAT_BPTC_RGBF:
  183. bcdec_format = BCdec_BC6S;
  184. target_format = Image::FORMAT_RGBH;
  185. break;
  186. case Image::FORMAT_BPTC_RGBA:
  187. bcdec_format = BCdec_BC7;
  188. target_format = Image::FORMAT_RGBA8;
  189. break;
  190. default:
  191. ERR_FAIL_MSG("bcdec: Can't decompress unknown format: " + Image::get_format_name(source_format) + ".");
  192. break;
  193. }
  194. int mm_count = p_image->get_mipmap_count();
  195. int64_t target_size = Image::get_image_data_size(width, height, target_format, p_image->has_mipmaps());
  196. // Decompressed data.
  197. Vector<uint8_t> data;
  198. data.resize(target_size);
  199. uint8_t *wb = data.ptrw();
  200. // Source data.
  201. const uint8_t *rb = p_image->get_data().ptr();
  202. // Decompress mipmaps.
  203. for (int i = 0; i <= mm_count; i++) {
  204. int mipmap_w = 0, mipmap_h = 0;
  205. int64_t src_ofs = Image::get_image_mipmap_offset_and_dimensions(width, height, source_format, i, mipmap_w, mipmap_h);
  206. int64_t dst_ofs = Image::get_image_mipmap_offset(width, height, target_format, i);
  207. decompress_image(bcdec_format, rb + src_ofs, wb + dst_ofs, mipmap_w, mipmap_h);
  208. }
  209. p_image->set_data(width, height, p_image->has_mipmaps(), target_format, data);
  210. // Swap channels if the format is using a channel swizzle.
  211. if (source_format == Image::FORMAT_DXT5_RA_AS_RG) {
  212. p_image->convert_ra_rgba8_to_rg();
  213. }
  214. print_verbose(vformat("bcdec: Decompression of a %dx%d %s image with %d mipmaps took %d ms.",
  215. p_image->get_width(), p_image->get_height(), Image::get_format_name(source_format), p_image->get_mipmap_count(), OS::get_singleton()->get_ticks_msec() - start_time));
  216. }