Re: Masking an NSImage
Re: Masking an NSImage
- Subject: Re: Masking an NSImage
- From: Ricky Sharp <email@hidden>
- Date: Thu, 17 Mar 2005 18:27:24 -0600
On Mar 17, 2005, at 5:07 PM, Todd Yandell wrote:
I'm looking for a way to mask an NSImage using another NSImage.
Normally, I would just use Quartz's compositing modes to do this, but
for that to work, one of the images has to have an alpha channel. What
I'm trying to do is this: take one image (say, a normal, mask-less
JPEG) and mask off parts of that image using another black-and-white
mask image. In short, I want to add an alpha channel to an image that
doesn't already have one. Is there any way to do this in Cocoa/Quartz?
Assuming that both your source and mask images will always have an
NSBitmapImageRep as their best representation, I would create a
resultant image that would have both your image data (from the source
image) and alpha (from the mask image).
Look into creating a NSBitmapImageRep and initializing it with
initWithBitmapDataPlanes:pixelsWide:pixelsHigh:bitsPerSample:
samplesPerPixel: etc. etc.
Obtain this new image rep's bitmap data using bitmapData.
Obtain the image data from your source and mask images as follows:
NSImage* theSourceImage = ...
NSImageRep* theSourceImageRep = [theSourceImage
bestRepresentationForDevice:nil];
if ([theSourceImageRep isKindOfClass:[NSBitmapImageRep class])
{
unsigned char* sourceImageData = [theSourceImageRep bitmapData]
...
}
Similar block to get mask's data.
Then iterate through the source and mask data and effectively merge
them together (note that you may or may not have to premultiply the
alpha). Put the result into the resultant bitmap data.
Finally, add the NSBitmapImageRep to a new NSImage.
Or directly use Quartz. The APIs CGImageCreate and CGImageMaskCreate
come to mind.
___________________________________________________________
Ricky A. Sharp mailto:email@hidden
Instant Interactive(tm) http://www.instantinteractive.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden