input Linear RGB specifiy output RGB or sRGB

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?".
TedBaker
Posts: 50
Joined: 2017-10-10T14:14:55-07:00
Authentication code: 1151

Re: input Linear RGB specifiy output RGB or sRGB

Post by TedBaker »

fmw42 wrote: 2017-10-21T19:36:58-07:00
I will report the issue.

It should read:
Normally, the conversion to separate each channel of an sRGB color image produces separate non-linear grayscale images. However the same concept can be applied, if it is desired to keep the separate channels as linear grayscale. For example, the following produces linear grayscale channels.
I notice they updated the legacy page, but they have not updated the v7 version of the page.

Also the examples that relate to this 'error' in the both versions of the documentation are still either wrong, or later examples don't have the proper context as they refer to previous examples that are "Wrong".
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: input Linear RGB specifiy output RGB or sRGB

Post by fmw42 »

OK, I see. I will see what I can do about it.
TedBaker
Posts: 50
Joined: 2017-10-10T14:14:55-07:00
Authentication code: 1151

Re: input Linear RGB specifiy output RGB or sRGB

Post by TedBaker »

Both these pages https://imagemagick.org/script/color-management.php and https://legacy.imagemagick.org/script/c ... gement.php

the legacy and the current still have examples that are WRONG!

Code: Select all

convert myimage.png -separate myimage_channels_%d.png
convert myimage_channels_*.png -combine myimage2.png
In the above example, the result is darker than the original, because the channels were separate as linear gray and subsequently combined as linear color.
in ImageMagick 6.9.7-4
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: input Linear RGB specifiy output RGB or sRGB

Post by fmw42 »

Is your myimage.png linear or non-linear. You have to tell IM what the colorspace is before separating and before combining.

I tried to fix the page, but only had Magick change it in one paragraph. It needs to be rewritten. I have not had time nor will I for about a week. You can remind me next week and I will try to look at it.

But please be more specific about what you are doing and your input and output regarding linear and non-linear. The proper way to do separation and combine is as if it were different colorspaces. See the sections following http://www.imagemagick.org/Usage/color_basics/#separate and http://www.imagemagick.org/Usage/color_ ... bine_other
TedBaker
Posts: 50
Joined: 2017-10-10T14:14:55-07:00
Authentication code: 1151

Re: input Linear RGB specifiy output RGB or sRGB

Post by TedBaker »

In my case my input file is linear, and I want to stay that way!

Code: Select all

convert logo: -strip -set colorspace RGB \
    -separate \
    \( -clone 0 -poly ".1,-1" -poly "1,1.65" \) -swap 0,3 -delete 3 \
    \( -clone 1 -poly ".1,-1" -poly "1,1.65" \) -swap 1,3 -delete 3 \
    \( -clone 2 -poly ".1,-1" -poly "1,1.65" \) -swap 2,3 -delete 3 \
    -set colorspace RGB \
    -combine -type TrueColor \
    myfile.tif
This is simplified, but I believe everything is linear in memory?, except that when it writes the file it is converted to sRGB, which is fine, but why?
i.e. what are the rules?

Code: Select all

convert logo: -strip -set colorspace RGB \
    -fx "u?u:1" \
    -channel R -fx "(.1/u)^1.65" \
    -channel G -fx "(.1/u)^1.65" \
    -channel B -fx "(.1/u)^1.65" \
    myfile2.tif
This ^ should almost the same except for u?u:1 but it's not.

Thankyou for you help.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: input Linear RGB specifiy output RGB or sRGB

Post by fmw42 »

This does not make a linear input. It keeps the same colorspace, but tries to set the gamma=1, which will only work if the image type supports keeping the gamma in its meta data

Code: Select all

convert logo: -set colorspace RGB ...
You need to do the following to convert pixel data to linear.

Code: Select all

convert logo: -colorspace RGB ...
Also Tiff does not support gamma as meta data, so IM will report gamma=0.4545

Try this first:

Code: Select all

convert logo: -colorspace RGB rgb.png
identify -format "%[gamma]\n" rgb.png
1
So the above makes a linear rgb.png and shows gamma=1. You can see that since the image is darker

Code: Select all

convert rgb.png \
-separate \
-set colorspace RGB \
-combine -colorspace RGB \
new_rgb.tif
identify -format "%[gamma]\n" new_rgb.tif
0.4545
The above keeps the image as linear, since it is darker, but reports gamma=0.4545, since TIFF does not save the gamma value in its meta data.

But doing the same with png output, keeps the image as linear since it is darker, but PNG does save gamma=1 in the meta data.

Code: Select all

convert rgb.png \
-separate \
-set colorspace RGB \
-combine -colorspace RGB \
new_rgb.png
identify -format "%[gamma]\n" new_rgb.png
1
In fact this works also, since the image is already linear:

Code: Select all

convert rgb.png \
-separate \
-set colorspace RGB \
-combine \
new_rgb.png
identify -format "%[gamma]\n" new_rgb.png
1
But it is aways best to use -set colorspace YYY before -combine and -colorspace XXX after -combine.

So this should also work:

Code: Select all

convert logo: -colorspace RGB \
-separate \
-set colorspace RGB \
-combine -colorspace RGB \
new_rgb.png
identify -format "%[gamma]\n" new_rgb.png
1
This should now work for your command:

Code: Select all

convert logo: -colorspace RGB \
-separate \
\( -clone 0 -poly ".1,-1" -poly "1,1.65" \) -swap 0,3 -delete 3 \
\( -clone 1 -poly ".1,-1" -poly "1,1.65" \) -swap 1,3 -delete 3 \
\( -clone 2 -poly ".1,-1" -poly "1,1.65" \) -swap 2,3 -delete 3 \
-set colorspace RGB \
-combine -colorspace RGB -type TrueColor \
myfile.png
identify -format "%[gamma]\n" myfile.png
1
I note that if you leave -strip in, then the reported gamma=0.4545, even though the images look the same. I am not sure why this is happening. The logo: image is an GIF file.
TedBaker
Posts: 50
Joined: 2017-10-10T14:14:55-07:00
Authentication code: 1151

Re: input Linear RGB specifiy output RGB or sRGB

Post by TedBaker »

Thanks fred, but I don't think my post got the point across, and I am still confused what the rules are, or in fact if there is a bug somewhere... 6.9.7-4

The first part of my example probably caused confusion, so lets start with a tif file which I want to consider as linear. My understanding is there is no meaningful gamma meta data in a tiff, so the only way to know it's linear is to put linear data in there in the first place?

Code: Select all

convert logo: logo.tif
convert logo: -set colorspace RGB logo.tif
convert logo: -colorspace RGB logo.tif
For example the first two commands leave an identical file because the gamma field is not set/changed, and no changes to the image have been made. The final command does change the image data, but the meta data still shows gamma=0.4545

But it doesn't matter I just what the next step to treated as if it was a linear file. So pick one of the commands above and THEN apply the following two commands, (the last one if your prefer). I expect the same result, but that does not happen, and I would like to understand why.

Code: Select all

convert logo.tif -strip -set colorspace RGB \
 -fx "u?u:1"  \
 -separate  \
 \( -clone 0 -poly ".1,-1" -poly "1,1.65" \) -swap 0,3 -delete 3 \
 \( -clone 1 -poly ".1,-1" -poly "1,1.65" \) -swap 1,3 -delete 3 \
 \( -clone 2 -poly ".1,-1" -poly "1,1.65" \) -swap 2,3 -delete 3 \
 -set colorspace RGB     -combine -type TrueColor     myfile.tif

Code: Select all

convert logo.tif -strip -set colorspace RGB  \
 -fx "u?u:1" \
 -channel R -fx "(.1/u)^1.65" \
 -channel G -fx "(.1/u)^1.65" \
 -channel B -fx "(.1/u)^1.65" \
 myfile2.tif
In the above examples I have told IM -set colorspace RGB after loading the file, before -separate and before -combine, so I expect linear outputs at all times?
fmw42 wrote: 2017-11-12T22:38:37-07:00 But it is aways best to use -set colorspace YYY before -combine and -colorspace XXX after -combine.

Code: Select all

convert logo.tif -strip -set colorspace RGB \
 -fx "u?u:1"  \
 -separate  \
 \( -clone 0 -poly ".1,-1" -poly "1,1.65" \) -swap 0,3 -delete 3 \
 \( -clone 1 -poly ".1,-1" -poly "1,1.65" \) -swap 1,3 -delete 3 \
 \( -clone 2 -poly ".1,-1" -poly "1,1.65" \) -swap 2,3 -delete 3 \
 -set colorspace RGB     -combine -type TrueColor   -colorspace RGB  myfile3.tif
Yes If I add this first command I now get the same result, but this seems inconsistent with the way -separate works,is this how it is supposed to work, I cannot find once "correct" example in the documentation that covers this.

I am not sure why this needs to be so opaque?

Ideally some documentation that explains it like so:

file_on_disk.tif ---read--> data_in_memory, color_space_flags
data_in_memory, color_space_flags -----separate----> data_in_memory, color_space_flags
data_in_memory, color_space_flags ----combine----> data_in_memory, color_space_flags
data_in_memory, color_space_flags ----create_file----> new_file_on_disk.tif

covering each stage, with what is in the file, what is in memory and what is the status of the color space & gamma, whether the meta data in the file is relevant (when it's ignored or doesn't work), as the meta data is obviously affecting the result as when I now remove -set colorspace RGB from the following two examples I now get the same result as the myfile2.tif and myfile3, but it's different to myfile1.tif and its not obvious or intuitive why or a bug...

Code: Select all

convert logo.tif -strip \
 -fx "u?u:1"  \
 -separate  \
 \( -clone 0 -poly ".1,-1" -poly "1,1.65" \) -swap 0,3 -delete 3 \
 \( -clone 1 -poly ".1,-1" -poly "1,1.65" \) -swap 1,3 -delete 3 \
 \( -clone 2 -poly ".1,-1" -poly "1,1.65" \) -swap 2,3 -delete 3 \
 -combine -type TrueColor     myfile4.tif

Code: Select all

convert logo.tif -strip  \
 -fx "u?u:1" \
 -channel R -fx "(.1/u)^1.65" \
 -channel G -fx "(.1/u)^1.65" \
 -channel B -fx "(.1/u)^1.65" \
 myfile5.tif
TedBaker
Posts: 50
Joined: 2017-10-10T14:14:55-07:00
Authentication code: 1151

Re: input Linear RGB specifiy output RGB or sRGB

Post by TedBaker »

Just following up, as there is indeed a bug somewhere.

6.9.7-4 gives a different result than 6.9.9-20, 6.9.9-24

with the use of:

Code: Select all

convert logo.tif -strip -set colorspace RGB \
 -fx "u?u:1"  \
 -separate  \
 \( -clone 0 -poly ".1,-1" -poly "1,1.65" \) -swap 0,3 -delete 3 \
 \( -clone 1 -poly ".1,-1" -poly "1,1.65" \) -swap 1,3 -delete 3 \
 \( -clone 2 -poly ".1,-1" -poly "1,1.65" \) -swap 2,3 -delete 3 \
 -set colorspace RGB     -combine -type TrueColor   -colorspace RGB  myfile3.tif
indeed the following example also get differents results between 6.9.7-4, 6.9.9-20, 6.9.9-24

Code: Select all

convert logo: -colorspace RGB \
-separate \
\( -clone 0 -poly ".1,-1" -poly "1,1.65" \) -swap 0,3 -delete 3 \
\( -clone 1 -poly ".1,-1" -poly "1,1.65" \) -swap 1,3 -delete 3 \
\( -clone 2 -poly ".1,-1" -poly "1,1.65" \) -swap 2,3 -delete 3 \
-set colorspace RGB \
-combine -colorspace RGB -type TrueColor \
myfile.png
but which one has the bug? ...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: input Linear RGB specifiy output RGB or sRGB

Post by fmw42 »

Try this:

Code: Select all

convert logo: logo.tif
convert logo: -set colorspace RGB logo1.tif
convert logo: -colorspace RGB logo2.tif

logo.tif will be non-linear. logo1.tif will be non-linear. logo2.tif will be the only one that is linear. You can tell because the linear one will be darker.

You have to change the pixel data to linear and the only way to do that is with -colorspace RGB. If you use -set colorspace it simply tries to set the colorspace to linear in the meta data. Tiff does not support RGB colorspace nor gamma=1. So logo1.tif will still have non-linear pixels.

Check that in your various versions. If it does not work for some version that that version is buggy.

This should also result in a linear Tiff, but the gamma value will be wrong.

Code: Select all

convert logo: -colorspace RGB \
-separate \
-set colorspace RGB \
-combine -colorspace RGB \
new_rgb.tiff
Test that out in your versions.

Start simple. Then when you understand that, then add your other clone processing.

Sorry, I do not have access right now to IM 6.9.7.4.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: input Linear RGB specifiy output RGB or sRGB

Post by fmw42 »

I checked the following in IM 6.9.7.4 and it behaves correctly -- the same as above with 6.9.9.23.

Code: Select all

convert logo: logo.tif
convert logo: -set colorspace RGB logo1.tif
convert logo: -colorspace RGB logo2.tif
Only logo2.tif is linear and darker than the other two.

However, I do see a slight different with the results of your command in those two versions upon viewing alternately.

Code: Select all

convert logo: -colorspace RGB \
-separate \
\( -clone 0 -poly ".1,-1" -poly "1,1.65" \) -swap 0,3 -delete 3 \
\( -clone 1 -poly ".1,-1" -poly "1,1.65" \) -swap 1,3 -delete 3 \
\( -clone 2 -poly ".1,-1" -poly "1,1.65" \) -swap 2,3 -delete 3 \
-set colorspace RGB \
-combine -colorspace RGB -type TrueColor \
myfile_69923.png

Code: Select all

convert logo: -colorspace RGB \
-separate \
\( -clone 0 -poly ".1,-1" -poly "1,1.65" \) -swap 0,3 -delete 3 \
\( -clone 1 -poly ".1,-1" -poly "1,1.65" \) -swap 1,3 -delete 3 \
\( -clone 2 -poly ".1,-1" -poly "1,1.65" \) -swap 2,3 -delete 3 \
-set colorspace RGB \
-combine -colorspace RGB -type TrueColor \
myfile_6974.png


My guess is that there was a bug in IM 6974 that has been fixed subsequently, possibly an issue with -poly. In the logo image, the beard is not constant and has a gray shading to it. You see that in reverse in the IM 69923 version but it is solid black in the 6974 version. Therefore I think the 6974 version is wrong.
TedBaker
Posts: 50
Joined: 2017-10-10T14:14:55-07:00
Authentication code: 1151

Re: input Linear RGB specifiy output RGB or sRGB

Post by TedBaker »

fmw42 wrote: 2017-11-13T11:46:12-07:00 Start simple. Then when you understand that, then add your other clone processing.
Which is how I discovered the bug.

fmw42 wrote: 2017-11-13T11:46:12-07:00Tiff does not support RGB colorspace nor gamma=1
My point exactly, pick any tiff file you like, and then process it, with 6.9.7-4, 6.9.9-20 or 6.9.9-23, you would expect the same result no?

That is not the case, 6.9.7-4 works differently, than 6.9.9-20 or 6.9.9-23, one of them has a bug, what I can't tell is which one is the one with the bug... I suspect its actually the later.

Actually you don't need tiff as the bug exists with png.

for example this gives a different result (they look different) with 6.9.7-4 compared to 6.9.9-23

Code: Select all

convert logo: -colorspace RGB \
-separate \
\( -clone 0 -poly ".1,-1" -poly "1,1.65" \) -swap 0,3 -delete 3 \
\( -clone 1 -poly ".1,-1" -poly "1,1.65" \) -swap 1,3 -delete 3 \
\( -clone 2 -poly ".1,-1" -poly "1,1.65" \) -swap 2,3 -delete 3 \
-set colorspace RGB \
-combine -colorspace RGB -type TrueColor \
myfile.png
Last edited by TedBaker on 2017-11-13T13:09:07-07:00, edited 4 times in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: input Linear RGB specifiy output RGB or sRGB

Post by snibgo »

TedBaker wrote:but which one has the bug? ...
I suggest you reduce the commands to the minimum that shows the difference between versions. For example, I suspect the lines with "-poly" have no significance.

When you have "-channel" in a command, you should always have a "+channel" after the operations you want restricted to certain channels.
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: input Linear RGB specifiy output RGB or sRGB

Post by fmw42 »

Here is a simple test that shows that IM 6.9.7.4 is processing correctly and IM 6.9.9.23 is not.

Processing directly and again with -separate and -combine, shows a difference for IM 6.9.9.23, but properly the same for IM 6.9.7.4

Code: Select all

im6974 convert logo: -colorspace RGB logo_linear1_6974.png
Image

Code: Select all

im6974 convert logo: -colorspace RGB \
-separate \
-set colorspace RGB \
-combine -colorspace RGB \
logo_linear2_6974.png
Image

The above two are both linear in IM 6.9.7.4

Code: Select all

convert logo: -colorspace RGB logo_linear1_69923.png
Image

Code: Select all

convert logo: -colorspace RGB \
-separate \
-set colorspace RGB \
-combine -colorspace RGB \
logo_linear2_69923.png
Image

But using -separate and -combine in IM 6.9.9.23 is resulting in a non-linear result. It also fails if I output to TIFF. So it is not apparently a PNG issue.

The same command works fine in IM 7.0.7.11

Code: Select all

im7 magick logo: -colorspace RGB \
-separate \
-set colorspace RGB \
-combine -colorspace RGB \
logo_linear2_70711.png
Image


This looks like a bug to me in later versions of IM 6

P.S. I have tracked it down to a change between 6.9.9.9 (works correctly with linear result) and 6.9.9.10 (gives nonlinear result)


Oddly, this works fine, if I save the linear png first ( 6.9.9.23)

Code: Select all

convert logo: -colorspace RGB rgb.png
Image

Code: Select all

convert rgb.png \
-separate \
-set colorspace RGB \
-combine -colorspace RGB \
new_rgb.png
Image
TedBaker
Posts: 50
Joined: 2017-10-10T14:14:55-07:00
Authentication code: 1151

Re: input Linear RGB specifiy output RGB or sRGB

Post by TedBaker »

snibgo wrote: 2017-11-13T13:06:15-07:00
TedBaker wrote:but which one has the bug? ...
I suggest you reduce the commands to the minimum that shows the difference between versions. For example, I suspect the lines with "-poly" have no significance.

When you have "-channel" in a command, you should always have a "+channel" after the operations you want restricted to certain channels.
consider the following creates some very strange results, the plain convert is 6.9.7-4 in the script. The resulting file 6myfile-6.9.9-23-2.png is particularly strange.

Code: Select all

#!/bin/bash
convert logo: -colorspace RGB \
-separate \
\( -clone 0 -poly "1,1" \) -swap 0,3 -delete 3 \
\( -clone 1 -poly "1,1" \) -swap 1,3 -delete 3 \
\( -clone 2 -poly "1,1" \) -swap 2,3 -delete 3 \
-set colorspace RGB \
-combine -colorspace RGB -type TrueColor \
1myfile-6.8.7-4.png

convert-6.9.9-23 logo: -colorspace RGB \
-separate \
\( -clone 0 -poly "1,1" \) -swap 0,3 -delete 3 \
\( -clone 1 -poly "1,1" \) -swap 1,3 -delete 3 \
\( -clone 2 -poly "1,1" \) -swap 2,3 -delete 3 \
-set colorspace RGB \
-combine -colorspace RGB -type TrueColor \
2myfile-6.9.9-23.png

convert logo: -colorspace RGB \
-separate \
\( -clone 0 -poly "1,1" \) -swap 0,3 -delete 3 \
\( -clone 1 -poly "1,1" \) -swap 1,3 -delete 3 \
\( -clone 2 -poly "1,1" \) -swap 2,3 -delete 3 \
-set colorspace RGB \
-combine -colorspace RGB -type TrueColor -colorspace RGB \
3myfile-6.8.7-4-1.png

convert-6.9.9-23 logo: -colorspace RGB \
-separate \
\( -clone 0 -poly "1,1" \) -swap 0,3 -delete 3 \
\( -clone 1 -poly "1,1" \) -swap 1,3 -delete 3 \
\( -clone 2 -poly "1,1" \) -swap 2,3 -delete 3 \
-set colorspace RGB \
-combine -colorspace RGB -type TrueColor -colorspace RGB \
4myfile-6.9.9-23-1.png

convert logo: -colorspace RGB \
-separate \
\( -clone 0 -poly "1,1" \) -swap 0,3 -delete 3 \
\( -clone 1 -poly "1,1" \) -swap 1,3 -delete 3 \
\( -clone 2 -poly "1,1" \) -swap 2,3 -delete 3 \
-set colorspace RGB \
-combine -colorspace RGB -type TrueColor -colorspace sRGB \
5myfile-6.8.7-4-2.png

convert-6.9.9-23 logo: -colorspace RGB \
-separate \
\( -clone 0 -poly "1,1" \) -swap 0,3 -delete 3 \
\( -clone 1 -poly "1,1" \) -swap 1,3 -delete 3 \
\( -clone 2 -poly "1,1" \) -swap 2,3 -delete 3 \
-set colorspace RGB \
-combine -colorspace RGB -type TrueColor -colorspace sRGB \
6myfile-6.9.9-23-2.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: input Linear RGB specifiy output RGB or sRGB

Post by fmw42 »

See my example above that shows that IM 6.9.9.10 and beyond are not creating linear output when using -separate and -combine.
Post Reply