• 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
Converting a CMYK NSImage to RGB
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Converting a CMYK NSImage to RGB


  • Subject: Converting a CMYK NSImage to RGB
  • From: Ron Aldrich <email@hidden>
  • Date: Thu, 12 Feb 2009 17:52:39 -0800

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


  • Follow-Ups:
    • Re: Converting a CMYK NSImage to RGB
      • From: Ken Ferry <email@hidden>
  • Prev by Date: Re: Communication between objects
  • Next by Date: CALayer wierdness
  • Previous by thread: Re: Proper NSOperation isCancelled handling
  • Next by thread: Re: Converting a CMYK NSImage to RGB
  • Index(es):
    • Date
    • Thread