From a5b29ec110be35f8f912a4d4e79e1e859dd51073 Mon Sep 17 00:00:00 2001 From: "Yevhen Babiichuk (DustDFG)" Date: Tue, 24 Sep 2024 19:27:19 +0300 Subject: [PATCH] Image: add missing error printing to format specific functions Plus updated docs Note it doesn't affect load_from_file because it uses dynamic loaders while these functions are wrappers around direct calls to functions defined inside modules without using resource loader api Signed-off-by: Yevhen Babiichuk (DustDFG) --- core/io/image.cpp | 32 +++++++++++++++++++++----------- doc/classes/Image.xml | 10 +++++++++- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/core/io/image.cpp b/core/io/image.cpp index aa391b77dd43..dc7005393625 100644 --- a/core/io/image.cpp +++ b/core/io/image.cpp @@ -88,12 +88,11 @@ const char *Image::format_names[Image::FORMAT_MAX] = { SavePNGFunc Image::save_png_func = nullptr; SaveJPGFunc Image::save_jpg_func = nullptr; SaveEXRFunc Image::save_exr_func = nullptr; +SaveWebPFunc Image::save_webp_func = nullptr; SavePNGBufferFunc Image::save_png_buffer_func = nullptr; SaveEXRBufferFunc Image::save_exr_buffer_func = nullptr; SaveJPGBufferFunc Image::save_jpg_buffer_func = nullptr; - -SaveWebPFunc Image::save_webp_func = nullptr; SaveWebPBufferFunc Image::save_webp_buffer_func = nullptr; void Image::_put_pixelb(int p_x, int p_y, uint32_t p_pixel_size, uint8_t *p_data, const uint8_t *p_pixel) { @@ -2607,9 +2606,10 @@ Error Image::save_png(const String &p_path) const { } Error Image::save_jpg(const String &p_path, float p_quality) const { - if (save_jpg_func == nullptr) { - return ERR_UNAVAILABLE; - } + ERR_FAIL_NULL_V_MSG( + save_jpg_func, + ERR_UNAVAILABLE, + "The JPG module isn't enabled. Recompile the Godot editor or export template binary with the `module_jpg_enabled=yes` SCons option."); return save_jpg_func(p_path, Ref((Image *)this), p_quality); } @@ -2631,9 +2631,10 @@ Vector Image::save_jpg_to_buffer(float p_quality) const { } Error Image::save_exr(const String &p_path, bool p_grayscale) const { - if (save_exr_func == nullptr) { - return ERR_UNAVAILABLE; - } + ERR_FAIL_NULL_V_MSG( + save_exr_func, + ERR_UNAVAILABLE, + "The TinyEXR module isn't enabled. Recompile the Godot editor with the `module_tinyexr_enabled=yes` SCons option."); return save_exr_func(p_path, Ref((Image *)this), p_grayscale); } @@ -2646,9 +2647,10 @@ Vector Image::save_exr_to_buffer(bool p_grayscale) const { } Error Image::save_webp(const String &p_path, const bool p_lossy, const float p_quality) const { - if (save_webp_func == nullptr) { - return ERR_UNAVAILABLE; - } + ERR_FAIL_NULL_V_MSG( + save_webp_func, + ERR_UNAVAILABLE, + "The WEBP module isn't enabled. Recompile the Godot editor or export template binary with the `module_webp_enabled=yes` SCons option."); ERR_FAIL_COND_V_MSG(p_lossy && !(0.0f <= p_quality && p_quality <= 1.0f), ERR_INVALID_PARAMETER, "The WebP lossy quality was set to " + rtos(p_quality) + ", which is not valid. WebP lossy quality must be between 0.0 and 1.0 (inclusive)."); return save_webp_func(p_path, Ref((Image *)this), p_lossy, p_quality); @@ -4023,10 +4025,18 @@ Error Image::load_png_from_buffer(const Vector &p_array) { } Error Image::load_jpg_from_buffer(const Vector &p_array) { + ERR_FAIL_NULL_V_MSG( + _jpg_mem_loader_func, + ERR_UNAVAILABLE, + "The JPG module isn't enabled. Recompile the Godot editor or export template binary with the `module_jpg_enabled=yes` SCons option."); return _load_from_buffer(p_array, _jpg_mem_loader_func); } Error Image::load_webp_from_buffer(const Vector &p_array) { + ERR_FAIL_NULL_V_MSG( + _webp_mem_loader_func, + ERR_UNAVAILABLE, + "The WEBP module isn't enabled. Recompile the Godot editor or export template binary with the `module_webp_enabled=yes` SCons option."); return _load_from_buffer(p_array, _webp_mem_loader_func); } diff --git a/doc/classes/Image.xml b/doc/classes/Image.xml index 0fd84fb452e6..823f2ae4ca78 100644 --- a/doc/classes/Image.xml +++ b/doc/classes/Image.xml @@ -357,6 +357,7 @@ Loads an image from the binary contents of a JPEG file. + [b]Note:[/b] This method is only available in engine builds with the JPG module enabled. By default, the JPG module is enabled, but it can be disabled at build-time using the [code]module_jpg_enabled=no[/code] SCons option. @@ -407,6 +408,7 @@ Loads an image from the binary contents of a WebP file. + [b]Note:[/b] This method is only available in engine builds with the WEBP module enabled. By default, the WEBP module is enabled, but it can be disabled at build-time using the [code]module_webp_enabled=no[/code] SCons option. @@ -463,6 +465,7 @@ Saves the image as an EXR file to [param path]. If [param grayscale] is [code]true[/code] and the image has only one channel, it will be saved explicitly as monochrome rather than one red channel. This function will return [constant ERR_UNAVAILABLE] if Godot was compiled without the TinyEXR module. + [b]Note:[/b] This method is only available in engine builds with the TinyEXR module enabled. By default, the TinyEXR module is enabled, but it can be disabled at build-time using the [code]module_tinyexr_enabled=no[/code] SCons option. [b]Note:[/b] The TinyEXR module is disabled in non-editor builds, which means [method save_exr] will return [constant ERR_UNAVAILABLE] when it is called from an exported project. @@ -471,7 +474,8 @@ Saves the image as an EXR file to a byte array. If [param grayscale] is [code]true[/code] and the image has only one channel, it will be saved explicitly as monochrome rather than one red channel. This function will return an empty byte array if Godot was compiled without the TinyEXR module. - [b]Note:[/b] The TinyEXR module is disabled in non-editor builds, which means [method save_exr] will return an empty byte array when it is called from an exported project. + [b]Note:[/b] This method is only available in engine builds with the TinyEXR module enabled. By default, the TinyEXR module is enabled, but it can be disabled at build-time using the [code]module_tinyexr_enabled=no[/code] SCons option. + [b]Note:[/b] The TinyEXR module is disabled in non-editor builds, which means [method save_exr_to_buffer] will return an empty byte array when it is called from an exported project. @@ -481,6 +485,7 @@ Saves the image as a JPEG file to [param path] with the specified [param quality] between [code]0.01[/code] and [code]1.0[/code] (inclusive). Higher [param quality] values result in better-looking output at the cost of larger file sizes. Recommended [param quality] values are between [code]0.75[/code] and [code]0.90[/code]. Even at quality [code]1.00[/code], JPEG compression remains lossy. [b]Note:[/b] JPEG does not save an alpha channel. If the [Image] contains an alpha channel, the image will still be saved, but the resulting JPEG file won't contain the alpha channel. + [b]Note:[/b] This method is only available in engine builds with the JPG module enabled. By default, the JPG module is enabled, but it can be disabled at build-time using the [code]module_jpg_enabled=no[/code] SCons option. @@ -489,6 +494,7 @@ Saves the image as a JPEG file to a byte array with the specified [param quality] between [code]0.01[/code] and [code]1.0[/code] (inclusive). Higher [param quality] values result in better-looking output at the cost of larger byte array sizes (and therefore memory usage). Recommended [param quality] values are between [code]0.75[/code] and [code]0.90[/code]. Even at quality [code]1.00[/code], JPEG compression remains lossy. [b]Note:[/b] JPEG does not save an alpha channel. If the [Image] contains an alpha channel, the image will still be saved, but the resulting byte array won't contain the alpha channel. + [b]Note:[/b] This method is only available in engine builds with the JPG module enabled. By default, the JPG module is enabled, but it can be disabled at build-time using the [code]module_jpg_enabled=no[/code] SCons option. @@ -512,6 +518,7 @@ Saves the image as a WebP (Web Picture) file to the file at [param path]. By default it will save lossless. If [param lossy] is true, the image will be saved lossy, using the [param quality] setting between 0.0 and 1.0 (inclusive). Lossless WebP offers more efficient compression than PNG. [b]Note:[/b] The WebP format is limited to a size of 16383×16383 pixels, while PNG can save larger images. + [b]Note:[/b] This method is only available in engine builds with the WEBP module enabled. By default, the WEBP module is enabled, but it can be disabled at build-time using the [code]module_webp_enabled=no[/code] SCons option. @@ -521,6 +528,7 @@ Saves the image as a WebP (Web Picture) file to a byte array. By default it will save lossless. If [param lossy] is true, the image will be saved lossy, using the [param quality] setting between 0.0 and 1.0 (inclusive). Lossless WebP offers more efficient compression than PNG. [b]Note:[/b] The WebP format is limited to a size of 16383×16383 pixels, while PNG can save larger images. + [b]Note:[/b] This method is only available in engine builds with the WEBP module enabled. By default, the WEBP module is enabled, but it can be disabled at build-time using the [code]module_webp_enabled=no[/code] SCons option.