将NSImage从CMYK转换为RGB

时间:2013-01-23 18:16:24

标签: ios objective-c macos nsimage nsbitmapimagerep

经过一整天的搜索,我发现了一些类似的答案,但似乎没有什么对我有用。

我已经构建了一个在OSX中使用的应用程序,它从用户输入的目录中获取图像列表,通过重命名过程运行,转换为JPG,希望在您的帮助下,转换为RGB颜色。 / p>

NSImage *image = [[NSImage alloc] initWithContentsOfFile:from];

NSInteger imageWidth = image.size.width * 4.16666666667;
NSInteger imageHeight = image.size.height * 4.16666666667;

NSBitmapImageRep* theRGBImageRep = [[NSBitmapImageRep alloc]
    initWithBitmapDataPlanes:NULL pixelsWide:imageWidth pixelsHigh:imageHeight
        bitsPerSample: 8 samplesPerPixel: 4 hasAlpha: YES isPlanar: NO
    colorSpaceName: NSCalibratedRGBColorSpace bytesPerRow: 0 bitsPerPixel: 0];

NSGraphicsContext* theContext = [NSGraphicsContext
                    graphicsContextWithBitmapImageRep: theRGBImageRep];

NSImageRep* theImageRep = [image bestRepresentationForRect:
   NSMakeRect(0.0, 0.0, imageWidth, imageHeight) context:theContext
     hints:[NSDictionary dictionaryWithObject:NSDeviceRGBColorSpace
                     forKey:NSDeviceColorSpaceName]];

[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];;
[image addRepresentation: theRGBImageRep];

NSDictionary *imageProps = [NSDictionary dictionaryWithObjectsAndKeys:
        [NSNumber numberWithFloat:1.0],NSImageCompressionFactor, nil];
NSData *bitmapData = [theRGBImageRep representationUsingType:NSJPEGFileType
                   properties:imageProps];

if ([bitmapData writeToFile:toJPG atomically:YES] == YES){
    feedback = [NSString stringWithFormat:@"%@ -> %@\n", string, jpgFileName];
    [readOut insertText:feedback];
    [readOut display];
} else {
    feedback = [NSString stringWithFormat:@"%@ - FAILED\n", string];
    [errorLog insertText:feedback];
    [errorLog display];
}

图像在指定文件夹中以JPEG格式正确输出,但始终保持CMYK。还有其他人遇到过这个吗?

编辑显示已淘汰的结果:

更新的代码(这是目前正在提供的结果)

1 个答案:

答案 0 :(得分:1)

试试这个

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];


  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];


ASSIGNOBJECT(rgbImage, [[[NSImage alloc] initWithSize: [theImageRepsize]] autorelease]);

[rgbImage addRepresentation: theRGBImageRep];
}
else
{
ASSIGNOBJECT(rgbImage, inputImage);
 }