~ubuntu-branches/ubuntu/saucy/darktable/saucy

« back to all changes in this revision

Viewing changes to src/iop/tonecurve.c

  • Committer: Bazaar Package Importer
  • Author(s): David Bremner
  • Date: 2011-04-14 23:42:12 UTC
  • Revision ID: james.westby@ubuntu.com-20110414234212-kuffcz5wiu18v6ra
Tags: upstream-0.8
ImportĀ upstreamĀ versionĀ 0.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    This file is part of darktable,
 
3
    copyright (c) 2009--2010 johannes hanika.
 
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 <stdlib.h>
 
22
#include <math.h>
 
23
#include <assert.h>
 
24
#include <string.h>
 
25
#include "iop/tonecurve.h"
 
26
#include "gui/histogram.h"
 
27
#include "gui/presets.h"
 
28
#include "develop/develop.h"
 
29
#include "control/control.h"
 
30
#include "gui/gtk.h"
 
31
#include "common/opencl.h"
 
32
 
 
33
#define DT_GUI_CURVE_EDITOR_INSET 5
 
34
#define DT_GUI_CURVE_INFL .3f
 
35
 
 
36
DT_MODULE(1)
 
37
 
 
38
const char *name()
 
39
{
 
40
  return _("tone curve");
 
41
}
 
42
 
 
43
 
 
44
int
 
45
groups ()
 
46
{
 
47
  return IOP_GROUP_CORRECT;
 
48
}
 
49
 
 
50
 
 
51
#ifdef HAVE_OPENCL
 
52
void
 
53
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 *roi_in, const dt_iop_roi_t *roi_out)
 
54
{
 
55
  dt_iop_tonecurve_data_t *d = (dt_iop_tonecurve_data_t *)piece->data;
 
56
  dt_iop_tonecurve_global_data_t *gd = (dt_iop_tonecurve_global_data_t *)self->data;
 
57
 
 
58
  cl_int err;
 
59
  const int devid = piece->pipe->devid;
 
60
  size_t sizes[] = {roi_in->width, roi_in->height, 1};
 
61
  cl_mem dev_m = dt_opencl_copy_host_to_device(d->table, 256, 256, devid, sizeof(float));
 
62
  dt_opencl_set_kernel_arg(darktable.opencl, devid, gd->kernel_tonecurve, 0, sizeof(cl_mem), (void *)&dev_in);
 
63
  dt_opencl_set_kernel_arg(darktable.opencl, devid, gd->kernel_tonecurve, 1, sizeof(cl_mem), (void *)&dev_out);
 
64
  dt_opencl_set_kernel_arg(darktable.opencl, devid, gd->kernel_tonecurve, 2, sizeof(cl_mem), (void *)&dev_m);
 
65
  err = dt_opencl_enqueue_kernel_2d(darktable.opencl, devid, gd->kernel_tonecurve, sizes);
 
66
  if(err != CL_SUCCESS) fprintf(stderr, "couldn't enqueue tonecurve kernel! %d\n", err);
 
67
  clReleaseMemObject(dev_m);
 
68
}
 
69
#endif
 
70
 
 
71
void process (struct dt_iop_module_t *self, dt_dev_pixelpipe_iop_t *piece, void *i, void *o, const dt_iop_roi_t *roi_in, const dt_iop_roi_t *roi_out)
 
72
{
 
73
  const int ch = piece->colors;
 
74
  dt_iop_tonecurve_data_t *d = (dt_iop_tonecurve_data_t *)(piece->data);
 
75
#ifdef _OPENMP
 
76
#pragma omp parallel for default(none) shared(roi_out, i, o, d) schedule(static)
 
77
#endif
 
78
  for(int k=0; k<roi_out->height; k++)
 
79
  {
 
80
    float *in = ((float *)i) + k*ch*roi_out->width;
 
81
    float *out = ((float *)o) + k*ch*roi_out->width;
 
82
    for (int j=0; j<roi_out->width; j++,in+=ch,out+=ch)
 
83
    {
 
84
      // in Lab: correct compressed Luminance for saturation:
 
85
      const int t = CLAMP((int)(in[0]/100.0*0xfffful), 0, 0xffff);
 
86
      out[0] = d->table[t];
 
87
      if(in[0] > 0.01f)
 
88
      {
 
89
        out[1] = in[1] * out[0]/in[0];
 
90
        out[2] = in[2] * out[0]/in[0];
 
91
      }
 
92
      else
 
93
      {
 
94
        out[1] = in[1];
 
95
        out[2] = in[2];
 
96
      }
 
97
    }
 
98
  }
 
99
}
 
100
 
 
101
void init_presets (dt_iop_module_so_t *self)
 
102
{
 
103
  dt_iop_tonecurve_params_t p;
 
104
  p.tonecurve_preset = 0;
 
105
 
 
106
  float linear[6] = {0.0, 0.08, 0.4, 0.6, 0.92, 1.0};
 
107
  for(int k=0; k<6; k++) p.tonecurve_x[k] = linear[k];
 
108
  for(int k=0; k<6; k++) p.tonecurve_y[k] = linear[k];
 
109
  dt_gui_presets_add_generic(_("linear"), self->op, &p, sizeof(p), 1);
 
110
 
 
111
  for(int k=0; k<6; k++) p.tonecurve_x[k] = linear[k];
 
112
  for(int k=0; k<6; k++) p.tonecurve_y[k] = linear[k];
 
113
  p.tonecurve_y[1] -= 0.03;
 
114
  p.tonecurve_y[4] += 0.03;
 
115
  p.tonecurve_y[2] -= 0.03;
 
116
  p.tonecurve_y[3] += 0.03;
 
117
  for(int k=1; k<5; k++) p.tonecurve_y[k] = powf(p.tonecurve_y[k], 2.2f);
 
118
  for(int k=1; k<5; k++) p.tonecurve_x[k] = powf(p.tonecurve_x[k], 2.2f);
 
119
  dt_gui_presets_add_generic(_("med contrast"), self->op, &p, sizeof(p), 1);
 
120
 
 
121
  for(int k=0; k<6; k++) p.tonecurve_x[k] = linear[k];
 
122
  for(int k=0; k<6; k++) p.tonecurve_y[k] = linear[k];
 
123
  p.tonecurve_y[1] -= 0.06;
 
124
  p.tonecurve_y[4] += 0.06;
 
125
  p.tonecurve_y[2] -= 0.10;
 
126
  p.tonecurve_y[3] += 0.10;
 
127
  for(int k=1; k<5; k++) p.tonecurve_y[k] = powf(p.tonecurve_y[k], 2.2f);
 
128
  for(int k=1; k<5; k++) p.tonecurve_x[k] = powf(p.tonecurve_x[k], 2.2f);
 
129
  dt_gui_presets_add_generic(_("high contrast"), self->op, &p, sizeof(p), 1);
 
130
}
 
131
 
 
132
void commit_params (struct dt_iop_module_t *self, dt_iop_params_t *p1, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
 
133
{
 
134
  // pull in new params to gegl
 
135
  dt_iop_tonecurve_data_t *d = (dt_iop_tonecurve_data_t *)(piece->data);
 
136
  dt_iop_tonecurve_params_t *p = (dt_iop_tonecurve_params_t *)p1;
 
137
#ifdef HAVE_GEGL
 
138
  for(int k=0; k<6; k++) dt_draw_curve_set_point(d->curve, k, p->tonecurve_x[k], p->tonecurve_y[k]);
 
139
  gegl_node_set(piece->input, "curve", d->curve, NULL);
 
140
#else
 
141
  for(int k=0; k<6; k++)
 
142
    dt_draw_curve_set_point(d->curve, k, p->tonecurve_x[k], p->tonecurve_y[k]);
 
143
  dt_draw_curve_calc_values(d->curve, 0.0f, 1.0f, 0x10000, NULL, d->table);
 
144
  for(int k=0; k<0x10000; k++) d->table[k] *= 100.0f;
 
145
  // for(int k=0;k<0x10000;k++)
 
146
  // // d->table[k] = (uint16_t)(0xffff*dt_draw_curve_calc_value(d->curve, (1.0/0x10000)*k));
 
147
  // d->table[k] = 100.0*dt_draw_curve_calc_value(d->curve, (1.0/0x10000)*k);
 
148
#endif
 
149
}
 
150
 
 
151
void init_pipe (struct dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
 
152
{
 
153
  // create part of the gegl pipeline
 
154
  dt_iop_tonecurve_data_t *d = (dt_iop_tonecurve_data_t *)malloc(sizeof(dt_iop_tonecurve_data_t));
 
155
  dt_iop_tonecurve_params_t *default_params = (dt_iop_tonecurve_params_t *)self->default_params;
 
156
  piece->data = (void *)d;
 
157
  d->curve = dt_draw_curve_new(0.0, 1.0, CUBIC_SPLINE);
 
158
  for(int k=0; k<6; k++) (void)dt_draw_curve_add_point(d->curve, default_params->tonecurve_x[k], default_params->tonecurve_y[k]);
 
159
#ifdef HAVE_GEGL
 
160
  piece->input = piece->output = gegl_node_new_child(pipe->gegl, "operation", "gegl:dt-contrast-curve", "sampling-points", 65535, "curve", d->curve, NULL);
 
161
#else
 
162
  for(int k=0; k<0x10000; k++) d->table[k] = 100.0f*k/0x10000; // identity
 
163
#endif
 
164
}
 
165
 
 
166
void cleanup_pipe (struct dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
 
167
{
 
168
  // clean up everything again.
 
169
#ifdef HAVE_GEGL
 
170
  // dt_iop_tonecurve_data_t *d = (dt_iop_tonecurve_data_t *)(piece->data);
 
171
  (void)gegl_node_remove_child(pipe->gegl, piece->input);
 
172
  // (void)gegl_node_remove_child(pipe->gegl, d->node);
 
173
  // (void)gegl_node_remove_child(pipe->gegl, piece->output);
 
174
#endif
 
175
  dt_iop_tonecurve_data_t *d = (dt_iop_tonecurve_data_t *)(piece->data);
 
176
  dt_draw_curve_destroy(d->curve);
 
177
  free(piece->data);
 
178
}
 
179
 
 
180
void gui_update(struct dt_iop_module_t *self)
 
181
{
 
182
  // nothing to do, gui curve is read directly from params during expose event.
 
183
  gtk_widget_queue_draw(self->widget);
 
184
}
 
185
 
 
186
void init(dt_iop_module_t *module)
 
187
{
 
188
  module->params = malloc(sizeof(dt_iop_tonecurve_params_t));
 
189
  module->default_params = malloc(sizeof(dt_iop_tonecurve_params_t));
 
190
  module->default_enabled = 0;
 
191
  module->priority = 700;
 
192
  module->params_size = sizeof(dt_iop_tonecurve_params_t);
 
193
  module->gui_data = NULL;
 
194
  dt_iop_tonecurve_params_t tmp = (dt_iop_tonecurve_params_t)
 
195
  {
 
196
    {
 
197
      0.0, 0.08, 0.4, 0.6, 0.92, 1.0
 
198
    },
 
199
    {0.0, 0.08, 0.4, 0.6, 0.92, 1.0},
 
200
    0
 
201
  };
 
202
  memcpy(module->params, &tmp, sizeof(dt_iop_tonecurve_params_t));
 
203
  memcpy(module->default_params, &tmp, sizeof(dt_iop_tonecurve_params_t));
 
204
}
 
205
 
 
206
void init_global(dt_iop_module_so_t *module)
 
207
{
 
208
  const int program = 2; // basic.cl, from programs.conf
 
209
  dt_iop_tonecurve_global_data_t *gd = (dt_iop_tonecurve_global_data_t *)malloc(sizeof(dt_iop_tonecurve_global_data_t));
 
210
  module->data = gd;
 
211
  gd->kernel_tonecurve = dt_opencl_create_kernel(darktable.opencl, program, "tonecurve");
 
212
}
 
213
 
 
214
void cleanup_global(dt_iop_module_so_t *module)
 
215
{
 
216
  dt_iop_tonecurve_global_data_t *gd = (dt_iop_tonecurve_global_data_t *)module->data;
 
217
  dt_opencl_free_kernel(darktable.opencl, gd->kernel_tonecurve);
 
218
  free(module->data);
 
219
  module->data = NULL;
 
220
}
 
221
 
 
222
void cleanup(dt_iop_module_t *module)
 
223
{
 
224
  free(module->gui_data);
 
225
  module->gui_data = NULL;
 
226
  free(module->params);
 
227
  module->params = NULL;
 
228
}
 
229
 
 
230
void gui_init(struct dt_iop_module_t *self)
 
231
{
 
232
  self->gui_data = malloc(sizeof(dt_iop_tonecurve_gui_data_t));
 
233
  dt_iop_tonecurve_gui_data_t *c = (dt_iop_tonecurve_gui_data_t *)self->gui_data;
 
234
  dt_iop_tonecurve_params_t *p = (dt_iop_tonecurve_params_t *)self->params;
 
235
 
 
236
  c->minmax_curve = dt_draw_curve_new(0.0, 1.0, CUBIC_SPLINE);
 
237
  for(int k=0; k<6; k++) (void)dt_draw_curve_add_point(c->minmax_curve, p->tonecurve_x[k], p->tonecurve_y[k]);
 
238
  c->mouse_x = c->mouse_y = -1.0;
 
239
  c->selected = -1;
 
240
  c->selected_offset = c->selected_y = c->selected_min = c->selected_max = 0.0;
 
241
  c->dragging = 0;
 
242
  c->x_move = -1;
 
243
  self->widget = GTK_WIDGET(gtk_vbox_new(FALSE, 5));
 
244
  c->area = GTK_DRAWING_AREA(gtk_drawing_area_new());
 
245
  GtkWidget *asp = gtk_aspect_frame_new(NULL, 0.5, 0.5, 1.0, TRUE);
 
246
  gtk_box_pack_start(GTK_BOX(self->widget), asp, TRUE, TRUE, 0);
 
247
  gtk_container_add(GTK_CONTAINER(asp), GTK_WIDGET(c->area));
 
248
  // gtk_box_pack_start(GTK_BOX(self->widget), GTK_WIDGET(c->area), TRUE, TRUE, 0);
 
249
  gtk_drawing_area_size(c->area, 258, 258);
 
250
 
 
251
  gtk_widget_add_events(GTK_WIDGET(c->area), GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK);
 
252
  g_signal_connect (G_OBJECT (c->area), "expose-event",
 
253
  G_CALLBACK (dt_iop_tonecurve_expose), self);
 
254
  g_signal_connect (G_OBJECT (c->area), "button-press-event",
 
255
  G_CALLBACK (dt_iop_tonecurve_button_press), self);
 
256
  g_signal_connect (G_OBJECT (c->area), "button-release-event",
 
257
  G_CALLBACK (dt_iop_tonecurve_button_release), self);
 
258
  g_signal_connect (G_OBJECT (c->area), "motion-notify-event",
 
259
  G_CALLBACK (dt_iop_tonecurve_motion_notify), self);
 
260
  g_signal_connect (G_OBJECT (c->area), "leave-notify-event",
 
261
  G_CALLBACK (dt_iop_tonecurve_leave_notify), self);
 
262
}
 
263
 
 
264
void gui_cleanup(struct dt_iop_module_t *self)
 
265
{
 
266
  dt_iop_tonecurve_gui_data_t *c = (dt_iop_tonecurve_gui_data_t *)self->gui_data;
 
267
  dt_draw_curve_destroy(c->minmax_curve);
 
268
  free(self->gui_data);
 
269
  self->gui_data = NULL;
 
270
}
 
271
 
 
272
 
 
273
static gboolean dt_iop_tonecurve_leave_notify(GtkWidget *widget, GdkEventCrossing *event, gpointer user_data)
 
274
{
 
275
  dt_iop_module_t *self = (dt_iop_module_t *)user_data;
 
276
  dt_iop_tonecurve_gui_data_t *c = (dt_iop_tonecurve_gui_data_t *)self->gui_data;
 
277
  c->mouse_x = c->mouse_y = -1.0;
 
278
  gtk_widget_queue_draw(widget);
 
279
  return TRUE;
 
280
}
 
281
 
 
282
static gboolean dt_iop_tonecurve_expose(GtkWidget *widget, GdkEventExpose *event, gpointer user_data)
 
283
{
 
284
  dt_iop_module_t *self = (dt_iop_module_t *)user_data;
 
285
  dt_iop_tonecurve_gui_data_t *c = (dt_iop_tonecurve_gui_data_t *)self->gui_data;
 
286
  dt_iop_tonecurve_params_t *p = (dt_iop_tonecurve_params_t *)self->params;
 
287
  for(int k=0; k<6; k++) dt_draw_curve_set_point(c->minmax_curve, k, p->tonecurve_x[k], p->tonecurve_y[k]);
 
288
  const int inset = DT_GUI_CURVE_EDITOR_INSET;
 
289
  int width = widget->allocation.width, height = widget->allocation.height;
 
290
  cairo_surface_t *cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
 
291
  cairo_t *cr = cairo_create(cst);
 
292
  // clear bg
 
293
  cairo_set_source_rgb (cr, .2, .2, .2);
 
294
  cairo_paint(cr);
 
295
 
 
296
  cairo_translate(cr, inset, inset);
 
297
  width -= 2*inset;
 
298
  height -= 2*inset;
 
299
 
 
300
#if 0
 
301
  // draw shadow around
 
302
  float alpha = 1.0f;
 
303
  for(int k=0; k<inset; k++)
 
304
  {
 
305
    cairo_rectangle(cr, -k, -k, width + 2*k, height + 2*k);
 
306
    cairo_set_source_rgba(cr, 0, 0, 0, alpha);
 
307
    alpha *= 0.6f;
 
308
    cairo_fill(cr);
 
309
  }
 
310
#else
 
311
  cairo_set_line_width(cr, 1.0);
 
312
  cairo_set_source_rgb (cr, .1, .1, .1);
 
313
  cairo_rectangle(cr, 0, 0, width, height);
 
314
  cairo_stroke(cr);
 
315
#endif
 
316
 
 
317
  cairo_set_source_rgb (cr, .3, .3, .3);
 
318
  cairo_rectangle(cr, 0, 0, width, height);
 
319
  cairo_fill(cr);
 
320
 
 
321
  if(c->mouse_y > 0 || c->dragging)
 
322
  {
 
323
    float oldx1, oldy1;
 
324
    oldx1 = p->tonecurve_x[c->selected];
 
325
    oldy1 = p->tonecurve_y[c->selected];
 
326
 
 
327
    if(c->selected == 0) dt_draw_curve_set_point(c->minmax_curve, 1, p->tonecurve_x[1], fmaxf(c->selected_min, p->tonecurve_y[1]));
 
328
    if(c->selected == 2) dt_draw_curve_set_point(c->minmax_curve, 1, p->tonecurve_x[1], fminf(c->selected_min, fmaxf(0.0, p->tonecurve_y[1] + DT_GUI_CURVE_INFL*(c->selected_min - oldy1))));
 
329
    if(c->selected == 3) dt_draw_curve_set_point(c->minmax_curve, 4, p->tonecurve_x[4], fmaxf(c->selected_min, fminf(1.0, p->tonecurve_y[4] + DT_GUI_CURVE_INFL*(c->selected_min - oldy1))));
 
330
    if(c->selected == 5) dt_draw_curve_set_point(c->minmax_curve, 4, p->tonecurve_x[4], fminf(c->selected_min, p->tonecurve_y[4]));
 
331
    dt_draw_curve_set_point(c->minmax_curve, c->selected, oldx1, c->selected_min);
 
332
    dt_draw_curve_calc_values(c->minmax_curve, 0.0, 1.0, DT_IOP_TONECURVE_RES, c->draw_min_xs, c->draw_min_ys);
 
333
 
 
334
    if(c->selected == 0) dt_draw_curve_set_point(c->minmax_curve, 1, p->tonecurve_x[1], fmaxf(c->selected_max, p->tonecurve_y[1]));
 
335
    if(c->selected == 2) dt_draw_curve_set_point(c->minmax_curve, 1, p->tonecurve_x[1], fminf(c->selected_max, fmaxf(0.0, p->tonecurve_y[1] + DT_GUI_CURVE_INFL*(c->selected_max - oldy1))));
 
336
    if(c->selected == 3) dt_draw_curve_set_point(c->minmax_curve, 4, p->tonecurve_x[4], fmaxf(c->selected_max, fminf(1.0, p->tonecurve_y[4] + DT_GUI_CURVE_INFL*(c->selected_max - oldy1))));
 
337
    if(c->selected == 5) dt_draw_curve_set_point(c->minmax_curve, 4, p->tonecurve_x[4], fminf(c->selected_max, p->tonecurve_y[4]));
 
338
    dt_draw_curve_set_point  (c->minmax_curve, c->selected, oldx1, c->selected_max);
 
339
    dt_draw_curve_calc_values(c->minmax_curve, 0.0, 1.0, DT_IOP_TONECURVE_RES, c->draw_max_xs, c->draw_max_ys);
 
340
 
 
341
  }
 
342
  for(int k=0; k<6; k++) dt_draw_curve_set_point(c->minmax_curve, k, p->tonecurve_x[k], p->tonecurve_y[k]);
 
343
  dt_draw_curve_calc_values(c->minmax_curve, 0.0, 1.0, DT_IOP_TONECURVE_RES, c->draw_xs, c->draw_ys);
 
344
 
 
345
  // draw grid
 
346
  cairo_set_line_width(cr, .4);
 
347
  cairo_set_source_rgb (cr, .1, .1, .1);
 
348
  dt_draw_grid(cr, 4, 0, 0, width, height);
 
349
 
 
350
  // draw x positions
 
351
  cairo_set_line_width(cr, 1.);
 
352
  cairo_set_source_rgb(cr, 0.6, 0.6, 0.6);
 
353
  const float arrw = 7.0f;
 
354
  for(int k=1; k<5; k++)
 
355
  {
 
356
    cairo_move_to(cr, width*p->tonecurve_x[k], height+inset-1);
 
357
    cairo_rel_line_to(cr, -arrw*.5f, 0);
 
358
    cairo_rel_line_to(cr, arrw*.5f, -arrw);
 
359
    cairo_rel_line_to(cr, arrw*.5f, arrw);
 
360
    cairo_close_path(cr);
 
361
    if(c->x_move == k) cairo_fill(cr);
 
362
    else               cairo_stroke(cr);
 
363
  }
 
364
 
 
365
  // draw selected cursor
 
366
  cairo_set_line_width(cr, 1.);
 
367
  cairo_translate(cr, 0, height);
 
368
 
 
369
  // draw lum h istogram in background
 
370
  dt_develop_t *dev = darktable.develop;
 
371
  float *hist, hist_max;
 
372
  hist = dev->histogram_pre;
 
373
  hist_max = dev->histogram_pre_max;
 
374
  if(hist_max > 0)
 
375
  {
 
376
    cairo_save(cr);
 
377
    cairo_scale(cr, width/63.0, -(height-5)/(float)hist_max);
 
378
    cairo_set_source_rgba(cr, .2, .2, .2, 0.5);
 
379
    dt_gui_histogram_draw_8(cr, hist, 3);
 
380
    cairo_restore(cr);
 
381
  }
 
382
 
 
383
  if(c->mouse_y > 0 || c->dragging)
 
384
  {
 
385
    // draw min/max, if selected
 
386
    cairo_set_source_rgba(cr, .6, .6, .6, .5);
 
387
    cairo_move_to(cr, 0, - height*c->draw_min_ys[0]);
 
388
    for(int k=1; k<DT_IOP_TONECURVE_RES; k++)   cairo_line_to(cr, k*width/(DT_IOP_TONECURVE_RES-1.0), - height*c->draw_min_ys[k]);
 
389
    cairo_line_to(cr, width, - height*c->draw_min_ys[DT_IOP_TONECURVE_RES-1]);
 
390
    cairo_line_to(cr, width, - height*c->draw_max_ys[DT_IOP_TONECURVE_RES-1]);
 
391
    for(int k=DT_IOP_TONECURVE_RES-2; k>=0; k--) cairo_line_to(cr, k*width/(DT_IOP_TONECURVE_RES-1.0), - height*c->draw_max_ys[k]);
 
392
    cairo_close_path(cr);
 
393
    cairo_fill(cr);
 
394
    // draw mouse focus circle
 
395
    cairo_set_source_rgb(cr, .9, .9, .9);
 
396
    const float pos = MAX(0, (DT_IOP_TONECURVE_RES-1) * c->mouse_x/(float)width - 1);
 
397
    int k = (int)pos;
 
398
    const float f = k - pos;
 
399
    if(k >= DT_IOP_TONECURVE_RES-1) k = DT_IOP_TONECURVE_RES - 2;
 
400
    float ht = -height*(f*c->draw_ys[k] + (1-f)*c->draw_ys[k+1]);
 
401
    cairo_arc(cr, c->mouse_x, ht, 4, 0, 2.*M_PI);
 
402
    cairo_stroke(cr);
 
403
  }
 
404
 
 
405
  // draw curve
 
406
  cairo_set_line_width(cr, 2.);
 
407
  cairo_set_source_rgb(cr, .9, .9, .9);
 
408
  // cairo_set_line_cap  (cr, CAIRO_LINE_CAP_SQUARE);
 
409
  cairo_move_to(cr, 0, -height*c->draw_ys[0]);
 
410
  for(int k=1; k<DT_IOP_TONECURVE_RES; k++) cairo_line_to(cr, k*width/(DT_IOP_TONECURVE_RES-1.0), - height*c->draw_ys[k]);
 
411
  cairo_stroke(cr);
 
412
 
 
413
  cairo_destroy(cr);
 
414
  cairo_t *cr_pixmap = gdk_cairo_create(gtk_widget_get_window(widget));
 
415
  cairo_set_source_surface (cr_pixmap, cst, 0, 0);
 
416
  cairo_paint(cr_pixmap);
 
417
  cairo_destroy(cr_pixmap);
 
418
  cairo_surface_destroy(cst);
 
419
  return TRUE;
 
420
}
 
421
 
 
422
static gboolean dt_iop_tonecurve_motion_notify(GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
 
423
{
 
424
  dt_iop_module_t *self = (dt_iop_module_t *)user_data;
 
425
  dt_iop_tonecurve_gui_data_t *c = (dt_iop_tonecurve_gui_data_t *)self->gui_data;
 
426
  dt_iop_tonecurve_params_t *p = (dt_iop_tonecurve_params_t *)self->params;
 
427
  const int inset = DT_GUI_CURVE_EDITOR_INSET;
 
428
  int height = widget->allocation.height - 2*inset, width = widget->allocation.width - 2*inset;
 
429
  if(!c->dragging) c->mouse_x = CLAMP(event->x - inset, 0, width);
 
430
  c->mouse_y = CLAMP(event->y - inset, 0, height);
 
431
 
 
432
  if(c->dragging)
 
433
  {
 
434
    if(c->x_move >= 0)
 
435
    {
 
436
      const float mx = CLAMP(event->x - inset, 0, width)/(float)width;
 
437
      if(c->x_move > 0 && c->x_move < 6-1)
 
438
      {
 
439
        const float minx = p->tonecurve_x[c->x_move-1] + 0.001f;
 
440
        const float maxx = p->tonecurve_x[c->x_move+1] - 0.001f;
 
441
        p->tonecurve_x[c->x_move] = fminf(maxx, fmaxf(minx, mx));
 
442
      }
 
443
    }
 
444
    else
 
445
    {
 
446
      float f = c->selected_y - (c->mouse_y-c->selected_offset)/height;
 
447
      f = fmaxf(c->selected_min, fminf(c->selected_max, f));
 
448
      if(c->selected == 0) p->tonecurve_y[1] = fmaxf(f, p->tonecurve_y[1]);
 
449
      if(c->selected == 2) p->tonecurve_y[1] = fminf(f, fmaxf(0.0, p->tonecurve_y[1] + DT_GUI_CURVE_INFL*(f - p->tonecurve_y[2])));
 
450
      if(c->selected == 3) p->tonecurve_y[4] = fmaxf(f, fminf(1.0, p->tonecurve_y[4] + DT_GUI_CURVE_INFL*(f - p->tonecurve_y[3])));
 
451
      if(c->selected == 5) p->tonecurve_y[4] = fminf(f, p->tonecurve_y[4]);
 
452
      p->tonecurve_y[c->selected] = f;
 
453
    }
 
454
    dt_dev_add_history_item(darktable.develop, self, TRUE);
 
455
  }
 
456
  else
 
457
  {
 
458
    if(event->y > height)
 
459
    {
 
460
      c->x_move = 1;
 
461
      const float mx = CLAMP(event->x - inset, 0, width)/(float)width;
 
462
      float dist = fabsf(p->tonecurve_x[1] - mx);
 
463
      for(int k=2; k<5; k++)
 
464
      {
 
465
        float d2 = fabsf(p->tonecurve_x[k] - mx);
 
466
        if(d2 < dist)
 
467
        {
 
468
          c->x_move = k;
 
469
          dist = d2;
 
470
        }
 
471
      }
 
472
    }
 
473
    else
 
474
    {
 
475
      c->x_move = -1;
 
476
    }
 
477
    float pos = (event->x - inset)/width;
 
478
    float min = 100.0;
 
479
    int nearest = 0;
 
480
    for(int k=0; k<6; k++)
 
481
    {
 
482
      float dist = (pos - p->tonecurve_x[k]);
 
483
      dist *= dist;
 
484
      if(dist < min)
 
485
      {
 
486
        min = dist;
 
487
        nearest = k;
 
488
      }
 
489
    }
 
490
    c->selected = nearest;
 
491
    c->selected_y = p->tonecurve_y[c->selected];
 
492
    c->selected_offset = c->mouse_y;
 
493
    const float f = 0.8f;
 
494
    if(c->selected == 0)
 
495
    {
 
496
      c->selected_min = 0.0f;
 
497
      c->selected_max = 0.2f;
 
498
    }
 
499
    else if(c->selected == 5)
 
500
    {
 
501
      c->selected_min = 0.8f;
 
502
      c->selected_max = 1.0f;
 
503
    }
 
504
    else
 
505
    {
 
506
      c->selected_min = fmaxf(c->selected_y - 0.2f, (1.-f)*c->selected_y + f*p->tonecurve_y[c->selected-1]);
 
507
      c->selected_max = fminf(c->selected_y + 0.2f, (1.-f)*c->selected_y + f*p->tonecurve_y[c->selected+1]);
 
508
    }
 
509
    if(c->selected == 1) c->selected_max *= 0.7;
 
510
    if(c->selected == 4) c->selected_min = 1.0 - 0.7*(1.0 - c->selected_min);
 
511
  }
 
512
  gtk_widget_queue_draw(widget);
 
513
 
 
514
  gint x, y;
 
515
  gdk_window_get_pointer(event->window, &x, &y, NULL);
 
516
  return TRUE;
 
517
}
 
518
 
 
519
static gboolean dt_iop_tonecurve_button_press(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
 
520
{
 
521
  // set active point
 
522
  if(event->button == 1)
 
523
  {
 
524
    dt_iop_module_t *self = (dt_iop_module_t *)user_data;
 
525
    dt_iop_tonecurve_gui_data_t *c = (dt_iop_tonecurve_gui_data_t *)self->gui_data;
 
526
    c->dragging = 1;
 
527
    return TRUE;
 
528
  }
 
529
  return FALSE;
 
530
}
 
531
 
 
532
static gboolean dt_iop_tonecurve_button_release(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
 
533
{
 
534
  if(event->button == 1)
 
535
  {
 
536
    dt_iop_module_t *self = (dt_iop_module_t *)user_data;
 
537
    dt_iop_tonecurve_gui_data_t *c = (dt_iop_tonecurve_gui_data_t *)self->gui_data;
 
538
    c->dragging = 0;
 
539
    return TRUE;
 
540
  }
 
541
  return FALSE;
 
542
}
 
543