Core Graphics: creating an image mask from a PDF?


Jim
 

On Dec 14, 2020, at 8:17 PM, Graham Cox <graham@...> wrote:

I am using Core Graphics. I have an image as a PDF resource that I want to use as a mask - specifically, the PDF only contains black and white graphics as vectors. I have code that can turn that into a CGImageRef as a normal bitmap (to any desired size I want, which is why I start with a PDF file), but the bitmap image I end up with is, of course, black and white. I actually want to draw that in any colour I choose by using a mask image and a fill colour.

Core Graphics has a function CGImageMaskCreate(), which takes a bunch of the usual parameters but only works with a data provider. There is no PDF data provider (AFAIK).

The bitmap image code works by creating a CGBitmapImageContext, drawing the PDF into it, then using CGBitmapContextImageCreate() to obtain the bitmap. But there’s no mask equivalent of this approach. There is also no function to create a mask image from a bitmap image.

Can anyone think of a solution here? Either to create a mask image from a PDF, or a way to draw a black and white bitmap colourised at will. The latter may be possible using CGImageCreateWithMaskingColors(), but the documentation isn’t very clear and the Quartz drawing guide seems to suggest it’s more for background removal than what I need here. If anyone has an example of how to use it (if it’s even appropriate here) that would be great too.
I’ve used CoreImage (CIBlendWithMask filter) to use a mask.

https://developer.apple.com/library/archive/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/filter/ci/CIBlendWithMask

CoreImage makes it easy to create solid-color images as well.

CIImage *bkgImage = [CIImage imageWithColor:[CIColor colorWithRed:0 green:0 blue:0 alpha:0]];

Alternatively, if you want to stick with the CoreGraphics functions, you can use ImageIO to create a data output of your CGImageRef bitmap that you should be able to feed into CGImageMaskCreate().

Jim Crate


Graham Cox
 

Hi all,

I am using Core Graphics. I have an image as a PDF resource that I want to use as a mask - specifically, the PDF only contains black and white graphics as vectors. I have code that can turn that into a CGImageRef as a normal bitmap (to any desired size I want, which is why I start with a PDF file), but the bitmap image I end up with is, of course, black and white. I actually want to draw that in any colour I choose by using a mask image and a fill colour.

Core Graphics has a function CGImageMaskCreate(), which takes a bunch of the usual parameters but only works with a data provider. There is no PDF data provider (AFAIK).

The bitmap image code works by creating a CGBitmapImageContext, drawing the PDF into it, then using CGBitmapContextImageCreate() to obtain the bitmap. But there’s no mask equivalent of this approach. There is also no function to create a mask image from a bitmap image.

Can anyone think of a solution here? Either to create a mask image from a PDF, or a way to draw a black and white bitmap colourised at will. The latter may be possible using CGImageCreateWithMaskingColors(), but the documentation isn’t very clear and the Quartz drawing guide seems to suggest it’s more for background removal than what I need here. If anyone has an example of how to use it (if it’s even appropriate here) that would be great too.


—Graham