In QuickDraw, masking can be accomplished using bitmaps that to determine how color information is copied from the pixels in a source image to the corresponding pixels in a destination image. Masks are passed to the QuickDraw functions CopyMask and CopyDeepMask in the maskBits parameter. Masks can have a depth of up to 8 bits per component.
QuickDraw uses the following compositing formula to compute the contribution of each color component in the source and destination pixels:
(1 – mask) x source + (mask) x destination
In this formula, the mask values are normalized to range from 0 through 1. High mask values reduce the contribution of source pixels—in effect, the mask contains “inverse alpha” information with respect to the source bitmap.
Quartz supports two kinds of masks:
An image mask. This is a specialized image (CGImageRef), created by calling the function CGImageMaskCreate, that contains only inverse alpha information. Image masks can have a depth of up to 8 bits per pixel. Quartz image masks are a direct analogue of QuickDraw masks; the same compositing formula is used to apply mask values to source and destination color values, but on a per pixel basis:
(1 – mask) x source + (mask) x destination
For more information about image masks, see Bitmap Images and Image Masks in Quartz 2D Programming Guide.
A masking image. In Mac OS X v10.4 and later, you can mask an image with another image by calling the function CGImageCreateWithMask and supplying an image as the mask parameter rather than an image mask. Use an image as a mask when you want to achieve an effect opposite of what you get when you mask an image with an image mask. Source samples of an image that is used as a mask operate as alpha values. An image sample value (S):
Equal to 1 paints the corresponding image sample at full coverage.
Equal to 0 blocks painting the corresponding image sample.
Greater than 0 and less than 1 allows painting the corresponding image sample with an alpha value of S.
Starting in Mac OS X v10.4, you can use the function CGImageCreateWithMask to mask an image with either an image mask or an image. The function CGImageCreateWithMaskingColors is used for chroma key masking. Masks can also be intersected with the current clipping area in a graphics context using the function CGContextClipToMask.
Replacing Mask Regions
Relevant Resources
Last updated: 2006-09-05