Repository: SerenityOS/serenity
File: Userland/Libraries/LibGfx/ImageFormats/CCITTDecoder.cpp (C++)

Code before:
{
    u8 size {};
    u8 potential_code {};
    while (size < 7) {
        potential_code <<= 1;
        potential_code |= TRY(input_bit_stream.read_bit());
        ++size;

        if (auto const maybe_mode = get_code_from_table(node_codes, potential_code, size); maybe_mode.has_value())
            return *maybe_mode;
    }

    return Variant<ModeCode, InvalidResult>(InvalidResult { potential_code });
}

enum class Search : u8 {
    B1,
    B2,
};

struct CCITTStatus {
    ReferenceLine current_line {};
    bool has_reached_eol { false };
};

ErrorOr<void> ensure_invalid_result_is_actually_eol(BigEndianInputBitStream& input_bit_stream, InvalidResult partially_read_eol)
{
    if (partially_read_eol != 0)
        return Error::from_string_literal("CCITTDecoder: Unable to find the correct mode");

    auto const remaining_eol = TRY(input_bit_stream.read_bits(5));
    if (remaining_eol != 1) {
        return Error::from_string_literal("CCITTDecoder: Unable to find the correct mode");
    }

    return {};
}

ErrorOr<CCITTStatus> decode_single_ccitt_2d_line(BigEndianInputBitStream& input_bit_stream, BigEndianOutputBitStream& decoded_bits, ReferenceLine&& reference_line, u32 image_width)
{
    CCITTStatus status {};
    Color current_color { ccitt_white };
    u32 column {};
    u32 remainder_from_pass_mode {};

    auto const next_change_on_reference_line = [&]() -> ErrorOr<Change> {
        // 4.2.1.3.1 Definition of changing picture elements
        Optional<Change> next_change {}; // This is referred to as b1 in the spec.
        u32 offset {};
        while (!next_change.has_value()) {
            if (reference_line.is_empty() || reference_line.size() <= offset)

Review comment (suggestion):
nit: I'd spell out "NFC" in the commit description. (I usually write "No behavior change" instead of "No functional change", but eh.)

Code after:
{
    u8 size {};
    u8 potential_code {};
    while (size < 7) {
        potential_code <<= 1;
        potential_code |= TRY(input_bit_stream.read_bit());
        ++size;

        if (auto const maybe_mode = get_code_from_table(node_codes, potential_code, size); maybe_mode.has_value())
            return *maybe_mode;
    }

    return Variant<ModeCode, InvalidResult>(InvalidResult { potential_code });
}

enum class Search : u8 {
    B1,
    B2,
};

struct CCITTStatus {
    ReferenceLine current_line {};
    bool has_reached_eol { false };
};

ErrorOr<void> ensure_invalid_result_is_actually_eol(BigEndianInputBitStream& input_bit_stream, InvalidResult partially_read_eol, Group4Options const& options)
{
    if (partially_read_eol != 0)
        return Error::from_string_literal("CCITTDecoder: Unable to find the correct mode");

    auto const remaining_eol = TRY(input_bit_stream.read_bits(5));
    if (options.has_end_of_block == Group4Options::HasEndOfBlock::Yes
        && remaining_eol == 0) {
        // Some PDF like 00000337.pdf ends with an EOFB [1] that is byte aligned. This is
        // what we are trying to detect/read here. As we already read 12 bytes from
        // partially_read_eol and remaining_eol, we need to realign ourselves first.
        // [1] 2.4.1.1 End-of-facsimile block

        static constexpr u32 EOFB = 0x001001;

        u8 fill_bits_length = (12 + input_bit_stream.bits_until_next_byte_boundary()) % 8;
        u8 to_read = fill_bits_length + 12;
        auto potential_eofb = TRY(input_bit_stream.read_bits(to_read));

        // We already checked that the 12 first bits were zeroes, so here we check that the
        // last to_read bits end with EOFB.
        if (potential_eofb != EOFB)
            return Error::from_string_literal("CCITTDecoder: Unable to find the correct mode");
    } else if (remaining_eol != 1) {
        return Error::from_string_literal("CCITTDecoder: Unable to find the correct mode");
    }