Handle 16 bit PNG files in sRGB format

(cherry picked from commit e7e632cbed)
This commit is contained in:
Johan Rastén 2020-07-09 20:59:38 +02:00 committed by Rémi Verschelde
parent 6eb1a2eacb
commit 4c2d0d38fd
3 changed files with 10 additions and 5 deletions

View File

@ -51,7 +51,7 @@ Error ImageLoaderPNG::load_image(Ref<Image> p_image, FileAccess *f, bool p_force
f->close(); f->close();
} }
PoolVector<uint8_t>::Read reader = file_buffer.read(); PoolVector<uint8_t>::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<String> *p_extensions) const { void ImageLoaderPNG::get_recognized_extensions(List<String> *p_extensions) const {
@ -64,7 +64,8 @@ Ref<Image> ImageLoaderPNG::load_mem_png(const uint8_t *p_png, int p_size) {
Ref<Image> img; Ref<Image> img;
img.instance(); 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<Image>()); ERR_FAIL_COND_V(err, Ref<Image>());
return img; return img;

View File

@ -58,8 +58,7 @@ static bool check_error(const png_image &image) {
return false; return false;
} }
Error png_to_image(const uint8_t *p_source, size_t p_size, Ref<Image> p_image) { Error png_to_image(const uint8_t *p_source, size_t p_size, bool p_force_linear, Ref<Image> p_image) {
png_image png_img; png_image png_img;
zeromem(&png_img, sizeof(png_img)); zeromem(&png_img, sizeof(png_img));
png_img.version = PNG_IMAGE_VERSION; png_img.version = PNG_IMAGE_VERSION;
@ -100,6 +99,11 @@ Error png_to_image(const uint8_t *p_source, size_t p_size, Ref<Image> p_image) {
return ERR_UNAVAILABLE; 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); const png_uint_32 stride = PNG_IMAGE_ROW_STRIDE(png_img);
PoolVector<uint8_t> buffer; PoolVector<uint8_t> buffer;
Error err = buffer.resize(PNG_IMAGE_BUFFER_SIZE(png_img, stride)); Error err = buffer.resize(PNG_IMAGE_BUFFER_SIZE(png_img, stride));

View File

@ -37,7 +37,7 @@
namespace PNGDriverCommon { namespace PNGDriverCommon {
// Attempt to load png from buffer (p_source, p_size) into p_image // 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<Image> p_image); Error png_to_image(const uint8_t *p_source, size_t p_size, bool p_force_linear, Ref<Image> p_image);
// Append p_image, as a png, to p_buffer. // Append p_image, as a png, to p_buffer.
// Contents of p_buffer is unspecified if error returned. // Contents of p_buffer is unspecified if error returned.