reversible add?

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?".
Post Reply
foobarbaz

reversible add?

Post 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?
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: reversible add?

Post 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
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
foobarbaz

Re: reversible add?

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: reversible add?

Post 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.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: reversible add?

Post 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
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: reversible add?

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: reversible add?

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
foobarbaz

Re: reversible add?

Post 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)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: reversible add?

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply