Debian Codegraph @

modules/etcpak/image_decompress_etcpak.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* image_decompress_etcpak.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_etcpak.h"
32
33 #include "core/os/os.h"
34 #include "core/string/print_string.h"
35
36 #include <DecodeRGB.hpp>
37
38 #define ETCPAK_R_BLOCK_SIZE 8
39 #define ETCPAK_RG_BLOCK_SIZE 16
40 #define ETCPAK_RGB_BLOCK_SIZE 8
41 #define ETCPAK_RGBA_BLOCK_SIZE 16
42
43 template <void (*decompress_func)(const void *, void *, size_t), int block_size, int pixel_size>
44 static inline void _safe_decompress_mipmap(int width, int height, const uint8_t *src, uint8_t *dst) {
45 // A stack-allocated output buffer large enough to contain an entire uncompressed block.
46 uint8_t temp_buf[4 * 4 * pixel_size];
47
48 // The amount of misaligned pixels on each axis.
49 const int width_diff = width - (width & ~0x03);
50 const int height_diff = height - (height & ~0x03);
51
52 // The amount of uncompressed blocks on each axis.
53 const int width_blocks = (width & ~0x03) / 4;
54 const int height_blocks = (height & ~0x03) / 4;
55
56 // The pitch of the image in bytes.
57 const int image_pitch = width * pixel_size;
58 // The pitch of a block in bytes.
59 const int block_pitch = 4 * pixel_size;
60 // The pitch of the last block in bytes.
61 const int odd_pitch = width_diff * pixel_size;
62
63 size_t src_pos = 0;
64 size_t dst_pos = 0;
65
66 // Decompress the blocks, starting from the top.
67 for (int y = 0; y < height_blocks; y += 1) {
68 // Decompress the blocks, starting from the left.
69 for (int x = 0; x < width_blocks; x += 1) {
70 decompress_func(&src[src_pos], &dst[dst_pos], width);
71 src_pos += block_size;
72 dst_pos += block_pitch;
73 }
74
75 // Decompress the block on the right.
76 if (width_diff > 0) {
77 decompress_func(&src[src_pos], temp_buf, 4);
78
79 // Copy the data from the temporary buffer to the output.
80 for (int i = 0; i < 4; i++) {
81 memcpy(&dst[dst_pos + i * image_pitch], &temp_buf[i * block_pitch], odd_pitch);
82 }
83
84 src_pos += block_size;
85 dst_pos += odd_pitch;
86 }
87
88 // Skip to the next row of blocks, the current one has already been filled.
89 dst_pos += 3 * image_pitch;
90 }
91
92 // Decompress the blocks at the bottom of the image.
93 if (height_diff > 0) {
94 // Decompress the blocks at the bottom.
95 for (int x = 0; x < width_blocks; x += 1) {
96 decompress_func(&src[src_pos], temp_buf, 4);
97
98 // Copy the data from the temporary buffer to the output.
99 for (int i = 0; i < height_diff; i++) {
100 memcpy(&dst[dst_pos + i * image_pitch], &temp_buf[i * block_pitch], block_pitch);
101 }
102
103 src_pos += block_size;
104 dst_pos += block_pitch;
105 }
106
107 // Decompress the block in the lower-right corner.
108 if (width_diff > 0) {
109 decompress_func(&src[src_pos], temp_buf, 4);
110
111 // Copy the data from the temporary buffer to the output.
112 for (int i = 0; i < height_diff; i++) {
113 memcpy(&dst[dst_pos + i * image_pitch], &temp_buf[i * block_pitch], odd_pitch);
114 }
115
116 src_pos += block_size;
117 dst_pos += odd_pitch;
118 }
119 }
120 }
121
122 template <void (*decompress_func)(const void *, void *, size_t), int block_size, int pixel_size>
123 static inline void _decompress_mipmap(int width, int height, const uint8_t *src, uint8_t *dst) {
124 size_t src_pos = 0;
125 size_t dst_pos = 0;
126
127 // The size of a single block in bytes.
128 const int block_pitch = 4 * pixel_size;
129
130 for (int y = 0; y < height; y += 4) {
131 for (int x = 0; x < width; x += 4) {
132 decompress_func(&src[src_pos], &dst[dst_pos], width);
133 src_pos += block_size;
134 dst_pos += block_pitch;
135 }
136
137 // Skip to the next row of blocks, the current one has already been filled.
138 dst_pos += 3 * width * pixel_size;
139 }
140 }
141
142 static void decompress_image(EtcpakFormat format, const void *src, void *dst, const uint64_t width, const uint64_t height) {
143 const uint8_t *src_blocks = reinterpret_cast<const uint8_t *>(src);
144 uint8_t *dec_blocks = reinterpret_cast<uint8_t *>(dst);
145
146 const uint64_t aligned_width = (width + 3) & ~0x03;
147 const uint64_t aligned_height = (height + 3) & ~0x03;
148
149 if (width != aligned_width || height != aligned_height) {
150 switch (format) {
151 case Etcpak_R: {
152 _safe_decompress_mipmap<DecodeRBlock, ETCPAK_R_BLOCK_SIZE, 4>(width, height, src_blocks, dec_blocks);
153 } break;
154 case Etcpak_RG: {
155 _safe_decompress_mipmap<DecodeRGBlock, ETCPAK_RG_BLOCK_SIZE, 4>(width, height, src_blocks, dec_blocks);
156 } break;
157 case Etcpak_RGB: {
158 _safe_decompress_mipmap<DecodeRGBBlock, ETCPAK_RGB_BLOCK_SIZE, 4>(width, height, src_blocks, dec_blocks);
159 } break;
160 case Etcpak_RGBA: {
161 _safe_decompress_mipmap<DecodeRGBABlock, ETCPAK_RGBA_BLOCK_SIZE, 4>(width, height, src_blocks, dec_blocks);
162 } break;
163 }
164 } else {
165 switch (format) {
166 case Etcpak_R: {
167 _decompress_mipmap<DecodeRBlock, ETCPAK_R_BLOCK_SIZE, 4>(width, height, src_blocks, dec_blocks);
168 } break;
169 case Etcpak_RG: {
170 _decompress_mipmap<DecodeRGBlock, ETCPAK_RG_BLOCK_SIZE, 4>(width, height, src_blocks, dec_blocks);
171 } break;
172 case Etcpak_RGB: {
173 _decompress_mipmap<DecodeRGBBlock, ETCPAK_RGB_BLOCK_SIZE, 4>(width, height, src_blocks, dec_blocks);
174 } break;
175 case Etcpak_RGBA: {
176 _decompress_mipmap<DecodeRGBABlock, ETCPAK_RGBA_BLOCK_SIZE, 4>(width, height, src_blocks, dec_blocks);
177 } break;
178 }
179 }
180 }
181
182 void _decompress_etc(Image *p_image) {
183 uint64_t start_time = OS::get_singleton()->get_ticks_msec();
184
185 int width = p_image->get_width();
186 int height = p_image->get_height();
187
188 Image::Format source_format = p_image->get_format();
189 Image::Format target_format = Image::FORMAT_RGBA8;
190
191 EtcpakFormat etcpak_format = Etcpak_R;
192
193 switch (source_format) {
194 case Image::FORMAT_ETC:
195 case Image::FORMAT_ETC2_RGB8:
196 etcpak_format = Etcpak_RGB;
197 break;
198
199 case Image::FORMAT_ETC2_RGBA8:
200 case Image::FORMAT_ETC2_RA_AS_RG:
201 etcpak_format = Etcpak_RGBA;
202 break;
203
204 case Image::FORMAT_ETC2_R11:
205 etcpak_format = Etcpak_R;
206 break;
207
208 case Image::FORMAT_ETC2_RG11:
209 etcpak_format = Etcpak_RG;
210 break;
211
212 default:
213 ERR_FAIL_MSG(vformat("etcpak: Can't decompress image %s with an unknown format: %s.", p_image->get_path(), Image::get_format_name(source_format)));
214 break;
215 }
216
217 int mm_count = p_image->get_mipmap_count();
218 int64_t target_size = Image::get_image_data_size(width, height, target_format, p_image->has_mipmaps());
219
220 // Decompressed data.
221 Vector<uint8_t> data;
222 data.resize(target_size);
223 uint8_t *wb = data.ptrw();
224
225 // Source data.
226 const uint8_t *rb = p_image->ptr();
227
228 // Decompress mipmaps.
229 for (int i = 0; i <= mm_count; i++) {
230 int mipmap_w = 0, mipmap_h = 0;
231 int64_t src_ofs = Image::get_image_mipmap_offset(width, height, source_format, i);
232 int64_t dst_ofs = Image::get_image_mipmap_offset_and_dimensions(width, height, target_format, i, mipmap_w, mipmap_h);
233 decompress_image(etcpak_format, rb + src_ofs, wb + dst_ofs, mipmap_w, mipmap_h);
234 }
235
236 p_image->set_data(p_image->get_width(), p_image->get_height(), p_image->has_mipmaps(), target_format, data);
237
238 // Swap channels if the format is using a channel swizzle.
239 if (source_format == Image::FORMAT_ETC2_RA_AS_RG) {
240 p_image->convert_ra_rgba8_to_rg();
241 }
242
243 print_verbose(vformat("etcpak: Decompression of %dx%d %s image %s with %d mipmaps took %d ms.",
244 p_image->get_width(), p_image->get_height(), Image::get_format_name(source_format), p_image->get_path(), p_image->get_mipmap_count(), OS::get_singleton()->get_ticks_msec() - start_time));
245 }