From c07cd406cbe3a80869c657b1b0ce22088954cccb Mon Sep 17 00:00:00 2001 From: Haoyu Qiu Date: Sun, 12 Nov 2023 00:04:16 +0800 Subject: [PATCH] Fix crash when saving compressed image as JPG & WebP --- modules/jpg/image_loader_jpegd.cpp | 6 +++++- modules/webp/webp_common.cpp | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/modules/jpg/image_loader_jpegd.cpp b/modules/jpg/image_loader_jpegd.cpp index 0b9fcf4455c9..e7fa909706ed 100644 --- a/modules/jpg/image_loader_jpegd.cpp +++ b/modules/jpg/image_loader_jpegd.cpp @@ -156,7 +156,11 @@ class ImageLoaderJPGOSBuffer : public jpge::output_stream { static Error _jpgd_save_to_output_stream(jpge::output_stream *p_output_stream, const Ref &p_img, float p_quality) { ERR_FAIL_COND_V(p_img.is_null() || p_img->is_empty(), ERR_INVALID_PARAMETER); - Ref image = p_img; + Ref image = p_img->duplicate(); + if (image->is_compressed()) { + Error error = image->decompress(); + ERR_FAIL_COND_V_MSG(error != OK, error, "Couldn't decompress image."); + } if (image->get_format() != Image::FORMAT_RGB8) { image->convert(Image::FORMAT_RGB8); } diff --git a/modules/webp/webp_common.cpp b/modules/webp/webp_common.cpp index bc34a25733e8..3a2ac5a90e67 100644 --- a/modules/webp/webp_common.cpp +++ b/modules/webp/webp_common.cpp @@ -59,6 +59,10 @@ Vector _webp_packer(const Ref &p_image, float p_quality, bool p_ compression_method = CLAMP(compression_method, 0, 6); Ref img = p_image->duplicate(); + if (img->is_compressed()) { + Error error = img->decompress(); + ERR_FAIL_COND_V_MSG(error != OK, Vector(), "Couldn't decompress image."); + } if (img->detect_alpha()) { img->convert(Image::FORMAT_RGBA8); } else {