~ubuntu-branches/ubuntu/gutsy/gimp/gutsy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimphistogram module Copyright (C) 1999 Jay Cox <jaycox@gimp.org>
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <string.h>

#include <glib-object.h>

#include "libgimpmath/gimpmath.h"

#include "base-types.h"

#include "gimphistogram.h"
#include "pixel-processor.h"
#include "pixel-region.h"


#ifdef ENABLE_MP
#define NUM_SLOTS  GIMP_MAX_NUM_THREADS
#else
#define NUM_SLOTS  1
#endif


struct _GimpHistogram
{
  gint           n_channels;
#ifdef ENABLE_MP
  GStaticMutex   mutex;
  gchar          slots[NUM_SLOTS];
#endif
  gdouble       *values[NUM_SLOTS];
};


/*  local function prototypes  */

static void  gimp_histogram_alloc_values         (GimpHistogram *histogram,
                                                  gint           bytes);
static void  gimp_histogram_free_values          (GimpHistogram *histogram);
static void  gimp_histogram_calculate_sub_region (GimpHistogram *histogram,
                                                  PixelRegion   *region,
                                                  PixelRegion   *mask);


/*  public functions  */

GimpHistogram *
gimp_histogram_new (void)
{
  GimpHistogram *histogram = g_slice_new0 (GimpHistogram);

#ifdef ENABLE_MP
  g_static_mutex_init (&histogram->mutex);
#endif

  return histogram;
}

void
gimp_histogram_free (GimpHistogram *histogram)
{
  g_return_if_fail (histogram != NULL);

  gimp_histogram_free_values (histogram);
  g_slice_free (GimpHistogram, histogram);
}

void
gimp_histogram_calculate (GimpHistogram *histogram,
                          PixelRegion   *region,
                          PixelRegion   *mask)
{
  gint i;

  g_return_if_fail (histogram != NULL);

  if (! region)
    {
      gimp_histogram_free_values (histogram);
      return;
    }

  gimp_histogram_alloc_values (histogram, region->bytes);

  for (i = 0; i < NUM_SLOTS; i++)
    if (histogram->values[i])
      memset (histogram->values[i],
              0, histogram->n_channels * 256 * sizeof (gdouble));

  pixel_regions_process_parallel ((PixelProcessorFunc)
                                  gimp_histogram_calculate_sub_region,
                                  histogram, 2, region, mask);

#ifdef ENABLE_MP
  /* add up all slots */
  for (i = 1; i < NUM_SLOTS; i++)
    if (histogram->values[i])
      {
        gint j;

        for (j = 0; j < histogram->n_channels * 256; j++)
          histogram->values[0][j] += histogram->values[i][j];
      }
#endif
}


#define HISTOGRAM_VALUE(c,i) (histogram->values[0][(c) * 256 + (i)])


gdouble
gimp_histogram_get_maximum (GimpHistogram        *histogram,
                            GimpHistogramChannel  channel)
{
  gdouble max = 0.0;
  gint    x;

  g_return_val_if_fail (histogram != NULL, 0.0);

  /*  the gray alpha channel is in slot 1  */
  if (histogram->n_channels == 3 && channel == GIMP_HISTOGRAM_ALPHA)
    channel = 1;

  if (! histogram->values[0] ||
      (channel != GIMP_HISTOGRAM_RGB && channel >= histogram->n_channels))
    return 0.0;

  if (channel == GIMP_HISTOGRAM_RGB)
    for (x = 0; x < 256; x++)
      {
        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));
      }
  else
    for (x = 0; x < 256; x++)
      {
        max = MAX (max, HISTOGRAM_VALUE (channel, x));
      }

  return max;
}

gdouble
gimp_histogram_get_value (GimpHistogram        *histogram,
                          GimpHistogramChannel  channel,
                          gint                  bin)
{
  g_return_val_if_fail (histogram != NULL, 0.0);

  /*  the gray alpha channel is in slot 1  */
  if (histogram->n_channels == 3 && channel == GIMP_HISTOGRAM_ALPHA)
    channel = 1;

  if (! histogram->values[0] ||
      bin < 0 || bin >= 256 ||
      (channel == GIMP_HISTOGRAM_RGB && histogram->n_channels < 4) ||
      (channel != GIMP_HISTOGRAM_RGB && channel >= histogram->n_channels))
    return 0.0;

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

      min = MIN (min, HISTOGRAM_VALUE (GIMP_HISTOGRAM_GREEN, bin));

      return MIN (min, HISTOGRAM_VALUE (GIMP_HISTOGRAM_BLUE, bin));
    }
  else
    {
      return HISTOGRAM_VALUE (channel, bin);
    }
}

gdouble
gimp_histogram_get_channel (GimpHistogram        *histogram,
                            GimpHistogramChannel  channel,
                            gint                  bin)
{
  g_return_val_if_fail (histogram != NULL, 0.0);

  if (histogram->n_channels > 3)
    channel++;

  return gimp_histogram_get_value (histogram, channel, bin);
}

gint
gimp_histogram_n_channels (GimpHistogram *histogram)
{
  g_return_val_if_fail (histogram != NULL, 0);

  return histogram->n_channels - 1;
}

gdouble
gimp_histogram_get_count (GimpHistogram        *histogram,
                          GimpHistogramChannel  channel,
                          gint                  start,
                          gint                  end)
{
  gint    i;
  gdouble count = 0.0;

  g_return_val_if_fail (histogram != NULL, 0.0);

  /*  the gray alpha channel is in slot 1  */
  if (histogram->n_channels == 3 && channel == GIMP_HISTOGRAM_ALPHA)
    channel = 1;

  if (channel == GIMP_HISTOGRAM_RGB)
    return (gimp_histogram_get_count (histogram,
                                      GIMP_HISTOGRAM_RED, start, end)   +
            gimp_histogram_get_count (histogram,
                                      GIMP_HISTOGRAM_GREEN, start, end) +
            gimp_histogram_get_count (histogram,
                                      GIMP_HISTOGRAM_BLUE, start, end));

  if (! histogram->values[0] ||
      start > end ||
      channel >= histogram->n_channels)
    return 0.0;

  start = CLAMP (start, 0, 255);
  end   = CLAMP (end, 0, 255);

  for (i = start; i <= end; i++)
    count += HISTOGRAM_VALUE (channel, i);

  return count;
}

gdouble
gimp_histogram_get_mean (GimpHistogram        *histogram,
                         GimpHistogramChannel  channel,
                         gint                  start,
                         gint                  end)
{
  gint    i;
  gdouble mean = 0.0;
  gdouble count;

  g_return_val_if_fail (histogram != NULL, 0.0);

  /*  the gray alpha channel is in slot 1  */
  if (histogram->n_channels == 3 && channel == GIMP_HISTOGRAM_ALPHA)
    channel = 1;

  if (! histogram->values[0] ||
      start > end ||
      (channel == GIMP_HISTOGRAM_RGB && histogram->n_channels < 4) ||
      (channel != GIMP_HISTOGRAM_RGB && channel >= histogram->n_channels))
    return 0.0;

  start = CLAMP (start, 0, 255);
  end = CLAMP (end, 0, 255);

  if (channel == GIMP_HISTOGRAM_RGB)
    {
      for (i = start; i <= end; i++)
        mean += (i * HISTOGRAM_VALUE (GIMP_HISTOGRAM_RED,   i) +
                 i * HISTOGRAM_VALUE (GIMP_HISTOGRAM_GREEN, i) +
                 i * HISTOGRAM_VALUE (GIMP_HISTOGRAM_BLUE,  i));
    }
  else
    {
      for (i = start; i <= end; i++)
        mean += i * HISTOGRAM_VALUE (channel, i);
    }

  count = gimp_histogram_get_count (histogram, channel, start, end);

  if (count > 0.0)
    return mean / count;

  return mean;
}

gint
gimp_histogram_get_median (GimpHistogram         *histogram,
                           GimpHistogramChannel   channel,
                           gint                   start,
                           gint                   end)
{
  gint    i;
  gdouble sum = 0.0;
  gdouble count;

  g_return_val_if_fail (histogram != NULL, -1);

  /*  the gray alpha channel is in slot 1  */
  if (histogram->n_channels == 3 && channel == GIMP_HISTOGRAM_ALPHA)
    channel = 1;

  if (! histogram->values[0] ||
      start > end ||
      (channel == GIMP_HISTOGRAM_RGB && histogram->n_channels < 4) ||
      (channel != GIMP_HISTOGRAM_RGB && channel >= histogram->n_channels))
    return 0;

  start = CLAMP (start, 0, 255);
  end = CLAMP (end, 0, 255);

  count = gimp_histogram_get_count (histogram, channel, start, end);

  if (channel == GIMP_HISTOGRAM_RGB)
    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)
          return i;
      }
  else
    for (i = start; i <= end; i++)
      {
        sum += HISTOGRAM_VALUE (channel, i);

        if (sum * 2 > count)
          return i;
      }

  return -1;
}

/*
 * 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)
{
  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 (histogram != NULL, -1);

  /*  the gray alpha channel is in slot 1  */
  if (histogram->n_channels == 3 && channel == GIMP_HISTOGRAM_ALPHA)
    channel = 1;

  if (! histogram->values[0] ||
      start > end ||
      (channel == GIMP_HISTOGRAM_RGB && histogram->n_channels < 4) ||
      (channel != GIMP_HISTOGRAM_RGB && channel >= histogram->n_channels))
    return 0;

  start = CLAMP (start, 0, 255);
  end = CLAMP (end, 0, 255);

  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;

  for (i = 1; i <= maxval; i++)
    {
      if (hist[i] > hist_max)
	hist_max = hist[i];

      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)
    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;
	  }
      }

  return threshold;
}

gdouble
gimp_histogram_get_std_dev (GimpHistogram        *histogram,
                            GimpHistogramChannel  channel,
                            gint                  start,
                            gint                  end)
{
  gint    i;
  gdouble dev = 0.0;
  gdouble count;
  gdouble mean;

  g_return_val_if_fail (histogram != NULL, 0.0);

  /*  the gray alpha channel is in slot 1  */
  if (histogram->n_channels == 3 && channel == GIMP_HISTOGRAM_ALPHA)
    channel = 1;

  if (! histogram->values[0] ||
      start > end ||
      (channel == GIMP_HISTOGRAM_RGB && histogram->n_channels < 4) ||
      (channel != GIMP_HISTOGRAM_RGB && channel >= histogram->n_channels))
    return 0.0;

  mean  = gimp_histogram_get_mean  (histogram, channel, start, end);
  count = gimp_histogram_get_count (histogram, channel, start, end);

  if (count == 0.0)
    count = 1.0;

  for (i = start; i <= end; i++)
    dev += gimp_histogram_get_value (histogram, channel, i) * SQR (i - mean);

  return sqrt (dev / count);
}


/*  private functions  */

static void
gimp_histogram_alloc_values (GimpHistogram *histogram,
                             gint           bytes)
{
  if (bytes + 1 != histogram->n_channels)
    {
      gimp_histogram_free_values (histogram);

      histogram->n_channels = bytes + 1;

      histogram->values[0] = g_new (gdouble, histogram->n_channels * 256);
    }
}

static void
gimp_histogram_free_values (GimpHistogram *histogram)
{
  gint i;

  for (i = 0; i < NUM_SLOTS; i++)
    if (histogram->values[i])
      {
        g_free (histogram->values[i]);
        histogram->values[i] = NULL;
      }

  histogram->n_channels = 0;
}

static void
gimp_histogram_calculate_sub_region (GimpHistogram *histogram,
                                     PixelRegion   *region,
                                     PixelRegion   *mask)
{
  const guchar *src, *msrc;
  const guchar *m, *s;
  gdouble      *values;
  gint          h, w, max;

#ifdef ENABLE_MP
  gint slot = 0;

  /* find an unused temporary slot to put our results in and lock it */
  g_static_mutex_lock (&histogram->mutex);

  while (histogram->slots[slot])
    slot++;

  values = histogram->values[slot];
  histogram->slots[slot] = 1;

  g_static_mutex_unlock (&histogram->mutex);

  if (! values)
    {
      histogram->values[slot] = g_new0 (gdouble, histogram->n_channels * 256);
      values = histogram->values[slot];
    }

#else
  values = histogram->values[0];
#endif

#define VALUE(c,i) (values[(c) * 256 + (i)])

  h = region->h;
  w = region->w;

  if (mask)
    {
      gdouble masked;

      src  = region->data;
      msrc = mask->data;

      switch (region->bytes)
        {
        case 1:
          while (h--)
            {
              s = src;
              m = msrc;
              w = region->w;

              while (w--)
                {
                  masked = m[0] / 255.0;

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

                  s += 1;
                  m += 1;
                }

              src  += region->rowstride;
              msrc += mask->rowstride;
            }
          break;

        case 2:
          while (h--)
            {
              s = src;
              m = msrc;
              w = region->w;

              while (w--)
                {
                  masked = m[0] / 255.0;

                  VALUE (0, s[0]) += masked;
                  VALUE (1, s[1]) += masked;

                  s += 2;
                  m += 1;
                }

              src  += region->rowstride;
              msrc += mask->rowstride;
            }
          break;

        case 3: /* calculate separate value values */
          while (h--)
            {
              s = src;
              m = msrc;
              w = region->w;

              while (w--)
                {
                  masked = m[0] / 255.0;

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

                  max = (s[0] > s[1]) ? s[0] : s[1];

                  if (s[2] > max)
                    VALUE (0, s[2]) += masked;
                  else
                    VALUE (0, max) += masked;

                  s += 3;
                  m += 1;
                }

              src  += region->rowstride;
              msrc += mask->rowstride;
            }
          break;

        case 4: /* calculate separate value values */
          while (h--)
            {
              s = src;
              m = msrc;
              w = region->w;

              while (w--)
                {
                  masked = m[0] / 255.0;

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

                  max = (s[0] > s[1]) ? s[0] : s[1];

                  if (s[2] > max)
                   VALUE (0, s[2]) += masked;
                  else
                    VALUE (0, max) += masked;

                  s += 4;
                  m += 1;
                }

              src  += region->rowstride;
              msrc += mask->rowstride;
            }
          break;
        }
    }
  else /* no mask */
    {
      src = region->data;

      switch (region->bytes)
        {
        case 1:
          while (h--)
            {
              s = src;
              w = region->w;

              while (w--)
                {
                  VALUE (0, s[0]) += 1.0;

                  s += 1;
                }

              src += region->rowstride;
            }
          break;

        case 2:
          while (h--)
            {
              s = src;
              w = region->w;

              while (w--)
                {
                  VALUE (0, s[0]) += 1.0;
                  VALUE (1, s[1]) += 1.0;

                  s += 2;
                }

              src += region->rowstride;
            }
          break;

        case 3: /* calculate separate value values */
          while (h--)
            {
              s = src;
              w = region->w;

              while (w--)
                {
                  VALUE (1, s[0]) += 1.0;
                  VALUE (2, s[1]) += 1.0;
                  VALUE (3, s[2]) += 1.0;

                  max = (s[0] > s[1]) ? s[0] : s[1];

                  if (s[2] > max)
                    VALUE (0, s[2]) += 1.0;
                  else
                    VALUE (0, max) += 1.0;

                  s += 3;
                }

              src += region->rowstride;
            }
          break;

        case 4: /* calculate separate value values */
          while (h--)
            {
              s = src;
              w = region->w;

              while (w--)
                {
                  VALUE (1, s[0]) += 1.0;
                  VALUE (2, s[1]) += 1.0;
                  VALUE (3, s[2]) += 1.0;
                  VALUE (4, s[3]) += 1.0;

                  max = (s[0] > s[1]) ? s[0] : s[1];

                  if (s[2] > max)
                    VALUE (0, s[2]) += 1.0;
                  else
                    VALUE (0, max) += 1.0;

                  s += 4;
                }

              src += region->rowstride;
            }
          break;
        }
    }

#ifdef ENABLE_MP
  /* unlock this slot */
  g_static_mutex_lock (&histogram->mutex);

  histogram->slots[slot] = 0;

  g_static_mutex_unlock (&histogram->mutex);
#endif
}