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

Code before:
    // Each Frame is byte-aligned by invoking ZeroPadToByte() (B.2.7)
    stream.align_to_byte_boundary();

    Frame frame;

    frame.frame_header = TRY(read_frame_header(stream, size_header, metadata));

    if (!frame.frame_header.have_crop) {
        frame.width = size_header.width;
        frame.height = size_header.height;
    } else {
        frame.width = frame.frame_header.width;
        frame.height = frame.frame_header.height;
    }

    if (frame.frame_header.upsampling > 1) {
        frame.width = ceil(static_cast<double>(frame.width) / frame.frame_header.upsampling);
        frame.height = ceil(static_cast<double>(frame.height) / frame.frame_header.upsampling);
    }

    // "If lf_level > 0 (which is also a field in frame_header), then
    // width = ceil(width / (1 << (3 * lf_level))) and height = ceil(height / (1 << (3 * lf_level)))."
    if (frame.frame_header.lf_level > 0) {
        frame.width = ceil(static_cast<f32>(frame.width) / (1 << (3 * frame.frame_header.lf_level)));
        frame.height = ceil(static_cast<f32>(frame.height) / (1 << (3 * frame.frame_header.lf_level)));
    }

    dbgln_if(JPEGXL_DEBUG, "Frame{}: {}x{} {} - {} - flags({}){}"sv,
        frame.frame_header.name.is_empty() ? ""sv : MUST(String::formatted(" \"{}\"", frame.frame_header.name)),
        frame.width, frame.height,
        frame.frame_header.encoding,
        frame.frame_header.frame_type,
        to_underlying(frame.frame_header.flags),
        frame.frame_header.is_last ? " - is_last"sv : ""sv);

    auto const group_dim = frame.frame_header.group_dim();
    auto const frame_width = static_cast<double>(frame.width);
    auto const frame_height = static_cast<double>(frame.height);
    frame.num_groups = ceil(frame_width / group_dim) * ceil(frame_height / group_dim);
    frame.num_lf_groups = ceil(frame_width / (group_dim * 8)) * ceil(frame_height / (group_dim * 8));

    frame.toc = TRY(read_toc(stream, frame.frame_header, frame.num_groups, frame.num_lf_groups));

    if constexpr (JPEGXL_DEBUG) {
        dbgln("TOC: index |  size | offset");
        for (u32 i {}; i < frame.toc.entries.size(); ++i)
            dbgln("     {:5} | {:5} | {:6}", i, frame.toc.entries[i], frame.toc.group_offsets[i]);
    }

    auto bits_per_sample = metadata.bit_depth.bits_per_sample;


Review comment (refactor):
We should have an intergral `ceil_div` helper, if that helps

Code after:
    // Each Frame is byte-aligned by invoking ZeroPadToByte() (B.2.7)
    stream.align_to_byte_boundary();

    Frame frame;

    frame.frame_header = TRY(read_frame_header(stream, size_header, metadata));

    if (!frame.frame_header.have_crop) {
        frame.width = size_header.width;
        frame.height = size_header.height;
    } else {
        frame.width = frame.frame_header.width;
        frame.height = frame.frame_header.height;
    }

    if (frame.frame_header.upsampling > 1) {
        frame.width = ceil_div(frame.width, frame.frame_header.upsampling);
        frame.height = ceil_div(frame.height, frame.frame_header.upsampling);
    }

    // "If lf_level > 0 (which is also a field in frame_header), then
    // width = ceil(width / (1 << (3 * lf_level))) and height = ceil(height / (1 << (3 * lf_level)))."
    if (frame.frame_header.lf_level > 0) {
        frame.width = ceil_div(frame.width, 1u << (3 * frame.frame_header.lf_level));
        frame.height = ceil_div(frame.height, 1u << (3 * frame.frame_header.lf_level));
    }

    dbgln_if(JPEGXL_DEBUG, "Frame{}: {}x{} {} - {} - flags({}){}"sv,
        frame.frame_header.name.is_empty() ? ""sv : MUST(String::formatted(" \"{}\"", frame.frame_header.name)),
        frame.width, frame.height,
        frame.frame_header.encoding,
        frame.frame_header.frame_type,
        to_underlying(frame.frame_header.flags),
        frame.frame_header.is_last ? " - is_last"sv : ""sv);

    auto const group_dim = frame.frame_header.group_dim();
    auto const frame_width = static_cast<double>(frame.width);
    auto const frame_height = static_cast<double>(frame.height);
    frame.num_groups = ceil(frame_width / group_dim) * ceil(frame_height / group_dim);
    frame.num_lf_groups = ceil(frame_width / (group_dim * 8)) * ceil(frame_height / (group_dim * 8));

    frame.toc = TRY(read_toc(stream, frame.frame_header, frame.num_groups, frame.num_lf_groups));

    if constexpr (JPEGXL_DEBUG) {
        dbgln("TOC: index |  size | offset");
        for (u32 i {}; i < frame.toc.entries.size(); ++i)
            dbgln("     {:5} | {:5} | {:6}", i, frame.toc.entries[i], frame.toc.group_offsets[i]);
    }

    auto bits_per_sample = metadata.bit_depth.bits_per_sample;