Search code examples
delphijpegdelphi-xecmyk

Delphi XE and JPG with CMYK colorspace (detect and/or convert)


Short version: Is there any simple way how to find out if the JPG file is CMYK or RGB in Delphi XE? Preferably without any additional libraries...

Longer version: I have a 3rd party library that accepts JPEG. But for whatever reason the library needs to know whether they are in CMYK or RGB colorspace. And what is worse it renders the CMYK ones with wrong colors (probably something with CMS or so...). Delphi itself (TJPEGImage) displays the CMYK image correctly but it seems that all the "CMYK vs RGB stuff" is hidden in jpeg.pas implementation section and is not published...

I searched a lot of stuff on the Internet but here I found only few years old posts. Also I've found Gabriel Corneanu's modified jpeg.dcu but it is all about making Delphi load the image correctly. And it seems that XE does load the CMYK JPEGs correctly.

In the end I would prefer to convert the CMYK image to RGB and even as it sounds horrible I do not mind doing it by recoding the JPEG (if there is no other better option). Basically load it to bitmap and save the bitmap as JPEG if needed...

P.S. I would like to avoid bringing another libraries to my project as much as possible and I would like to stay within "standard Delphi" as much as possible so if I ever need to move this project to Mac, iOS or Android I do not have to deal with a lot of external stuff...


Solution

  • We've narrowed the solutions to three:

    • Parse the JPEG ourselves - this seems to be risky
    • Slightly modify Delphi's jpeg.pas (change the name of the class and expose the CMYK info) - this seems to be viable option but we are not sure if it is copyright OK and as jpeg.pas heavily uses a lot of OBJs this could prove unusable on other platforms in the future
    • Use 3rd party library, we've found NativeJPG http://www.simdesign.nl/nativejpg.html and it seems to do the job quite well and it should be written in pure Pascal so we think that there is quite a good chance it will be easy to port to other platfroms if needed.