31
29
#include "base-types.h"
33
#include "config/gimpbaseconfig.h"
35
31
#include "gimphistogram.h"
36
32
#include "pixel-processor.h"
37
33
#include "pixel-region.h"
37
#define NUM_SLOTS GIMP_MAX_NUM_THREADS
40
43
struct _GimpHistogram
47
pthread_mutex_t mutex;
49
gdouble ***tmp_values;
48
gchar slots[NUM_SLOTS];
50
gdouble *values[NUM_SLOTS];
55
54
/* local function prototypes */
57
static void gimp_histogram_alloc_values (GimpHistogram *histogram,
59
static void gimp_histogram_free_values (GimpHistogram *histogram);
60
static void gimp_histogram_calculate_sub_region (GimpHistogram *histogram,
56
static void gimp_histogram_alloc_values (GimpHistogram *histogram,
58
static void gimp_histogram_free_values (GimpHistogram *histogram);
59
static void gimp_histogram_calculate_sub_region (GimpHistogram *histogram,
65
64
/* public functions */
68
gimp_histogram_new (GimpBaseConfig *config)
67
gimp_histogram_new (void)
70
GimpHistogram *histogram;
72
g_return_val_if_fail (GIMP_IS_BASE_CONFIG (config), NULL);
74
histogram = g_new0 (GimpHistogram, 1);
77
histogram->values = NULL;
78
histogram->n_channels = 0;
69
GimpHistogram *histogram = g_new0 (GimpHistogram, 1);
81
pthread_mutex_init (&histogram->mutex, NULL);
83
histogram->num_slots = config->num_processors;
84
histogram->tmp_slots = g_new0 (gchar, histogram->num_slots);
85
histogram->tmp_values = g_new0 (gdouble **, histogram->num_slots);
86
#endif /* ENABLE_MP */
72
g_static_mutex_init (&histogram->mutex);
122
102
gimp_histogram_alloc_values (histogram, region->bytes);
125
for (i = 0; i < histogram->num_slots; i++)
127
histogram->tmp_values[i] = g_new0 (gdouble *, histogram->n_channels);
128
histogram->tmp_slots[i] = 0;
130
for (j = 0; j < histogram->n_channels; j++)
134
histogram->tmp_values[i][j] = g_new0 (gdouble, 256);
136
for (k = 0; k < 256; k++)
137
histogram->tmp_values[i][j][k] = 0.0;
142
for (i = 0; i < histogram->n_channels; i++)
143
for (j = 0; j < 256; j++)
144
histogram->values[i][j] = 0.0;
146
pixel_regions_process_parallel ((p_func) gimp_histogram_calculate_sub_region,
104
for (i = 0; i < NUM_SLOTS; i++)
105
if (histogram->values[i])
106
memset (histogram->values[i],
107
0, histogram->n_channels * 256 * sizeof (gdouble));
109
pixel_regions_process_parallel ((PixelProcessorFunc)
110
gimp_histogram_calculate_sub_region,
147
111
histogram, 2, region, mask);
150
/* add up all the tmp buffers and free their memmory */
151
for (i = 0; i < histogram->num_slots; i++)
153
for (j = 0; j < histogram->n_channels; j++)
157
for (k = 0; k < 256; k++)
158
histogram->values[j][k] += histogram->tmp_values[i][j][k];
160
g_free (histogram->tmp_values[i][j]);
163
g_free (histogram->tmp_values[i]);
114
/* add up all slots */
115
for (i = 1; i < NUM_SLOTS; i++)
116
if (histogram->values[i])
120
for (j = 0; j < histogram->n_channels * 256; j++)
121
histogram->values[0][j] += histogram->values[i][j];
127
#define HISTOGRAM_VALUE(c,i) (histogram->values[0][(c) * 256 + (i)])
169
131
gimp_histogram_get_maximum (GimpHistogram *histogram,
170
GimpHistogramChannel channel)
132
GimpHistogramChannel channel)
172
134
gdouble max = 0.0;
178
140
if (histogram->n_channels == 3 && channel == GIMP_HISTOGRAM_ALPHA)
181
if (! histogram->values ||
143
if (! histogram->values[0] ||
182
144
(channel != GIMP_HISTOGRAM_RGB && channel >= histogram->n_channels))
185
147
if (channel == GIMP_HISTOGRAM_RGB)
186
148
for (x = 0; x < 256; x++)
188
max = MAX (max, histogram->values[GIMP_HISTOGRAM_RED][x]);
189
max = MAX (max, histogram->values[GIMP_HISTOGRAM_GREEN][x]);
190
max = MAX (max, histogram->values[GIMP_HISTOGRAM_BLUE][x]);
150
max = MAX (max, HISTOGRAM_VALUE (GIMP_HISTOGRAM_RED, x));
151
max = MAX (max, HISTOGRAM_VALUE (GIMP_HISTOGRAM_GREEN, x));
152
max = MAX (max, HISTOGRAM_VALUE (GIMP_HISTOGRAM_BLUE, x));
193
155
for (x = 0; x < 256; x++)
194
if (histogram->values[channel][x] > max)
195
max = histogram->values[channel][x];
157
max = MAX (max, HISTOGRAM_VALUE (channel, x));
201
164
gimp_histogram_get_value (GimpHistogram *histogram,
202
GimpHistogramChannel channel,
165
GimpHistogramChannel channel,
205
168
g_return_val_if_fail (histogram != NULL, 0.0);
217
180
if (channel == GIMP_HISTOGRAM_RGB)
219
gdouble min = histogram->values[GIMP_HISTOGRAM_RED][bin];
221
min = MIN (min, histogram->values[GIMP_HISTOGRAM_GREEN][bin]);
223
return MIN (min, histogram->values[GIMP_HISTOGRAM_BLUE][bin]);
182
gdouble min = HISTOGRAM_VALUE (GIMP_HISTOGRAM_RED, bin);
184
min = MIN (min, HISTOGRAM_VALUE (GIMP_HISTOGRAM_GREEN, bin));
186
return MIN (min, HISTOGRAM_VALUE (GIMP_HISTOGRAM_BLUE, bin));
226
return histogram->values[channel][bin];
190
return HISTOGRAM_VALUE (channel, bin);
230
195
gimp_histogram_get_channel (GimpHistogram *histogram,
231
GimpHistogramChannel channel,
196
GimpHistogramChannel channel,
234
199
g_return_val_if_fail (histogram != NULL, 0.0);
236
201
if (histogram->n_channels > 3)
237
return gimp_histogram_get_value (histogram, channel + 1, bin);
239
return gimp_histogram_get_value (histogram, channel, bin);
204
return gimp_histogram_get_value (histogram, channel, bin);
265
230
if (channel == GIMP_HISTOGRAM_RGB)
266
231
return (gimp_histogram_get_count (histogram,
267
232
GIMP_HISTOGRAM_RED, start, end) +
268
gimp_histogram_get_count (histogram,
233
gimp_histogram_get_count (histogram,
269
234
GIMP_HISTOGRAM_GREEN, start, end) +
270
gimp_histogram_get_count (histogram,
235
gimp_histogram_get_count (histogram,
271
236
GIMP_HISTOGRAM_BLUE, start, end));
273
if (! histogram->values ||
238
if (! histogram->values[0] ||
275
240
channel >= histogram->n_channels)
312
277
if (channel == GIMP_HISTOGRAM_RGB)
314
279
for (i = start; i <= end; i++)
315
mean += (i * histogram->values[GIMP_HISTOGRAM_RED][i] +
316
i * histogram->values[GIMP_HISTOGRAM_GREEN][i] +
317
i * histogram->values[GIMP_HISTOGRAM_BLUE][i]);
280
mean += (i * HISTOGRAM_VALUE (GIMP_HISTOGRAM_RED, i) +
281
i * HISTOGRAM_VALUE (GIMP_HISTOGRAM_GREEN, i) +
282
i * HISTOGRAM_VALUE (GIMP_HISTOGRAM_BLUE, i));
321
286
for (i = start; i <= end; i++)
322
mean += i * histogram->values[channel][i];
287
mean += i * HISTOGRAM_VALUE (channel, i);
325
290
count = gimp_histogram_get_count (histogram, channel, start, end);
360
325
if (channel == GIMP_HISTOGRAM_RGB)
361
326
for (i = start; i <= end; i++)
363
sum += (histogram->values[GIMP_HISTOGRAM_RED][i] +
364
histogram->values[GIMP_HISTOGRAM_GREEN][i] +
365
histogram->values[GIMP_HISTOGRAM_BLUE][i]);
328
sum += (HISTOGRAM_VALUE (GIMP_HISTOGRAM_RED, i) +
329
HISTOGRAM_VALUE (GIMP_HISTOGRAM_GREEN, i) +
330
HISTOGRAM_VALUE (GIMP_HISTOGRAM_BLUE, i));
371
336
for (i = start; i <= end; i++)
373
sum += histogram->values[channel][i];
338
sum += HISTOGRAM_VALUE (channel, i);
348
* adapted from GNU ocrad 0.14 : page_image_io.cc : otsu_th
350
* N. Otsu, "A threshold selection method from gray-level histograms,"
351
* IEEE Trans. Systems, Man, and Cybernetics, vol. 9, no. 1, pp. 62-66, 1979.
354
gimp_histogram_get_threshold (GimpHistogram *histogram,
355
GimpHistogramChannel channel,
361
gdouble *hist = NULL;
362
gdouble *chist = NULL;
363
gdouble *cmom = NULL;
364
gdouble hist_max = 0.0;
365
gdouble chist_max = 0.0;
366
gdouble cmom_max = 0.0;
367
gdouble bvar_max = 0.0;
368
gint threshold = 127;
370
g_return_val_if_fail (histogram != NULL, -1);
372
/* the gray alpha channel is in slot 1 */
373
if (histogram->n_channels == 3 && channel == GIMP_HISTOGRAM_ALPHA)
376
if (! histogram->values[0] ||
378
(channel == GIMP_HISTOGRAM_RGB && histogram->n_channels < 4) ||
379
(channel != GIMP_HISTOGRAM_RGB && channel >= histogram->n_channels))
382
start = CLAMP (start, 0, 255);
383
end = CLAMP (end, 0, 255);
385
maxval = end - start;
387
hist = g_newa (gdouble, maxval + 1);
388
chist = g_newa (gdouble, maxval + 1);
389
cmom = g_newa (gdouble, maxval + 1);
391
if (channel == GIMP_HISTOGRAM_RGB)
393
for (i = start; i <= end; i++)
394
hist[i - start] = (HISTOGRAM_VALUE (GIMP_HISTOGRAM_RED, i) +
395
HISTOGRAM_VALUE (GIMP_HISTOGRAM_GREEN, i) +
396
HISTOGRAM_VALUE (GIMP_HISTOGRAM_BLUE, i));
400
for (i = start; i <= end; i++)
401
hist[i - start] = HISTOGRAM_VALUE (channel, i);
408
for (i = 1; i <= maxval; i++)
410
if (hist[i] > hist_max)
413
chist[i] = chist[i-1] + hist[i];
414
cmom[i] = cmom[i-1] + i * hist[i];
417
chist_max = chist[maxval];
418
cmom_max = cmom[maxval];
421
for (i = 0; i < maxval; ++i)
422
if (chist[i] > 0 && chist[i] < chist_max)
426
bvar = (gdouble) cmom[i] / chist[i];
427
bvar -= (cmom_max - cmom[i]) / (chist_max - chist[i]);
430
bvar *= chist_max - chist[i];
435
threshold = start + i;
383
443
gimp_histogram_get_std_dev (GimpHistogram *histogram,
384
GimpHistogramChannel channel,
444
GimpHistogramChannel channel,
389
449
gdouble dev = 0.0;
422
481
gimp_histogram_alloc_values (GimpHistogram *histogram,
427
484
if (bytes + 1 != histogram->n_channels)
429
486
gimp_histogram_free_values (histogram);
431
488
histogram->n_channels = bytes + 1;
432
histogram->values = g_new0 (gdouble *, histogram->n_channels);
434
for (i = 0; i < histogram->n_channels; i++)
435
histogram->values[i] = g_new (gdouble, 256);
490
histogram->values[0] = g_new (gdouble, histogram->n_channels * 256);
444
if (histogram->values)
446
for (i = 0; i < histogram->n_channels; i++)
499
for (i = 0; i < NUM_SLOTS; i++)
500
if (histogram->values[i])
447
502
g_free (histogram->values[i]);
449
g_free (histogram->values);
451
histogram->values = NULL;
452
histogram->n_channels = 0;
503
histogram->values[i] = NULL;
506
histogram->n_channels = 0;
457
510
gimp_histogram_calculate_sub_region (GimpHistogram *histogram,
461
514
const guchar *src, *msrc;
462
515
const guchar *m, *s;
469
522
/* find an unused temporary slot to put our results in and lock it */
470
pthread_mutex_lock (&histogram->mutex);
523
g_static_mutex_lock (&histogram->mutex);
472
while (histogram->tmp_slots[slot])
525
while (histogram->slots[slot])
475
values = histogram->tmp_values[slot];
476
histogram->tmp_slots[slot] = 1;
478
pthread_mutex_unlock (&histogram->mutex);
528
values = histogram->values[slot];
529
histogram->slots[slot] = 1;
531
g_static_mutex_unlock (&histogram->mutex);
535
histogram->values[slot] = g_new0 (gdouble, histogram->n_channels * 256);
536
values = histogram->values[slot];
481
values = histogram->values;
540
values = histogram->values[0];
543
#define VALUE(c,i) (values[(c) * 256 + (i)])
491
552
src = region->data;
492
553
msrc = mask->data;
500
switch (region->bytes)
505
masked = m[0] / 255.0;
506
values[0][s[0]] += masked;
515
masked = m[0] / 255.0;
516
values[0][s[0]] += masked;
517
values[1][s[1]] += masked;
523
case 3: /* calculate separate value values */
526
masked = m[0] / 255.0;
527
values[1][s[0]] += masked;
528
values[2][s[1]] += masked;
529
values[3][s[2]] += masked;
530
max = (s[0] > s[1]) ? s[0] : s[1];
533
values[0][s[2]] += masked;
535
values[0][max] += masked;
542
case 4: /* calculate separate value values */
545
masked = m[0] / 255.0;
546
values[1][s[0]] += masked;
547
values[2][s[1]] += masked;
548
values[3][s[2]] += masked;
549
values[4][s[3]] += masked;
550
max = (s[0] > s[1]) ? s[0] : s[1];
553
values[0][s[2]] += masked;
555
values[0][max] += masked;
563
src += region->rowstride;
564
msrc += mask->rowstride;
555
switch (region->bytes)
566
masked = m[0] / 255.0;
568
VALUE (0, s[0]) += masked;
574
src += region->rowstride;
575
msrc += mask->rowstride;
588
masked = m[0] / 255.0;
590
VALUE (0, s[0]) += masked;
591
VALUE (1, s[1]) += masked;
597
src += region->rowstride;
598
msrc += mask->rowstride;
602
case 3: /* calculate separate value values */
611
masked = m[0] / 255.0;
613
VALUE (1, s[0]) += masked;
614
VALUE (2, s[1]) += masked;
615
VALUE (3, s[2]) += masked;
617
max = (s[0] > s[1]) ? s[0] : s[1];
620
VALUE (0, s[2]) += masked;
622
VALUE (0, max) += masked;
628
src += region->rowstride;
629
msrc += mask->rowstride;
633
case 4: /* calculate separate value values */
642
masked = m[0] / 255.0;
644
VALUE (1, s[0]) += masked;
645
VALUE (2, s[1]) += masked;
646
VALUE (3, s[2]) += masked;
647
VALUE (4, s[3]) += masked;
649
max = (s[0] > s[1]) ? s[0] : s[1];
652
VALUE (0, s[2]) += masked;
654
VALUE (0, max) += masked;
660
src += region->rowstride;
661
msrc += mask->rowstride;
567
666
else /* no mask */
569
668
src = region->data;
576
switch(region->bytes)
581
values[0][s[0]] += 1.0;
589
values[0][s[0]] += 1.0;
590
values[1][s[1]] += 1.0;
595
case 3: /* calculate separate value values */
598
values[1][s[0]] += 1.0;
599
values[2][s[1]] += 1.0;
600
values[3][s[2]] += 1.0;
601
max = (s[0] > s[1]) ? s[0] : s[1];
604
values[0][s[2]] += 1.0;
606
values[0][max] += 1.0;
612
case 4: /* calculate separate value values */
615
values[1][s[0]] += 1.0;
616
values[2][s[1]] += 1.0;
617
values[3][s[2]] += 1.0;
618
values[4][s[3]] += 1.0;
619
max = (s[0] > s[1]) ? s[0] : s[1];
622
values[0][s[2]] += 1.0;
624
values[0][max] += 1.0;
631
src += region->rowstride;
670
switch (region->bytes)
680
VALUE (0, s[0]) += 1.0;
685
src += region->rowstride;
697
VALUE (0, s[0]) += 1.0;
698
VALUE (1, s[1]) += 1.0;
703
src += region->rowstride;
707
case 3: /* calculate separate value values */
715
VALUE (1, s[0]) += 1.0;
716
VALUE (2, s[1]) += 1.0;
717
VALUE (3, s[2]) += 1.0;
719
max = (s[0] > s[1]) ? s[0] : s[1];
722
VALUE (0, s[2]) += 1.0;
724
VALUE (0, max) += 1.0;
729
src += region->rowstride;
733
case 4: /* calculate separate value values */
741
VALUE (1, s[0]) += 1.0;
742
VALUE (2, s[1]) += 1.0;
743
VALUE (3, s[2]) += 1.0;
744
VALUE (4, s[3]) += 1.0;
746
max = (s[0] > s[1]) ? s[0] : s[1];
749
VALUE (0, s[2]) += 1.0;
751
VALUE (0, max) += 1.0;
756
src += region->rowstride;
636
763
/* unlock this slot */
637
/* we shouldn't have to use mutex locks here */
638
pthread_mutex_lock (&histogram->mutex);
639
histogram->tmp_slots[slot] = 0;
640
pthread_mutex_unlock (&histogram->mutex);
764
g_static_mutex_lock (&histogram->mutex);
766
histogram->slots[slot] = 0;
768
g_static_mutex_unlock (&histogram->mutex);