1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 01:17:35 +00:00

LibGfx/TIFF: Put manage_extra_channels in its own function

This commit is contained in:
Lucas CHOLLET 2024-01-18 19:23:11 -05:00 committed by Andrew Kaster
parent 3f4bf7a0c7
commit 984272d83e

View file

@ -161,14 +161,12 @@ private:
return OptionalNone {}; return OptionalNone {};
} }
ErrorOr<Color> read_color(BigEndianInputBitStream& stream) ErrorOr<u8> manage_extra_channels(BigEndianInputBitStream& stream, Vector<u32> const& bits_per_sample) const
{ {
auto bits_per_sample = *m_metadata.bits_per_sample();
// Section 7: Additional Baseline TIFF Requirements // 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 // 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. // them gracefully, using the values of the SamplesPerPixel and BitsPerSample fields.
auto manage_extra_channels = [&]() -> ErrorOr<u8> {
// Both unknown and alpha channels are considered as extra channels, so let's iterate over // 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. // them, conserve the alpha value (if any) and discard everything else.
@ -185,20 +183,24 @@ private:
} }
return alpha.value_or(NumericLimits<u8>::max()); return alpha.value_or(NumericLimits<u8>::max());
}; }
ErrorOr<Color> read_color(BigEndianInputBitStream& stream)
{
auto bits_per_sample = *m_metadata.bits_per_sample();
if (m_metadata.photometric_interpretation() == PhotometricInterpretation::RGB) { if (m_metadata.photometric_interpretation() == PhotometricInterpretation::RGB) {
auto const first_component = TRY(read_component(stream, bits_per_sample[0])); 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 second_component = TRY(read_component(stream, bits_per_sample[1]));
auto const third_component = TRY(read_component(stream, bits_per_sample[2])); 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); return Color(first_component, second_component, third_component, alpha);
} }
if (m_metadata.photometric_interpretation() == PhotometricInterpretation::RGBPalette) { if (m_metadata.photometric_interpretation() == PhotometricInterpretation::RGBPalette) {
auto const index = TRY(stream.read_bits<u16>(bits_per_sample[0])); auto const index = TRY(stream.read_bits<u16>(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 // SamplesPerPixel == 1 is a requirement for RGBPalette
// From description of PhotometricInterpretation in Section 8: Baseline Field Reference Guide // From description of PhotometricInterpretation in Section 8: Baseline Field Reference Guide
@ -229,7 +231,7 @@ private:
if (m_metadata.photometric_interpretation() == PhotometricInterpretation::WhiteIsZero) if (m_metadata.photometric_interpretation() == PhotometricInterpretation::WhiteIsZero)
luminosity = ~luminosity; 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); return Color(luminosity, luminosity, luminosity, alpha);
} }