Composite Producing Diffs Where There Are None

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
h1d3m3
Posts: 8
Joined: 2009-07-27T13:48:16-07:00
Authentication code: 8675309

Composite Producing Diffs Where There Are None

Post by h1d3m3 »

1) Convert a SVG to PNG (using an external Batik Tool, not ImageMagick). The png image is called hello.png.
2) Run the following command : "composite hello.png hello.png -compose difference out1.png" which will show the difference between the two files. In this case, they are the exact same file, so the difference should be nothing.
3) Display the file out1.png and it shows differences. How could a compare against the same file ever have differences?

Original Image (hello.png):
Image

Difference Image (out1.png):
Image

As a side note, we're seeing differences in separate files where we thought there should be none...that lead us to this sanity checking. This does not seem to be an issue when the images are jpegs.

Lastly, we did a Absolute Error count ("compare -metric AE hello.png hello.png null:" ) and the result is 0. So the compare tool seems to be telling us the right thing. Try downloading the hello.png image and running the composite command listed above. Perhaps there is something in the hello.png file that is confusing composite? Other PNGs and JPEGs do not produce this behaviour.

Let me know if more info is needed. Thanks.

Version: ImageMagick 6.6.1-0 2010-04-20 Q16 http://www.imagemagick.org (I have also seen this on the latest version of the windows binary)
OS : MacOs X (also windows)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Composite Producing Diffs Where There Are None

Post by snibgo »

The result is black (transparent or opaque) where the original is fully transparent or opaque. There are differences only where the original is partly transparent. I don't know if this is expected or a bug.

If we use "minus", we get black everywhere (tranparent, opaque or inbetween).
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: Composite Producing Diffs Where There Are None

Post by fmw42 »

you have not included your alpha channel in the diff operations.

if you do:

convert hello.png hello.png -channel rgba -alpha on -compose difference -composite hello_diff.png

it will be totally transparent

but if you do

convert hello.png hello.png -channel rgba -alpha on -compose difference -composite -alpha off hello_diff2.png

it will be totally black

or if you do (set alpha to opaque)

convert hello.png hello.png -alpha opaque -compose difference -composite hello_diff3.png

it will also be totally black
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Composite Producing Diffs Where There Are None

Post by snibgo »

Interesting that merely including "-channel rgba" makes no difference.

composite -channel rgba -compose difference hello.png hello.png out2.png

gives an identical weird result (and so does the equivalent "convert"). We have to use "-alpha" to get a sensible result. The documentation doesn't seem to say what "difference" should do with alpha, so I suppose it can do whatever it wants.
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: Composite Producing Diffs Where There Are None

Post by fmw42 »

-channel rgba tell you that you are interested in using the alpha, but you have to be sure it is enabled by using -alpha on.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Composite Producing Diffs Where There Are None

Post by anthony »

snibgo wrote:The result is black (transparent or opaque) where the original is fully transparent or opaque. There are differences only where the original is partly transparent. I don't know if this is expected or a bug.

If we use "minus", we get black everywhere (tranparent, opaque or inbetween).
snibgo is perfectly correct. and I can confirm that this is the NORMAL behaviour of difference.


Difference with transparency follow teh SVG specificaion. color values are weighted by their alpha values before comparing (that still produces black) but alpha channel is then 'Over' blended.

See Mathematical Composition and Alpha Blending

Using normal SVG defination alpha values are NOT compared! It also ignores any channel
selection before IM v 6.6.1-6

To compare alpha as well, download the VERY LATEST ImageMagick (this change was only added last week). then set -channel RGBA
See Image Channel Mathematics using Image Composition

Code: Select all

   composite -channel rgba -compose difference hello.png hello.png out2.png
The result will be transparent if the alpha channels are equivelent!

To check for all differences, you now need to separate and add ALL four channels, to form a gray scale image... ("convert" is better here as you can do multiple operations, "composite" can not)
Thresholding with zero will then so ALL differences!

Code: Select all

   convert hello.png hello.png -channel rgba -compose difference -composite \
               -separate -compose plus -composite -threshold 0  different_pixels.png
WARNING: The above will also find differences in two transparent pixels in the pixels
has different fully-transparent colors! Yes PNG does save such pixels! See Alpha Off for an example of this.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Composite Producing Diffs Where There Are None

Post by anthony »

snibgo wrote:Interesting that merely including "-channel rgba" makes no difference.
It does in the VERY latest IM! v6.6.1-6

Before this NO composition understood -channel. now only mathematical composition methods understands this setting (and only to switch between SVG defintion, and per-channel mathematics.

This was added as it was required to get Difference Morphology Operators to work correctly when using a -channel A to apply morphology to images with transparent shapes.

For example to extract just the 'edge' pixels from a shape.

Code: Select all

   convert http://www.imagemagick.org/Usage/canvas/shape.gif  shape.gif
   convert shape.gif -channel A -morphology EdgeIn Diamond  shape_edge_pixels.gif
Image Image Image
Before IM v6.6.1-6 this would fail because 'Difference' did not get a difference of just the alpha channel!

As a bonus have a look at this conversion of the edge pixels, (and why I wanted to extract them!)
to fill-in the transparent background of the image, using those edge colors.

Code: Select all

 convert shape_edge_pixels.gif txt:- |    sed '1d; / 0) /d; s/:.* /,/;' | \
      convert shape_edge_pixels.gif -sparse-color shepards '@-'    shape_edge_in_lights.png
convert shape_edge_in_lights.png shape.gif -composite shape_in_lights.png
Image Image Image
See Sparse Color as a Fill Operator
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply