Re: Converting a CMYK NSImage to RGB
Re: Converting a CMYK NSImage to RGB
- Subject: Re: Converting a CMYK NSImage to RGB
- From: Ken Ferry <email@hidden>
- Date: Fri, 13 Feb 2009 13:41:40 -0800
Hi Ron,
It was the -[NSImage lockFocus]/-[NSImage unlockFocus] that did it. You
should just take them out, they weren't relevant to what you were trying to
do.
-[NSImage lockFocus] sets the receiver up as a drawing _destination_. You
aren't trying to draw into the image here, you're trying to draw _from_ it.
lockFocus will
(1) Create a buffer.
(2) Make the buffer the current drawing graphics context, so that methods
like NSRectFill are directed to the buffer.
(3) Draw the existing contents of the image into the new buffer.
unlockFocus will
(4) pop the current graphics context back to whatever it was before
lockFocus was called.
(5) take the contents of the buffer and make them the new contents of the
image.
In particular, lockFocus/unlockFocus is necessarily destructive. The old
image representations are discarded, replaced with a new one.
-Ken
On Thu, Feb 12, 2009 at 5:52 PM, Ron Aldrich <email@hidden> wrote:
> Folks,
>
> I'm having an issue where CMYK images aren't being recognized by my quartz
> compositions, so I need to convert them to RGB for display.
>
> I've written a routine to do it, but its causing problems.
>
> NSImageRep* theImageRep = [inputImage bestRepresentationForDevice:
> [NSDictionary dictionaryWithObject: NSDeviceRGBColorSpace
>
> forKey: NSDeviceColorSpaceName]];
>
> NSString* theColorSpace = [theImageRep colorSpaceName];
>
> if ([theColorSpace isEqualToString: NSDeviceCMYKColorSpace])
> {
> NSBitmapImageRep* theRGBImageRep = [[[NSBitmapImageRep alloc]
> initWithBitmapDataPlanes: NULL
>
> pixelsWide: [theImageRep pixelsWide]
>
> pixelsHigh: [theImageRep pixelsHigh]
>
> bitsPerSample: 8
>
> samplesPerPixel: 4
>
> hasAlpha: YES
>
> isPlanar: NO
>
> colorSpaceName: NSDeviceRGBColorSpace
>
> bytesPerRow: 0
>
> bitsPerPixel: 0] autorelease];
>
> [inputImage lockFocus];
>
> NSGraphicsContext* theContext = [NSGraphicsContext
> graphicsContextWithBitmapImageRep: theRGBImageRep];
> [NSGraphicsContext saveGraphicsState];
> [NSGraphicsContext setCurrentContext: theContext];
>
> NSRect theBoundsRect = NSMakeRect(0.0, 0.0, [theRGBImageRep pixelsWide],
> [theRGBImageRep pixelsHigh]);
>
> [[NSColor clearColor] set];
> NSRectFill(theBoundsRect);
>
> [theImageRep drawInRect: theBoundsRect];
>
> [NSGraphicsContext restoreGraphicsState];
>
> [inputImage unlockFocus];
>
> ASSIGNOBJECT(rgbImage, [[[NSImage alloc] initWithSize: [theImageRep
> size]] autorelease]);
>
> [rgbImage addRepresentation: theRGBImageRep];
> }
> else
> {
> ASSIGNOBJECT(rgbImage, inputImage);
> }
>
> The resulting image is correct, but somewhere along the line, my original
> image (inputImage) is being modified - the CMYK representation is being
> removed, and replaced with a downsampled RGB representation. The goal here
> was to leave inputImage unmodified, so that when it is finally sent to the
> printer, the CMYK representation would be used.
>
> inputImage before creating the RGB version.
>
> NSImage 0x12313dd0 Size={340.08, 340.08} Reps=(
> NSBitmapImageRep 0xd8201c0 Size={340.08, 340.08}
> ColorSpace=NSDeviceCMYKColorSpace BPS=8 BPP=32 Pixels=1417x1417 Alpha=NO
> Planar=NO Format=0
> )
>
> inputImage after creating the RGB version.
>
> NSImage 0x12313dd0 Size={340.08, 340.08} Reps=(
> NSCachedImageRep 0xd824530 Size={340, 340}
> ColorSpace=NSCalibratedRGBColorSpace BPS=8 Pixels=340x340 Alpha=NO
> )
>
> Clearly, I'm doing something wrong. I added calls to [inputImage
> lockFocus] and [inputImage unlockFocus] in order to resolve a multi-thread
> problem, but that seems to have caused this problem.
>
> So, what's the best way for me to convert an NSImage of unknown pedigree to
> a simple RGB representation?
>
> Thanks,
>
> Ron Aldrich
> Software Architects, Inc.
>
> _______________________________________________
>
> 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
>
_______________________________________________
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