Page 1 of 1

Perl Magick -set colorspace XYZ & -colorspace XYZ equivalents

Posted: 2016-12-02T07:53:08-07:00
by miket
HI,

I'm using PerlMagick to process a large number of large images. The process is broadly as follows:

Pipe converted (high gamut) raw files into and from dcraw.
Convert to "xyY" colorspace.
Process files.
Convert to wide gamut RGB (e.g. PhotoPro).

I eventually discovered that the ->Set(colorspace=>"xyY") command (which is undocumented) works very much like the command line -colorspace xyY.

However, when piping the files from dcraw, PerlMagick always sees these as "sRGB" files, even if I use dcraw to output a wider gamut (or XYZ) format.

Having ducked the wider gamut issue, by importing from dcraw as sRGB then using ->Set(colorspace=>"xyY") has allowed me successfully develop the process workflow.

I now want to address the high gamut input requirement and import from dcraw in XYZ format. Is there an equivalent to the command line -set colorspace XYZ in perl magick which allows you to alter the colorspace tag WITHOUT doing a conversion (the ->Set(colorspace=>???) command assumes sRGB input and always does a conversion from an assumed sRGB colorspace when applied). I've tried a myriad of permutations and combinations - to no avail :(

I'd be grateful if anyone knows the syntax to mimic the -set colorspace command, or for a work around to allow IM to recognise the dcraw output as XYZ format.

(There seem to be a number of unresolved comments on the forum regarding the issue of whether PerlMagic's Set command emulates the command line -Set command or the -define command).

Mike

Version: ImageMagick 7.0.2-4 Q16 x86_64 2016-09-15

Re: Perl Magick -set colorspace XYZ & -colorspace XYZ equivalents

Posted: 2016-12-02T21:45:18-07:00
by snibgo
I don't use Perl, and can't answer your question: what Perl is equivalent to CLI "-set colorspace ZZZ" and "-colorspace ZZZ"?

From the code in Magick.xs SetAttribute(), it seems that image->Set(colorspace=>"ZZZ") is equivalent to "-colorspace ZZZ", not the "-set" version, which is weird.

If you think there is a bug in the Perl documentation or implementation, I suggest you post to the "Bugs" forum. For requested new features, post to the "Developers" forum.

However, I query your reason for wanting this. When dcraw creates an image file in a given colorspace, it encodes the pixel values through a formula, and embeds an ICC profile that enables other software to correctly process those values. So the natural IM command to use isn't "-colorspace" but "-profile".

Perl seems to have a "profile" method.

For example (Windows commands):

Use dcraw to read a NEF file, creating x.tiff encoded as ProPhoto:

Code: Select all

dcraw -v -o 4 -T -O x.tiff in.NEF
Use IM to read that file, increase the red channel by 10%, convert to sRGB, and save as x.png:

Code: Select all

convert x.tiff -channel R -evaluate multiply 1.1 +channel -profile sRGB.icc x.png

Re: Perl Magick -set colorspace XYZ & -colorspace XYZ equivalents

Posted: 2016-12-04T20:18:23-07:00
by miket
Snigbo,

Thanks for your suggestions. I had already tried the -profile option (which does what it says on the tin in PerlMagick). Unfortunately I need to do my processing in "xyY" space and could not find an appropriate .icc profile, so this approach did not work for me :( .

I did however (somewhat reluctantly) bite the bullet and added a new option into PerlMagick. It took a while to realise that the .xc file which needed updating was the one in /PerlMagick/quantum/Q16HDRI.xc

For anybody else who is interested, I inserted code below the following existing code:

Code: Select all

      if (LocaleCompare(attribute,"colorspace") == 0)
        {
          sp=SvPOK(sval) ? ParseCommandOption(MagickColorspaceOptions,
            MagickFalse,SvPV(sval,na)) : SvIV(sval);
          if (sp < 0)
            {
              ThrowPerlException(exception,OptionError,"UnrecognizedColorspace",
                SvPV(sval,na));
              break;
            }
          for ( ; image; image=image->next)
            (void) TransformImageColorspace(image,(ColorspaceType) sp,
              exception);
          break;
        }
And inserted :

Code: Select all

      if (LocaleCompare(attribute,"colorspaceset") == 0)		/* MCT */
        {
          sp=SvPOK(sval) ? ParseCommandOption(MagickColorspaceOptions,
            MagickFalse,SvPV(sval,na)) : SvIV(sval);
          if (sp < 0)
            {
              ThrowPerlException(exception,OptionError,"UnrecognizedColorspace",
                SvPV(sval,na));
              break;
            }
          for ( ; image; image=image->next)
            (void) SetImageColorspace(image,(ColorspaceType) sp,
              exception);
          break;
        }
Compiled and installed using

cd PerlMagick/quantum
sudo perl Makefile.PL
sudo make
sudo make install

This now allows me to use $file->Set('colorspaceset'=>"XYZ") to set the image colorspace without any conversion.

I can then use the original $file->Set('colorspace'=>"xyY") to convert the image as required.

Job's a good 'un :D :D :D

(Perhaps I'll have a go at resolving my Compose:Clamp issue next !

Mike

Re: Perl Magick -set colorspace XYZ & -colorspace XYZ equivalents

Posted: 2016-12-05T02:01:12-07:00
by snibgo
Excellent. (And congratulations on your bravery!) I've suggested to the developers that this is added as a permanent standard feature. See thread viewtopic.php?f=2&t=30994

Re: Perl Magick -set colorspace XYZ & -colorspace XYZ equivalents

Posted: 2016-12-05T07:04:10-07:00
by miket
There is a slight snag with my solution. It seems that when IM is recompiled, the Q16HDRI.xc and (I think) the quantum.xc files are overwritten.

It seems to be necessary to alter the quantum.xc.in file, rather than the Q16HDRI.xc file. This then necessitates the additional step of recompiling/installing IM and subsequently compiling/installing PerlMagic as above.

Re: Perl Magick -set colorspace XYZ & -colorspace XYZ equivalents

Posted: 2016-12-05T07:14:23-07:00
by snibgo
Please see viewtopic.php?f=2&t=30994 with Magick's suggested solution.

Re: Perl Magick -set colorspace XYZ & -colorspace XYZ equivalents

Posted: 2016-12-05T07:29:20-07:00
by miket
Just given that a go, with no luck :( . Have responded on that thread.

Mike