1 /*
2     This file is part of darktable,
3     Copyright (C) 2009-2021 darktable developers.
4 
5     darktable is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9 
10     darktable is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with darktable.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21 #include <assert.h>
22 #include <math.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "bauhaus/bauhaus.h"
28 #include "common/opencl.h"
29 #include "common/colorspaces_inline_conversions.h"
30 #include "common/rgb_norms.h"
31 #include "control/control.h"
32 #include "develop/develop.h"
33 #include "develop/imageop.h"
34 #include "develop/imageop_math.h"
35 #include "develop/imageop_gui.h"
36 #include "dtgtk/drawingarea.h"
37 #include "dtgtk/paint.h"
38 #include "gui/draw.h"
39 #include "gui/gtk.h"
40 #include "gui/presets.h"
41 #include "gui/color_picker_proxy.h"
42 #include "gui/accelerators.h"
43 #include "iop/iop_api.h"
44 #include "libs/colorpicker.h"
45 
46 #define DT_GUI_CURVE_EDITOR_INSET DT_PIXEL_APPLY_DPI(1)
47 #define DT_GUI_CURVE_INFL .3f
48 
49 #define DT_IOP_TONECURVE_RES 256
50 #define DT_IOP_TONECURVE_MAXNODES 20
51 
52 DT_MODULE_INTROSPECTION(5, dt_iop_tonecurve_params_t)
53 
54 static gboolean dt_iop_tonecurve_draw(GtkWidget *widget, cairo_t *crf, gpointer user_data);
55 static gboolean dt_iop_tonecurve_motion_notify(GtkWidget *widget, GdkEventMotion *event, gpointer user_data);
56 static gboolean dt_iop_tonecurve_button_press(GtkWidget *widget, GdkEventButton *event, gpointer user_data);
57 static gboolean dt_iop_tonecurve_leave_notify(GtkWidget *widget, GdkEventCrossing *event, gpointer user_data);
58 static gboolean dt_iop_tonecurve_enter_notify(GtkWidget *widget, GdkEventCrossing *event, gpointer user_data);
59 static gboolean dt_iop_tonecurve_key_press(GtkWidget *widget, GdkEventKey *event, gpointer user_data);
60 
61 
62 typedef enum tonecurve_channel_t
63 {
64   ch_L = 0,
65   ch_a = 1,
66   ch_b = 2,
67   ch_max = 3
68 } tonecurve_channel_t;
69 /*
70 typedef enum dt_iop_tonecurve_preservecolors_t
71 {
72   DT_TONECURVE_PRESERVE_NONE = 0,
73   DT_TONECURVE_PRESERVE_LUMINANCE = 1,
74   DT_TONECURVE_PRESERVE_LMAX = 2,
75   DT_TONECURVE_PRESERVE_LAVG = 3,
76   DT_TONECURVE_PRESERVE_LSUM = 4,
77   DT_TONECURVE_PRESERVE_LNORM = 5,
78   DT_TONECURVE_PRESERVE_LBP = 6,
79 } dt_iop_tonecurve_preservecolors_t;
80 */
81 typedef struct dt_iop_tonecurve_node_t
82 {
83   float x;
84   float y;
85 } dt_iop_tonecurve_node_t;
86 
87 typedef enum dt_iop_tonecurve_autoscale_t
88 {
89   DT_S_SCALE_AUTOMATIC = 1,        /* automatically adjust saturation based on L_out/L_in
90                                       $DESCRIPTION: "Lab, linked channels" */
91   DT_S_SCALE_MANUAL = 0,           /* user specified curves
92                                       $DESCRIPTION: "Lab, independent channels" */
93   DT_S_SCALE_AUTOMATIC_XYZ = 2,    /* automatically adjust saturation by
94                                       transforming the curve C to C' like:
95                                       L_out=C(L_in) -> Y_out=C'(Y_in) and applying C' to the X and Z
96                                       channels, too (and then transforming it back to Lab of course)
97                                       $DESCRIPTION: "XYZ, linked channels" */
98   DT_S_SCALE_AUTOMATIC_RGB = 3,    /* similar to above but use an rgb working space
99                                       $DESCRIPTION: "RGB, linked channels" */
100 } dt_iop_tonecurve_autoscale_t;
101 
102 // parameter structure of tonecurve 1st version, needed for use in legacy_params()
103 typedef struct dt_iop_tonecurve_params1_t
104 {
105   float tonecurve_x[6], tonecurve_y[6];
106   int tonecurve_preset;
107 } dt_iop_tonecurve_params1_t;
108 
109 // parameter structure of tonecurve 3rd version, needed for use in legacy_params()
110 typedef struct dt_iop_tonecurve_params3_t
111 {
112   dt_iop_tonecurve_node_t tonecurve[3][DT_IOP_TONECURVE_MAXNODES]; // three curves (L, a, b) with max number
113                                                                    // of nodes
114   int tonecurve_nodes[3];
115   int tonecurve_type[3];
116   int tonecurve_autoscale_ab;
117   int tonecurve_preset;
118 } dt_iop_tonecurve_params3_t;
119 
120 typedef struct dt_iop_tonecurve_params4_t
121 {
122   dt_iop_tonecurve_node_t tonecurve[3][DT_IOP_TONECURVE_MAXNODES]; // three curves (L, a, b) with max number
123                                                                    // of nodes
124   int tonecurve_nodes[3];
125   int tonecurve_type[3];
126   int tonecurve_autoscale_ab;
127   int tonecurve_preset;
128   int tonecurve_unbound_ab;
129 } dt_iop_tonecurve_params4_t;
130 
131 typedef struct dt_iop_tonecurve_params_t
132 {
133   dt_iop_tonecurve_node_t tonecurve[3][DT_IOP_TONECURVE_MAXNODES]; // three curves (L, a, b) with max number
134                                                                    // of nodes
135   int tonecurve_nodes[3];
136   int tonecurve_type[3]; // $DEFAULT: MONOTONE_HERMITE
137   dt_iop_tonecurve_autoscale_t tonecurve_autoscale_ab; //$DEFAULT: DT_S_SCALE_AUTOMATIC_RGB $DESCRIPTION: "color space"
138   int tonecurve_preset; // $DEFAULT: 0
139   int tonecurve_unbound_ab; // $DEFAULT: 1
140   dt_iop_rgb_norms_t preserve_colors; // $DEFAULT: DT_RGB_NORM_AVERAGE $DESCRIPTION: "preserve colors"
141 } dt_iop_tonecurve_params_t;
142 
143 typedef struct dt_iop_tonecurve_gui_data_t
144 {
145   dt_draw_curve_t *minmax_curve[3]; // curves for gui to draw
146   int minmax_curve_nodes[3];
147   int minmax_curve_type[3];
148   GtkBox *hbox;
149   GtkDrawingArea *area;
150   GtkSizeGroup *sizegroup;
151   GtkWidget *autoscale_ab;
152   GtkNotebook *channel_tabs;
153   GtkWidget *colorpicker;
154   GtkWidget *interpolator;
155   tonecurve_channel_t channel;
156   double mouse_x, mouse_y;
157   int selected;
158   float draw_xs[DT_IOP_TONECURVE_RES], draw_ys[DT_IOP_TONECURVE_RES];
159   float draw_min_xs[DT_IOP_TONECURVE_RES], draw_min_ys[DT_IOP_TONECURVE_RES];
160   float draw_max_xs[DT_IOP_TONECURVE_RES], draw_max_ys[DT_IOP_TONECURVE_RES];
161   float loglogscale;
162   int semilog;
163   GtkWidget *logbase;
164   GtkWidget *preserve_colors;
165 } dt_iop_tonecurve_gui_data_t;
166 
167 typedef struct dt_iop_tonecurve_data_t
168 {
169   dt_draw_curve_t *curve[3];     // curves for pipe piece and pixel processing
170   int curve_nodes[3];            // number of nodes
171   int curve_type[3];             // curve style (e.g. CUBIC_SPLINE)
172   float table[3][0x10000];       // precomputed look-up tables for tone curve
173   float unbounded_coeffs_L[3];   // approximation for extrapolation of L
174   float unbounded_coeffs_ab[12]; // approximation for extrapolation of ab (left and right)
175   int autoscale_ab;
176   int unbound_ab;
177   int preserve_colors;
178 } dt_iop_tonecurve_data_t;
179 
180 typedef struct dt_iop_tonecurve_global_data_t
181 {
182   float picked_color[3];
183   float picked_color_min[3];
184   float picked_color_max[3];
185   float picked_output_color[3];
186   int kernel_tonecurve;
187 } dt_iop_tonecurve_global_data_t;
188 
189 
name()190 const char *name()
191 {
192   return _("tone curve");
193 }
194 
default_group()195 int default_group()
196 {
197   return IOP_GROUP_TONE | IOP_GROUP_GRADING;
198 }
199 
flags()200 int flags()
201 {
202   return IOP_FLAGS_SUPPORTS_BLENDING | IOP_FLAGS_ALLOW_TILING;
203 }
204 
default_colorspace(dt_iop_module_t * self,dt_dev_pixelpipe_t * pipe,dt_dev_pixelpipe_iop_t * piece)205 int default_colorspace(dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
206 {
207   return iop_cs_Lab;
208 }
209 
description(struct dt_iop_module_t * self)210 const char *description(struct dt_iop_module_t *self)
211 {
212   return dt_iop_set_description(self, _("alter an image’s tones using curves"),
213                                       _("corrective and creative"),
214                                       _("linear or non-linear, Lab, display-referred"),
215                                       _("non-linear, Lab"),
216                                       _("non-linear, Lab, display-referred"));
217 }
218 
legacy_params(dt_iop_module_t * self,const void * const old_params,const int old_version,void * new_params,const int new_version)219 int legacy_params(dt_iop_module_t *self, const void *const old_params, const int old_version,
220                   void *new_params, const int new_version)
221 {
222   if(old_version == 1 && new_version == 5)
223   {
224     dt_iop_tonecurve_params1_t *o = (dt_iop_tonecurve_params1_t *)old_params;
225     dt_iop_tonecurve_params_t *n = (dt_iop_tonecurve_params_t *)new_params;
226 
227     // start with a fresh copy of default parameters
228     // unfortunately default_params aren't inited at this stage.
229     *n = (dt_iop_tonecurve_params_t){ { { { 0.0, 0.0 }, { 1.0, 1.0 } },
230                                         { { 0.0, 0.0 }, { 0.5, 0.5 }, { 1.0, 1.0 } },
231                                         { { 0.0, 0.0 }, { 0.5, 0.5 }, { 1.0, 1.0 } } },
232                                       { 2, 3, 3 },
233                                       { MONOTONE_HERMITE, MONOTONE_HERMITE, MONOTONE_HERMITE },
234                                       1,
235                                       0,
236                                       1 };
237     for(int k = 0; k < 6; k++) n->tonecurve[ch_L][k].x = o->tonecurve_x[k];
238     for(int k = 0; k < 6; k++) n->tonecurve[ch_L][k].y = o->tonecurve_y[k];
239     n->tonecurve_nodes[ch_L] = 6;
240     n->tonecurve_type[ch_L] = CUBIC_SPLINE;
241     n->tonecurve_autoscale_ab = 1;
242     n->tonecurve_preset = o->tonecurve_preset;
243     n->tonecurve_unbound_ab = 0;
244     n->preserve_colors = 0;
245     return 0;
246   }
247   else if(old_version == 2 && new_version == 5)
248   {
249     // version 2 never really materialized so there should be no legacy history stacks of that version around
250     return 1;
251   }
252   else if(old_version == 3 && new_version == 5)
253   {
254     dt_iop_tonecurve_params3_t *o = (dt_iop_tonecurve_params3_t *)old_params;
255     dt_iop_tonecurve_params_t *n = (dt_iop_tonecurve_params_t *)new_params;
256 
257     memcpy(n->tonecurve, o->tonecurve, sizeof(n->tonecurve));
258     memcpy(n->tonecurve_nodes, o->tonecurve_nodes, sizeof(n->tonecurve_nodes));
259     memcpy(n->tonecurve_type, o->tonecurve_type, sizeof(n->tonecurve_type));
260     n->tonecurve_autoscale_ab = o->tonecurve_autoscale_ab;
261     n->tonecurve_preset = o->tonecurve_preset;
262     n->tonecurve_unbound_ab = 0;
263     n->preserve_colors = 0;
264     return 0;
265   }
266   else if(old_version == 4 && new_version == 5)
267   {
268     dt_iop_tonecurve_params4_t *o = (dt_iop_tonecurve_params4_t *)old_params;
269     dt_iop_tonecurve_params_t *n = (dt_iop_tonecurve_params_t *)new_params;
270 
271     memcpy(n->tonecurve, o->tonecurve, sizeof(dt_iop_tonecurve_params4_t));
272     n->preserve_colors = 0;
273     return 0;
274   }
275   return 1;
276 }
277 
278 #ifdef HAVE_OPENCL
process_cl(struct dt_iop_module_t * self,dt_dev_pixelpipe_iop_t * piece,cl_mem dev_in,cl_mem dev_out,const dt_iop_roi_t * const roi_in,const dt_iop_roi_t * const roi_out)279 int process_cl(struct dt_iop_module_t *self, dt_dev_pixelpipe_iop_t *piece, cl_mem dev_in, cl_mem dev_out,
280                const dt_iop_roi_t *const roi_in, const dt_iop_roi_t *const roi_out)
281 {
282   dt_iop_tonecurve_data_t *d = (dt_iop_tonecurve_data_t *)piece->data;
283   dt_iop_tonecurve_global_data_t *gd = (dt_iop_tonecurve_global_data_t *)self->global_data;
284   cl_mem dev_L = NULL;
285   cl_mem dev_a = NULL;
286   cl_mem dev_b = NULL;
287   cl_mem dev_coeffs_L = NULL;
288   cl_mem dev_coeffs_ab = NULL;
289   cl_int err = -999;
290 
291   const int devid = piece->pipe->devid;
292   const int width = roi_in->width;
293   const int height = roi_in->height;
294   const int autoscale_ab = d->autoscale_ab;
295   const int unbound_ab = d->unbound_ab;
296   const float low_approximation = d->table[0][(int)(0.01f * 0x10000ul)];
297   const int preserve_colors = d->preserve_colors;
298 
299   const dt_iop_order_iccprofile_info_t *const work_profile
300     = dt_ioppr_add_profile_info_to_list(self->dev, DT_COLORSPACE_PROPHOTO_RGB, "", INTENT_PERCEPTUAL);
301 
302   cl_mem dev_profile_info = NULL;
303   cl_mem dev_profile_lut = NULL;
304   dt_colorspaces_iccprofile_info_cl_t *profile_info_cl;
305   cl_float *profile_lut_cl = NULL;
306 
307   err = dt_ioppr_build_iccprofile_params_cl(work_profile, devid, &profile_info_cl, &profile_lut_cl,
308                                             &dev_profile_info, &dev_profile_lut);
309   if(err != CL_SUCCESS) goto error;
310 
311   dev_L = dt_opencl_copy_host_to_device(devid, d->table[ch_L], 256, 256, sizeof(float));
312   if(dev_L == NULL) goto error;
313 
314   dev_a = dt_opencl_copy_host_to_device(devid, d->table[ch_a], 256, 256, sizeof(float));
315   if(dev_a == NULL) goto error;
316 
317   dev_b = dt_opencl_copy_host_to_device(devid, d->table[ch_b], 256, 256, sizeof(float));
318   if(dev_b == NULL) goto error;
319 
320   dev_coeffs_L = dt_opencl_copy_host_to_device_constant(devid, sizeof(float) * 3, d->unbounded_coeffs_L);
321   if(dev_coeffs_L == NULL) goto error;
322 
323   dev_coeffs_ab = dt_opencl_copy_host_to_device_constant(devid, sizeof(float) * 12, d->unbounded_coeffs_ab);
324   if(dev_coeffs_ab == NULL) goto error;
325 
326   size_t sizes[] = { ROUNDUPWD(width), ROUNDUPHT(height), 1 };
327   dt_opencl_set_kernel_arg(devid, gd->kernel_tonecurve, 0, sizeof(cl_mem), (void *)&dev_in);
328   dt_opencl_set_kernel_arg(devid, gd->kernel_tonecurve, 1, sizeof(cl_mem), (void *)&dev_out);
329   dt_opencl_set_kernel_arg(devid, gd->kernel_tonecurve, 2, sizeof(int), (void *)&width);
330   dt_opencl_set_kernel_arg(devid, gd->kernel_tonecurve, 3, sizeof(int), (void *)&height);
331   dt_opencl_set_kernel_arg(devid, gd->kernel_tonecurve, 4, sizeof(cl_mem), (void *)&dev_L);
332   dt_opencl_set_kernel_arg(devid, gd->kernel_tonecurve, 5, sizeof(cl_mem), (void *)&dev_a);
333   dt_opencl_set_kernel_arg(devid, gd->kernel_tonecurve, 6, sizeof(cl_mem), (void *)&dev_b);
334   dt_opencl_set_kernel_arg(devid, gd->kernel_tonecurve, 7, sizeof(int), (void *)&autoscale_ab);
335   dt_opencl_set_kernel_arg(devid, gd->kernel_tonecurve, 8, sizeof(int), (void *)&unbound_ab);
336   dt_opencl_set_kernel_arg(devid, gd->kernel_tonecurve, 9, sizeof(cl_mem), (void *)&dev_coeffs_L);
337   dt_opencl_set_kernel_arg(devid, gd->kernel_tonecurve, 10, sizeof(cl_mem), (void *)&dev_coeffs_ab);
338   dt_opencl_set_kernel_arg(devid, gd->kernel_tonecurve, 11, sizeof(float), (void *)&low_approximation);
339   dt_opencl_set_kernel_arg(devid, gd->kernel_tonecurve, 12, sizeof(int), (void *)&preserve_colors);
340   dt_opencl_set_kernel_arg(devid, gd->kernel_tonecurve, 13, sizeof(cl_mem), (void *)&dev_profile_info);
341   dt_opencl_set_kernel_arg(devid, gd->kernel_tonecurve, 14, sizeof(cl_mem), (void *)&dev_profile_lut);
342   err = dt_opencl_enqueue_kernel_2d(devid, gd->kernel_tonecurve, sizes);
343 
344   if(err != CL_SUCCESS) goto error;
345   dt_opencl_release_mem_object(dev_L);
346   dt_opencl_release_mem_object(dev_a);
347   dt_opencl_release_mem_object(dev_b);
348   dt_opencl_release_mem_object(dev_coeffs_L);
349   dt_opencl_release_mem_object(dev_coeffs_ab);
350   dt_ioppr_free_iccprofile_params_cl(&profile_info_cl, &profile_lut_cl, &dev_profile_info, &dev_profile_lut);
351   return TRUE;
352 
353 error:
354   dt_opencl_release_mem_object(dev_L);
355   dt_opencl_release_mem_object(dev_a);
356   dt_opencl_release_mem_object(dev_b);
357   dt_opencl_release_mem_object(dev_coeffs_L);
358   dt_opencl_release_mem_object(dev_coeffs_ab);
359   dt_ioppr_free_iccprofile_params_cl(&profile_info_cl, &profile_lut_cl, &dev_profile_info, &dev_profile_lut);
360   dt_print(DT_DEBUG_OPENCL, "[opencl_tonecurve] couldn't enqueue kernel! %d\n", err);
361   return FALSE;
362 }
363 #endif
364 /*
365 static inline float dt_prophoto_rgb_luminance(const float *const rgb)
366 {
367   return (rgb[0] * 0.2880402f + rgb[1] * 0.7118741f + rgb[2] * 0.0000857f);
368 }
369 */
process(struct dt_iop_module_t * self,dt_dev_pixelpipe_iop_t * piece,const void * const i,void * const o,const dt_iop_roi_t * const roi_in,const dt_iop_roi_t * const roi_out)370 void process(struct dt_iop_module_t *self, dt_dev_pixelpipe_iop_t *piece, const void *const i, void *const o,
371              const dt_iop_roi_t *const roi_in, const dt_iop_roi_t *const roi_out)
372 {
373   if (!dt_iop_have_required_input_format(4 /*we need full-color pixels*/, self, piece->colors,
374                                          i, o, roi_in, roi_out))
375     return; // image has been copied through to output and module's trouble flag has been updated
376 
377   const dt_iop_tonecurve_data_t *const restrict d = (dt_iop_tonecurve_data_t *)(piece->data);
378   const dt_iop_order_iccprofile_info_t *const work_profile
379     = dt_ioppr_add_profile_info_to_list(self->dev, DT_COLORSPACE_PROPHOTO_RGB, "", INTENT_PERCEPTUAL);
380   const float xm_L = 1.0f / d->unbounded_coeffs_L[0];
381   const float xm_ar = 1.0f / d->unbounded_coeffs_ab[0];
382   const float xm_al = 1.0f - 1.0f / d->unbounded_coeffs_ab[3];
383   const float xm_br = 1.0f / d->unbounded_coeffs_ab[6];
384   const float xm_bl = 1.0f - 1.0f / d->unbounded_coeffs_ab[9];
385   const float low_approximation = d->table[0][(int)(0.01f * 0x10000ul)];
386 
387   const size_t npixels = (size_t)roi_out->width * roi_out->height;
388   const int autoscale_ab = d->autoscale_ab;
389   const int unbound_ab = d->unbound_ab;
390 
391   const float *const restrict in = (float*)i;
392   float *const restrict out = (float*)o;
393 
394 #ifdef _OPENMP
395 #pragma omp parallel for default(none) \
396   dt_omp_firstprivate(npixels, autoscale_ab, low_approximation, xm_al, xm_ar, xm_bl, xm_br, xm_L, unbound_ab, work_profile) \
397   dt_omp_sharedconst(d, in, out) \
398   schedule(static)
399 #endif
400   for(int k = 0; k < 4*npixels; k += 4)
401   {
402     const float L_in = in[k] / 100.0f;
403 
404     out[k+0] = (L_in < xm_L) ? d->table[ch_L][CLAMP((int)(L_in * 0x10000ul), 0, 0xffff)]
405       : dt_iop_eval_exp(d->unbounded_coeffs_L, L_in);
406 
407     if(autoscale_ab == DT_S_SCALE_MANUAL)
408     {
409       const float a_in = (in[k+1] + 128.0f) / 256.0f;
410       const float b_in = (in[k+2] + 128.0f) / 256.0f;
411 
412       if(unbound_ab == 0)
413       {
414         // old style handling of a/b curves: only lut lookup with clamping
415         out[k+1] = d->table[ch_a][CLAMP((int)(a_in * 0x10000ul), 0, 0xffff)];
416         out[k+2] = d->table[ch_b][CLAMP((int)(b_in * 0x10000ul), 0, 0xffff)];
417       }
418       else
419       {
420         // new style handling of a/b curves: lut lookup with two-sided extrapolation;
421         // mind the x-axis reversal for the left-handed side
422         out[k+1] = (a_in > xm_ar)
423           ? dt_iop_eval_exp(d->unbounded_coeffs_ab, a_in)
424           : ((a_in < xm_al) ? dt_iop_eval_exp(d->unbounded_coeffs_ab + 3, 1.0f - a_in)
425              : d->table[ch_a][CLAMP((int)(a_in * 0x10000ul), 0, 0xffff)]);
426         out[k+2] = (b_in > xm_br)
427           ? dt_iop_eval_exp(d->unbounded_coeffs_ab + 6, b_in)
428           : ((b_in < xm_bl) ? dt_iop_eval_exp(d->unbounded_coeffs_ab + 9, 1.0f - b_in)
429              : d->table[ch_b][CLAMP((int)(b_in * 0x10000ul), 0, 0xffff)]);
430       }
431     }
432     else if(autoscale_ab == DT_S_SCALE_AUTOMATIC)
433     {
434       // in Lab: correct compressed Luminance for saturation:
435       if(L_in > 0.01f)
436       {
437         out[k+1] = in[k+1] * out[k] / in[k+0];
438         out[k+2] = in[k+2] * out[k] / in[k+0];
439       }
440       else
441       {
442         out[k+1] = in[k+1] * low_approximation;
443         out[k+2] = in[k+2] * low_approximation;
444       }
445     }
446     else if(autoscale_ab == DT_S_SCALE_AUTOMATIC_XYZ)
447     {
448       dt_aligned_pixel_t XYZ;
449       dt_Lab_to_XYZ(in + k, XYZ);
450       for(int c=0;c<3;c++)
451         XYZ[c] = (XYZ[c] < xm_L) ? d->table[ch_L][CLAMP((int)(XYZ[c] * 0x10000ul), 0, 0xffff)]
452           : dt_iop_eval_exp(d->unbounded_coeffs_L, XYZ[c]);
453       dt_XYZ_to_Lab(XYZ, out + k);
454     }
455     else if(autoscale_ab == DT_S_SCALE_AUTOMATIC_RGB)
456     {
457       dt_aligned_pixel_t rgb = {0, 0, 0};
458       dt_Lab_to_prophotorgb(in + k, rgb);
459       if(d->preserve_colors == DT_RGB_NORM_NONE)
460       {
461         for(int c = 0; c < 3; c++)
462         {
463           rgb[c] = (rgb[c] < xm_L) ? d->table[ch_L][CLAMP((int)(rgb[c] * 0x10000ul), 0, 0xffff)]
464             : dt_iop_eval_exp(d->unbounded_coeffs_L, rgb[c]);
465         }
466       }
467       else
468       {
469         float ratio = 1.f;
470         const float lum = dt_rgb_norm(rgb, d->preserve_colors, work_profile);
471         if(lum > 0.f)
472         {
473           const float curve_lum = (lum < xm_L)
474             ? d->table[ch_L][CLAMP((int)(lum * 0x10000ul), 0, 0xffff)]
475             : dt_iop_eval_exp(d->unbounded_coeffs_L, lum);
476           ratio = curve_lum / lum;
477         }
478         for(size_t c = 0; c < 3; c++)
479         {
480           rgb[c] = (ratio * rgb[c]);
481         }
482       }
483       dt_prophotorgb_to_Lab(rgb, out + k);
484     }
485 
486     out[k+3] = in[k+3];
487   }
488 }
489 
490 static const struct
491 {
492   const char *name;
493   const char *maker;
494   const char *model;
495   int iso_min;
496   float iso_max;
497   struct dt_iop_tonecurve_params_t preset;
498 } preset_camera_curves[] = {
499   // This is where you can paste the line provided by dt-curve-tool
500   // Here is a valid example for you to compare
501   // clang-format off
502     // nikon d750 by Edouard Gomez
503     {"Nikon D750", "NIKON CORPORATION", "NIKON D750", 0, FLT_MAX, {{{{0.000000, 0.000000}, {0.083508, 0.073677}, {0.212191, 0.274799}, {0.397095, 0.594035}, {0.495025, 0.714660}, {0.683565, 0.878550}, {0.854059, 0.950927}, {1.000000, 1.000000}}, {{0.000000, 0.000000}, {0.125000, 0.125000}, {0.250000, 0.250000}, {0.375000, 0.375000}, {0.500000, 0.500000}, {0.625000, 0.625000}, {0.750000, 0.750000}, {0.875000, 0.875000}}, {{0.000000, 0.000000}, {0.125000, 0.125000}, {0.250000, 0.250000}, {0.375000, 0.375000}, {0.500000, 0.500000}, {0.625000, 0.625000}, {0.750000, 0.750000}, {0.875000, 0.875000} }}, {8, 8, 8}, {2, 2, 2}, 1, 0, 0}},
504     // nikon d5100 contributed by Stefan Kauerauf
505     {"NIKON D5100", "NIKON CORPORATION", "NIKON D5100", 0, FLT_MAX, {{{{0.000000, 0.000000}, {0.000957, 0.000176}, {0.002423, 0.000798}, {0.005893, 0.003685}, {0.013219, 0.006619}, {0.023372, 0.011954}, {0.037580, 0.017817}, {0.069695, 0.035353}, {0.077276, 0.040315}, {0.123707, 0.082707}, {0.145249, 0.112105}, {0.189168, 0.186135}, {0.219576, 0.243677}, {0.290201, 0.385251}, {0.428150, 0.613355}, {0.506199, 0.700256}, {0.622833, 0.805488}, {0.702763, 0.870959}, {0.935053, 0.990139}, {1.000000, 1.000000}}, {{0.000000, 0.000000}, {0.050000, 0.050000}, {0.100000, 0.100000}, {0.150000, 0.150000}, {0.200000, 0.200000}, {0.250000, 0.250000}, {0.300000, 0.300000}, {0.350000, 0.350000}, {0.400000, 0.400000}, {0.450000, 0.450000}, {0.500000, 0.500000}, {0.550000, 0.550000}, {0.600000, 0.600000}, {0.650000, 0.650000}, {0.700000, 0.700000}, {0.750000, 0.750000}, {0.800000, 0.800000}, {0.850000, 0.850000}, {0.900000, 0.900000}, {0.950000, 0.950000}}, {{0.000000, 0.000000}, {0.050000, 0.050000}, {0.100000, 0.100000}, {0.150000, 0.150000}, {0.200000, 0.200000}, {0.250000, 0.250000}, {0.300000, 0.300000}, {0.350000, 0.350000}, {0.400000, 0.400000}, {0.450000, 0.450000}, {0.500000, 0.500000}, {0.550000, 0.550000}, {0.600000, 0.600000}, {0.650000, 0.650000}, {0.700000, 0.700000}, {0.750000, 0.750000}, {0.800000, 0.800000}, {0.850000, 0.850000}, {0.900000, 0.900000}, {0.950000, 0.950000}}}, {20, 20, 20}, {2, 2, 2}, 1, 0, 0}},
506     // nikon d7000 by Edouard Gomez
507     {"Nikon D7000", "NIKON CORPORATION", "NIKON D7000", 0, FLT_MAX, {{{{0.000000, 0.000000}, {0.110633, 0.111192}, {0.209771, 0.286963}, {0.355888, 0.561236}, {0.454987, 0.673098}, {0.769212, 0.920485}, {0.800468, 0.933428}, {1.000000, 1.000000}}, {{0.000000, 0.000000}, {0.125000, 0.125000}, {0.250000, 0.250000}, {0.375000, 0.375000}, {0.500000, 0.500000}, {0.625000, 0.625000}, {0.750000, 0.750000}, {0.875000, 0.875000}}, {{0.000000, 0.000000}, {0.125000, 0.125000}, {0.250000, 0.250000}, {0.375000, 0.375000}, {0.500000, 0.500000}, {0.625000, 0.625000}, {0.750000, 0.750000}, {0.875000, 0.875000}}}, {8, 8, 8}, {2, 2, 2}, 1, 0, 0}},
508     // nikon d7200 standard by Ralf Brown (firmware 1.00)
509     {"Nikon D7200", "NIKON CORPORATION", "NIKON D7200", 0, FLT_MAX, {{{{0.000000, 0.000000}, {0.000618, 0.003286}, {0.001639, 0.003705}, {0.005227, 0.005101}, {0.013299, 0.011192}, {0.016048, 0.013130}, {0.037941, 0.027014}, {0.058195, 0.041339}, {0.086531, 0.069088}, {0.116679, 0.107283}, {0.155629, 0.159422}, {0.205477, 0.246265}, {0.225923, 0.287343}, {0.348056, 0.509104}, {0.360629, 0.534732}, {0.507562, 0.762089}, {0.606899, 0.865692}, {0.734828, 0.947468}, {0.895488, 0.992021}, {1.000000, 1.000000}}, {{0.000000, 0.000000}, {0.050000, 0.050000}, {0.100000, 0.100000}, {0.150000, 0.150000}, {0.200000, 0.200000}, {0.250000, 0.250000}, {0.300000, 0.300000}, {0.350000, 0.350000}, {0.400000, 0.400000}, {0.450000, 0.450000}, {0.500000, 0.500000}, {0.550000, 0.550000}, {0.600000, 0.600000}, {0.650000, 0.650000}, {0.700000, 0.700000}, {0.750000, 0.750000}, {0.800000, 0.800000}, {0.850000, 0.850000}, {0.900000, 0.900000}, {0.950000, 0.950000}}, {{0.000000, 0.000000}, {0.050000, 0.050000}, {0.100000, 0.100000}, {0.150000, 0.150000}, {0.200000, 0.200000}, {0.250000, 0.250000}, {0.300000, 0.300000}, {0.350000, 0.350000}, {0.400000, 0.400000}, {0.450000, 0.450000}, {0.500000, 0.500000}, {0.550000, 0.550000}, {0.600000, 0.600000}, {0.650000, 0.650000}, {0.700000, 0.700000}, {0.750000, 0.750000}, {0.800000, 0.800000}, {0.850000, 0.850000}, {0.900000, 0.900000}, {0.950000, 0.950000}}}, {20, 20, 20}, {2, 2, 2}, 1, 0, 0}},
510     // nikon d7500 by Anders Bennehag (firmware C 1.00, LD 2.016)
511     {"NIKON D7500", "NIKON CORPORATION", "NIKON D7500", 0, FLT_MAX, {{{{0.000000, 0.000000}, {0.000421, 0.003412}, {0.003775, 0.004001}, {0.013762, 0.008704}, {0.016698, 0.010230}, {0.034965, 0.018732}, {0.087311, 0.049808}, {0.101389, 0.060789}, {0.166845, 0.145269}, {0.230944, 0.271288}, {0.333399, 0.502609}, {0.353207, 0.542549}, {0.550014, 0.819535}, {0.731749, 0.944033}, {0.783283, 0.960546}, {1.000000, 1.000000}}, {{0.000000, 0.000000}, {0.062500, 0.062500}, {0.125000, 0.125000}, {0.187500, 0.187500}, {0.250000, 0.250000}, {0.312500, 0.312500}, {0.375000, 0.375000}, {0.437500, 0.437500}, {0.500000, 0.500000}, {0.562500, 0.562500}, {0.625000, 0.625000}, {0.687500, 0.687500}, {0.750000, 0.750000}, {0.812500, 0.812500}, {0.875000, 0.875000}, {0.937500, 0.937500}}, {{0.000000, 0.000000}, {0.062500, 0.062500}, {0.125000, 0.125000}, {0.187500, 0.187500}, {0.250000, 0.250000}, {0.312500, 0.312500}, {0.375000, 0.375000}, {0.437500, 0.437500}, {0.500000, 0.500000}, {0.562500, 0.562500}, {0.625000, 0.625000}, {0.687500, 0.687500}, {0.750000, 0.750000}, {0.812500, 0.812500}, {0.875000, 0.875000}, {0.937500, 0.937500}}}, {16, 16, 16}, {2, 2, 2}, 1, 0, 0}},
512     // nikon d90 by Edouard Gomez
513     {"Nikon D90", "NIKON CORPORATION", "NIKON D90", 0, FLT_MAX, {{{{0.000000, 0.000000}, {0.002915, 0.006453}, {0.023324, 0.021601}, {0.078717, 0.074963}, {0.186589, 0.242230}, {0.364432, 0.544956}, {0.629738, 0.814127}, {1.000000, 1.000000}}, {{0.000000, 0.000000}, {0.125000, 0.125000}, {0.250000, 0.250000}, {0.375000, 0.375000}, {0.500000, 0.500000}, {0.625000, 0.625000}, {0.750000, 0.750000}, {0.875000, 0.875000}}, {{0.000000, 0.000000}, {0.125000, 0.125000}, {0.250000, 0.250000}, {0.375000, 0.375000}, {0.500000, 0.500000}, {0.625000, 0.625000}, {0.750000, 0.750000}, {0.875000, 0.875000}}}, {8, 8, 8}, {2, 2, 2}, 1, 0, 0}},
514     // Olympus OM-D E-M10 II by Lukas Schrangl
515     {"Olympus OM-D E-M10 II", "OLYMPUS CORPORATION    ", "E-M10MarkII     ", 0, FLT_MAX, {{{{0.000000, 0.000000}, {0.004036, 0.000809}, {0.015047, 0.009425}, {0.051948, 0.042053}, {0.071777, 0.066635}, {0.090018, 0.086722}, {0.110197, 0.118773}, {0.145817, 0.171861}, {0.207476, 0.278652}, {0.266832, 0.402823}, {0.428061, 0.696319}, {0.559728, 0.847113}, {0.943576, 0.993482}, {1.000000, 1.000000}}, {{0.000000, 0.000000}, {0.071429, 0.071429}, {0.142857, 0.142857}, {0.214286, 0.214286}, {0.285714, 0.285714}, {0.357143, 0.357143}, {0.428571, 0.428571}, {0.500000, 0.500000}, {0.571429, 0.571429}, {0.642857, 0.642857}, {0.714286, 0.714286}, {0.785714, 0.785714}, {0.857143, 0.857143}, {0.928571, 0.928571}}, {{0.000000, 0.000000}, {0.071429, 0.071429}, {0.142857, 0.142857}, {0.214286, 0.214286}, {0.285714, 0.285714}, {0.357143, 0.357143}, {0.428571, 0.428571}, {0.500000, 0.500000}, {0.571429, 0.571429}, {0.642857, 0.642857}, {0.714286, 0.714286}, {0.785714, 0.785714}, {0.857143, 0.857143}, {0.928571, 0.928571}}}, {14, 14, 14}, {2, 2, 2}, 1, 0, 0}},
516   // clang-format on
517 };
518 
init_presets(dt_iop_module_so_t * self)519 void init_presets(dt_iop_module_so_t *self)
520 {
521   dt_iop_tonecurve_params_t p;
522   memset(&p, 0, sizeof(p));
523   p.tonecurve_nodes[ch_L] = 6;
524   p.tonecurve_nodes[ch_a] = 7;
525   p.tonecurve_nodes[ch_b] = 7;
526   p.tonecurve_type[ch_L] = CUBIC_SPLINE;
527   p.tonecurve_type[ch_a] = CUBIC_SPLINE;
528   p.tonecurve_type[ch_b] = CUBIC_SPLINE;
529   p.tonecurve_preset = 0;
530   p.tonecurve_autoscale_ab = DT_S_SCALE_AUTOMATIC_RGB;
531   p.tonecurve_unbound_ab = 1;
532 
533   float linear_ab[7] = { 0.0, 0.08, 0.3, 0.5, 0.7, 0.92, 1.0 };
534 
535   // linear a, b curves for presets
536   for(int k = 0; k < 7; k++) p.tonecurve[ch_a][k].x = linear_ab[k];
537   for(int k = 0; k < 7; k++) p.tonecurve[ch_a][k].y = linear_ab[k];
538   for(int k = 0; k < 7; k++) p.tonecurve[ch_b][k].x = linear_ab[k];
539   for(int k = 0; k < 7; k++) p.tonecurve[ch_b][k].y = linear_ab[k];
540 
541   // More useful low contrast curve (based on Samsung NX -2 Contrast)
542   p.tonecurve[ch_L][0].x = 0.000000;
543   p.tonecurve[ch_L][1].x = 0.003862;
544   p.tonecurve[ch_L][2].x = 0.076613;
545   p.tonecurve[ch_L][3].x = 0.169355;
546   p.tonecurve[ch_L][4].x = 0.774194;
547   p.tonecurve[ch_L][5].x = 1.000000;
548   p.tonecurve[ch_L][0].y = 0.000000;
549   p.tonecurve[ch_L][1].y = 0.007782;
550   p.tonecurve[ch_L][2].y = 0.156182;
551   p.tonecurve[ch_L][3].y = 0.290352;
552   p.tonecurve[ch_L][4].y = 0.773852;
553   p.tonecurve[ch_L][5].y = 1.000000;
554   dt_gui_presets_add_generic(_("contrast compression"), self->op,
555                              self->version(), &p, sizeof(p), 1, DEVELOP_BLEND_CS_RGB_DISPLAY);
556 
557   p.tonecurve_nodes[ch_L] = 7;
558   float linear_L[7] = { 0.0, 0.08, 0.17, 0.50, 0.83, 0.92, 1.0 };
559 
560   // Linear - no contrast
561   for(int k = 0; k < 7; k++) p.tonecurve[ch_L][k].x = linear_L[k];
562   for(int k = 0; k < 7; k++) p.tonecurve[ch_L][k].y = linear_L[k];
563   dt_gui_presets_add_generic(_("gamma 1.0 (linear)"), self->op,
564                              self->version(), &p, sizeof(p), 1, DEVELOP_BLEND_CS_RGB_DISPLAY);
565 
566   // Linear contrast
567   for(int k = 0; k < 7; k++) p.tonecurve[ch_L][k].x = linear_L[k];
568   for(int k = 0; k < 7; k++) p.tonecurve[ch_L][k].y = linear_L[k];
569   p.tonecurve[ch_L][1].y -= 0.020;
570   p.tonecurve[ch_L][2].y -= 0.030;
571   p.tonecurve[ch_L][4].y += 0.030;
572   p.tonecurve[ch_L][5].y += 0.020;
573   dt_gui_presets_add_generic(_("contrast - med (linear)"), self->op,
574                              self->version(), &p, sizeof(p), 1, DEVELOP_BLEND_CS_RGB_DISPLAY);
575 
576   for(int k = 0; k < 7; k++) p.tonecurve[ch_L][k].x = linear_L[k];
577   for(int k = 0; k < 7; k++) p.tonecurve[ch_L][k].y = linear_L[k];
578   p.tonecurve[ch_L][1].y -= 0.040;
579   p.tonecurve[ch_L][2].y -= 0.060;
580   p.tonecurve[ch_L][4].y += 0.060;
581   p.tonecurve[ch_L][5].y += 0.040;
582   dt_gui_presets_add_generic(_("contrast - high (linear)"), self->op,
583                              self->version(), &p, sizeof(p), 1, DEVELOP_BLEND_CS_RGB_DISPLAY);
584 
585   // Gamma contrast
586   for(int k = 0; k < 7; k++) p.tonecurve[ch_L][k].x = linear_L[k];
587   for(int k = 0; k < 7; k++) p.tonecurve[ch_L][k].y = linear_L[k];
588   p.tonecurve[ch_L][1].y -= 0.020;
589   p.tonecurve[ch_L][2].y -= 0.030;
590   p.tonecurve[ch_L][4].y += 0.030;
591   p.tonecurve[ch_L][5].y += 0.020;
592   for(int k = 1; k < 6; k++) p.tonecurve[ch_L][k].x = powf(p.tonecurve[ch_L][k].x, 2.2f);
593   for(int k = 1; k < 6; k++) p.tonecurve[ch_L][k].y = powf(p.tonecurve[ch_L][k].y, 2.2f);
594   dt_gui_presets_add_generic(_("contrast - med (gamma 2.2)"), self->op,
595                              self->version(), &p, sizeof(p), 1, DEVELOP_BLEND_CS_RGB_DISPLAY);
596 
597   for(int k = 0; k < 7; k++) p.tonecurve[ch_L][k].x = linear_L[k];
598   for(int k = 0; k < 7; k++) p.tonecurve[ch_L][k].y = linear_L[k];
599   p.tonecurve[ch_L][1].y -= 0.040;
600   p.tonecurve[ch_L][2].y -= 0.060;
601   p.tonecurve[ch_L][4].y += 0.060;
602   p.tonecurve[ch_L][5].y += 0.040;
603   for(int k = 1; k < 6; k++) p.tonecurve[ch_L][k].x = powf(p.tonecurve[ch_L][k].x, 2.2f);
604   for(int k = 1; k < 6; k++) p.tonecurve[ch_L][k].y = powf(p.tonecurve[ch_L][k].y, 2.2f);
605   dt_gui_presets_add_generic(_("contrast - high (gamma 2.2)"), self->op,
606                              self->version(), &p, sizeof(p), 1, DEVELOP_BLEND_CS_RGB_DISPLAY);
607 
608   /** for pure power-like functions, we need more nodes close to the bounds**/
609 
610   p.tonecurve_type[ch_L] = MONOTONE_HERMITE;
611 
612   for(int k = 0; k < 7; k++) p.tonecurve[ch_L][k].x = linear_L[k];
613   for(int k = 0; k < 7; k++) p.tonecurve[ch_L][k].y = linear_L[k];
614 
615   // Gamma 2.0 - no contrast
616   for(int k = 1; k < 6; k++) p.tonecurve[ch_L][k].y = powf(linear_L[k], 2.0f);
617   dt_gui_presets_add_generic(_("gamma 2.0"), self->op,
618                              self->version(), &p, sizeof(p), 1, DEVELOP_BLEND_CS_RGB_DISPLAY);
619 
620   // Gamma 0.5 - no contrast
621   for(int k = 1; k < 6; k++) p.tonecurve[ch_L][k].y = powf(linear_L[k], 0.5f);
622   dt_gui_presets_add_generic(_("gamma 0.5"), self->op,
623                              self->version(), &p, sizeof(p), 1, DEVELOP_BLEND_CS_RGB_DISPLAY);
624 
625   // Log2 - no contrast
626   for(int k = 1; k < 6; k++) p.tonecurve[ch_L][k].y = logf(linear_L[k] + 1.0f) / logf(2.0f);
627   dt_gui_presets_add_generic(_("logarithm (base 2)"), self->op,
628                              self->version(), &p, sizeof(p), 1, DEVELOP_BLEND_CS_RGB_DISPLAY);
629 
630   // Exp2 - no contrast
631   for(int k = 1; k < 6; k++) p.tonecurve[ch_L][k].y = powf(2.0f, linear_L[k]) - 1.0f;
632   dt_gui_presets_add_generic(_("exponential (base 2)"), self->op,
633                              self->version(), &p, sizeof(p), 1, DEVELOP_BLEND_CS_RGB_DISPLAY);
634 
635   for (int k=0; k<sizeof(preset_camera_curves)/sizeof(preset_camera_curves[0]); k++)
636   {
637     // insert the preset
638     dt_gui_presets_add_generic(preset_camera_curves[k].name, self->op, self->version(),
639                                &preset_camera_curves[k].preset, sizeof(p), 1, DEVELOP_BLEND_CS_RGB_DISPLAY);
640 
641     // restrict it to model, maker
642     dt_gui_presets_update_mml(preset_camera_curves[k].name, self->op, self->version(),
643                               preset_camera_curves[k].maker, preset_camera_curves[k].model, "");
644 
645     // restrict it to  iso
646     dt_gui_presets_update_iso(preset_camera_curves[k].name, self->op, self->version(),
647                               preset_camera_curves[k].iso_min, preset_camera_curves[k].iso_max);
648 
649     // restrict it to raw images
650     dt_gui_presets_update_ldr(preset_camera_curves[k].name, self->op, self->version(), FOR_RAW);
651 
652     // hide all non-matching presets in case the model string is set.
653     dt_gui_presets_update_filter(preset_camera_curves[k].name, self->op, self->version(), 1);
654   }
655 }
656 
commit_params(struct dt_iop_module_t * self,dt_iop_params_t * p1,dt_dev_pixelpipe_t * pipe,dt_dev_pixelpipe_iop_t * piece)657 void commit_params(struct dt_iop_module_t *self, dt_iop_params_t *p1, dt_dev_pixelpipe_t *pipe,
658                    dt_dev_pixelpipe_iop_t *piece)
659 {
660   dt_iop_tonecurve_data_t *d = (dt_iop_tonecurve_data_t *)(piece->data);
661   dt_iop_tonecurve_params_t *p = (dt_iop_tonecurve_params_t *)p1;
662 
663   if((pipe->type & DT_DEV_PIXELPIPE_PREVIEW) == DT_DEV_PIXELPIPE_PREVIEW)
664     piece->request_histogram |= (DT_REQUEST_ON);
665   else
666     piece->request_histogram &= ~(DT_REQUEST_ON);
667 
668   for(int ch = 0; ch < ch_max; ch++)
669   {
670     // take care of possible change of curve type or number of nodes (not yet implemented in UI)
671     if(d->curve_type[ch] != p->tonecurve_type[ch] || d->curve_nodes[ch] != p->tonecurve_nodes[ch])
672     {
673       dt_draw_curve_destroy(d->curve[ch]);
674       d->curve[ch] = dt_draw_curve_new(0.0, 1.0, p->tonecurve_type[ch]);
675       d->curve_nodes[ch] = p->tonecurve_nodes[ch];
676       d->curve_type[ch] = p->tonecurve_type[ch];
677       for(int k = 0; k < p->tonecurve_nodes[ch]; k++)
678         (void)dt_draw_curve_add_point(d->curve[ch], p->tonecurve[ch][k].x, p->tonecurve[ch][k].y);
679     }
680     else
681     {
682       for(int k = 0; k < p->tonecurve_nodes[ch]; k++)
683         dt_draw_curve_set_point(d->curve[ch], k, p->tonecurve[ch][k].x, p->tonecurve[ch][k].y);
684     }
685     dt_draw_curve_calc_values(d->curve[ch], 0.0f, 1.0f, 0x10000, NULL, d->table[ch]);
686   }
687   for(int k = 0; k < 0x10000; k++) d->table[ch_L][k] *= 100.0f;
688   for(int k = 0; k < 0x10000; k++) d->table[ch_a][k] = d->table[ch_a][k] * 256.0f - 128.0f;
689   for(int k = 0; k < 0x10000; k++) d->table[ch_b][k] = d->table[ch_b][k] * 256.0f - 128.0f;
690 
691   piece->process_cl_ready = 1;
692   if(p->tonecurve_autoscale_ab == DT_S_SCALE_AUTOMATIC_XYZ)
693   {
694     // derive curve for XYZ:
695     for(int k=0;k<0x10000;k++)
696     {
697       dt_aligned_pixel_t XYZ = {k/(float)0x10000, k/(float)0x10000, k/(float)0x10000};
698       dt_aligned_pixel_t Lab = {0.0};
699       dt_XYZ_to_Lab(XYZ, Lab);
700       Lab[0] = d->table[ch_L][CLAMP((int)(Lab[0]/100.0f * 0x10000), 0, 0xffff)];
701       dt_Lab_to_XYZ(Lab, XYZ);
702       d->table[ch_L][k] = XYZ[1]; // now mapping Y_in to Y_out
703     }
704   }
705   else if(p->tonecurve_autoscale_ab == DT_S_SCALE_AUTOMATIC_RGB)
706   {
707     // derive curve for rgb:
708     for(int k=0;k<0x10000;k++)
709     {
710       dt_aligned_pixel_t rgb = {k/(float)0x10000, k/(float)0x10000, k/(float)0x10000};
711       dt_aligned_pixel_t Lab = {0.0};
712       dt_prophotorgb_to_Lab(rgb, Lab);
713       Lab[0] = d->table[ch_L][CLAMP((int)(Lab[0]/100.0f * 0x10000), 0, 0xffff)];
714       dt_Lab_to_prophotorgb(Lab, rgb);
715       d->table[ch_L][k] = rgb[1]; // now mapping G_in to G_out
716     }
717   }
718 
719   d->autoscale_ab = p->tonecurve_autoscale_ab;
720   d->unbound_ab = p->tonecurve_unbound_ab;
721   d->preserve_colors = p->preserve_colors;
722 
723   // extrapolation for L-curve (right hand side only):
724   const float xm_L = p->tonecurve[ch_L][p->tonecurve_nodes[ch_L] - 1].x;
725   const float x_L[4] = { 0.7f * xm_L, 0.8f * xm_L, 0.9f * xm_L, 1.0f * xm_L };
726   const float y_L[4] = { d->table[ch_L][CLAMP((int)(x_L[0] * 0x10000ul), 0, 0xffff)],
727                          d->table[ch_L][CLAMP((int)(x_L[1] * 0x10000ul), 0, 0xffff)],
728                          d->table[ch_L][CLAMP((int)(x_L[2] * 0x10000ul), 0, 0xffff)],
729                          d->table[ch_L][CLAMP((int)(x_L[3] * 0x10000ul), 0, 0xffff)] };
730   dt_iop_estimate_exp(x_L, y_L, 4, d->unbounded_coeffs_L);
731 
732   // extrapolation for a-curve right side:
733   const float xm_ar = p->tonecurve[ch_a][p->tonecurve_nodes[ch_a] - 1].x;
734   const float x_ar[4] = { 0.7f * xm_ar, 0.8f * xm_ar, 0.9f * xm_ar, 1.0f * xm_ar };
735   const float y_ar[4] = { d->table[ch_a][CLAMP((int)(x_ar[0] * 0x10000ul), 0, 0xffff)],
736                           d->table[ch_a][CLAMP((int)(x_ar[1] * 0x10000ul), 0, 0xffff)],
737                           d->table[ch_a][CLAMP((int)(x_ar[2] * 0x10000ul), 0, 0xffff)],
738                           d->table[ch_a][CLAMP((int)(x_ar[3] * 0x10000ul), 0, 0xffff)] };
739   dt_iop_estimate_exp(x_ar, y_ar, 4, d->unbounded_coeffs_ab);
740 
741   // extrapolation for a-curve left side (we need to mirror the x-axis):
742   const float xm_al = 1.0f - p->tonecurve[ch_a][0].x;
743   const float x_al[4] = { 0.7f * xm_al, 0.8f * xm_al, 0.9f * xm_al, 1.0f * xm_al };
744   const float y_al[4] = { d->table[ch_a][CLAMP((int)((1.0f - x_al[0]) * 0x10000ul), 0, 0xffff)],
745                           d->table[ch_a][CLAMP((int)((1.0f - x_al[1]) * 0x10000ul), 0, 0xffff)],
746                           d->table[ch_a][CLAMP((int)((1.0f - x_al[2]) * 0x10000ul), 0, 0xffff)],
747                           d->table[ch_a][CLAMP((int)((1.0f - x_al[3]) * 0x10000ul), 0, 0xffff)] };
748   dt_iop_estimate_exp(x_al, y_al, 4, d->unbounded_coeffs_ab + 3);
749 
750   // extrapolation for b-curve right side:
751   const float xm_br = p->tonecurve[ch_b][p->tonecurve_nodes[ch_b] - 1].x;
752   const float x_br[4] = { 0.7f * xm_br, 0.8f * xm_br, 0.9f * xm_br, 1.0f * xm_br };
753   const float y_br[4] = { d->table[ch_b][CLAMP((int)(x_br[0] * 0x10000ul), 0, 0xffff)],
754                           d->table[ch_b][CLAMP((int)(x_br[1] * 0x10000ul), 0, 0xffff)],
755                           d->table[ch_b][CLAMP((int)(x_br[2] * 0x10000ul), 0, 0xffff)],
756                           d->table[ch_b][CLAMP((int)(x_br[3] * 0x10000ul), 0, 0xffff)] };
757   dt_iop_estimate_exp(x_br, y_br, 4, d->unbounded_coeffs_ab + 6);
758 
759   // extrapolation for b-curve left side (we need to mirror the x-axis):
760   const float xm_bl = 1.0f - p->tonecurve[ch_b][0].x;
761   const float x_bl[4] = { 0.7f * xm_bl, 0.8f * xm_bl, 0.9f * xm_bl, 1.0f * xm_bl };
762   const float y_bl[4] = { d->table[ch_b][CLAMP((int)((1.0f - x_bl[0]) * 0x10000ul), 0, 0xffff)],
763                           d->table[ch_b][CLAMP((int)((1.0f - x_bl[1]) * 0x10000ul), 0, 0xffff)],
764                           d->table[ch_b][CLAMP((int)((1.0f - x_bl[2]) * 0x10000ul), 0, 0xffff)],
765                           d->table[ch_b][CLAMP((int)((1.0f - x_bl[3]) * 0x10000ul), 0, 0xffff)] };
766   dt_iop_estimate_exp(x_bl, y_bl, 4, d->unbounded_coeffs_ab + 9);
767 }
768 
eval_grey(float x)769 static float eval_grey(float x)
770 {
771   // "log base" is a combined scaling and offset change so that x->[0,1] with
772   // the left side of the histogram expanded (slider->right) or not (slider left, linear)
773   return x;
774 }
775 
init_pipe(struct dt_iop_module_t * self,dt_dev_pixelpipe_t * pipe,dt_dev_pixelpipe_iop_t * piece)776 void init_pipe(struct dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
777 {
778   // create part of the pixelpipe
779   dt_iop_tonecurve_data_t *d = (dt_iop_tonecurve_data_t *)malloc(sizeof(dt_iop_tonecurve_data_t));
780   dt_iop_tonecurve_params_t *default_params = (dt_iop_tonecurve_params_t *)self->default_params;
781   piece->data = (void *)d;
782   d->autoscale_ab = DT_S_SCALE_AUTOMATIC;
783   d->unbound_ab = 1;
784   for(int ch = 0; ch < ch_max; ch++)
785   {
786     d->curve[ch] = dt_draw_curve_new(0.0, 1.0, default_params->tonecurve_type[ch]);
787     d->curve_nodes[ch] = default_params->tonecurve_nodes[ch];
788     d->curve_type[ch] = default_params->tonecurve_type[ch];
789     for(int k = 0; k < default_params->tonecurve_nodes[ch]; k++)
790       (void)dt_draw_curve_add_point(d->curve[ch], default_params->tonecurve[ch][k].x,
791                                     default_params->tonecurve[ch][k].y);
792   }
793   for(int k = 0; k < 0x10000; k++) d->table[ch_L][k] = 100.0f * k / 0x10000;          // identity for L
794   for(int k = 0; k < 0x10000; k++) d->table[ch_a][k] = 256.0f * k / 0x10000 - 128.0f; // identity for a
795   for(int k = 0; k < 0x10000; k++) d->table[ch_b][k] = 256.0f * k / 0x10000 - 128.0f; // identity for b
796 }
797 
cleanup_pipe(struct dt_iop_module_t * self,dt_dev_pixelpipe_t * pipe,dt_dev_pixelpipe_iop_t * piece)798 void cleanup_pipe(struct dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
799 {
800   // clean up everything again.
801   dt_iop_tonecurve_data_t *d = (dt_iop_tonecurve_data_t *)(piece->data);
802   for(int ch = 0; ch < ch_max; ch++) dt_draw_curve_destroy(d->curve[ch]);
803   free(piece->data);
804   piece->data = NULL;
805 }
806 
gui_reset(struct dt_iop_module_t * self)807 void gui_reset(struct dt_iop_module_t *self)
808 {
809   dt_iop_tonecurve_gui_data_t *g = (dt_iop_tonecurve_gui_data_t *)self->gui_data;
810   dt_iop_tonecurve_params_t *p = (dt_iop_tonecurve_params_t *)self->params;
811   dt_bauhaus_combobox_set(g->interpolator, p->tonecurve_type[ch_L]);
812   dt_bauhaus_combobox_set(g->preserve_colors, p->preserve_colors);
813   dt_bauhaus_slider_set(g->logbase, 0);
814   g->loglogscale = 0;
815   g->semilog = 0;
816 
817   g->channel = (tonecurve_channel_t)ch_L;
818   gtk_widget_queue_draw(self->widget);
819 }
820 
gui_update(struct dt_iop_module_t * self)821 void gui_update(struct dt_iop_module_t *self)
822 {
823   dt_iop_tonecurve_gui_data_t *g = (dt_iop_tonecurve_gui_data_t *)self->gui_data;
824   dt_iop_tonecurve_params_t *p = (dt_iop_tonecurve_params_t *)self->params;
825 
826   dt_bauhaus_combobox_set_from_value(g->autoscale_ab, p->tonecurve_autoscale_ab);
827 
828   gui_changed(self, g->autoscale_ab, 0);
829 
830   dt_bauhaus_combobox_set(g->interpolator, p->tonecurve_type[ch_L]);
831   dt_bauhaus_combobox_set(g->preserve_colors, p->preserve_colors);
832   g->loglogscale = eval_grey(dt_bauhaus_slider_get(g->logbase));
833   // that's all, gui curve is read directly from params during expose event.
834   gtk_widget_queue_draw(self->widget);
835 }
836 
init(dt_iop_module_t * module)837 void init(dt_iop_module_t *module)
838 {
839   dt_iop_default_init(module);
840 
841   module->request_histogram |= (DT_REQUEST_ON);
842 
843   dt_iop_tonecurve_params_t *d = module->default_params;
844 
845   d->tonecurve_nodes[0] = 2;
846   d->tonecurve_nodes[1] =
847   d->tonecurve_nodes[2] = 3;
848   d->tonecurve[0][1].x = d->tonecurve[0][1].y =
849   d->tonecurve[1][2].x = d->tonecurve[1][2].y =
850   d->tonecurve[2][2].x = d->tonecurve[2][2].y = 1.0f;
851   d->tonecurve[1][1].x = d->tonecurve[1][1].y =
852   d->tonecurve[2][1].x = d->tonecurve[2][1].y = 0.5f;
853 }
854 
init_global(dt_iop_module_so_t * module)855 void init_global(dt_iop_module_so_t *module)
856 {
857   const int program = 2; // basic.cl, from programs.conf
858   dt_iop_tonecurve_global_data_t *gd
859       = (dt_iop_tonecurve_global_data_t *)malloc(sizeof(dt_iop_tonecurve_global_data_t));
860   module->data = gd;
861   gd->kernel_tonecurve = dt_opencl_create_kernel(program, "tonecurve");
862   for(int k=0; k<3; k++)
863   {
864     gd->picked_color[k] = .0f;
865     gd->picked_color_min[k] = .0f;
866     gd->picked_color_max[k] = .0f;
867     gd->picked_output_color[k] = .0f;
868   }
869 }
870 
cleanup_global(dt_iop_module_so_t * module)871 void cleanup_global(dt_iop_module_so_t *module)
872 {
873   dt_iop_tonecurve_global_data_t *gd = (dt_iop_tonecurve_global_data_t *)module->data;
874   dt_opencl_free_kernel(gd->kernel_tonecurve);
875   free(module->data);
876   module->data = NULL;
877 }
878 
logbase_callback(GtkWidget * slider,dt_iop_module_t * self)879 static void logbase_callback(GtkWidget *slider, dt_iop_module_t *self)
880 {
881   if(darktable.gui->reset) return;
882   dt_iop_tonecurve_gui_data_t *g = (dt_iop_tonecurve_gui_data_t *)self->gui_data;
883   g->loglogscale = eval_grey(dt_bauhaus_slider_get(g->logbase));
884   gtk_widget_queue_draw(GTK_WIDGET(g->area));
885 }
886 
gui_changed(dt_iop_module_t * self,GtkWidget * w,void * previous)887 void gui_changed(dt_iop_module_t *self, GtkWidget *w, void *previous)
888 {
889   dt_iop_tonecurve_gui_data_t *g = (dt_iop_tonecurve_gui_data_t *)self->gui_data;
890   dt_iop_tonecurve_params_t *p = (dt_iop_tonecurve_params_t *)self->params;
891 
892   if(w == g->autoscale_ab)
893   {
894     g->channel = (tonecurve_channel_t)ch_L;
895     gtk_notebook_set_current_page(GTK_NOTEBOOK(g->channel_tabs), ch_L);
896 
897     gtk_notebook_set_show_tabs(g->channel_tabs, p->tonecurve_autoscale_ab == DT_S_SCALE_MANUAL);
898     gtk_widget_set_visible(g->preserve_colors, p->tonecurve_autoscale_ab == DT_S_SCALE_AUTOMATIC_RGB);
899 
900     gtk_widget_queue_draw(self->widget);
901   }
902 }
903 
interpolator_callback(GtkWidget * widget,dt_iop_module_t * self)904 static void interpolator_callback(GtkWidget *widget, dt_iop_module_t *self)
905 {
906   if(darktable.gui->reset) return;
907   dt_iop_tonecurve_params_t *p = (dt_iop_tonecurve_params_t *)self->params;
908   dt_iop_tonecurve_gui_data_t *g = (dt_iop_tonecurve_gui_data_t *)self->gui_data;
909   const int combo = dt_bauhaus_combobox_get(widget);
910   if(combo == 0) p->tonecurve_type[ch_L] = p->tonecurve_type[ch_a] = p->tonecurve_type[ch_b] = CUBIC_SPLINE;
911   if(combo == 1) p->tonecurve_type[ch_L] = p->tonecurve_type[ch_a] = p->tonecurve_type[ch_b] = CATMULL_ROM;
912   if(combo == 2) p->tonecurve_type[ch_L] = p->tonecurve_type[ch_a] = p->tonecurve_type[ch_b] = MONOTONE_HERMITE;
913   dt_dev_add_history_item(darktable.develop, self, TRUE);
914   gtk_widget_queue_draw(GTK_WIDGET(g->area));
915 }
916 
tab_switch(GtkNotebook * notebook,GtkWidget * page,guint page_num,gpointer user_data)917 static void tab_switch(GtkNotebook *notebook, GtkWidget *page, guint page_num, gpointer user_data)
918 {
919   dt_iop_module_t *self = (dt_iop_module_t *)user_data;
920   dt_iop_tonecurve_gui_data_t *c = (dt_iop_tonecurve_gui_data_t *)self->gui_data;
921   if(darktable.gui->reset) return;
922   c->channel = (tonecurve_channel_t)page_num;
923   gtk_widget_queue_draw(self->widget);
924 }
925 
area_resized(GtkWidget * widget,GdkEvent * event,gpointer user_data)926 static gboolean area_resized(GtkWidget *widget, GdkEvent *event, gpointer user_data)
927 {
928   GtkRequisition r;
929   GtkAllocation allocation;
930   gtk_widget_get_allocation(widget, &allocation);
931   r.width = allocation.width;
932   r.height = allocation.width;
933   gtk_widget_get_preferred_size(widget, &r, NULL);
934   return TRUE;
935 }
936 
to_log(const float x,const float base,const int ch,const int semilog,const int is_ordinate)937 static float to_log(const float x, const float base, const int ch, const int semilog, const int is_ordinate)
938 {
939   // don't log-encode the a and b channels
940   if(base > 0.0f && ch == ch_L)
941   {
942     if (semilog == 1 && is_ordinate == 1)
943     {
944       // we don't want log on ordinate axis in semilog x
945       return x;
946     }
947     else if (semilog == -1 && is_ordinate == 0)
948     {
949       // we don't want log on abcissa axis in semilog y
950       return x;
951     }
952     else
953     {
954       return logf(x * base + 1.0f) / logf(base + 1.0f);
955     }
956   }
957   else
958   {
959     return x;
960   }
961 }
962 
to_lin(const float x,const float base,const int ch,const int semilog,const int is_ordinate)963 static float to_lin(const float x, const float base, const int ch, const int semilog, const int is_ordinate)
964 {
965   // don't log-encode the a and b channels
966   if(base > 0.0f && ch == ch_L)
967   {
968     if (semilog == 1 && is_ordinate == 1)
969     {
970       // we don't want log on ordinate axis in semilog x
971       return x;
972     }
973     else if (semilog == -1 && is_ordinate == 0)
974     {
975       // we don't want log on abcissa axis in semilog y
976       return x;
977     }
978     else
979     {
980       return (powf(base + 1.0f, x) - 1.0f) / base;
981     }
982   }
983   else
984   {
985     return x;
986   }
987 }
988 
color_picker_apply(dt_iop_module_t * self,GtkWidget * picker,dt_dev_pixelpipe_iop_t * piece)989 void color_picker_apply(dt_iop_module_t *self, GtkWidget *picker, dt_dev_pixelpipe_iop_t *piece)
990 {
991   dt_iop_tonecurve_global_data_t *gd = (dt_iop_tonecurve_global_data_t *)self->global_data;
992 
993   for(int k=0; k<3; k++)
994   {
995     gd->picked_color[k] = self->picked_color[k];
996     gd->picked_color_min[k] = self->picked_color_min[k];
997     gd->picked_color_max[k] = self->picked_color_max[k];
998     gd->picked_output_color[k] = self->picked_output_color[k];
999   }
1000   dt_control_queue_redraw_widget(self->widget);
1001 }
1002 
dt_iop_tonecurve_sanity_check(dt_iop_module_t * self,GtkWidget * widget)1003 static void dt_iop_tonecurve_sanity_check(dt_iop_module_t *self, GtkWidget *widget)
1004 {
1005   dt_iop_tonecurve_gui_data_t *c = (dt_iop_tonecurve_gui_data_t *)self->gui_data;
1006   dt_iop_tonecurve_params_t *p = (dt_iop_tonecurve_params_t *)self->params;
1007 
1008   int ch = c->channel;
1009   int nodes = p->tonecurve_nodes[ch];
1010   dt_iop_tonecurve_node_t *tonecurve = p->tonecurve[ch];
1011   int autoscale_ab = p->tonecurve_autoscale_ab;
1012 
1013   // if autoscale_ab is on: do not modify a and b curves
1014   if((autoscale_ab != DT_S_SCALE_MANUAL) && ch != ch_L) return;
1015 
1016   if(nodes <= 2) return;
1017 
1018   const float mx = tonecurve[c->selected].x;
1019 
1020   // delete vertex if order has changed
1021   // for all points, x coordinate of point must be strictly larger than
1022   // the x coordinate of the previous point
1023   if((c->selected > 0 && (tonecurve[c->selected - 1].x >= mx))
1024      || (c->selected < nodes - 1 && (tonecurve[c->selected + 1].x <= mx)))
1025   {
1026     for(int k = c->selected; k < nodes - 1; k++)
1027     {
1028       tonecurve[k].x = tonecurve[k + 1].x;
1029       tonecurve[k].y = tonecurve[k + 1].y;
1030     }
1031     c->selected = -2; // avoid re-insertion of that point immediately after this
1032     p->tonecurve_nodes[ch]--;
1033   }
1034 }
1035 
_move_point_internal(dt_iop_module_t * self,GtkWidget * widget,float dx,float dy,guint state)1036 static gboolean _move_point_internal(dt_iop_module_t *self, GtkWidget *widget, float dx, float dy, guint state)
1037 {
1038   dt_iop_tonecurve_params_t *p = (dt_iop_tonecurve_params_t *)self->params;
1039   dt_iop_tonecurve_gui_data_t *c = (dt_iop_tonecurve_gui_data_t *)self->gui_data;
1040 
1041   int ch = c->channel;
1042   dt_iop_tonecurve_node_t *tonecurve = p->tonecurve[ch];
1043 
1044   float multiplier;
1045 
1046   if(dt_modifier_is(state, GDK_SHIFT_MASK))
1047   {
1048     multiplier = dt_conf_get_float("darkroom/ui/scale_rough_step_multiplier");
1049   }
1050   else if(dt_modifier_is(state, GDK_CONTROL_MASK))
1051   {
1052     multiplier = dt_conf_get_float("darkroom/ui/scale_precise_step_multiplier");
1053   }
1054   else
1055   {
1056     multiplier = dt_conf_get_float("darkroom/ui/scale_step_multiplier");
1057   }
1058 
1059   dx *= multiplier;
1060   dy *= multiplier;
1061 
1062   tonecurve[c->selected].x = CLAMP(tonecurve[c->selected].x + dx, 0.0f, 1.0f);
1063   tonecurve[c->selected].y = CLAMP(tonecurve[c->selected].y + dy, 0.0f, 1.0f);
1064 
1065   dt_iop_tonecurve_sanity_check(self, widget);
1066 
1067   gtk_widget_queue_draw(widget);
1068 
1069   dt_iop_queue_history_update(self, FALSE);
1070   return TRUE;
1071 }
1072 
1073 #define TONECURVE_DEFAULT_STEP (0.001f)
1074 
_scrolled(GtkWidget * widget,GdkEventScroll * event,gpointer user_data)1075 static gboolean _scrolled(GtkWidget *widget, GdkEventScroll *event, gpointer user_data)
1076 {
1077   dt_iop_module_t *self = (dt_iop_module_t *)user_data;
1078   dt_iop_tonecurve_params_t *p = (dt_iop_tonecurve_params_t *)self->params;
1079   dt_iop_tonecurve_gui_data_t *c = (dt_iop_tonecurve_gui_data_t *)self->gui_data;
1080 
1081   if(dt_gui_ignore_scroll(event)) return FALSE;
1082 
1083   int ch = c->channel;
1084   int autoscale_ab = p->tonecurve_autoscale_ab;
1085 
1086   // if autoscale_ab is on: do not modify a and b curves
1087   if((autoscale_ab != DT_S_SCALE_MANUAL) && ch != ch_L) return TRUE;
1088 
1089   if(c->selected < 0) return TRUE;
1090 
1091   gdouble delta_y;
1092   if(dt_gui_get_scroll_delta(event, &delta_y))
1093   {
1094     delta_y *= -TONECURVE_DEFAULT_STEP;
1095     return _move_point_internal(self, widget, 0.0, delta_y, event->state);
1096   }
1097 
1098   return TRUE;
1099 }
1100 
dt_iop_tonecurve_key_press(GtkWidget * widget,GdkEventKey * event,gpointer user_data)1101 static gboolean dt_iop_tonecurve_key_press(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
1102 {
1103   dt_iop_module_t *self = (dt_iop_module_t *)user_data;
1104   dt_iop_tonecurve_params_t *p = (dt_iop_tonecurve_params_t *)self->params;
1105   dt_iop_tonecurve_gui_data_t *c = (dt_iop_tonecurve_gui_data_t *)self->gui_data;
1106 
1107   int ch = c->channel;
1108   int autoscale_ab = p->tonecurve_autoscale_ab;
1109 
1110   // if autoscale_ab is on: do not modify a and b curves
1111   if((autoscale_ab != DT_S_SCALE_MANUAL) && ch != ch_L) return FALSE;
1112 
1113   if(c->selected < 0) return FALSE;
1114 
1115   int handled = 0;
1116   float dx = 0.0f, dy = 0.0f;
1117   if(event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_KP_Up)
1118   {
1119     handled = 1;
1120     dy = TONECURVE_DEFAULT_STEP;
1121   }
1122   else if(event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_KP_Down)
1123   {
1124     handled = 1;
1125     dy = -TONECURVE_DEFAULT_STEP;
1126   }
1127   else if(event->keyval == GDK_KEY_Right || event->keyval == GDK_KEY_KP_Right)
1128   {
1129     handled = 1;
1130     dx = TONECURVE_DEFAULT_STEP;
1131   }
1132   else if(event->keyval == GDK_KEY_Left || event->keyval == GDK_KEY_KP_Left)
1133   {
1134     handled = 1;
1135     dx = -TONECURVE_DEFAULT_STEP;
1136   }
1137 
1138   if(!handled) return FALSE;
1139 
1140   return _move_point_internal(self, widget, dx, dy, event->state);
1141 }
1142 
1143 #undef TONECURVE_DEFAULT_STEP
1144 
gui_init(struct dt_iop_module_t * self)1145 void gui_init(struct dt_iop_module_t *self)
1146 {
1147   dt_iop_tonecurve_gui_data_t *c = IOP_GUI_ALLOC(tonecurve);
1148   dt_iop_tonecurve_params_t *p = (dt_iop_tonecurve_params_t *)self->default_params;
1149 
1150   for(int ch = 0; ch < ch_max; ch++)
1151   {
1152     c->minmax_curve[ch] = dt_draw_curve_new(0.0, 1.0, p->tonecurve_type[ch]);
1153     c->minmax_curve_nodes[ch] = p->tonecurve_nodes[ch];
1154     c->minmax_curve_type[ch] = p->tonecurve_type[ch];
1155     for(int k = 0; k < p->tonecurve_nodes[ch]; k++)
1156       (void)dt_draw_curve_add_point(c->minmax_curve[ch], p->tonecurve[ch][k].x, p->tonecurve[ch][k].y);
1157   }
1158 
1159   c->channel = ch_L;
1160   c->mouse_x = c->mouse_y = -1.0;
1161   c->selected = -1;
1162   c->loglogscale = 0;
1163   c->semilog = 0;
1164   self->timeout_handle = 0;
1165 
1166   c->autoscale_ab = dt_bauhaus_combobox_from_params(self, "tonecurve_autoscale_ab");
1167   gtk_widget_set_tooltip_text(c->autoscale_ab, _("if set to auto, a and b curves have no effect and are "
1168                                                  "not displayed. chroma values (a and b) of each pixel are "
1169                                                  "then adjusted based on L curve data. auto XYZ is similar "
1170                                                  "but applies the saturation changes in XYZ space."));
1171   GtkWidget *hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1172 
1173   static dt_action_def_t notebook_def = { };
1174   c->channel_tabs = dt_ui_notebook_new(&notebook_def);
1175   dt_action_define_iop(self, NULL, N_("channel"), GTK_WIDGET(c->channel_tabs), &notebook_def);
1176   dt_ui_notebook_page(c->channel_tabs, N_("L"), _("tonecurve for L channel"));
1177   dt_ui_notebook_page(c->channel_tabs, N_("a"), _("tonecurve for a channel"));
1178   dt_ui_notebook_page(c->channel_tabs, N_("b"), _("tonecurve for b channel"));
1179   g_signal_connect(G_OBJECT(c->channel_tabs), "switch_page", G_CALLBACK(tab_switch), self);
1180   gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(c->channel_tabs), TRUE, TRUE, 0);
1181   gtk_box_pack_start(GTK_BOX(hbox), gtk_grid_new(), TRUE, TRUE, 0);
1182 
1183   c->colorpicker = dt_color_picker_new(self, DT_COLOR_PICKER_POINT_AREA, hbox);
1184   gtk_widget_set_tooltip_text(c->colorpicker, _("pick GUI color from image\nctrl+click or right-click to select an area"));
1185 
1186   gtk_box_pack_start(GTK_BOX(self->widget), hbox, FALSE, FALSE, 0);
1187 
1188   c->area = GTK_DRAWING_AREA(dtgtk_drawing_area_new_with_aspect_ratio(1.0));
1189   g_object_set_data(G_OBJECT(c->area), "iop-instance", self);
1190   dt_action_define_iop(self, NULL, N_("curve"), GTK_WIDGET(c->area), NULL);
1191   gtk_box_pack_start(GTK_BOX(self->widget), GTK_WIDGET(c->area), TRUE, TRUE, 0);
1192 
1193   // FIXME: that tooltip goes in the way of the numbers when you hover a node to get a reading
1194   //gtk_widget_set_tooltip_text(GTK_WIDGET(c->area), _("double click to reset curve"));
1195 
1196   gtk_widget_add_events(GTK_WIDGET(c->area), GDK_POINTER_MOTION_MASK | darktable.gui->scroll_mask
1197                                            | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1198                                            | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
1199   gtk_widget_set_can_focus(GTK_WIDGET(c->area), TRUE);
1200   g_signal_connect(G_OBJECT(c->area), "draw", G_CALLBACK(dt_iop_tonecurve_draw), self);
1201   g_signal_connect(G_OBJECT(c->area), "button-press-event", G_CALLBACK(dt_iop_tonecurve_button_press), self);
1202   g_signal_connect(G_OBJECT(c->area), "motion-notify-event", G_CALLBACK(dt_iop_tonecurve_motion_notify), self);
1203   g_signal_connect(G_OBJECT(c->area), "leave-notify-event", G_CALLBACK(dt_iop_tonecurve_leave_notify), self);
1204   g_signal_connect(G_OBJECT(c->area), "enter-notify-event", G_CALLBACK(dt_iop_tonecurve_enter_notify), self);
1205   g_signal_connect(G_OBJECT(c->area), "configure-event", G_CALLBACK(area_resized), self);
1206   g_signal_connect(G_OBJECT(c->area), "scroll-event", G_CALLBACK(_scrolled), self);
1207   g_signal_connect(G_OBJECT(c->area), "key-press-event", G_CALLBACK(dt_iop_tonecurve_key_press), self);
1208 
1209   /* From src/common/curve_tools.h :
1210     #define CUBIC_SPLINE 0
1211     #define CATMULL_ROM 1
1212     #define MONOTONE_HERMITE 2
1213   */
1214   c->interpolator = dt_bauhaus_combobox_new(self);
1215   dt_bauhaus_widget_set_label(c->interpolator, NULL, N_("interpolation method"));
1216   dt_bauhaus_combobox_add(c->interpolator, _("cubic spline"));
1217   dt_bauhaus_combobox_add(c->interpolator, _("centripetal spline"));
1218   dt_bauhaus_combobox_add(c->interpolator, _("monotonic spline"));
1219   gtk_box_pack_start(GTK_BOX(self->widget), c->interpolator , TRUE, TRUE, 0);
1220   gtk_widget_set_tooltip_text(c->interpolator, _("change this method if you see oscillations or cusps in the curve\n"
1221                                                  "- cubic spline is better to produce smooth curves but oscillates when nodes are too close\n"
1222                                                  "- centripetal is better to avoids cusps and oscillations with close nodes but is less smooth\n"
1223                                                  "- monotonic is better for accuracy of pure analytical functions (log, gamma, exp)\n"));
1224   g_signal_connect(G_OBJECT(c->interpolator), "value-changed", G_CALLBACK(interpolator_callback), self);
1225 
1226   c->preserve_colors = dt_bauhaus_combobox_from_params(self, "preserve_colors");
1227   gtk_widget_set_tooltip_text(c->preserve_colors, _("method to preserve colors when applying contrast"));
1228 
1229   c->logbase = dt_bauhaus_slider_new_with_range(self, 0.0f, 40.0f, 0.5f, 0.0f, 2);
1230   dt_bauhaus_widget_set_label(c->logbase, NULL, N_("scale for graph"));
1231   gtk_box_pack_start(GTK_BOX(self->widget), c->logbase , TRUE, TRUE, 0);
1232   g_signal_connect(G_OBJECT(c->logbase), "value-changed", G_CALLBACK(logbase_callback), self);
1233 
1234   c->sizegroup = GTK_SIZE_GROUP(gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL));
1235   gtk_size_group_add_widget(c->sizegroup, GTK_WIDGET(c->area));
1236   gtk_size_group_add_widget(c->sizegroup, GTK_WIDGET(c->channel_tabs));
1237 }
1238 
gui_cleanup(struct dt_iop_module_t * self)1239 void gui_cleanup(struct dt_iop_module_t *self)
1240 {
1241   dt_iop_tonecurve_gui_data_t *c = (dt_iop_tonecurve_gui_data_t *)self->gui_data;
1242   // this one we need to unref manually. not so the initially unowned widgets.
1243   g_object_unref(c->sizegroup);
1244   dt_draw_curve_destroy(c->minmax_curve[ch_L]);
1245   dt_draw_curve_destroy(c->minmax_curve[ch_a]);
1246   dt_draw_curve_destroy(c->minmax_curve[ch_b]);
1247   dt_iop_cancel_history_update(self);
1248 
1249   IOP_GUI_FREE;
1250 }
1251 
dt_iop_tonecurve_enter_notify(GtkWidget * widget,GdkEventCrossing * event,gpointer user_data)1252 static gboolean dt_iop_tonecurve_enter_notify(GtkWidget *widget, GdkEventCrossing *event, gpointer user_data)
1253 {
1254   gtk_widget_queue_draw(widget);
1255   return TRUE;
1256 }
1257 
dt_iop_tonecurve_leave_notify(GtkWidget * widget,GdkEventCrossing * event,gpointer user_data)1258 static gboolean dt_iop_tonecurve_leave_notify(GtkWidget *widget, GdkEventCrossing *event, gpointer user_data)
1259 {
1260   gtk_widget_queue_draw(widget);
1261   return TRUE;
1262 }
1263 
picker_scale(const float * in,float * out)1264 static void picker_scale(const float *in, float *out)
1265 {
1266   out[0] = CLAMP(in[0] / 100.0f, 0.0f, 1.0f);
1267   out[1] = CLAMP((in[1] + 128.0f) / 256.0f, 0.0f, 1.0f);
1268   out[2] = CLAMP((in[2] + 128.0f) / 256.0f, 0.0f, 1.0f);
1269 }
1270 
dt_iop_tonecurve_draw(GtkWidget * widget,cairo_t * crf,gpointer user_data)1271 static gboolean dt_iop_tonecurve_draw(GtkWidget *widget, cairo_t *crf, gpointer user_data)
1272 {
1273   dt_iop_module_t *self = (dt_iop_module_t *)user_data;
1274   dt_iop_tonecurve_gui_data_t *c = (dt_iop_tonecurve_gui_data_t *)self->gui_data;
1275   dt_iop_tonecurve_params_t *p = (dt_iop_tonecurve_params_t *)self->params;
1276   dt_iop_tonecurve_global_data_t *gd = (dt_iop_tonecurve_global_data_t *)self->global_data;
1277 
1278   int ch = c->channel;
1279   int nodes = p->tonecurve_nodes[ch];
1280   dt_iop_tonecurve_node_t *tonecurve = p->tonecurve[ch];
1281 
1282   if(c->minmax_curve_type[ch] != p->tonecurve_type[ch] || c->minmax_curve_nodes[ch] != p->tonecurve_nodes[ch])
1283   {
1284     dt_draw_curve_destroy(c->minmax_curve[ch]);
1285     c->minmax_curve[ch] = dt_draw_curve_new(0.0, 1.0, p->tonecurve_type[ch]);
1286     c->minmax_curve_nodes[ch] = p->tonecurve_nodes[ch];
1287     c->minmax_curve_type[ch] = p->tonecurve_type[ch];
1288     for(int k = 0; k < p->tonecurve_nodes[ch]; k++)
1289       (void)dt_draw_curve_add_point(c->minmax_curve[ch], p->tonecurve[ch][k].x, p->tonecurve[ch][k].y);
1290   }
1291   else
1292   {
1293     for(int k = 0; k < p->tonecurve_nodes[ch]; k++)
1294       dt_draw_curve_set_point(c->minmax_curve[ch], k, p->tonecurve[ch][k].x, p->tonecurve[ch][k].y);
1295   }
1296   dt_draw_curve_t *minmax_curve = c->minmax_curve[ch];
1297   dt_draw_curve_calc_values(minmax_curve, 0.0, 1.0, DT_IOP_TONECURVE_RES, c->draw_xs, c->draw_ys);
1298 
1299   float unbounded_coeffs[3];
1300   const float xm = tonecurve[nodes - 1].x;
1301   {
1302     const float x[4] = { 0.7f * xm, 0.8f * xm, 0.9f * xm, 1.0f * xm };
1303     const float y[4] = { c->draw_ys[CLAMP((int)(x[0] * DT_IOP_TONECURVE_RES), 0, DT_IOP_TONECURVE_RES - 1)],
1304                          c->draw_ys[CLAMP((int)(x[1] * DT_IOP_TONECURVE_RES), 0, DT_IOP_TONECURVE_RES - 1)],
1305                          c->draw_ys[CLAMP((int)(x[2] * DT_IOP_TONECURVE_RES), 0, DT_IOP_TONECURVE_RES - 1)],
1306                          c->draw_ys[CLAMP((int)(x[3] * DT_IOP_TONECURVE_RES), 0, DT_IOP_TONECURVE_RES - 1)] };
1307     dt_iop_estimate_exp(x, y, 4, unbounded_coeffs);
1308   }
1309 
1310   const int inset = DT_GUI_CURVE_EDITOR_INSET;
1311   GtkAllocation allocation;
1312   gtk_widget_get_allocation(widget, &allocation);
1313   int width = allocation.width, height = allocation.height;
1314   cairo_surface_t *cst = dt_cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
1315   cairo_t *cr = cairo_create(cst);
1316 
1317   cairo_translate(cr, inset, inset);
1318   width -= 2 * inset;
1319   height -= 2 * inset;
1320   char text[256];
1321 
1322   // Draw frame borders
1323   cairo_set_line_width(cr, DT_PIXEL_APPLY_DPI(0.5));
1324   set_color(cr, darktable.bauhaus->graph_border);
1325   cairo_rectangle(cr, 0, 0, width, height);
1326   cairo_stroke_preserve(cr);
1327 
1328   if (ch==ch_L)
1329   { // remove below black to white transition to improve readability of the graph
1330     cairo_set_source_rgb(cr, .3, .3, .3);
1331     cairo_rectangle(cr, 0, 0, width, height);
1332     cairo_fill(cr);
1333   }
1334   else
1335   {
1336     // Draw the background gradient along the diagonal
1337     // ch == 0 : black to white
1338     // ch == 1 : green to magenta
1339     // ch == 2 : blue to yellow
1340     const float origin[3][3] = { { 0.0f, 0.0f, 0.0f },                  // L = 0, @ (a, b) = 0
1341                                  { 0.0f, 231.0f/255.0f, 181.0f/255.0f },// a = -128 @ L = 75, b = 0
1342                                  { 0.0f, 30.0f/255.0f, 195.0f/255.0f}}; // b = -128 @ L = 75, a = 0
1343 
1344     const float destin[3][3] = { { 1.0f, 1.0f, 1.0f },                  // L = 100 @ (a, b) = 0
1345                                  { 1.0f, 0.0f, 192.0f/255.0f } ,        // a = 128 @ L = 75, b = 0
1346                                  { 215.0f/255.0f, 182.0f/255.0f, 0.0f}};// b = 128 @ L = 75, a = 0
1347 
1348    // since Cairo paints with sRGB at gamma 2.4, linear gradients are not linear and, at 50%,
1349    // we don't see the neutral grey we would expect in the middle of a linear gradient between
1350    // 2 complimentary colors. So we add it artificially, but that will break the smoothness
1351    // of the transition.
1352 
1353     // middle step for gradients (50 %)
1354     const float midgrey = to_log(0.45f, c->loglogscale, ch, c->semilog, 0);
1355 
1356     const float middle[3][3] = { { midgrey, midgrey, midgrey },   // L = 50 @ (a, b) = 0
1357                                  { 0.67f, 0.67f, 0.67f},          // L = 75 @ (a, b) = 0
1358                                  { 0.67f, 0.67f, 0.67f}};         // L = 75 @ (a, b) = 0
1359 
1360     const float opacities[3] = { 0.5f, 0.5f, 0.5f};
1361 
1362     cairo_pattern_t *pat;
1363     pat = cairo_pattern_create_linear (height, 0.0,  0.0, width);
1364     cairo_pattern_add_color_stop_rgba (pat, 1, origin[ch][0], origin[ch][1], origin[ch][2], opacities[ch]);
1365     cairo_pattern_add_color_stop_rgba (pat, 0.5, middle[ch][0], middle[ch][1], middle[ch][2], opacities[ch]);
1366     cairo_pattern_add_color_stop_rgba (pat, 0, destin[ch][0], destin[ch][1], destin[ch][2], opacities[ch]);
1367     cairo_set_source (cr, pat);
1368     cairo_fill (cr);
1369     cairo_pattern_destroy (pat);
1370   }
1371 
1372   // draw grid
1373   set_color(cr, darktable.bauhaus->graph_border);
1374 
1375   if (c->loglogscale > 0.0f && ch == ch_L )
1376   {
1377     if (c->semilog == 0)
1378     {
1379       dt_draw_loglog_grid(cr, 4, 0, height, width, 0, c->loglogscale + 1.0f);
1380     }
1381     else if (c->semilog == 1)
1382     {
1383       dt_draw_semilog_x_grid(cr, 4, 0, height, width, 0, c->loglogscale + 1.0f);
1384     }
1385     else if (c->semilog == -1)
1386     {
1387       dt_draw_semilog_y_grid(cr, 4, 0, height, width, 0, c->loglogscale + 1.0f);
1388     }
1389   }
1390   else
1391   {
1392     dt_draw_grid(cr, 4, 0, 0, width, height);
1393   }
1394 
1395   // draw identity line
1396   cairo_move_to(cr, 0, height);
1397   cairo_line_to(cr, width, 0);
1398   cairo_stroke(cr);
1399   cairo_translate(cr, 0, height);
1400 
1401   // draw histogram in background
1402   // only if module is enabled
1403   if(self->enabled)
1404   {
1405     float *raw_mean, *raw_min, *raw_max;
1406     float *raw_mean_output;
1407     dt_aligned_pixel_t picker_mean, picker_min, picker_max;
1408     const gboolean is_linear = darktable.lib->proxy.histogram.is_linear;
1409 
1410     raw_mean = gd->picked_color;
1411     raw_min = gd->picked_color_min;
1412     raw_max = gd->picked_color_max;
1413     raw_mean_output = gd->picked_output_color;
1414 
1415     const uint32_t *hist = self->histogram;
1416     const float hist_max = is_linear ? self->histogram_max[ch] : logf(1.0 + self->histogram_max[ch]);
1417     if(hist && hist_max > 0.0f)
1418     {
1419       cairo_save(cr);
1420       cairo_scale(cr, width / 255.0, -(height - DT_PIXEL_APPLY_DPI(5)) / hist_max);
1421       cairo_move_to(cr, 0, height);
1422       set_color(cr, darktable.bauhaus->inset_histogram);
1423 
1424       if (ch == ch_L && c->loglogscale > 0.0f)
1425       {
1426         dt_draw_histogram_8_log_base(cr, hist, 4, ch, is_linear, c->loglogscale + 1.0f);
1427       }
1428       else
1429       {
1430         dt_draw_histogram_8(cr, hist, 4, ch, is_linear);
1431       }
1432       cairo_restore(cr);
1433     }
1434 
1435     cairo_move_to(cr, 0, height);
1436     if(self->request_color_pick == DT_REQUEST_COLORPICK_MODULE &&
1437        gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(c->colorpicker)))
1438     {
1439       // the global live samples ...
1440       cairo_set_line_width(cr, DT_PIXEL_APPLY_DPI(3));
1441 
1442       for(GSList *samples = darktable.lib->proxy.colorpicker.live_samples; samples; samples = g_slist_next(samples))
1443       {
1444         dt_colorpicker_sample_t *sample = samples->data;
1445 
1446         picker_scale(sample->lab[DT_LIB_COLORPICKER_STATISTIC_MEAN], picker_mean);
1447         picker_scale(sample->lab[DT_LIB_COLORPICKER_STATISTIC_MIN], picker_min);
1448         picker_scale(sample->lab[DT_LIB_COLORPICKER_STATISTIC_MAX], picker_max);
1449 
1450         // Convert abcissa to log coordinates if needed
1451         picker_min[ch] = to_log(picker_min[ch], c->loglogscale, ch, c->semilog, 0);
1452         picker_max[ch] = to_log(picker_max[ch], c->loglogscale, ch, c->semilog, 0);
1453         picker_mean[ch] = to_log(picker_mean[ch], c->loglogscale, ch, c->semilog, 0);
1454 
1455         cairo_set_source_rgba(cr, 0.5, 0.7, 0.5, 0.35);
1456         cairo_rectangle(cr, width * picker_min[ch], 0, width * fmax(picker_max[ch] - picker_min[ch], 0.0f),
1457                         -height);
1458         cairo_fill(cr);
1459         cairo_set_source_rgba(cr, 0.5, 0.7, 0.5, 0.5);
1460         cairo_move_to(cr, width * picker_mean[ch], 0);
1461         cairo_line_to(cr, width * picker_mean[ch], -height);
1462         cairo_stroke(cr);
1463       }
1464 
1465       // ... and the local sample
1466       if(raw_max[0] >= 0.0f)
1467       {
1468         cairo_save(cr);
1469         PangoLayout *layout;
1470         PangoRectangle ink;
1471         PangoFontDescription *desc = pango_font_description_copy_static(darktable.bauhaus->pango_font_desc);
1472         pango_font_description_set_weight(desc, PANGO_WEIGHT_BOLD);
1473         pango_font_description_set_absolute_size(desc, PANGO_SCALE);
1474         layout = pango_cairo_create_layout(cr);
1475         pango_layout_set_font_description(layout, desc);
1476 
1477         picker_scale(raw_mean, picker_mean);
1478         picker_scale(raw_min, picker_min);
1479         picker_scale(raw_max, picker_max);
1480 
1481         // scale conservatively to 100% of width:
1482         snprintf(text, sizeof(text), "100.00 / 100.00 ( +100.00)");
1483         pango_layout_set_text(layout, text, -1);
1484         pango_layout_get_pixel_extents(layout, &ink, NULL);
1485         pango_font_description_set_absolute_size(desc, width*1.0/ink.width * PANGO_SCALE);
1486         pango_layout_set_font_description(layout, desc);
1487 
1488         picker_min[ch] = to_log(picker_min[ch], c->loglogscale, ch, c->semilog, 0);
1489         picker_max[ch] = to_log(picker_max[ch], c->loglogscale, ch, c->semilog, 0);
1490         picker_mean[ch] = to_log(picker_mean[ch], c->loglogscale, ch, c->semilog, 0);
1491 
1492         cairo_set_source_rgba(cr, 0.7, 0.5, 0.5, 0.35);
1493         cairo_rectangle(cr, width * picker_min[ch], 0, width * fmax(picker_max[ch] - picker_min[ch], 0.0f),
1494                         -height);
1495         cairo_fill(cr);
1496         cairo_set_source_rgba(cr, 0.9, 0.7, 0.7, 0.5);
1497         cairo_move_to(cr, width * picker_mean[ch], 0);
1498         cairo_line_to(cr, width * picker_mean[ch], -height);
1499         cairo_stroke(cr);
1500 
1501         snprintf(text, sizeof(text), "%.1f → %.1f", raw_mean[ch], raw_mean_output[ch]);
1502 
1503         set_color(cr, darktable.bauhaus->graph_fg);
1504         cairo_set_font_size(cr, DT_PIXEL_APPLY_DPI(0.04) * height);
1505         pango_layout_set_text(layout, text, -1);
1506         pango_layout_get_pixel_extents(layout, &ink, NULL);
1507         cairo_move_to(cr, 0.02f * width, -0.94 * height - ink.height - ink.y);
1508         pango_cairo_show_layout(cr, layout);
1509         cairo_stroke(cr);
1510         pango_font_description_free(desc);
1511         g_object_unref(layout);
1512         cairo_restore(cr);
1513       }
1514     }
1515   }
1516 
1517   // draw curve
1518   cairo_set_line_width(cr, DT_PIXEL_APPLY_DPI(3));
1519   set_color(cr, darktable.bauhaus->graph_fg);
1520 
1521   for(int k = 0; k < DT_IOP_TONECURVE_RES; k++)
1522   {
1523     const float xx = k / (DT_IOP_TONECURVE_RES - 1.0f);
1524     float yy;
1525 
1526     if(xx > xm)
1527     {
1528       yy = dt_iop_eval_exp(unbounded_coeffs, xx);
1529     }
1530     else
1531     {
1532       yy = c->draw_ys[k];
1533     }
1534 
1535     const float x = to_log(xx, c->loglogscale, ch, c->semilog, 0),
1536                 y = to_log(yy, c->loglogscale, ch, c->semilog, 1);
1537 
1538     cairo_line_to(cr, x * width, -height * y);
1539   }
1540   cairo_stroke(cr);
1541 
1542   // draw nodes positions
1543   cairo_set_line_width(cr, DT_PIXEL_APPLY_DPI(3));
1544   for(int k = 0; k < nodes; k++)
1545   {
1546     const float x = to_log(tonecurve[k].x, c->loglogscale, ch, c->semilog, 0),
1547                 y = to_log(tonecurve[k].y, c->loglogscale, ch, c->semilog, 1);
1548 
1549     cairo_arc(cr, x * width, -y * height, DT_PIXEL_APPLY_DPI(4), 0, 2. * M_PI);
1550     set_color(cr, darktable.bauhaus->graph_fg);
1551     cairo_stroke_preserve(cr);
1552     set_color(cr, darktable.bauhaus->graph_bg);
1553     cairo_fill(cr);
1554   }
1555 
1556   // draw selected cursor
1557   if(c->selected >= 0)
1558   {
1559     // draw information about current selected node
1560     PangoLayout *layout;
1561     PangoRectangle ink;
1562     PangoFontDescription *desc = pango_font_description_copy_static(darktable.bauhaus->pango_font_desc);
1563     pango_font_description_set_weight(desc, PANGO_WEIGHT_BOLD);
1564     pango_font_description_set_absolute_size(desc, PANGO_SCALE);
1565     layout = pango_cairo_create_layout(cr);
1566     pango_layout_set_font_description(layout, desc);
1567 
1568     // scale conservatively to 100% of width:
1569     snprintf(text, sizeof(text), "100.00 / 100.00 ( +100.00)");
1570     pango_layout_set_text(layout, text, -1);
1571     pango_layout_get_pixel_extents(layout, &ink, NULL);
1572     pango_font_description_set_absolute_size(desc, width*1.0/ink.width * PANGO_SCALE);
1573     pango_layout_set_font_description(layout, desc);
1574 
1575     const float min_scale_value = ch == ch_L ? 0.0f : -128.0f;
1576     const float max_scale_value = ch == ch_L ? 100.0f : 128.0f;
1577 
1578     const float x_node_value = tonecurve[c->selected].x * (max_scale_value - min_scale_value) + min_scale_value;
1579     const float y_node_value = tonecurve[c->selected].y * (max_scale_value - min_scale_value) + min_scale_value;
1580     const float d_node_value = y_node_value - x_node_value;
1581     snprintf(text, sizeof(text), "%.1f / %.1f ( %+.1f)", x_node_value, y_node_value, d_node_value);
1582 
1583     set_color(cr, darktable.bauhaus->graph_fg);
1584     pango_layout_set_text(layout, text, -1);
1585     pango_layout_get_pixel_extents(layout, &ink, NULL);
1586     cairo_move_to(cr, 0.98f * width - ink.width - ink.x, -0.02 * height - ink.height - ink.y);
1587     pango_cairo_show_layout(cr, layout);
1588     cairo_stroke(cr);
1589     pango_font_description_free(desc);
1590     g_object_unref(layout);
1591 
1592     // enlarge selected node
1593     set_color(cr, darktable.bauhaus->graph_fg_active);
1594     const float x = to_log(tonecurve[c->selected].x, c->loglogscale, ch, c->semilog, 0),
1595                 y = to_log(tonecurve[c->selected].y, c->loglogscale, ch, c->semilog, 1);
1596 
1597     cairo_arc(cr, x * width, -y * height, DT_PIXEL_APPLY_DPI(6), 0, 2. * M_PI);
1598     cairo_fill(cr);
1599   }
1600 
1601   cairo_destroy(cr);
1602   cairo_set_source_surface(crf, cst, 0, 0);
1603   cairo_paint(crf);
1604   cairo_surface_destroy(cst);
1605   return TRUE;
1606 }
1607 
_add_node(dt_iop_tonecurve_node_t * tonecurve,int * nodes,float x,float y)1608 static inline int _add_node(dt_iop_tonecurve_node_t *tonecurve, int *nodes, float x, float y)
1609 {
1610   int selected = -1;
1611   if(tonecurve[0].x > x)
1612     selected = 0;
1613   else
1614   {
1615     for(int k = 1; k < *nodes; k++)
1616     {
1617       if(tonecurve[k].x > x)
1618       {
1619         selected = k;
1620         break;
1621       }
1622     }
1623   }
1624   if(selected == -1) selected = *nodes;
1625   for(int i = *nodes; i > selected; i--)
1626   {
1627     tonecurve[i].x = tonecurve[i - 1].x;
1628     tonecurve[i].y = tonecurve[i - 1].y;
1629   }
1630   // found a new point
1631   tonecurve[selected].x = x;
1632   tonecurve[selected].y = y;
1633   (*nodes)++;
1634   return selected;
1635 }
1636 
dt_iop_tonecurve_motion_notify(GtkWidget * widget,GdkEventMotion * event,gpointer user_data)1637 static gboolean dt_iop_tonecurve_motion_notify(GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
1638 {
1639   dt_iop_module_t *self = (dt_iop_module_t *)user_data;
1640   dt_iop_tonecurve_gui_data_t *c = (dt_iop_tonecurve_gui_data_t *)self->gui_data;
1641   dt_iop_tonecurve_params_t *p = (dt_iop_tonecurve_params_t *)self->params;
1642 
1643   int ch = c->channel;
1644   int nodes = p->tonecurve_nodes[ch];
1645   dt_iop_tonecurve_node_t *tonecurve = p->tonecurve[ch];
1646   int autoscale_ab = p->tonecurve_autoscale_ab;
1647 
1648   // if autoscale_ab is on: do not modify a and b curves
1649   if((autoscale_ab != DT_S_SCALE_MANUAL) && ch != ch_L) goto finally;
1650 
1651   const int inset = DT_GUI_CURVE_EDITOR_INSET;
1652   GtkAllocation allocation;
1653   gtk_widget_get_allocation(widget, &allocation);
1654   int height = allocation.height - 2 * inset, width = allocation.width - 2 * inset;
1655   double old_m_x = c->mouse_x;
1656   double old_m_y = c->mouse_y;
1657   c->mouse_x = event->x - inset;
1658   c->mouse_y = event->y - inset;
1659 
1660   const float mx = CLAMP(c->mouse_x, 0, width) / width;
1661   const float my = 1.0f - CLAMP(c->mouse_y, 0, height) / height;
1662   const float linx = to_lin(mx, c->loglogscale, ch, c->semilog, 0),
1663               liny = to_lin(my, c->loglogscale, ch, c->semilog, 1);
1664 
1665   if(event->state & GDK_BUTTON1_MASK)
1666   {
1667     // got a vertex selected:
1668     if(c->selected >= 0)
1669     {
1670       // this is used to translate mause position in loglogscale to make this behavior unified with linear scale.
1671       const float translate_mouse_x = old_m_x / width - to_log(tonecurve[c->selected].x, c->loglogscale, ch, c->semilog, 0);
1672       const float translate_mouse_y = 1 - old_m_y / height - to_log(tonecurve[c->selected].y, c->loglogscale, ch, c->semilog, 1);
1673       // dx & dy are in linear coordinates
1674       const float dx = to_lin(c->mouse_x / width - translate_mouse_x, c->loglogscale, ch, c->semilog, 0)
1675                        - to_lin(old_m_x / width - translate_mouse_x, c->loglogscale, ch, c->semilog, 0);
1676       const float dy = to_lin(1 - c->mouse_y / height - translate_mouse_y, c->loglogscale, ch, c->semilog, 1)
1677                        - to_lin(1 - old_m_y / height - translate_mouse_y, c->loglogscale, ch, c->semilog, 1);
1678       return _move_point_internal(self, widget, dx, dy, event->state);
1679     }
1680     else if(nodes < DT_IOP_TONECURVE_MAXNODES && c->selected >= -1)
1681     {
1682       // no vertex was close, create a new one!
1683       c->selected = _add_node(tonecurve, &p->tonecurve_nodes[ch], linx, liny);
1684       dt_dev_add_history_item(darktable.develop, self, TRUE);
1685     }
1686   }
1687   else
1688   {
1689     // minimum area around the node to select it:
1690     float min = .04f;
1691     min *= min; // comparing against square
1692     int nearest = -1;
1693     for(int k = 0; k < nodes; k++)
1694     {
1695       float dist
1696           = (my - to_log(tonecurve[k].y, c->loglogscale, ch, c->semilog, 1)) * (my - to_log(tonecurve[k].y, c->loglogscale, ch, c->semilog, 1))
1697             + (mx - to_log(tonecurve[k].x, c->loglogscale, ch, c->semilog, 0)) * (mx - to_log(tonecurve[k].x, c->loglogscale, ch, c->semilog, 0));
1698       if(dist < min)
1699       {
1700         min = dist;
1701         nearest = k;
1702       }
1703     }
1704     c->selected = nearest;
1705   }
1706 finally:
1707   if(c->selected >= 0) gtk_widget_grab_focus(widget);
1708   gtk_widget_queue_draw(widget);
1709   return TRUE;
1710 }
1711 
dt_iop_tonecurve_button_press(GtkWidget * widget,GdkEventButton * event,gpointer user_data)1712 static gboolean dt_iop_tonecurve_button_press(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
1713 {
1714   dt_iop_module_t *self = (dt_iop_module_t *)user_data;
1715   dt_iop_tonecurve_params_t *p = (dt_iop_tonecurve_params_t *)self->params;
1716   dt_iop_tonecurve_params_t *d = (dt_iop_tonecurve_params_t *)self->default_params;
1717   dt_iop_tonecurve_gui_data_t *c = (dt_iop_tonecurve_gui_data_t *)self->gui_data;
1718 
1719   int ch = c->channel;
1720   int autoscale_ab = p->tonecurve_autoscale_ab;
1721   int nodes = p->tonecurve_nodes[ch];
1722   dt_iop_tonecurve_node_t *tonecurve = p->tonecurve[ch];
1723 
1724   if(event->button == 1)
1725   {
1726     if(event->type == GDK_BUTTON_PRESS && dt_modifier_is(event->state, GDK_CONTROL_MASK)
1727        && nodes < DT_IOP_TONECURVE_MAXNODES && c->selected == -1)
1728     {
1729       // if we are not on a node -> add a new node at the current x of the pointer and y of the curve at that x
1730       const int inset = DT_GUI_CURVE_EDITOR_INSET;
1731       GtkAllocation allocation;
1732       gtk_widget_get_allocation(widget, &allocation);
1733       int width = allocation.width - 2 * inset;
1734       c->mouse_x = event->x - inset;
1735       c->mouse_y = event->y - inset;
1736 
1737       const float mx = CLAMP(c->mouse_x, 0, width) / (float)width;
1738       const float linx = to_lin(mx, c->loglogscale, ch, c->semilog, 0);
1739 
1740       // don't add a node too close to others in x direction, it can crash dt
1741       int selected = -1;
1742       if(tonecurve[0].x > mx)
1743         selected = 0;
1744       else
1745       {
1746         for(int k = 1; k < nodes; k++)
1747         {
1748           if(tonecurve[k].x > mx)
1749           {
1750             selected = k;
1751             break;
1752           }
1753         }
1754       }
1755       if(selected == -1) selected = nodes;
1756       // > 0 -> check distance to left neighbour
1757       // < nodes -> check distance to right neighbour
1758       if(!((selected > 0 && linx - tonecurve[selected - 1].x <= 0.025) ||
1759            (selected < nodes && tonecurve[selected].x - linx <= 0.025)))
1760       {
1761         // evaluate the curve at the current x position
1762         const float y = dt_draw_curve_calc_value(c->minmax_curve[ch], linx);
1763 
1764         if(y >= 0.0 && y <= 1.0) // never add something outside the viewport, you couldn't change it afterwards
1765         {
1766           // create a new node
1767           selected = _add_node(tonecurve, &p->tonecurve_nodes[ch], linx, y);
1768 
1769           // maybe set the new one as being selected
1770           float min = .04f;
1771           min *= min; // comparing against square
1772           for(int k = 0; k < nodes; k++)
1773           {
1774             float other_y = to_log(tonecurve[k].y, c->loglogscale, ch, c->semilog, 1);
1775             float dist = (y - other_y) * (y - other_y);
1776             if(dist < min) c->selected = selected;
1777           }
1778 
1779           dt_dev_add_history_item(darktable.develop, self, TRUE);
1780           gtk_widget_queue_draw(self->widget);
1781         }
1782       }
1783       return TRUE;
1784     }
1785     else if(event->type == GDK_2BUTTON_PRESS)
1786     {
1787       // reset current curve
1788       // if autoscale_ab is on: allow only reset of L curve
1789       if(!((autoscale_ab != DT_S_SCALE_MANUAL) && ch != ch_L))
1790       {
1791         p->tonecurve_nodes[ch] = d->tonecurve_nodes[ch];
1792         p->tonecurve_type[ch] = d->tonecurve_type[ch];
1793         for(int k = 0; k < d->tonecurve_nodes[ch]; k++)
1794         {
1795           p->tonecurve[ch][k].x = d->tonecurve[ch][k].x;
1796           p->tonecurve[ch][k].y = d->tonecurve[ch][k].y;
1797         }
1798         c->selected = -2; // avoid motion notify re-inserting immediately.
1799         dt_bauhaus_combobox_set(c->interpolator, p->tonecurve_type[ch_L]);
1800         dt_dev_add_history_item(darktable.develop, self, TRUE);
1801         gtk_widget_queue_draw(self->widget);
1802       }
1803       else
1804       {
1805         if(ch != ch_L)
1806         {
1807           p->tonecurve_autoscale_ab = DT_S_SCALE_MANUAL;
1808           c->selected = -2; // avoid motion notify re-inserting immediately.
1809           dt_bauhaus_combobox_set(c->autoscale_ab, 1);
1810           dt_dev_add_history_item(darktable.develop, self, TRUE);
1811           gtk_widget_queue_draw(self->widget);
1812         }
1813       }
1814       return TRUE;
1815     }
1816   }
1817   else if(event->button == 3 && c->selected >= 0)
1818   {
1819     if(c->selected == 0 || c->selected == nodes - 1)
1820     {
1821       float reset_value = c->selected == 0 ? 0 : 1;
1822       tonecurve[c->selected].y = tonecurve[c->selected].x = reset_value;
1823       gtk_widget_queue_draw(self->widget);
1824       dt_dev_add_history_item(darktable.develop, self, TRUE);
1825       return TRUE;
1826     }
1827 
1828     for(int k = c->selected; k < nodes - 1; k++)
1829     {
1830       tonecurve[k].x = tonecurve[k + 1].x;
1831       tonecurve[k].y = tonecurve[k + 1].y;
1832     }
1833     tonecurve[nodes - 1].x = tonecurve[nodes - 1].y = 0;
1834     c->selected = -2; // avoid re-insertion of that point immediately after this
1835     p->tonecurve_nodes[ch]--;
1836     gtk_widget_queue_draw(self->widget);
1837     dt_dev_add_history_item(darktable.develop, self, TRUE);
1838     return TRUE;
1839   }
1840   return FALSE;
1841 }
1842 
1843 // modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
1844 // vim: shiftwidth=2 expandtab tabstop=2 cindent
1845 // kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
1846