The long question about tone curves in RawTherapee

I use RawTherapee 5.9 for DNG file processing and DCamProf for creation DCP profiles that are used in RawTherapee Color Management.

Usually I created the tone curve in RawTherapee and copy it to the DCP profile and it was okay.
However, recently I decided to divide the tone curve that I used to two separate curves, one of them put to the profile and another one - left in RawTherapee.

So task that I have is to divide tone curve to two separate tone curves that give me the initial curve when I sum them (probably you correct me here, but as I understand the algorithm the DCP tone curve was applied on the conversion step and Exporsure tone curve applied to the result of conversion).

On the first step I decided to simplify my task, as RawTherapee has two curves in Exposure.
For the testing purpose I took Standard Film Curve - ISO Low from RawTherapee Bundled Profiles. So the parameters of curve are:

Spline
0          0
0.11       0.09
0.32       0.43
0.66       0.87
1          1

The curve the curve I want to subtract from it has next parameters:

Spline
0              0
0.0702614      0.0604575
0.276611       0.330544
0.599693       0.67976
1              1

I decided that it might be good to use Python or something like that to calculate second curve. So, please see the code below:

# import some libs
import numpy as np
import matplotlib.pyplot as plt

# the first spline points
x1 = np.array([0,0.11,0.32,0.66,1])
y1 = np.array([0,0.09,0.43,0.87,1])

# the second spline points
x2 = np.array([0,0.0702614,0.276611,0.59969,1])
y2 = np.array([0,0.0604575,0.330544,0.67976,1])

Now I need some info about how the spline in RawTherepee (and DCamProf) was build.
(Unfortunately I am not so good in C to get this information from the sources)

As I currently don’t have this information, I supposed that it’s probably interpolation with degree = 3 (cubic spline):

# The degree of the polynomial for interpolation (cubic spline)
degree = 3

# let's get some coefficients
coefficients1 = np.polyfit(x1, y1, degree)
coefficients2 = np.polyfit(x2, y2, degree)

# and find the difference
coefficients_difference = coefficients1 - coefficients2

# Probably now we need to evaluate the results
# Define the desired range for x and y
x_min = 0
x_max = 1
y_min = 0
y_max = 1

# Create x values from 0 to 1
x_values = np.linspace(x_min, x_max, 100)
# print(x_values)

#result difference spline
y_values_difference = np.polyval(coefficients_difference, x_values)

# Calculate the corresponding y-values for each spline
y_values_spline1 = np.polyval(coefficients1, x_values)
y_values_spline2 = np.polyval(coefficients2, x_values)

#result difference spline
y_values_difference = np.polyval(coefficients_difference, x_values)

# and now we need to add the current x-value to each element of y_values_difference
y_values_difference_with_x = y_values_difference + x_values

I do this because I need to get the points for the result spline, so:

# Calculate the corresponding y-values for the difference spline at x1 points (same points as the first spline)
y_values_difference_with_x1 = np.polyval(coefficients_difference, x1) + x1

So, here the results that I’ve got:

The initial Standard Film Curve - ISO Low spline is red, the spline that was subtracted is blue, the result spline is orange.
And points that I’ve got:

# prepare for printing
# np.set_printoptions(formatter={'float': lambda x: "{0:0.3f}".format(x)})
np.set_printoptions(formatter={'float':"{0:0.5f}".format})
print("", x1, "\n", y_values_difference_with_x1)
 [0.00000 0.11000 0.32000 0.66000 1.00000] 
 [-0.00253 0.10071 0.35891 0.79482 0.99909]

To compare, the initial Standard Film Curve - ISO Low spline in RawTherapee:

So I have several questions:

  1. Is it possible to get any info about the math that used to build the splines in RawTherapee?
    (It is obvious that even initial spline (red) doesn’t match with spline in RawTherapee. Furhtermore red spline go above 1.0). So what algorithm was used in RawTherapee?

  2. Can anybody recommend something to resolve my task in easy way?

  3. If anybody has any information about DCamProf, if it use the same approach to build the splines as a RawTherapee?

  4. RawTherapee has several algorithms that might be used for Tone Curve (Standard, Flexible, Parametric, Control Cage) all of them have features, sometimes it is better to use one, sometimes another is it possible to divide for example the Standard curve between Control Cage and Standard, or between Parametric and Standard?
    (as RawTherapee has Film-like, Perceptual, Saturation and Value Blending, etc. algorithms, it might be used for interesting color effects, so it will be possible to blend that algorithm based on two tone curves, change colors but get exactly the same resulting tone curve)

It was a little bit long, sorry, but I wanted to describe the problem.

Thank you for any help and any information!

Best Regards,
Dmitry

While not a specific description of the algorithm, it’s worth noting that Anders discusses the application of linear-departure tone curves in camera profiles in the dcamprof documentation:

http://rawtherapee.com/mirror/dcamprof/dcamprof.html#tone_curves

Another dcamprof discussion that might come closer to your application is here:

http://rawtherapee.com/mirror/dcamprof/camera-profiling.html#step8

Frankly, I’m wondering about what you’re trying to accomplish. In the grand scheme of raw processing, there are usually two tone curves: 1) applied by the software itself, e.g., filmic and its ilk, and 2) the tone curve applied by the export or display profile, particular to the rendition medium. Those two are cumulative upon the initial raw data; since only the first one is where you get user discretion, you need to be careful of what it feeds to the export/display transform.

A regular transfer-function tone curve can be separated as additive terms to the original curve, that is, if at some point N on the original curve,

f(Nc1) + f(Nc2) = f(Norig)

Where you decide to cleave f(No) for f(Nc1) and f(Nc2) should have some semantic importance, what is it?

1 Like

Yes it is cubic.

Used by dcraw.c:

1 Like

These will not be the same…if given the way modules are applied and processed, then a curve in the DCP and then a second curve won’t produce the same result as those two curves entered at the tone curve stage will they… Just asking??

My guess is it matters what comes in between if anything.

1 Like

Thank you Richard!!!

The code you provided helped a lot!!!
Now I have my curves:

I believe it should work at least for two curves in RawTherapee :grinning:

Here the Python code, if it interesting for anybody:

from scipy.interpolate import CubicSpline

# as data I used the same x1, y1, x2, y2 - see the initial post
# x1, y1 - the data for arc.rtc curve that was used in DCamProf
# x2, y2 - the arbitrary curve I want to subtract from the first one

# Normalize data to the range [0, 1]
x1_norm = x1 / x1[-1]
y1_norm = y1 / y1[-1]
x2_norm = x2 / x2[-1]
y2_norm = y2 / y2[-1]

spline1 = CubicSpline(x1_norm, y1_norm)
spline2 = CubicSpline(x2_norm, y2_norm)

# Evaluate the spline at some specific points
x_out = np.linspace(0, 1, 100)  # Generate 100 points from 0 to 1
y1_out = spline1(x_out)
y2_out = spline2(x_out)

# Subtract the second spline from the first spline (and add x values)
y_diff = (y1_out - y2_out)+ x_out

# Plot
plt.figure(figsize=(8, 8))

# Plot the central diagonal line
plt.plot([0, 1], [0, 1], color='black', linestyle='--', label='Central Line')

# Plot the original data points
plt.scatter(x1_norm, y1_norm, color='red', label='Original Data Points 1')
plt.scatter(x2_norm, y2_norm, color='blue', label='Original Data Points 2')

# Plot the cubic spline interpolations
plt.plot(x_out, y1_out, label='Cubic Spline 1', color='red')
plt.plot(x_out, y2_out, label='Cubic Spline 2', color='blue')

# Plot the difference between the two splines
plt.plot(x_out, y_diff, label='Difference (Spline 1 - Spline 2)', color='green')

# Add labels and title
plt.xlabel('x')
plt.ylabel('y')
plt.title('Cubic Spline Interpolations and Difference')
plt.legend()

# Set the grid with step size 0.1
plt.xticks(np.arange(0, 1.1, 0.1))
plt.yticks(np.arange(0, 1.1, 0.1))

# Show the plot
plt.grid(True)
plt.show()

So now I can play a little bit with two curves in RawTherapee and then try to go to DComProf

1 Like

Hi Glenn,

I think you right, probably I have to describe the problem that I have and what I want to accomplish and probably anybody can give me advice.

I have the Pentax camera K1 Mark II. It isn’t very new camera, but I like it and it’s more than enough for me. For years I shoot to JPEG and it was pretty good. But once my wife asked me why the skin-tones are so red. I looked again and realized that she is right, I checked the colors from other Pentax cameras in the web and yes, the colors was too cold and skin-tones are also too cold, too red (don’t know why, but looks like it’s Pentax color specific). So, I decided to shoot to DNG to be able to fix it. As a result I choosed RawTherape (because of functionality and interface, I also like the idea to use Processing profiles).

The problem is the Camera standard Input Profile in RawTherapee is just awful. Color on the photo - are not a real colors and also not the colors that I had in Pentax jpeg - just awful.

So I used the DCamProf to generate my own DCP profile. I played with settings a little bit to do the specific look, but after some experiments decided not to do it and stopped on the simple profile, without specific look and with tone curve. I used arc curve from DCamProf, because of

Anyway, in order to make our profile fit for all-around photography we need to have an S-shaped tone curve to apply some contrast. So which shape to choose? With Adobe Camera Raw and the DNG reference code comes a standard curve which is used in many of Adobe’s own profiles and it’s also available as a built-in choice in DCamProf. It will provide a good result and if you don’t have some special need I suggest using that (-t acr provided to make-dcp).
Making a camera profile with DCamProf

That profile provided much better results than Camera standard Input Profile from RawTherapee, and I used it for a long time. I tend to have one all-purpose DCP profile for daylight and do some adjustments in RawTherapee, if necessary.

However, with time I found that that profile with acr curve has one problem - light clipping. So I modified the curve - to made it more flatter in Highlights. I use the DCP profile with this curve now and in general it’s pretty good, but…

But I found that despite the tone curve in DCP profile has less color-shift problems than for example Film curve in RawTherapee, the color-shift problem still exist. For example the green tend to be more yellow and more vivid, especially in lights. Actually the effect described here: Exposure - RawPedia , for Film-like in RawTherapee it less than for Standard, and for curve in DCP it’s less than for Film-like but still exists. I tried Perceptual, but it usually make colors too saturated.

Usually I decrease Chromaticity in Lab* and try to compensate the problems with highlights through masks in ColorToning/Color correction regions. But recently I tried the Saturation and Value Blending mode for Highlights and found that it works very good.

So now I have two ideas to try:

  1. To build the DCP profile with linear curve and than to cleave my curve to Tone curve 1 and Tone curve 2,
    Use Tone curve1 for Shadows, Darks and less for lights with Perceptual or Film-like mode. Use Tone curve 2 for Lights and Highlights with Saturation and Value Blending mode.
    The Python script that I tried to develop is necessary tool for this task, because if anybody try to do it without math he will obviously encounter some rather unusual effects, as we can’t imagine exactly how the two splines should go in order for them to form the curve we want.

  2. To try to use this approach with DCP tone curve. So in this case I’ll use curve in DCP profile instead of Tone Curve 1 and will use only one curve in RawTherapee, in Saturation and Value Blending mode for Lights and Highlights

I’ll really appreciate any advice about it!

What does the dcp from adobe provide for you …is it flawed as well?? For some camera’s they have more than one as well …

EDIT: As it turns out there are several…you might find the look table and base table data will account for the color issues you have… worth looking at assuming you have not…

Ah, now I get it…

FWIW, I prefer to keep my camera transform just doing color (that is, linear), so I’d prefer Approach #1…

Thank you, Todd!

I would say that the profiles that were generated with DCamProf are better than Pentax profiles from Adobe, I compared them several years ago when encountered with this question at firs time (it’s just my opinion of course). Looks like DCamProf do the really good work.

The problem I am talking about is pretty tiny, and I would say most photos that depict the same scenes and that was converted with Lightroom, for example, have the same problem, so it’s rather my perfectionism :grinning:

However, probably it might be possible to reduce that negative effect by splitting the Tone curve by two.

What is your opinion about this idea?

1 Like

Does the K-1 II not have a “auto matched camera profile”? I use the OG K-1 and have settled on the auto matched without tone curve and look table checked.

I suspect that the K-1 and K-1 II should work well with the same profiles. For my K-3 II i modified the “auto matched” K-3 profile and replaced the camera model only with good results.

Part of me actually like the Pentax colours (Natural) more but the hue shift of reds are to much and quickly recognized as “wrong” for many of my subjects.

Excuse me Richard,

Is it possible to ask you about some links to code for Parametric and Control cage curves implementation?
If you know something about descriptions of algorithms that were used for that two curves (in Wikipedia or anywhere else) it also might be very helpful.

Thank you in advance!
Dmitry

As I understand RawTherapee doesn’t have “auto matched camera profile” for Pentax K1 MarkII, please see the screenshot below:

I know that I can rename my profile, or any other profile and it would match, but by default it was no matched profile, and RawTherapee choosed “Camera standard”.

Probably I’ve missed something, so please correct me if I am wrong.

You’re right looks like it’s missing.

Thing is a simple rename didn’t do the trick for me. I’m pretty sure i used some command line tool to convert the dcp to json → change the camera string → convert back to dcp.

The Pentax profiles handle highlights very nicely particularly skin and whites. The auto matched one leaves them to saturated but I use it none the less. It’s a look thing I guess. It’s likely that the saturated look is more correct but less appealing.

If you shoot DNG I think RT will use the embedded profile. Unfortunately it’s all a bit unclear and the UI doesn’t actually say which one is active.
2023-08-04-235900_2560x1440_scrot

Using art you can explicitly switch between embedded and camera standard with visible difference.

To confirm you can actually use the DNG as a DCP file in RT. Just select the file as its own DCP. This should confirm the embedded profile is being used and you could see if it look different… Also the adobe profile editor is a nice tool and could easily tweak the red issue I think… and I also think you can add custom curves there…

Edit

Showing another way of using it by modifying the matrix and using a color checker… then you could tweak the individual patches to suit your needs as well…

Note I am suggesting this to work on DCP files for RT or Art not as an advert for Adobe…

I don’t use RawTherapee, but I’ve read up on the auto-matched tone curve tool and what it needs to do its thing is an embedded JPEG from which to calculate the difference between it and the raw data (cumulative distribution function of the two histograms). Do those cameras produce raws without embedded JPEGs?

Glen I think that is different than the automatched curve selection in color management in RT… if I recall correctly…one is the automatched tone curve and one is the automatched color profile… I could be mistaken but I think this is accurate

Not sure about the tone curves… it would be interesting to look at how the adobe vs Dcamprof profiles are displayed in the vector scope to see how the colors are shifted differently … I guess there are fancy ways to could compare colorchecker patches between the profiles…

Oh cripes, auto-matched camera profile. I’m lips-deep in resurrecting my office/workshop, need to not just dip in and out of the fora…

1 Like

:exploding_head: