diff --git a/core/io/image.compat.inc b/core/io/image.compat.inc new file mode 100644 index 000000000000..c60ff9382a98 --- /dev/null +++ b/core/io/image.compat.inc @@ -0,0 +1,48 @@ +/**************************************************************************/ +/* image.compat.inc */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#ifndef DISABLE_DEPRECATED + +#include "core/object/class_db.h" + +Vector Image::_save_exr_to_buffer_bind_compat_117800(bool p_grayscale) const { + return save_exr_to_buffer(p_grayscale); +} + +Error Image::_save_exr_bind_compat_117800(const String &p_path, bool p_grayscale) const { + return save_exr(p_path, p_grayscale); +} + +void Image::_bind_compatibility_methods() { + ClassDB::bind_compatibility_method(D_METHOD("save_exr", "path", "grayscale"), &Image::_save_exr_bind_compat_117800, DEFVAL(false)); + ClassDB::bind_compatibility_method(D_METHOD("save_exr_to_buffer", "grayscale"), &Image::_save_exr_to_buffer_bind_compat_117800, DEFVAL(false)); +} + +#endif // DISABLE_DEPRECATED diff --git a/core/io/image.cpp b/core/io/image.cpp index 0b4900892c34..55d138b9024f 100644 --- a/core/io/image.cpp +++ b/core/io/image.cpp @@ -29,6 +29,7 @@ /**************************************************************************/ #include "image.h" +#include "image.compat.inc" #include "core/config/project_settings.h" #include "core/error/error_macros.h" @@ -2820,19 +2821,18 @@ Vector Image::save_jpg_to_buffer(float p_quality) const { return save_jpg_buffer_func(Ref((Image *)this), p_quality); } -Error Image::save_exr(const String &p_path, bool p_grayscale) const { +Error Image::save_exr(const String &p_path, bool p_grayscale, bool p_color_image, float p_max_value) const { if (save_exr_func == nullptr) { return ERR_UNAVAILABLE; } - - return save_exr_func(p_path, Ref((Image *)this), p_grayscale); + return save_exr_func(p_path, Ref((Image *)this), p_grayscale, p_color_image, p_max_value); } -Vector Image::save_exr_to_buffer(bool p_grayscale) const { +Vector Image::save_exr_to_buffer(bool p_grayscale, bool p_color_image, float p_max_value) const { if (save_exr_buffer_func == nullptr) { return Vector(); } - return save_exr_buffer_func(Ref((Image *)this), p_grayscale); + return save_exr_buffer_func(Ref((Image *)this), p_grayscale, p_color_image, p_max_value); } Error Image::save_dds(const String &p_path) const { @@ -3879,8 +3879,8 @@ void Image::_bind_methods() { ClassDB::bind_method(D_METHOD("save_png_to_buffer"), &Image::save_png_to_buffer); ClassDB::bind_method(D_METHOD("save_jpg", "path", "quality"), &Image::save_jpg, DEFVAL(0.75)); ClassDB::bind_method(D_METHOD("save_jpg_to_buffer", "quality"), &Image::save_jpg_to_buffer, DEFVAL(0.75)); - ClassDB::bind_method(D_METHOD("save_exr", "path", "grayscale"), &Image::save_exr, DEFVAL(false)); - ClassDB::bind_method(D_METHOD("save_exr_to_buffer", "grayscale"), &Image::save_exr_to_buffer, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("save_exr", "path", "grayscale", "color_image", "max_linear_value"), &Image::save_exr, DEFVAL(false), DEFVAL(false), DEFVAL(-1.0)); + ClassDB::bind_method(D_METHOD("save_exr_to_buffer", "grayscale", "color_image", "max_linear_value"), &Image::save_exr_to_buffer, DEFVAL(false), DEFVAL(false), DEFVAL(-1.0)); ClassDB::bind_method(D_METHOD("save_dds", "path"), &Image::save_dds); ClassDB::bind_method(D_METHOD("save_dds_to_buffer"), &Image::save_dds_to_buffer); diff --git a/core/io/image.h b/core/io/image.h index 933478e457fd..30d64e300fd0 100644 --- a/core/io/image.h +++ b/core/io/image.h @@ -55,8 +55,8 @@ typedef Ref (*ScalableImageMemLoadFunc)(const uint8_t *p_data, int p_size typedef Error (*SaveWebPFunc)(const String &p_path, const Ref &p_img, const bool p_lossy, const float p_quality); typedef Vector (*SaveWebPBufferFunc)(const Ref &p_img, const bool p_lossy, const float p_quality); -typedef Error (*SaveEXRFunc)(const String &p_path, const Ref &p_img, bool p_grayscale); -typedef Vector (*SaveEXRBufferFunc)(const Ref &p_img, bool p_grayscale); +typedef Error (*SaveEXRFunc)(const String &p_path, const Ref &p_img, bool p_grayscale, bool p_color_image, float p_max_value); +typedef Vector (*SaveEXRBufferFunc)(const Ref &p_img, bool p_grayscale, bool p_color_image, float p_max_value); typedef Error (*SaveDDSFunc)(const String &p_path, const Ref &p_img); typedef Vector (*SaveDDSBufferFunc)(const Ref &p_img); @@ -257,6 +257,12 @@ class Image : public Resource { static void _bind_methods(); +#ifndef DISABLE_DEPRECATED + Vector _save_exr_to_buffer_bind_compat_117800(bool p_grayscale = false) const; + Error _save_exr_bind_compat_117800(const String &p_path, bool p_grayscale = false) const; + static void _bind_compatibility_methods(); +#endif + private: Format format = FORMAT_L8; Vector data; @@ -361,9 +367,9 @@ class Image : public Resource { Error save_dds(const String &p_path) const; Vector save_png_to_buffer() const; Vector save_jpg_to_buffer(float p_quality = 0.75) const; - Vector save_exr_to_buffer(bool p_grayscale = false) const; + Vector save_exr_to_buffer(bool p_grayscale = false, bool p_color_image = false, float p_max_value = -1.0f) const; Vector save_dds_to_buffer() const; - Error save_exr(const String &p_path, bool p_grayscale = false) const; + Error save_exr(const String &p_path, bool p_grayscale = false, bool p_color_image = false, float p_max_value = -1.0f) const; Error save_webp(const String &p_path, const bool p_lossy = false, const float p_quality = 0.75f) const; Vector save_webp_to_buffer(const bool p_lossy = false, const float p_quality = 0.75f) const; diff --git a/doc/classes/Image.xml b/doc/classes/Image.xml index 0eeef7de251a..a1bda1befcd3 100644 --- a/doc/classes/Image.xml +++ b/doc/classes/Image.xml @@ -495,15 +495,21 @@ + + - 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. + 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. Set [param color_image] to [code]true[/code] when saving a color image, such as a screenshot. Negative values will be included when [param color_image] is [code]false[/code], which may be useful for saving raw floating point data such as a lightmap that includes negative light information. Color component values in the resulting EXR file will not exceed [param max_linear_value] if [param max_linear_value] is not negative. This function will return [constant ERR_UNAVAILABLE] if Godot was compiled without the TinyEXR module. + When saving screenshots of a project that uses HDR output, use [method Window.get_output_max_linear_value] for [param max_linear_value]. + + - 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. + 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. Set [param color_image] to [code]true[/code] when saving a color image, such as a screenshot. Negative values will be included when [param color_image] is [code]false[/code], which may be useful for saving raw floating point data such as a lightmap that includes negative light information. Color component values in the resulting EXR file will not exceed [param max_linear_value] if [param max_linear_value] is not negative. This function will return an empty byte array if Godot was compiled without the TinyEXR module. + When saving screenshots of a project that uses HDR output, use [method Window.get_output_max_linear_value] for [param max_linear_value]. diff --git a/misc/extension_api_validation/4.6-stable/GH-117800.txt b/misc/extension_api_validation/4.6-stable/GH-117800.txt new file mode 100644 index 000000000000..3b92354eb65c --- /dev/null +++ b/misc/extension_api_validation/4.6-stable/GH-117800.txt @@ -0,0 +1,7 @@ +GH-117800 +-------------- + +Validate extension JSON: Error: Field 'classes/Image/methods/save_exr/arguments': size changed value in new API, from 2 to 4. +Validate extension JSON: Error: Field 'classes/Image/methods/save_exr_to_buffer/arguments': size changed value in new API, from 1 to 3. + +Added color_image and max_linear_value parameters to Image.save_exr functions for saving HDR screenshots. Use default values for existing behavior. diff --git a/modules/tinyexr/image_saver_tinyexr.cpp b/modules/tinyexr/image_saver_tinyexr.cpp index 4c2c21a7bdc6..248740782dac 100644 --- a/modules/tinyexr/image_saver_tinyexr.cpp +++ b/modules/tinyexr/image_saver_tinyexr.cpp @@ -145,7 +145,7 @@ static int get_channel_count(Image::Format p_format) { } } -Vector save_exr_buffer(const Ref &p_img, bool p_grayscale) { +Vector save_exr_buffer(const Ref &p_img, bool p_grayscale, bool p_color_image, float p_max_value) { Image::Format format = p_img->get_format(); if (!is_supported_format(format)) { @@ -213,7 +213,14 @@ Vector save_exr_buffer(const Ref &p_img, bool p_grayscale) { float *dst_wp = (float *)dst_w; for (int i = 0; i < pixel_count; ++i) { - dst_wp[i] = src_rp[channel_index + i * channel_count]; + float src_float = src_rp[channel_index + i * channel_count]; + if (p_max_value >= 0.f) { + src_float = fmin(p_max_value, src_float); + } + if (p_color_image) { + src_float = fmax(0.f, src_float); + } + dst_wp[i] = src_float; } } else if (src_pixel_type == SRC_HALF && target_pixel_type == TINYEXR_PIXELTYPE_HALF) { @@ -223,7 +230,19 @@ Vector save_exr_buffer(const Ref &p_img, bool p_grayscale) { uint16_t *dst_wp = (uint16_t *)dst_w; for (int i = 0; i < pixel_count; ++i) { - dst_wp[i] = src_rp[channel_index + i * channel_count]; + int src_rp_index = channel_index + i * channel_count; + if (p_max_value >= 0.f || p_color_image) { + float src_float = Math::halfptr_to_float(src_rp + src_rp_index); + if (p_max_value >= 0.f) { + src_float = fmin(p_max_value, src_float); + } + if (p_color_image) { + src_float = fmax(0.f, src_float); + } + dst_wp[i] = Math::make_half_float(src_float); + } else { + dst_wp[i] = src_rp[src_rp_index]; + } } } else if (src_pixel_type == SRC_BYTE && target_pixel_type == TINYEXR_PIXELTYPE_HALF) { @@ -233,7 +252,11 @@ Vector save_exr_buffer(const Ref &p_img, bool p_grayscale) { uint16_t *dst_wp = (uint16_t *)dst_w; for (int i = 0; i < pixel_count; ++i) { - dst_wp[i] = Math::make_half_float(src_rp[channel_index + i * channel_count] / 255.f); + float src_float = src_rp[channel_index + i * channel_count] / 255.f; + if (p_max_value >= 0.f) { + src_float = fmin(p_max_value, src_float); + } + dst_wp[i] = Math::make_half_float(src_float); } } else { @@ -285,8 +308,8 @@ Vector save_exr_buffer(const Ref &p_img, bool p_grayscale) { return buffer; } -Error save_exr(const String &p_path, const Ref &p_img, bool p_grayscale) { - const Vector buffer = save_exr_buffer(p_img, p_grayscale); +Error save_exr(const String &p_path, const Ref &p_img, bool p_grayscale, bool p_color_image, float p_max_value) { + const Vector buffer = save_exr_buffer(p_img, p_grayscale, p_color_image, p_max_value); if (buffer.is_empty()) { print_error(String("Saving EXR failed.")); return ERR_FILE_CANT_WRITE; diff --git a/modules/tinyexr/image_saver_tinyexr.h b/modules/tinyexr/image_saver_tinyexr.h index b5c773c6a6ad..2bd007640b20 100644 --- a/modules/tinyexr/image_saver_tinyexr.h +++ b/modules/tinyexr/image_saver_tinyexr.h @@ -32,5 +32,5 @@ #include "core/io/image.h" -Error save_exr(const String &p_path, const Ref &p_img, bool p_grayscale); -Vector save_exr_buffer(const Ref &p_img, bool p_grayscale); +Error save_exr(const String &p_path, const Ref &p_img, bool p_grayscale, bool p_color_image, float p_max_value); +Vector save_exr_buffer(const Ref &p_img, bool p_grayscale, bool p_color_image, float p_max_value);