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

Code before:
{
    // 18181-2: 9.1  JPEG XL Signature box (JXL␣)
    static constexpr Array signature = to_array<u8>({
        // clang-format off
        0x00, 0x00, 0x00, 0x0C,
        0x4A, 0x58, 0x4C, 0x20,
        0x0D, 0x0A, 0x87, 0x0A,
        // clang-format on
    });
    bool is_container = data.starts_with(signature);
    return is_raw_codestream(data) || is_container;
}

static ErrorOr<Vector<u8>> extract_codestream_from_container(NonnullOwnPtr<FixedMemoryStream> input)
{
    auto box_reader = TRY(ISOBMFF::Reader::create(move(input)));
    auto box_list = TRY(box_reader.read_entire_file());

    for (auto& box : box_list) {
        if (box->box_type() == ISOBMFF::BoxType::JPEGXLCodestreamBox) {
            auto& codestream_box = *reinterpret_cast<ISOBMFF::JPEGXLCodestreamBox*>(box.ptr());
            return move(codestream_box.codestream);
        }
    }

    bool saw_jxlp_box = false;
    Vector<u8> codestream;
    for (auto& box : box_list) {
        if (box->box_type() == ISOBMFF::BoxType::JPEGXLPartialCodestreamBox) {
            saw_jxlp_box = true;
            // FIXME: Do we need to do anything with part indices here?
            auto& partial_box = *reinterpret_cast<ISOBMFF::JPEGXLPartialCodestreamBox*>(box.ptr());
            TRY(codestream.try_extend(partial_box.codestream));
        }
    }
    if (saw_jxlp_box)
        return codestream;

    return Error::from_string_literal("JPEGXLLoader: No jxlc box and no jxlp boxes found");
}

ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> JPEGXLImageDecoderPlugin::create(ReadonlyBytes data)
{
    auto stream = TRY(try_make<FixedMemoryStream>(data));
    Optional<Vector<u8>> jxlc_content;
    if (!is_raw_codestream(data)) {
        jxlc_content = TRY(extract_codestream_from_container(move(stream)));
        stream = TRY(try_make<FixedMemoryStream>(jxlc_content->span()));
    }
    auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) JPEGXLImageDecoderPlugin(move(jxlc_content), move(stream))));
    TRY(plugin->m_context->decode_image_header());

Review comment (suggestion):
The spec says that the `jxlc` and `jxlp` boxes are mutually exclusive. It would be nice to report an error if they are both present.
> A JPEG XL file shall contain either exactly one JPEG XL codestream box, or one or more JPEG XL partial codestream boxes, but not both.

Code after:
{
    // 18181-2: 9.1  JPEG XL Signature box (JXL␣)
    static constexpr Array signature = to_array<u8>({
        // clang-format off
        0x00, 0x00, 0x00, 0x0C,
        0x4A, 0x58, 0x4C, 0x20,
        0x0D, 0x0A, 0x87, 0x0A,
        // clang-format on
    });
    bool is_container = data.starts_with(signature);
    return is_raw_codestream(data) || is_container;
}

static ErrorOr<Vector<u8>> extract_codestream_from_container(NonnullOwnPtr<FixedMemoryStream> input)
{
    auto box_reader = TRY(ISOBMFF::Reader::create(move(input)));
    auto box_list = TRY(box_reader.read_entire_file());

    size_t jxlc_box_count = 0;
    size_t jxlp_box_count = 0;
    for (auto& box : box_list) {
        if (box->box_type() == ISOBMFF::BoxType::JPEGXLCodestreamBox)
            jxlc_box_count++;
        else if (box->box_type() == ISOBMFF::BoxType::JPEGXLPartialCodestreamBox)
            jxlp_box_count++;
    }

    // "A JPEG XL file shall contain either exactly one JPEG XL codestream box, or one or more JPEG XL partial
    //  codestream boxes, but not both."
    if (jxlc_box_count == 0 && jxlp_box_count == 0)
        return Error::from_string_literal("JPEGXLLoader: No jxlc box and no jxlp boxes found");
    if (jxlc_box_count > 1)
        return Error::from_string_literal("JPEGXLLoader: Multiple jxlc boxes found");
    if (jxlp_box_count > 0 && jxlc_box_count > 0)
        return Error::from_string_literal("JPEGXLLoader: Both jxlc box and jxlp boxes found");

    if (jxlc_box_count > 0) {
        auto& box = *box_list.find_if([](auto& box) { return box->box_type() == ISOBMFF::BoxType::JPEGXLCodestreamBox; });
        auto& codestream_box = static_cast<ISOBMFF::JPEGXLCodestreamBox&>(*box);
        return move(codestream_box.codestream);
    }

    // "The index modulo 2^31 shall be 0 for the first partial
    //  codestream box, and incremented by 1 for each next partial codestream box. The index shall be lower
    //  than 2^31, except for the last partial codestream box, which shall have an index of at least 2^31. The boxes
    //  shall appear in the file in order of increasing index. The full concatenation of all partial codestream
    //  boxes in this order shall form exactly one complete and valid JPEG XL codestream."
    // FIXME: Try to prevent the extra copy, maybe with a non-contiguous steam class.
    VERIFY(jxlp_box_count > 0);
    size_t next_part_index = 0;
    Vector<u8> codestream;