/* PIKA - Photo and Image Kooker Application * a rebranding of The GNU Image Manipulation Program (created with heckimp) * A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio * * Original copyright, applying to most contents (license remains unchanged): * Copyright (C) 1995 Spencer Kimball and Peter Mattis * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "config.h" #include #include #include "libpikabase/pikabase.h" #include "libpikawidgets/pikawidgets.h" #include "widgets-types.h" #include "core/pika.h" #include "core/pikaasync.h" #include "core/pikadrawable.h" #include "core/pikadrawable-histogram.h" #include "core/pikahistogram.h" #include "core/pikaimage.h" #include "pikadocked.h" #include "pikahelp-ids.h" #include "pikahistogrambox.h" #include "pikahistogrameditor.h" #include "pikahistogramview.h" #include "pikapropwidgets.h" #include "pikasessioninfo-aux.h" #include "pikawidgets-utils.h" #include "pika-intl.h" enum { PROP_0, PROP_TRC }; static void pika_histogram_editor_docked_iface_init (PikaDockedInterface *iface); static void pika_histogram_editor_finalize (GObject *object); static void pika_histogram_editor_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec); static void pika_histogram_editor_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec); static void pika_histogram_editor_set_aux_info (PikaDocked *docked, GList *aux_info); static GList * pika_histogram_editor_get_aux_info (PikaDocked *docked); static void pika_histogram_editor_set_image (PikaImageEditor *editor, PikaImage *image); static void pika_histogram_editor_layer_changed (PikaImage *image, PikaHistogramEditor *editor); static void pika_histogram_editor_frozen_update (PikaHistogramEditor *editor, const GParamSpec *pspec); static void pika_histogram_editor_buffer_update (PikaHistogramEditor *editor, const GParamSpec *pspec); static void pika_histogram_editor_update (PikaHistogramEditor *editor); static gboolean pika_histogram_editor_idle_update (PikaHistogramEditor *editor); static gboolean pika_histogram_menu_sensitivity (gint value, gpointer data); static void pika_histogram_editor_menu_update (PikaHistogramEditor *editor); static void pika_histogram_editor_name_update (PikaHistogramEditor *editor); static void pika_histogram_editor_info_update (PikaHistogramEditor *editor); static gboolean pika_histogram_editor_view_draw (PikaHistogramEditor *editor, cairo_t *cr); G_DEFINE_TYPE_WITH_CODE (PikaHistogramEditor, pika_histogram_editor, PIKA_TYPE_IMAGE_EDITOR, G_IMPLEMENT_INTERFACE (PIKA_TYPE_DOCKED, pika_histogram_editor_docked_iface_init)) #define parent_class pika_histogram_editor_parent_class static PikaDockedInterface *parent_docked_iface = NULL; static void pika_histogram_editor_class_init (PikaHistogramEditorClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); PikaImageEditorClass *image_editor_class = PIKA_IMAGE_EDITOR_CLASS (klass); object_class->finalize = pika_histogram_editor_finalize; object_class->set_property = pika_histogram_editor_set_property; object_class->get_property = pika_histogram_editor_get_property; image_editor_class->set_image = pika_histogram_editor_set_image; g_object_class_install_property (object_class, PROP_TRC, g_param_spec_enum ("trc", _("Linear/Perceptual"), NULL, PIKA_TYPE_TRC_TYPE, PIKA_TRC_LINEAR, PIKA_PARAM_READWRITE | G_PARAM_CONSTRUCT)); } static void pika_histogram_editor_init (PikaHistogramEditor *editor) { PikaHistogramView *view; GtkWidget *hbox; GtkWidget *label; GtkWidget *menu; GtkWidget *grid; gint i; const gchar *pika_histogram_editor_labels[] = { N_("Mean:"), N_("Std dev:"), N_("Median:"), N_("Pixels:"), N_("Count:"), N_("Percentile:") }; editor->box = pika_histogram_box_new (); pika_editor_set_show_name (PIKA_EDITOR (editor), TRUE); view = PIKA_HISTOGRAM_BOX (editor->box)->view; hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4); gtk_box_pack_start (GTK_BOX (editor), hbox, FALSE, FALSE, 0); gtk_widget_show (hbox); editor->menu = menu = pika_prop_enum_combo_box_new (G_OBJECT (view), "histogram-channel", 0, 0); pika_enum_combo_box_set_icon_prefix (PIKA_ENUM_COMBO_BOX (menu), "pika-channel"); pika_int_combo_box_set_sensitivity (PIKA_INT_COMBO_BOX (editor->menu), pika_histogram_menu_sensitivity, editor, NULL); pika_int_combo_box_set_active (PIKA_INT_COMBO_BOX (editor->menu), view->channel); gtk_box_pack_start (GTK_BOX (hbox), menu, FALSE, FALSE, 0); pika_help_set_help_data (editor->menu, _("Histogram channel"), NULL); menu = pika_prop_enum_icon_box_new (G_OBJECT (view), "histogram-scale", "pika-histogram", 0, 0); gtk_box_pack_end (GTK_BOX (hbox), menu, FALSE, FALSE, 0); menu = pika_prop_enum_icon_box_new (G_OBJECT (editor), "trc", "pika-color-space", -1, -1); gtk_box_pack_end (GTK_BOX (hbox), menu, FALSE, FALSE, 0); gtk_box_pack_start (GTK_BOX (editor), editor->box, TRUE, TRUE, 0); gtk_widget_show (GTK_WIDGET (editor->box)); g_signal_connect_swapped (view, "range-changed", G_CALLBACK (pika_histogram_editor_info_update), editor); g_signal_connect_swapped (view, "notify::histogram-channel", G_CALLBACK (pika_histogram_editor_info_update), editor); g_signal_connect_swapped (view, "draw", G_CALLBACK (pika_histogram_editor_view_draw), editor); grid = gtk_grid_new (); gtk_grid_set_column_spacing (GTK_GRID (grid), 2); gtk_box_pack_start (GTK_BOX (editor), grid, FALSE, FALSE, 0); gtk_widget_show (grid); for (i = 0; i < 6; i++) { gint x = (i / 3) * 2; gint y = (i % 3); label = gtk_label_new (gettext (pika_histogram_editor_labels[i])); pika_label_set_attributes (GTK_LABEL (label), PANGO_ATTR_WEIGHT, PANGO_WEIGHT_BOLD, PANGO_ATTR_SCALE, PANGO_SCALE_SMALL, -1); gtk_label_set_xalign (GTK_LABEL (label), 1.0); gtk_widget_set_hexpand (label, TRUE); gtk_grid_attach (GTK_GRID (grid), label, x, y, 1, 1); gtk_widget_show (label); editor->labels[i] = label = g_object_new (GTK_TYPE_LABEL, "xalign", 0.0, "yalign", 0.5, "width-chars", i > 2 ? 9 : 5, NULL); pika_label_set_attributes (GTK_LABEL (editor->labels[i]), PANGO_ATTR_SCALE, PANGO_SCALE_SMALL, -1); gtk_grid_attach (GTK_GRID (grid), label, x + 1, y, 1, 1); gtk_widget_show (label); } } static void pika_histogram_editor_docked_iface_init (PikaDockedInterface *docked_iface) { parent_docked_iface = g_type_interface_peek_parent (docked_iface); if (! parent_docked_iface) parent_docked_iface = g_type_default_interface_peek (PIKA_TYPE_DOCKED); docked_iface->set_aux_info = pika_histogram_editor_set_aux_info; docked_iface->get_aux_info = pika_histogram_editor_get_aux_info; } static void pika_histogram_editor_finalize (GObject *object) { if (PIKA_HISTOGRAM_EDITOR (object)->idle_id) g_source_remove (PIKA_HISTOGRAM_EDITOR (object)->idle_id); G_OBJECT_CLASS (parent_class)->finalize (object); } static void pika_histogram_editor_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { PikaHistogramEditor *editor = PIKA_HISTOGRAM_EDITOR (object); PikaHistogramView *view = PIKA_HISTOGRAM_BOX (editor->box)->view; switch (property_id) { case PROP_TRC: editor->trc = g_value_get_enum (value); if (editor->histogram) { g_clear_object (&editor->histogram); pika_histogram_view_set_histogram (view, NULL); } if (editor->bg_histogram) { g_clear_object (&editor->bg_histogram); pika_histogram_view_set_background (view, NULL); } pika_histogram_editor_update (editor); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; } } static void pika_histogram_editor_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) { PikaHistogramEditor *editor = PIKA_HISTOGRAM_EDITOR (object); switch (property_id) { case PROP_TRC: g_value_set_enum (value, editor->trc); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; } } static void pika_histogram_editor_set_aux_info (PikaDocked *docked, GList *aux_info) { PikaHistogramEditor *editor = PIKA_HISTOGRAM_EDITOR (docked); PikaHistogramView *view = PIKA_HISTOGRAM_BOX (editor->box)->view; parent_docked_iface->set_aux_info (docked, aux_info); pika_session_info_aux_set_props (G_OBJECT (view), aux_info, "histogram-channel", "histogram-scale", NULL); } static GList * pika_histogram_editor_get_aux_info (PikaDocked *docked) { PikaHistogramEditor *editor = PIKA_HISTOGRAM_EDITOR (docked); PikaHistogramView *view = PIKA_HISTOGRAM_BOX (editor->box)->view; GList *aux_info; aux_info = parent_docked_iface->get_aux_info (docked); return g_list_concat (aux_info, pika_session_info_aux_new_from_props (G_OBJECT (view), "histogram-channel", "histogram-scale", NULL)); } static void pika_histogram_editor_set_image (PikaImageEditor *image_editor, PikaImage *image) { PikaHistogramEditor *editor = PIKA_HISTOGRAM_EDITOR (image_editor); PikaHistogramView *view = PIKA_HISTOGRAM_BOX (editor->box)->view; if (image_editor->image) { if (editor->idle_id) { g_source_remove (editor->idle_id); editor->idle_id = 0; } editor->update_pending = FALSE; g_signal_handlers_disconnect_by_func (image_editor->image, pika_histogram_editor_update, editor); g_signal_handlers_disconnect_by_func (image_editor->image, pika_histogram_editor_layer_changed, editor); g_signal_handlers_disconnect_by_func (image_editor->image, pika_histogram_editor_menu_update, editor); if (editor->histogram) { g_clear_object (&editor->histogram); pika_histogram_view_set_histogram (view, NULL); } if (editor->bg_histogram) { g_clear_object (&editor->bg_histogram); pika_histogram_view_set_background (view, NULL); } } PIKA_IMAGE_EDITOR_CLASS (parent_class)->set_image (image_editor, image); if (image) { g_signal_connect_object (image, "mode-changed", G_CALLBACK (pika_histogram_editor_menu_update), editor, G_CONNECT_SWAPPED); g_signal_connect_object (image, "selected-layers-changed", G_CALLBACK (pika_histogram_editor_layer_changed), editor, 0); g_signal_connect_object (image, "mask-changed", G_CALLBACK (pika_histogram_editor_update), editor, G_CONNECT_SWAPPED); } pika_histogram_editor_layer_changed (image, editor); } GtkWidget * pika_histogram_editor_new (void) { return g_object_new (PIKA_TYPE_HISTOGRAM_EDITOR, NULL); } static void pika_histogram_editor_layer_changed (PikaImage *image, PikaHistogramEditor *editor) { if (editor->drawable) { PikaHistogramView *view = PIKA_HISTOGRAM_BOX (editor->box)->view; if (editor->histogram) { g_clear_object (&editor->histogram); pika_histogram_view_set_histogram (view, NULL); } if (editor->bg_histogram) { g_clear_object (&editor->bg_histogram); pika_histogram_view_set_background (view, NULL); } g_signal_handlers_disconnect_by_func (editor->drawable, pika_histogram_editor_name_update, editor); g_signal_handlers_disconnect_by_func (editor->drawable, pika_histogram_editor_menu_update, editor); g_signal_handlers_disconnect_by_func (editor->drawable, pika_histogram_editor_update, editor); g_signal_handlers_disconnect_by_func (editor->drawable, pika_histogram_editor_buffer_update, editor); g_signal_handlers_disconnect_by_func (editor->drawable, pika_histogram_editor_frozen_update, editor); editor->drawable = NULL; } if (image) { GList *layers; layers = pika_image_get_selected_layers (image); /* * TODO: right now, we only support making an histogram for a single * layer. In future, it would be nice to have the ability to make the * histogram for: * - all individual pixels of selected layers; * - the merged composition of selected layers; * - the visible image. */ editor->drawable = (g_list_length (layers) == 1 ? layers->data : NULL); } pika_histogram_editor_menu_update (editor); if (editor->drawable) { g_signal_connect_object (editor->drawable, "notify::frozen", G_CALLBACK (pika_histogram_editor_frozen_update), editor, G_CONNECT_SWAPPED); g_signal_connect_object (editor->drawable, "notify::buffer", G_CALLBACK (pika_histogram_editor_buffer_update), editor, G_CONNECT_SWAPPED); g_signal_connect_object (editor->drawable, "update", G_CALLBACK (pika_histogram_editor_update), editor, G_CONNECT_SWAPPED); g_signal_connect_object (editor->drawable, "alpha-changed", G_CALLBACK (pika_histogram_editor_menu_update), editor, G_CONNECT_SWAPPED); g_signal_connect_object (editor->drawable, "name-changed", G_CALLBACK (pika_histogram_editor_name_update), editor, G_CONNECT_SWAPPED); pika_histogram_editor_buffer_update (editor, NULL); } else if (editor->histogram) { editor->recompute = TRUE; gtk_widget_queue_draw (GTK_WIDGET (editor->box)); } pika_histogram_editor_info_update (editor); pika_histogram_editor_name_update (editor); } static void pika_histogram_editor_calculate_async_callback (PikaAsync *async, PikaHistogramEditor *editor) { editor->calculate_async = NULL; if (pika_async_is_finished (async) && editor->histogram) { if (editor->bg_pending) { PikaHistogramView *view = PIKA_HISTOGRAM_BOX (editor->box)->view; editor->bg_histogram = pika_histogram_duplicate (editor->histogram); pika_histogram_view_set_background (view, editor->bg_histogram); } pika_histogram_editor_info_update (editor); } editor->bg_pending = FALSE; if (editor->update_pending) pika_histogram_editor_update (editor); } static gboolean pika_histogram_editor_validate (PikaHistogramEditor *editor) { if (editor->recompute || ! editor->histogram) { if (editor->drawable && /* avoid calculating the histogram of a detached layer. this can * happen during pika_image_remove_layer(), as a result of a pending * "expose-event" signal (handled in * pika_histogram_editor_view_expose()) executed through * gtk_tree_view_clamp_node_visible(), as a result of the * PikaLayerTreeView in the Layers dialog receiving the image's * "selected-layers-changed" signal before us. See bug #795716, * comment 6. */ pika_item_is_attached (PIKA_ITEM (editor->drawable))) { PikaAsync *async; if (! editor->histogram) { PikaHistogramView *view = PIKA_HISTOGRAM_BOX (editor->box)->view; editor->histogram = pika_histogram_new (editor->trc); pika_histogram_clear_values ( editor->histogram, babl_format_get_n_components ( pika_drawable_get_format (editor->drawable))); pika_histogram_view_set_histogram (view, editor->histogram); } async = pika_drawable_calculate_histogram_async (editor->drawable, editor->histogram, TRUE); editor->calculate_async = async; pika_async_add_callback ( async, (PikaAsyncCallback) pika_histogram_editor_calculate_async_callback, editor); g_object_unref (async); } else if (editor->histogram) { pika_histogram_clear_values (editor->histogram, 0); pika_histogram_editor_info_update (editor); } editor->recompute = FALSE; if (editor->idle_id) { g_source_remove (editor->idle_id); editor->idle_id = 0; } } return (editor->histogram != NULL); } static void pika_histogram_editor_frozen_update (PikaHistogramEditor *editor, const GParamSpec *pspec) { PikaHistogramView *view = PIKA_HISTOGRAM_BOX (editor->box)->view; if (pika_viewable_preview_is_frozen (PIKA_VIEWABLE (editor->drawable))) { /* Only do the background histogram if the histogram is visible. * This is a workaround for the fact that recalculating the * histogram is expensive and that it is only validated when it * is shown. So don't slow down painting by doing something that * is not even seen by the user. */ if (! editor->bg_histogram && gtk_widget_is_drawable (GTK_WIDGET (editor))) { if (editor->idle_id) { g_source_remove (editor->idle_id); pika_histogram_editor_idle_update (editor); } if (pika_histogram_editor_validate (editor)) { if (editor->calculate_async) { editor->bg_pending = TRUE; } else { editor->bg_histogram = pika_histogram_duplicate ( editor->histogram); pika_histogram_view_set_background (view, editor->bg_histogram); } } } } else { if (editor->bg_histogram) { g_clear_object (&editor->bg_histogram); pika_histogram_view_set_background (view, NULL); } editor->bg_pending = FALSE; if (editor->update_pending) pika_async_cancel_and_wait (editor->calculate_async); } } static void pika_histogram_editor_buffer_update (PikaHistogramEditor *editor, const GParamSpec *pspec) { g_object_set (editor, "trc", pika_drawable_get_trc (editor->drawable), NULL); } static void pika_histogram_editor_update (PikaHistogramEditor *editor) { if (editor->bg_pending) { editor->update_pending = TRUE; return; } editor->update_pending = FALSE; if (editor->calculate_async) pika_async_cancel_and_wait (editor->calculate_async); if (editor->idle_id) g_source_remove (editor->idle_id); editor->idle_id = g_timeout_add_full (G_PRIORITY_LOW, 200, (GSourceFunc) pika_histogram_editor_idle_update, editor, NULL); } static gboolean pika_histogram_editor_idle_update (PikaHistogramEditor *editor) { editor->idle_id = 0; /* Mark the histogram for recomputation and queue a redraw. * We will then recalculate the histogram when the view is exposed. */ editor->recompute = TRUE; gtk_widget_queue_draw (GTK_WIDGET (editor->box)); return FALSE; } static gboolean pika_histogram_menu_sensitivity (gint value, gpointer data) { PikaHistogramEditor *editor = PIKA_HISTOGRAM_EDITOR (data); PikaHistogramChannel channel = value; if (editor->histogram) return pika_histogram_has_channel (editor->histogram, channel); return FALSE; } static void pika_histogram_editor_menu_update (PikaHistogramEditor *editor) { PikaHistogramView *view = PIKA_HISTOGRAM_BOX (editor->box)->view; gtk_widget_queue_draw (editor->menu); if (editor->histogram && ! pika_histogram_has_channel (editor->histogram, view->channel)) { pika_histogram_view_set_channel (view, PIKA_HISTOGRAM_VALUE); } } static void pika_histogram_editor_name_update (PikaHistogramEditor *editor) { const gchar *name = NULL; if (editor->drawable) name = pika_object_get_name (editor->drawable); pika_editor_set_name (PIKA_EDITOR (editor), name); } static void pika_histogram_editor_info_update (PikaHistogramEditor *editor) { PikaHistogramView *view = PIKA_HISTOGRAM_BOX (editor->box)->view; PikaHistogram *hist = editor->histogram; if (hist) { gint n_bins; gdouble pixels; gdouble count; gchar text[12]; n_bins = pika_histogram_n_bins (hist); pixels = pika_histogram_get_count (hist, view->channel, 0, n_bins - 1); count = pika_histogram_get_count (hist, view->channel, view->start, view->end); /* For the RGB histogram, we need to divide by three * since it combines three histograms in one */ if (view->channel == PIKA_HISTOGRAM_RGB) { pixels /= 3; count /= 3; } g_snprintf (text, sizeof (text), "%.3f", pika_histogram_get_mean (hist, view->channel, view->start, view->end)); gtk_label_set_text (GTK_LABEL (editor->labels[0]), text); g_snprintf (text, sizeof (text), "%.3f", pika_histogram_get_std_dev (hist, view->channel, view->start, view->end)); gtk_label_set_text (GTK_LABEL (editor->labels[1]), text); g_snprintf (text, sizeof (text), "%.3f", pika_histogram_get_median (hist, view->channel, view->start, view->end)); gtk_label_set_text (GTK_LABEL (editor->labels[2]), text); g_snprintf (text, sizeof (text), "%d", (gint) pixels); gtk_label_set_text (GTK_LABEL (editor->labels[3]), text); g_snprintf (text, sizeof (text), "%d", (gint) count); gtk_label_set_text (GTK_LABEL (editor->labels[4]), text); g_snprintf (text, sizeof (text), "%.1f", (pixels > 0 ? (100.0 * count / pixels) : 0.0)); gtk_label_set_text (GTK_LABEL (editor->labels[5]), text); } else { gint i; for (i = 0; i < 6; i++) gtk_label_set_text (GTK_LABEL (editor->labels[i]), NULL); } } static gboolean pika_histogram_editor_view_draw (PikaHistogramEditor *editor, cairo_t *cr) { pika_histogram_editor_validate (editor); return FALSE; }