Commit 6ae2a9bc authored by jaycox's avatar jaycox
Browse files

added gimphistogram*, histogramwidget*, removed histogram.[ch]


	* app/Makefile.am: added gimphistogram*, histogramwidget*,
	removed histogram.[ch]

	* app/histogram.[ch]: removed.  replaced with histogramwidget.[ch].

	* app/{gimphistogramP.h, gimphistogram.h, gimphistogram.c}: new
 	functions that calculate histograms in parallel and perform
 	calculations on them.

	* app/histogramwidget.[ch]: Same as old histogram.[ch], only it is
 	now a real widget, and it uses GimpHistograms instead of arrays of
 	values.

	* app/curves.c: #include gimphistogram.h instead of histogram.h.

	* app/equalize.c: use GimpHistogram and GimpLut.

	* app/gimpbrush.c, app/gimpimage.c, app/gimpset.c: use
	GTK_RUN_FIRST in calls to gimp_signal_new.

	* app/histogram_tool.c, app/levels.c, app/threshold.c: modified to
 	use the new HistogramWidget.

	* app/paint_funcs.c: removed some unused variables.

	* app/preferences_dialog.c: only display the num-processor field
 	if we are configured --with-mp

	* plug-ins/gee/gee.c: removed a couple of c++ style comments.
parent 2e0e6892
......@@ -194,6 +194,8 @@ gimp_SOURCES = \
gimpprogress.h \
gimprc.c \
gimprc.h \
gimphistogram.c \
gimphistogram.h \
global_edit.c \
global_edit.h \
gradient.c \
......@@ -202,8 +204,8 @@ gimp_SOURCES = \
gradient.h \
gximage.c \
gximage.h \
histogram.c \
histogram.h \
histogramwidget.c \
histogramwidget.h \
histogram_tool.c \
histogram_tool.h \
hue_saturation.c \
......
......@@ -26,7 +26,7 @@
#include "drawable.h"
#include "general.h"
#include "gdisplay.h"
#include "histogram.h"
#include "gimphistogram.h"
#include "image_map.h"
#include "interface.h"
#include "curves.h"
......
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* gimphistogram module Copyright (C) 1999 Jay Cox <jaycox@earthlink.net>
*
* 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 "gimphistogramP.h"
#include "gimphistogram.h"
#include "pixel_region.h"
#include "gimpdrawable.h"
#include "channel.h"
#include "gimpimage.h"
#include "gimprc.h"
#include <glib.h>
#include <math.h>
#ifdef ENABLE_MP
#include <pthread.h>
#endif /* ENABLE_MP */
GimpHistogram *
gimp_histogram_new()
{
GimpHistogram *histogram;
histogram = g_new(GimpHistogram, 1);
histogram->values = NULL;
histogram->nchannels = 0;
return histogram;
}
void
gimp_histogram_free (GimpHistogram *histogram)
{
int i;
if (histogram->values)
{
for (i = 0; i < histogram->nchannels; i++)
g_free(histogram->values[i]);
g_free(histogram->values);
}
g_free(histogram);
}
void
gimp_histogram_calculate_sub_region(GimpHistogram *histogram,
PixelRegion *region, PixelRegion *mask)
{
const unsigned char *src, *msrc;
const unsigned char *m, *s;
double **values;
int h, w, max;
#ifdef ENABLE_MP
int slot = 0;
/* find an unused temporary slot to put our results in and lock it */
pthread_mutex_lock(&histogram->mutex);
{
while (histogram->tmp_slots[slot])
slot++;
values = histogram->tmp_values[slot];
histogram->tmp_slots[slot] = 1;
}
pthread_mutex_unlock(&histogram->mutex);
#else /* !ENABLE_MP */
values = histogram->values;
#endif
h = region->h;
w = region->w;
if (mask)
{
double masked;
src = region->data;
msrc = region->data;
while (h--)
{
s = src;
m = msrc;
w = region->w;
switch(region->bytes)
{
case 1:
while (w--)
{
masked = m[0] / 255.0;
values[0][s[0]] += masked;
s += 1;
m += 1;
} break;
case 2:
while (w--)
{
masked = m[0] / 255.0;
values[0][s[0]] += masked;
values[1][s[1]] += masked;
s += 2;
m += 1;
} break;
case 3: /* calculate seperate value values */
while (w--)
{
masked = m[0] / 255.0;
values[1][s[0]] += masked;
values[2][s[1]] += masked;
values[3][s[2]] += masked;
max = (s[0] > s[1]) ? s[0] : s[1];
if (s[2] > max)
values[0][s[2]] += masked;
else
values[0][max] += masked;
s += 3;
m += 1;
} break;
case 4: /* calculate seperate value values */
while (w--)
{
masked = m[0] / 255.0;
values[1][s[0]] += masked;
values[2][s[1]] += masked;
values[3][s[2]] += masked;
values[4][s[3]] += masked;
max = (s[0] > s[1]) ? s[0] : s[1];
if (s[2] > max)
values[0][s[2]] += masked;
else
values[0][max] += masked;
s += 3;
m += 1;
} break;
}
src += region->rowstride;
msrc += mask->rowstride;
}
}
else /* no mask */
{
src = region->data;
while (h--)
{
s = src;
w = region->w;
switch(region->bytes)
{
case 1:
while (w--)
{
values[0][s[0]] += 1.0;
s += 1;
} break;
case 2:
while (w--)
{
values[0][s[0]] += 1.0;
values[1][s[1]] += 1.0;
s += 2;
} break;
case 3: /* calculate seperate value values */
while (w--)
{
values[1][s[0]] += 1.0;
values[2][s[1]] += 1.0;
values[3][s[2]] += 1.0;
max = (s[0] > s[1]) ? s[0] : s[1];
if (s[2] > max)
values[0][s[2]] += 1.0;
else
values[0][max] += 1.0;
s += 3;
} break;
case 4: /* calculate seperate value values */
while (w--)
{
values[1][s[0]] += 1.0;
values[2][s[1]] += 1.0;
values[3][s[2]] += 1.0;
values[4][s[3]] += 1.0;
max = (s[0] > s[1]) ? s[0] : s[1];
if (s[2] > max)
values[0][s[2]] += 1.0;
else
values[0][max] += 1.0;
s += 3;
} break;
}
src += region->rowstride;
}
}
#ifdef ENABLE_MP
/* unlock this slot */
/* we shouldn't have to use mutex locks here */
pthread_mutex_lock(&histogram->mutex);
histogram->tmp_slots[slot] = 0;
pthread_mutex_unlock(&histogram->mutex);
#endif
}
static void gimp_histogram_alloc(GimpHistogram *histogram, int bytes)
{
int i;
if (bytes + 1 != histogram->nchannels)
{
if (histogram->values)
{
for (i = 0; i < histogram->nchannels; i++)
g_free(histogram->values[i]);
g_free(histogram->values);
}
histogram->nchannels = bytes + 1;
histogram->values = g_new(double *, histogram->nchannels);
for (i = 0; i < histogram->nchannels; i++)
histogram->values[i] = g_new(double, 256);
}
}
void
gimp_histogram_calculate (GimpHistogram *histogram, PixelRegion *region,
PixelRegion *mask)
{
int i, j, k;
gimp_histogram_alloc(histogram, region->bytes);
#ifdef ENABLE_MP
pthread_mutex_init(&histogram->mutex, NULL);
histogram->tmp_slots = g_new(char, num_processors);
histogram->tmp_values = g_new(double **, num_processors);
for (i = 0; i < num_processors; i++)
{
histogram->tmp_values[i] = g_new(double *, histogram->nchannels);
histogram->tmp_slots[i] = 0;
for (j = 0; j < histogram->nchannels; j++)
{
histogram->tmp_values[i][j] = g_new(double, 256);
for (k = 0; k < 256; k++)
histogram->tmp_values[i][j][k] = 0.0;
}
}
#endif
for (i = 0; i < histogram->nchannels; i++)
for (j = 0; j < 256; j++)
histogram->values[i][j] = 0.0;
pixel_regions_process_parallel((p_func)gimp_histogram_calculate_sub_region,
histogram, 2, region, mask);
#ifdef ENABLE_MP
/* add up all the tmp buffers and free their memmory */
for (i = 0; i < num_processors; i++)
{
for (j = 0; j < histogram->nchannels; j++)
{
for (k = 0; k < 256; k++)
histogram->values[j][k] += histogram->tmp_values[i][j][k];
g_free (histogram->tmp_values[i][j]);
}
g_free(histogram->tmp_values[i]);
}
g_free(histogram->tmp_values);
g_free(histogram->tmp_slots);
#endif
}
void
gimp_histogram_calculate_drawable(GimpHistogram *histogram,
GimpDrawable *drawable)
{
PixelRegion region;
PixelRegion mask;
int x1, y1, x2, y2;
int off_x, off_y;
int no_mask;
no_mask = (drawable_mask_bounds (drawable, &x1, &y1, &x2, &y2) == FALSE);
pixel_region_init (&region, gimp_drawable_data (drawable), x1, y1,
(x2 - x1), (y2 - y1), FALSE);
if (!no_mask)
{
Channel *sel_mask;
GimpImage *gimage;
gimage = gimp_drawable_gimage(drawable);
sel_mask = gimp_image_get_mask (gimage);
drawable_offsets (drawable, &off_x, &off_y);
pixel_region_init (&mask, gimp_drawable_data (GIMP_DRAWABLE (sel_mask)),
x1 + off_x, y1 + off_y, (x2 - x1), (y2 - y1), FALSE);
gimp_histogram_calculate (histogram, &region, &mask);
}
else
gimp_histogram_calculate (histogram, &region, NULL);
}
double
gimp_histogram_get_maximum (GimpHistogram *histogram, int channel)
{
double max = 0.0;
int x;
for (x = 0; x < 256; x++)
if (histogram->values[channel][x] > max)
max = histogram->values[channel][x];
return max;
}
double
gimp_histogram_get_value (GimpHistogram *histogram, int channel, int bin)
{
if (channel < histogram->nchannels && bin >= 0 && bin < 256)
return histogram->values[channel][bin];
return 0.0;
}
double
gimp_histogram_get_channel (GimpHistogram *histogram, int channel, int bin)
{
if (histogram->nchannels > 3)
return gimp_histogram_get_value(histogram, channel + 1, bin);
else
return gimp_histogram_get_value(histogram, channel , bin);
}
int
gimp_histogram_nchannels (GimpHistogram *histogram)
{
return histogram->nchannels - 1;
}
double
gimp_histogram_get_count (GimpHistogram *histogram, int start, int end)
{
int i;
double count = 0.0;
for (i = start; i <= end; i++)
count += histogram->values[0][i];
return count;
}
double
gimp_histogram_get_mean (GimpHistogram *histogram, int channel,
int start, int end)
{
int i;
double mean = 0.0;
double count;
for (i = start; i <= end; i++)
mean += i * histogram->values[channel][i];
count = gimp_histogram_get_count(histogram, start, end);
if (count > 0.0)
return mean / count;
return mean;
}
int
gimp_histogram_get_median (GimpHistogram *histogram, int channel, int start, int end)
{
int i;
double sum = 0.0;
double count;
count = gimp_histogram_get_count(histogram, start, end);
for (i = start; i <= end; i++)
{
sum += i * histogram->values[channel][i];
if (sum * 2 > count)
return i;
}
return -1;
}
double
gimp_histogram_get_std_dev (GimpHistogram *histogram, int channel,
int start, int end)
{
int i;
double dev = 0.0;
double count;
double mean;
mean = gimp_histogram_get_mean(histogram, channel, start, end);
count = gimp_histogram_get_count(histogram, start, end);
if (count == 0.0)
count = 1.0;
for (i = start; i <= end; i++)
dev += gimp_histogram_get_value(histogram, channel, i) *
(i - mean) * (i - mean);
return sqrt (dev / count);
}
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* gimphistogram module Copyright (C) 1999 Jay Cox <jaycox@earthlink.net>
*
* 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.
*/
#ifndef __GIMP_HISTOGRAM_H__
#define __GIMP_HISTOGRAM_H__
#include "pixel_region.h"
#include "gimpdrawable.h"
#define HISTOGRAM_VALUE 0
#define HISTOGRAM_RED 1
#define HISTOGRAM_GREEN 2
#define HISTOGRAM_BLUE 3
#define HISTOGRAM_ALPHA 4
typedef struct _GimpHistogram GimpHistogram;
GimpHistogram *gimp_histogram_new ();
void gimp_histogram_free (GimpHistogram *);
void gimp_histogram_calculate (GimpHistogram *,
PixelRegion *region,
PixelRegion *mask);
void gimp_histogram_calculate_drawable (GimpHistogram *,
GimpDrawable *);
double gimp_histogram_get_maximum (GimpHistogram *, int channel);
double gimp_histogram_get_count (GimpHistogram *,
int start, int end);
double gimp_histogram_get_mean (GimpHistogram *, int chan,
int start, int end);
int gimp_histogram_get_median (GimpHistogram *, int chan,
int start, int end);
double gimp_histogram_get_std_dev (GimpHistogram *, int chan,
int start, int end);
double gimp_histogram_get_value (GimpHistogram *, int channel,
int bin);
double gimp_histogram_get_channel (GimpHistogram *, int color,
int bin);
int gimp_histogram_nchannels (GimpHistogram *);
#endif /* __GIMP_HISTOGRAM_H__ */
......@@ -23,7 +23,7 @@
#include "drawable.h"
#include "general.h"
#include "gdisplay.h"
#include "histogram.h"
#include "histogramwidget.h"
#include "image_map.h"
#include "interface.h"
#include "threshold.h"
......@@ -48,7 +48,8 @@ struct _ThresholdDialog
GtkWidget *shell;
GtkWidget *low_threshold_text;
GtkWidget *high_threshold_text;
Histogram *histogram;
HistogramWidget *histogram;
GimpHistogram *hist;
GimpDrawable *drawable;
ImageMap image_map;
......@@ -81,12 +82,22 @@ static void *threshold_options = NULL;
static ThresholdDialog *threshold_dialog = NULL;
static void threshold (PixelRegion *, PixelRegion *, void *);
static void threshold_histogram_info (PixelRegion *, PixelRegion *, HistogramValues, void *);
static void threshold_histogram_range (int, int, HistogramValues, void *);
static void threshold_histogram_range (HistogramWidget *, int, int,
void*);
static Argument * threshold_invoker (Argument *);
/* threshold machinery */
static void
threshold_2 (void *user_data,
PixelRegion *srcPR,
PixelRegion *destPR)
{
/* this function just re-orders the arguments so we can use
pixel_regions_process_paralell */
threshold(srcPR, destPR, user_data);
}
static void
threshold (PixelRegion *srcPR,
PixelRegion *destPR,
......@@ -140,68 +151,9 @@ threshold (PixelRegion *srcPR,
}
static void
threshold_histogram_info (PixelRegion *srcPR,
PixelRegion *maskPR,
HistogramValues values,
void *user_data)
{
ThresholdDialog *td;
unsigned char *src, *s;
unsigned char *mask, *m;
int w, h;
int value;
mask = NULL;
m = NULL;
td = (ThresholdDialog *) user_data;
h = srcPR->h;
src = srcPR->data;
if (maskPR)
mask = maskPR->data;
while (h--)
{
w = srcPR->w;
s = src;
if (maskPR)
m = mask;
while (w--)
{
if (td->color)
{
value = MAX (s[RED_PIX], s[GREEN_PIX]);
value = MAX (value, s[BLUE_PIX]);
}
else
value = s[GRAY_PIX];
if (maskPR)
values[HISTOGRAM_VALUE][value] += (double) *m / 255.0;
else
values[HISTOGRAM_VALUE][value] += 1.0;
s += srcPR->bytes;
if (maskPR)
m += maskPR->bytes;
}
src += srcPR->rowstride;
if (maskPR)
mask += maskPR->rowstride;
}
}
static void
threshold_histogram_range (int start,
threshold_histogram_range (HistogramWidget *w,
int start,