From d375b5c2a549deb2ba53428ffe644c72b71f354c Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Thu, 15 Feb 2024 20:55:35 -0500 Subject: [PATCH] LibGfx/TIFF: Also cache the result of `alpha_channel_index()` This function was called over and over in `manage_extra_channels()`, even if the result depends only on the metadata. Instead, we now call it once and store the result. --- Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp index 8d9f1b393f..dfcbfa59ba 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp @@ -116,6 +116,7 @@ public: m_image_width = m_metadata.image_width().value(); if (m_metadata.predictor().has_value()) m_predictor = m_metadata.predictor().value(); + m_alpha_channel_index = alpha_channel_index(); } ErrorOr decode_frame() @@ -213,12 +214,11 @@ private: // them, conserve the alpha value (if any) and discard everything else. auto const number_base_channels = samples_for_photometric_interpretation(); - auto const alpha_index = alpha_channel_index(); Optional alpha {}; for (u8 i = number_base_channels; i < m_bits_per_sample.size(); ++i) { - if (alpha_index == i) + if (m_alpha_channel_index == i) alpha = TRY(read_component(stream, m_bits_per_sample[i])); else TRY(read_component(stream, m_bits_per_sample[i])); @@ -344,7 +344,7 @@ private: color.set_red(last_color->red() + color.red()); color.set_green(last_color->green() + color.green()); color.set_blue(last_color->blue() + color.blue()); - if (alpha_channel_index().has_value()) + if (m_alpha_channel_index.has_value()) color.set_alpha(last_color->alpha() + color.alpha()); } @@ -686,6 +686,8 @@ private: Vector m_bits_per_sample {}; u32 m_image_width {}; Predictor m_predictor {}; + + Optional m_alpha_channel_index {}; }; }