• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Masking an NSImage
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Masking an NSImage
      • From: Todd Yandell <email@hidden>
References: 
 >Masking an NSImage (From: Todd Yandell <email@hidden>)

  • Prev by Date: Re: Self-scheduling threading
  • Next by Date: Re: Self-scheduling threading
  • Previous by thread: Masking an NSImage
  • Next by thread: Re: Masking an NSImage
  • Index(es):
    • Date
    • Thread