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

LibGfx/TIFF: Use LZWDecoder::decode_all()

This commit is contained in:
Lucas CHOLLET 2023-11-27 22:44:14 -05:00 committed by Andreas Kling
parent 2a5cb5becb
commit cc816b486c

View file

@ -115,40 +115,19 @@ private:
TRY(loop_over_pixels([this]() { return read_value<u8>(); })); TRY(loop_over_pixels([this]() { return read_value<u8>(); }));
break; break;
case Compression::LZW: { case Compression::LZW: {
Vector<u8> result_buffer {}; ByteBuffer decoded_bytes {};
Optional<Compress::LZWDecoder<BigEndianInputBitStream>> decoder {}; u32 read_head {};
u16 clear_code {};
u16 end_of_information_code {};
auto initializer = [&](u32 bytes) -> ErrorOr<void> { auto initializer = [&](u32 bytes) -> ErrorOr<void> {
auto strip_stream = make<FixedMemoryStream>(TRY(m_stream->read_in_place<u8 const>(bytes))); decoded_bytes = TRY(Compress::LZWDecoder<BigEndianInputBitStream>::decode_all(TRY(m_stream->read_in_place<u8 const>(bytes)), 8, -1));
auto lzw_stream = make<BigEndianInputBitStream>(MaybeOwned<Stream>(move(strip_stream))); read_head = 0;
decoder = Compress::LZWDecoder { MaybeOwned<BigEndianInputBitStream> { move(lzw_stream) }, 8, -1 };
clear_code = decoder->add_control_code();
end_of_information_code = decoder->add_control_code();
return {}; return {};
}; };
auto read_lzw_byte = [&]() -> ErrorOr<u8> { auto read_lzw_byte = [&]() -> ErrorOr<u8> {
while (true) { if (read_head < decoded_bytes.size())
if (!result_buffer.is_empty()) return decoded_bytes[read_head++];
return result_buffer.take_first(); return Error::from_string_literal("TIFFImageDecoderPlugin: Reached end of LZW stream");
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();
}
}; };
TRY(loop_over_pixels([read_lzw_byte = move(read_lzw_byte)]() { return read_lzw_byte(); }, move(initializer))); TRY(loop_over_pixels([read_lzw_byte = move(read_lzw_byte)]() { return read_lzw_byte(); }, move(initializer)));