【问题标题】:Multiplying RGBA with two CGImageRefs将 RGBA 与两个 CGImageRefs 相乘
【发布时间】:2010-09-16 09:38:34
【问题描述】:

我想将两个 CGImage 的 r、g、b 和 a 值相乘,以使用单个图像作为遮罩(在其 alpha 通道中)和色调(在 rgb 通道中)。我第一次尝试很简单:

CGContextDrawImage(context, CGRectMake(0, 0, _w, _h), handle());
CGContextSetBlendMode(context, kCGBlendModeMultiply);
CGContextDrawImage(context, CGRectMake(0, 0, other->width(), other->height()), over);

但是,这只会着色,不会遮盖。因此,我将遮罩图像中的 alpha 提取到 CGImage 遮罩灰度图像中(使用 CGDataProvider)并将其用作上下文中的遮罩:

// "mask" is "other"'s alpha channel as an inverted grayscale image
CGContextSaveGState(context);
CGContextClipToMask(context, CGRectMake(0, 0, other->width(), other->height()), mask);
CGImageRelease(mask);
CGContextDrawImage(context, CGRectMake(0, 0, _w, _h), handle());
CGContextRestoreGState(context); // Don't apply the clip mask to 'other', since it already has it
CGContextSetBlendMode(context, kCGBlendModeMultiply);
CGContextDrawImage(context, CGRectMake(0, 0, other->width(), other->height()), other->handle());

但它仍然看起来不正确。图像变得更亮;相乘的图像是否应该总是至少更暗?图片预览:

http://dl.dropbox.com/u/6775/multiply/index.html

提前感谢您的启发:)

【问题讨论】:

    标签: core-graphics cgcontext cgimage masking


    【解决方案1】:

    第一件事是您不需要将 Alpha 通道提取到单独的通道来进行遮罩。您可以使用kCGBlendModeDestinationIn 绘制它。对于倒置掩码,同样的事情,但使用kCGBlendModeDestinationOut

    因此,解决方案是使用 Multiply 绘制第二张图像,然后使用 Destination In 或 Out 再次绘制它。

    【讨论】:

    • 这是一个 优秀 解决方案,但我才意识到为什么我从未考虑过它:这个应用程序必须支持 10.4 :( (不过我会将其标记为正确,因为它对于所陈述的问题是正确的
    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    相关资源
    最近更新 更多