From 9b50b5793b66f730fee0908df66110dcf6fa0264 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Sun, 14 Jan 2024 23:03:41 -0500 Subject: [PATCH] LibGfx/TIFF: Factorize code to verify that CCITT images are correct --- .../LibGfx/ImageFormats/TIFFLoader.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp index 7a04f5ba33..38b61d1f6c 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp @@ -258,6 +258,18 @@ private: return {}; } + ErrorOr ensure_tags_are_correct_for_ccitt() const + { + // Section 8: Baseline Field Reference Guide + // BitsPerSample must be 1, since this type of compression is defined only for bilevel images. + if (m_metadata.bits_per_sample()->size() > 1) + return Error::from_string_literal("TIFFImageDecoderPlugin: CCITT image with BitsPerSample greater than one"); + if (m_metadata.photometric_interpretation() != PhotometricInterpretation::WhiteIsZero && m_metadata.photometric_interpretation() != PhotometricInterpretation::BlackIsZero) + return Error::from_string_literal("TIFFImageDecoderPlugin: CCITT compression is used on a non bilevel image"); + + return {}; + } + ErrorOr decode_frame_impl() { switch (*m_metadata.compression()) { @@ -270,12 +282,7 @@ private: break; } case Compression::CCITT: { - // Section 8: Baseline Field Reference Guide - // BitsPerSample must be 1, since this type of compression is defined only for bilevel images. - if (m_metadata.bits_per_sample()->size() > 1) - return Error::from_string_literal("TIFFImageDecoderPlugin: CCITT image with BitsPerSample greater than one"); - if (m_metadata.photometric_interpretation() != PhotometricInterpretation::WhiteIsZero && m_metadata.photometric_interpretation() != PhotometricInterpretation::BlackIsZero) - return Error::from_string_literal("TIFFImageDecoderPlugin: CCITT compression is used on a non bilevel image"); + TRY(ensure_tags_are_correct_for_ccitt()); ByteBuffer decoded_bytes {}; auto decode_ccitt_1D_strip = [&](u32 num_bytes) -> ErrorOr {