1 /** -*- C++ -*-
2  *
3  *  This file is part of RawTherapee.
4  *
5  *  Copyright (c) 2017 Alberto Griggio <alberto.griggio@gmail.com>
6  *
7  *  RawTherapee is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  RawTherapee is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with RawTherapee.  If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 // adapted from the "color correction" module of Darktable. Original copyright follows
22 /*
23     copyright (c) 2009--2010 johannes hanika.
24 
25     darktable is free software: you can redistribute it and/or modify
26     it under the terms of the GNU General Public License as published by
27     the Free Software Foundation, either version 3 of the License, or
28     (at your option) any later version.
29 
30     darktable is distributed in the hope that it will be useful,
31     but WITHOUT ANY WARRANTY; without even the implied warranty of
32     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33     GNU General Public License for more details.
34 
35     You should have received a copy of the GNU General Public License
36     along with darktable.  If not, see <https://www.gnu.org/licenses/>.
37 */
38 
39 #include "labgrid.h"
40 
41 #include "../rtengine/color.h"
42 #include "options.h"
43 #include "rtimage.h"
44 
45 using rtengine::Color;
46 
47 
48 //-----------------------------------------------------------------------------
49 // LabGridArea
50 //-----------------------------------------------------------------------------
51 
notifyListener()52 bool LabGridArea::notifyListener()
53 {
54     if (listener) {
55         const auto round =
56             [](float v) -> float
57             {
58                 return int(v * 1000) / 1000.f;
59             };
60         listener->panelChanged(evt, Glib::ustring::compose(evtMsg, round(high_a), round(high_b), round(low_a), round(low_b)));
61     }
62     return false;
63 }
64 
65 
LabGridArea(rtengine::ProcEvent evt,const Glib::ustring & msg,bool enable_low)66 LabGridArea::LabGridArea(rtengine::ProcEvent evt, const Glib::ustring &msg, bool enable_low):
67     Gtk::DrawingArea(),
68     evt(evt), evtMsg(msg),
69     litPoint(NONE),
70     low_a(0.f), high_a(0.f), low_b(0.f), high_b(0.f),
71     defaultLow_a(0.f), defaultHigh_a(0.f), defaultLow_b(0.f), defaultHigh_b(0.f),
72     listener(nullptr),
73     edited(false),
74     isDragged(false),
75     low_enabled(enable_low)
76 {
77     set_can_focus(false); // prevent moving the grid while you're moving a point
78     add_events(Gdk::EXPOSURE_MASK | Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK);
79     set_name("LabGrid");
80     get_style_context()->add_class("drawingarea");
81 }
82 
getParams(double & la,double & lb,double & ha,double & hb) const83 void LabGridArea::getParams(double &la, double &lb, double &ha, double &hb) const
84 {
85     la = low_a;
86     ha = high_a;
87     lb = low_b;
88     hb = high_b;
89 }
90 
91 
setParams(double la,double lb,double ha,double hb,bool notify)92 void LabGridArea::setParams(double la, double lb, double ha, double hb, bool notify)
93 {
94     const double lo = -1.0;
95     const double hi = 1.0;
96     low_a = rtengine::LIM(la, lo, hi);
97     low_b = rtengine::LIM(lb, lo, hi);
98     high_a = rtengine::LIM(ha, lo, hi);
99     high_b = rtengine::LIM(hb, lo, hi);
100     queue_draw();
101     if (notify) {
102         notifyListener();
103     }
104 }
105 
setDefault(double la,double lb,double ha,double hb)106 void LabGridArea::setDefault (double la, double lb, double ha, double hb)
107 {
108     defaultLow_a = la;
109     defaultLow_b = lb;
110     defaultHigh_a = ha;
111     defaultHigh_b = hb;
112 }
113 
114 
reset(bool toInitial)115 void LabGridArea::reset(bool toInitial)
116 {
117     if (toInitial) {
118         setParams(defaultLow_a, defaultLow_b, defaultHigh_a, defaultHigh_b, true);
119     } else {
120         setParams(0., 0., 0., 0., true);
121     }
122 }
123 
124 
setEdited(bool yes)125 void LabGridArea::setEdited(bool yes)
126 {
127     edited = yes;
128 }
129 
130 
getEdited() const131 bool LabGridArea::getEdited() const
132 {
133     return edited;
134 }
135 
136 
setListener(ToolPanelListener * l)137 void LabGridArea::setListener(ToolPanelListener *l)
138 {
139     listener = l;
140 }
141 
142 
on_style_updated()143 void LabGridArea::on_style_updated ()
144 {
145     setDirty(true);
146     queue_draw ();
147 }
148 
149 
on_draw(const::Cairo::RefPtr<Cairo::Context> & crf)150 bool LabGridArea::on_draw(const ::Cairo::RefPtr<Cairo::Context> &crf)
151 {
152     Gtk::Allocation allocation = get_allocation();
153     allocation.set_x(0);
154     allocation.set_y(0);
155 
156     // setDrawRectangle will allocate the backbuffer Surface
157     if (setDrawRectangle(Cairo::FORMAT_ARGB32, allocation)) {
158         setDirty(true);
159     }
160 
161     if (!isDirty() || !surfaceCreated()) {
162         return true;
163     }
164 
165     Glib::RefPtr<Gtk::StyleContext> style = get_style_context();
166     Gtk::Border padding = getPadding(style);  // already scaled
167     Cairo::RefPtr<Cairo::Context> cr = getContext();
168 
169     if (isDirty()) {
170         int width = allocation.get_width();
171         int height = allocation.get_height();
172 
173         int s = RTScalable::getScale();
174 
175         cr->set_line_cap(Cairo::LINE_CAP_SQUARE);
176 
177         // clear background
178         cr->set_source_rgba (0., 0., 0., 0.);
179         cr->set_operator (Cairo::OPERATOR_CLEAR);
180         cr->paint ();
181         cr->set_operator (Cairo::OPERATOR_OVER);
182         style->render_background(cr,
183                 inset * s + padding.get_left() - s,
184                 inset * s + padding.get_top() - s,
185                 width - 2 * inset * s - padding.get_right() - padding.get_left() + 2 * s,
186                 height - 2 * inset * s - padding.get_top() - padding.get_bottom() + 2 * s
187                 );
188 
189         // drawing the cells
190         cr->translate(inset * s + padding.get_left(), inset * s + padding.get_top());
191         cr->set_antialias(Cairo::ANTIALIAS_NONE);
192         width -= 2 * inset * s + padding.get_right() + padding.get_left();
193         height -= 2 * inset * s + padding.get_top() + padding.get_bottom();
194 
195         // flip y:
196         cr->translate(0, height);
197         cr->scale(1., -1.);
198         const int cells = 8;
199         float step = 12000.f / float(cells/2);
200         double cellW = double(width) / double(cells);
201         double cellH = double(height) / double(cells);
202         double cellYMin = 0.;
203         double cellYMax = std::floor(cellH);
204         for (int j = 0; j < cells; j++) {
205             double cellXMin = 0.;
206             double cellXMax = std::floor(cellW);
207             for (int i = 0; i < cells; i++) {
208                 float R, G, B;
209                 float x, y, z;
210                 int ii = i - cells/2;
211                 int jj = j - cells/2;
212                 float a = step * (ii + 0.5);
213                 float b = step * (jj + 0.5);
214                 Color::Lab2XYZ(25000.f, a, b, x, y, z);
215                 Color::xyz2srgb(x, y, z, R, G, B);
216                 cr->set_source_rgb(R / 65535.f, G / 65535.f, B / 65535.f);
217                 cr->rectangle(
218                         cellXMin,
219                         cellYMin,
220                         cellXMax - cellXMin - (i == cells-1 ? 0. : double(s)),
221                         cellYMax - cellYMin - (j == cells-1 ? 0. : double(s))
222                         );
223                 cellXMin = cellXMax;
224                 cellXMax = std::floor(cellW * double(i+2) + 0.01);
225                 cr->fill();
226             }
227             cellYMin = cellYMax;
228             cellYMax = std::floor(cellH * double(j+2) + 0.01);
229         }
230 
231         // drawing the connection line
232         cr->set_antialias(Cairo::ANTIALIAS_DEFAULT);
233         float loa, hia, lob, hib;
234         loa = .5f * (width + width * low_a);
235         hia = .5f * (width + width * high_a);
236         lob = .5f * (height + height * low_b);
237         hib = .5f * (height + height * high_b);
238         cr->set_line_width(2. * double(s));
239         cr->set_source_rgb(0.6, 0.6, 0.6);
240         cr->move_to(loa, lob);
241         cr->line_to(hia, hib);
242         cr->stroke();
243 
244         // drawing points
245         if (low_enabled) {
246             cr->set_source_rgb(0.1, 0.1, 0.1);
247             if (litPoint == LOW) {
248                 cr->arc(loa, lob, 5 * s, 0, 2. * rtengine::RT_PI);
249             } else {
250                 cr->arc(loa, lob, 3 * s, 0, 2. * rtengine::RT_PI);
251             }
252             cr->fill();
253         }
254 
255         cr->set_source_rgb(0.9, 0.9, 0.9);
256         if (litPoint == HIGH) {
257             cr->arc(hia, hib, 5 * s, 0, 2. * rtengine::RT_PI);
258         } else {
259             cr->arc(hia, hib, 3 * s, 0, 2. * rtengine::RT_PI);
260         }
261         cr->fill();
262     }
263 
264     copySurface(crf);
265     return false;
266 }
267 
268 
on_button_press_event(GdkEventButton * event)269 bool LabGridArea::on_button_press_event(GdkEventButton *event)
270 {
271     if (event->button == 1) {
272         if (event->type == GDK_2BUTTON_PRESS) {
273             switch (litPoint) {
274             case NONE:
275                 low_a = low_b = high_a = high_b = 0.f;
276                 break;
277             case LOW:
278                 low_a = low_b = 0.f;
279                 break;
280             case HIGH:
281                 high_a = high_b = 0.f;
282                 break;
283             }
284             edited = true;
285             notifyListener();
286             queue_draw();
287         } else if (event->type == GDK_BUTTON_PRESS && litPoint != NONE) {
288             isDragged = true;
289         }
290         return false;
291     }
292     return true;
293 }
294 
295 
on_button_release_event(GdkEventButton * event)296 bool LabGridArea::on_button_release_event(GdkEventButton *event)
297 {
298     if (event->button == 1) {
299         isDragged = false;
300         return false;
301     }
302     return true;
303 }
304 
305 
on_motion_notify_event(GdkEventMotion * event)306 bool LabGridArea::on_motion_notify_event(GdkEventMotion *event)
307 {
308     if (isDragged && delayconn.connected()) {
309         delayconn.disconnect();
310     }
311 
312     Glib::RefPtr<Gtk::StyleContext> style = get_style_context();
313     Gtk::Border padding = getPadding(style);  // already scaled
314 
315     State oldLitPoint = litPoint;
316 
317     int s = RTScalable::getScale();
318     int width = get_allocated_width() - 2 * inset * s - padding.get_right() - padding.get_left();
319     int height = get_allocated_height() - 2 * inset * s - padding.get_top() - padding.get_bottom();
320     const float mouse_x = std::min(double(std::max(event->x - inset * s - padding.get_right(), 0.)), double(width));
321     const float mouse_y = std::min(double(std::max(get_allocated_height() - 1 - event->y - inset * s - padding.get_bottom(), 0.)), double(height));
322     const float ma = (2.0 * mouse_x - width) / (float)width;
323     const float mb = (2.0 * mouse_y - height) / (float)height;
324     if (isDragged) {
325         if (litPoint == LOW) {
326             low_a = ma;
327             low_b = mb;
328         } else if (litPoint == HIGH) {
329             high_a = ma;
330             high_b = mb;
331         }
332         edited = true;
333         grab_focus();
334         if (options.adjusterMinDelay == 0) {
335             notifyListener();
336         } else {
337             delayconn = Glib::signal_timeout().connect(sigc::mem_fun(*this, &LabGridArea::notifyListener), options.adjusterMinDelay);
338         }
339         queue_draw();
340     } else {
341         litPoint = NONE;
342         float la = low_a;
343         float lb = low_b;
344         float ha = high_a;
345         float hb = high_b;
346         const float thrs = 0.05f;
347         const float distlo = (la - ma) * (la - ma) + (lb - mb) * (lb - mb);
348         const float disthi = (ha - ma) * (ha - ma) + (hb - mb) * (hb - mb);
349         if (low_enabled && distlo < thrs * thrs && distlo < disthi) {
350             litPoint = LOW;
351         } else if (disthi < thrs * thrs && disthi <= distlo) {
352             litPoint = HIGH;
353         }
354         if ((oldLitPoint == NONE && litPoint != NONE) || (oldLitPoint != NONE && litPoint == NONE)) {
355             queue_draw();
356         }
357     }
358     return true;
359 }
360 
361 
get_request_mode_vfunc() const362 Gtk::SizeRequestMode LabGridArea::get_request_mode_vfunc() const
363 {
364     return Gtk::SIZE_REQUEST_HEIGHT_FOR_WIDTH;
365 }
366 
367 
get_preferred_width_vfunc(int & minimum_width,int & natural_width) const368 void LabGridArea::get_preferred_width_vfunc(int &minimum_width, int &natural_width) const
369 {
370     Glib::RefPtr<Gtk::StyleContext> style = get_style_context();
371     Gtk::Border padding = getPadding(style);  // already scaled
372     int s = RTScalable::getScale();
373     int p = padding.get_left() + padding.get_right();
374 
375     minimum_width = 50 * s + p;
376     natural_width = 150 * s + p;  // same as GRAPH_SIZE from mycurve.h
377 }
378 
379 
get_preferred_height_for_width_vfunc(int width,int & minimum_height,int & natural_height) const380 void LabGridArea::get_preferred_height_for_width_vfunc(int width, int &minimum_height, int &natural_height) const
381 {
382     Glib::RefPtr<Gtk::StyleContext> style = get_style_context();
383     Gtk::Border padding = getPadding(style);  // already scaled
384 
385     minimum_height = natural_height = width - padding.get_left() - padding.get_right() + padding.get_top() + padding.get_bottom();
386 }
387 
388 
lowEnabled() const389 bool LabGridArea::lowEnabled() const
390 {
391     return low_enabled;
392 }
393 
394 
setLowEnabled(bool yes)395 void LabGridArea::setLowEnabled(bool yes)
396 {
397     if (low_enabled != yes) {
398         low_enabled = yes;
399         queue_draw();
400     }
401 }
402 
403 
404 //-----------------------------------------------------------------------------
405 // LabGrid
406 //-----------------------------------------------------------------------------
407 
LabGrid(rtengine::ProcEvent evt,const Glib::ustring & msg,bool enable_low)408 LabGrid::LabGrid(rtengine::ProcEvent evt, const Glib::ustring &msg, bool enable_low):
409     grid(evt, msg, enable_low)
410 {
411     Gtk::Button *reset = Gtk::manage(new Gtk::Button());
412     reset->set_tooltip_markup(M("ADJUSTER_RESET_TO_DEFAULT"));
413     reset->add(*Gtk::manage(new RTImage("undo-small.png", "redo-small.png")));
414     reset->signal_button_release_event().connect(sigc::mem_fun(*this, &LabGrid::resetPressed));
415 
416     setExpandAlignProperties(reset, false, false, Gtk::ALIGN_CENTER, Gtk::ALIGN_START);
417     reset->set_relief(Gtk::RELIEF_NONE);
418     reset->get_style_context()->add_class(GTK_STYLE_CLASS_FLAT);
419     reset->set_can_focus(false);
420     reset->set_size_request(-1, 20);
421 
422     pack_start(grid, true, true);
423     pack_start(*reset, false, false);
424     show_all_children();
425 }
426 
427 
resetPressed(GdkEventButton * event)428 bool LabGrid::resetPressed(GdkEventButton *event)
429 {
430     grid.reset(event->state & GDK_CONTROL_MASK);
431     return false;
432 }
433