[resolved - no bug] -compose plus -layers flatten

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
HugoRune
Posts: 90
Joined: 2009-03-11T02:45:12-07:00
Authentication code: 8675309

[resolved - no bug] -compose plus -layers flatten

Post by HugoRune »

"-compose plus -layers flatten" alway produces a blank white image

Example:

convert logo: logo: -compose plus -layers flatten test.png
Image Blank image

convert logo: logo: -compose plus -composite test.png
Image Correct output


compose multiply -layers flatten has no such problems
convert logo: logo: -compose multiply -layers flatten test.png
Image also correct

but some other methods like compose screen -layers flatten also do not work correctly

ImageMagick 6.5.5-2 2009-08-25 Q16
Last edited by HugoRune on 2009-08-26T19:05:01-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -compose plus -layers flatten

Post by fmw42 »

same with -compose minus -layers flatten

but -compose add -layers flatten does produce a non-white result (but add vs plus will wrap-around in grayscale)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: -compose plus -layers flatten

Post by anthony »

convert logo: logo: -compose plus -layers flatten test.png

pure white image!

that is correct output because you are really doing three compose operations

( background + logo: ) + logo: ) -> white

Background color defaults to white so the image goes beyond white and clipped at white.

This is NOT a bug, but a miss-understanding of how the operator works.

This is the way all 'layer merging' type operators work 'flatten' 'mosaic' 'merge'.
Actually it is also similar to how border, extent, frame works too, just with
different canvas color sources.

Now if you want a equivalent of a multiply by 2 then set background to black!!!

convert logo: logo: -background black -compose plus -layers flatten test.png

And you will get the right answer.

However as you are using two images only and no transparency why not use -composite instead!

convert logo: logo: -compose plus -composite test.png

no background will be involved then!
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: -compose plus -layers flatten

Post by anthony »

fmw42 wrote:same with -compose minus -layers flatten

but -compose add -layers flatten does produce a non-white result (but add vs plus will wrap-around in grayscale)

-compose add and -compose substract are modulus operators, that is the values wrap at white and black. They are also effected by background, but white is almost exactly the same as black, or zero, so their is little effect from the default background.

However with a 'flatten' background is still involved and the background color can be used (and is used) to set a 'phase' type offset to the modulus addition/subtraction.

Remember multi-image layer merging, 'flatten', 'mosaic', and 'merge' all work by starting with a background canvas, then subtracting each image from that canvas.

(..( background OP image1 ) OP image2 ) OP image 3 )....

Multiply is unaffected as white is a image * 1.0 which is the image.
Screen is effected as image SCREEN 1.0 => 1.0 (use back instead)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
HugoRune
Posts: 90
Joined: 2009-03-11T02:45:12-07:00
Authentication code: 8675309

Re: -compose plus -layers flatten

Post by HugoRune »

anthony wrote: that is correct output because you are really doing three compose operations

( background + logo: ) + logo: ) -> white
I see, that makes sense, thanks!

I assumed the background was always an implicit "under" composition, no matter what.

Do i have to set the background depending on what type of composition I use, or is there a "None" background that has no effect in all cases?
anthony wrote:However as you are using two images only and no transparency why not use -composite instead!
I simplified the test case. I actually have a least 4 images at a time I want to add ( and they are not all identical either :) )
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: -compose plus -layers flatten

Post by anthony »

HugoRune wrote:I assumed the background was always an implicit "under" composition, no matter what.
No it is an implicit starting canvas onto which images are layered, which is created from a clone of the first image (so it gets the first images meta-data) and sized according to the requires result of the layer operation (which is how the operators differ).

Usually the default composition is 'Over' which places image on top (over) the background and previous images. However I have used -background none -compose DstOver to place images under each other (reversed order).

Actually almost all the mathematical operators use an 'over' alpha blending. As such anything that is transparent does not take part. Also the order is reversed, which is important operators that matter (like minus and divide)

So by using -background none for any mathematical layering the canvas will not take part in the mathematics, with the first image just simply layers over the canvas.

-background none image1 image2 -compose minus -layer flatten

will place image 1 over 'none' then subtract that from image 2

where as using black would produce

image2 - ( image1 - black)

Note the order of operations! For minus (or division) that can be critical!
Do I have to set the background depending on what type of composition I use, or is there a "None" background that has no effect in all cases?
None generally has no effect, due to 'Over' alpha blending used by MOST mathematical operators (see above). 'Over' alpha blending is equivalent to a mathematical multiply of just the alpha values, but has extra effects on semi-transparent color mixing. Its handling is very precisely defined by the SVG specification for image composition.

However 'over' blending is not used for: plus, subtract, add, minus; as the alpha values are also mathematically applied! Only the 'plus' method is defined in the SVG document, and it is important that is is handled in this way. IM is the only one to implement the other three, that I know of, but they follow the same convention as 'plus' for alpha handling. See...
http://www.imagemagick.org/Usage/compose/#plus_blend

If over alpha blending is important then 'linear_dodge' is equivalent to 'plus' but with over alpha blending.

image2 plus image1 == image2 linear_dodge image1

when no transparency is involved.

Also if you negate an image to be subtracted you can use 'linear_burn' to 'minus' and image with 'over' alpha blending.

image2 minus image1 == image2 linear_burn ( image1 negate )

again these are equally only when semi-transparency is not involved and over alpha blending is wanted. This is actually the way photoshop does image subtraction!

Look at the raw tables of composition
http://www.imagemagick.org/Usage/compose/tables/

For more detail download my compose operator testing script
http://www.imagemagick.org/Usage/scripts/compose_table
and run

Code: Select all

compose_table -mb
Option -mb is for mathematics operators with alpha blending tests. This is what I have been using to debug the compose operators.
HugoRune wrote:I simplified the test case. I actually have a least 4 images at a time I want to add ( and they are not all identical either :) )

Then just set -background to black and all will be well! I have done this for example to add together the differences of the three color channels.

See Image Comparing, Difference Images
http://www.imagemagick.org/Usage/compare/#difference

Code: Select all

  convert bag_frame1.gif bag_frame2.gif -compose difference -composite \
          -separate -background black -compose plus -flatten \
          difference_plus.gif
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply