Path: blob/master/modules/etcpak/image_decompress_etcpak.cpp
10277 views
/**************************************************************************/1/* image_decompress_etcpak.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "image_decompress_etcpak.h"3132#include "core/os/os.h"33#include "core/string/print_string.h"3435#include <DecodeRGB.hpp>3637#define ETCPAK_R_BLOCK_SIZE 838#define ETCPAK_RG_BLOCK_SIZE 1639#define ETCPAK_RGB_BLOCK_SIZE 840#define ETCPAK_RGBA_BLOCK_SIZE 164142template <void (*decompress_func)(const void *, void *, size_t), int block_size, int pixel_size>43static inline void _safe_decompress_mipmap(int width, int height, const uint8_t *src, uint8_t *dst) {44// A stack-allocated output buffer large enough to contain an entire uncompressed block.45uint8_t temp_buf[4 * 4 * pixel_size];4647// The amount of misaligned pixels on each axis.48const int width_diff = width - (width & ~0x03);49const int height_diff = height - (height & ~0x03);5051// The amount of uncompressed blocks on each axis.52const int width_blocks = (width & ~0x03) / 4;53const int height_blocks = (height & ~0x03) / 4;5455// The pitch of the image in bytes.56const int image_pitch = width * pixel_size;57// The pitch of a block in bytes.58const int block_pitch = 4 * pixel_size;59// The pitch of the last block in bytes.60const int odd_pitch = width_diff * pixel_size;6162size_t src_pos = 0;63size_t dst_pos = 0;6465// Decompress the blocks, starting from the top.66for (int y = 0; y < height_blocks; y += 1) {67// Decompress the blocks, starting from the left.68for (int x = 0; x < width_blocks; x += 1) {69decompress_func(&src[src_pos], &dst[dst_pos], width);70src_pos += block_size;71dst_pos += block_pitch;72}7374// Decompress the block on the right.75if (width_diff > 0) {76decompress_func(&src[src_pos], temp_buf, 4);7778// Copy the data from the temporary buffer to the output.79for (int i = 0; i < 4; i++) {80memcpy(&dst[dst_pos + i * image_pitch], &temp_buf[i * block_pitch], odd_pitch);81}8283src_pos += block_size;84dst_pos += odd_pitch;85}8687// Skip to the next row of blocks, the current one has already been filled.88dst_pos += 3 * image_pitch;89}9091// Decompress the blocks at the bottom of the image.92if (height_diff > 0) {93// Decompress the blocks at the bottom.94for (int x = 0; x < width_blocks; x += 1) {95decompress_func(&src[src_pos], temp_buf, 4);9697// Copy the data from the temporary buffer to the output.98for (int i = 0; i < height_diff; i++) {99memcpy(&dst[dst_pos + i * image_pitch], &temp_buf[i * block_pitch], block_pitch);100}101102src_pos += block_size;103dst_pos += block_pitch;104}105106// Decompress the block in the lower-right corner.107if (width_diff > 0) {108decompress_func(&src[src_pos], temp_buf, 4);109110// Copy the data from the temporary buffer to the output.111for (int i = 0; i < height_diff; i++) {112memcpy(&dst[dst_pos + i * image_pitch], &temp_buf[i * block_pitch], odd_pitch);113}114115src_pos += block_size;116dst_pos += odd_pitch;117}118}119}120121template <void (*decompress_func)(const void *, void *, size_t), int block_size, int pixel_size>122static inline void _decompress_mipmap(int width, int height, const uint8_t *src, uint8_t *dst) {123size_t src_pos = 0;124size_t dst_pos = 0;125126// The size of a single block in bytes.127const int block_pitch = 4 * pixel_size;128129for (int y = 0; y < height; y += 4) {130for (int x = 0; x < width; x += 4) {131decompress_func(&src[src_pos], &dst[dst_pos], width);132src_pos += block_size;133dst_pos += block_pitch;134}135136// Skip to the next row of blocks, the current one has already been filled.137dst_pos += 3 * width * pixel_size;138}139}140141static void decompress_image(EtcpakFormat format, const void *src, void *dst, const uint64_t width, const uint64_t height) {142const uint8_t *src_blocks = reinterpret_cast<const uint8_t *>(src);143uint8_t *dec_blocks = reinterpret_cast<uint8_t *>(dst);144145const uint64_t aligned_width = (width + 3) & ~0x03;146const uint64_t aligned_height = (height + 3) & ~0x03;147148if (width != aligned_width || height != aligned_height) {149switch (format) {150case Etcpak_R: {151_safe_decompress_mipmap<DecodeRBlock, ETCPAK_R_BLOCK_SIZE, 4>(width, height, src_blocks, dec_blocks);152} break;153case Etcpak_RG: {154_safe_decompress_mipmap<DecodeRGBlock, ETCPAK_RG_BLOCK_SIZE, 4>(width, height, src_blocks, dec_blocks);155} break;156case Etcpak_RGB: {157_safe_decompress_mipmap<DecodeRGBBlock, ETCPAK_RGB_BLOCK_SIZE, 4>(width, height, src_blocks, dec_blocks);158} break;159case Etcpak_RGBA: {160_safe_decompress_mipmap<DecodeRGBABlock, ETCPAK_RGBA_BLOCK_SIZE, 4>(width, height, src_blocks, dec_blocks);161} break;162}163} else {164switch (format) {165case Etcpak_R: {166_decompress_mipmap<DecodeRBlock, ETCPAK_R_BLOCK_SIZE, 4>(width, height, src_blocks, dec_blocks);167} break;168case Etcpak_RG: {169_decompress_mipmap<DecodeRGBlock, ETCPAK_RG_BLOCK_SIZE, 4>(width, height, src_blocks, dec_blocks);170} break;171case Etcpak_RGB: {172_decompress_mipmap<DecodeRGBBlock, ETCPAK_RGB_BLOCK_SIZE, 4>(width, height, src_blocks, dec_blocks);173} break;174case Etcpak_RGBA: {175_decompress_mipmap<DecodeRGBABlock, ETCPAK_RGBA_BLOCK_SIZE, 4>(width, height, src_blocks, dec_blocks);176} break;177}178}179}180181void _decompress_etc(Image *p_image) {182uint64_t start_time = OS::get_singleton()->get_ticks_msec();183184int width = p_image->get_width();185int height = p_image->get_height();186187Image::Format source_format = p_image->get_format();188Image::Format target_format = Image::FORMAT_RGBA8;189190EtcpakFormat etcpak_format = Etcpak_R;191192switch (source_format) {193case Image::FORMAT_ETC:194case Image::FORMAT_ETC2_RGB8:195etcpak_format = Etcpak_RGB;196break;197198case Image::FORMAT_ETC2_RGBA8:199case Image::FORMAT_ETC2_RA_AS_RG:200etcpak_format = Etcpak_RGBA;201break;202203case Image::FORMAT_ETC2_R11:204etcpak_format = Etcpak_R;205break;206207case Image::FORMAT_ETC2_RG11:208etcpak_format = Etcpak_RG;209break;210211default:212ERR_FAIL_MSG(vformat("etcpak: Can't decompress image %s with an unknown format: %s.", p_image->get_path(), Image::get_format_name(source_format)));213break;214}215216int mm_count = p_image->get_mipmap_count();217int64_t target_size = Image::get_image_data_size(width, height, target_format, p_image->has_mipmaps());218219// Decompressed data.220Vector<uint8_t> data;221data.resize(target_size);222uint8_t *wb = data.ptrw();223224// Source data.225const uint8_t *rb = p_image->ptr();226227// Decompress mipmaps.228for (int i = 0; i <= mm_count; i++) {229int mipmap_w = 0, mipmap_h = 0;230int64_t src_ofs = Image::get_image_mipmap_offset(width, height, source_format, i);231int64_t dst_ofs = Image::get_image_mipmap_offset_and_dimensions(width, height, target_format, i, mipmap_w, mipmap_h);232decompress_image(etcpak_format, rb + src_ofs, wb + dst_ofs, mipmap_w, mipmap_h);233}234235p_image->set_data(p_image->get_width(), p_image->get_height(), p_image->has_mipmaps(), target_format, data);236237// Swap channels if the format is using a channel swizzle.238if (source_format == Image::FORMAT_ETC2_RA_AS_RG) {239p_image->convert_ra_rgba8_to_rg();240}241242print_verbose(vformat("etcpak: Decompression of %dx%d %s image %s with %d mipmaps took %d ms.",243p_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));244}245246247