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

[SOLVED]: Composition clipping/clamping

Post by LDAsh » 2019-02-10T08:37:28-07:00

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

Re: Composition clipping/clamping

Post by snibgo » 2019-02-10T09:05:48-07:00

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: 24920
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Composition clipping/clamping

Post by fmw42 » 2019-02-10T11:37:05-07:00

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

Re: Composition clipping/clamping

Post by snibgo » 2019-02-10T13:18:29-07:00

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

Re: Composition clipping/clamping

Post by LDAsh » 2019-02-10T23:40:29-07:00

"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: 24920
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Composition clipping/clamping

Post by fmw42 » 2019-02-10T23:43:11-07:00

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

Re: Composition clipping/clamping

Post by LDAsh » 2019-02-10T23:45:39-07:00

Sorry, I just edited my post and indeed your suggestion solved the issue. Thanks! :)

User avatar
fmw42
Posts: 24920
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Composition clipping/clamping

Post by fmw42 » 2019-02-10T23:47:11-07:00

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

Re: [SOLVED]: Composition clipping/clamping

Post by LDAsh » 2019-02-11T02:47:23-07:00

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

Re: [SOLVED]: Composition clipping/clamping

Post by snibgo » 2019-02-11T05:27:56-07:00

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