Page 1 of 1

[SOLVED]: Composition clipping/clamping

Posted: 2019-02-10T08:37:28-07:00
by LDAsh
I'm having trouble using "compose", particularly "subtract" with 2 images. It seems that when black is subtracted from black, it cycles those pixels through to white again. In Paint Shop Pro, I get the same result in the "arithmetic" (subtract) method by unchecking the "clip color values" option. Otherwise there is nothing lower than 0,0,0. Unchecking that option seems to mean "0-1=255", if you get what I mean.

So I believe it's either "clip" or "clamp" that I need in the command-line, but after so much experimenting with different orders, nothing is fixing this issue. Maybe it should be "-set option:"? Which order should the command be?

Re: Composition clipping/clamping

Posted: 2019-02-10T09:05:48-07:00
by snibgo
What version of IM? On what platform? What is your exact command?

I can't reproduce your problem, with IM v7.0.7-28 on Windows 8.1. Black minus black is black.

Code: Select all

f:\web\im>%IMG7%magick xc:Black xc:Black -compose Subtract -composite txt:
# ImageMagick pixel enumeration: 1,1,65535,srgb
0,0: (0,0,0)  #000000000000  black

f:\web\im>%IMG7%magick xc:Black xc:Black -compose Minus -composite txt:
# ImageMagick pixel enumeration: 1,1,65535,srgb
0,0: (0,0,0)  #000000000000  black

Re: Composition clipping/clamping

Posted: 2019-02-10T11:37:05-07:00
by fmw42
In place of using -compose subtract (-compose modulus subtract), use -compose minus. The first wraps around, the second is automatically clamped. See https://imagemagick.org/Usage/compose/#math

Re: Composition clipping/clamping

Posted: 2019-02-10T13:18:29-07:00
by snibgo
There should be no wrapping or clipping if the result of the operation is in the range 0% to 100% inclusive. The result of 0% minus (or subtract) 0% should be 0%.

I suspect the OP isn't really subtracting black from black.

Re: Composition clipping/clamping

Posted: 2019-02-10T23:40:29-07:00
by LDAsh
"Version: ImageMagick 6.9.0-1 Q16 x86 2014-12-22"
I need to use this version because it's the latest that my XP single-core machine will run, as far as I can remember.
"convert img.png ref.png -compose subtract -composite -negate out.png"
I still suspect I need "-set option:compose:clamp=on" (or is it 'true'?) but all combinations I've tried are failing to fix this. I would have assumed this would be the default anyway.
I've uploaded everything here:-
http://www.violae.net/temp/compose_clamp.zip
_______________________

Ah, I tried Fred's suggestion again and this time I swapped the order of the images processed, and it worked perfectly! Thanks so much! :lol:
"convert ref.png img.png -compose minus -composite out.png"

Re: Composition clipping/clamping

Posted: 2019-02-10T23:43:11-07:00
by fmw42
Your linked file does not work for me. I get a 404 error. Also have you tried -compose minus as I mentioned above. Is your IM version HDRI compiled. If so, then you might need -clamp. If not, then you should not, especially if you use -compose minus.

Re: Composition clipping/clamping

Posted: 2019-02-10T23:45:39-07:00
by LDAsh
Sorry, I just edited my post and indeed your suggestion solved the issue. Thanks! :)

Re: Composition clipping/clamping

Posted: 2019-02-10T23:47:11-07:00
by fmw42
Yes, with -compose minus or divide, you need to swap the order of the images.

convert img1 img2 -compose minus -composite result

is like img2-img1. So I always add +swap as

convert img1 img2 +swap -compose minus -composite result

to achieve img1-img2.

see https://imagemagick.org/Usage/compose/#math with regard to src and dst

Re: [SOLVED]: Composition clipping/clamping

Posted: 2019-02-11T02:47:23-07:00
by LDAsh
Oops, a new side-issue surfaced from this, I only just realised...

When using an image that is a different proportion, the output is the larger image. Strangely this is different from using "subtract", "add" or "darken", it only happens with "minus". I tried using "+swap" as suggested, but the same happens. I'm not doing anything different from the other commands so it's really confusing and I can only assume "minus" particularly (in this version, at least) is broken?

(EDIT: I'm going to start a new topic about a possible workaround because I want to know how to do it that way anyway...)
viewtopic.php?f=1&t=35480

Re: [SOLVED]: Composition clipping/clamping

Posted: 2019-02-11T05:27:56-07:00
by snibgo
The output from "-composite" will be the same size as the first of its inputs.

I suggest you consider using "-compose MinusSrc" or "-compose MinusDst" as appropriate.