From 984272d83ee5a0c29d609fad8ec820e5033a5e07 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Thu, 18 Jan 2024 19:23:11 -0500 Subject: [PATCH] LibGfx/TIFF: Put `manage_extra_channels` in its own function --- .../LibGfx/ImageFormats/TIFFLoader.cpp | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp index 9b66836d2d..6be1d72d55 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp @@ -161,44 +161,46 @@ private: return OptionalNone {}; } - ErrorOr read_color(BigEndianInputBitStream& stream) + ErrorOr manage_extra_channels(BigEndianInputBitStream& stream, Vector const& bits_per_sample) const { - auto bits_per_sample = *m_metadata.bits_per_sample(); - // Section 7: Additional Baseline TIFF Requirements // Some TIFF files may have more components per pixel than you think. A Baseline TIFF reader must skip over // them gracefully, using the values of the SamplesPerPixel and BitsPerSample fields. - auto manage_extra_channels = [&]() -> ErrorOr { - // Both unknown and alpha channels are considered as extra channels, so let's iterate over - // 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(); + // Both unknown and alpha channels are considered as extra channels, so let's iterate over + // them, conserve the alpha value (if any) and discard everything else. - Optional alpha {}; + auto const number_base_channels = samples_for_photometric_interpretation(); + auto const alpha_index = alpha_channel_index(); - for (u8 i = number_base_channels; i < bits_per_sample.size(); ++i) { - if (alpha_index == i) - alpha = TRY(read_component(stream, bits_per_sample[i])); - else - TRY(read_component(stream, bits_per_sample[i])); - } + Optional alpha {}; - return alpha.value_or(NumericLimits::max()); - }; + for (u8 i = number_base_channels; i < bits_per_sample.size(); ++i) { + if (alpha_index == i) + alpha = TRY(read_component(stream, bits_per_sample[i])); + else + TRY(read_component(stream, bits_per_sample[i])); + } + + return alpha.value_or(NumericLimits::max()); + } + + ErrorOr read_color(BigEndianInputBitStream& stream) + { + auto bits_per_sample = *m_metadata.bits_per_sample(); if (m_metadata.photometric_interpretation() == PhotometricInterpretation::RGB) { auto const first_component = TRY(read_component(stream, bits_per_sample[0])); auto const second_component = TRY(read_component(stream, bits_per_sample[1])); auto const third_component = TRY(read_component(stream, bits_per_sample[2])); - auto const alpha = TRY(manage_extra_channels()); + auto const alpha = TRY(manage_extra_channels(stream, bits_per_sample)); return Color(first_component, second_component, third_component, alpha); } if (m_metadata.photometric_interpretation() == PhotometricInterpretation::RGBPalette) { auto const index = TRY(stream.read_bits(bits_per_sample[0])); - auto const alpha = TRY(manage_extra_channels()); + auto const alpha = TRY(manage_extra_channels(stream, bits_per_sample)); // SamplesPerPixel == 1 is a requirement for RGBPalette // From description of PhotometricInterpretation in Section 8: Baseline Field Reference Guide @@ -229,7 +231,7 @@ private: if (m_metadata.photometric_interpretation() == PhotometricInterpretation::WhiteIsZero) luminosity = ~luminosity; - auto const alpha = TRY(manage_extra_channels()); + auto const alpha = TRY(manage_extra_channels(stream, bits_per_sample)); return Color(luminosity, luminosity, luminosity, alpha); }