CMYK NSImage get pixel data

921 views Asked by At

I'm loading CMYK jpeg image to NSImage, How can i extract c,m,y,k values for a specific pixel ? How can i get byte array with all the CMYK pixel data.. in RGB images i'm using .bitmapData but it seems for CMYK images it is all 0xff.

I have tried converting the NSImage to RGB color-space but i didn't like the results.. and i actually want the c,m,y,k values and not the equivalent rgb values

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

        NSBitmapImageRep* rep = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];
    bool isCMYK = [rep.colorSpaceName isEqualToString: NSDeviceCMYKColorSpace];
    const unsigned char *bytes = rep.bitmapData;
// all values at bytes are not weird and does not represent CMYK values

p.s i can't use core graphics since this cocoa code is compiled using GNUStep.

1

There are 1 answers

0
IvanAtBest On

I did not test this, but does this code return the right values?

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

NSBitmapImageRep* rep = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];
bool isCMYK = [rep.colorSpaceName isEqualToString: NSDeviceCMYKColorSpace];
if(isCMYK){
    NSColor* aColor = [raw_img colorAtX:0 y:0];
    NSString* theColorSpace = [aColor colorSpaceName];

    if ([theColorSpace isEqualToString: NSDeviceCMYKColorSpace]){
        NSLog(@"C:%f,M%f,Y:%f,K%f",
            aColor.cyanComponent,
            aColor.magentaComponent,
            aColor.yellowComponent,
            aColor.blackComponent);
    }
}

This should be a good starting point if it gives you want you want.