|
| 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 | +static void decompress_image(EtcpakFormat format, const void *src, void *dst, const uint64_t width, const uint64_t height) { |
| 44 | + const uint8_t *src_blocks = reinterpret_cast<const uint8_t *>(src); |
| 45 | + uint8_t *dec_blocks = reinterpret_cast<uint8_t *>(dst); |
| 46 | + |
| 47 | +#define DECOMPRESS_LOOP(m_func, m_block_size, m_color_bytesize) \ |
| 48 | + for (uint64_t y = 0; y < height; y += 4) { \ |
| 49 | + for (uint64_t x = 0; x < width; x += 4) { \ |
| 50 | + m_func(&src_blocks[src_pos], &dec_blocks[dst_pos], width); \ |
| 51 | + src_pos += m_block_size; \ |
| 52 | + dst_pos += 4 * m_color_bytesize; \ |
| 53 | + } \ |
| 54 | + dst_pos += 3 * width * m_color_bytesize; \ |
| 55 | + } |
| 56 | + |
| 57 | +#define DECOMPRESS_LOOP_SAFE(m_func, m_block_size, m_color_bytesize, m_output) \ |
| 58 | + for (uint64_t y = 0; y < height; y += 4) { \ |
| 59 | + for (uint64_t x = 0; x < width; x += 4) { \ |
| 60 | + const uint32_t yblock = MIN(height - y, 4ul); \ |
| 61 | + const uint32_t xblock = MIN(width - x, 4ul); \ |
| 62 | + \ |
| 63 | + const bool incomplete = yblock < 4 && xblock < 4; \ |
| 64 | + uint8_t *dec_out = incomplete ? m_output : &dec_blocks[y * 4 * width + x * m_color_bytesize]; \ |
| 65 | + \ |
| 66 | + m_func(&src_blocks[src_pos], dec_out, incomplete ? 4 : width); \ |
| 67 | + src_pos += m_block_size; \ |
| 68 | + \ |
| 69 | + if (incomplete) { \ |
| 70 | + for (uint32_t cy = 0; cy < yblock; cy++) { \ |
| 71 | + for (uint32_t cx = 0; cx < xblock; cx++) { \ |
| 72 | + memcpy(&dec_blocks[(y + cy) * 4 * width + (x + cx) * m_color_bytesize], &m_output[cy * 4 + cx * m_color_bytesize], m_color_bytesize); \ |
| 73 | + } \ |
| 74 | + } \ |
| 75 | + } \ |
| 76 | + } \ |
| 77 | + } |
| 78 | + |
| 79 | + if (width % 4 != 0 || height % 4 != 0) { |
| 80 | + uint64_t src_pos = 0; |
| 81 | + |
| 82 | + uint8_t rgba8_output[4 * 4 * 4]; |
| 83 | + |
| 84 | + switch (format) { |
| 85 | + case Etcpak_R: { |
| 86 | + DECOMPRESS_LOOP_SAFE(DecodeRBlock, ETCPAK_R_BLOCK_SIZE, 4, rgba8_output) |
| 87 | + } break; |
| 88 | + case Etcpak_RG: { |
| 89 | + DECOMPRESS_LOOP_SAFE(DecodeRGBlock, ETCPAK_RG_BLOCK_SIZE, 4, rgba8_output) |
| 90 | + } break; |
| 91 | + case Etcpak_RGB: { |
| 92 | + DECOMPRESS_LOOP_SAFE(DecodeRGBBlock, ETCPAK_RGB_BLOCK_SIZE, 4, rgba8_output) |
| 93 | + } break; |
| 94 | + case Etcpak_RGBA: { |
| 95 | + DECOMPRESS_LOOP_SAFE(DecodeRGBABlock, ETCPAK_RGBA_BLOCK_SIZE, 4, rgba8_output) |
| 96 | + } break; |
| 97 | + } |
| 98 | + |
| 99 | + } else { |
| 100 | + uint64_t src_pos = 0, dst_pos = 0; |
| 101 | + |
| 102 | + switch (format) { |
| 103 | + case Etcpak_R: { |
| 104 | + DECOMPRESS_LOOP(DecodeRBlock, ETCPAK_R_BLOCK_SIZE, 4) |
| 105 | + } break; |
| 106 | + case Etcpak_RG: { |
| 107 | + DECOMPRESS_LOOP(DecodeRGBlock, ETCPAK_RG_BLOCK_SIZE, 4) |
| 108 | + } break; |
| 109 | + case Etcpak_RGB: { |
| 110 | + DECOMPRESS_LOOP(DecodeRGBBlock, ETCPAK_RGB_BLOCK_SIZE, 4) |
| 111 | + } break; |
| 112 | + case Etcpak_RGBA: { |
| 113 | + DECOMPRESS_LOOP(DecodeRGBABlock, ETCPAK_RGBA_BLOCK_SIZE, 4) |
| 114 | + } break; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | +#undef DECOMPRESS_LOOP |
| 119 | +#undef DECOMPRESS_LOOP_SAFE |
| 120 | +} |
| 121 | + |
| 122 | +void _decompress_etc(Image *p_image) { |
| 123 | + uint64_t start_time = OS::get_singleton()->get_ticks_msec(); |
| 124 | + |
| 125 | + int width = p_image->get_width(); |
| 126 | + int height = p_image->get_height(); |
| 127 | + |
| 128 | + // Compressed images' dimensions should be padded to the upper multiple of 4. |
| 129 | + // If they aren't, they need to be realigned (the actual data is correctly padded though). |
| 130 | + if (width % 4 != 0 || height % 4 != 0) { |
| 131 | + int new_width = width + (4 - (width % 4)); |
| 132 | + int new_height = height + (4 - (height % 4)); |
| 133 | + |
| 134 | + print_verbose(vformat("Compressed image (%s) has dimensions are not multiples of 4 (%dx%d), aligning to (%dx%d)", p_image->get_path(), width, height, new_width, new_height)); |
| 135 | + |
| 136 | + width = new_width; |
| 137 | + height = new_height; |
| 138 | + } |
| 139 | + |
| 140 | + Image::Format source_format = p_image->get_format(); |
| 141 | + Image::Format target_format = Image::FORMAT_RGBA8; |
| 142 | + |
| 143 | + EtcpakFormat etcpak_format = Etcpak_R; |
| 144 | + |
| 145 | + switch (source_format) { |
| 146 | + case Image::FORMAT_ETC: |
| 147 | + case Image::FORMAT_ETC2_RGB8: |
| 148 | + etcpak_format = Etcpak_RGB; |
| 149 | + break; |
| 150 | + |
| 151 | + case Image::FORMAT_ETC2_RGBA8: |
| 152 | + case Image::FORMAT_ETC2_RA_AS_RG: |
| 153 | + etcpak_format = Etcpak_RGBA; |
| 154 | + break; |
| 155 | + |
| 156 | + case Image::FORMAT_ETC2_R11: |
| 157 | + etcpak_format = Etcpak_R; |
| 158 | + break; |
| 159 | + |
| 160 | + case Image::FORMAT_ETC2_RG11: |
| 161 | + etcpak_format = Etcpak_RG; |
| 162 | + break; |
| 163 | + |
| 164 | + default: |
| 165 | + 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))); |
| 166 | + break; |
| 167 | + } |
| 168 | + |
| 169 | + int mm_count = p_image->get_mipmap_count(); |
| 170 | + int64_t target_size = Image::get_image_data_size(width, height, target_format, p_image->has_mipmaps()); |
| 171 | + |
| 172 | + // Decompressed data. |
| 173 | + Vector<uint8_t> data; |
| 174 | + data.resize(target_size); |
| 175 | + uint8_t *wb = data.ptrw(); |
| 176 | + |
| 177 | + // Source data. |
| 178 | + const uint8_t *rb = p_image->ptr(); |
| 179 | + |
| 180 | + // Decompress mipmaps. |
| 181 | + for (int i = 0; i <= mm_count; i++) { |
| 182 | + int mipmap_w = 0, mipmap_h = 0; |
| 183 | + int64_t src_ofs = Image::get_image_mipmap_offset_and_dimensions(width, height, source_format, i, mipmap_w, mipmap_h); |
| 184 | + int64_t dst_ofs = Image::get_image_mipmap_offset(width, height, target_format, i); |
| 185 | + decompress_image(etcpak_format, rb + src_ofs, wb + dst_ofs, mipmap_w, mipmap_h); |
| 186 | + } |
| 187 | + |
| 188 | + p_image->set_data(p_image->get_width(), p_image->get_height(), p_image->has_mipmaps(), target_format, data); |
| 189 | + |
| 190 | + // Swap channels if the format is using a channel swizzle. |
| 191 | + if (source_format == Image::FORMAT_ETC2_RA_AS_RG) { |
| 192 | + p_image->convert_ra_rgba8_to_rg(); |
| 193 | + } |
| 194 | + |
| 195 | + print_verbose(vformat("etcpak: Decompression of %dx%d %s image %s with %d mipmaps took %d ms.", |
| 196 | + 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)); |
| 197 | +} |
0 commit comments