From 4c2d0d38fd148afe806b69cad697d76b9f31b938 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Rast=C3=A9n?= Date: Thu, 9 Jul 2020 20:59:38 +0200 Subject: [PATCH] Handle 16 bit PNG files in sRGB format (cherry picked from commit e7e632cbedce3104e08b44525c6da78913fd8e53) --- drivers/png/image_loader_png.cpp | 5 +++-- drivers/png/png_driver_common.cpp | 8 ++++++-- drivers/png/png_driver_common.h | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/drivers/png/image_loader_png.cpp b/drivers/png/image_loader_png.cpp index eb7d196e1d3..6a49fa46367 100644 --- a/drivers/png/image_loader_png.cpp +++ b/drivers/png/image_loader_png.cpp @@ -51,7 +51,7 @@ Error ImageLoaderPNG::load_image(Ref p_image, FileAccess *f, bool p_force f->close(); } PoolVector::Read reader = file_buffer.read(); - return PNGDriverCommon::png_to_image(reader.ptr(), buffer_size, p_image); + return PNGDriverCommon::png_to_image(reader.ptr(), buffer_size, p_force_linear, p_image); } void ImageLoaderPNG::get_recognized_extensions(List *p_extensions) const { @@ -64,7 +64,8 @@ Ref ImageLoaderPNG::load_mem_png(const uint8_t *p_png, int p_size) { Ref img; img.instance(); - Error err = PNGDriverCommon::png_to_image(p_png, p_size, img); + // the value of p_force_linear does not matter since it only applies to 16 bit + Error err = PNGDriverCommon::png_to_image(p_png, p_size, false, img); ERR_FAIL_COND_V(err, Ref()); return img; diff --git a/drivers/png/png_driver_common.cpp b/drivers/png/png_driver_common.cpp index f4dbf135bb6..4994a33bb2c 100644 --- a/drivers/png/png_driver_common.cpp +++ b/drivers/png/png_driver_common.cpp @@ -58,8 +58,7 @@ static bool check_error(const png_image &image) { return false; } -Error png_to_image(const uint8_t *p_source, size_t p_size, Ref p_image) { - +Error png_to_image(const uint8_t *p_source, size_t p_size, bool p_force_linear, Ref p_image) { png_image png_img; zeromem(&png_img, sizeof(png_img)); png_img.version = PNG_IMAGE_VERSION; @@ -100,6 +99,11 @@ Error png_to_image(const uint8_t *p_source, size_t p_size, Ref p_image) { return ERR_UNAVAILABLE; } + if (!p_force_linear) { + // assume 16 bit pngs without sRGB or gAMA chunks are in sRGB format + png_img.flags |= PNG_IMAGE_FLAG_16BIT_sRGB; + } + const png_uint_32 stride = PNG_IMAGE_ROW_STRIDE(png_img); PoolVector buffer; Error err = buffer.resize(PNG_IMAGE_BUFFER_SIZE(png_img, stride)); diff --git a/drivers/png/png_driver_common.h b/drivers/png/png_driver_common.h index ee1d32ce68f..5f411f0aad2 100644 --- a/drivers/png/png_driver_common.h +++ b/drivers/png/png_driver_common.h @@ -37,7 +37,7 @@ namespace PNGDriverCommon { // Attempt to load png from buffer (p_source, p_size) into p_image -Error png_to_image(const uint8_t *p_source, size_t p_size, Ref p_image); +Error png_to_image(const uint8_t *p_source, size_t p_size, bool p_force_linear, Ref p_image); // Append p_image, as a png, to p_buffer. // Contents of p_buffer is unspecified if error returned.