Page 1 of 1

Rounding error in -compose subtract

Posted: 2010-04-29T20:11:40-07:00
by foxyshadis
Hello, I'm trying to use subtract for a research project. It mostly works quite well, the original image is reconstructed perfectly, but in a few spots huge errors manifest - where it seems to create a difference of 255 in one or more channels. In all other cases I've verified that the difference is zero, so it's not going both ways. I tried both miff and png as intermediate images.

Looking closer, the problem only and always occurs when a channel in the second image is 255, it will be zero'd. If you reverse the order of the arguments, it doesn't happen (in my test image, I have no pixels at 255, it's a blur).

Here are the exact commandlines I used to create this:

composite -compose subtract blur_fc.png mini_fc.png subtestc.miff
composite -compose subtract blur_fc.png subtestc.miff subtest_fromc.png
composite -compose difference mini_fc.png subtest_fromc.png subtest_diffc.png

Here's the test images if you'd like to look at it.

Bug?

Re: Rounding error in -compose subtract

Posted: 2010-04-29T21:00:23-07:00
by fmw42

Re: Rounding error in -compose subtract

Posted: 2010-04-29T21:03:37-07:00
by foxyshadis
I installed the 32-bit, Q8 version, and the bug does not appear. I was originally using the x64 Q16 version. I've just verified that the 32-bit Q16 version is incorrect as well. It looks like this line in composite.c:

Code: Select all

  if (pixel < 0.0)
    pixel+=(QuantumRange+1.0);
It looks like the floating point arithmetic is just far enough off to round incorrectly. I'm not really sure if that should be 1.1 or 0.9 to compensate. My brain is pretty tired at the moment.

Re: Rounding error in -compose subtract

Posted: 2010-04-29T21:04:38-07:00
by foxyshadis
fmw42 wrote:-compose subtract will wrap. try using -compose minus. see http://www.imagemagick.org/Usage/compose/#subtract and http://www.imagemagick.org/Usage/compose/#minus
That's the intent. Check my command lines again - if I used minus, it'll destroy the recreated image.