From 8239aeec8ede9c6fadec56e842665c392a835a0a Mon Sep 17 00:00:00 2001 From: BlueCube3310 <53150244+BlueCube3310@users.noreply.github.com> Date: Sat, 24 Feb 2024 12:41:08 +0100 Subject: [PATCH] Image: Add static `is_format_compressed` function. --- core/io/image.cpp | 10 +++++++--- core/io/image.h | 1 + modules/astcenc/image_compress_astcenc.cpp | 2 +- modules/basis_universal/image_compress_basisu.cpp | 2 +- modules/cvtt/image_compress_cvtt.cpp | 2 +- modules/etcpak/image_compress_etcpak.cpp | 2 +- 6 files changed, 12 insertions(+), 7 deletions(-) diff --git a/core/io/image.cpp b/core/io/image.cpp index b094290ac8a3..ad471720b2ae 100644 --- a/core/io/image.cpp +++ b/core/io/image.cpp @@ -521,7 +521,7 @@ void Image::convert(Format p_new_format) { // Includes the main image. const int mipmap_count = get_mipmap_count() + 1; - if (format > FORMAT_RGBE9995 || p_new_format > FORMAT_RGBE9995) { + if (Image::is_format_compressed(format) || Image::is_format_compressed(p_new_format)) { ERR_FAIL_MSG("Cannot convert to <-> from compressed formats. Use compress() and decompress() instead."); } else if (format > FORMAT_RGBA8 || p_new_format > FORMAT_RGBA8) { @@ -1662,7 +1662,7 @@ int Image::_get_dst_image_size(int p_width, int p_height, Format p_format, int & } bool Image::_can_modify(Format p_format) const { - return p_format <= FORMAT_RGBE9995; + return !Image::is_format_compressed(p_format); } template FORMAT_RGBE9995; + return is_format_compressed(format); +} + +bool Image::is_format_compressed(Format p_format) { + return p_format > FORMAT_RGBE9995; } Error Image::decompress() { diff --git a/core/io/image.h b/core/io/image.h index 2cabbb767a5e..daddfac59d7d 100644 --- a/core/io/image.h +++ b/core/io/image.h @@ -376,6 +376,7 @@ class Image : public Resource { Error compress_from_channels(CompressMode p_mode, UsedChannels p_channels, ASTCFormat p_astc_format = ASTC_FORMAT_4x4); Error decompress(); bool is_compressed() const; + static bool is_format_compressed(Format p_format); void fix_alpha_edges(); void premultiply_alpha(); diff --git a/modules/astcenc/image_compress_astcenc.cpp b/modules/astcenc/image_compress_astcenc.cpp index 1c643d780d5a..4019330db7bb 100644 --- a/modules/astcenc/image_compress_astcenc.cpp +++ b/modules/astcenc/image_compress_astcenc.cpp @@ -41,7 +41,7 @@ void _compress_astc(Image *r_img, Image::ASTCFormat p_format) { // TODO: See how to handle lossy quality. Image::Format img_format = r_img->get_format(); - if (img_format >= Image::FORMAT_DXT1) { + if (Image::is_format_compressed(img_format)) { return; // Do not compress, already compressed. } diff --git a/modules/basis_universal/image_compress_basisu.cpp b/modules/basis_universal/image_compress_basisu.cpp index 72e7977eefb7..1a918c9a98cf 100644 --- a/modules/basis_universal/image_compress_basisu.cpp +++ b/modules/basis_universal/image_compress_basisu.cpp @@ -243,7 +243,7 @@ Ref basis_universal_unpacker_ptr(const uint8_t *p_data, int p_size) { basist::basisu_image_level_info basisu_level; transcoder.get_image_level_info(src_ptr, src_size, basisu_level, 0, i); - uint32_t mip_block_or_pixel_count = image_format >= Image::FORMAT_DXT1 ? basisu_level.m_total_blocks : basisu_level.m_orig_width * basisu_level.m_orig_height; + uint32_t mip_block_or_pixel_count = Image::is_format_compressed(image_format) ? basisu_level.m_total_blocks : basisu_level.m_orig_width * basisu_level.m_orig_height; int ofs = Image::get_image_mipmap_offset(basisu_info.m_width, basisu_info.m_height, image_format, i); bool result = transcoder.transcode_image_level(src_ptr, src_size, 0, i, dst + ofs, mip_block_or_pixel_count, basisu_format); diff --git a/modules/cvtt/image_compress_cvtt.cpp b/modules/cvtt/image_compress_cvtt.cpp index ad707722703e..9ebde0e8b06d 100644 --- a/modules/cvtt/image_compress_cvtt.cpp +++ b/modules/cvtt/image_compress_cvtt.cpp @@ -142,7 +142,7 @@ static void _digest_job_queue(void *p_job_queue, uint32_t p_index) { } void image_compress_cvtt(Image *p_image, Image::UsedChannels p_channels) { - if (p_image->get_format() >= Image::FORMAT_BPTC_RGBA) { + if (p_image->is_compressed()) { return; //do not compress, already compressed } int w = p_image->get_width(); diff --git a/modules/etcpak/image_compress_etcpak.cpp b/modules/etcpak/image_compress_etcpak.cpp index 087d3a931469..5a470adc03fd 100644 --- a/modules/etcpak/image_compress_etcpak.cpp +++ b/modules/etcpak/image_compress_etcpak.cpp @@ -92,7 +92,7 @@ void _compress_etcpak(EtcpakType p_compresstype, Image *r_img) { uint64_t start_time = OS::get_singleton()->get_ticks_msec(); Image::Format img_format = r_img->get_format(); - if (img_format >= Image::FORMAT_DXT1) { + if (Image::is_format_compressed(img_format)) { return; // Do not compress, already compressed. } if (img_format > Image::FORMAT_RGBA8) {