|
| 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 | + |
| 31 | +#include "image_decompress_bcdec.h" |
| 32 | + |
| 33 | +#include "core/os/os.h" |
| 34 | +#include "core/string/print_string.h" |
| 35 | + |
| 36 | +#define BCDEC_IMPLEMENTATION |
| 37 | +#include "thirdparty/misc/bcdec.h" |
| 38 | + |
| 39 | +inline void bcdec_bc6h_half_s(const void *compressedBlock, void *decompressedBlock, int destinationPitch) { |
| 40 | + bcdec_bc6h_half(compressedBlock, decompressedBlock, destinationPitch, true); |
| 41 | +} |
| 42 | + |
| 43 | +inline void bcdec_bc6h_half_u(const void *compressedBlock, void *decompressedBlock, int destinationPitch) { |
| 44 | + bcdec_bc6h_half(compressedBlock, decompressedBlock, destinationPitch, false); |
| 45 | +} |
| 46 | + |
| 47 | +static void decompress_image(BCdecFormat format, const void *src, void *dst, const uint64_t width, const uint64_t height) { |
| 48 | + const uint8_t *src_blocks = reinterpret_cast<const uint8_t *>(src); |
| 49 | + uint8_t *dec_blocks = reinterpret_cast<uint8_t *>(dst); |
| 50 | + uint64_t src_pos = 0, dst_pos = 0; |
| 51 | + |
| 52 | +#define DECOMPRESS_LOOP(func, block_size, color_bytesize, color_components) \ |
| 53 | + for (uint64_t y = 0; y < height; y += 4) { \ |
| 54 | + for (uint64_t x = 0; x < width; x += 4) { \ |
| 55 | + func(&src_blocks[src_pos], &dec_blocks[dst_pos], width *color_components); \ |
| 56 | + src_pos += block_size; \ |
| 57 | + dst_pos += 4 * color_bytesize; \ |
| 58 | + } \ |
| 59 | + dst_pos += 3 * width * color_bytesize; \ |
| 60 | + } |
| 61 | + |
| 62 | + switch (format) { |
| 63 | + case BCdec_BC1: { |
| 64 | + DECOMPRESS_LOOP(bcdec_bc1, BCDEC_BC1_BLOCK_SIZE, 4, 4) |
| 65 | + } break; |
| 66 | + case BCdec_BC2: { |
| 67 | + DECOMPRESS_LOOP(bcdec_bc2, BCDEC_BC2_BLOCK_SIZE, 4, 4) |
| 68 | + } break; |
| 69 | + case BCdec_BC3: { |
| 70 | + DECOMPRESS_LOOP(bcdec_bc3, BCDEC_BC3_BLOCK_SIZE, 4, 4) |
| 71 | + } break; |
| 72 | + case BCdec_BC4: { |
| 73 | + DECOMPRESS_LOOP(bcdec_bc4, BCDEC_BC4_BLOCK_SIZE, 1, 1) |
| 74 | + } break; |
| 75 | + case BCdec_BC5: { |
| 76 | + DECOMPRESS_LOOP(bcdec_bc5, BCDEC_BC5_BLOCK_SIZE, 2, 2) |
| 77 | + } break; |
| 78 | + case BCdec_BC6U: { |
| 79 | + DECOMPRESS_LOOP(bcdec_bc6h_half_u, BCDEC_BC6H_BLOCK_SIZE, 6, 3) |
| 80 | + } break; |
| 81 | + case BCdec_BC6S: { |
| 82 | + DECOMPRESS_LOOP(bcdec_bc6h_half_s, BCDEC_BC6H_BLOCK_SIZE, 6, 3) |
| 83 | + } break; |
| 84 | + case BCdec_BC7: { |
| 85 | + DECOMPRESS_LOOP(bcdec_bc7, BCDEC_BC7_BLOCK_SIZE, 4, 4) |
| 86 | + } break; |
| 87 | + } |
| 88 | + |
| 89 | +#undef DECOMPRESS_LOOP |
| 90 | +} |
| 91 | + |
| 92 | +void image_decompress_bcdec(Image *p_image) { |
| 93 | + uint64_t start_time = OS::get_singleton()->get_ticks_msec(); |
| 94 | + |
| 95 | + int w = p_image->get_width(); |
| 96 | + int h = p_image->get_height(); |
| 97 | + |
| 98 | + Image::Format source_format = p_image->get_format(); |
| 99 | + Image::Format target_format = Image::FORMAT_MAX; |
| 100 | + |
| 101 | + BCdecFormat bcdec_format = BCdec_BC1; |
| 102 | + |
| 103 | + switch (source_format) { |
| 104 | + case Image::FORMAT_DXT1: |
| 105 | + bcdec_format = BCdec_BC1; |
| 106 | + target_format = Image::FORMAT_RGBA8; |
| 107 | + break; |
| 108 | + |
| 109 | + case Image::FORMAT_DXT3: |
| 110 | + bcdec_format = BCdec_BC2; |
| 111 | + target_format = Image::FORMAT_RGBA8; |
| 112 | + break; |
| 113 | + |
| 114 | + case Image::FORMAT_DXT5: |
| 115 | + case Image::FORMAT_DXT5_RA_AS_RG: |
| 116 | + bcdec_format = BCdec_BC3; |
| 117 | + target_format = Image::FORMAT_RGBA8; |
| 118 | + break; |
| 119 | + |
| 120 | + case Image::FORMAT_RGTC_R: |
| 121 | + bcdec_format = BCdec_BC4; |
| 122 | + target_format = Image::FORMAT_R8; |
| 123 | + break; |
| 124 | + |
| 125 | + case Image::FORMAT_RGTC_RG: |
| 126 | + bcdec_format = BCdec_BC5; |
| 127 | + target_format = Image::FORMAT_RG8; |
| 128 | + break; |
| 129 | + |
| 130 | + case Image::FORMAT_BPTC_RGBFU: |
| 131 | + bcdec_format = BCdec_BC6U; |
| 132 | + target_format = Image::FORMAT_RGBH; |
| 133 | + break; |
| 134 | + |
| 135 | + case Image::FORMAT_BPTC_RGBF: |
| 136 | + bcdec_format = BCdec_BC6S; |
| 137 | + target_format = Image::FORMAT_RGBH; |
| 138 | + break; |
| 139 | + |
| 140 | + case Image::FORMAT_BPTC_RGBA: |
| 141 | + bcdec_format = BCdec_BC7; |
| 142 | + target_format = Image::FORMAT_RGBA8; |
| 143 | + break; |
| 144 | + |
| 145 | + default: |
| 146 | + ERR_FAIL_MSG("bcdec: Can't decompress unknown format: " + Image::get_format_name(source_format) + "."); |
| 147 | + break; |
| 148 | + } |
| 149 | + |
| 150 | + int mm_count = p_image->get_mipmap_count(); |
| 151 | + int64_t target_size = Image::get_image_data_size(w, h, target_format, p_image->has_mipmaps()); |
| 152 | + |
| 153 | + Vector<uint8_t> data; |
| 154 | + data.resize(target_size); |
| 155 | + |
| 156 | + const uint8_t *rb = p_image->get_data().ptr(); |
| 157 | + uint8_t *wb = data.ptrw(); |
| 158 | + |
| 159 | + // Decompress mipmaps. |
| 160 | + for (int i = 0; i <= mm_count; i++) { |
| 161 | + int64_t src_ofs = 0, mipmap_size = 0; |
| 162 | + int mipmap_w = 0, mipmap_h = 0; |
| 163 | + p_image->get_mipmap_offset_size_and_dimensions(i, src_ofs, mipmap_size, mipmap_w, mipmap_h); |
| 164 | + |
| 165 | + int64_t dst_ofs = Image::get_image_mipmap_offset(p_image->get_width(), p_image->get_height(), target_format, i); |
| 166 | + decompress_image(bcdec_format, rb + src_ofs, wb + dst_ofs, mipmap_w, mipmap_h); |
| 167 | + |
| 168 | + w >>= 1; |
| 169 | + h >>= 1; |
| 170 | + } |
| 171 | + |
| 172 | + p_image->set_data(p_image->get_width(), p_image->get_height(), p_image->has_mipmaps(), target_format, data); |
| 173 | + |
| 174 | + // Swap channels if necessary. |
| 175 | + if (source_format == Image::FORMAT_DXT5_RA_AS_RG) { |
| 176 | + p_image->convert_ra_rgba8_to_rg(); |
| 177 | + } |
| 178 | + |
| 179 | + print_verbose(vformat("bcdec: Decompression of a %dx%d %s image with %d mipmaps took %d ms.", |
| 180 | + 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)); |
| 181 | +} |
0 commit comments