[SOLVED]: Composition clipping/clamping

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
LDAsh
Posts: 35
Joined: 2015-04-27T23:10:58-07:00
Authentication code: 6789

[SOLVED]: Composition clipping/clamping

Post 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?
Last edited by LDAsh on 2019-02-10T23:53:03-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Composition clipping/clamping

Post 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
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Composition clipping/clamping

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Composition clipping/clamping

Post 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.
snibgo's IM pages: im.snibgo.com
LDAsh
Posts: 35
Joined: 2015-04-27T23:10:58-07:00
Authentication code: 6789

Re: Composition clipping/clamping

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

Re: Composition clipping/clamping

Post 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.
LDAsh
Posts: 35
Joined: 2015-04-27T23:10:58-07:00
Authentication code: 6789

Re: Composition clipping/clamping

Post by LDAsh »

Sorry, I just edited my post and indeed your suggestion solved the issue. Thanks! :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Composition clipping/clamping

Post 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
LDAsh
Posts: 35
Joined: 2015-04-27T23:10:58-07:00
Authentication code: 6789

Re: [SOLVED]: Composition clipping/clamping

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: [SOLVED]: Composition clipping/clamping

Post 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.
snibgo's IM pages: im.snibgo.com
Post Reply