Tone Curve Math Question

Does anyone know how to write a mathematical expression for a curve that transitions from linear to logarithmic at a defined cut point? I.E., if I wanted a curve that boosts linear exposure by 4x (2 stops) but rolls off logarithmically to 1.0 with the transition falling at the point where the linear and logarithmic curves blend smoothly.

I’m assuming it will need to be a conditional if/then expression, such as

if ($x<=[cut point], $x4, log2(a$x+b)/2^a - ???)

The tricky part for me is determining the exact point where the linear and log curves meet and understanding how to scale/position the log portion to blend into the linear portion.

Thanks!

I’m not a math person by any stretch, but functionally should be pretty easy to plot a linear slope to a known point in the log function, and conditionally trap the plot to one or the other based on that point.

The ARIB STD-B67 log-gamma algorithm found here https://en.wikipedia.org/wiki/Hybrid_log%E2%80%93gamma, provides a framework.

Edit: Ah, for a moving EC slope, you’ll need to scooch the log function back and forth, a little more complicated. Also complicated by finding a common slope at the connecting point. I think there’s code in either/both dt and RT to do this for their EC tool…

Thinking further, you’ll need to define a function logstart(ec), which takes the ec and returns a log x,y to which to transition. Thinking even further, I don’t think you’d want to use a straight log function, because as you increase ec, the remaining log curve would be flatter… I just looked at dt/RT manuals, none do this directly, but RT seems to have something similar in Highlight Compression…

Here is my attempt:

If we are looking for a function such that:
f(x) = a\;x, ~~~~~~~~~~~~~~~~~\text{if}~~~x<\tau
f(x) = \log{(b\;x + c)}~~~\text{if}~~~x>=\tau

assuming a and \tau are known. We can determine b and c by considering \mathcal{C}^1 smoothness constraints:

  • f(\tau) \rightarrow a\;\tau = \log{(b\;\tau + c)}, and
  • f^{'}(\tau) \rightarrow a = \frac{b}{b\;\tau + c}

which means: b\;\tau + c = \exp{(a\;\tau)}, and thus:

  • b = a\;\exp{(a\;\tau)}
  • c = \exp{(a\;\tau)}\;(1 - a\;\tau)

And then, your function becomes:
f(x) = a\;x, ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\text{if}~~~x<\tau
f(x) = a\;\tau + \log{(a\;(x-\tau) + 1)}~~~\text{if}~~~x>=\tau


Some examples of function plots:

5 Likes

Does the unbreak profile not do something like this in DT?? Just guessing on this…

There are of course other alternatives, such as:

f(x) = \cases{ a x & $x < \tau$ \\ 1+ (a \tau-1)e^{(ax-a\tau)/(a\tau-1)} & $x \ge \tau$ }

where a > 0 and 0 \le \tau < 1/a. This gives you

This has the benefit of having a maximum value of 1.0.

You may want to check out equalization, normalization or low-light papers. There are plenty of formulae there.

It’s actually quite fun to find curves like this :stuck_out_tongue:

This one is similar to the one above, but bounded by [0,1] on the input range and output domain, and has the exact values of (0,0) and (1,1):

f(x) = \cases{ a x & $x < \tau$ \\ 1+ (a \tau-1)\left(\frac{x-1}{\tau-1}\right)^{(a\tau-a)/(a\tau-1)} & $x \ge \tau$ }

where a \ge 1 and 0 \le \tau < 1/a. This gives you

1 Like

I share that sickness; my problem is my math skills are crap. I’m like a fool on a motorcycle in the woods, hitting trees… :crazy_face:

I think I’m going to add this one to my exposure comp tool, a nicely-shaped highlight rolloff…

Thanks :slight_smile: Be careful for the edge-case where a=1 and \tau=1 (unity transform), because that leads to some divisions by 0.

1 Like

Be careful for the edge-case where a=1 and τ=1…

That won’t arise if the first condition is (x <= τ) instead of (x < τ). In other words, just swap the position of “=”. The result should be the same.

2 Likes

The equation at pag.37 looks good, I’ve never tested it
https://www.itu.int/dms_pub/itu-r/opb/rep/R-REP-BT.2407-2017-PDF-E.pdf

1 Like

Here’s another rather ridiculous one to play with: Graphing Calculator

f(x) = \cases{\frac{x}{t}\frac{u+(n-1)(u-t)}{1+(n-1)(u-t)}\left(\frac{t}{nu-(n-1)t}\right)^{1/n} & $x\le t$\\ \frac{u+(n-1)(u-t)}{1+(n-1)(u-t)}\left(\frac{nx-(n-1)t}{nu-(n-1)t}\right)^{1/n} & $t<x<u$\\ \frac{x+(n-1)(u-t)}{1+(n-1)(u-t)} & $x \ge u$ }

This one starts and ends with a linear segment, connected by a 1/n power term.

Arghh!!! Yer’ killing me here!!! :laughing:

I started a rolloff branch in rawproc, and I’m playing with your penultimate function. UI-wise, needs preparatory math to allow a simple 1-10 slider to feed it; not sure where I’d start with u and n…

Speaking of rawproc, it’s improved a lot since I last used it. I only wish there was an “add g’mic code” step… :smiley:

1 Like

I’ve thought about such a tool intermittently; I really should do it because G’MIC was really the inspiration for rawproc’s toolchain organization. I guess it could be as simple as what I do for groups: just provide a box to type stuff in, and then the marshalling code to put my image array into something G’MIC will digest.

Now, rawproc internal representation is 0.0-1.0; would you be willing to surround your code with conversion ops to translate to 0.0-255.0 and back to 0.0-1.0?

1 Like

G’MIC doesn’t care about ranges. It is just a math tool with image processing capabilities. You could have options that decide what to do pre and post filter. E.g. normalize range to 0-255 or 0-1 and how much to clip.

Absolutely! In all seriousness, this would be an incredibly useful feature. Prototyping work with raws is not the easiest thing without the required pre-processing. I sometimes work on the bayer side and sometimes after it. It makes perfect sense to test an idea in a high level g’mic script before writing some C.

Allrighty then… I’ll start a gmic branch this weekend, probably after I merge the highlight rollout branch.

6 Likes

If you have any question about how gmic works, just let us know, we’d be happy to help.

2 Likes