Repository: LadybirdBrowser/ladybird
File: Libraries/LibGfx/Bitmap.cpp (C++)

Code before:
            result.maximum_green_error = max(result.maximum_green_error, green_error);
            result.maximum_blue_error = max(result.maximum_blue_error, blue_error);
            result.maximum_alpha_error = max(result.maximum_alpha_error, alpha_error);
        }
    }

    result.identical = result.pixel_error_count == 0;
    result.total_error = result.total_red_error + result.total_green_error + result.total_blue_error + result.total_alpha_error;

    u8 maximum_red_green_error = max(result.maximum_red_error, result.maximum_green_error);
    u8 maximum_blue_alpha_error = max(result.maximum_blue_error, result.maximum_alpha_error);
    result.maximum_error = max(maximum_red_green_error, maximum_blue_alpha_error);

    return result;
}

void Bitmap::set_alpha_type_destructive(AlphaType alpha_type)
{
    if (alpha_type == m_alpha_type)
        return;

    if (m_format == BitmapFormat::BGRx8888 || m_format == BitmapFormat::RGBx8888)
        return;

#ifdef AK_OS_MACOS
    vImage_Buffer buf { .data = m_data, .height = vImagePixelCount(height()), .width = vImagePixelCount(width()), .rowBytes = pitch() };
    vImage_Error err;
    if (m_alpha_type == AlphaType::Unpremultiplied) {
        switch (m_format) {
        case BitmapFormat::BGRA8888:
            err = vImagePremultiplyData_BGRA8888(&buf, &buf, kvImageNoFlags);
            break;
        case BitmapFormat::RGBA8888:
            err = vImagePremultiplyData_RGBA8888(&buf, &buf, kvImageNoFlags);
            break;
        default:
            VERIFY_NOT_REACHED();
        }
    } else {
        switch (m_format) {
        case BitmapFormat::BGRA8888:
            err = vImageUnpremultiplyData_BGRA8888(&buf, &buf, kvImageNoFlags);
            break;
        case BitmapFormat::RGBA8888:
            err = vImageUnpremultiplyData_RGBA8888(&buf, &buf, kvImageNoFlags);
            break;
        default:
            VERIFY_NOT_REACHED();
        }
    }
    VERIFY(err == kvImageNoError);

Review comment (suggestion):
Maybe this should still set `m_alpha_type` to the requested value, technically we should always pass along the right Skia alpha type based on the format but better to be safe than sorry?

Tangent: maybe "opaque" would be a nice third alpha type to have here, Skia probably has the right idea.

Code after:

            result.maximum_red_error = max(result.maximum_red_error, red_error);
            result.maximum_green_error = max(result.maximum_green_error, green_error);
            result.maximum_blue_error = max(result.maximum_blue_error, blue_error);
            result.maximum_alpha_error = max(result.maximum_alpha_error, alpha_error);
        }
    }

    result.identical = result.pixel_error_count == 0;
    result.total_error = result.total_red_error + result.total_green_error + result.total_blue_error + result.total_alpha_error;

    u8 maximum_red_green_error = max(result.maximum_red_error, result.maximum_green_error);
    u8 maximum_blue_alpha_error = max(result.maximum_blue_error, result.maximum_alpha_error);
    result.maximum_error = max(maximum_red_green_error, maximum_blue_alpha_error);

    return result;
}

void Bitmap::set_alpha_type_destructive(AlphaType alpha_type)
{
    if (alpha_type == m_alpha_type)
        return;

    if (m_format == BitmapFormat::BGRx8888 || m_format == BitmapFormat::RGBx8888) {
        m_alpha_type = alpha_type;
        return;
    }

#ifdef AK_OS_MACOS
    vImage_Buffer buf { .data = m_data, .height = vImagePixelCount(height()), .width = vImagePixelCount(width()), .rowBytes = pitch() };
    vImage_Error err;
    if (m_alpha_type == AlphaType::Unpremultiplied) {
        switch (m_format) {
        case BitmapFormat::BGRA8888:
            err = vImagePremultiplyData_BGRA8888(&buf, &buf, kvImageNoFlags);
            break;
        case BitmapFormat::RGBA8888:
            err = vImagePremultiplyData_RGBA8888(&buf, &buf, kvImageNoFlags);
            break;
        default:
            VERIFY_NOT_REACHED();
        }
    } else {
        switch (m_format) {
        case BitmapFormat::BGRA8888:
            err = vImageUnpremultiplyData_BGRA8888(&buf, &buf, kvImageNoFlags);
            break;
        case BitmapFormat::RGBA8888:
            err = vImageUnpremultiplyData_RGBA8888(&buf, &buf, kvImageNoFlags);
            break;
        default: