From 472a886dfcc204b4295689bc6be4393b0853fa81 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Sun, 29 Oct 2023 16:12:37 -0400 Subject: [PATCH] LibGfx/TIFF: Refactor the decoding loop This is done to ease the introduction of the support of other compressions. --- .../LibGfx/ImageFormats/TIFFLoader.cpp | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp index 8ca592b94e..df2df3352d 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp @@ -102,13 +102,9 @@ private: PackBits = 32773, }; - ErrorOr decode_frame_impl() + template + ErrorOr loop_over_pixels(ByteReader&& byte_reader) { - if (m_compression != Compression::NoCompression) - return Error::from_string_literal("Compressed TIFF are not supported yet :^)"); - - m_bitmap = TRY(Bitmap::create(BitmapFormat::BGRA8888, m_size)); - for (u32 strip_index = 0; strip_index < m_strip_offsets.size(); ++strip_index) { TRY(m_stream->seek(m_strip_offsets[strip_index])); for (u32 row = 0; row < m_rows_per_strip; row++) { @@ -117,11 +113,27 @@ private: break; for (u32 column = 0; column < static_cast(m_size.width()); ++column) { - Color const color { TRY(read_value()), TRY(read_value()), TRY(read_value()) }; + auto const color = Color { TRY(byte_reader()), TRY(byte_reader()), TRY(byte_reader()) }; m_bitmap->set_pixel(column, scanline, color); } } } + + return {}; + } + + ErrorOr decode_frame_impl() + { + m_bitmap = TRY(Bitmap::create(BitmapFormat::BGRA8888, m_size)); + + switch (m_compression) { + case Compression::NoCompression: + TRY(loop_over_pixels([this]() { return read_value(); })); + break; + default: + return Error::from_string_literal("Compressed TIFF are not supported yet :^)"); + } + return {}; }