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

Code before:

static void ycck_to_cmyk(Vector<Macroblock>& macroblocks)
{
    // 7 - Conversions between colour encodings
    // YCCK is obtained from CMYK by converting the CMY channels to YCC channel.

    // To convert back into RGB, we only need the 3 first components, which are baseline YCbCr
    ycbcr_to_rgb(macroblocks);

    // RGB to CMY, as mentioned in https://www.smcm.iqfr.csic.es/docs/intel/ipp/ipp_manual/IPPI/ippi_ch15/functn_YCCKToCMYK_JPEG.htm#functn_YCCKToCMYK_JPEG
    for (auto& macroblock : macroblocks) {
        for (u8 i = 0; i < 64; ++i) {
            macroblock.r[i] = 255 - macroblock.r[i];
            macroblock.g[i] = 255 - macroblock.g[i];
            macroblock.b[i] = 255 - macroblock.b[i];
        }
    }
}

static void grayscale_to_rgb(Vector<Macroblock>& macroblocks)
{
    for (auto& macroblock : macroblocks) {
        for (u8 i = 0; i < 64; ++i) {
            macroblock.g[i] = macroblock.y[i];
            macroblock.b[i] = macroblock.y[i];
        }
    }
}
static ErrorOr<void> handle_color_transform(JPEGLoadingContext const& context, Vector<Macroblock>& macroblocks)
{
    // Note: This is non-standard but some encoder still add the App14 segment for grayscale images.
    //       So let's ignore the color transform value if we only have one component.
    if (context.color_transform.has_value() && context.components.size() != 1) {
        // https://www.itu.int/rec/dologin_pub.asp?lang=e&id=T-REC-T.872-201206-I!!PDF-E&type=items
        // 6.5.3 - APP14 marker segment for colour encoding

        switch (*context.color_transform) {
        case ColorTransform::CmykOrRgb:
            if (context.components.size() == 4) {
                // Nothing to do here.
            } else if (context.components.size() == 3) {
                // Note: components.size() == 3 means that we have an RGB image, so no color transformation is needed.
            } else {
                return Error::from_string_literal("Wrong number of components for CMYK or RGB, aborting.");
            }
            break;
        case ColorTransform::YCbCr:
            ycbcr_to_rgb(macroblocks);
            break;
        case ColorTransform::YCCK:
            ycck_to_cmyk(macroblocks);

Review comment (suggestion):
Can you also check how this compares to an equivalent
```cpp
memcpy(&macroblock.g, &macroblock.y, sizeof(macroblock.y);
memcpy(&macroblock.b, &macroblock.y, sizeof(macroblock.y);
``` 
Also maybe add a note that `r` and `y` name the same thing

Code after:
        ReadonlySpan<i16>(macroblock.r).copy_to(macroblock.g);
        ReadonlySpan<i16>(macroblock.r).copy_to(macroblock.b);
    }
}

static ErrorOr<void> handle_color_transform(JPEGLoadingContext const& context, Vector<Macroblock>& macroblocks)
{
    // Note: This is non-standard but some encoder still add the App14 segment for grayscale images.
    //       So let's ignore the color transform value if we only have one component.
    if (context.color_transform.has_value() && context.components.size() != 1) {
        // https://www.itu.int/rec/dologin_pub.asp?lang=e&id=T-REC-T.872-201206-I!!PDF-E&type=items
        // 6.5.3 - APP14 marker segment for colour encoding

        switch (*context.color_transform) {
        case ColorTransform::CmykOrRgb:
            if (context.components.size() == 4) {
                // Nothing to do here.
            } else if (context.components.size() == 3) {
                // Note: components.size() == 3 means that we have an RGB image, so no color transformation is needed.
            } else {
                return Error::from_string_literal("Wrong number of components for CMYK or RGB, aborting.");
            }
            break;
        case ColorTransform::YCbCr:
            ycbcr_to_rgb(macroblocks);
            break;
        case ColorTransform::YCCK:
            ycck_to_cmyk(macroblocks);
            break;
        }

        return {};
    }

    // No App14 segment is present, assuming :
    //      - 1 components means grayscale
    //      - 3 components means YCbCr
    //      - 4 components means CMYK (Nothing to do here).
    if (context.components.size() == 3)
        ycbcr_to_rgb(macroblocks);

    if (context.components.size() == 1)
        grayscale_to_rgb(macroblocks);

    return {};
}

static ErrorOr<void> compose_bitmap(JPEGLoadingContext& context, Vector<Macroblock> const& macroblocks)
{
    context.bitmap = TRY(Bitmap::create(BitmapFormat::BGRx8888, { context.frame.width, context.frame.height }));