1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 07:57:47 +00:00

LibGfx/CCITT: Put the code to read the run length in its own function

This is already nice to do for the sole purpose of the readability but
that will also become handy for the 2D decoder.
This commit is contained in:
Lucas CHOLLET 2024-02-15 15:51:58 -05:00 committed by Jelle Raaijmakers
parent 3d63dd5c53
commit 9116cc3f45

View file

@ -272,27 +272,11 @@ Color invert(Color current_color)
return current_color == ccitt_white ? ccitt_black : ccitt_white;
}
ErrorOr<void> decode_single_ccitt3_1d_line(BigEndianInputBitStream& input_bit_stream, BigEndianOutputBitStream& decoded_bits, u32 image_width)
ErrorOr<u32> read_run_length(BigEndianInputBitStream& input_bit_stream, Color current_color, u32 image_width, u32 column)
{
// We always flip the color when entering the loop, so let's initialize the
// color with black to make the first marker actually be white.
Color current_color { ccitt_black };
u32 run_length = 0;
u32 column = 0;
while (column < image_width) {
if (run_length > 0) {
run_length--;
TRY(decoded_bits.write_bits(current_color == ccitt_white ? 0u : 1u, 1));
++column;
continue;
}
current_color = invert(current_color);
u8 size {};
u16 potential_code {};
u32 run_length {};
while (size < 14) {
potential_code <<= 1;
potential_code |= TRY(input_bit_stream.read_bit());
@ -313,7 +297,31 @@ ErrorOr<void> decode_single_ccitt3_1d_line(BigEndianInputBitStream& input_bit_st
return Error::from_string_literal("TIFFImageDecoderPlugin: Invalid CCITT code");
if (column + run_length > image_width)
return Error::from_string_literal("TIFFImageDecoderPlugin: CCITT codes encode for more that a line");
return Error::from_string_literal("TIFFImageDecoderPlugin: CCITT codes encode for more than a line");
return run_length;
}
ErrorOr<void> decode_single_ccitt3_1d_line(BigEndianInputBitStream& input_bit_stream, BigEndianOutputBitStream& decoded_bits, u32 image_width)
{
// We always flip the color when entering the loop, so let's initialize the
// color with black to make the first marker actually be white.
Color current_color { ccitt_black };
u32 run_length = 0;
u32 column = 0;
while (column < image_width) {
if (run_length > 0) {
run_length--;
TRY(decoded_bits.write_bits(current_color == ccitt_white ? 0u : 1u, 1));
++column;
continue;
}
current_color = invert(current_color);
run_length += TRY(read_run_length(input_bit_stream, current_color, image_width, column));
}
TRY(decoded_bits.align_to_byte_boundary());