0

I have a CGImageRef in RGB obtained from ZXing barcode library, however, I need this file in CMYK Color Space. I made several attempt to get this using Cocoa, unsuccessfully.

The closest I could get is to get the picture in CMYK, but it lost its transparency during the conversion. The transparency has been converted to white somehow.

Here is the code I used:

NSImage * sourceImage = [[NSImage alloc] initWithContentsOfFile:finalPath.path];
NSBitmapImageRep * sourceImageRep = [[sourceImage representations] objectAtIndex:0];
NSColorSpace * targetColorSpace = [NSColorSpace genericCMYKColorSpace];
NSBitmapImageRep * targetImageRep = [sourceImageRep bitmapImageRepByConvertingToColorSpace:targetColorSpace
                                                                                   renderingIntent:NSColorRenderingIntentPerceptual];
NSData * data = [targetImageRep representationUsingType: NSTIFFFileType properties: nil];
[data writeToFile:finalPath.path atomically: NO];

Result: File converted to CMYK but the barcode lost its transparency.

martinjack
  • 51
  • 6
  • Could the problem be the NSColorRenderingIntentPerceptual? – matt Apr 05 '18 at 17:42
  • I thought the same, but I tried all different options and same result with all. – martinjack Apr 05 '18 at 17:50
  • Oh, well, sorry about that... OK, let me try this — could be problem be the NSTIFFFileType? I've never seen a tiff with transparency. Maybe a PNG would work better? – matt Apr 05 '18 at 18:18
  • Unfortunately, PNG only works with RGB Color Space, not CMYK. – martinjack Apr 05 '18 at 18:20
  • So maybe what you're trying to do is impossible? I know nothing of CMYK (well, what I know I've managed to forget). – matt Apr 05 '18 at 18:22
  • Possible duplicate of [Save NSView representation ignores transparency](https://stackoverflow.com/questions/7604287/save-nsview-representation-ignores-transparency) – Giacomo Catenazzi Apr 06 '18 at 05:42
  • I managed to do it using ImageMagick :) But I wanted to be able to do it directly with Cocoa – martinjack Apr 06 '18 at 09:35
  • In my opinion, converting to CMYK is generally not useful unless you're seeking to send an image directly to a printer -- where transparency will be irrelevant. If the barcode is also in color, there is the additional complication that [conversions to and from CMYK are not trivial](https://stackoverflow.com/questions/14088375/how-can-i-convert-rgb-to-cmyk-and-vice-versa-in-python/53110587#53110587) -- especially as there is "no meaning for CMYK colors that is as ubiquitous as sRGB is for RGB". – Peter O. Nov 11 '18 at 22:40

0 Answers0