I think clamping after the blur makes no difference. The important part is that (O-GB) may be negative, so that mustn't be clamped. "-compose Mathematics" works pixel-by-pixel, with each calculation in floating point, though each result here is then written to Q16 integer.
fmw42 wrote:My guess is that the third term is getting parts missed by clamping O-GB.
Yes.
The formula USM='O+(O-GB)-inv(O+inv(GB))' seems to have come from Gimp, which I think records pixel values as 0..255 only. So, when O<GB, (O-GB) is clamped to zero. To compensate, we need to subtract something, and that's what "-inv(O+inv(GB))" is for.
For example, suppose O=33 and GB=44.
USM = 33 + (33-44) - inv(33+inv(44))
Negative values are clamped to zero, and inv(X) = 255-X, so
USM = 33 + (0) - (255- (33+(255-44)))
= 33 - (255 - (33 + (211)))
= 33 - (255 - 244)
= 33 - 11
= 22.
We get the same result from USM = 2*O-GB = 2*33-44 = 66-44 = 22.
Internally, the IM USM takes a longer route because it uses (O-GB) for the threshold:
USM = O + (O-GB) = 33 + (33-44) = 33 + (-11) = 22.
Internally, (O-GB) is stored as a floating point.
fmw42 wrote:Nevertheless, when I computed O+(O-GB) in my script above using clamp=off in HDRI, it did not work.
I'm unsure which of your scripts you mean. When you use "-evaluate-sequence add" to add O and GB and the third term, all three terms are always positive, so you need to
subtract the third term.