gimp/app/base/gimphistogram.c

527 lines
11 KiB
C
Raw Normal View History

/* 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"
#ifdef ENABLE_MP
#include <pthread.h>
#endif
removed from CVS, they are generated. 2001-12-07 Sven Neumann <sven@gimp.org> * app/core/gimpmarshal.[ch]: removed from CVS, they are generated. * app/base/Makefile.am * app/base/base-enums.h: new file defining enums that are to be registered. Used to build app/base/base-enums.c. * app/base/base-types.h: include base-enums.h. * tools/pdbgen/Makefile.am * tools/pdbgen/enumcode.pl * tools/pdbgen/enums.pl: parse the new base-enums.h file and modified the perl voodoo so it doesn't prefix enums with GIMP_ that are already properly namespaced. * app/core/core-types.h: don't need to chop GIMP from enum. * app/pdb/color_cmds.c * app/pdb/tools_cmds.c * libgimp/gimpenums.h * plug-ins/script-fu/script-fu-constants.c: regenerated. * app/config/gimpconfig-deserialize.[ch] * app/config/gimpconfig-serialize.[ch] * app/config/gimpconfig.[ch]: made GimpConfig an interface including a reasonable default implementation that works on object properties. * app/config/Makefile.am * app/config/gimpbaseconfig.[ch]: new GimpBaseConfig using the GimpConfig interface. Yet only used for testing from app/main.c. * app/main.c: test the new GimpBaseConfig object. * app/gimprc.c * app/base/base-config.h * app/base/*.c * app/core/gimpdatafiles.c * app/core/gimpdrawable-transform.c * app/core/gimppreviewcache.c * app/gui/preferences-dialog.c * app/paint-funcs/paint-funcs.c * app/xcf/xcf-seek.c: need to include glib-object.h since base-config contains registered enums now. Follow name change of InterpolationType to GimpInterpolationType.
2001-12-07 16:10:53 +00:00
#include <glib-object.h>
#include "libgimpmath/gimpmath.h"
#include "base-types.h"
Finally landed the new GimpConfig based gimprc parser. It's not finished 2002-11-18 Sven Neumann <sven@gimp.org> Finally landed the new GimpConfig based gimprc parser. It's not finished yet but we need to start somewhere. This release removes the old gimprc.[ch] files. The gimprc format changes slightly, but the changes are minimal. The Preferences dialog is temporarily disabled since it still needs to be ported. If you are are afraid, stay away from CVS for a few days ;-) * app/Makefile.am * app/gimprc.[ch]: removed the old gimprc system. * app/base/Makefile.am * app/base/base-config.[ch]: removed these files in favor of config/gimpbaseconfig.[ch]. * app/core/Makefile.am * app/core/gimpcoreconfig.[ch]: removed these files in favor of config/gimpcoreconfig.[ch]. * app/config/Makefile.am * app/config/config-types.h: moved typedefs into this new file. * app/config/gimpbaseconfig.[ch] * app/config/gimpcoreconfig.[ch] * app/config/gimpdisplayconfig.[ch] * app/config/gimpguiconfig.[ch] * app/config/gimprc.[ch] * app/config/test-config.c: brought into shape for real use. * app/base/base-types.h: include config/config-types.h here. Added a global GimpBaseConfig *base_config variable to ease migration. * app/gui/Makefile.am: temporarily disabled the preferences dialog. * app/app_procs.c * app/undo.c * app/undo_history.c * app/base/base.[ch] * app/base/gimphistogram.c * app/base/pixel-processor.c * app/base/temp-buf.c * app/base/tile-cache.c * app/core/core-types.h * app/core/gimp-documents.c * app/core/gimp.c * app/core/gimpbrush.c * app/core/gimpbrushgenerated.c * app/core/gimpcontext.c * app/core/gimpdrawable-transform.c * app/core/gimpimage-new.c * app/core/gimpimage.c * app/core/gimpimagefile.c * app/core/gimpmodules.c * app/core/gimppattern.c * app/display/Makefile.am * app/display/gimpdisplay-handlers.c * app/display/gimpdisplay.[ch] * app/display/gimpdisplayshell-callbacks.c * app/display/gimpdisplayshell-handlers.c * app/display/gimpdisplayshell-layer-select.c * app/display/gimpdisplayshell-render.c * app/display/gimpdisplayshell-scale.c * app/display/gimpdisplayshell-scroll.c * app/display/gimpdisplayshell-selection.c * app/display/gimpdisplayshell.[ch] * app/display/gimpnavigationview.c * app/file/file-save.c * app/gui/device-status-dialog.c * app/gui/dialogs-constructors.c * app/gui/file-commands.c * app/gui/file-new-dialog.c * app/gui/file-open-dialog.c * app/gui/file-save-dialog.c * app/gui/gui.c * app/gui/menus.c * app/gui/paths-dialog.c * app/gui/resize-dialog.c * app/gui/session.c * app/gui/test-commands.c * app/gui/tips-dialog.c * app/gui/tips-dialog.h * app/gui/user-install-dialog.c * app/gui/view-commands.c * app/paint/gimppaintcore.c * app/plug-in/plug-in.c * app/plug-in/plug-ins.c * app/tools/gimpbezierselecttool.c * app/tools/gimpbucketfilltool.c * app/tools/gimpcolorpickertool.c * app/tools/gimpcroptool.c * app/tools/gimpeditselectiontool.c * app/tools/gimpfuzzyselecttool.c * app/tools/gimpinktool.c * app/tools/gimpmagnifytool.c * app/tools/gimpmeasuretool.c * app/tools/gimppainttool.c * app/tools/gimppathtool.c * app/tools/gimptexttool.[ch] * app/tools/selection_options.c * app/tools/tools.c * app/tools/transform_options.c * app/widgets/gimphelp.c * app/widgets/gimpitemfactory.c * app/widgets/gimpselectioneditor.c * app/xcf/xcf-load.c * tools/pdbgen/pdb/fileops.pdb * tools/pdbgen/pdb/gimprc.pdb * tools/pdbgen/pdb/image.pdb * tools/pdbgen/pdb/layer.pdb * tools/pdbgen/pdb/transform_tools.pdb: use the new config system instead of the old gimprc stuff. * etc/gimprc.in * etc/gimprc_user.in: adapted to the new gimprc format. Will update the man-page later... * app/pdb/fileops_cmds.c * app/pdb/gimprc_cmds.c * app/pdb/image_cmds.c * app/pdb/layer_cmds.c * app/pdb/transform_tools_cmds.c * libgimp/gimpgimprc_pdb.c: regenerated.
2002-11-18 20:50:31 +00:00
#include "config/gimpbaseconfig.h"
#include "gimphistogram.h"
#include "pixel-processor.h"
#include "pixel-region.h"
app/Makefile.am app/channel_pvt.h app/drawable_pvt.h app/gdisplayF.h 2000-12-29 Michael Natterer <mitch@gimp.org> * app/Makefile.am * app/channel_pvt.h * app/drawable_pvt.h * app/gdisplayF.h * app/gimpdrawableP.h * app/gimpimageP.h * app/layer_pvt.h * app/toolsF.h: removed these files. * app/apptypes.h * tools/pdbgen/enums.pl: added tons of opaque typedefs and enums. * tools/pdbgen/pdb/brush_select.pdb * tools/pdbgen/pdb/brushes.pdb * tools/pdbgen/pdb/channel.pdb * tools/pdbgen/pdb/color.pdb * tools/pdbgen/pdb/convert.pdb * tools/pdbgen/pdb/display.pdb * tools/pdbgen/pdb/drawable.pdb * tools/pdbgen/pdb/fileops.pdb * tools/pdbgen/pdb/gradient_select.pdb * tools/pdbgen/pdb/gradients.pdb * tools/pdbgen/pdb/help.pdb * tools/pdbgen/pdb/image.pdb * tools/pdbgen/pdb/layer.pdb * tools/pdbgen/pdb/pattern_select.pdb * tools/pdbgen/pdb/patterns.pdb * tools/pdbgen/pdb/selection.pdb * tools/pdbgen/pdb/tools.pdb * app/*: chainsaw #include cleanup: - Never (never!!) include stuff in header files except where we need access to structures' contents (like derived objects). - Added prototypes and proper formating in many files. - The #include order in *all* *.c files is as follows: #include "config.h" #include <system stuff> #include <gtk/gtk.h> #include "apptypes.h" #include "gimp stuff" #include "libgimp stuff" #include "libgimp/gimpintl.h" By following this scheme we can easily see a file's dependencies from it's #include's and can grep for the inclusion to find out where a file is used. * tools/pdbgen/app.pl: changed to follow the include scheme above. * libgimp/Makefile.am * libgimp/gimpuitypes.h: new file, included from libgimp/gimpui.h and from app/apptypes.h. * libgimp/gimpcolorbutton.[ch] * libgimp/gimpdialog.[ch] * libgimp/gimphelpui.[ch] * libgimp/gimpparasite.[ch] * libgimp/gimppatheditor.[ch] * libgimp/gimpprotocol.c * libgimp/gimpquerybox.[ch] * libgimp/gimpsizeentry.[ch] * libgimp/gimptypes.h * libgimp/gimpui.h * libgimp/gimpunit.h * libgimp/gimpunitmenu.[ch] * libgimp/gimpwidgets.[ch]: changed accordingly. * plug-ins/FractalExplorer/Dialogs.c * plug-ins/gdyntext/message_window.c * plug-ins/imagemap/imap_default_dialog.c * plug-ins/imagemap/imap_file.c: these files used to include "libgimp/gimpui.h" without including "libgimp/gimp.h". This is no longer possible because the libgimpui headers don't inlcude "libgimp/gimpunit.h" any more.
2000-12-29 15:22:01 +00:00
struct _GimpHistogram
{
gint bins;
gdouble **values;
gint n_channels;
#ifdef ENABLE_MP
pthread_mutex_t mutex;
gint num_slots;
gdouble ***tmp_values;
gchar *tmp_slots;
#endif
};
/* 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 (GimpBaseConfig *config)
{
GimpHistogram *histogram;
g_return_val_if_fail (GIMP_IS_BASE_CONFIG (config), NULL);
histogram = g_new0 (GimpHistogram, 1);
histogram->bins = 0;
histogram->values = NULL;
histogram->n_channels = 0;
#ifdef ENABLE_MP
pthread_mutex_init (&histogram->mutex, NULL);
histogram->num_slots = config->num_processors;
histogram->tmp_slots = g_new0 (gchar, histogram->num_slots);
histogram->tmp_values = g_new0 (gdouble **, histogram->num_slots);
#endif /* ENABLE_MP */
return histogram;
}
void
gimp_histogram_free (GimpHistogram *histogram)
{
g_return_if_fail (histogram != NULL);
#ifdef ENABLE_MP
pthread_mutex_destroy (&histogram->mutex);
g_free (histogram->tmp_values);
g_free (histogram->tmp_slots);
#endif
gimp_histogram_free_values (histogram);
g_free (histogram);
}
void
gimp_histogram_calculate (GimpHistogram *histogram,
PixelRegion *region,
PixelRegion *mask)
{
gint i, j;
g_return_if_fail (histogram != NULL);
g_return_if_fail (region != NULL);
gimp_histogram_alloc_values (histogram, region->bytes);
#ifdef ENABLE_MP
for (i = 0; i < histogram->num_slots; i++)
{
histogram->tmp_values[i] = g_new0 (gdouble *, histogram->n_channels);
histogram->tmp_slots[i] = 0;
for (j = 0; j < histogram->n_channels; j++)
{
gint k;
histogram->tmp_values[i][j] = g_new0 (gdouble, 256);
for (k = 0; k < 256; k++)
histogram->tmp_values[i][j][k] = 0.0;
}
}
#endif
for (i = 0; i < histogram->n_channels; 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 < histogram->num_slots; i++)
{
for (j = 0; j < histogram->n_channels; j++)
{
gint k;
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]);
}
#endif
}
gdouble
gimp_histogram_get_maximum (GimpHistogram *histogram,
GimpHistogramChannel channel)
{
gdouble max = 0.0;
gint x;
g_return_val_if_fail (histogram != NULL, 0.0);
for (x = 0; x < 256; x++)
if (histogram->values[channel][x] > max)
max = histogram->values[channel][x];
return max;
}
gdouble
gimp_histogram_get_value (GimpHistogram *histogram,
GimpHistogramChannel channel,
gint bin)
{
g_return_val_if_fail (histogram != NULL, 0.0);
if (channel < histogram->n_channels && bin >= 0 && bin < 256)
return histogram->values[channel][bin];
return 0.0;
}
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)
return gimp_histogram_get_value (histogram, channel + 1, bin);
else
return gimp_histogram_get_value (histogram, channel, bin);
}
gint
gimp_histogram_nchannels (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);
for (i = start; i <= end; i++)
count += histogram->values[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);
for (i = start; i <= end; i++)
mean += i * histogram->values[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);
count = gimp_histogram_get_count (histogram, channel, start, end);
for (i = start; i <= end; i++)
{
sum += histogram->values[channel][i];
if (sum * 2 > count)
return i;
}
return -1;
}
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);
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) *
(i - mean) * (i - mean);
return sqrt (dev / count);
}
/* private functions */
static void
gimp_histogram_alloc_values (GimpHistogram *histogram,
gint bytes)
{
gint i;
if (bytes + 1 != histogram->n_channels)
{
gimp_histogram_free_values (histogram);
histogram->n_channels = bytes + 1;
histogram->values = g_new0 (gdouble *, histogram->n_channels);
for (i = 0; i < histogram->n_channels; i++)
histogram->values[i] = g_new (double, 256);
}
}
static void
gimp_histogram_free_values (GimpHistogram *histogram)
{
gint i;
if (histogram->values)
{
for (i = 0; i < histogram->n_channels; i++)
g_free (histogram->values[i]);
g_free (histogram->values);
histogram->values = NULL;
}
}
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 */
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
values = histogram->values;
#endif
h = region->h;
w = region->w;
if (mask)
{
gdouble masked;
src = region->data;
msrc = mask->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 += 4;
}
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
}