RGBA Multiplication with Two CGImageRefs

I would like to multiply r, g, b and the values ​​of two CGImages, use one image as a mask (in its alpha channel) and tint (in rgb channels). I first tried simply:

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

However, these are only shades and are not masked. So I extracted the alpha from the masking image into a CGImage grayscale mask image (using CGDataProvider) and used this as a mask in context:

// "mask" is "other" 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());
However, it still doesn't look right. The image is getting lighter; if the multiplied image should be at least darker? Image Preview at:

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

Thanks in advance for the enlightenment :)

+3
1

-, - . kCGBlendModeDestinationIn. , kCGBlendModeDestinationOut.

, , Multiply, Destination In Out.

+2

Source: https://habr.com/ru/post/1765064/


All Articles