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

Code before:
    Optional<JBIG2::HuffmanTable> huffman_table;

    // Set on intermediate direct region segments after they've been decoded.
    OwnPtr<Gfx::BilevelImage> aux_buffer;
};

template<JBIG2::CombinationOperator Operator>
static void composite_bilevel_image_impl(BilevelImage& out, BilevelImage const& bitmap, Gfx::IntRect clip_rect, Gfx::IntPoint position)
{
    for (int y = clip_rect.top(); y < clip_rect.bottom(); ++y) {
        for (int x = clip_rect.left(); x < clip_rect.right(); ++x) {
            bool src_bit = bitmap.get_bit(x - position.x(), y - position.y());
            bool dst_bit = out.get_bit(x, y);

            bool result = [&]() -> bool {
                if (Operator == JBIG2::CombinationOperator::Or)
                    return dst_bit || src_bit;
                if (Operator == JBIG2::CombinationOperator::And)
                    return dst_bit && src_bit;
                if (Operator == JBIG2::CombinationOperator::Xor)
                    return dst_bit ^ src_bit;
                if (Operator == JBIG2::CombinationOperator::XNor)
                    return !(dst_bit ^ src_bit);
                if (Operator == JBIG2::CombinationOperator::Replace)
                    return src_bit;
                VERIFY_NOT_REACHED();
            }();

            out.set_bit(x, y, result);
        }
    }
}

static void composite_bilevel_image(BilevelImage& out, BilevelImage const& bitmap, Gfx::IntPoint position, JBIG2::CombinationOperator operator_)
{
    IntRect bitmap_rect { position, { bitmap.width(), bitmap.height() } };
    IntRect out_rect { { 0, 0 }, { out.width(), out.height() } };
    IntRect clip_rect = bitmap_rect.intersected(out_rect);

    switch (operator_) {
    case JBIG2::CombinationOperator::Or:
        composite_bilevel_image_impl<JBIG2::CombinationOperator::Or>(out, bitmap, clip_rect, position);
        return;
    case JBIG2::CombinationOperator::And:
        composite_bilevel_image_impl<JBIG2::CombinationOperator::And>(out, bitmap, clip_rect, position);
        return;
    case JBIG2::CombinationOperator::Xor:
        composite_bilevel_image_impl<JBIG2::CombinationOperator::Xor>(out, bitmap, clip_rect, position);
        return;
    case JBIG2::CombinationOperator::XNor:
        composite_bilevel_image_impl<JBIG2::CombinationOperator::XNor>(out, bitmap, clip_rect, position);

Review comment (suggestion):
this could be use `if constexpr` instead
which can fully replace the iife
then `VERIFY_NOT_REACHED() -> static_assert(false);` in the last else branch I think

Code after:
    Optional<JBIG2::HuffmanTable> huffman_table;

    // Set on intermediate direct region segments after they've been decoded.
    OwnPtr<Gfx::BilevelImage> aux_buffer;
};

static void composite_bilevel_image(BilevelImage& out, BilevelImage const& bitmap, Gfx::IntPoint position, JBIG2::CombinationOperator operator_)
{
    static constexpr auto combine = [](bool dst, bool src, JBIG2::CombinationOperator op) -> bool {
        switch (op) {
        case JBIG2::CombinationOperator::Or:
            return dst || src;
        case JBIG2::CombinationOperator::And:
            return dst && src;
        case JBIG2::CombinationOperator::Xor:
            return dst ^ src;
        case JBIG2::CombinationOperator::XNor:
            return !(dst ^ src);
        case JBIG2::CombinationOperator::Replace:
            return src;
        }
        VERIFY_NOT_REACHED();
    };

    IntRect bitmap_rect { position, { bitmap.width(), bitmap.height() } };
    IntRect out_rect { { 0, 0 }, { out.width(), out.height() } };
    IntRect clip_rect = bitmap_rect.intersected(out_rect);

    for (int y = clip_rect.top(); y < clip_rect.bottom(); ++y) {
        for (int x = clip_rect.left(); x < clip_rect.right(); ++x) {
            bool src_bit = bitmap.get_bit(x - position.x(), y - position.y());
            bool dst_bit = out.get_bit(x, y);
            out.set_bit(x, y, combine(dst_bit, src_bit, operator_));
        }
    }
}

struct Page {
    IntSize size;

    // This is never CombinationOperator::Replace for Pages.
    JBIG2::CombinationOperator default_combination_operator { JBIG2::CombinationOperator::Or };

    bool direct_region_segments_override_default_combination_operator { false };

    OwnPtr<BilevelImage> bits;
};

struct JBIG2LoadingContext {
    enum class State {
        NotDecoded = 0,