From 18871e23d7db28c982449d0600bb8585528fb7a3 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Fri, 9 Feb 2024 21:42:02 -0500 Subject: [PATCH] LibGfx/TIFF: Make decoders take an `IntSize` They will also need the width of the sub-image when we will add support for tiles. --- .../LibGfx/ImageFormats/TIFFLoader.cpp | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp index 934cef992f0..15332cfc354 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp @@ -262,7 +262,7 @@ private: return CMYK { first_component, second_component, third_component, fourth_component }; } - template, u32, u32> StripDecoder> + template, u32, IntSize> StripDecoder> ErrorOr loop_over_pixels(StripDecoder&& strip_decoder) { auto const strips_offset = *m_metadata.strip_offsets(); @@ -278,8 +278,10 @@ private: for (u32 strip_index = 0; strip_index < strips_offset.size(); ++strip_index) { TRY(m_stream->seek(strips_offset[strip_index])); + auto const strip_width = *m_metadata.image_width(); auto const rows_in_strip = strip_index < strips_offset.size() - 1 ? rows_per_strip : *m_metadata.image_length() - rows_per_strip * strip_index; - auto const decoded_bytes = TRY(strip_decoder(strip_byte_counts[strip_index], rows_in_strip)); + + auto const decoded_bytes = TRY(strip_decoder(strip_byte_counts[strip_index], {strip_width, rows_in_strip})); auto decoded_strip = make(decoded_bytes); auto decoded_stream = make(move(decoded_strip)); @@ -359,7 +361,7 @@ private: { switch (*m_metadata.compression()) { case Compression::NoCompression: { - auto identity = [&](u32 num_bytes, u32) { + auto identity = [&](u32 num_bytes, IntSize) { return m_stream->read_in_place(num_bytes); }; @@ -370,9 +372,9 @@ private: TRY(ensure_tags_are_correct_for_ccitt()); ByteBuffer decoded_bytes {}; - auto decode_ccitt_rle_strip = [&](u32 num_bytes, u32 image_height) -> ErrorOr { + auto decode_ccitt_rle_strip = [&](u32 num_bytes, IntSize image_size) -> ErrorOr { auto const encoded_bytes = TRY(read_bytes_considering_fill_order(num_bytes)); - decoded_bytes = TRY(CCITT::decode_ccitt_rle(encoded_bytes, *m_metadata.image_width(), image_height)); + decoded_bytes = TRY(CCITT::decode_ccitt_rle(encoded_bytes, image_size.width(), image_size.height())); return decoded_bytes; }; @@ -384,9 +386,9 @@ private: auto const parameters = parse_t4_options(*m_metadata.t4_options()); ByteBuffer decoded_bytes {}; - auto decode_group3_strip = [&](u32 num_bytes, u32 strip_height) -> ErrorOr { + auto decode_group3_strip = [&](u32 num_bytes, IntSize image_size) -> ErrorOr { auto const encoded_bytes = TRY(read_bytes_considering_fill_order(num_bytes)); - decoded_bytes = TRY(CCITT::decode_ccitt_group3(encoded_bytes, *m_metadata.image_width(), strip_height, parameters)); + decoded_bytes = TRY(CCITT::decode_ccitt_group3(encoded_bytes, image_size.width(), image_size.height(), parameters)); return decoded_bytes; }; @@ -395,7 +397,7 @@ private: } case Compression::LZW: { ByteBuffer decoded_bytes {}; - auto decode_lzw_strip = [&](u32 num_bytes, u32) -> ErrorOr { + auto decode_lzw_strip = [&](u32 num_bytes, IntSize) -> ErrorOr { auto const encoded_bytes = TRY(m_stream->read_in_place(num_bytes)); if (encoded_bytes.is_empty()) @@ -424,7 +426,7 @@ private: // This is an extension from the Technical Notes from 2002: // https://web.archive.org/web/20160305055905/http://partners.adobe.com/public/developer/en/tiff/TIFFphotoshop.pdf ByteBuffer decoded_bytes {}; - auto decode_zlib = [&](u32 num_bytes, u32) -> ErrorOr { + auto decode_zlib = [&](u32 num_bytes, IntSize) -> ErrorOr { auto stream = make(MaybeOwned(*m_stream), num_bytes); auto decompressed_stream = TRY(Compress::ZlibDecompressor::create(move(stream))); decoded_bytes = TRY(decompressed_stream->read_until_eof(4096)); @@ -438,7 +440,7 @@ private: // Section 9: PackBits Compression ByteBuffer decoded_bytes {}; - auto decode_packbits_strip = [&](u32 num_bytes, u32) -> ErrorOr { + auto decode_packbits_strip = [&](u32 num_bytes, IntSize) -> ErrorOr { auto const encoded_bytes = TRY(m_stream->read_in_place(num_bytes)); decoded_bytes = TRY(Compress::PackBits::decode_all(encoded_bytes)); return decoded_bytes;