Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Difficulty masking an image with another image



HI folks,

I'm just getting my feet wet with Quartz 2D and built a small app to try out some functions. The following code doesn't behave as I expected. Specifically the asterisked line below leaves maskedImage as nil. Either MyImage or aGrayScaleImage will display correctly. Any help appreciated.

Thanks,
Pete

void drawMaskedImage(CGContextRef context, CGRect contextRect)
{
CGImageRef image, mask, maskedImage;
CGDataProviderRef imageProvider, maskProvider;
CFStringRef imagePath, maskPath;
CFURLRef imageUrl, maskUrl;

NSString *k9TeethImage = [[NSBundle mainBundle] pathForImageResource:@"MyImage.png"];
char *imageFileName=[k9TeethImage UTF8String];
NSString *maskImage = [[NSBundle mainBundle] pathForImageResource:@"aGrayScaleImage.png"];
char *maskFileName=[maskImage UTF8String];


imagePath = CFStringCreateWithCString (NULL, imageFileName, kCFStringEncodingUTF8);
imageUrl = CFURLCreateWithFileSystemPath (NULL, imagePath, kCFURLPOSIXPathStyle, NULL);
maskPath = CFStringCreateWithCString (NULL, maskFileName, kCFStringEncodingUTF8);
maskUrl = CFURLCreateWithFileSystemPath (NULL, maskPath, kCFURLPOSIXPathStyle, NULL);

imageProvider = CGDataProviderCreateWithURL (imageUrl);
maskProvider = CGDataProviderCreateWithURL (maskUrl);

image = CGImageCreateWithPNGDataProvider (imageProvider, NULL, true, kCGRenderingIntentDefault);
mask = CGImageCreateWithPNGDataProvider (maskProvider, NULL, true, kCGRenderingIntentDefault);


	CFRelease(imagePath);
	CFRelease(maskPath);
	CFRelease (imageUrl);
	CFRelease (maskUrl);
	CGDataProviderRelease (imageProvider);
	CGDataProviderRelease (maskProvider);
	
	CGRect aRect=CGRectMake (0, 0, 900, 1200);
	CGSize theSize=CGSizeMake (900,1200);

CGLayerRef imageLayerRef=CGLayerCreateWithContext (context, theSize, NULL);
CGContextRef layerContext=CGLayerGetContext (imageLayerRef);
// Create the masked image
maskedImage=CGImageCreateWithMask(image, mask); // ****************************************
// Draw it to the layer
CGContextDrawImage(layerContext,aRect,maskedImage);
// Then draw the layer to the window graphics context
CGContextDrawLayerInRect(context, contextRect, imageLayerRef);
}


- (void)drawRect:(NSRect)rect
{
float a,b,c,d;
a=rect.origin.x;
b=rect.origin.y;
c=rect.size.width;
d=rect.size.height;
CGRect theRect=CGRectMake(a,b,c,d);
CGContextRef theContext=[[NSGraphicsContext currentContext] graphicsPort];
drawMaskedImage(theContext,theRect);
}


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Quartz-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/quartz-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.