-colorspace removes ICC profile in Gray TIFF output file

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
datro
Posts: 15
Joined: 2018-01-16T19:26:03-07:00
Authentication code: 1152
Location: Washington D.C. area

-colorspace removes ICC profile in Gray TIFF output file

Post by datro »

IM 7.0.7-19 Q16 x64
Windows 10 64-bit machine

I am resizing Gray TIFF files in a Windows batch file. Relevant input file properties BEFORE any processing:

Code: Select all

magick identify -verbose -quiet test.tif
...
Format: TIFF
Colorspace: Gray
Type: Grayscale
Gamma: 0.454545
Properties:
    icc:copyright: Copyright 1999 Adobe Systems Incorporated
    icc:description: Gray Gamma 2.2
    icc:manufacturer: Gray Gamma 2.2
    icc:model: Gray Gamma 2.2
    photoshop:ColorMode: 1
    photoshop:ICCProfile: Gray Gamma 2.2
Profiles:
    Profile-8bim: 11078 bytes
    Profile-icc: 408 bytes
    Profile-xmp: 16909 bytes
I am converting the image to a linear colorspace (LinearGray) before the resize and then back to Gray as follows:

Code: Select all

magick -quiet "%~1" -filter Quadratic -colorspace LinearGray -distort Resize 10800 -colorspace Gray ... 
However, this removes all traces of the "Gray Gamma 2.2" ICC profile information in the output file meta-data. The following properties are removed:

Code: Select all

    icc:copyright: Copyright 1999 Adobe Systems Incorporated
    icc:description: Gray Gamma 2.2
    icc:manufacturer: Gray Gamma 2.2
    icc:model: Gray Gamma 2.2

    Profile-icc: 408 bytes    
    
I don't understand why the profile is removed. I am only using -colorspace to get into a linear space for the resize, then again to get back to the original colorspace. In my view, this should NOT remove any profile data. It complicates things for me later in my workflow chain.

Out of curiosity, I tried using -gamma instead of -colorspace as follows:

Code: Select all

magick -quiet "%~1" -filter Quadratic -gamma 0.454545 -distort Resize 10800 -gamma 2.2 ...
and this did NOT remove the profile data (but based on my research I believe using -gamma is sub-optimal for this purpose).

Given that "-colorspace" is the preferred way to convert to linear light and back for resize operations, how can it be done and preserve the profile data in TIFF files?

Thanks,
Dave
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -colorspace removes ICC profile in Gray TIFF output file

Post by fmw42 »

Can you post a small gray tif that shows this behavior, so we can test? You can upload to some free hosting service that does not change the image format (or zip it first). Dropbox is good, but any free one that will allow zip file or not change the image or meta data would do. You can then put the URL here.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -colorspace removes ICC profile in Gray TIFF output file

Post by fmw42 »

OK. I can confirm that the profile is lost, when simply doing the following in IM 7.0.7.21 Q16 Mac OSX.

Code: Select all

magick lena.png -colorspace gray -profile /Users/fred/images/profiles/GrayGamma22.icm -compress none lena.tif

Code: Select all

magick lena.tif -colorspace LinearGray -distort Resize 200% -colorspace Gray lena2.tif
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: -colorspace removes ICC profile in Gray TIFF output file

Post by snibgo »

IM has two methods for changing colorspaces: "-profile" and "-colorspace". For images that have embedded profiles, the "-profile" method should generally be used.

Elle Stone has made a variety of colorspaces, including "Gray-elle-V2-g10.icc", which is linear gamma. See https://github.com/ellelstone/elles_icc ... r/profiles

So you can extract the current profile, then convert the image to linear, do the work, and convert back:

Code: Select all

magick "%~1" myprofile.icc

magick -quiet "%~1" -filter Quadratic -profile Gray-elle-V2-g10.icc -distort Resize 10800 -profile myprofile.icc ... 
snibgo's IM pages: im.snibgo.com
datro
Posts: 15
Joined: 2018-01-16T19:26:03-07:00
Authentication code: 1152
Location: Washington D.C. area

Re: -colorspace removes ICC profile in Gray TIFF output file

Post by datro »

OK, thanks. Using "-profile" sounds like a solution, though I sill wonder why the "-colorspace" round-trip removes the profile. Also doing profile conversions makes me a bit nervous since I believe there is potential loss of accuracy in the round-trip. These images are ultimately being printed as fine art prints and I want to use the method which absolutely minimizes losses from conversions (this is why I'm looking into ImageMagick in the first place for the resize).

What about the following "hybrid" approach which still uses "-colorspace", but saves the profile file as you suggested, then simply adds it back in with "-profile" (I've tested this and it seems to work):

Code: Select all

magick "%~1" myprofile.icc
magick "%~1" -filter Quadratic -colorspace LinearGray -distort Resize 10800 -colorspace Gray -profile myprofile.icc ... 
How would the above approach compare to your recommended "-profile" round-trip in terms of accuracy? Worse? Better?

Thanks,
Dave
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: -colorspace removes ICC profile in Gray TIFF output file

Post by snibgo »

datro wrote:... I still wonder why the "-colorspace" round-trip removes the profile.
Possibly because an ICC profile is a precise definition of the colour (including the tone) that should result from each numerical value. As soon as you change colorspace, that definition is no longer valid. You can, of course, simply save the profile and restore (assign) it at the end.
datro wrote:How would the above approach compare to your recommended "-profile" round-trip in terms of accuracy? Worse? Better?
Assuming your embedded profile is accurate, (and assuming Elle Stone's profile is accurate, which I don't doubt), then the two approaches will either give the same result, or the "-profile" method will be more correct.

In any round-trip, there is a potential loss of accuracy. You can test this for images with embedded profiles, eg:

Code: Select all

magick mytiff.tiff myprofile.icc

magick mytiff.tiff -profile AnyProfile.icc -profile myprofile.icc test.tiff

magick compare -metric RMSE mytiff.tiff test.tiff NULL:
The output is something like "292.521 (0.00446359)". The number in parentheses is between 0.0 and 1.0, where 0.0 means there is no difference. A number larger than 0.01 means the difference is probably visible.

With a similar "magick compare", you can compare any two images that you expect to be similar.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -colorspace removes ICC profile in Gray TIFF output file

Post by fmw42 »

Profile have been improved in Imagemagick for higher precision recently.

2017-11-17 6.9.9-24 Cristy <quetzlzacatenango@image...>
Support HDRI color profile management.


2017-11-12 7.0.7-12 Cristy <quetzlzacatenango@image...>
Support HDRI color profile management.
datro
Posts: 15
Joined: 2018-01-16T19:26:03-07:00
Authentication code: 1152
Location: Washington D.C. area

Re: -colorspace removes ICC profile in Gray TIFF output file

Post by datro »

fmw42 wrote: 2018-01-23T17:01:21-07:00 Profile have been improved in Imagemagick for higher precision recently.

2017-11-12 7.0.7-12 Cristy <quetzlzacatenango@image...>
Support HDRI color profile management.
It is not clear to me what is meant by "Support HDRI color profile management."
Does it mean:
- The HDRI versions of IM now have color profile management functionality (but didn't have this before)?
- All versions of IM now have improved precision in their color profile management?
- Only HDRI versions of IM have improved precision in their color profile management?
- Something else?

How exactly does the term "HDRI" apply to profile management as used above? Are we talking about improved profile management precision or is it simply a reference to the 32-bit HDRI versions? Is there something more specific I can read that explains what is meant by the Changelog entry you have referenced?

Thanks,
Dave
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -colorspace removes ICC profile in Gray TIFF output file

Post by fmw42 »

I believe it means that rather than integer computations with the profile, IM now does floating point computations. Possibly it means it can handle out-of-gamut profile better or more accurately.

But see viewtopic.php?f=2&t=33059&p=151271&hili ... le#p151248

The Imagemagick developer would have to explain in more detail and/or correct my statement.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: -colorspace removes ICC profile in Gray TIFF output file

Post by magick »

Previously profiles were applied as 16 bit integers, its faster and more efficient, but is not useful to HDRI because it includes a quantization step. We now apply color profile transforms as floating point pixels which supports out-of-band pixel values such as negative values as required for proper HDRI support.
datro
Posts: 15
Joined: 2018-01-16T19:26:03-07:00
Authentication code: 1152
Location: Washington D.C. area

Re: -colorspace removes ICC profile in Gray TIFF output file

Post by datro »

magick wrote: 2018-01-24T04:10:21-07:00 Previously profiles were applied as 16 bit integers, its faster and more efficient, but is not useful to HDRI because it includes a quantization step. We now apply color profile transforms as floating point pixels which supports out-of-band pixel values such as negative values as required for proper HDRI support.
OK, getting more clear, thank you. So just to confirm, I think you are saying I need to be using an HDRI version of IM to get the floating-point precision in profile management...correct?

And if that's the case, I would like to encourage the developers to release HDRI binary packages for the Windows platform as a matter of course. Now I'll have to go figure out how to compile and build IM HDRI on my Windows 10 system...

Thanks,
Dave
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: -colorspace removes ICC profile in Gray TIFF output file

Post by snibgo »

See http://www.imagemagick.org/script/download.php#windows binary distributions with HDRI in the name.
snibgo's IM pages: im.snibgo.com
Post Reply