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

Code before:
            if (width == 3 || order == 3)
                return Error::from_string_literal("JPEGXLImageDecoderPlugin: Invalid width or order value");

            u64 stride = width;
            if ((flags & 16) != 0)
                stride = TRY(read_varint(command_stream));

            if (stride * 4 >= out.size() || stride < width)
                return Error::from_string_literal("JPEGXLImageDecoderPlugin: Invalid stride value");

            auto const num = TRY(read_varint(command_stream));
            ByteBuffer bytes;
            TRY(bytes.try_resize(num));
            TRY(data_stream.read_until_filled(bytes));
            if (width == 2 || width == 4)
                TRY(shuffle(bytes, width));

            for (u64 i = 0; i < num; i += width) {
                // NOTE: 0 <= order <= 2
                u8 const N = order + 1;
                Array<u32, 3> prev {};
                for (u8 j = 0; j < N; ++j) {
                    Array<u8, 4> bytes {};
                    for (u8 k = 0; k < width; ++k)
                        bytes[4 - width + k] = out[stride * (j + 1) + k];
                    prev[j] = AK::convert_between_host_and_big_endian(*bit_cast<u32*>(bytes.data()));
                }

                u32 p;
                if (order == 0)
                    p = prev[0];
                else if (order == 1)
                    p = 2 * prev[0] - prev[1];
                else if (order == 2)
                    p = 3 * prev[0] - 3 * prev[1] + prev[2];

                for (u8 j = 0; j < width && i + j < num; ++j) {
                    u8 const val = (bytes[i + j] + (p >> (8 * (width - 1 - j)))) & 255;
                    TRY(out.try_append(val));
                }
            }
        } else if (command == 10) {
            TRY(out.try_append("XYZ "sv.bytes()));
            Array<u8, 4> constexpr zeros {};
            TRY(out.try_append(zeros));
            auto bytes = TRY(out.get_bytes_for_writing(12));
            TRY(data_stream.read_until_filled(bytes));
        } else if (command >= 16 and command < 24) {
            Array constexpr strings = { "XYZ "sv, "desc"sv, "text"sv, "mluc"sv, "para"sv, "curv"sv, "sf32"sv, "gbd "sv };
            TRY(out.try_append(strings[command - 16].bytes()));
            Array<u8, 4> constexpr zeros {};

Review comment (suggestion):
`prev[j] = *bit_cast<BigEndian<u32> const*>(bytes.data());` is what we usually do, i think

Code after:
            if (width == 3 || order == 3)
                return Error::from_string_literal("JPEGXLImageDecoderPlugin: Invalid width or order value");

            u64 stride = width;
            if ((flags & 16) != 0)
                stride = TRY(read_varint(command_stream));

            if (stride * 4 >= out.size() || stride < width)
                return Error::from_string_literal("JPEGXLImageDecoderPlugin: Invalid stride value");

            auto const num = TRY(read_varint(command_stream));
            ByteBuffer bytes;
            TRY(bytes.try_resize(num));
            TRY(data_stream.read_until_filled(bytes));
            if (width == 2 || width == 4)
                TRY(shuffle(bytes, width));

            for (u64 i = 0; i < num; i += width) {
                // NOTE: 0 <= order <= 2
                u8 const N = order + 1;
                Array<u32, 3> prev {};
                for (u8 j = 0; j < N; ++j) {
                    // "read u(width * 8) from the output ICC profile starting from
                    //  (stride * (j + 1)) bytes before the current output size,
                    //  interpreted as a big-endian unsigned integer of width bytes"
                    Array<u8, 4> bytes {};
                    for (u8 k = 0; k < width; ++k)
                        bytes[4 - width + k] = out[out.size() - stride * (j + 1) + k];
                    prev[j] = *bit_cast<BigEndian<u32> const*>(bytes.data());
                }

                u32 p;
                if (order == 0)
                    p = prev[0];
                else if (order == 1)
                    p = 2 * prev[0] - prev[1];
                else if (order == 2)
                    p = 3 * prev[0] - 3 * prev[1] + prev[2];

                for (u8 j = 0; j < width && i + j < num; ++j) {
                    u8 const val = (bytes[i + j] + (p >> (8 * (width - 1 - j)))) & 255;
                    TRY(out.try_append(val));
                }
            }
        } else if (command == 10) {
            TRY(out.try_append("XYZ "sv.bytes()));
            Array<u8, 4> constexpr zeros {};
            TRY(out.try_append(zeros));
            auto bytes = TRY(out.get_bytes_for_writing(12));
            TRY(data_stream.read_until_filled(bytes));
        } else if (command >= 16 and command < 24) {