1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18 #include "config.h"
19 
20 #include <gegl.h>
21 #include <gtk/gtk.h>
22 
23 #include "libgimpbase/gimpbase.h"
24 #include "libgimpmath/gimpmath.h"
25 #include "libgimpconfig/gimpconfig.h"
26 #include "libgimpwidgets/gimpwidgets.h"
27 
28 #include "tools-types.h"
29 
30 #include "gegl/gimp-babl.h"
31 
32 #include "operations/gimplevelsconfig.h"
33 #include "operations/gimpoperationlevels.h"
34 
35 #include "core/gimp-gui.h"
36 #include "core/gimpasync.h"
37 #include "core/gimpcancelable.h"
38 #include "core/gimpdrawable.h"
39 #include "core/gimpdrawable-histogram.h"
40 #include "core/gimperror.h"
41 #include "core/gimphistogram.h"
42 #include "core/gimpimage.h"
43 #include "core/gimptoolinfo.h"
44 #include "core/gimptriviallycancelablewaitable.h"
45 #include "core/gimpwaitable.h"
46 
47 #include "widgets/gimpcolorbar.h"
48 #include "widgets/gimphandlebar.h"
49 #include "widgets/gimphelp-ids.h"
50 #include "widgets/gimphistogramview.h"
51 #include "widgets/gimppropwidgets.h"
52 #include "widgets/gimpwidgets-constructors.h"
53 
54 #include "display/gimpdisplay.h"
55 
56 #include "gimphistogramoptions.h"
57 #include "gimplevelstool.h"
58 
59 #include "gimp-intl.h"
60 
61 
62 #define PICK_LOW_INPUT    (1 << 0)
63 #define PICK_GAMMA        (1 << 1)
64 #define PICK_HIGH_INPUT   (1 << 2)
65 #define PICK_ALL_CHANNELS (1 << 8)
66 
67 #define HISTOGRAM_WIDTH    256
68 #define GRADIENT_HEIGHT     12
69 #define CONTROL_HEIGHT      10
70 
71 
72 /*  local function prototypes  */
73 
74 static void       gimp_levels_tool_finalize       (GObject          *object);
75 
76 static gboolean   gimp_levels_tool_initialize     (GimpTool         *tool,
77                                                    GimpDisplay      *display,
78                                                    GError          **error);
79 
80 static gchar    * gimp_levels_tool_get_operation  (GimpFilterTool   *filter_tool,
81                                                    gchar           **description);
82 static void       gimp_levels_tool_dialog         (GimpFilterTool   *filter_tool);
83 static void       gimp_levels_tool_reset          (GimpFilterTool   *filter_tool);
84 static void       gimp_levels_tool_config_notify  (GimpFilterTool   *filter_tool,
85                                                    GimpConfig       *config,
86                                                    const GParamSpec *pspec);
87 static gboolean   gimp_levels_tool_settings_import(GimpFilterTool   *filter_tool,
88                                                    GInputStream     *input,
89                                                    GError          **error);
90 static gboolean   gimp_levels_tool_settings_export(GimpFilterTool   *filter_tool,
91                                                    GOutputStream    *output,
92                                                    GError          **error);
93 static void       gimp_levels_tool_color_picked   (GimpFilterTool   *filter_tool,
94                                                    gpointer          identifier,
95                                                    gdouble           x,
96                                                    gdouble           y,
97                                                    const Babl       *sample_format,
98                                                    const GimpRGB    *color);
99 
100 static void       gimp_levels_tool_export_setup   (GimpSettingsBox  *settings_box,
101                                                    GtkFileChooserDialog *dialog,
102                                                    gboolean          export,
103                                                    GimpLevelsTool   *tool);
104 
105 static void       levels_update_input_bar         (GimpLevelsTool   *tool);
106 
107 static void       levels_channel_callback         (GtkWidget        *widget,
108                                                    GimpFilterTool   *filter_tool);
109 static void       levels_channel_reset_callback   (GtkWidget        *widget,
110                                                    GimpFilterTool   *filter_tool);
111 
112 static gboolean   levels_menu_sensitivity         (gint              value,
113                                                    gpointer          data);
114 
115 static void       levels_stretch_callback         (GtkWidget        *widget,
116                                                    GimpLevelsTool   *tool);
117 static void       levels_linear_gamma_changed     (GtkAdjustment    *adjustment,
118                                                    GimpLevelsTool   *tool);
119 
120 static void       levels_to_curves_callback       (GtkWidget        *widget,
121                                                    GimpFilterTool   *filter_tool);
122 
123 
G_DEFINE_TYPE(GimpLevelsTool,gimp_levels_tool,GIMP_TYPE_FILTER_TOOL)124 G_DEFINE_TYPE (GimpLevelsTool, gimp_levels_tool, GIMP_TYPE_FILTER_TOOL)
125 
126 #define parent_class gimp_levels_tool_parent_class
127 
128 
129 void
130 gimp_levels_tool_register (GimpToolRegisterCallback  callback,
131                            gpointer                  data)
132 {
133   (* callback) (GIMP_TYPE_LEVELS_TOOL,
134                 GIMP_TYPE_HISTOGRAM_OPTIONS,
135                 gimp_color_options_gui,
136                 0,
137                 "gimp-levels-tool",
138                 _("Levels"),
139                 _("Adjust color levels"),
140                 N_("_Levels..."), NULL,
141                 NULL, GIMP_HELP_TOOL_LEVELS,
142                 GIMP_ICON_TOOL_LEVELS,
143                 data);
144 }
145 
146 static void
gimp_levels_tool_class_init(GimpLevelsToolClass * klass)147 gimp_levels_tool_class_init (GimpLevelsToolClass *klass)
148 {
149   GObjectClass        *object_class      = G_OBJECT_CLASS (klass);
150   GimpToolClass       *tool_class        = GIMP_TOOL_CLASS (klass);
151   GimpFilterToolClass *filter_tool_class = GIMP_FILTER_TOOL_CLASS (klass);
152 
153   object_class->finalize             = gimp_levels_tool_finalize;
154 
155   tool_class->initialize             = gimp_levels_tool_initialize;
156 
157   filter_tool_class->get_operation   = gimp_levels_tool_get_operation;
158   filter_tool_class->dialog          = gimp_levels_tool_dialog;
159   filter_tool_class->reset           = gimp_levels_tool_reset;
160   filter_tool_class->config_notify   = gimp_levels_tool_config_notify;
161   filter_tool_class->settings_import = gimp_levels_tool_settings_import;
162   filter_tool_class->settings_export = gimp_levels_tool_settings_export;
163   filter_tool_class->color_picked    = gimp_levels_tool_color_picked;
164 }
165 
166 static void
gimp_levels_tool_init(GimpLevelsTool * tool)167 gimp_levels_tool_init (GimpLevelsTool *tool)
168 {
169 }
170 
171 static void
gimp_levels_tool_finalize(GObject * object)172 gimp_levels_tool_finalize (GObject *object)
173 {
174   GimpLevelsTool *tool = GIMP_LEVELS_TOOL (object);
175 
176   g_clear_object (&tool->histogram);
177   g_clear_object (&tool->histogram_async);
178 
179   G_OBJECT_CLASS (parent_class)->finalize (object);
180 }
181 
182 static gboolean
gimp_levels_tool_initialize(GimpTool * tool,GimpDisplay * display,GError ** error)183 gimp_levels_tool_initialize (GimpTool     *tool,
184                              GimpDisplay  *display,
185                              GError      **error)
186 {
187   GimpFilterTool   *filter_tool = GIMP_FILTER_TOOL (tool);
188   GimpLevelsTool   *l_tool      = GIMP_LEVELS_TOOL (tool);
189   GimpImage        *image       = gimp_display_get_image (display);
190   GimpDrawable     *drawable    = gimp_image_get_active_drawable (image);
191   GimpLevelsConfig *config;
192   gdouble           scale_factor;
193   gdouble           step_increment;
194   gdouble           page_increment;
195   gint              digits;
196 
197   if (! GIMP_TOOL_CLASS (parent_class)->initialize (tool, display, error))
198     {
199       return FALSE;
200     }
201 
202   config = GIMP_LEVELS_CONFIG (filter_tool->config);
203 
204   g_clear_object (&l_tool->histogram);
205   g_clear_object (&l_tool->histogram_async);
206   l_tool->histogram = gimp_histogram_new (config->linear);
207 
208   l_tool->histogram_async = gimp_drawable_calculate_histogram_async (
209     drawable, l_tool->histogram, FALSE);
210   gimp_histogram_view_set_histogram (GIMP_HISTOGRAM_VIEW (l_tool->histogram_view),
211                                      l_tool->histogram);
212 
213   if (gimp_drawable_get_component_type (drawable) == GIMP_COMPONENT_TYPE_U8)
214     {
215       scale_factor   = 255.0;
216       step_increment = 1.0;
217       page_increment = 8.0;
218       digits         = 0;
219     }
220   else
221     {
222       scale_factor   = 100;
223       step_increment = 0.01;
224       page_increment = 1.0;
225       digits         = 2;
226     }
227 
228   gimp_prop_widget_set_factor (l_tool->low_input_spinbutton,
229                                scale_factor, step_increment, page_increment,
230                                digits);
231   gimp_prop_widget_set_factor (l_tool->high_input_spinbutton,
232                                scale_factor, step_increment, page_increment,
233                                digits);
234   gimp_prop_widget_set_factor (l_tool->low_output_spinbutton,
235                                scale_factor, step_increment, page_increment,
236                                digits);
237   gimp_prop_widget_set_factor (l_tool->high_output_spinbutton,
238                                scale_factor, step_increment, page_increment,
239                                digits);
240 
241   gtk_adjustment_configure (l_tool->gamma_linear,
242                             scale_factor / 2.0,
243                             0, scale_factor, 0.1, 1.0, 0);
244 
245   return TRUE;
246 }
247 
248 static gchar *
gimp_levels_tool_get_operation(GimpFilterTool * filter_tool,gchar ** description)249 gimp_levels_tool_get_operation (GimpFilterTool  *filter_tool,
250                                 gchar          **description)
251 {
252   *description = g_strdup (_("Adjust Color Levels"));
253 
254   return g_strdup ("gimp:levels");
255 }
256 
257 
258 /*******************/
259 /*  Levels dialog  */
260 /*******************/
261 
262 static GtkWidget *
gimp_levels_tool_color_picker_new(GimpLevelsTool * tool,guint value)263 gimp_levels_tool_color_picker_new (GimpLevelsTool *tool,
264                                    guint           value)
265 {
266   const gchar *icon_name;
267   const gchar *help;
268   gboolean     all_channels = (value & PICK_ALL_CHANNELS) != 0;
269 
270   switch (value & 0xF)
271     {
272     case PICK_LOW_INPUT:
273       icon_name = GIMP_ICON_COLOR_PICKER_BLACK;
274 
275       if (all_channels)
276         help = _("Pick black point for all channels");
277       else
278         help = _("Pick black point for the selected channel");
279       break;
280 
281     case PICK_GAMMA:
282       icon_name = GIMP_ICON_COLOR_PICKER_GRAY;
283 
284       if (all_channels)
285         help = _("Pick gray point for all channels");
286       else
287         help = _("Pick gray point for the selected channel");
288       break;
289 
290     case PICK_HIGH_INPUT:
291       icon_name = GIMP_ICON_COLOR_PICKER_WHITE;
292 
293       if (all_channels)
294         help = _("Pick white point for all channels");
295       else
296         help = _("Pick white point for the selected channel");
297       break;
298 
299     default:
300       return NULL;
301     }
302 
303   return gimp_filter_tool_add_color_picker (GIMP_FILTER_TOOL (tool),
304                                             GUINT_TO_POINTER (value),
305                                             icon_name,
306                                             help,
307                                             /* pick_abyss = */ FALSE,
308                                             NULL, NULL);
309 }
310 
311 static void
gimp_levels_tool_dialog(GimpFilterTool * filter_tool)312 gimp_levels_tool_dialog (GimpFilterTool *filter_tool)
313 {
314   GimpLevelsTool   *tool         = GIMP_LEVELS_TOOL (filter_tool);
315   GimpToolOptions  *tool_options = GIMP_TOOL_GET_OPTIONS (filter_tool);
316   GimpLevelsConfig *config       = GIMP_LEVELS_CONFIG (filter_tool->config);
317   GtkListStore     *store;
318   GtkWidget        *main_vbox;
319   GtkWidget        *frame_vbox;
320   GtkWidget        *vbox;
321   GtkWidget        *vbox2;
322   GtkWidget        *vbox3;
323   GtkWidget        *hbox;
324   GtkWidget        *hbox2;
325   GtkWidget        *label;
326   GtkWidget        *main_frame;
327   GtkWidget        *frame;
328   GtkWidget        *button;
329   GtkWidget        *spinbutton;
330   GtkAdjustment    *adjustment;
331   GtkWidget        *bar;
332   GtkWidget        *handle_bar;
333   gint              border;
334 
335   g_signal_connect (filter_tool->settings_box, "file-dialog-setup",
336                     G_CALLBACK (gimp_levels_tool_export_setup),
337                     filter_tool);
338 
339   main_vbox = gimp_filter_tool_dialog_get_vbox (filter_tool);
340 
341   /*  The combo box for selecting channels  */
342   main_frame = gimp_frame_new (NULL);
343   gtk_box_pack_start (GTK_BOX (main_vbox), main_frame, TRUE, TRUE, 0);
344   gtk_widget_show (main_frame);
345 
346   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
347   gtk_frame_set_label_widget (GTK_FRAME (main_frame), hbox);
348   gtk_widget_show (hbox);
349 
350   label = gtk_label_new_with_mnemonic (_("Cha_nnel:"));
351   gimp_label_set_attributes (GTK_LABEL (label),
352                              PANGO_ATTR_WEIGHT, PANGO_WEIGHT_BOLD,
353                              -1);
354   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
355   gtk_widget_show (label);
356 
357   store = gimp_enum_store_new_with_range (GIMP_TYPE_HISTOGRAM_CHANNEL,
358                                           GIMP_HISTOGRAM_VALUE,
359                                           GIMP_HISTOGRAM_ALPHA);
360   tool->channel_menu =
361     gimp_enum_combo_box_new_with_model (GIMP_ENUM_STORE (store));
362   g_object_unref (store);
363 
364   g_object_add_weak_pointer (G_OBJECT (tool->channel_menu),
365                              (gpointer) &tool->channel_menu);
366 
367   gimp_enum_combo_box_set_icon_prefix (GIMP_ENUM_COMBO_BOX (tool->channel_menu),
368                                        "gimp-channel");
369   gimp_int_combo_box_set_sensitivity (GIMP_INT_COMBO_BOX (tool->channel_menu),
370                                       levels_menu_sensitivity, filter_tool, NULL);
371   gtk_box_pack_start (GTK_BOX (hbox), tool->channel_menu, FALSE, FALSE, 0);
372   gtk_widget_show (tool->channel_menu);
373 
374   g_signal_connect (tool->channel_menu, "changed",
375                     G_CALLBACK (levels_channel_callback),
376                     tool);
377 
378   gtk_label_set_mnemonic_widget (GTK_LABEL (label), tool->channel_menu);
379 
380   button = gtk_button_new_with_mnemonic (_("R_eset Channel"));
381   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
382   gtk_widget_show (button);
383 
384   g_signal_connect (button, "clicked",
385                     G_CALLBACK (levels_channel_reset_callback),
386                     tool);
387 
388   /*  The histogram scale radio buttons  */
389   hbox2 = gimp_prop_enum_icon_box_new (G_OBJECT (tool_options),
390                                        "histogram-scale", "gimp-histogram",
391                                        0, 0);
392   gtk_box_pack_end (GTK_BOX (hbox), hbox2, FALSE, FALSE, 0);
393   gtk_widget_show (hbox2);
394 
395   /*  The linear/perceptual radio buttons  */
396   hbox2 = gimp_prop_boolean_icon_box_new (G_OBJECT (config),
397                                           "linear",
398                                           GIMP_ICON_COLOR_SPACE_LINEAR,
399                                           GIMP_ICON_COLOR_SPACE_PERCEPTUAL,
400                                           _("Adjust levels in linear light"),
401                                           _("Adjust levels perceptually"));
402   gtk_box_pack_end (GTK_BOX (hbox), hbox2, FALSE, FALSE, 0);
403   gtk_widget_show (hbox2);
404 
405   frame_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 4);
406   gtk_container_add (GTK_CONTAINER (main_frame), frame_vbox);
407   gtk_widget_show (frame_vbox);
408 
409   /*  Input levels frame  */
410   frame = gimp_frame_new (_("Input Levels"));
411   gtk_box_pack_start (GTK_BOX (frame_vbox), frame, TRUE, TRUE, 0);
412   gtk_widget_show (frame);
413 
414   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
415   gtk_container_add (GTK_CONTAINER (frame), vbox);
416   gtk_widget_show (vbox);
417 
418   frame = gtk_frame_new (NULL);
419   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
420   gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
421   gtk_widget_show (frame);
422 
423   vbox2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
424   gtk_container_add (GTK_CONTAINER (frame), vbox2);
425   gtk_widget_show (vbox2);
426 
427   tool->histogram_view = gimp_histogram_view_new (FALSE);
428 
429   g_object_add_weak_pointer (G_OBJECT (tool->histogram_view),
430                              (gpointer) &tool->histogram_view);
431 
432   gtk_box_pack_start (GTK_BOX (vbox2), tool->histogram_view, TRUE, TRUE, 0);
433   gtk_widget_show (GTK_WIDGET (tool->histogram_view));
434 
435   g_object_bind_property (G_OBJECT (tool_options),         "histogram-scale",
436                           G_OBJECT (tool->histogram_view), "histogram-scale",
437                           G_BINDING_SYNC_CREATE |
438                           G_BINDING_BIDIRECTIONAL);
439 
440   g_object_get (tool->histogram_view, "border-width", &border, NULL);
441 
442   vbox3 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
443   gtk_container_set_border_width (GTK_CONTAINER (vbox3), border);
444   gtk_box_pack_start (GTK_BOX (vbox2), vbox3, FALSE, FALSE, 0);
445   gtk_widget_show (vbox3);
446 
447   tool->input_bar = g_object_new (GIMP_TYPE_COLOR_BAR, NULL);
448   gtk_widget_set_size_request (tool->input_bar, -1, GRADIENT_HEIGHT / 2);
449   gtk_box_pack_start (GTK_BOX (vbox3), tool->input_bar, FALSE, FALSE, 0);
450   gtk_widget_show (tool->input_bar);
451 
452   bar = g_object_new (GIMP_TYPE_COLOR_BAR, NULL);
453   gtk_widget_set_size_request (bar, -1, GRADIENT_HEIGHT / 2);
454   gtk_box_pack_start (GTK_BOX (vbox3), bar, FALSE, FALSE, 0);
455   gtk_widget_show (bar);
456 
457   handle_bar = g_object_new (GIMP_TYPE_HANDLE_BAR, NULL);
458   gtk_widget_set_size_request (handle_bar, -1, CONTROL_HEIGHT);
459   gtk_box_pack_start (GTK_BOX (vbox3), handle_bar, FALSE, FALSE, 0);
460   gtk_widget_show (handle_bar);
461 
462   gimp_handle_bar_connect_events (GIMP_HANDLE_BAR (handle_bar),
463                                   tool->input_bar);
464   gimp_handle_bar_connect_events (GIMP_HANDLE_BAR (handle_bar),
465                                   bar);
466 
467   /*  Horizontal box for input levels spinbuttons  */
468   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
469   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
470   gtk_widget_show (hbox);
471 
472   /*  low input spin  */
473   hbox2 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 2);
474   gtk_box_pack_start (GTK_BOX (hbox), hbox2, FALSE, FALSE, 0);
475   gtk_widget_show (hbox2);
476 
477   button = gimp_levels_tool_color_picker_new (tool, PICK_LOW_INPUT);
478   gtk_box_pack_start (GTK_BOX (hbox2), button, FALSE, FALSE, 0);
479   gtk_widget_show (button);
480 
481   tool->low_input_spinbutton = spinbutton =
482     gimp_prop_spin_button_new (filter_tool->config, "low-input",
483                                0.01, 0.1, 1);
484   gtk_box_pack_start (GTK_BOX (hbox2), spinbutton, FALSE, FALSE, 0);
485   gtk_widget_show (spinbutton);
486 
487   tool->low_input = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (spinbutton));
488   gimp_handle_bar_set_adjustment (GIMP_HANDLE_BAR (handle_bar), 0,
489                                   tool->low_input);
490 
491   /*  clamp input toggle  */
492   hbox2 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 2);
493   gtk_box_pack_start (GTK_BOX (hbox), hbox2, TRUE, FALSE, 0);
494   gtk_widget_show (hbox2);
495 
496   button = gimp_prop_check_button_new (filter_tool->config, "clamp-input",
497                                        _("Clamp _input"));
498   gtk_box_pack_start (GTK_BOX (hbox2), button, FALSE, FALSE, 0);
499   gtk_widget_show (button);
500 
501   /*  input gamma spin  */
502   spinbutton = gimp_prop_spin_button_new (filter_tool->config, "gamma",
503                                           0.01, 0.1, 2);
504   gtk_box_pack_start (GTK_BOX (hbox2), spinbutton, FALSE, FALSE, 0);
505   gimp_help_set_help_data (spinbutton, _("Gamma"), NULL);
506   gtk_widget_show (spinbutton);
507 
508   tool->gamma = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (spinbutton));
509 
510   tool->gamma_linear = GTK_ADJUSTMENT (gtk_adjustment_new (127, 0, 255,
511                                                            0.1, 1.0, 0.0));
512   g_signal_connect (tool->gamma_linear, "value-changed",
513                     G_CALLBACK (levels_linear_gamma_changed),
514                     tool);
515 
516   gimp_handle_bar_set_adjustment (GIMP_HANDLE_BAR (handle_bar), 1,
517                                   tool->gamma_linear);
518   g_object_unref (tool->gamma_linear);
519 
520   /*  high input spin  */
521   hbox2 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 2);
522   gtk_box_pack_end (GTK_BOX (hbox), hbox2, FALSE, FALSE, 0);
523   gtk_widget_show (hbox2);
524 
525   button = gimp_levels_tool_color_picker_new (tool, PICK_HIGH_INPUT);
526   gtk_box_pack_start (GTK_BOX (hbox2), button, FALSE, FALSE, 0);
527   gtk_widget_show (button);
528 
529   spinbutton = gimp_prop_spin_button_new (filter_tool->config, "high-input",
530                                           0.01, 0.1, 1);
531   gtk_box_pack_start (GTK_BOX (hbox2), spinbutton, FALSE, FALSE, 0);
532   gtk_widget_show (spinbutton);
533   tool->high_input_spinbutton = spinbutton;
534 
535   tool->high_input = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (spinbutton));
536   gimp_handle_bar_set_adjustment (GIMP_HANDLE_BAR (handle_bar), 2,
537                                   tool->high_input);
538 
539   /*  Output levels frame  */
540   frame = gimp_frame_new (_("Output Levels"));
541   gtk_box_pack_start (GTK_BOX (frame_vbox), frame, FALSE, FALSE, 0);
542   gtk_widget_show (frame);
543 
544   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 4);
545   gtk_container_add (GTK_CONTAINER (frame), vbox);
546   gtk_widget_show (vbox);
547 
548   frame = gtk_frame_new (NULL);
549   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
550   gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
551   gtk_widget_show (frame);
552 
553   vbox2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
554   gtk_container_set_border_width (GTK_CONTAINER (vbox2), border);
555   gtk_container_add (GTK_CONTAINER (frame), vbox2);
556   gtk_widget_show (vbox2);
557 
558   tool->output_bar = g_object_new (GIMP_TYPE_COLOR_BAR, NULL);
559   gtk_widget_set_size_request (tool->output_bar, -1, GRADIENT_HEIGHT);
560   gtk_box_pack_start (GTK_BOX (vbox2), tool->output_bar, FALSE, FALSE, 0);
561   gtk_widget_show (tool->output_bar);
562 
563   handle_bar = g_object_new (GIMP_TYPE_HANDLE_BAR, NULL);
564   gtk_widget_set_size_request (handle_bar, -1, CONTROL_HEIGHT);
565   gtk_box_pack_start (GTK_BOX (vbox2), handle_bar, FALSE, FALSE, 0);
566   gtk_widget_show (handle_bar);
567 
568   gimp_handle_bar_connect_events (GIMP_HANDLE_BAR (handle_bar),
569                                   tool->output_bar);
570 
571   /*  Horizontal box for levels spin widgets  */
572   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
573   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
574   gtk_widget_show (hbox);
575 
576   /*  low output spin  */
577   tool->low_output_spinbutton = spinbutton =
578     gimp_prop_spin_button_new (filter_tool->config, "low-output",
579                                0.01, 0.1, 1);
580   gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
581   gtk_widget_show (spinbutton);
582 
583   adjustment = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (spinbutton));
584   gimp_handle_bar_set_adjustment (GIMP_HANDLE_BAR (handle_bar), 0, adjustment);
585 
586   /*  clamp output toggle  */
587   button = gimp_prop_check_button_new (filter_tool->config, "clamp-output",
588                                        _("Clamp outpu_t"));
589   gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, FALSE, 0);
590   gtk_widget_show (button);
591 
592   /*  high output spin  */
593   tool->high_output_spinbutton = spinbutton =
594     gimp_prop_spin_button_new (filter_tool->config, "high-output",
595                                0.01, 0.1, 1);
596   gtk_box_pack_end (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
597   gtk_widget_show (spinbutton);
598 
599   adjustment = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (spinbutton));
600   gimp_handle_bar_set_adjustment (GIMP_HANDLE_BAR (handle_bar), 2, adjustment);
601 
602   /*  all channels frame  */
603   main_frame = gimp_frame_new (_("All Channels"));
604   gtk_box_pack_start (GTK_BOX (main_vbox), main_frame, FALSE, FALSE, 0);
605   gtk_widget_show (main_frame);
606 
607   frame_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 4);
608   gtk_container_add (GTK_CONTAINER (main_frame), frame_vbox);
609   gtk_widget_show (frame_vbox);
610 
611   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
612   gtk_box_pack_start (GTK_BOX (frame_vbox), hbox, FALSE, FALSE, 0);
613   gtk_widget_show (hbox);
614 
615   button = gtk_button_new_with_mnemonic (_("_Auto Input Levels"));
616   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
617   gimp_help_set_help_data (button,
618                            _("Adjust levels for all channels automatically"),
619                            NULL);
620   gtk_widget_show (button);
621 
622   g_signal_connect (button, "clicked",
623                     G_CALLBACK (levels_stretch_callback),
624                     tool);
625 
626   button = gimp_levels_tool_color_picker_new (tool,
627                                               PICK_HIGH_INPUT |
628                                               PICK_ALL_CHANNELS);
629   gtk_box_pack_end (GTK_BOX (hbox), button, FALSE, FALSE, 0);
630   gtk_widget_show (button);
631 
632   button = gimp_levels_tool_color_picker_new (tool,
633                                               PICK_GAMMA |
634                                               PICK_ALL_CHANNELS);
635   gtk_box_pack_end (GTK_BOX (hbox), button, FALSE, FALSE, 0);
636   gtk_widget_show (button);
637 
638   button = gimp_levels_tool_color_picker_new (tool,
639                                               PICK_LOW_INPUT |
640                                               PICK_ALL_CHANNELS);
641   gtk_box_pack_end (GTK_BOX (hbox), button, FALSE, FALSE, 0);
642   gtk_widget_show (button);
643 
644   button = gimp_icon_button_new (GIMP_ICON_TOOL_CURVES,
645                                  _("Edit these Settings as Curves"));
646   gtk_box_pack_start (GTK_BOX (main_vbox), button, FALSE, FALSE, 0);
647   gtk_widget_show (button);
648 
649   g_signal_connect (button, "clicked",
650                     G_CALLBACK (levels_to_curves_callback),
651                     tool);
652 
653   gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->channel_menu),
654                                  config->channel);
655 }
656 
657 static void
gimp_levels_tool_reset(GimpFilterTool * filter_tool)658 gimp_levels_tool_reset (GimpFilterTool *filter_tool)
659 {
660   GimpHistogramChannel channel;
661 
662   g_object_get (filter_tool->config,
663                 "channel", &channel,
664                 NULL);
665 
666   GIMP_FILTER_TOOL_CLASS (parent_class)->reset (filter_tool);
667 
668   g_object_set (filter_tool->config,
669                 "channel", channel,
670                 NULL);
671 }
672 
673 static void
gimp_levels_tool_config_notify(GimpFilterTool * filter_tool,GimpConfig * config,const GParamSpec * pspec)674 gimp_levels_tool_config_notify (GimpFilterTool   *filter_tool,
675                                 GimpConfig       *config,
676                                 const GParamSpec *pspec)
677 {
678   GimpLevelsTool   *levels_tool   = GIMP_LEVELS_TOOL (filter_tool);
679   GimpLevelsConfig *levels_config = GIMP_LEVELS_CONFIG (config);
680 
681   GIMP_FILTER_TOOL_CLASS (parent_class)->config_notify (filter_tool,
682                                                         config, pspec);
683 
684   if (! levels_tool->channel_menu ||
685       ! levels_tool->histogram_view)
686     return;
687 
688   if (! strcmp (pspec->name, "linear"))
689     {
690       g_clear_object (&levels_tool->histogram);
691       g_clear_object (&levels_tool->histogram_async);
692       levels_tool->histogram = gimp_histogram_new (levels_config->linear);
693 
694       levels_tool->histogram_async = gimp_drawable_calculate_histogram_async (
695         GIMP_TOOL (filter_tool)->drawable, levels_tool->histogram, FALSE);
696       gimp_histogram_view_set_histogram (GIMP_HISTOGRAM_VIEW (levels_tool->histogram_view),
697                                          levels_tool->histogram);
698     }
699   else if (! strcmp (pspec->name, "channel"))
700     {
701       gimp_histogram_view_set_channel (GIMP_HISTOGRAM_VIEW (levels_tool->histogram_view),
702                                        levels_config->channel);
703       gimp_color_bar_set_channel (GIMP_COLOR_BAR (levels_tool->output_bar),
704                                   levels_config->channel);
705       gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (levels_tool->channel_menu),
706                                      levels_config->channel);
707     }
708   else if (! strcmp (pspec->name, "gamma")     ||
709            ! strcmp (pspec->name, "low-input") ||
710            ! strcmp (pspec->name, "high-input"))
711     {
712       gdouble low  = gtk_adjustment_get_value (levels_tool->low_input);
713       gdouble high = gtk_adjustment_get_value (levels_tool->high_input);
714       gdouble delta, mid, tmp, value;
715 
716       gtk_adjustment_set_lower (levels_tool->high_input,   low);
717       gtk_adjustment_set_lower (levels_tool->gamma_linear, low);
718 
719       gtk_adjustment_set_upper (levels_tool->low_input,    high);
720       gtk_adjustment_set_upper (levels_tool->gamma_linear, high);
721 
722       levels_update_input_bar (levels_tool);
723 
724       delta = (high - low) / 2.0;
725       mid   = low + delta;
726       tmp   = log10 (1.0 / levels_config->gamma[levels_config->channel]);
727       value = mid + delta * tmp;
728 
729       gtk_adjustment_set_value (levels_tool->gamma_linear, value);
730     }
731 }
732 
733 static gboolean
gimp_levels_tool_settings_import(GimpFilterTool * filter_tool,GInputStream * input,GError ** error)734 gimp_levels_tool_settings_import (GimpFilterTool  *filter_tool,
735                                   GInputStream    *input,
736                                   GError         **error)
737 {
738   GimpLevelsConfig *config = GIMP_LEVELS_CONFIG (filter_tool->config);
739   gchar             header[64];
740   gsize             bytes_read;
741 
742   if (! g_input_stream_read_all (input, header, sizeof (header),
743                                  &bytes_read, NULL, error) ||
744       bytes_read != sizeof (header))
745     {
746       g_prefix_error (error, _("Could not read header: "));
747       return FALSE;
748     }
749 
750   g_seekable_seek (G_SEEKABLE (input), 0, G_SEEK_SET, NULL, NULL);
751 
752   if (g_str_has_prefix (header, "# GIMP Levels File\n"))
753     return gimp_levels_config_load_cruft (config, input, error);
754 
755   return GIMP_FILTER_TOOL_CLASS (parent_class)->settings_import (filter_tool,
756                                                                  input,
757                                                                  error);
758 }
759 
760 static gboolean
gimp_levels_tool_settings_export(GimpFilterTool * filter_tool,GOutputStream * output,GError ** error)761 gimp_levels_tool_settings_export (GimpFilterTool  *filter_tool,
762                                   GOutputStream   *output,
763                                   GError         **error)
764 {
765   GimpLevelsTool   *tool   = GIMP_LEVELS_TOOL (filter_tool);
766   GimpLevelsConfig *config = GIMP_LEVELS_CONFIG (filter_tool->config);
767 
768   if (tool->export_old_format)
769     return gimp_levels_config_save_cruft (config, output, error);
770 
771   return GIMP_FILTER_TOOL_CLASS (parent_class)->settings_export (filter_tool,
772                                                                  output,
773                                                                  error);
774 }
775 
776 static void
levels_input_adjust_by_color(GimpLevelsConfig * config,guint value,GimpHistogramChannel channel,const GimpRGB * color)777 levels_input_adjust_by_color (GimpLevelsConfig     *config,
778                               guint                 value,
779                               GimpHistogramChannel  channel,
780                               const GimpRGB        *color)
781 {
782   switch (value & 0xF)
783     {
784     case PICK_LOW_INPUT:
785       gimp_levels_config_adjust_by_colors (config, channel, color, NULL, NULL);
786       break;
787     case PICK_GAMMA:
788       gimp_levels_config_adjust_by_colors (config, channel, NULL, color, NULL);
789       break;
790     case PICK_HIGH_INPUT:
791       gimp_levels_config_adjust_by_colors (config, channel, NULL, NULL, color);
792       break;
793     default:
794       break;
795     }
796 }
797 
798 static void
gimp_levels_tool_color_picked(GimpFilterTool * color_tool,gpointer identifier,gdouble x,gdouble y,const Babl * sample_format,const GimpRGB * color)799 gimp_levels_tool_color_picked (GimpFilterTool *color_tool,
800                                gpointer        identifier,
801                                gdouble         x,
802                                gdouble         y,
803                                const Babl     *sample_format,
804                                const GimpRGB  *color)
805 {
806   GimpFilterTool   *filter_tool = GIMP_FILTER_TOOL (color_tool);
807   GimpLevelsConfig *config      = GIMP_LEVELS_CONFIG (filter_tool->config);
808   GimpRGB           rgb         = *color;
809   guint             value       = GPOINTER_TO_UINT (identifier);
810 
811   if (config->linear)
812     babl_process (babl_fish (babl_format ("R'G'B'A double"),
813                              babl_format ("RGBA double")),
814                   &rgb, &rgb, 1);
815 
816   if (value & PICK_ALL_CHANNELS &&
817       gimp_babl_format_get_base_type (sample_format) == GIMP_RGB)
818     {
819       GimpHistogramChannel  channel;
820 
821       /*  first reset the value channel  */
822       switch (value & 0xF)
823         {
824         case PICK_LOW_INPUT:
825           config->low_input[GIMP_HISTOGRAM_VALUE] = 0.0;
826           break;
827         case PICK_GAMMA:
828           config->gamma[GIMP_HISTOGRAM_VALUE] = 1.0;
829           break;
830         case PICK_HIGH_INPUT:
831           config->high_input[GIMP_HISTOGRAM_VALUE] = 1.0;
832           break;
833         default:
834           break;
835         }
836 
837       /*  then adjust all color channels  */
838       for (channel = GIMP_HISTOGRAM_RED;
839            channel <= GIMP_HISTOGRAM_BLUE;
840            channel++)
841         {
842           levels_input_adjust_by_color (config, value, channel, &rgb);
843         }
844     }
845   else
846     {
847       levels_input_adjust_by_color (config, value, config->channel, &rgb);
848     }
849 }
850 
851 static void
gimp_levels_tool_export_setup(GimpSettingsBox * settings_box,GtkFileChooserDialog * dialog,gboolean export,GimpLevelsTool * tool)852 gimp_levels_tool_export_setup (GimpSettingsBox      *settings_box,
853                                GtkFileChooserDialog *dialog,
854                                gboolean              export,
855                                GimpLevelsTool       *tool)
856 {
857   GtkWidget *button;
858 
859   if (! export)
860     return;
861 
862   button = gtk_check_button_new_with_mnemonic (_("Use _old levels file format"));
863   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
864                                 tool->export_old_format);
865   gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (dialog), button);
866   gtk_widget_show (button);
867 
868   g_signal_connect (button, "toggled",
869                     G_CALLBACK (gimp_toggle_button_update),
870                     &tool->export_old_format);
871 }
872 
873 static void
levels_update_input_bar(GimpLevelsTool * tool)874 levels_update_input_bar (GimpLevelsTool *tool)
875 {
876   GimpFilterTool   *filter_tool = GIMP_FILTER_TOOL (tool);
877   GimpLevelsConfig *config      = GIMP_LEVELS_CONFIG (filter_tool->config);
878 
879   switch (config->channel)
880     {
881       gdouble value;
882 
883     case GIMP_HISTOGRAM_VALUE:
884     case GIMP_HISTOGRAM_ALPHA:
885     case GIMP_HISTOGRAM_RGB:
886     case GIMP_HISTOGRAM_LUMINANCE:
887       {
888         guchar v[256];
889         gint   i;
890 
891         for (i = 0; i < 256; i++)
892           {
893             value = gimp_operation_levels_map_input (config,
894                                                      config->channel,
895                                                      i / 255.0);
896             v[i] = CLAMP (value, 0.0, 1.0) * 255.999;
897           }
898 
899         gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->input_bar),
900                                     v, v, v);
901       }
902       break;
903 
904     case GIMP_HISTOGRAM_RED:
905     case GIMP_HISTOGRAM_GREEN:
906     case GIMP_HISTOGRAM_BLUE:
907       {
908         guchar r[256];
909         guchar g[256];
910         guchar b[256];
911         gint   i;
912 
913         for (i = 0; i < 256; i++)
914           {
915             value = gimp_operation_levels_map_input (config,
916                                                      GIMP_HISTOGRAM_RED,
917                                                      i / 255.0);
918             r[i] = CLAMP (value, 0.0, 1.0) * 255.999;
919 
920             value = gimp_operation_levels_map_input (config,
921                                                      GIMP_HISTOGRAM_GREEN,
922                                                      i / 255.0);
923             g[i] = CLAMP (value, 0.0, 1.0) * 255.999;
924 
925             value = gimp_operation_levels_map_input (config,
926                                                      GIMP_HISTOGRAM_BLUE,
927                                                      i / 255.0);
928             b[i] = CLAMP (value, 0.0, 1.0) * 255.999;
929           }
930 
931         gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->input_bar),
932                                     r, g, b);
933       }
934       break;
935     }
936 }
937 
938 static void
levels_channel_callback(GtkWidget * widget,GimpFilterTool * filter_tool)939 levels_channel_callback (GtkWidget      *widget,
940                          GimpFilterTool *filter_tool)
941 {
942   GimpLevelsConfig *config = GIMP_LEVELS_CONFIG (filter_tool->config);
943   gint              value;
944 
945   if (gimp_int_combo_box_get_active (GIMP_INT_COMBO_BOX (widget), &value) &&
946       config->channel != value)
947     {
948       g_object_set (config,
949                     "channel", value,
950                     NULL);
951     }
952 }
953 
954 static void
levels_channel_reset_callback(GtkWidget * widget,GimpFilterTool * filter_tool)955 levels_channel_reset_callback (GtkWidget      *widget,
956                                GimpFilterTool *filter_tool)
957 {
958   gimp_levels_config_reset_channel (GIMP_LEVELS_CONFIG (filter_tool->config));
959 }
960 
961 static gboolean
levels_menu_sensitivity(gint value,gpointer data)962 levels_menu_sensitivity (gint      value,
963                          gpointer  data)
964 {
965   GimpDrawable         *drawable = GIMP_TOOL (data)->drawable;
966   GimpHistogramChannel  channel  = value;
967 
968   if (!drawable)
969     return FALSE;
970 
971   switch (channel)
972     {
973     case GIMP_HISTOGRAM_VALUE:
974       return TRUE;
975 
976     case GIMP_HISTOGRAM_RED:
977     case GIMP_HISTOGRAM_GREEN:
978     case GIMP_HISTOGRAM_BLUE:
979       return gimp_drawable_is_rgb (drawable);
980 
981     case GIMP_HISTOGRAM_ALPHA:
982       return gimp_drawable_has_alpha (drawable);
983 
984     case GIMP_HISTOGRAM_RGB:
985       return FALSE;
986 
987     case GIMP_HISTOGRAM_LUMINANCE:
988       return FALSE;
989     }
990 
991   return FALSE;
992 }
993 
994 static void
levels_stretch_callback(GtkWidget * widget,GimpLevelsTool * levels_tool)995 levels_stretch_callback (GtkWidget      *widget,
996                          GimpLevelsTool *levels_tool)
997 {
998   GimpTool       *tool        = GIMP_TOOL (levels_tool);
999   GimpFilterTool *filter_tool = GIMP_FILTER_TOOL (levels_tool);
1000   GimpWaitable   *waitable;
1001 
1002   waitable = gimp_trivially_cancelable_waitable_new (
1003     GIMP_WAITABLE (levels_tool->histogram_async));
1004 
1005   gimp_wait (tool->tool_info->gimp, waitable, _("Calculating histogram..."));
1006 
1007   g_object_unref (waitable);
1008 
1009   if (gimp_async_is_synced   (levels_tool->histogram_async) &&
1010       gimp_async_is_finished (levels_tool->histogram_async))
1011     {
1012       gimp_levels_config_stretch (GIMP_LEVELS_CONFIG (filter_tool->config),
1013                                   levels_tool->histogram,
1014                                   gimp_drawable_is_rgb (tool->drawable));
1015     }
1016 }
1017 
1018 static void
levels_linear_gamma_changed(GtkAdjustment * adjustment,GimpLevelsTool * tool)1019 levels_linear_gamma_changed (GtkAdjustment  *adjustment,
1020                              GimpLevelsTool *tool)
1021 {
1022   gdouble low_input  = gtk_adjustment_get_value (tool->low_input);
1023   gdouble high_input = gtk_adjustment_get_value (tool->high_input);
1024   gdouble delta, mid, tmp, value;
1025 
1026   delta = (high_input - low_input) / 2.0;
1027 
1028   if (delta >= 0.5)
1029     {
1030       mid   = low_input + delta;
1031       tmp   = (gtk_adjustment_get_value (adjustment) - mid) / delta;
1032       value = 1.0 / pow (10, tmp);
1033 
1034       /*  round the gamma value to the nearest 1/100th  */
1035       value = floor (value * 100 + 0.5) / 100.0;
1036 
1037       gtk_adjustment_set_value (tool->gamma, value);
1038     }
1039 }
1040 
1041 static void
levels_to_curves_callback(GtkWidget * widget,GimpFilterTool * filter_tool)1042 levels_to_curves_callback (GtkWidget      *widget,
1043                            GimpFilterTool *filter_tool)
1044 {
1045   GimpLevelsConfig *config = GIMP_LEVELS_CONFIG (filter_tool->config);
1046   GimpCurvesConfig *curves;
1047 
1048   curves = gimp_levels_config_to_curves_config (config);
1049 
1050   gimp_filter_tool_edit_as (filter_tool,
1051                             "gimp-curves-tool",
1052                             GIMP_CONFIG (curves));
1053 
1054   g_object_unref (curves);
1055 }
1056