Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image: add missing error printing to format specific functions #97780

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions core/io/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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>((Image *)this), p_quality);
}
Expand All @@ -2631,9 +2631,10 @@ Vector<uint8_t> 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>((Image *)this), p_grayscale);
}
Expand All @@ -2646,9 +2647,10 @@ Vector<uint8_t> 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>((Image *)this), p_lossy, p_quality);
Expand Down Expand Up @@ -4023,10 +4025,18 @@ Error Image::load_png_from_buffer(const Vector<uint8_t> &p_array) {
}

Error Image::load_jpg_from_buffer(const Vector<uint8_t> &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<uint8_t> &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);
}

Expand Down
10 changes: 9 additions & 1 deletion doc/classes/Image.xml
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@
<param index="0" name="buffer" type="PackedByteArray" />
<description>
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.
</description>
</method>
<method name="load_ktx_from_buffer">
Expand Down Expand Up @@ -407,6 +408,7 @@
<param index="0" name="buffer" type="PackedByteArray" />
<description>
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.
</description>
</method>
<method name="normal_map_to_xy">
Expand Down Expand Up @@ -463,6 +465,7 @@
<param index="1" name="grayscale" type="bool" default="false" />
<description>
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.
</description>
</method>
Expand All @@ -471,7 +474,8 @@
<param index="0" name="grayscale" type="bool" default="false" />
<description>
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.
</description>
</method>
<method name="save_jpg" qualifiers="const">
Expand All @@ -481,6 +485,7 @@
<description>
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.
</description>
</method>
<method name="save_jpg_to_buffer" qualifiers="const">
Expand All @@ -489,6 +494,7 @@
<description>
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.
</description>
</method>
<method name="save_png" qualifiers="const">
Expand All @@ -512,6 +518,7 @@
<description>
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.
</description>
</method>
<method name="save_webp_to_buffer" qualifiers="const">
Expand All @@ -521,6 +528,7 @@
<description>
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.
</description>
</method>
<method name="set_data">
Expand Down
Loading