Page 1 of 1

possible bug PHP JPG CMYK to RGB conversion

Posted: 2013-10-03T01:08:37-07:00
by piotrk
hi

I have found possible bug in ImageMagick 6.8.6-3 2013-09-09 Q16 PHP version

most of the files (especially PSD files) convert without any problems but there's sometimes problem with JPG files
(original CMYK on the left and RGB after conversion on the right):
Image

conversion PHP script

Code: Select all

  $image = new Imagick('test file.jpg');
  if ($image->getImageColorspace() == Imagick::COLORSPACE_CMYK) {
    echo "converting from CMYK / <br>";
    $profiles = $image->getImageProfiles('*', false);
    // we're only interested if ICC profile(s) exist
    $has_icc_profile = (array_search('icc', $profiles) !== false);
    // if it doesnt have a CMYK ICC profile, we add one
    if ($has_icc_profile === false) {
      $icc_cmyk = file_get_contents('USWebUncoated.icc');
      if ($image->profileImage('icc', $icc_cmyk)) { echo "cmyk profile added <br>"; }
      //$image->writeImage("test file cmyk2.jpg");
      unset($icc_cmyk);
    }
    // then we add an RGB profile
    //$icc_rgb = file_get_contents('sRGB_v4_ICC_preference.icc');
    $icc_rgb = file_get_contents('AdobeRGB1998.icc');
    if ($image->profileImage('icc', $icc_rgb)) { echo "rgb profile added <br>"; }
    if ($image->setImageColorSpace(Imagick::COLORSPACE_RGB)) { echo "colorspace set to rgb <br>"; }
    unset($icc_rgb);

    $php_vs_arr = preg_split("/\./", phpversion());
    $php_vs = $php_vs_arr[0] . '.' . $php_vs_arr[1];
    //fix gamma, hue, saturation, brightness
    if($php_vs < 5.3) {
      echo "<5.3";
      //ADJUST GAMMA BY 20% for 5.2.x
      $range = $image->getQuantumRange();
      $image->levelImage(0, 2.0, $range['quantumRangeString']);
    } else {
      echo ">5.3";
      //php 5.3 hack FOR INVERTED COLORS
      $image->negateImage(false, Imagick::CHANNEL_ALL);
    }
  }
  $image->writeImage("test file rgb.jpg");
the file with problems for testing can be dowloaded from: http://tulipano.pl/imagicktest.jpg

Re: possible bug PHP JPG CMYK to RGB conversion

Posted: 2013-10-03T01:59:31-07:00
by snibgo
Why are you using AdobeRGB colorspace? Are you also doing a gamma or other shift?

When I convert (v6.8.7-0) your original to sRGB.icc or sRGB_v4_preference.icc or Adobe1998.icc, and do a flicker test against the original, using Microsoft Photo Viewer, I can't see much if any difference.

Re: possible bug PHP JPG CMYK to RGB conversion

Posted: 2013-10-03T03:26:16-07:00
by piotrk
I have tried different color spaces
with the recommended "sRGB_v4_ICC_preference.icc" the result looks the same for me - i.e. wrong - the soup on the photo has a strong light magenta cast

Re: possible bug PHP JPG CMYK to RGB conversion

Posted: 2013-10-03T03:36:15-07:00
by snibgo
What software are you using for the comparison? Don't forget that your software will need to convert one or both images to sRGB.

Are you also doing a gamma or other shift?

Can you post up your converted image?

Re: possible bug PHP JPG CMYK to RGB conversion

Posted: 2013-10-03T04:01:38-07:00
by piotrk
here are the images:

original but scaled down CMYK:
Image

the same convertet to RGB (notice the negative):
Image

the same after color inversion:
Image

I'm working on php>5.3 so as you can see in the script I'm not doing any gamma or other shift - just the "negateImage" - this is the original script that created the above images

Re: possible bug PHP JPG CMYK to RGB conversion

Posted: 2013-10-03T04:20:49-07:00
by snibgo
There may be a slight colour cast but far worse is the lightening of the dark areas.

Something may be wrong with your PHP code, but I'm not an expert. I don't know why you need to negate. I am just using:

Code: Select all

convert imagicktest.jpg -profile sRGB_v4_icc_preference.icc i.jpg
You might try that, both at the command line and in PHP. For the PHP, just put it in an "exec()".

Re: possible bug PHP JPG CMYK to RGB conversion

Posted: 2013-10-03T04:29:58-07:00
by piotrk
from what I have seen on the web the nagation is a "normal" thing

as I have said the whole conversion process works for some files just fine, but for some not

unfortunatelly I cannot run exec() on this server even to check for me if the conversion is working ok for this file :(

Re: possible bug PHP JPG CMYK to RGB conversion

Posted: 2013-10-03T04:40:10-07:00
by snibgo
Your code includes:

Code: Select all

if ($image->setImageColorSpace(Imagick::COLORSPACE_RGB)) { echo "colorspace set to rgb <br>"; }
I don't know what this does.

Re: possible bug PHP JPG CMYK to RGB conversion

Posted: 2014-09-29T13:36:25-07:00
by thuddle
We are experiencing a similar issue after Amazon AMI update
ImageMagick 6.7.8-9 2014-05-12 Q16
imagick 3.1.2
PHP 5.3.29 (cli) (built: Aug 20 2014 16:41:38)
The following code used to work now image rather than being yellow flowers are blue flowers

# if CMYK attempt to convert to RGB.
if ($image->getImageColorspace() == Imagick::COLORSPACE_CMYK) {
$profiles = $image->getImageProfiles('*', false);
# we're only interested if ICC profile(s) exist
$has_icc_profile = (array_search('icc', $profiles) !== false);
# if it doesnt have a CMYK ICC profile, we add one
if ($has_icc_profile === false) {
$icc_cmyk = file_get_contents(dirname(__FILE__).'/USWebUncoated.icc');
$image->profileImage('icc', $icc_cmyk);
#$image->setImageProfile('icc', $icc_cmyk);
unset($icc_cmyk);
}
# then we add an RGB profile
$icc_rgb = file_get_contents(dirname(__FILE__).'/sRGB_v4_ICC_preference.icc');
$image->profileImage('icc', $icc_rgb);
#$image->setImageProfile('icc', $icc_rgb);
unset($icc_rgb);
$image->setImageColorSpace(Imagick::COLORSPACE_SRGB);
}

Re: possible bug PHP JPG CMYK to RGB conversion

Posted: 2014-09-29T14:10:32-07:00
by thuddle
Just found this. See comment from 5 months ago.

http://php.net/manual/en/imagick.profileimage.php

I have checked before and after Amazon AMI upgrade. Before lcms is present. After it is not.