1 /*
2     This file is part of darktable,
3     Copyright (C) 2009-2020 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 "common/darktable.h"
22 #include "common/debug.h"
23 #include "common/imagebuf.h"
24 #include "control/control.h"
25 #include "develop/develop.h"
26 #include "develop/imageop.h"
27 #include "gui/draw.h"
28 #include "gui/gtk.h"
29 #include "gui/presets.h"
30 #include "iop/iop_api.h"
31 #include <assert.h>
32 #include <math.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #include "iop/equalizer_eaw.h"
37 
38 // #define DT_GUI_EQUALIZER_INSET 5
39 // #define DT_GUI_CURVE_INFL .3f
40 
41 DT_MODULE_INTROSPECTION(1, dt_iop_equalizer_params_t)
42 
43 
44 #define DT_IOP_EQUALIZER_RES 64
45 #define DT_IOP_EQUALIZER_BANDS 6
46 #define DT_IOP_EQUALIZER_MAX_LEVEL 6
47 
48 typedef struct dt_iop_equalizer_params_t
49 {
50   float equalizer_x[3][DT_IOP_EQUALIZER_BANDS], equalizer_y[3][DT_IOP_EQUALIZER_BANDS];
51 } dt_iop_equalizer_params_t;
52 
53 typedef enum dt_iop_equalizer_channel_t
54 {
55   DT_IOP_EQUALIZER_L = 0,
56   DT_IOP_EQUALIZER_a = 1,
57   DT_IOP_EQUALIZER_b = 2
58 } dt_iop_equalizer_channel_t;
59 
60 typedef struct dt_iop_equalizer_gui_data_t
61 {
62   dt_draw_curve_t *minmax_curve; // curve for gui to draw
63   GtkBox *hbox;
64   GtkDrawingArea *area;
65   GtkComboBox *presets;
66   GtkRadioButton *channel_button[3];
67   double mouse_x, mouse_y, mouse_pick;
68   float mouse_radius;
69   dt_iop_equalizer_params_t drag_params;
70   int dragging;
71   int x_move;
72   dt_iop_equalizer_channel_t channel;
73   float draw_xs[DT_IOP_EQUALIZER_RES], draw_ys[DT_IOP_EQUALIZER_RES];
74   float draw_min_xs[DT_IOP_EQUALIZER_RES], draw_min_ys[DT_IOP_EQUALIZER_RES];
75   float draw_max_xs[DT_IOP_EQUALIZER_RES], draw_max_ys[DT_IOP_EQUALIZER_RES];
76   float band_hist[DT_IOP_EQUALIZER_BANDS];
77   float band_max;
78 } dt_iop_equalizer_gui_data_t;
79 
80 typedef struct dt_iop_equalizer_data_t
81 {
82   dt_draw_curve_t *curve[3];
83   int num_levels;
84 } dt_iop_equalizer_data_t;
85 
86 
name()87 const char *name()
88 {
89   return _("legacy equalizer");
90 }
91 
92 
default_group()93 int default_group()
94 {
95   return IOP_GROUP_CORRECT | IOP_GROUP_EFFECTS;
96 }
97 
flags()98 int flags()
99 {
100   return IOP_FLAGS_DEPRECATED;
101 }
102 
default_colorspace(dt_iop_module_t * self,dt_dev_pixelpipe_t * pipe,dt_dev_pixelpipe_iop_t * piece)103 int default_colorspace(dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
104 {
105   return iop_cs_Lab;
106 }
107 
deprecated_msg()108 const char *deprecated_msg()
109 {
110   return _("this module is deprecated. better use contrast equalizer module instead.");
111 }
112 
113 
process(struct dt_iop_module_t * self,dt_dev_pixelpipe_iop_t * piece,const void * const ivoid,void * const ovoid,const dt_iop_roi_t * const roi_in,const dt_iop_roi_t * const roi_out)114 void process(struct dt_iop_module_t *self, dt_dev_pixelpipe_iop_t *piece, const void *const ivoid,
115              void *const ovoid, const dt_iop_roi_t *const roi_in, const dt_iop_roi_t *const roi_out)
116 {
117   const int chs = piece->colors;
118   const int width = roi_in->width, height = roi_in->height;
119   const float scale = roi_in->scale;
120   dt_iop_image_copy_by_size(ovoid, ivoid, width, height, chs);
121 #if 1
122   // printf("thread %d starting equalizer", (int)pthread_self());
123   // if(piece->iscale != 1.0) printf(" for preview\n");
124   // else printf("\n");
125   dt_iop_equalizer_data_t *d = (dt_iop_equalizer_data_t *)(piece->data);
126   // dt_iop_equalizer_gui_data_t *c = (dt_iop_equalizer_gui_data_t *)self->gui_data;
127 
128   // 1 pixel in this buffer represents 1.0/scale pixels in original image:
129   const float l1 = 1.0f + dt_log2f(piece->iscale / scale); // finest level
130   float lm = 0;
131   for(int k = MIN(width, height) * piece->iscale / scale; k; k >>= 1) lm++; // coarsest level
132   lm = MIN(DT_IOP_EQUALIZER_MAX_LEVEL, l1 + lm);
133   // level 1 => full resolution
134   int numl = 0;
135   for(int k = MIN(width, height); k; k >>= 1) numl++;
136   const int numl_cap = MIN(DT_IOP_EQUALIZER_MAX_LEVEL - l1 + 1.5, numl);
137   // printf("level range in %d %d: %f %f, cap: %d\n", 1, d->num_levels, l1, lm, numl_cap);
138 
139   // TODO: fixed alloc for data piece at capped resolution?
140   float **tmp = (float **)calloc(numl_cap, sizeof(float *));
141   for(int k = 1; k < numl_cap; k++)
142   {
143     const int wd = (int)(1 + (width >> (k - 1))), ht = (int)(1 + (height >> (k - 1)));
144     tmp[k] = (float *)malloc(sizeof(float) * wd * ht);
145   }
146 
147   for(int level = 1; level < numl_cap; level++) dt_iop_equalizer_wtf(ovoid, tmp, level, width, height);
148 
149 #if 0
150   // printf("transformed\n");
151   // store luma wavelet histogram for later drawing
152   if(self->dev->gui_attached && piece->iscale == 1.0 && self->dev->preview_pipe && c) // 1.0 => full pipe, only for gui applications.
153   {
154     float *out = (float *)ovoid;
155     // chose full pipe and current window.
156     int cnt[DT_IOP_EQUALIZER_BANDS];
157     for(int i=0; i<DT_IOP_EQUALIZER_BANDS; i++) cnt[i] = 0;
158     for(int l=1; l<numl_cap; l++)
159     {
160       const float lv = (lm-l1)*(l-1)/(float)(numl_cap-1) + l1; // appr level in real image.
161       const int band = CLAMP(.5f + (1.0 - lv / d->num_levels) * (DT_IOP_EQUALIZER_BANDS), 0, DT_IOP_EQUALIZER_BANDS);
162       c->band_hist[band] = 0.0f;
163       cnt[band]++;
164       int ch = (int)c->channel;
165       {
166         const int step = 1<<l;
167         for(int j=0; j<height; j+=step)      for(int i=step/2; i<width; i+=step) c->band_hist[band] += out[chs*width*j + chs*i + ch]*out[chs*width*j + chs*i + ch];
168         for(int j=step/2; j<height; j+=step) for(int i=0; i<width; i+=step)      c->band_hist[band] += out[chs*width*j + chs*i + ch]*out[chs*width*j + chs*i + ch];
169         for(int j=step/2; j<height; j+=step) for(int i=step/2; i<width; i+=step) c->band_hist[band] += out[chs*width*j + chs*i + ch]*out[chs*width*j + chs*i + ch]*.5f;
170       }
171     }
172     c->band_max = 0.0f;
173     for(int i=0; i<DT_IOP_EQUALIZER_BANDS; i++)
174     {
175       if(cnt[i]) c->band_hist[i] /= cnt[i];
176       else c->band_hist[i] = 0.0;
177       c->band_max = fmaxf(c->band_max, c->band_hist[i]);
178       // printf("band %d = %f\n", i, c->band_hist[i]);
179     }
180   }
181 #endif
182   // printf("histogrammed\n");
183 
184   for(int l = 1; l < numl_cap; l++)
185   {
186     float *out = (float *)ovoid;
187     const float lv = (lm - l1) * (l - 1) / (float)(numl_cap - 1) + l1; // appr level in real image.
188     const float band = CLAMP((1.0 - lv / d->num_levels), 0, 1.0);
189     for(int ch = 0; ch < 3; ch++)
190     {
191       // coefficients in range [0, 2], 1 being neutral.
192       const float coeff = 2 * dt_draw_curve_calc_value(d->curve[ch == 0 ? 0 : 1], band);
193       const int step = 1 << l;
194 #if 1 // scale coefficients
195       for(int j = 0; j < height; j += step)
196         for(int i = step / 2; i < width; i += step) out[(size_t)chs * width * j + (size_t)chs * i + ch] *= coeff;
197       for(int j = step / 2; j < height; j += step)
198         for(int i = 0; i < width; i += step) out[(size_t)chs * width * j + (size_t)chs * i + ch] *= coeff;
199       for(int j = step / 2; j < height; j += step)
200         for(int i = step / 2; i < width; i += step)
201           out[(size_t)chs * width * j + (size_t)chs * i + ch] *= coeff * coeff;
202 #else // soft-thresholding (shrinkage)
203 #define wshrink                                                                                              \
204   (copysignf(fmaxf(0.0f, fabsf(out[(size_t)chs * width * j + chs * i + ch]) - (1.0 - coeff)),                \
205              out[(size_t)chs * width * j + chs * i + ch]))
206       for(int j = 0; j < height; j += step)
207         for(int i = step / 2; i < width; i += step) out[(size_t)chs * width * j + chs * i + ch] = wshrink;
208       for(int j = step / 2; j < height; j += step)
209         for(int i = 0; i < width; i += step) out[(size_t)chs * width * j + chs * i + ch] = wshrink;
210       for(int j = step / 2; j < height; j += step)
211         for(int i = step / 2; i < width; i += step) out[(size_t)chs * width * j + chs * i + ch] = wshrink;
212 #undef wshrink
213 #endif
214     }
215   }
216   // printf("applied\n");
217   for(int level = numl_cap - 1; level > 0; level--) dt_iop_equalizer_iwtf(ovoid, tmp, level, width, height);
218 
219   for(int k = 1; k < numl_cap; k++) free(tmp[k]);
220   free(tmp);
221 // printf("thread %d finished equalizer", (int)pthread_self());
222 // if(piece->iscale != 1.0) printf(" for preview\n");
223 // else printf("\n");
224 #endif
225 }
226 
commit_params(struct dt_iop_module_t * self,dt_iop_params_t * p1,dt_dev_pixelpipe_t * pipe,dt_dev_pixelpipe_iop_t * piece)227 void commit_params(struct dt_iop_module_t *self, dt_iop_params_t *p1, dt_dev_pixelpipe_t *pipe,
228                    dt_dev_pixelpipe_iop_t *piece)
229 {
230   // pull in new params to pipe
231   dt_iop_equalizer_data_t *d = (dt_iop_equalizer_data_t *)(piece->data);
232   dt_iop_equalizer_params_t *p = (dt_iop_equalizer_params_t *)p1;
233 
234   for(int ch = 0; ch < 3; ch++)
235     for(int k = 0; k < DT_IOP_EQUALIZER_BANDS; k++)
236       dt_draw_curve_set_point(d->curve[ch], k, p->equalizer_x[ch][k], p->equalizer_y[ch][k]);
237   int l = 0;
238   for(int k = (int)MIN(pipe->iwidth * pipe->iscale, pipe->iheight * pipe->iscale); k; k >>= 1) l++;
239   d->num_levels = MIN(DT_IOP_EQUALIZER_MAX_LEVEL, l);
240 }
241 
init_pipe(struct dt_iop_module_t * self,dt_dev_pixelpipe_t * pipe,dt_dev_pixelpipe_iop_t * piece)242 void init_pipe(struct dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
243 {
244   // create part of the pixelpipe
245   dt_iop_equalizer_data_t *d = (dt_iop_equalizer_data_t *)malloc(sizeof(dt_iop_equalizer_data_t));
246   dt_iop_equalizer_params_t *default_params = (dt_iop_equalizer_params_t *)self->default_params;
247   piece->data = (void *)d;
248   for(int ch = 0; ch < 3; ch++)
249   {
250     d->curve[ch] = dt_draw_curve_new(0.0, 1.0, CATMULL_ROM);
251     for(int k = 0; k < DT_IOP_EQUALIZER_BANDS; k++)
252       (void)dt_draw_curve_add_point(d->curve[ch], default_params->equalizer_x[ch][k],
253                                     default_params->equalizer_y[ch][k]);
254   }
255   int l = 0;
256   for(int k = (int)MIN(pipe->iwidth * pipe->iscale, pipe->iheight * pipe->iscale); k; k >>= 1) l++;
257   d->num_levels = MIN(DT_IOP_EQUALIZER_MAX_LEVEL, l);
258 }
259 
cleanup_pipe(struct dt_iop_module_t * self,dt_dev_pixelpipe_t * pipe,dt_dev_pixelpipe_iop_t * piece)260 void cleanup_pipe(struct dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
261 {
262 // clean up everything again.
263   dt_iop_equalizer_data_t *d = (dt_iop_equalizer_data_t *)(piece->data);
264   for(int ch = 0; ch < 3; ch++) dt_draw_curve_destroy(d->curve[ch]);
265   free(piece->data);
266   piece->data = NULL;
267 }
268 
gui_update(struct dt_iop_module_t * self)269 void gui_update(struct dt_iop_module_t *self)
270 {
271   // nothing to do, gui curve is read directly from params during expose event.
272   // gtk_widget_queue_draw(self->widget);
273 }
274 
init(dt_iop_module_t * module)275 void init(dt_iop_module_t *module)
276 {
277   module->params = calloc(1, sizeof(dt_iop_equalizer_params_t));
278   module->default_params = calloc(1, sizeof(dt_iop_equalizer_params_t));
279   module->default_enabled = 0; // we're a rather slow and rare op.
280   module->params_size = sizeof(dt_iop_equalizer_params_t);
281   module->gui_data = NULL;
282   dt_iop_equalizer_params_t *d = module->default_params;
283   for(int ch = 0; ch < 3; ch++)
284   {
285     for(int k = 0; k < DT_IOP_EQUALIZER_BANDS; k++)
286       d->equalizer_x[ch][k] = k / (float)(DT_IOP_EQUALIZER_BANDS - 1);
287     for(int k = 0; k < DT_IOP_EQUALIZER_BANDS; k++) d->equalizer_y[ch][k] = 0.5f;
288   }
289 }
290 
291 #if 0
292 void init_presets (dt_iop_module_so_t *self)
293 {
294   DT_DEBUG_SQLITE3_EXEC(darktable.db, "begin", NULL, NULL, NULL);
295   dt_iop_equalizer_params_t p;
296 
297   for(int k=0; k<DT_IOP_EQUALIZER_BANDS; k++)
298   {
299     p.equalizer_x[DT_IOP_EQUALIZER_L][k] = k/(DT_IOP_EQUALIZER_BANDS-1.0);
300     p.equalizer_x[DT_IOP_EQUALIZER_a][k] = k/(DT_IOP_EQUALIZER_BANDS-1.0);
301     p.equalizer_x[DT_IOP_EQUALIZER_b][k] = k/(DT_IOP_EQUALIZER_BANDS-1.0);
302     p.equalizer_y[DT_IOP_EQUALIZER_L][k] = .5f+.5f*k/(float)DT_IOP_EQUALIZER_BANDS;
303     p.equalizer_y[DT_IOP_EQUALIZER_a][k] = .5f;
304     p.equalizer_y[DT_IOP_EQUALIZER_b][k] = .5f;
305   }
306   dt_gui_presets_add_generic(_("sharpen (strong)"), self->op, self->version(), &p, sizeof(p), 1);
307   for(int k=0; k<DT_IOP_EQUALIZER_BANDS; k++)
308   {
309     p.equalizer_x[DT_IOP_EQUALIZER_L][k] = k/(DT_IOP_EQUALIZER_BANDS-1.0);
310     p.equalizer_x[DT_IOP_EQUALIZER_a][k] = k/(DT_IOP_EQUALIZER_BANDS-1.0);
311     p.equalizer_x[DT_IOP_EQUALIZER_b][k] = k/(DT_IOP_EQUALIZER_BANDS-1.0);
312     p.equalizer_y[DT_IOP_EQUALIZER_L][k] = .5f+.25f*k/(float)DT_IOP_EQUALIZER_BANDS;
313     p.equalizer_y[DT_IOP_EQUALIZER_a][k] = .5f;
314     p.equalizer_y[DT_IOP_EQUALIZER_b][k] = .5f;
315   }
316   dt_gui_presets_add_generic(C_("equalizer", "sharpen"), self->op, self->version(), &p, sizeof(p), 1);
317   for(int ch=0; ch<3; ch++)
318   {
319     for(int k=0; k<DT_IOP_EQUALIZER_BANDS; k++) p.equalizer_x[ch][k] = k/(float)(DT_IOP_EQUALIZER_BANDS-1);
320     for(int k=0; k<DT_IOP_EQUALIZER_BANDS; k++) p.equalizer_y[ch][k] = 0.5f;
321   }
322   dt_gui_presets_add_generic(_("null"), self->op, self->version(), &p, sizeof(p), 1);
323   for(int k=0; k<DT_IOP_EQUALIZER_BANDS; k++)
324   {
325     p.equalizer_x[DT_IOP_EQUALIZER_L][k] = k/(DT_IOP_EQUALIZER_BANDS-1.0);
326     p.equalizer_x[DT_IOP_EQUALIZER_a][k] = k/(DT_IOP_EQUALIZER_BANDS-1.0);
327     p.equalizer_x[DT_IOP_EQUALIZER_b][k] = k/(DT_IOP_EQUALIZER_BANDS-1.0);
328     p.equalizer_y[DT_IOP_EQUALIZER_L][k] = .5f-.2f*k/(float)DT_IOP_EQUALIZER_BANDS;
329     p.equalizer_y[DT_IOP_EQUALIZER_a][k] = fmaxf(0.0f, .5f-.3f*k/(float)DT_IOP_EQUALIZER_BANDS);
330     p.equalizer_y[DT_IOP_EQUALIZER_b][k] = fmaxf(0.0f, .5f-.3f*k/(float)DT_IOP_EQUALIZER_BANDS);
331   }
332   dt_gui_presets_add_generic(_("denoise"), self->op, self->version(), &p, sizeof(p), 1);
333   for(int k=0; k<DT_IOP_EQUALIZER_BANDS; k++)
334   {
335     p.equalizer_x[DT_IOP_EQUALIZER_L][k] = k/(DT_IOP_EQUALIZER_BANDS-1.0);
336     p.equalizer_x[DT_IOP_EQUALIZER_a][k] = k/(DT_IOP_EQUALIZER_BANDS-1.0);
337     p.equalizer_x[DT_IOP_EQUALIZER_b][k] = k/(DT_IOP_EQUALIZER_BANDS-1.0);
338     p.equalizer_y[DT_IOP_EQUALIZER_L][k] = .5f-.4f*k/(float)DT_IOP_EQUALIZER_BANDS;
339     p.equalizer_y[DT_IOP_EQUALIZER_a][k] = fmaxf(0.0f, .5f-.6f*k/(float)DT_IOP_EQUALIZER_BANDS);
340     p.equalizer_y[DT_IOP_EQUALIZER_b][k] = fmaxf(0.0f, .5f-.6f*k/(float)DT_IOP_EQUALIZER_BANDS);
341   }
342   dt_gui_presets_add_generic(_("denoise (strong)"), self->op, self->version(), &p, sizeof(p), 1);
343   DT_DEBUG_SQLITE3_EXEC(darktable.db, "commit", NULL, NULL, NULL);
344 }
345 #endif
346 
gui_init(struct dt_iop_module_t * self)347 void gui_init(struct dt_iop_module_t *self)
348 {
349   IOP_GUI_ALLOC(equalizer);
350 
351   self->widget = dt_ui_label_new(_("this module will be removed in the future\nand is only here so you can "
352                                    "switch it off\nand move to the new equalizer."));
353 
354 #if 0
355   dt_iop_equalizer_gui_data_t *c = (dt_iop_equalizer_gui_data_t *)self->gui_data;
356   dt_iop_equalizer_params_t *p = (dt_iop_equalizer_params_t *)self->params;
357 
358   c->band_max = 0;
359   c->channel = DT_IOP_EQUALIZER_L;
360   int ch = (int)c->channel;
361   c->minmax_curve = dt_draw_curve_new(0.0, 1.0, HERMITE_SPLINE);
362   for(int k=0; k<DT_IOP_EQUALIZER_BANDS; k++) (void)dt_draw_curve_add_point(c->minmax_curve, p->equalizer_x[ch][k], p->equalizer_y[ch][k]);
363   c->mouse_x = c->mouse_y = c->mouse_pick = -1.0;
364   c->dragging = 0;
365   c->x_move = -1;
366   c->mouse_radius = 1.0/DT_IOP_EQUALIZER_BANDS;
367 
368   self->widget = GTK_WIDGET(gtk_box_new(GTK_ORIENTATION_VERTICAL, 0));
369   c->area = GTK_DRAWING_AREA(gtk_drawing_area_new());
370   gtk_box_pack_start(GTK_BOX(self->widget), GTK_WIDGET(c->area), TRUE, TRUE, 0);
371   gtk_widget_set_size_request(GTK_WIDGET(c->area), 195, 195);
372 
373   gtk_widget_add_events(GTK_WIDGET(c->area), GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | darktable.gui.scroll_mask);
374   g_signal_connect (G_OBJECT (c->area), "draw",
375                     G_CALLBACK (dt_iop_equalizer_expose), self);
376   g_signal_connect (G_OBJECT (c->area), "button-press-event",
377                     G_CALLBACK (dt_iop_equalizer_button_press), self);
378   g_signal_connect (G_OBJECT (c->area), "button-release-event",
379                     G_CALLBACK (dt_iop_equalizer_button_release), self);
380   g_signal_connect (G_OBJECT (c->area), "motion-notify-event",
381                     G_CALLBACK (dt_iop_equalizer_motion_notify), self);
382   g_signal_connect (G_OBJECT (c->area), "leave-notify-event",
383                     G_CALLBACK (dt_iop_equalizer_leave_notify), self);
384   g_signal_connect (G_OBJECT (c->area), "scroll-event",
385                     G_CALLBACK (dt_iop_equalizer_scrolled), self);
386   // init gtk stuff
387   c->hbox = GTK_BOX(gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0));
388   gtk_box_pack_start(GTK_BOX(self->widget), GTK_WIDGET(c->hbox), FALSE, FALSE, 0);
389 
390   c->channel_button[0] = GTK_RADIO_BUTTON(gtk_radio_button_new_with_label(NULL, _("luma")));
391   c->channel_button[1] = GTK_RADIO_BUTTON(gtk_radio_button_new_with_label_from_widget(c->channel_button[0], _("chroma")));
392   // c->channel_button[2] = GTK_RADIO_BUTTON(gtk_radio_button_new_with_label_from_widget(c->channel_button[0], "b"));
393 
394   g_signal_connect (G_OBJECT (c->channel_button[0]), "toggled",
395                     G_CALLBACK (dt_iop_equalizer_button_toggled), self);
396   g_signal_connect (G_OBJECT (c->channel_button[1]), "toggled",
397                     G_CALLBACK (dt_iop_equalizer_button_toggled), self);
398   // g_signal_connect (G_OBJECT (c->channel_button[2]), "toggled",
399   //                   G_CALLBACK (dt_iop_equalizer_button_toggled), self);
400 
401   // gtk_box_pack_end(GTK_BOX(c->hbox), GTK_WIDGET(c->channel_button[2]), FALSE, FALSE, 5);
402   gtk_box_pack_end(GTK_BOX(c->hbox), GTK_WIDGET(c->channel_button[1]), FALSE, FALSE, 0);
403   gtk_box_pack_end(GTK_BOX(c->hbox), GTK_WIDGET(c->channel_button[0]), FALSE, FALSE, 0);
404 #endif
405 }
406 
407 #if 0
408 static gboolean dt_iop_equalizer_leave_notify(GtkWidget *widget, GdkEventCrossing *event, gpointer user_data)
409 {
410   dt_iop_module_t *self = (dt_iop_module_t *)user_data;
411   dt_iop_equalizer_gui_data_t *c = (dt_iop_equalizer_gui_data_t *)self->gui_data;
412   // c->mouse_radius = 1.0/DT_IOP_EQUALIZER_BANDS;
413   if(!c->dragging) c->mouse_x = c->mouse_y = -1.0;
414   gtk_widget_queue_draw(widget);
415   return TRUE;
416 }
417 
418 // fills in new parameters based on mouse position (in 0,1)
419 static void dt_iop_equalizer_get_params(dt_iop_equalizer_params_t *p, const int ch, const double mouse_x, const double mouse_y, const float rad)
420 {
421   for(int k=0; k<DT_IOP_EQUALIZER_BANDS; k++)
422   {
423     const float f = expf(-(mouse_x - p->equalizer_x[ch][k])*(mouse_x - p->equalizer_x[ch][k])/(rad*rad));
424     p->equalizer_y[ch][k] = (1-f)*p->equalizer_y[ch][k] + f*mouse_y;
425   }
426 }
427 
428 static gboolean dt_iop_equalizer_expose(GtkWidget *widget, cairo_t *crf, gpointer user_data)
429 {
430   dt_iop_module_t *self = (dt_iop_module_t *)user_data;
431   dt_iop_equalizer_gui_data_t *c = (dt_iop_equalizer_gui_data_t *)self->gui_data;
432   dt_iop_equalizer_params_t p = *(dt_iop_equalizer_params_t *)self->params;
433   int ch = (int)c->channel;
434   for(int k=0; k<DT_IOP_EQUALIZER_BANDS; k++) dt_draw_curve_set_point(c->minmax_curve, k, p.equalizer_x[ch][k], p.equalizer_y[ch][k]);
435   const int inset = DT_GUI_EQUALIZER_INSET;
436   int width = allocation.width, height = allocation.height;
437   cairo_surface_t *cst = dt_cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
438   cairo_t *cr = cairo_create(cst);
439   // clear bg
440   cairo_set_source_rgb (cr, .2, .2, .2);
441   cairo_paint(cr);
442 
443   cairo_translate(cr, inset, inset);
444   width -= 2*inset;
445   height -= 2*inset;
446 
447   cairo_set_line_width(cr, 1.0);
448   cairo_set_source_rgb (cr, .1, .1, .1);
449   cairo_rectangle(cr, 0, 0, width, height);
450   cairo_stroke(cr);
451 
452   cairo_set_source_rgb (cr, .3, .3, .3);
453   cairo_rectangle(cr, 0, 0, width, height);
454   cairo_fill(cr);
455 
456   if(c->mouse_y > 0 || c->dragging)
457   {
458     // draw min/max curves:
459     dt_iop_equalizer_get_params(&p, c->channel, c->mouse_x, 1., c->mouse_radius);
460     for(int k=0; k<DT_IOP_EQUALIZER_BANDS; k++)
461       dt_draw_curve_set_point(c->minmax_curve, k, p.equalizer_x[ch][k], p.equalizer_y[ch][k]);
462     dt_draw_curve_calc_values(c->minmax_curve, 0.0, 1.0, DT_IOP_EQUALIZER_RES, c->draw_min_xs, c->draw_min_ys);
463 
464     p = *(dt_iop_equalizer_params_t *)self->params;
465     dt_iop_equalizer_get_params(&p, c->channel, c->mouse_x, .0, c->mouse_radius);
466     for(int k=0; k<DT_IOP_EQUALIZER_BANDS; k++)
467       dt_draw_curve_set_point(c->minmax_curve, k, p.equalizer_x[ch][k], p.equalizer_y[ch][k]);
468     dt_draw_curve_calc_values(c->minmax_curve, 0.0, 1.0, DT_IOP_EQUALIZER_RES, c->draw_max_xs, c->draw_max_ys);
469   }
470 
471   // draw grid
472   cairo_set_line_width(cr, .4);
473   cairo_set_source_rgb (cr, .1, .1, .1);
474   dt_draw_grid(cr, 8, 0, 0, width, height);
475 
476   // draw x positions
477   cairo_set_line_width(cr, 1.);
478   cairo_set_source_rgb(cr, 0.6, 0.6, 0.6);
479   const float arrw = 7.0f;
480   for(int k=0; k<DT_IOP_EQUALIZER_BANDS; k++)
481   {
482     cairo_move_to(cr, width*p.equalizer_x[c->channel][k], height+inset-1);
483     cairo_rel_line_to(cr, -arrw*.5f, 0);
484     cairo_rel_line_to(cr, arrw*.5f, -arrw);
485     cairo_rel_line_to(cr, arrw*.5f, arrw);
486     cairo_close_path(cr);
487     if(c->x_move == k) cairo_fill(cr);
488     else               cairo_stroke(cr);
489   }
490 
491   // draw selected cursor
492   cairo_set_line_width(cr, 1.);
493   cairo_translate(cr, 0, height);
494 
495   // draw frequency histogram in bg.
496 #if 1
497   if(c->band_max > 0)
498   {
499     cairo_save(cr);
500     cairo_scale(cr, width/(DT_IOP_EQUALIZER_BANDS-1.0), -(height-5)/c->band_max);
501     cairo_set_source_rgba(cr, .2, .2, .2, 0.5);
502     cairo_move_to(cr, 0, 0);
503     for(int k=0; k<DT_IOP_EQUALIZER_BANDS; k++) cairo_line_to(cr, k, c->band_hist[k]);
504     cairo_line_to(cr, DT_IOP_EQUALIZER_BANDS-1.0, 0.);
505     cairo_close_path(cr);
506     cairo_fill(cr);
507     cairo_restore(cr);
508   }
509 #endif
510 
511   // cairo_set_operator(cr, CAIRO_OPERATOR_ADD);
512   cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
513   cairo_set_line_width(cr, 2.);
514   for(int i=0; i<3; i++)
515   {
516     // draw curves, selected last.
517     int ch = ((int)c->channel+i+1)%3;
518     if(ch == 2) continue;
519     switch(ch)
520     {
521       case DT_IOP_EQUALIZER_L:
522         cairo_set_source_rgba(cr, .6, .6, .6, .3);
523         break;
524       case DT_IOP_EQUALIZER_a:
525         cairo_set_source_rgba(cr, .4, .2, .0, .4);
526         break;
527       default: //case DT_IOP_EQUALIZER_b:
528         cairo_set_source_rgba(cr, 0., .2, .4, .4);
529         break;
530     }
531     p = *(dt_iop_equalizer_params_t *)self->params;
532     for(int k=0; k<DT_IOP_EQUALIZER_BANDS; k++)
533       dt_draw_curve_set_point(c->minmax_curve, k, p.equalizer_x[ch][k], p.equalizer_y[ch][k]);
534     dt_draw_curve_calc_values(c->minmax_curve, 0.0, 1.0, DT_IOP_EQUALIZER_RES, c->draw_xs, c->draw_ys);
535     // cairo_set_line_cap  (cr, CAIRO_LINE_CAP_SQUARE);
536     cairo_move_to(cr, 0, 0);
537     for(int k=0; k<DT_IOP_EQUALIZER_RES; k++) cairo_line_to(cr, k*width/(float)(DT_IOP_EQUALIZER_RES-1), - height*c->draw_ys[k]);
538     cairo_line_to(cr, width, 0);
539     cairo_close_path(cr);
540     cairo_stroke_preserve(cr);
541     cairo_fill(cr);
542   }
543 
544   // draw dots on knots
545   cairo_save(cr);
546   cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
547   cairo_set_line_width(cr, 1.);
548   for(int k=0; k<DT_IOP_EQUALIZER_BANDS; k++)
549   {
550     cairo_arc(cr, width*p.equalizer_x[c->channel][k], - height*p.equalizer_y[c->channel][k], 3.0, 0.0, 2.0*M_PI);
551     if(c->x_move == k) cairo_fill(cr);
552     else               cairo_stroke(cr);
553   }
554   cairo_restore(cr);
555 
556   if(c->mouse_y > 0 || c->dragging)
557   {
558     // draw min/max, if selected
559     // cairo_set_source_rgba(cr, .6, .6, .6, .5);
560     cairo_move_to(cr, 0, - height*c->draw_min_ys[0]);
561     for(int k=1; k<DT_IOP_EQUALIZER_RES; k++)    cairo_line_to(cr, k*width/(float)(DT_IOP_EQUALIZER_RES-1), - height*c->draw_min_ys[k]);
562     for(int k=DT_IOP_EQUALIZER_RES-2; k>=0; k--) cairo_line_to(cr, k*width/(float)(DT_IOP_EQUALIZER_RES-1), - height*c->draw_max_ys[k]);
563     cairo_close_path(cr);
564     cairo_fill(cr);
565     // draw mouse focus circle
566     cairo_set_source_rgba(cr, .9, .9, .9, .5);
567     const float pos = DT_IOP_EQUALIZER_RES * c->mouse_x;
568     int k = (int)pos;
569     const float f = k - pos;
570     if(k >= DT_IOP_EQUALIZER_RES-1) k = DT_IOP_EQUALIZER_RES - 2;
571     float ht = -height*(f*c->draw_ys[k] + (1-f)*c->draw_ys[k+1]);
572     cairo_arc(cr, c->mouse_x*width, ht, c->mouse_radius*width, 0, 2.*M_PI);
573     cairo_stroke(cr);
574   }
575 
576   cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
577 
578   cairo_destroy(cr);
579   cairo_set_source_surface (crf, cst, 0, 0);
580   cairo_paint(crf);
581   cairo_surface_destroy(cst);
582   return TRUE;
583 }
584 
585 static gboolean dt_iop_equalizer_motion_notify(GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
586 {
587   dt_iop_module_t *self = (dt_iop_module_t *)user_data;
588   dt_iop_equalizer_gui_data_t *c = (dt_iop_equalizer_gui_data_t *)self->gui_data;
589   dt_iop_equalizer_params_t *p = (dt_iop_equalizer_params_t *)self->params;
590   const int inset = DT_GUI_EQUALIZER_INSET;
591   int height = allocation.height - 2*inset, width = allocation.width - 2*inset;
592   if(!c->dragging) c->mouse_x = CLAMP(event->x - inset, 0, width)/(float)width;
593   c->mouse_y = 1.0 - CLAMP(event->y - inset, 0, height)/(float)height;
594   if(c->dragging)
595   {
596     *p = c->drag_params;
597     if(c->x_move >= 0)
598     {
599       const float mx = CLAMP(event->x - inset, 0, width)/(float)width;
600       if(c->x_move > 0 && c->x_move < DT_IOP_EQUALIZER_BANDS-1)
601       {
602         const float minx = p->equalizer_x[c->channel][c->x_move-1] + 0.001f;
603         const float maxx = p->equalizer_x[c->channel][c->x_move+1] - 0.001f;
604         p->equalizer_x[c->channel][c->x_move] = fminf(maxx, fmaxf(minx, mx));
605       }
606     }
607     else
608     {
609       dt_iop_equalizer_get_params(p, c->channel, c->mouse_x, c->mouse_y + c->mouse_pick, c->mouse_radius);
610     }
611     dt_dev_add_history_item(darktable.develop, self, TRUE);
612   }
613   else if(event->y > height)
614   {
615     c->x_move = 0;
616     float dist = fabsf(p->equalizer_x[c->channel][0] - c->mouse_x);
617     for(int k=1; k<DT_IOP_EQUALIZER_BANDS; k++)
618     {
619       float d2 = fabsf(p->equalizer_x[c->channel][k] - c->mouse_x);
620       if(d2 < dist)
621       {
622         c->x_move = k;
623         dist = d2;
624       }
625     }
626   }
627   else
628   {
629     c->x_move = -1;
630   }
631   gtk_widget_queue_draw(widget);
632 
633   return TRUE;
634 }
635 
636 static gboolean dt_iop_equalizer_button_press(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
637 {
638   // set active point
639   if(event->button == 1)
640   {
641     dt_iop_module_t *self = (dt_iop_module_t *)user_data;
642     dt_iop_equalizer_gui_data_t *c = (dt_iop_equalizer_gui_data_t *)self->gui_data;
643     c->drag_params = *(dt_iop_equalizer_params_t *)self->params;
644     const int inset = DT_GUI_EQUALIZER_INSET;
645     int height = allocation.height - 2*inset, width = allocation.width - 2*inset;
646     c->mouse_pick = dt_draw_curve_calc_value(c->minmax_curve, CLAMP(event->x - inset, 0, width)/(float)width);
647     c->mouse_pick -= 1.0 - CLAMP(event->y - inset, 0, height)/(float)height;
648     c->dragging = 1;
649     return TRUE;
650   }
651   return FALSE;
652 }
653 
654 static gboolean dt_iop_equalizer_button_release(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
655 {
656   if(event->button == 1)
657   {
658     dt_iop_module_t *self = (dt_iop_module_t *)user_data;
659     dt_iop_equalizer_gui_data_t *c = (dt_iop_equalizer_gui_data_t *)self->gui_data;
660     c->dragging = 0;
661     return TRUE;
662   }
663   return FALSE;
664 }
665 
666 static gboolean dt_iop_equalizer_scrolled(GtkWidget *widget, GdkEventScroll *event, gpointer user_data)
667 {
668   dt_iop_module_t *self = (dt_iop_module_t *)user_data;
669   dt_iop_equalizer_gui_data_t *c = (dt_iop_equalizer_gui_data_t *)self->gui_data;
670 
671   gdouble delta_y;
672   if(dt_gui_get_scroll_deltas(event, NULL, &delta_y))
673   {
674     c->mouse_radius = CLAMP(c->mouse_radius * (1.0 + 0.1 * delta_y), 0.25 / DT_IOP_EQUALIZER_BANDS, 1.0);
675     gtk_widget_queue_draw(widget);
676   }
677 
678   return TRUE;
679 }
680 
681 static void dt_iop_equalizer_button_toggled(GtkToggleButton *togglebutton, gpointer user_data)
682 {
683   dt_iop_module_t *self = (dt_iop_module_t *)user_data;
684   dt_iop_equalizer_gui_data_t *c = (dt_iop_equalizer_gui_data_t *)self->gui_data;
685   if(gtk_toggle_button_get_active(togglebutton))
686   {
687     for(int k=0; k<3; k++) if(c->channel_button[k] == GTK_RADIO_BUTTON(togglebutton))
688       {
689         c->channel = (dt_iop_equalizer_channel_t)k;
690         gtk_widget_queue_draw(self->widget);
691         return;
692       }
693   }
694 }
695 #endif
696 
697 // modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
698 // vim: shiftwidth=2 expandtab tabstop=2 cindent
699 // kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
700