Godot Engine – Multi-platform 2D and 3D game engine
Fix half-float sRGB conversion in `Image`
David Chaveza5c4b9b4ff5e
diff --git a/core/io/image.cpp b/core/io/image.cppindex 569e719979f..7035dd5ae27 100644--- a/core/io/image.cpp+++ b/core/io/image.cpp@@ -4318,8 +4318,12 @@ _FORCE_INLINE_ uint16_t _srgb2lin_hf(uint16_t p_s) {void Image::_srgb2lin_hft4(void *p_td, uint32_t p_i) {Image *img = (Image *)p_td;uint16_t *data_ptr = (uint16_t *)img->data.ptrw();+ int pixel_count = img->data.size() / 8;for (int x = 0; x < img->width; x++) {int i = img->width * p_i + x;+ if (i >= pixel_count) {+ break;+ }data_ptr[(i << 2) + 0] = _srgb2lin_hf(data_ptr[(i << 2) + 0]);data_ptr[(i << 2) + 1] = _srgb2lin_hf(data_ptr[(i << 2) + 1]);data_ptr[(i << 2) + 2] = _srgb2lin_hf(data_ptr[(i << 2) + 2]);@@ -4329,8 +4333,12 @@ void Image::_srgb2lin_hft4(void *p_td, uint32_t p_i) {void Image::_srgb2lin_hft3(void *p_td, uint32_t p_i) {Image *img = (Image *)p_td;uint16_t *data_ptr = (uint16_t *)img->data.ptrw();+ int pixel_count = img->data.size() / 6;for (int x = 0; x < img->width; x++) {int i = img->width * p_i + x;+ if (i >= pixel_count) {+ break;+ }data_ptr[(i * 3) + 0] = _srgb2lin_hf(data_ptr[(i * 3) + 0]);data_ptr[(i * 3) + 1] = _srgb2lin_hf(data_ptr[(i * 3) + 1]);data_ptr[(i * 3) + 2] = _srgb2lin_hf(data_ptr[(i * 3) + 2]);@@ -4346,11 +4354,14 @@ void Image::srgb_to_linear() {ERR_FAIL_COND(format != FORMAT_RGB8 && format != FORMAT_RGBA8 && format != FORMAT_RGBH && format != FORMAT_RGBAH);+ // Copy on write here, so the threaded tasks below don't race to do it.+ data.ptrw();+if (format == FORMAT_RGBAH) {- WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_native_group_task(&Image::_srgb2lin_hft4, this, height, -1, true, String("SRGB2LIN4"));+ WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_native_group_task(&Image::_srgb2lin_hft4, this, Math::division_round_up((int)(data.size() / 8), width), -1, true, String("SRGB2LIN4"));WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);} else if (format == FORMAT_RGBH) {- WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_native_group_task(&Image::_srgb2lin_hft3, this, height, -1, true, String("SRGB2LIN3"));+ WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_native_group_task(&Image::_srgb2lin_hft3, this, Math::division_round_up((int)(data.size() / 6), width), -1, true, String("SRGB2LIN3"));WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);} else if (format == FORMAT_RGBA8) {int len = data.size() / 4;@@ -4387,19 +4398,27 @@ _FORCE_INLINE_ uint16_t _lin2srgb_hf(uint16_t p_l) {void Image::_lin2srgb_hft4(void *p_td, uint32_t p_i) {Image *img = (Image *)p_td;uint16_t *data_ptr = (uint16_t *)img->data.ptrw();+ int pixel_count = img->data.size() / 8;for (int x = 0; x < img->width; x++) {int i = img->width * p_i + x;- data_ptr[(p_i << 2) + 0] = _lin2srgb_hf(data_ptr[(i << 2) + 0]);- data_ptr[(p_i << 2) + 1] = _lin2srgb_hf(data_ptr[(i << 2) + 1]);- data_ptr[(p_i << 2) + 2] = _lin2srgb_hf(data_ptr[(i << 2) + 2]);+ if (i >= pixel_count) {+ break;+ }+ data_ptr[(i << 2) + 0] = _lin2srgb_hf(data_ptr[(i << 2) + 0]);+ data_ptr[(i << 2) + 1] = _lin2srgb_hf(data_ptr[(i << 2) + 1]);+ data_ptr[(i << 2) + 2] = _lin2srgb_hf(data_ptr[(i << 2) + 2]);}}void Image::_lin2srgb_hft3(void *p_td, uint32_t p_i) {Image *img = (Image *)p_td;uint16_t *data_ptr = (uint16_t *)img->data.ptrw();+ int pixel_count = img->data.size() / 6;for (int x = 0; x < img->width; x++) {int i = img->width * p_i + x;+ if (i >= pixel_count) {+ break;+ }data_ptr[(i * 3) + 0] = _lin2srgb_hf(data_ptr[(i * 3) + 0]);data_ptr[(i * 3) + 1] = _lin2srgb_hf(data_ptr[(i * 3) + 1]);data_ptr[(i * 3) + 2] = _lin2srgb_hf(data_ptr[(i * 3) + 2]);@@ -4415,11 +4434,14 @@ void Image::linear_to_srgb() {ERR_FAIL_COND(format != FORMAT_RGB8 && format != FORMAT_RGBA8 && format != FORMAT_RGBH && format != FORMAT_RGBAH);+ // Copy on write here, so the threaded tasks below don't race to do it.+ data.ptrw();+if (format == FORMAT_RGBAH) {- WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_native_group_task(&Image::_lin2srgb_hft4, this, height, -1, true, String("LIN2SRGB4"));+ WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_native_group_task(&Image::_lin2srgb_hft4, this, Math::division_round_up((int)(data.size() / 8), width), -1, true, String("LIN2SRGB4"));WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);} else if (format == FORMAT_RGBH) {- WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_native_group_task(&Image::_lin2srgb_hft3, this, height, -1, true, String("LIN2SRGB3"));+ WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_native_group_task(&Image::_lin2srgb_hft3, this, Math::division_round_up((int)(data.size() / 6), width), -1, true, String("LIN2SRGB3"));WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);} else if (format == FORMAT_RGBA8) {int len = data.size() / 4;diff --git a/tests/core/io/test_image.cpp b/tests/core/io/test_image.cppindex e1040b11252..0a0bf385964 100644--- a/tests/core/io/test_image.cpp+++ b/tests/core/io/test_image.cpp@@ -474,6 +474,50 @@ TEST_CASE("[Image] Custom mipmaps") {}}+TEST_CASE("[Image] Linear and sRGB conversion of half-float formats") {+ const Color linear = Color(0.5, 0.5, 0.5);+ const Color srgb = linear.linear_to_srgb();++ auto all_pixels_are = [](const Ref<Image> &p_image, const Color &p_color) {+ for (int mip = 0; mip <= p_image->get_mipmap_count(); mip++) {+ Ref<Image> level = p_image->get_image_from_mipmap(mip);+ for (int y = 0; y < level->get_height(); y++) {+ for (int x = 0; x < level->get_width(); x++) {+ const Color pixel = level->get_pixel(x, y);+ if (!Math::is_equal_approx(pixel.r, p_color.r, 0.001f) || !Math::is_equal_approx(pixel.g, p_color.g, 0.001f) || !Math::is_equal_approx(pixel.b, p_color.b, 0.001f)) {+ return false;+ }+ }+ }+ }+ return true;+ };++ for (Image::Format format : { Image::FORMAT_RGBH, Image::FORMAT_RGBAH }) {+ Ref<Image> image = memnew(Image(8, 8, true, format));+ image->fill(linear);++ image->linear_to_srgb();+ CHECK_MESSAGE(all_pixels_are(image, srgb), "linear_to_srgb() should convert every pixel of every mipmap.");++ image->srgb_to_linear();+ CHECK_MESSAGE(all_pixels_are(image, linear), "srgb_to_linear() should convert every pixel of every mipmap.");++ // Holding the data makes the conversion copy it on write.+ Ref<Image> shared = memnew(Image(512, 512, false, format));+ shared->fill(linear);+ const Vector<uint8_t> held_linear = shared->get_data();+ shared->linear_to_srgb();+ CHECK_MESSAGE(all_pixels_are(shared, srgb), "linear_to_srgb() should convert every pixel when the data is shared.");+ CHECK_MESSAGE(all_pixels_are(Image::create_from_data(512, 512, false, format, held_linear), linear), "linear_to_srgb() should not change other copies of the data.");++ const Vector<uint8_t> held_srgb = shared->get_data();+ shared->srgb_to_linear();+ CHECK_MESSAGE(all_pixels_are(shared, linear), "srgb_to_linear() should convert every pixel when the data is shared.");+ CHECK_MESSAGE(all_pixels_are(Image::create_from_data(512, 512, false, format, held_srgb), srgb), "srgb_to_linear() should not change other copies of the data.");+ }+}+TEST_CASE("[Image] Convert image") {for (int format = Image::FORMAT_RF; format < Image::FORMAT_RGBE9995; format++) {for (int new_format = Image::FORMAT_RF; new_format < Image::FORMAT_RGBE9995; new_format++) {