From cc816b486c933a48d0b9d1fc8c6e2cd1b59efcc3 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Mon, 27 Nov 2023 22:44:14 -0500 Subject: [PATCH] LibGfx/TIFF: Use `LZWDecoder::decode_all()` --- .../LibGfx/ImageFormats/TIFFLoader.cpp | 35 ++++--------------- 1 file changed, 7 insertions(+), 28 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp index 45c96baf6a..7984a8cd29 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp @@ -115,40 +115,19 @@ private: TRY(loop_over_pixels([this]() { return read_value(); })); break; case Compression::LZW: { - Vector result_buffer {}; - Optional> decoder {}; - - u16 clear_code {}; - u16 end_of_information_code {}; + ByteBuffer decoded_bytes {}; + u32 read_head {}; auto initializer = [&](u32 bytes) -> ErrorOr { - auto strip_stream = make(TRY(m_stream->read_in_place(bytes))); - auto lzw_stream = make(MaybeOwned(move(strip_stream))); - decoder = Compress::LZWDecoder { MaybeOwned { move(lzw_stream) }, 8, -1 }; - - clear_code = decoder->add_control_code(); - end_of_information_code = decoder->add_control_code(); - + decoded_bytes = TRY(Compress::LZWDecoder::decode_all(TRY(m_stream->read_in_place(bytes)), 8, -1)); + read_head = 0; return {}; }; auto read_lzw_byte = [&]() -> ErrorOr { - while (true) { - if (!result_buffer.is_empty()) - return result_buffer.take_first(); - - auto const code = TRY(decoder->next_code()); - - if (code == clear_code) { - decoder->reset(); - continue; - } - - if (code == end_of_information_code) - return Error::from_string_literal("TIFFImageDecoderPlugin: Reached end of LZW stream"); - - result_buffer = decoder->get_output(); - } + if (read_head < decoded_bytes.size()) + return decoded_bytes[read_head++]; + return Error::from_string_literal("TIFFImageDecoderPlugin: Reached end of LZW stream"); }; TRY(loop_over_pixels([read_lzw_byte = move(read_lzw_byte)]() { return read_lzw_byte(); }, move(initializer)));