confusing inconsistency: histogram alpha vs fx alpha

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

confusing inconsistency: histogram alpha vs fx alpha

Post by fmw42 »

fmw42 wrote:This is confusing to me. It is a simple example of an overall issue.

convert -size 100x100 xc:white -matte white_a.png

identify -verbose white_a.png
...
Channel statistics:
gray:
min: 1 (1)
max: 1 (1)
mean: 1 (1)
standard deviation: -0 (-0)
alpha:
min: 0 (0)
max: 0 (0)
mean: 0 (0)
standard deviation: -0 (-0)

Shows Alpha = 0

But:

convert white_a.png -format "%[fx:mean]" info:
1

Shows Alpha = 1

One or the other is really opacity and not alpha!

It would be nice if this be made consistent one way or the other, if not to difficult
[/quote]

anthony wrote:well by definition of fx that should be 1 (alpha)

Channel stats however is showing matte, when it is titled as alpha (could be made to go either way).
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: confusing inconsistency: histogram alpha vs fx alpha

Post by magick »

We have a patch in the ImageMagick subversion trunk for the problem you reported. It will also be available in the next point release.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Still confusing inconsistency: histogram alpha vs fx alpha

Post by fmw42 »

I apologize for my continuation of this topic.

I am still confused. According to the definition of alpha on http://imagemagick.org/script/color.php, it says alpha is 0 for fully transparent and 1 for fully opaque.

The verbose info seems correct now.

So testing: turn alpha on and keep opaque:
convert -size 100x100 xc:white -alpha on white_a.png
identify -verbose white_a.png
Channel statistics:
gray:
min: 1 (1)
max: 1 (1)
mean: 1 (1)
standard deviation: -0 (-0)
alpha:
min: 1 (1)
max: 1 (1)
mean: 1 (1)
standard deviation: -0 (-0)

So this seems correct as the alpha is on and in opaque mode and shows as 1.


Next, create a fully transparent image:
convert -size 100x100 xc:none none.png
identify -verbose none.png
gray:
min: 0 (0)
max: 0 (0)
mean: 0 (0)
standard deviation: -0 (-0)
alpha:
min: 0 (0)
max: 0 (0)
mean: 0 (0)

So this seems correct also as fully transparent is 0.


However, I am still getting puzzling results from fx in trying to determine an overall alpha value.


Each of these 3 seems to be indicating full transparency on an opaque image with no alpha channel:

convert -size 100x100 gradient: -scale 1x1 -format "%[fx:a]" info:
0 (seems incorrect)
convert -size 100x100 gradient: -scale 1x1 -channel a -format "%[fx:a]" info:
0 (seems incorrect)
convert -size 100x100 gradient: -scale 1x1 -channel a -separate -format "%[fx:a]" info:
0 (seems incorrect)

These next 3 are inconsistent for an image with alpha channel on, but set opaque:

convert -size 100x100 gradient: -alpha on -scale 1x1 -format "%[fx:a]" info:
1 (seems correct)
convert -size 100x100 gradient: -alpha on -scale 1x1 -channel a -format "%[fx:a]" info:
1 (seems correct)
convert -size 100x100 gradient: -alpha on -scale 1x1 -channel a -separate -format "%[fx:a]" info:
0 (seems incorrect)


And these are not quite consistent for a transparent gradient:

convert -size 100x100 gradient:none-white -scale 1x1 -format "%[fx:a]" info:
0.500008
convert -size 100x100 gradient:none-white -scale 1x1 -channel a -format "%[fx:a]" info:
0.500008
convert -size 100x100 gradient:none-white -scale 1x1 -channel a -separate -format "%[fx:a]" info:
0.499992

Can someone please clarify this form me. Perhaps I misunderstand something fundamental here about these inconsistencies.

The original purpose of all my tests was to see if there is any way to determine if an alpha channel is enabled on an image and if so what its overall (average) value is. Is this possible? If so how?

The only way I can see to determine if an alpha channel is on (irrespective of its value) is:
[ "$(identify -verbose <image> | grep 'alpha')" = "" ] && echo "alpha off" || echo "alpha on"

Is there some other pure IM way, such as a string format?

Then I wanted to use one of the fx escape methods above to determine its overall value. But they are not consistent. Advice is requested.

User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: confusing inconsistency: histogram alpha vs fx alpha

Post by anthony »

The formula '-channel A -format '%[fx:mean]' info:" should return the right 'mean' regardless of if alpha channel is enabled or not.
If it isn't or not being used the result should be '0'.
But that does not appear to be the case...
convert -size 100x100 xc:'#00000000' -channel A -format '%[fx:mean]' info:
0

convert -size 100x100 xc:'#000000FF' -channel A -format '%[fx:mean]' info:
0

convert -size 100x100 xc:white -channel A -format '%[fx:mean]' info:
1
Something does not seem right. I can't seem to get a consistant result!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: confusing inconsistency: histogram alpha vs fx alpha

Post by magick »

We fixed fx:a in the latest subversion trunk and added the %A escape formatting character to return MagickTrue if the alpha channel is activated otherwise MagickFalse.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: confusing inconsistency: histogram alpha vs fx alpha

Post by magick »

Internally the alpha channel is stored as opacity (1.0-alpha). Externally we present the value as alpha. It was a design decision originally for efficiency. By default images are opaque and we could efficiently initialize an image to all zeros rather than the less efficient method of setting RGB to 0 (black) and alpha to 1 (1 = opaque). When ImageMagick 7.0.0 is release we may store alpha internally as alpha (0 = transparent) rather than opacity. Until then we may have a few situations where opacity is returned rather than alpha.
Post Reply