~ubuntu-branches/ubuntu/maverick/gimp/maverick-updates

« back to all changes in this revision

Viewing changes to app/widgets/gimphistogrameditor.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-12-09 19:44:52 UTC
  • Revision ID: james.westby@ubuntu.com-20051209194452-yggpemjlofpjqyf4
Tags: upstream-2.2.9
ImportĀ upstreamĀ versionĀ 2.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an 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 2 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, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 */
 
18
 
 
19
#include "config.h"
 
20
 
 
21
#include <gtk/gtk.h>
 
22
 
 
23
#include "libgimpwidgets/gimpwidgets.h"
 
24
 
 
25
#include "widgets-types.h"
 
26
 
 
27
#include "base/gimphistogram.h"
 
28
#include "base/pixel-region.h"
 
29
 
 
30
#include "config/gimpbaseconfig.h"
 
31
 
 
32
#include "core/gimp.h"
 
33
#include "core/gimpdrawable.h"
 
34
#include "core/gimpdrawable-histogram.h"
 
35
#include "core/gimpimage.h"
 
36
 
 
37
#include "gimpdocked.h"
 
38
#include "gimpenumcombobox.h"
 
39
#include "gimpenumstore.h"
 
40
#include "gimphelp-ids.h"
 
41
#include "gimphistogrambox.h"
 
42
#include "gimphistogrameditor.h"
 
43
#include "gimphistogramview.h"
 
44
#include "gimppropwidgets.h"
 
45
#include "gimpsessioninfo.h"
 
46
#include "gimpwidgets-utils.h"
 
47
 
 
48
#include "gimp-intl.h"
 
49
 
 
50
 
 
51
static void  gimp_histogram_editor_class_init     (GimpHistogramEditorClass *klass);
 
52
static void  gimp_histogram_editor_init           (GimpHistogramEditor      *editor);
 
53
static void    gimp_histogram_editor_docked_iface_init (GimpDockedInterface *docked_iface);
 
54
static void    gimp_histogram_editor_set_aux_info (GimpDocked          *docked,
 
55
                                                   GList               *aux_info);
 
56
static GList * gimp_histogram_editor_get_aux_info (GimpDocked          *docked);
 
57
 
 
58
static void  gimp_histogram_editor_set_image      (GimpImageEditor     *editor,
 
59
                                                   GimpImage           *gimage);
 
60
static void  gimp_histogram_editor_layer_changed  (GimpImage           *gimage,
 
61
                                                   GimpHistogramEditor *editor);
 
62
static void  gimp_histogram_editor_update         (GimpHistogramEditor *editor);
 
63
 
 
64
static gboolean gimp_histogram_editor_idle_update (GimpHistogramEditor *editor);
 
65
static gboolean gimp_histogram_editor_item_visible (GtkTreeModel       *model,
 
66
                                                    GtkTreeIter        *iter,
 
67
                                                    gpointer            data);
 
68
static void  gimp_histogram_editor_menu_update    (GimpHistogramEditor *editor);
 
69
static void  gimp_histogram_editor_info_update    (GimpHistogramEditor *editor);
 
70
 
 
71
 
 
72
static GimpImageEditorClass *parent_class        = NULL;
 
73
static GimpDockedInterface  *parent_docked_iface = NULL;
 
74
 
 
75
 
 
76
GType
 
77
gimp_histogram_editor_get_type (void)
 
78
{
 
79
  static GType editor_type = 0;
 
80
 
 
81
  if (! editor_type)
 
82
    {
 
83
      static const GTypeInfo editor_info =
 
84
      {
 
85
        sizeof (GimpHistogramEditorClass),
 
86
        (GBaseInitFunc) NULL,
 
87
        (GBaseFinalizeFunc) NULL,
 
88
        (GClassInitFunc) gimp_histogram_editor_class_init,
 
89
        NULL,           /* class_finalize */
 
90
        NULL,           /* class_data     */
 
91
        sizeof (GimpHistogramEditor),
 
92
        0,              /* n_preallocs    */
 
93
        (GInstanceInitFunc) gimp_histogram_editor_init,
 
94
      };
 
95
      static const GInterfaceInfo docked_iface_info =
 
96
      {
 
97
        (GInterfaceInitFunc) gimp_histogram_editor_docked_iface_init,
 
98
        NULL,           /* iface_finalize */
 
99
        NULL            /* iface_data     */
 
100
      };
 
101
 
 
102
      editor_type = g_type_register_static (GIMP_TYPE_IMAGE_EDITOR,
 
103
                                            "GimpHistogramEditor",
 
104
                                            &editor_info, 0);
 
105
      g_type_add_interface_static (editor_type, GIMP_TYPE_DOCKED,
 
106
                                   &docked_iface_info);
 
107
    }
 
108
 
 
109
  return editor_type;
 
110
}
 
111
 
 
112
static void
 
113
gimp_histogram_editor_class_init (GimpHistogramEditorClass* klass)
 
114
{
 
115
  GimpImageEditorClass *image_editor_class = GIMP_IMAGE_EDITOR_CLASS (klass);
 
116
 
 
117
  parent_class = g_type_class_peek_parent (klass);
 
118
 
 
119
  image_editor_class->set_image = gimp_histogram_editor_set_image;
 
120
}
 
121
 
 
122
static void
 
123
gimp_histogram_editor_init (GimpHistogramEditor *editor)
 
124
{
 
125
  GimpHistogramView *view;
 
126
  GtkWidget         *hbox;
 
127
  GtkWidget         *label;
 
128
  GtkWidget         *menu;
 
129
  GtkWidget         *table;
 
130
  gint               i;
 
131
 
 
132
  const gchar *gimp_histogram_editor_labels[] =
 
133
    {
 
134
      N_("Mean:"),
 
135
      N_("Std Dev:"),
 
136
      N_("Median:"),
 
137
      N_("Pixels:"),
 
138
      N_("Count:"),
 
139
      N_("Percentile:")
 
140
    };
 
141
 
 
142
  editor->drawable  = NULL;
 
143
  editor->histogram = NULL;
 
144
  editor->idle_id   = 0;
 
145
  editor->box       = gimp_histogram_box_new ();
 
146
 
 
147
  editor->name = label = gtk_label_new (_("(None)"));
 
148
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
 
149
  gimp_label_set_attributes (GTK_LABEL (editor->name),
 
150
                             PANGO_ATTR_STYLE, PANGO_STYLE_ITALIC,
 
151
                             -1);
 
152
  gtk_box_pack_start (GTK_BOX (editor), label, FALSE, FALSE, 0);
 
153
  gtk_widget_show (label);
 
154
 
 
155
  view = GIMP_HISTOGRAM_BOX (editor->box)->view;
 
156
 
 
157
  hbox = gtk_hbox_new (FALSE, 6);
 
158
  gtk_box_pack_start (GTK_BOX (editor), hbox, FALSE, FALSE, 0);
 
159
  gtk_widget_show (hbox);
 
160
 
 
161
  label = gtk_label_new (_("Channel:"));
 
162
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
 
163
  gtk_widget_show (label);
 
164
 
 
165
  editor->menu = menu = gimp_prop_enum_combo_box_new (G_OBJECT (view),
 
166
                                                      "histogram-channel",
 
167
                                                      0, 0);
 
168
  gimp_enum_combo_box_set_stock_prefix (GIMP_ENUM_COMBO_BOX (menu),
 
169
                                        "gimp-channel");
 
170
  gimp_enum_combo_box_set_visible (GIMP_ENUM_COMBO_BOX (editor->menu),
 
171
                                   gimp_histogram_editor_item_visible,
 
172
                                   editor);
 
173
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (editor->menu),
 
174
                                 view->channel);
 
175
  gtk_box_pack_start (GTK_BOX (hbox), menu, FALSE, FALSE, 0);
 
176
  gtk_widget_show (menu);
 
177
 
 
178
  menu = gimp_prop_enum_stock_box_new (G_OBJECT (view),
 
179
                                       "histogram-scale", "gimp-histogram",
 
180
                                       0, 0);
 
181
  gtk_box_pack_end (GTK_BOX (hbox), menu, FALSE, FALSE, 0);
 
182
  gtk_widget_show (menu);
 
183
 
 
184
  gtk_box_pack_start (GTK_BOX (editor), editor->box, TRUE, TRUE, 0);
 
185
  gtk_widget_show (GTK_WIDGET (editor->box));
 
186
 
 
187
  g_signal_connect_swapped (view, "range_changed",
 
188
                            G_CALLBACK (gimp_histogram_editor_info_update),
 
189
                            editor);
 
190
  g_signal_connect_swapped (view, "notify::histogram-channel",
 
191
                            G_CALLBACK (gimp_histogram_editor_info_update),
 
192
                            editor);
 
193
 
 
194
  table = gtk_table_new (3, 4, FALSE);
 
195
  gtk_table_set_col_spacings (GTK_TABLE (table), 6);
 
196
  gtk_box_pack_start (GTK_BOX (editor), table, FALSE, FALSE, 0);
 
197
  gtk_widget_show (table);
 
198
 
 
199
  for (i = 0; i < 6; i++)
 
200
    {
 
201
      gint x = (i / 3) * 2;
 
202
      gint y = (i % 3);
 
203
 
 
204
      label = gtk_label_new (gettext (gimp_histogram_editor_labels[i]));
 
205
      gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
 
206
      gtk_table_attach (GTK_TABLE (table), label, x, x + 1, y, y + 1,
 
207
                        GTK_FILL, GTK_FILL, 2, 2);
 
208
      gtk_widget_show (label);
 
209
 
 
210
      editor->labels[i] = label = gtk_label_new (NULL);
 
211
      gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
 
212
      gtk_table_attach (GTK_TABLE (table), label, x + 1, x + 2, y, y + 1,
 
213
                        GTK_FILL, GTK_FILL, 2, 2);
 
214
      gtk_widget_show (label);
 
215
    }
 
216
}
 
217
 
 
218
static void
 
219
gimp_histogram_editor_docked_iface_init (GimpDockedInterface *docked_iface)
 
220
{
 
221
  parent_docked_iface = g_type_interface_peek_parent (docked_iface);
 
222
 
 
223
  docked_iface->set_aux_info = gimp_histogram_editor_set_aux_info;
 
224
  docked_iface->get_aux_info = gimp_histogram_editor_get_aux_info;
 
225
}
 
226
 
 
227
static void
 
228
gimp_histogram_editor_set_aux_info (GimpDocked *docked,
 
229
                                    GList      *aux_info)
 
230
{
 
231
  GimpHistogramEditor *editor = GIMP_HISTOGRAM_EDITOR (docked);
 
232
  GimpHistogramView   *view   = GIMP_HISTOGRAM_BOX (editor->box)->view;
 
233
 
 
234
  if (parent_docked_iface->set_aux_info)
 
235
    parent_docked_iface->set_aux_info (docked, aux_info);
 
236
 
 
237
  gimp_session_info_aux_set_props (G_OBJECT (view), aux_info,
 
238
                                   "histogram-channel",
 
239
                                   "histogram-scale",
 
240
                                   NULL);
 
241
}
 
242
 
 
243
static GList *
 
244
gimp_histogram_editor_get_aux_info (GimpDocked *docked)
 
245
{
 
246
  GimpHistogramEditor *editor = GIMP_HISTOGRAM_EDITOR (docked);
 
247
  GimpHistogramView   *view   = GIMP_HISTOGRAM_BOX (editor->box)->view;
 
248
  GList               *aux_info;
 
249
 
 
250
  aux_info = gimp_session_info_aux_new_from_props (G_OBJECT (view),
 
251
                                                   "histogram-channel",
 
252
                                                   "histogram-scale",
 
253
                                                   NULL);
 
254
 
 
255
  if (parent_docked_iface->get_aux_info)
 
256
    return g_list_concat (parent_docked_iface->get_aux_info (docked),
 
257
                          aux_info);
 
258
  else
 
259
    return aux_info;
 
260
}
 
261
 
 
262
static void
 
263
gimp_histogram_editor_set_image (GimpImageEditor *image_editor,
 
264
                                 GimpImage       *gimage)
 
265
{
 
266
  GimpHistogramEditor *editor = GIMP_HISTOGRAM_EDITOR (image_editor);
 
267
  GimpHistogramView   *view   = GIMP_HISTOGRAM_BOX (editor->box)->view;
 
268
 
 
269
  if (image_editor->gimage)
 
270
    {
 
271
      if (editor->idle_id)
 
272
        {
 
273
          g_source_remove (editor->idle_id);
 
274
          editor->idle_id = 0;
 
275
        }
 
276
 
 
277
      g_signal_handlers_disconnect_by_func (image_editor->gimage,
 
278
                                            gimp_histogram_editor_layer_changed,
 
279
                                            editor);
 
280
      g_signal_handlers_disconnect_by_func (image_editor->gimage,
 
281
                                            gimp_histogram_editor_menu_update,
 
282
                                            editor);
 
283
 
 
284
      if (editor->histogram)
 
285
        {
 
286
          gimp_histogram_free (editor->histogram);
 
287
          editor->histogram = NULL;
 
288
        }
 
289
 
 
290
      gimp_histogram_view_set_histogram (view, NULL);
 
291
    }
 
292
 
 
293
  GIMP_IMAGE_EDITOR_CLASS (parent_class)->set_image (image_editor, gimage);
 
294
 
 
295
  if (gimage)
 
296
    {
 
297
      editor->histogram =
 
298
        gimp_histogram_new (GIMP_BASE_CONFIG (gimage->gimp->config));
 
299
 
 
300
      gimp_histogram_view_set_histogram (view, editor->histogram);
 
301
 
 
302
      g_signal_connect_object (gimage, "mode_changed",
 
303
                               G_CALLBACK (gimp_histogram_editor_menu_update),
 
304
                               editor, G_CONNECT_SWAPPED);
 
305
      g_signal_connect_object (gimage, "active_layer_changed",
 
306
                               G_CALLBACK (gimp_histogram_editor_layer_changed),
 
307
                               editor, 0);
 
308
      g_signal_connect_object (gimage, "mask_changed",
 
309
                               G_CALLBACK (gimp_histogram_editor_update),
 
310
                               editor, G_CONNECT_SWAPPED);
 
311
    }
 
312
 
 
313
  gimp_histogram_editor_layer_changed (gimage, editor);
 
314
}
 
315
 
 
316
GtkWidget *
 
317
gimp_histogram_editor_new (void)
 
318
{
 
319
  return g_object_new (GIMP_TYPE_HISTOGRAM_EDITOR, NULL);
 
320
}
 
321
 
 
322
static void
 
323
gimp_histogram_editor_layer_changed (GimpImage           *gimage,
 
324
                                     GimpHistogramEditor *editor)
 
325
{
 
326
  const gchar *name;
 
327
 
 
328
  if (editor->drawable)
 
329
    {
 
330
      g_signal_handlers_disconnect_by_func (editor->drawable,
 
331
                                            gimp_histogram_editor_menu_update,
 
332
                                            editor);
 
333
      g_signal_handlers_disconnect_by_func (editor->drawable,
 
334
                                            gimp_histogram_editor_update,
 
335
                                            editor);
 
336
      editor->drawable = NULL;
 
337
    }
 
338
 
 
339
  if (gimage)
 
340
    editor->drawable = (GimpDrawable *) gimp_image_get_active_layer (gimage);
 
341
 
 
342
  gimp_histogram_editor_menu_update (editor);
 
343
 
 
344
  if (editor->drawable)
 
345
    {
 
346
      name = gimp_object_get_name (GIMP_OBJECT (editor->drawable));
 
347
 
 
348
      g_signal_connect_object (editor->drawable, "invalidate_preview",
 
349
                               G_CALLBACK (gimp_histogram_editor_update),
 
350
                               editor, G_CONNECT_SWAPPED);
 
351
      g_signal_connect_object (editor->drawable, "alpha_changed",
 
352
                               G_CALLBACK (gimp_histogram_editor_menu_update),
 
353
                               editor, G_CONNECT_SWAPPED);
 
354
 
 
355
      gimp_histogram_editor_update (editor);
 
356
    }
 
357
  else
 
358
    {
 
359
      name = _("(None)");
 
360
 
 
361
      if (editor->histogram)
 
362
        {
 
363
          gimp_histogram_calculate (editor->histogram, NULL, NULL);
 
364
          gtk_widget_queue_draw GTK_WIDGET (editor->box);
 
365
          gimp_histogram_editor_info_update (editor);
 
366
        }
 
367
    }
 
368
 
 
369
  gtk_label_set_text (GTK_LABEL (editor->name), name);
 
370
}
 
371
 
 
372
static void
 
373
gimp_histogram_editor_update (GimpHistogramEditor *editor)
 
374
{
 
375
  if (editor->idle_id)
 
376
    g_source_remove (editor->idle_id);
 
377
 
 
378
  editor->idle_id = g_idle_add_full (G_PRIORITY_LOW,
 
379
                                     (GSourceFunc) gimp_histogram_editor_idle_update,
 
380
                                     editor,
 
381
                                     (GDestroyNotify) NULL);
 
382
}
 
383
 
 
384
static gboolean
 
385
gimp_histogram_editor_idle_update (GimpHistogramEditor *editor)
 
386
{
 
387
  editor->idle_id = 0;
 
388
 
 
389
  if (editor->drawable && editor->histogram)
 
390
    {
 
391
      gimp_drawable_calculate_histogram (editor->drawable, editor->histogram);
 
392
      gtk_widget_queue_draw GTK_WIDGET (editor->box);
 
393
      gimp_histogram_editor_info_update (editor);
 
394
    }
 
395
 
 
396
  return FALSE;
 
397
}
 
398
 
 
399
static gboolean
 
400
gimp_histogram_editor_channel_valid (GimpHistogramEditor  *editor,
 
401
                                     GimpHistogramChannel  channel)
 
402
{
 
403
  if (editor->drawable)
 
404
    {
 
405
      switch (channel)
 
406
        {
 
407
        case GIMP_HISTOGRAM_VALUE:
 
408
          return TRUE;
 
409
 
 
410
        case GIMP_HISTOGRAM_RED:
 
411
        case GIMP_HISTOGRAM_GREEN:
 
412
        case GIMP_HISTOGRAM_BLUE:
 
413
        case GIMP_HISTOGRAM_RGB:
 
414
          return gimp_drawable_is_rgb (editor->drawable);
 
415
 
 
416
        case GIMP_HISTOGRAM_ALPHA:
 
417
          return gimp_drawable_has_alpha (editor->drawable);
 
418
        }
 
419
    }
 
420
 
 
421
  return TRUE;
 
422
}
 
423
 
 
424
static gboolean
 
425
gimp_histogram_editor_item_visible (GtkTreeModel *model,
 
426
                                    GtkTreeIter  *iter,
 
427
                                    gpointer      data)
 
428
{
 
429
  GimpHistogramEditor  *editor = GIMP_HISTOGRAM_EDITOR (data);
 
430
  GimpHistogramChannel  channel;
 
431
 
 
432
  gtk_tree_model_get (model, iter,
 
433
                      GIMP_INT_STORE_VALUE, &channel,
 
434
                      -1);
 
435
 
 
436
  return gimp_histogram_editor_channel_valid (editor, channel);
 
437
}
 
438
 
 
439
static void
 
440
gimp_histogram_editor_menu_update (GimpHistogramEditor *editor)
 
441
{
 
442
  GimpHistogramView *view = GIMP_HISTOGRAM_BOX (editor->box)->view;
 
443
  GtkTreeModel      *model;
 
444
 
 
445
  model = gtk_combo_box_get_model (GTK_COMBO_BOX (editor->menu));
 
446
  gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (model));
 
447
 
 
448
  if (! gimp_histogram_editor_channel_valid (editor, view->channel))
 
449
    {
 
450
      gimp_histogram_view_set_channel (view, GIMP_HISTOGRAM_VALUE);
 
451
    }
 
452
}
 
453
 
 
454
static void
 
455
gimp_histogram_editor_info_update (GimpHistogramEditor *editor)
 
456
{
 
457
  GimpHistogramView *view = GIMP_HISTOGRAM_BOX (editor->box)->view;
 
458
  GimpHistogram     *hist = editor->histogram;
 
459
 
 
460
  if (hist)
 
461
    {
 
462
      gdouble pixels;
 
463
      gdouble count;
 
464
      gchar   text[12];
 
465
 
 
466
      pixels = gimp_histogram_get_count (hist, view->channel, 0, 255);
 
467
      count  = gimp_histogram_get_count (hist, view->channel,
 
468
                                         view->start, view->end);
 
469
 
 
470
      g_snprintf (text, sizeof (text), "%3.1f",
 
471
                  gimp_histogram_get_mean (hist, view->channel,
 
472
                                           view->start, view->end));
 
473
      gtk_label_set_text (GTK_LABEL (editor->labels[0]), text);
 
474
 
 
475
      g_snprintf (text, sizeof (text), "%3.1f",
 
476
                  gimp_histogram_get_std_dev (hist, view->channel,
 
477
                                              view->start, view->end));
 
478
      gtk_label_set_text (GTK_LABEL (editor->labels[1]), text);
 
479
 
 
480
      g_snprintf (text, sizeof (text), "%3.1f",
 
481
                  (gdouble) gimp_histogram_get_median  (hist, view->channel,
 
482
                                                        view->start,
 
483
                                                        view->end));
 
484
      gtk_label_set_text (GTK_LABEL (editor->labels[2]), text);
 
485
 
 
486
      g_snprintf (text, sizeof (text), "%8d", (gint) pixels);
 
487
      gtk_label_set_text (GTK_LABEL (editor->labels[3]), text);
 
488
 
 
489
      g_snprintf (text, sizeof (text), "%8d", (gint) count);
 
490
      gtk_label_set_text (GTK_LABEL (editor->labels[4]), text);
 
491
 
 
492
      g_snprintf (text, sizeof (text), "%4.1f", (pixels > 0 ?
 
493
                                                 (100.0 * count / pixels) :
 
494
                                                 0.0));
 
495
      gtk_label_set_text (GTK_LABEL (editor->labels[5]), text);
 
496
    }
 
497
  else
 
498
    {
 
499
      gint i;
 
500
 
 
501
      for (i = 0; i < 6; i++)
 
502
        gtk_label_set_text (GTK_LABEL (editor->labels[i]), NULL);
 
503
    }
 
504
}