From 6c79201f43087f778aaadacf0c2cada368122c07 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Fri, 27 Jan 2023 20:32:47 -0500 Subject: [PATCH] LibGfx: Make PNGImageDecoderPlugin::icc_data() return data if present --- Userland/Libraries/LibGfx/PNGLoader.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Userland/Libraries/LibGfx/PNGLoader.cpp b/Userland/Libraries/LibGfx/PNGLoader.cpp index 5e931cbfff..2578eae7ef 100644 --- a/Userland/Libraries/LibGfx/PNGLoader.cpp +++ b/Userland/Libraries/LibGfx/PNGLoader.cpp @@ -133,6 +133,7 @@ struct PNGLoadingContext { Optional coding_independent_code_points; Optional gamma; Optional embedded_icc_profile; + Optional decompressed_icc_profile; Optional sRGB_rendering_intent; Checked compute_row_size_for_width(int width) @@ -1080,6 +1081,30 @@ ErrorOr PNGImageDecoderPlugin::frame(size_t index) ErrorOr> PNGImageDecoderPlugin::icc_data() { + if (!decode_png_chunks(*m_context)) + return Error::from_string_literal("PNGImageDecoderPlugin: Decoding chunks failed"); + + if (m_context->embedded_icc_profile.has_value()) { + if (!m_context->decompressed_icc_profile.has_value()) { + auto result = Compress::ZlibDecompressor::decompress_all(m_context->embedded_icc_profile->compressed_data); + if (!result.has_value()) { + m_context->embedded_icc_profile.clear(); + return Error::from_string_literal("PNGImageDecoderPlugin: Decompression of ICC profile failed"); + } + m_context->decompressed_icc_profile = move(*result); + } + + return m_context->decompressed_icc_profile.value(); + } + + // FIXME: Eventually, look at coding_independent_code_points, chromaticities_and_whitepoint, gamma, sRGB_rendering_intent too. + // The order is: + // 1. Use coding_independent_code_points if it exists, ignore the rest. + // 2. Use embedded_icc_profile if it exists, ignore the rest. + // 3. Use sRGB_rendering_intent if it exists, ignore the rest. + // 4. Use gamma to adjust gamma and chromaticities_and_whitepoint to adjust color. + // (Order between 2 and 3 isn't fully clear, but "It is recommended that the sRGB and iCCP chunks do not appear simultaneously in a PNG datastream." + return OptionalNone {}; }