Re: Creating Gray Scale Image give big leak...
Re: Creating Gray Scale Image give big leak...
- Subject: Re: Creating Gray Scale Image give big leak...
- From: Jerry LeVan <email@hidden>
- Date: Tue, 18 Dec 2007 20:33:32 -0500
On Dec 18, 2007, at 12:20 PM, Heinrich Giesen wrote:
Hi,
On 16.12.2007, at 21:03, Jerry LeVan wrote:
Is there a way to pass an image to a proc that will transform the
image via
core image filters and get a new NSImage back without any leaks?
I do not know why your code leaks, but for me it looks a bit too
complicated.
For creating a gray imageRep I use the following code, which is a
category of NSImageRep
and works without any memory leak (and is fast):
- (NSBitmapImageRep *) grayRepresentation
{
NSSize origSize = [self size];
// create a new representation
NSBitmapImageRep *newRep =
[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:[self pixelsWide]
pixelsHigh:[self pixelsHigh]
bitsPerSample:8
samplesPerPixel:1
hasAlpha:NO // not allowed !
isPlanar:NO
colorSpaceName:NSCalibratedWhiteColorSpace
bytesPerRow:0
bitsPerPixel:0 ];
// this new imagerep has (as default) a resolution of 72 dpi
[NSGraphicsContext saveGraphicsState];
NSGraphicsContext *context = [NSGraphicsContext
graphicsContextWithBitmapImageRep:newRep];
[NSGraphicsContext setCurrentContext:context];
[self drawInRect:NSMakeRect( 0, 0, [newRep pixelsWide], [newRep
pixelsHigh] )];
[NSGraphicsContext restoreGraphicsState];
[newRep setSize:origSize];
return [newRep autorelease];
}
Heinrich
Heinrich thank you for your timely response, my computer memory also
thanks you :)
I am still trying figure out, after a several year layoff, what is
going on in Cocoa.
Part of my original quest was to learn how to apply a core image
filter to an
image and get a new image (with the filter applied) back without
enormous leaks.
My every attempt at the core image solution has failed miserably with
giant leaks,
however your technique is just what I need for my app ( ie produce a
grayscale image).
I have recast your code into a method...
*******************************************
// Returns an autoreleased grayscale version of
// its parameter theImage
- (NSImage *) monochromeImage:(NSImage*) theImage
{
// get NSImage to bitmaprep
NSBitmapImageRep * abitmap;
abitmap = (NSBitmapImageRep*)[theImage bestRepresentationForDevice:nil];
int pw = [abitmap pixelsWide];
int ph = [abitmap pixelsHigh];
NSBitmapImageRep * bitmap = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL pixelsWide:pw
pixelsHigh:ph bitsPerSample:8 samplesPerPixel:1
hasAlpha:NO isPlanar:NO colorSpaceName:NSCalibratedWhiteColorSpace
bytesPerRow:0 bitsPerPixel:0];
[bitmap setSize: [theImage size]];
NSImage * image =[ [NSImage alloc] initWithSize:[theImage size ] ];
NSGraphicsContext *nsContext = [NSGraphicsContext
graphicsContextWithBitmapImageRep:bitmap];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext: nsContext];
// Do I need a lockFocus here?
[ theImage drawAtPoint:NSMakePoint(0,0) fromRect:NSZeroRect
operation:NSCompositeCopy fraction:1.0];
// Restore the previous graphics context and state.
[NSGraphicsContext restoreGraphicsState];
[image addRepresentation: bitmap];
[bitmap release];
return [image autorelease];
}
The only problem that I have is when I rotate an image, say 45 degrees.
I do this by drawing the (rotated) image into a new image that is
large enough
to hold the rotated image.
When the new image is displayed in my NSImageView it looks nice. When
I take
the rotated image and apply the above method the grayscale image draws
correctly
but the bits that are "outside" of the rotated image in the enlarged
image are
rendered as black instead of white.
Thanks,
Jerry
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden