Debian Codegraph @

modules/bcdec/image_decompress_bcdec.cpp

raw symbols SCIP
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 template <void (*decompress_func)(const void *, void *, int), int block_size, int pixel_size, int component_size>
48 static inline void _safe_decompress_mipmap(int width, int height, const uint8_t *src, uint8_t *dst) {
49 // A stack-allocated output buffer large enough to contain an entire uncompressed block.
50 uint8_t temp_buf[4 * 4 * pixel_size];
51
52 // The amount of misaligned pixels on each axis.
53 const int width_diff = width - (width & ~0x03);
54 const int height_diff = height - (height & ~0x03);
55
56 // The amount of uncompressed blocks on each axis.
57 const int width_blocks = (width & ~0x03) / 4;
58 const int height_blocks = (height & ~0x03) / 4;
59
60 // The pitch of the image in bytes.
61 const int image_pitch = width * pixel_size;
62 // The pitch of a block in bytes.
63 const int block_pitch = 4 * pixel_size;
64 // The pitch of the last block in bytes.
65 const int odd_pitch = width_diff * pixel_size;
66
67 size_t src_pos = 0;
68 size_t dst_pos = 0;
69
70 // Decompress the blocks, starting from the top.
71 for (int y = 0; y < height_blocks; y += 1) {
72 // Decompress the blocks, starting from the left.
73 for (int x = 0; x < width_blocks; x += 1) {
74 decompress_func(&src[src_pos], &dst[dst_pos], image_pitch / component_size);
75 src_pos += block_size;
76 dst_pos += block_pitch;
77 }
78
79 // Decompress the block on the right.
80 if (width_diff > 0) {
81 decompress_func(&src[src_pos], temp_buf, block_pitch / component_size);
82
83 // Copy the data from the temporary buffer to the output.
84 for (int i = 0; i < 4; i++) {
85 memcpy(&dst[dst_pos + i * image_pitch], &temp_buf[i * block_pitch], odd_pitch);
86 }
87
88 src_pos += block_size;
89 dst_pos += odd_pitch;
90 }
91
92 // Skip to the next row of blocks, the current one has already been filled.
93 dst_pos += 3 * image_pitch;
94 }
95
96 // Decompress the blocks at the bottom of the image.
97 if (height_diff > 0) {
98 // Decompress the blocks at the bottom.
99 for (int x = 0; x < width_blocks; x += 1) {
100 decompress_func(&src[src_pos], temp_buf, block_pitch / component_size);
101
102 // Copy the data from the temporary buffer to the output.
103 for (int i = 0; i < height_diff; i++) {
104 memcpy(&dst[dst_pos + i * image_pitch], &temp_buf[i * block_pitch], block_pitch);
105 }
106
107 src_pos += block_size;
108 dst_pos += block_pitch;
109 }
110
111 // Decompress the block in the lower-right corner.
112 if (width_diff > 0) {
113 decompress_func(&src[src_pos], temp_buf, block_pitch / component_size);
114
115 // Copy the data from the temporary buffer to the output.
116 for (int i = 0; i < height_diff; i++) {
117 memcpy(&dst[dst_pos + i * image_pitch], &temp_buf[i * block_pitch], odd_pitch);
118 }
119
120 src_pos += block_size;
121 dst_pos += odd_pitch;
122 }
123 }
124 }
125
126 template <void (*decompress_func)(const void *, void *, int), int block_size, int pixel_size, int component_size>
127 static inline void _decompress_mipmap(int width, int height, const uint8_t *src, uint8_t *dst) {
128 size_t src_pos = 0;
129 size_t dst_pos = 0;
130
131 // The size of a single block in bytes.
132 const int block_pitch = 4 * pixel_size;
133 // The pitch of the image in bytes.
134 const int image_pitch = width * pixel_size;
135
136 for (int y = 0; y < height; y += 4) {
137 for (int x = 0; x < width; x += 4) {
138 decompress_func(&src[src_pos], &dst[dst_pos], image_pitch / component_size);
139 src_pos += block_size;
140 dst_pos += block_pitch;
141 }
142
143 // Skip to the next row of blocks, the current one has already been filled.
144 dst_pos += 3 * image_pitch;
145 }
146 }
147
148 static void decompress_image(BCdecFormat format, const void *src, void *dst, const uint64_t width, const uint64_t height) {
149 const uint8_t *src_blocks = reinterpret_cast<const uint8_t *>(src);
150 uint8_t *dec_blocks = reinterpret_cast<uint8_t *>(dst);
151
152 const uint64_t aligned_width = (width + 3) & ~0x03;
153 const uint64_t aligned_height = (height + 3) & ~0x03;
154
155 if (width != aligned_width || height != aligned_height) {
156 // Decompress the mipmap in a 'safe' way, which involves starting from the top left.
157 // For each block row, decompress all of the 'full' blocks, then the misaligned one (on the x axis).
158 // Then, decompress the final misaligned block row at the bottom.
159 // Finally, decompress the misaligned block at the bottom right.
160 switch (format) {
161 case BCdec_BC1: {
162 _safe_decompress_mipmap<bcdec_bc1, BCDEC_BC1_BLOCK_SIZE, 4, 1>(width, height, src_blocks, dec_blocks);
163 } break;
164 case BCdec_BC2: {
165 _safe_decompress_mipmap<bcdec_bc2, BCDEC_BC2_BLOCK_SIZE, 4, 1>(width, height, src_blocks, dec_blocks);
166 } break;
167 case BCdec_BC3: {
168 _safe_decompress_mipmap<bcdec_bc3, BCDEC_BC3_BLOCK_SIZE, 4, 1>(width, height, src_blocks, dec_blocks);
169 } break;
170 case BCdec_BC4: {
171 _safe_decompress_mipmap<bcdec_bc4, BCDEC_BC4_BLOCK_SIZE, 1, 1>(width, height, src_blocks, dec_blocks);
172 } break;
173 case BCdec_BC5: {
174 _safe_decompress_mipmap<bcdec_bc5, BCDEC_BC5_BLOCK_SIZE, 2, 1>(width, height, src_blocks, dec_blocks);
175 } break;
176 case BCdec_BC6U: {
177 _safe_decompress_mipmap<bcdec_bc6h_half_u, BCDEC_BC6H_BLOCK_SIZE, 6, 2>(width, height, src_blocks, dec_blocks);
178 } break;
179 case BCdec_BC6S: {
180 _safe_decompress_mipmap<bcdec_bc6h_half_s, BCDEC_BC6H_BLOCK_SIZE, 6, 2>(width, height, src_blocks, dec_blocks);
181 } break;
182 case BCdec_BC7: {
183 _safe_decompress_mipmap<bcdec_bc7, BCDEC_BC7_BLOCK_SIZE, 4, 1>(width, height, src_blocks, dec_blocks);
184 } break;
185 }
186 } else {
187 // Just decompress as usual, as fast as possible.
188 switch (format) {
189 case BCdec_BC1: {
190 _decompress_mipmap<bcdec_bc1, BCDEC_BC1_BLOCK_SIZE, 4, 1>(width, height, src_blocks, dec_blocks);
191 } break;
192 case BCdec_BC2: {
193 _decompress_mipmap<bcdec_bc2, BCDEC_BC2_BLOCK_SIZE, 4, 1>(width, height, src_blocks, dec_blocks);
194 } break;
195 case BCdec_BC3: {
196 _decompress_mipmap<bcdec_bc3, BCDEC_BC3_BLOCK_SIZE, 4, 1>(width, height, src_blocks, dec_blocks);
197 } break;
198 case BCdec_BC4: {
199 _decompress_mipmap<bcdec_bc4, BCDEC_BC4_BLOCK_SIZE, 1, 1>(width, height, src_blocks, dec_blocks);
200 } break;
201 case BCdec_BC5: {
202 _decompress_mipmap<bcdec_bc5, BCDEC_BC5_BLOCK_SIZE, 2, 1>(width, height, src_blocks, dec_blocks);
203 } break;
204 case BCdec_BC6U: {
205 _decompress_mipmap<bcdec_bc6h_half_u, BCDEC_BC6H_BLOCK_SIZE, 6, 2>(width, height, src_blocks, dec_blocks);
206 } break;
207 case BCdec_BC6S: {
208 _decompress_mipmap<bcdec_bc6h_half_s, BCDEC_BC6H_BLOCK_SIZE, 6, 2>(width, height, src_blocks, dec_blocks);
209 } break;
210 case BCdec_BC7: {
211 _decompress_mipmap<bcdec_bc7, BCDEC_BC7_BLOCK_SIZE, 4, 1>(width, height, src_blocks, dec_blocks);
212 } break;
213 }
214 }
215 }
216
217 void image_decompress_bcdec(Image *p_image) {
218 uint64_t start_time = OS::get_singleton()->get_ticks_msec();
219
220 int width = p_image->get_width();
221 int height = p_image->get_height();
222
223 Image::Format source_format = p_image->get_format();
224 Image::Format target_format = Image::FORMAT_MAX;
225
226 BCdecFormat bcdec_format = BCdec_BC1;
227
228 switch (source_format) {
229 case Image::FORMAT_DXT1:
230 bcdec_format = BCdec_BC1;
231 target_format = Image::FORMAT_RGBA8;
232 break;
233
234 case Image::FORMAT_DXT3:
235 bcdec_format = BCdec_BC2;
236 target_format = Image::FORMAT_RGBA8;
237 break;
238
239 case Image::FORMAT_DXT5:
240 case Image::FORMAT_DXT5_RA_AS_RG:
241 bcdec_format = BCdec_BC3;
242 target_format = Image::FORMAT_RGBA8;
243 break;
244
245 case Image::FORMAT_RGTC_R:
246 bcdec_format = BCdec_BC4;
247 target_format = Image::FORMAT_R8;
248 break;
249
250 case Image::FORMAT_RGTC_RG:
251 bcdec_format = BCdec_BC5;
252 target_format = Image::FORMAT_RG8;
253 break;
254
255 case Image::FORMAT_BPTC_RGBFU:
256 bcdec_format = BCdec_BC6U;
257 target_format = Image::FORMAT_RGBH;
258 break;
259
260 case Image::FORMAT_BPTC_RGBF:
261 bcdec_format = BCdec_BC6S;
262 target_format = Image::FORMAT_RGBH;
263 break;
264
265 case Image::FORMAT_BPTC_RGBA:
266 bcdec_format = BCdec_BC7;
267 target_format = Image::FORMAT_RGBA8;
268 break;
269
270 default:
271 ERR_FAIL_MSG("bcdec: Can't decompress unknown format: " + Image::get_format_name(source_format) + ".");
272 break;
273 }
274
275 int mm_count = p_image->get_mipmap_count();
276 int64_t target_size = Image::get_image_data_size(width, height, target_format, p_image->has_mipmaps());
277
278 // Decompressed data.
279 Vector<uint8_t> data;
280 data.resize(target_size);
281 uint8_t *wb = data.ptrw();
282
283 // Source data.
284 const uint8_t *rb = p_image->get_data().ptr();
285
286 // Decompress mipmaps.
287 for (int i = 0; i <= mm_count; i++) {
288 int mipmap_w = 0, mipmap_h = 0;
289 int64_t src_ofs = Image::get_image_mipmap_offset(width, height, source_format, i);
290 int64_t dst_ofs = Image::get_image_mipmap_offset_and_dimensions(width, height, target_format, i, mipmap_w, mipmap_h);
291 decompress_image(bcdec_format, rb + src_ofs, wb + dst_ofs, mipmap_w, mipmap_h);
292 }
293
294 p_image->set_data(width, height, p_image->has_mipmaps(), target_format, data);
295
296 // Swap channels if the format is using a channel swizzle.
297 if (source_format == Image::FORMAT_DXT5_RA_AS_RG) {
298 p_image->convert_ra_rgba8_to_rg();
299 }
300
301 print_verbose(vformat("bcdec: Decompression of a %dx%d %s image with %d mipmaps took %d ms.",
302 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));
303 }