Blender AgX in darktable (proof of concept)

I’ve been playing with the source code of the AgX module, mainly to learn about the 18%->18% mapping we discussed earlier. I can see what @kofa meant, it is indeed difficult to make the plugin memorize the current brightness level. Or rather, it looks deceptively easy but the straightforward solutions don’t work. For example, just using the current brightness value:

  dt_iop_agx_params_t params_with_mid_gray = *p;
  // params_with_mid_gray.curve_pivot_y_linear_output = 0.18f;
  // params_with_mid_gray.curve_pivot_x_shift_ratio = 0.f;

fails when selecting bright or dark parts of the picture - estimation of the output brightness level becomes then very inaccurate. I’ll try to experiment with other ideas.

But I noticed something else - the target_pivot_xshift conversion is non-linear (piece-wice linear). That is, shift=-1 and shift=1 are at different distances from the mid-gray exposure.

Does it matter at all? In most cases probably not - I’ve only noticed that when reading the code. But maybe it does when white/black relative exposure settings are very imbalanced?

As far as I understand, it originates from the desire of having the pivot input shift slider with a 0 in the middle and with a [-1:1] range. But is it really required? If either the range or the mid-gray point could be moved we could have a linear conversion between the pivot shift and pivot exposure.

If range [-1:1] is necessary and the mid-point shift has to be at 0, perhaps AgX could use a smooth (quadratic) curve instead of a PWL? For example, this equation smoothly interpolates between coordinates [-1, 0], [0, base_pivot_x], [1, 1]:

target_pivot_x = -((2*base_pivot_x-1)/2)*shift^2+1/2*shift+base_pivot_x