Page 1 of 1

reversible add?

Posted: 2010-10-17T08:09:52-07:00
by foobarbaz
The basic idea is to add an image to another image, and than go the other way.
Consider following script:

Code: Select all

#!/bin/bash
convert A.png -quality 5% jpg:- | convert jpg:- B.png
convert B.png -negate nB.png
composite -compose add A.png B.png AaB.png
composite -compose add AaB.png nB.png AA.png
compare A.png AA.png x:-
AA.png should be equal to A.png, but it somehow is not.
compositing nB.png with B.png gives completely white image (expected) why does it not cancel out when composed with A.png?
Where am i making wrong assumptions?

Re: reversible add?

Posted: 2010-10-17T08:30:30-07:00
by el_supremo
The conversion to and from JPG in the first step adds compression artifacts to B.png so that it is not identical to A.png. This will be particularly obvious when you use such a low compression quality.

Pete

Re: reversible add?

Posted: 2010-10-17T08:41:06-07:00
by foobarbaz
Sorry, i should have explained better.
jpg (B.png) is there just to add different image to the source.
I than add negated jpg, so it should cancel out.
Instead i get this:
A.png (source): Image
AA.png (output): Image

Re: reversible add?

Posted: 2010-10-17T11:48:16-07:00
by fmw42
Adding and subtracting via composite are not reversible in IM. After each step, there will be some clipping at 0 and Quantumrange that will then be lost.


see -compose Mathematics http://www.imagemagick.org/Usage/compose/#mathematics

Using jpgs as intermediate images is not a good idea as there will be compression losses as well.

Re: reversible add?

Posted: 2010-10-17T13:29:31-07:00
by el_supremo
If I read the code correctly the add and subtract composite operations do not clip, they wrap around. For example with Q8, 255 plus 1 is not clipped to 255 but gives a result of zero.
If you want clipping try the "plus" and "minus" composite operations. I used those with your example and the AA.png is white.

Pete

Re: reversible add?

Posted: 2010-10-17T14:14:05-07:00
by fmw42
el_supremo wrote:If I read the code correctly the add and subtract composite operations do not clip, they wrap around. For example with Q8, 255 plus 1 is not clipped to 255 but gives a result of zero.
If you want clipping try the "plus" and "minus" composite operations. I used those with your example and the AA.png is white.

Pete

Thanks Pete,

I was thinking mathematically and about -compose plus and minus. But his using -compose add and subtract will wrap around and perhaps that is what he is seeing.

Fred

Re: reversible add?

Posted: 2010-10-17T15:54:19-07:00
by anthony
fmw42 wrote: I was thinking mathematically and about -compose plus and minus. But his using -compose add and subtract will wrap around and perhaps that is what he is seeing.

Fred
Yes those artifacts are in areas on near pure black, and is producing maximum value primary and seconary colors. That is the errors are being wrapped!

Add a small constant to the source image, so that the errors remain +ve.

Re: reversible add?

Posted: 2010-10-22T14:19:20-07:00
by foobarbaz
Just to wrap this up.
I have spent some time to actually get to what was going on.
The corruptions were due to behavior of '-negate' operator (or my assumption about it).

For some reason i assumed that X + ~X == 0%, where it is not, and instead, X + ~X == 100% (full range)
So this has nothing to do with compositioning (which behaves as it should and doesnt do anything strange) - just adding '#000100010001' to my resulting image (AA.png) made it exact match with source image (A.png)

Re: reversible add?

Posted: 2010-10-22T18:49:32-07:00
by anthony
This is because negate does not generate negative values but a negative image using positive values. That is negate is equivalent to X = ( 1 - X )

As a result X + negate(X) = 1

This is one reason why LinearBurn (the Photoshop subtraction function) actually requires you
to negate the image to subtract. It then simply adds the image and subtracts 1 to produce the
subtraction result.