From 9ae17e3a7acec196cafdfba8a7e21cd1cb85bc58 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Sat, 10 Feb 2024 00:57:25 -0500 Subject: [PATCH] LibGfx/CCITT: Align the output stream on byte-boundary after each line This makes the CCITT decoder in line with what the TIFF decoder is expecting. --- Userland/Libraries/LibGfx/ImageFormats/CCITTDecoder.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/CCITTDecoder.cpp b/Userland/Libraries/LibGfx/ImageFormats/CCITTDecoder.cpp index ace6134fd8..9c52911fc1 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/CCITTDecoder.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/CCITTDecoder.cpp @@ -313,6 +313,8 @@ ErrorOr decode_single_ccitt3_1d_line(BigEndianInputBitStream& input_bit_st return Error::from_string_literal("TIFFImageDecoderPlugin: CCITT codes encode for more that a line"); } + TRY(decoded_bits.align_to_byte_boundary()); + return {}; } @@ -333,7 +335,8 @@ ErrorOr decode_ccitt_rle(ReadonlyBytes bytes, u32 image_width, u32 i auto strip_stream = make(bytes); auto bit_stream = make(MaybeOwned(*strip_stream)); - ByteBuffer decoded_bytes = TRY(ByteBuffer::create_zeroed(ceil_div(image_width * image_height, 8))); + // Note: We put image_height extra-space to handle at most one alignment to byte boundary per line. + ByteBuffer decoded_bytes = TRY(ByteBuffer::create_zeroed(ceil_div(image_width * image_height, 8) + image_height)); auto output_stream = make(decoded_bytes.bytes()); auto decoded_bits = make(MaybeOwned(*output_stream)); @@ -341,7 +344,6 @@ ErrorOr decode_ccitt_rle(ReadonlyBytes bytes, u32 image_width, u32 i TRY(decode_single_ccitt3_1d_line(*bit_stream, *decoded_bits, image_width)); bit_stream->align_to_byte_boundary(); - TRY(decoded_bits->align_to_byte_boundary()); } return decoded_bytes; @@ -352,7 +354,8 @@ ErrorOr decode_ccitt_group3(ReadonlyBytes bytes, u32 image_width, u3 auto strip_stream = make(bytes); auto bit_stream = make(MaybeOwned(*strip_stream)); - ByteBuffer decoded_bytes = TRY(ByteBuffer::create_zeroed(ceil_div(image_width * image_height, 8))); + // Note: We put image_height extra-space to handle at most one alignment to byte boundary per line. + ByteBuffer decoded_bytes = TRY(ByteBuffer::create_zeroed(ceil_div(image_width * image_height, 8) + image_height)); auto output_stream = make(decoded_bytes.bytes()); auto decoded_bits = make(MaybeOwned(*output_stream));