gimphistogram.c 27.9 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * gimphistogram module Copyright (C) 1999 Jay Cox <jaycox@gimp.org>
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9 10 11 12 13 14 15 16
 * (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
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 19 20 21
 */

#include "config.h"

22
#include <string.h>
23
#include <cairo.h>
24
#include <gegl.h>
25
#include <gdk-pixbuf/gdk-pixbuf.h>
26

27
#include "libgimpmath/gimpmath.h"
28
#include "libgimpcolor/gimpcolor.h"
29

30 31
#include "core-types.h"

32 33
#include "gegl/gimp-babl.h"

34
#include "gimphistogram.h"
35

36

37 38 39 40 41 42 43 44 45
enum
{
  PROP_0,
  PROP_N_CHANNELS,
  PROP_N_BINS,
  PROP_VALUES
};

struct _GimpHistogramPrivate
46
{
47 48 49 50
  gboolean  linear;
  gint      n_channels;
  gint      n_bins;
  gdouble  *values;
51
};
52 53


Michael Natterer's avatar
Michael Natterer committed
54 55
/*  local function prototypes  */

56 57 58 59 60 61 62 63 64
static void     gimp_histogram_finalize     (GObject       *object);
static void     gimp_histogram_set_property (GObject       *object,
                                             guint          property_id,
                                             const GValue  *value,
                                             GParamSpec    *pspec);
static void     gimp_histogram_get_property (GObject       *object,
                                             guint          property_id,
                                             GValue        *value,
                                             GParamSpec    *pspec);
Michael Natterer's avatar
Michael Natterer committed
65

66 67
static gint64   gimp_histogram_get_memsize  (GimpObject    *object,
                                             gint64        *gui_size);
Michael Natterer's avatar
Michael Natterer committed
68

69 70 71
static void     gimp_histogram_alloc_values (GimpHistogram *histogram,
                                             gint           n_components,
                                             gint           n_bins);
Michael Natterer's avatar
Michael Natterer committed
72

73 74 75 76 77 78 79 80

G_DEFINE_TYPE (GimpHistogram, gimp_histogram, GIMP_TYPE_OBJECT)

#define parent_class gimp_histogram_parent_class


static void
gimp_histogram_class_init (GimpHistogramClass *klass)
81
{
82 83 84 85 86 87 88 89
  GObjectClass      *object_class      = G_OBJECT_CLASS (klass);
  GimpObjectClass   *gimp_object_class = GIMP_OBJECT_CLASS (klass);

  object_class->finalize         = gimp_histogram_finalize;
  object_class->set_property     = gimp_histogram_set_property;
  object_class->get_property     = gimp_histogram_get_property;

  gimp_object_class->get_memsize = gimp_histogram_get_memsize;
Sven Neumann's avatar
Sven Neumann committed
90

91 92
  g_object_class_install_property (object_class, PROP_N_CHANNELS,
                                   g_param_spec_int ("n-channels", NULL, NULL,
93
                                                     0, 6, 0,
94
                                                     GIMP_PARAM_READABLE));
95

96 97 98 99 100 101 102 103 104 105 106 107
  g_object_class_install_property (object_class, PROP_N_BINS,
                                   g_param_spec_int ("n-bins", NULL, NULL,
                                                     256, 1024, 1024,
                                                     GIMP_PARAM_READABLE));

  /* this is just for notifications */
  g_object_class_install_property (object_class, PROP_VALUES,
                                   g_param_spec_boolean ("values", NULL, NULL,
                                                         FALSE,
                                                         G_PARAM_READABLE));

  g_type_class_add_private (klass, sizeof (GimpHistogramPrivate));
108 109
}

110 111
static void
gimp_histogram_init (GimpHistogram *histogram)
112
{
113 114 115
  histogram->priv = G_TYPE_INSTANCE_GET_PRIVATE (histogram,
                                                 GIMP_TYPE_HISTOGRAM,
                                                 GimpHistogramPrivate);
116

117 118 119 120 121 122 123
  histogram->priv->n_bins = 256;
}

static void
gimp_histogram_finalize (GObject *object)
{
  GimpHistogram *histogram = GIMP_HISTOGRAM (object);
124

125 126 127
  gimp_histogram_clear_values (histogram);

  G_OBJECT_CLASS (parent_class)->finalize (object);
128 129
}

130 131 132 133 134
static void
gimp_histogram_set_property (GObject      *object,
                             guint         property_id,
                             const GValue *value,
                             GParamSpec   *pspec)
Michael Natterer's avatar
Michael Natterer committed
135
{
136 137 138 139 140 141 142
  switch (property_id)
    {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}
Michael Natterer's avatar
Michael Natterer committed
143

144 145 146 147 148 149 150
static void
gimp_histogram_get_property (GObject    *object,
                             guint       property_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
  GimpHistogram *histogram = GIMP_HISTOGRAM (object);
151

152
  switch (property_id)
153
    {
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    case PROP_N_CHANNELS:
      g_value_set_int (value, histogram->priv->n_channels);
      break;

    case PROP_N_BINS:
      g_value_set_int (value, histogram->priv->n_bins);
      break;

    case PROP_VALUES:
      /* return a silly boolean */
      g_value_set_boolean (value, histogram->priv->values != NULL);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
170
    }
Michael Natterer's avatar
Michael Natterer committed
171 172
}

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
static gint64
gimp_histogram_get_memsize (GimpObject *object,
                            gint64     *gui_size)
{
  GimpHistogram *histogram = GIMP_HISTOGRAM (object);
  gint64         memsize   = 0;

  if (histogram->priv->values)
    memsize += (histogram->priv->n_channels *
                histogram->priv->n_bins * sizeof (gdouble));

  return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
                                                                  gui_size);
}

/*  public functions  */

GimpHistogram *
191
gimp_histogram_new (gboolean linear)
192
{
193 194
  GimpHistogram *histogram = g_object_new (GIMP_TYPE_HISTOGRAM, NULL);

195
  histogram->priv->linear = linear;
196 197

  return histogram;
198 199
}

200 201 202 203 204 205 206 207 208 209 210 211 212 213
/**
 * gimp_histogram_duplicate:
 * @histogram: a %GimpHistogram
 *
 * Creates a duplicate of @histogram. The duplicate has a reference
 * count of 1 and contains the values from @histogram.
 *
 * Return value: a newly allocated %GimpHistogram
 **/
GimpHistogram *
gimp_histogram_duplicate (GimpHistogram *histogram)
{
  GimpHistogram *dup;

214
  g_return_val_if_fail (GIMP_IS_HISTOGRAM (histogram), NULL);
215

216
  dup = gimp_histogram_new (histogram->priv->linear);
217

218 219 220 221 222 223
  dup->priv->n_channels = histogram->priv->n_channels;
  dup->priv->n_bins     = histogram->priv->n_bins;
  dup->priv->values     = g_memdup (histogram->priv->values,
                                    sizeof (gdouble) *
                                    dup->priv->n_channels *
                                    dup->priv->n_bins);
224 225 226 227

  return dup;
}

228
void
229
gimp_histogram_calculate (GimpHistogram       *histogram,
230 231 232 233 234
                          GeglBuffer          *buffer,
                          const GeglRectangle *buffer_rect,
                          GeglBuffer          *mask,
                          const GeglRectangle *mask_rect)
{
235 236 237 238 239
  GimpHistogramPrivate *priv;
  GeglBufferIterator   *iter;
  const Babl           *format;
  gint                  n_components;
  gint                  n_bins;
240
  gfloat                n_bins_1f;
241
  gfloat                temp;
242

243
  g_return_if_fail (GIMP_IS_HISTOGRAM (histogram));
244 245 246
  g_return_if_fail (GEGL_IS_BUFFER (buffer));
  g_return_if_fail (buffer_rect != NULL);

247 248
  priv = histogram->priv;

249 250
  format = gegl_buffer_get_format (buffer);

251 252 253 254 255
  if (babl_format_get_type (format, 0) == babl_type ("u8"))
    n_bins = 256;
  else
    n_bins = 1024;

256
  if (babl_format_is_palette (format))
257 258
    {
      if (babl_format_has_alpha (format))
259 260 261 262 263 264
        {
          if (priv->linear)
            format = babl_format ("RGB float");
          else
            format = babl_format ("R'G'B' float");
        }
265
      else
266 267 268 269 270 271
        {
          if (priv->linear)
            format = babl_format ("RGBA float");
          else
            format = babl_format ("R'G'B'A float");
        }
272
    }
273
  else
274 275 276
    {
      const Babl *model = babl_format_get_model (format);

277 278
      if (model == babl_model ("Y") ||
          model == babl_model ("Y'"))
279
        {
280
          if (priv->linear)
281 282
            format = babl_format ("Y float");
          else
283
            format = babl_format ("Y' float");
284
        }
285 286
      else if (model == babl_model ("YA") ||
               model == babl_model ("Y'A"))
287
        {
288 289
          if (priv->linear)
            format = babl_format ("YA float");
290
          else
291
            format = babl_format ("Y'A float");
292
        }
293 294
      else if (model == babl_model ("RGB") ||
               model == babl_model ("R'G'B'"))
295
        {
296 297
          if (priv->linear)
            format = babl_format ("RGB float");
298
          else
299
            format = babl_format ("R'G'B' float");
300
        }
301 302
      else if (model == babl_model ("RGBA") ||
               model == babl_model ("R'G'B'A"))
303
        {
304 305 306 307
          if (priv->linear)
            format = babl_format ("RGBA float");
          else
            format = babl_format ("R'G'B'A float");
308 309 310 311 312 313
        }
      else
        {
          g_return_if_reached ();
        }
    }
314

Simon Budig's avatar
Simon Budig committed
315
  n_components = babl_format_get_n_components (format);
316

317 318
  g_object_freeze_notify (G_OBJECT (histogram));

319
  gimp_histogram_alloc_values (histogram, n_components, n_bins);
320

321
  iter = gegl_buffer_iterator_new (buffer, buffer_rect, 0, format,
322
                                   GEGL_ACCESS_READ, GEGL_ABYSS_NONE);
323 324 325 326

  if (mask)
    gegl_buffer_iterator_add (iter, mask, mask_rect, 0,
                              babl_format ("Y float"),
327
                              GEGL_ACCESS_READ, GEGL_ABYSS_NONE);
328

329 330
  n_bins_1f = priv->n_bins - 1;

331 332 333 334 335
#define VALUE(c,i) (*(temp = (i) * n_bins_1f,                                  \
                      &priv->values[(c) * priv->n_bins +                       \
                                    SIGNED_ROUND (SAFE_CLAMP (temp,            \
                                                              0.0f,            \
                                                              n_bins_1f))]))
336 337 338

  while (gegl_buffer_iterator_next (iter))
    {
339 340
      const gfloat *data   = iter->data[0];
      gint          length = iter->length;
341
      gfloat        max;
342
      gfloat        luminance;
343 344 345 346 347

      if (mask)
        {
          const gfloat *mask_data = iter->data[1];

348
          switch (n_components)
349 350
            {
            case 1:
351
              while (length--)
352 353 354 355 356
                {
                  const gdouble masked = *mask_data;

                  VALUE (0, data[0]) += masked;

357
                  data += n_components;
358 359 360 361 362
                  mask_data += 1;
                }
              break;

            case 2:
363
              while (length--)
364 365
                {
                  const gdouble masked = *mask_data;
366
                  const gdouble weight = data[1];
367 368 369 370

                  VALUE (0, data[0]) += weight * masked;
                  VALUE (1, data[1]) += masked;

371
                  data += n_components;
372 373 374 375 376
                  mask_data += 1;
                }
              break;

            case 3: /* calculate separate value values */
377
              while (length--)
378 379 380 381 382 383 384 385 386 387 388
                {
                  const gdouble masked = *mask_data;

                  VALUE (1, data[0]) += masked;
                  VALUE (2, data[1]) += masked;
                  VALUE (3, data[2]) += masked;

                  max = MAX (data[0], data[1]);
                  max = MAX (data[2], max);
                  VALUE (0, max) += masked;

389 390 391
                  luminance = GIMP_RGB_LUMINANCE (data[0], data[1], data[2]);
                  VALUE (4, luminance) += masked;

392
                  data += n_components;
393 394 395 396 397
                  mask_data += 1;
                }
              break;

            case 4: /* calculate separate value values */
398
              while (length--)
399 400
                {
                  const gdouble masked = *mask_data;
401
                  const gdouble weight = data[3];
402 403 404 405 406 407 408 409 410 411

                  VALUE (1, data[0]) += weight * masked;
                  VALUE (2, data[1]) += weight * masked;
                  VALUE (3, data[2]) += weight * masked;
                  VALUE (4, data[3]) += masked;

                  max = MAX (data[0], data[1]);
                  max = MAX (data[2], max);
                  VALUE (0, max) += weight * masked;

412 413 414
                  luminance = GIMP_RGB_LUMINANCE (data[0], data[1], data[2]);
                  VALUE (5, luminance) += weight * masked;

415
                  data += n_components;
416 417 418 419 420 421 422
                  mask_data += 1;
                }
              break;
            }
        }
      else /* no mask */
        {
423
          switch (n_components)
424 425
            {
            case 1:
426
              while (length--)
427 428 429
                {
                  VALUE (0, data[0]) += 1.0;

430
                  data += n_components;
431 432 433 434
                }
              break;

            case 2:
435
              while (length--)
436
                {
437
                  const gdouble weight = data[1];
438 439 440 441

                  VALUE (0, data[0]) += weight;
                  VALUE (1, data[1]) += 1.0;

442
                  data += n_components;
443 444 445 446
                }
              break;

            case 3: /* calculate separate value values */
447
              while (length--)
448 449 450 451 452 453 454 455 456
                {
                  VALUE (1, data[0]) += 1.0;
                  VALUE (2, data[1]) += 1.0;
                  VALUE (3, data[2]) += 1.0;

                  max = MAX (data[0], data[1]);
                  max = MAX (data[2], max);
                  VALUE (0, max) += 1.0;

457 458 459
                  luminance = GIMP_RGB_LUMINANCE (data[0], data[1], data[2]);
                  VALUE (4, luminance) += 1.0;

460
                  data += n_components;
461 462 463 464
                }
              break;

            case 4: /* calculate separate value values */
465
              while (length--)
466
                {
467
                  const gdouble weight = data[3];
468 469 470 471 472 473 474 475 476 477

                  VALUE (1, data[0]) += weight;
                  VALUE (2, data[1]) += weight;
                  VALUE (3, data[2]) += weight;
                  VALUE (4, data[3]) += 1.0;

                  max = MAX (data[0], data[1]);
                  max = MAX (data[2], max);
                  VALUE (0, max) += weight;

478 479 480
                  luminance = GIMP_RGB_LUMINANCE (data[0], data[1], data[2]);
                  VALUE (5, luminance) += weight;

481
                  data += n_components;
482 483 484 485 486 487
                }
              break;
            }
        }
    }

488 489 490 491
  g_object_notify (G_OBJECT (histogram), "values");

  g_object_thaw_notify (G_OBJECT (histogram));

492 493 494
#undef VALUE
}

495 496 497
void
gimp_histogram_clear_values (GimpHistogram *histogram)
{
498
  g_return_if_fail (GIMP_IS_HISTOGRAM (histogram));
499

500
  if (histogram->priv->values)
501
    {
502 503 504 505
      g_free (histogram->priv->values);
      histogram->priv->values = NULL;

      g_object_notify (G_OBJECT (histogram), "values");
506
    }
507

508 509 510 511 512 513
  if (histogram->priv->n_channels)
    {
      histogram->priv->n_channels = 0;

      g_object_notify (G_OBJECT (histogram), "n-channels");
    }
514 515
}

516

517
#define HISTOGRAM_VALUE(c,i) (priv->values[(c) * priv->n_bins + (i)])
518 519


Michael Natterer's avatar
Michael Natterer committed
520
gdouble
521
gimp_histogram_get_maximum (GimpHistogram        *histogram,
522
                            GimpHistogramChannel  channel)
Michael Natterer's avatar
Michael Natterer committed
523
{
524 525 526 527 528
  GimpHistogramPrivate *priv;
  gdouble               max = 0.0;
  gint                  x;

  g_return_val_if_fail (GIMP_IS_HISTOGRAM (histogram), 0.0);
Michael Natterer's avatar
Michael Natterer committed
529

530
  priv = histogram->priv;
Michael Natterer's avatar
Michael Natterer committed
531

532
  /*  the gray alpha channel is in slot 1  */
533
  if (priv->n_channels == 4 && channel == GIMP_HISTOGRAM_ALPHA)
534
    channel = 1;
535 536 537
  /*  the luminance channel is in slot 4  */
  else if (priv->n_channels == 5 && channel == GIMP_HISTOGRAM_LUMINANCE)
    channel = 4;
538

539 540
  if (! priv->values ||
      (channel != GIMP_HISTOGRAM_RGB && channel >= priv->n_channels))
541 542
    return 0.0;

543
  if (channel == GIMP_HISTOGRAM_RGB)
544
    {
545
      for (x = 0; x < priv->n_bins; x++)
546 547 548 549 550 551
        {
          max = MAX (max, HISTOGRAM_VALUE (GIMP_HISTOGRAM_RED,   x));
          max = MAX (max, HISTOGRAM_VALUE (GIMP_HISTOGRAM_GREEN, x));
          max = MAX (max, HISTOGRAM_VALUE (GIMP_HISTOGRAM_BLUE,  x));
        }
    }
552
  else
553
    {
554
      for (x = 0; x < priv->n_bins; x++)
555 556 557 558
        {
          max = MAX (max, HISTOGRAM_VALUE (channel, x));
        }
    }
Michael Natterer's avatar
Michael Natterer committed
559 560 561 562 563

  return max;
}

gdouble
564
gimp_histogram_get_value (GimpHistogram        *histogram,
565 566
                          GimpHistogramChannel  channel,
                          gint                  bin)
Michael Natterer's avatar
Michael Natterer committed
567
{
568 569 570 571 572
  GimpHistogramPrivate *priv;

  g_return_val_if_fail (GIMP_IS_HISTOGRAM (histogram), 0.0);

  priv = histogram->priv;
Michael Natterer's avatar
Michael Natterer committed
573

574
  /*  the gray alpha channel is in slot 1  */
575
  if (priv->n_channels == 4 && channel == GIMP_HISTOGRAM_ALPHA)
576
    channel = 1;
577 578 579
  /*  the luminance channel is in slot 4  */
  else if (priv->n_channels == 5 && channel == GIMP_HISTOGRAM_LUMINANCE)
    channel = 4;
580

581 582 583 584
  if (! priv->values ||
      bin < 0 || bin >= priv->n_bins ||
      (channel == GIMP_HISTOGRAM_RGB && priv->n_channels < 4) ||
      (channel != GIMP_HISTOGRAM_RGB && channel >= priv->n_channels))
585 586 587 588
    return 0.0;

  if (channel == GIMP_HISTOGRAM_RGB)
    {
589
      gdouble min = HISTOGRAM_VALUE (GIMP_HISTOGRAM_RED, bin);
590

591
      min = MIN (min, HISTOGRAM_VALUE (GIMP_HISTOGRAM_GREEN, bin));
Michael Natterer's avatar
Michael Natterer committed
592

593
      return MIN (min, HISTOGRAM_VALUE (GIMP_HISTOGRAM_BLUE, bin));
594 595
    }
  else
596
    {
597
      return HISTOGRAM_VALUE (channel, bin);
598
    }
Michael Natterer's avatar
Michael Natterer committed
599 600 601
}

gdouble
602 603 604
gimp_histogram_get_component (GimpHistogram *histogram,
                              gint           component,
                              gint           bin)
Michael Natterer's avatar
Michael Natterer committed
605
{
606
  g_return_val_if_fail (GIMP_IS_HISTOGRAM (histogram), 0.0);
Michael Natterer's avatar
Michael Natterer committed
607

608
  if (histogram->priv->n_channels > 4)
609
    component++;
610

611
  return gimp_histogram_get_value (histogram, component, bin);
Michael Natterer's avatar
Michael Natterer committed
612 613 614
}

gint
615
gimp_histogram_n_channels (GimpHistogram *histogram)
Michael Natterer's avatar
Michael Natterer committed
616
{
617
  g_return_val_if_fail (GIMP_IS_HISTOGRAM (histogram), 0);
Michael Natterer's avatar
Michael Natterer committed
618

619
  return histogram->priv->n_channels - 2;
Michael Natterer's avatar
Michael Natterer committed
620 621
}

622 623 624
gint
gimp_histogram_n_bins (GimpHistogram *histogram)
{
625
  g_return_val_if_fail (GIMP_IS_HISTOGRAM (histogram), 0);
626

627
  return histogram->priv->n_bins;
628 629
}

Michael Natterer's avatar
Michael Natterer committed
630
gdouble
631 632
gimp_histogram_get_count (GimpHistogram        *histogram,
                          GimpHistogramChannel  channel,
633 634
                          gint                  start,
                          gint                  end)
Michael Natterer's avatar
Michael Natterer committed
635
{
636 637 638
  GimpHistogramPrivate *priv;
  gint                  i;
  gdouble               count = 0.0;
Michael Natterer's avatar
Michael Natterer committed
639

640 641 642
  g_return_val_if_fail (GIMP_IS_HISTOGRAM (histogram), 0.0);

  priv = histogram->priv;
Michael Natterer's avatar
Michael Natterer committed
643

644
  /*  the gray alpha channel is in slot 1  */
645
  if (priv->n_channels == 4 && channel == GIMP_HISTOGRAM_ALPHA)
646
    channel = 1;
647 648 649
  /*  the luminance channel is in slot 4  */
  else if (priv->n_channels == 5 && channel == GIMP_HISTOGRAM_LUMINANCE)
    channel = 4;
650

651 652 653
  if (channel == GIMP_HISTOGRAM_RGB)
    return (gimp_histogram_get_count (histogram,
                                      GIMP_HISTOGRAM_RED, start, end)   +
654
            gimp_histogram_get_count (histogram,
655
                                      GIMP_HISTOGRAM_GREEN, start, end) +
656
            gimp_histogram_get_count (histogram,
657 658
                                      GIMP_HISTOGRAM_BLUE, start, end));

659
  if (! priv->values ||
660
      start > end ||
661
      channel >= priv->n_channels)
662 663
    return 0.0;

664 665
  start = CLAMP (start, 0, priv->n_bins - 1);
  end   = CLAMP (end,   0, priv->n_bins - 1);
666

Michael Natterer's avatar
Michael Natterer committed
667
  for (i = start; i <= end; i++)
668
    count += HISTOGRAM_VALUE (channel, i);
Michael Natterer's avatar
Michael Natterer committed
669 670 671 672 673

  return count;
}

gdouble
674
gimp_histogram_get_mean (GimpHistogram        *histogram,
675 676 677
                         GimpHistogramChannel  channel,
                         gint                  start,
                         gint                  end)
Michael Natterer's avatar
Michael Natterer committed
678
{
679 680 681 682 683 684
  GimpHistogramPrivate *priv;
  gint                  i;
  gdouble               mean = 0.0;
  gdouble               count;

  g_return_val_if_fail (GIMP_IS_HISTOGRAM (histogram), 0.0);
Michael Natterer's avatar
Michael Natterer committed
685

686
  priv = histogram->priv;
Michael Natterer's avatar
Michael Natterer committed
687

688
  /*  the gray alpha channel is in slot 1  */
689
  if (priv->n_channels == 4 && channel == GIMP_HISTOGRAM_ALPHA)
690
    channel = 1;
691 692 693
  /*  the luminance channel is in slot 4  */
  else if (priv->n_channels == 5 && channel == GIMP_HISTOGRAM_LUMINANCE)
    channel = 4;
694

695
  if (! priv->values ||
696
      start > end ||
697
      (channel == GIMP_HISTOGRAM_RGB && priv->n_channels < 5) ||
698
      (channel != GIMP_HISTOGRAM_RGB && channel >= priv->n_channels))
699 700
    return 0.0;

701 702
  start = CLAMP (start, 0, priv->n_bins - 1);
  end   = CLAMP (end,   0, priv->n_bins - 1);
703 704 705 706

  if (channel == GIMP_HISTOGRAM_RGB)
    {
      for (i = start; i <= end; i++)
707 708 709 710 711 712 713
        {
          gdouble factor = (gdouble) i / (gdouble)  (priv->n_bins - 1);

          mean += (factor * HISTOGRAM_VALUE (GIMP_HISTOGRAM_RED,   i) +
                   factor * HISTOGRAM_VALUE (GIMP_HISTOGRAM_GREEN, i) +
                   factor * HISTOGRAM_VALUE (GIMP_HISTOGRAM_BLUE,  i));
        }
714 715 716 717
    }
  else
    {
      for (i = start; i <= end; i++)
718 719 720 721 722
        {
          gdouble factor = (gdouble) i / (gdouble)  (priv->n_bins - 1);

          mean += factor * HISTOGRAM_VALUE (channel, i);
        }
723
    }
Michael Natterer's avatar
Michael Natterer committed
724

725
  count = gimp_histogram_get_count (histogram, channel, start, end);
Michael Natterer's avatar
Michael Natterer committed
726 727 728 729 730 731 732

  if (count > 0.0)
    return mean / count;

  return mean;
}

733
gdouble
734
gimp_histogram_get_median (GimpHistogram         *histogram,
735 736 737
                           GimpHistogramChannel   channel,
                           gint                   start,
                           gint                   end)
Michael Natterer's avatar
Michael Natterer committed
738
{
739 740 741 742
  GimpHistogramPrivate *priv;
  gint                  i;
  gdouble               sum = 0.0;
  gdouble               count;
Michael Natterer's avatar
Michael Natterer committed
743

744
  g_return_val_if_fail (GIMP_IS_HISTOGRAM (histogram), -1.0);
745 746

  priv = histogram->priv;
Michael Natterer's avatar
Michael Natterer committed
747

748
  /*  the gray alpha channel is in slot 1  */
749
  if (priv->n_channels == 4 && channel == GIMP_HISTOGRAM_ALPHA)
750
    channel = 1;
751 752 753
  /*  the luminance channel is in slot 4  */
  else if (priv->n_channels == 5 && channel == GIMP_HISTOGRAM_LUMINANCE)
    channel = 4;
754

755
  if (! priv->values ||
756
      start > end ||
757
      (channel == GIMP_HISTOGRAM_RGB && priv->n_channels < 5) ||
758
      (channel != GIMP_HISTOGRAM_RGB && channel >= priv->n_channels))
759
    return 0.0;
760

761 762
  start = CLAMP (start, 0, priv->n_bins - 1);
  end   = CLAMP (end,   0, priv->n_bins - 1);
763

764
  count = gimp_histogram_get_count (histogram, channel, start, end);
Michael Natterer's avatar
Michael Natterer committed
765

766
  if (channel == GIMP_HISTOGRAM_RGB)
767 768 769 770 771 772 773 774
    {
      for (i = start; i <= end; i++)
        {
          sum += (HISTOGRAM_VALUE (GIMP_HISTOGRAM_RED,   i) +
                  HISTOGRAM_VALUE (GIMP_HISTOGRAM_GREEN, i) +
                  HISTOGRAM_VALUE (GIMP_HISTOGRAM_BLUE,  i));

          if (sum * 2 > count)
775
            return ((gdouble) i / (gdouble)  (priv->n_bins - 1));
776 777
        }
    }
778
  else
779 780 781 782
    {
      for (i = start; i <= end; i++)
        {
          sum += HISTOGRAM_VALUE (channel, i);
783

784
          if (sum * 2 > count)
785
            return ((gdouble) i / (gdouble)  (priv->n_bins - 1));
786 787
        }
    }
Michael Natterer's avatar
Michael Natterer committed
788

789
  return -1.0;
Michael Natterer's avatar
Michael Natterer committed
790 791
}

792 793 794 795 796 797 798 799 800 801 802 803
/*
 * adapted from GNU ocrad 0.14 : page_image_io.cc : otsu_th
 *
 *  N. Otsu, "A threshold selection method from gray-level histograms,"
 *  IEEE Trans. Systems, Man, and Cybernetics, vol. 9, no. 1, pp. 62-66, 1979.
 */
gdouble
gimp_histogram_get_threshold (GimpHistogram        *histogram,
                              GimpHistogramChannel  channel,
                              gint                  start,
                              gint                  end)
{
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
  GimpHistogramPrivate *priv;
  gint                 i;
  gint                 maxval;
  gdouble             *hist      = NULL;
  gdouble             *chist     = NULL;
  gdouble             *cmom      = NULL;
  gdouble              hist_max  = 0.0;
  gdouble              chist_max = 0.0;
  gdouble              cmom_max  = 0.0;
  gdouble              bvar_max  = 0.0;
  gint                 threshold = 127;

  g_return_val_if_fail (GIMP_IS_HISTOGRAM (histogram), -1);

  priv = histogram->priv;
819 820

  /*  the gray alpha channel is in slot 1  */
821
  if (priv->n_channels == 4 && channel == GIMP_HISTOGRAM_ALPHA)
822
    channel = 1;
823 824 825
  /*  the luminance channel is in slot 4  */
  else if (priv->n_channels == 5 && channel == GIMP_HISTOGRAM_LUMINANCE)
    channel = 4;
826

827
  if (! priv->values ||
828
      start > end ||
829
      (channel == GIMP_HISTOGRAM_RGB && priv->n_channels < 5) ||
830
      (channel != GIMP_HISTOGRAM_RGB && channel >= priv->n_channels))
831 832
    return 0;

833 834
  start = CLAMP (start, 0, priv->n_bins - 1);
  end   = CLAMP (end,   0, priv->n_bins - 1);
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858

  maxval = end - start;

  hist  = g_newa (gdouble, maxval + 1);
  chist = g_newa (gdouble, maxval + 1);
  cmom  = g_newa (gdouble, maxval + 1);

  if (channel == GIMP_HISTOGRAM_RGB)
    {
      for (i = start; i <= end; i++)
        hist[i - start] = (HISTOGRAM_VALUE (GIMP_HISTOGRAM_RED,   i) +
                           HISTOGRAM_VALUE (GIMP_HISTOGRAM_GREEN, i) +
                           HISTOGRAM_VALUE (GIMP_HISTOGRAM_BLUE,  i));
    }
  else
    {
      for (i = start; i <= end; i++)
        hist[i - start] = HISTOGRAM_VALUE (channel, i);
    }

  hist_max = hist[0];
  chist[0] = hist[0];
  cmom[0] = 0;

859
  for (i = 1; i <= maxval; i++)
860 861
    {
      if (hist[i] > hist_max)
862
        hist_max = hist[i];
863 864 865 866 867 868 869 870 871 872

      chist[i] = chist[i-1] + hist[i];
      cmom[i] = cmom[i-1] + i * hist[i];
    }

  chist_max = chist[maxval];
  cmom_max = cmom[maxval];
  bvar_max = 0;

  for (i = 0; i < maxval; ++i)
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
    {
      if (chist[i] > 0 && chist[i] < chist_max)
        {
          gdouble bvar;

          bvar = (gdouble) cmom[i] / chist[i];
          bvar -= (cmom_max - cmom[i]) / (chist_max - chist[i]);
          bvar *= bvar;
          bvar *= chist[i];
          bvar *= chist_max - chist[i];

          if (bvar > bvar_max)
            {
              bvar_max = bvar;
              threshold = start + i;
            }
        }
    }
891 892 893 894

  return threshold;
}

Michael Natterer's avatar
Michael Natterer committed
895
gdouble
896
gimp_histogram_get_std_dev (GimpHistogram        *histogram,
897 898 899
                            GimpHistogramChannel  channel,
                            gint                  start,
                            gint                  end)
Michael Natterer's avatar
Michael Natterer committed
900
{
901 902 903 904 905 906 907
  GimpHistogramPrivate *priv;
  gint                  i;
  gdouble               dev = 0.0;
  gdouble               count;
  gdouble               mean;

  g_return_val_if_fail (GIMP_IS_HISTOGRAM (histogram), 0.0);
Michael Natterer's avatar
Michael Natterer committed
908

909
  priv = histogram->priv;
Michael Natterer's avatar
Michael Natterer committed
910

911
  /*  the gray alpha channel is in slot 1  */
912
  if (priv->n_channels == 4 && channel == GIMP_HISTOGRAM_ALPHA)
913
    channel = 1;
914 915 916
  /*  the luminance channel is in slot 4  */
  else if (priv->n_channels == 5 && channel == GIMP_HISTOGRAM_LUMINANCE)
    channel = 4;
917

918
  if (! priv->values ||
919
      start > end ||
920
      (channel == GIMP_HISTOGRAM_RGB && priv->n_channels < 5) ||
921
      (channel != GIMP_HISTOGRAM_RGB && channel >= priv->n_channels))
922 923
    return 0.0;

Michael Natterer's avatar
Michael Natterer committed
924
  mean  = gimp_histogram_get_mean  (histogram, channel, start, end);
925
  count = gimp_histogram_get_count (histogram, channel, start, end);
Michael Natterer's avatar
Michael Natterer committed
926 927 928 929 930

  if (count == 0.0)
    count = 1.0;

  for (i = start; i <= end; i++)
931 932 933 934 935 936 937 938 939 940 941 942 943 944
    {
      gdouble value;

      if (channel == GIMP_HISTOGRAM_RGB)
        {
          value = (HISTOGRAM_VALUE (GIMP_HISTOGRAM_RED,   i) +
                   HISTOGRAM_VALUE (GIMP_HISTOGRAM_GREEN, i) +
                   HISTOGRAM_VALUE (GIMP_HISTOGRAM_BLUE,  i));
        }
      else
        {
          value = gimp_histogram_get_value (histogram, channel, i);
        }

945
      dev += value * SQR (((gdouble) i / (gdouble)  (priv->n_bins - 1)) - mean);
946
    }
Michael Natterer's avatar
Michael Natterer committed
947 948 949 950 951 952 953 954

  return sqrt (dev / count);
}


/*  private functions  */

static void
955
gimp_histogram_alloc_values (GimpHistogram *histogram,
956 957
                             gint           n_components,
                             gint           n_bins)
Michael Natterer's avatar
Michael Natterer committed
958
{
959 960
  GimpHistogramPrivate *priv = histogram->priv;

961
  if (n_components + 2 != priv->n_channels ||
962
      n_bins           != priv->n_bins)
Michael Natterer's avatar
Michael Natterer committed
963
    {
964
      gimp_histogram_clear_values (histogram);
Michael Natterer's avatar
Michael Natterer committed
965

966
      if (n_components + 2 != priv->n_channels)
967
        {
968
          priv->n_channels = n_components + 2;
969 970 971 972 973 974 975 976 977 978

          g_object_notify (G_OBJECT (histogram), "n-channels");
        }

      if (n_bins != priv->n_bins)
        {
          priv->n_bins = n_bins;

          g_object_notify (G_OBJECT (histogram), "n-bins");
        }
979

980
      priv->values = g_new0 (gdouble, priv->n_channels * priv->n_bins);
981 982 983
    }
  else
    {
984 985
      memset (priv->values, 0,
              priv->n_channels * priv->n_bins * sizeof (gdouble));
986 987
    }
}