gimp/plug-ins/common/smooth-palette.c

539 lines
17 KiB
C
Raw Normal View History

1997-11-24 22:05:25 +00:00
/*
* smooth palette - derive smooth palette from image
* Copyright (C) 1997 Scott Draves <spot@cs.cmu.edu>
*
* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*/
1997-11-24 22:05:25 +00:00
#include "config.h"
1997-11-24 22:05:25 +00:00
#include <string.h>
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
#include "libgimp/stdplugins-intl.h"
1997-11-24 22:05:25 +00:00
#define PLUG_IN_PROC "plug-in-smooth-palette"
Renamed tons of plug-ins to make more sense and to be consistent: 2008-03-24 Michael Natterer <mitch@gimp.org> Renamed tons of plug-ins to make more sense and to be consistent: * plug-ins/common/AlienMap2.c -> alien-map.c * plug-ins/common/CEL.c -> cel.c * plug-ins/common/CML_explorer.c -> cml-explorer.c * plug-ins/common/align_layers.c -> align-layers.c * plug-ins/common/animationplay.c -> animation-play.c * plug-ins/common/animoptimize.c -> animation-optimize.c * plug-ins/common/apply_lens.c -> lens-apply.c * plug-ins/common/autocrop.c -> crop-auto.c * plug-ins/common/autostretch_hsv.c -> contrast-stretch-hsv.c * plug-ins/common/borderaverage.c -> border-average.c * plug-ins/common/bumpmap.c -> bump-map.c * plug-ins/common/c_astretch.c -> contrast-stretch.c * plug-ins/common/ccanalyze.c -> color-cube-analyze.c * plug-ins/common/channel_mixer.c -> channel-mixer.c * plug-ins/common/color_enhance.c -> color-enhance.c * plug-ins/common/colortoalpha.c -> color-to-alpha.c * plug-ins/common/convmatrix.c -> convolution-matrix.c * plug-ins/common/curve_bend.c -> curve-bend.c * plug-ins/common/depthmerge.c -> depth-merge.c * plug-ins/common/dog.c -> edge-dog.c * plug-ins/common/exchange.c -> color-exchange.c * plug-ins/common/flarefx.c -> lens-flare.c * plug-ins/common/fp.c -> filter-pack.c * plug-ins/common/fractaltrace.c -> fractal-trace.c * plug-ins/common/gauss.c -> blur-gauss.c * plug-ins/common/gee_zoom.c -> gee-zoom.c * plug-ins/common/glasstile.c -> tile-glass.c * plug-ins/common/gqbist.c -> qbist.c * plug-ins/common/gradmap.c -> gradient-map.c * plug-ins/common/laplace.c -> edge-laplace.c * plug-ins/common/lens.c -> lens-distortion.c * plug-ins/common/lic.c -> van-gogh-lic.c * plug-ins/common/max_rgb.c -> max-rgb.c * plug-ins/common/mblur.c -> blur-motion.c * plug-ins/common/nlfilt.c -> nl-filter.c * plug-ins/common/noisify.c -> noise-rgb.c * plug-ins/common/normalize.c -> contrast-normalize.c * plug-ins/common/papertile.c -> tile-paper.c * plug-ins/common/polar.c -> polar-coords.c * plug-ins/common/randomize.c -> noise-randomize.c * plug-ins/common/redeye.c -> red-eye-removal.c * plug-ins/common/retinex.c -> contrast-retinex.c * plug-ins/common/sample_colorize.c -> sample-colorize.c * plug-ins/common/scatter_hsv.c -> noise-hsv.c * plug-ins/common/sel_gauss.c -> blur-gauss-selective.c * plug-ins/common/semiflatten.c -> semi-flatten.c * plug-ins/common/smooth_palette.c -> smooth-palette.c * plug-ins/common/snoise.c -> noise-solid.c * plug-ins/common/sobel.c -> edge-sobel.c * plug-ins/common/spheredesigner.c -> sphere-designer.c * plug-ins/common/spread.c -> noise-spread.c * plug-ins/common/struc.c -> apply-canvas.c * plug-ins/common/threshold_alpha.c -> threshold-alpha.c * plug-ins/common/tileit.c -> tile-small.c * plug-ins/common/tiler.c -> tile-seamless.c * plug-ins/common/uniteditor.c -> unit-editor.c * plug-ins/common/unsharp.c -> unsharp-mask.c * plug-ins/common/vinvert.c -> value-invert.c * plug-ins/common/vpropagate.c -> value-propagate.c * plug-ins/common/webbrowser.c -> web-browser.c * plug-ins/common/whirlpinch.c -> whirl-pinch.c * plug-ins/common/zealouscrop.c -> crop-zealous.c * plug-ins/common/plugin-defs.pl: changed accordingly. * plug-ins/common/Makefile.am: regenerated. svn path=/trunk/; revision=25192
2008-03-24 15:29:55 +00:00
#define PLUG_IN_BINARY "smooth-palette"
#define PLUG_IN_ROLE "gimp-smooth-palette"
typedef struct _Palette Palette;
typedef struct _PaletteClass PaletteClass;
1997-11-24 22:05:25 +00:00
struct _Palette
{
GimpPlugIn parent_instance;
};
removed our own action_area API and use GtkDialog's one. Create all 2003-11-06 Michael Natterer <mitch@gimp.org> * libgimpwidgets/gimpdialog.[ch]: removed our own action_area API and use GtkDialog's one. Create all dialogs without separator. Changed almost everything else too. Fixes bug #125143. * libgimpwidgets/gimpquerybox.c * libgimpwidgets/gimpunitmenu.c: changed accordingly. * libgimp/gimpexport.[ch]: ditto. Renamed enum GimpExportReturnType to GimpExportReturn. * libgimp/gimpcompat.h: added a #define for the old name. * themes/Default/gtkrc: increased action_area border to 6 pixels. * app/display/gimpdisplayshell-filter-dialog.c * app/display/gimpdisplayshell-scale.c * app/display/gimpprogress.c * app/gui/brush-select.c * app/gui/channels-commands.c * app/gui/color-notebook.c * app/gui/convert-dialog.c * app/gui/file-new-dialog.c * app/gui/font-select.c * app/gui/gradient-editor-commands.c * app/gui/gradient-select.c * app/gui/grid-dialog.c * app/gui/image-commands.c * app/gui/info-window.c * app/gui/layers-commands.c * app/gui/module-browser.c * app/gui/offset-dialog.c * app/gui/palette-import-dialog.c * app/gui/palette-select.c * app/gui/pattern-select.c * app/gui/preferences-dialog.c * app/gui/qmask-commands.c * app/gui/resize-dialog.c * app/gui/resolution-calibrate-dialog.c * app/gui/stroke-dialog.c * app/gui/templates-commands.c * app/gui/user-install-dialog.c * app/gui/vectors-commands.c * app/tools/gimpcolorpickertool.c * app/tools/gimpcroptool.c * app/tools/gimpimagemaptool.c * app/tools/gimpmeasuretool.c * app/tools/gimptransformtool.c * app/widgets/gimptexteditor.c * app/widgets/gimptooldialog.[ch] * app/widgets/gimpviewabledialog.[ch] * app/widgets/gimpwidgets-utils.c: changed accordingly and increased the dialogs' outer borders to 6 pixels all over the place. * plug-ins/*/*.c: changed accordingly. The plug-ins may be arbitrarily broken, I tested none of them.
2003-11-06 15:27:05 +00:00
struct _PaletteClass
1997-11-24 22:05:25 +00:00
{
GimpPlugInClass parent_class;
1997-11-24 22:05:25 +00:00
};
#define PALETTE_TYPE (palette_get_type ())
#define PALETTE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PALETTE_TYPE, Palette))
GType palette_get_type (void) G_GNUC_CONST;
static GList * palette_query_procedures (GimpPlugIn *plug_in);
static GimpProcedure * palette_create_procedure (GimpPlugIn *plug_in,
const gchar *name);
static GimpValueArray * palette_run (GimpProcedure *procedure,
GimpRunMode run_mode,
GimpImage *image,
gint n_drawables,
GimpDrawable **drawables,
GimpProcedureConfig *config,
gpointer run_data);
static gboolean dialog (GimpProcedure *procedure,
GimpProcedureConfig *config,
GimpDrawable *drawable);
static GimpImage * smooth_palette (GimpProcedureConfig *config,
GimpDrawable *drawable,
GimpLayer **layer);
G_DEFINE_TYPE (Palette, palette, GIMP_TYPE_PLUG_IN)
GIMP_MAIN (PALETTE_TYPE)
DEFINE_STD_SET_I18N
1997-11-24 22:05:25 +00:00
#define TRY_SIZE 10000
1997-11-24 22:05:25 +00:00
static void
palette_class_init (PaletteClass *klass)
1997-11-24 22:05:25 +00:00
{
GimpPlugInClass *plug_in_class = GIMP_PLUG_IN_CLASS (klass);
1997-11-24 22:05:25 +00:00
plug_in_class->query_procedures = palette_query_procedures;
plug_in_class->create_procedure = palette_create_procedure;
plug_in_class->set_i18n = STD_SET_I18N;
}
1997-11-24 22:05:25 +00:00
static void
palette_init (Palette *palette)
{
}
static GList *
palette_query_procedures (GimpPlugIn *plug_in)
{
return g_list_append (NULL, g_strdup (PLUG_IN_PROC));
}
1997-11-24 22:05:25 +00:00
static GimpProcedure *
palette_create_procedure (GimpPlugIn *plug_in,
const gchar *name)
{
GimpProcedure *procedure = NULL;
1997-11-24 22:05:25 +00:00
if (! strcmp (name, PLUG_IN_PROC))
{
procedure = gimp_image_procedure_new (plug_in, name,
GIMP_PDB_PROC_TYPE_PLUGIN,
palette_run, NULL, NULL);
gimp_procedure_set_image_types (procedure, "RGB*");
gimp_procedure_set_sensitivity_mask (procedure,
GIMP_PROCEDURE_SENSITIVE_DRAWABLE);
gimp_procedure_set_menu_label (procedure, _("Smoo_th Palette..."));
gimp_procedure_add_menu_path (procedure, "<Image>/Colors/Info");
gimp_procedure_set_documentation (procedure,
_("Derive a smooth color palette "
"from the image"),
"help!",
name);
gimp_procedure_set_attribution (procedure,
"Scott Draves",
"Scott Draves",
"1997");
gimp_procedure_add_int_argument (procedure, "width",
_("_Width"),
_("Width"),
2, GIMP_MAX_IMAGE_SIZE, 256,
G_PARAM_READWRITE);
gimp_procedure_add_int_argument (procedure, "height",
_("_Height"),
_("Height"),
2, GIMP_MAX_IMAGE_SIZE, 64,
G_PARAM_READWRITE);
gimp_procedure_add_int_argument (procedure, "n-tries",
_("Search _depth"),
_("Search depth"),
1, 1024, 50,
G_PARAM_READWRITE);
gimp_procedure_add_boolean_argument (procedure, "show-image",
_("Show image"),
_("Show image"),
TRUE,
G_PARAM_READWRITE);
gimp_procedure_add_image_return_value (procedure, "new-image",
_("New image"),
_("Output image"),
FALSE,
G_PARAM_READWRITE);
gimp_procedure_add_layer_return_value (procedure, "new-layer",
_("New layer"),
_("Output layer"),
FALSE,
G_PARAM_READWRITE);
}
return procedure;
}
static GimpValueArray *
palette_run (GimpProcedure *procedure,
GimpRunMode run_mode,
GimpImage *image,
gint n_drawables,
GimpDrawable **drawables,
GimpProcedureConfig *config,
gpointer run_data)
{
GimpImage *new_image;
GimpLayer *new_layer;
GimpDrawable *drawable;
gboolean show_image;
gegl_init (NULL, NULL);
if (n_drawables != 1)
{
GError *error = NULL;
g_set_error (&error, GIMP_PLUG_IN_ERROR, 0,
_("Procedure '%s' only works with one drawable."),
PLUG_IN_PROC);
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_CALLING_ERROR,
error);
}
else
{
drawable = drawables[0];
}
if (run_mode == GIMP_RUN_INTERACTIVE && ! dialog (procedure, config, drawable))
{
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_CANCEL,
NULL);
1997-11-24 22:05:25 +00:00
}
if (gimp_drawable_is_rgb (drawable))
{
gimp_progress_init (_("Deriving smooth palette"));
Cleaned up and improved the message system: 2003-06-13 Michael Natterer <mitch@gimp.org> Cleaned up and improved the message system: * app/core/gimp.[ch]: added "const gchar *domain" to GimpMessageFunc (a NULL domain means the message is from the GIMP core, everything else is a plug-in). * app/errors.c: pass "domain == NULL" to gimp_message(). * tools/pdbgen/pdb/message.pdb: derive the message domain from the current plug-in's menu_path (evil hack but works reasonably well). * app/pdb/message_cmds.c: regenerated. * app/widgets/gimpwidgets-utils.[ch] (gimp_message_box): added a header showing the message domain and changed the dialog layout to follow the HIG more closely. * app/gui/error-console-dialog.[ch]: removed. * app/widgets/gimperrorconsole.[ch] * app/gui/error-console-commands.[ch] * app/gui/error-console-menu.[ch]: new files containing a re-implementation of the error console dialog. * app/gui/Makefile.am * app/gui/dialogs-constructors.c * app/gui/gui.c * app/gui/menus.c * app/widgets/Makefile.am * app/widgets/widgets-types.h: changed accordingly. * app/display/gimpprogress.c: added more spacing and removed the separator (more HIG compliant). * plug-ins/[most plug-ins].c: Changed lots of messages and progress strings: - Removed plug-in names from messages since that's automatically covered by "domain" now. - Put all filenames in ''. - Changed "Loading" to "Opening". - Added "..." to all progress messages. - Cleaned up all file open/save error messages to look the same and include g_strerror(errno). - Removed special casing for progress bars and *always* show them, not only if run_mode != GIMP_RUN_NONINTERACTIVE (we can't expect all plug-ins to do this correctly but need to hack the core to sort out unwanted progress bars). Unrelated: - Cleaned up indentation, spacing, #includes, coding style and other stuff while I was at all these files.
2003-06-13 14:37:00 +00:00
new_image = smooth_palette (config, drawable, &new_layer);
g_object_get (config, "show-image", &show_image, NULL);
if (show_image)
gimp_display_new (new_image);
}
else
{
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_EXECUTION_ERROR,
NULL);
Cleaned up and improved the message system: 2003-06-13 Michael Natterer <mitch@gimp.org> Cleaned up and improved the message system: * app/core/gimp.[ch]: added "const gchar *domain" to GimpMessageFunc (a NULL domain means the message is from the GIMP core, everything else is a plug-in). * app/errors.c: pass "domain == NULL" to gimp_message(). * tools/pdbgen/pdb/message.pdb: derive the message domain from the current plug-in's menu_path (evil hack but works reasonably well). * app/pdb/message_cmds.c: regenerated. * app/widgets/gimpwidgets-utils.[ch] (gimp_message_box): added a header showing the message domain and changed the dialog layout to follow the HIG more closely. * app/gui/error-console-dialog.[ch]: removed. * app/widgets/gimperrorconsole.[ch] * app/gui/error-console-commands.[ch] * app/gui/error-console-menu.[ch]: new files containing a re-implementation of the error console dialog. * app/gui/Makefile.am * app/gui/dialogs-constructors.c * app/gui/gui.c * app/gui/menus.c * app/widgets/Makefile.am * app/widgets/widgets-types.h: changed accordingly. * app/display/gimpprogress.c: added more spacing and removed the separator (more HIG compliant). * plug-ins/[most plug-ins].c: Changed lots of messages and progress strings: - Removed plug-in names from messages since that's automatically covered by "domain" now. - Put all filenames in ''. - Changed "Loading" to "Opening". - Added "..." to all progress messages. - Cleaned up all file open/save error messages to look the same and include g_strerror(errno). - Removed special casing for progress bars and *always* show them, not only if run_mode != GIMP_RUN_NONINTERACTIVE (we can't expect all plug-ins to do this correctly but need to hack the core to sort out unwanted progress bars). Unrelated: - Cleaned up indentation, spacing, #includes, coding style and other stuff while I was at all these files.
2003-06-13 14:37:00 +00:00
}
1997-11-24 22:05:25 +00:00
return gimp_procedure_new_return_values (procedure, GIMP_PDB_SUCCESS, NULL);
1997-11-24 22:05:25 +00:00
}
2016-11-21 11:25:10 +01:00
static gfloat
pix_diff (gfloat *pal,
guint bpp,
gint i,
gint j)
{
2016-11-21 11:25:10 +01:00
gfloat r = 0.f;
guint k;
1997-11-24 22:05:25 +00:00
for (k = 0; k < bpp; k++)
{
2016-11-21 11:25:10 +01:00
gfloat p1 = pal[j * bpp + k];
gfloat p2 = pal[i * bpp + k];
r += (p1 - p2) * (p1 - p2);
}
1997-11-24 22:05:25 +00:00
return r;
}
static void
2016-11-21 11:25:10 +01:00
pix_swap (gfloat *pal,
guint bpp,
gint i,
gint j)
{
guint k;
for (k = 0; k < bpp; k++)
{
2016-11-21 11:25:10 +01:00
gfloat t = pal[j * bpp + k];
pal[j * bpp + k] = pal[i * bpp + k];
pal[i * bpp + k] = t;
}
1997-11-24 22:05:25 +00:00
}
static GimpImage *
smooth_palette (GimpProcedureConfig *config,
GimpDrawable *drawable,
GimpLayer **layer)
{
GimpImage *new_image;
gint psize, i, j;
2016-11-21 11:25:10 +01:00
guint bpp;
gint sel_x1, sel_y1;
gint width, height;
gint config_width;
gint config_height;
gint config_n_tries;
2016-11-21 11:25:10 +01:00
GeglBuffer *buffer;
GeglSampler *sampler;
2016-11-21 11:25:10 +01:00
gfloat *pal;
GRand *gr;
removed our own action_area API and use GtkDialog's one. Create all 2003-11-06 Michael Natterer <mitch@gimp.org> * libgimpwidgets/gimpdialog.[ch]: removed our own action_area API and use GtkDialog's one. Create all dialogs without separator. Changed almost everything else too. Fixes bug #125143. * libgimpwidgets/gimpquerybox.c * libgimpwidgets/gimpunitmenu.c: changed accordingly. * libgimp/gimpexport.[ch]: ditto. Renamed enum GimpExportReturnType to GimpExportReturn. * libgimp/gimpcompat.h: added a #define for the old name. * themes/Default/gtkrc: increased action_area border to 6 pixels. * app/display/gimpdisplayshell-filter-dialog.c * app/display/gimpdisplayshell-scale.c * app/display/gimpprogress.c * app/gui/brush-select.c * app/gui/channels-commands.c * app/gui/color-notebook.c * app/gui/convert-dialog.c * app/gui/file-new-dialog.c * app/gui/font-select.c * app/gui/gradient-editor-commands.c * app/gui/gradient-select.c * app/gui/grid-dialog.c * app/gui/image-commands.c * app/gui/info-window.c * app/gui/layers-commands.c * app/gui/module-browser.c * app/gui/offset-dialog.c * app/gui/palette-import-dialog.c * app/gui/palette-select.c * app/gui/pattern-select.c * app/gui/preferences-dialog.c * app/gui/qmask-commands.c * app/gui/resize-dialog.c * app/gui/resolution-calibrate-dialog.c * app/gui/stroke-dialog.c * app/gui/templates-commands.c * app/gui/user-install-dialog.c * app/gui/vectors-commands.c * app/tools/gimpcolorpickertool.c * app/tools/gimpcroptool.c * app/tools/gimpimagemaptool.c * app/tools/gimpmeasuretool.c * app/tools/gimptransformtool.c * app/widgets/gimptexteditor.c * app/widgets/gimptooldialog.[ch] * app/widgets/gimpviewabledialog.[ch] * app/widgets/gimpwidgets-utils.c: changed accordingly and increased the dialogs' outer borders to 6 pixels all over the place. * plug-ins/*/*.c: changed accordingly. The plug-ins may be arbitrarily broken, I tested none of them.
2003-11-06 15:27:05 +00:00
2016-11-21 11:25:10 +01:00
const Babl *format = babl_format ("RGB float");
g_object_get (config,
"width", &config_width,
"height", &config_height,
"n-tries", &config_n_tries,
NULL);
new_image = gimp_image_new_with_precision (config_width,
config_height,
GIMP_RGB,
GIMP_PRECISION_FLOAT_LINEAR);
1997-11-24 22:05:25 +00:00
gimp_image_undo_disable (new_image);
2016-11-21 11:25:10 +01:00
*layer = gimp_layer_new (new_image, _("Background"),
config_width, config_height,
gimp_drawable_type (drawable),
100,
gimp_image_get_default_new_layer_mode (new_image));
2016-11-21 11:25:10 +01:00
gimp_image_insert_layer (new_image, *layer, NULL, 0);
2016-11-21 11:25:10 +01:00
if (! gimp_drawable_mask_intersect (drawable,
2016-11-21 11:25:10 +01:00
&sel_x1, &sel_y1, &width, &height))
return new_image;
2016-11-21 11:25:10 +01:00
gr = g_rand_new ();
1997-11-24 22:05:25 +00:00
psize = config_width;
1997-11-24 22:05:25 +00:00
buffer = gimp_drawable_get_buffer (drawable);
1997-11-24 22:05:25 +00:00
sampler = gegl_buffer_sampler_new (buffer, format, GEGL_SAMPLER_NEAREST);
2016-11-21 11:25:10 +01:00
bpp = babl_format_get_n_components (gegl_buffer_get_format (buffer));
2016-11-21 11:25:10 +01:00
pal = g_new (gfloat, psize * bpp);
1997-11-24 22:05:25 +00:00
/* get initial palette */
2016-11-21 11:25:10 +01:00
for (i = 0; i < psize; i++)
{
configure.in app/core/gimpbrushpipe.c app/gui/about-dialog.c 2002-11-20 Dave Neary <bolsh@gimp.org> * configure.in * app/core/gimpbrushpipe.c * app/gui/about-dialog.c * app/paint-funcs/paint-funcs-generic.h * app/paint-funcs/paint-funcs.c * libgimpmath/gimpmath.h * libgimpwidgets/gimpwidgets.c * plug-ins/common/CML_explorer.c * plug-ins/common/blur.c * plug-ins/common/cubism.c * plug-ins/common/gee.c * plug-ins/common/gee_zoom.c * plug-ins/common/gqbist.c * plug-ins/common/jigsaw.c * plug-ins/common/lic.c * plug-ins/common/noisify.c * plug-ins/common/nova.c * plug-ins/common/papertile.c * plug-ins/common/plasma.c * plug-ins/common/randomize.c * plug-ins/common/sample_colorize.c * plug-ins/common/scatter_hsv.c * plug-ins/common/shift.c * plug-ins/common/sinus.c * plug-ins/common/smooth_palette.c * plug-ins/common/snoise.c * plug-ins/common/sparkle.c * plug-ins/common/spheredesigner.c * plug-ins/common/spread.c * plug-ins/common/warp.c * plug-ins/common/wind.c * plug-ins/flame/cmap.c * plug-ins/flame/flame.c * plug-ins/flame/libifs.c * plug-ins/gflare/gflare.c * plug-ins/gimpressionist/gimpressionist.c * plug-ins/gimpressionist/gimpressionist.h * plug-ins/gimpressionist/plasma.c * plug-ins/gimpressionist/repaint.c * plug-ins/ifscompose/ifscompose_utils.c * plug-ins/maze/algorithms.c * plug-ins/maze/maze.c * plug-ins/maze/maze.h * plug-ins/mosaic/mosaic.c: Change all occurrences of RAND_MAX, G_MAXRAND, rand(), srand(), lrand48(), srand48(), random(), srandom(), RAND_FUNC and SRAND_FUNC to the appropriate g_rand* equivalent. Programs which require seed setting for reproducible results, and anything in the core, gets a dedicated GRand * for the lifetime required. Programs which only ever used random numbers for tossing a coin, rolling a dice, etc use g_random functions. For the rest, judgement was used. Where it was easy, a GRand * object was used and g_rand_* functions were preferred. This fixes bug #67386 in HEAD.
2002-11-20 09:27:48 +00:00
gint x = sel_x1 + g_rand_int_range (gr, 0, width);
gint y = sel_y1 + g_rand_int_range (gr, 0, height);
gegl_sampler_get (sampler,
(gdouble) x, (gdouble) y, NULL, pal + i * bpp,
GEGL_ABYSS_NONE);
}
1997-11-24 22:05:25 +00:00
g_object_unref (sampler);
2016-11-21 11:25:10 +01:00
g_object_unref (buffer);
1997-11-24 22:05:25 +00:00
/* reorder */
if (1)
{
2016-11-21 11:25:10 +01:00
gfloat *pal_best;
gfloat *original;
gdouble len_best = 0;
gint try;
pal_best = g_memdup2 (pal, bpp * psize);
original = g_memdup2 (pal, bpp * psize);
for (try = 0; try < config_n_tries; try++)
{
gdouble len;
if (! (try % 5))
gimp_progress_update (try / (double) config_n_tries);
memcpy (pal, original, bpp * psize);
/* scramble */
for (i = 1; i < psize; i++)
pix_swap (pal, bpp, i, g_rand_int_range (gr, 0, psize));
/* measure */
len = 0.0;
for (i = 1; i < psize; i++)
len += pix_diff (pal, bpp, i, i-1);
/* improve */
for (i = 0; i < TRY_SIZE; i++)
{
gint i0 = 1 + g_rand_int_range (gr, 0, psize-2);
gint i1 = 1 + g_rand_int_range (gr, 0, psize-2);
2016-11-21 11:25:10 +01:00
gfloat as_is, swapd;
if (1 == (i0 - i1))
{
as_is = (pix_diff (pal, bpp, i1 - 1, i1) +
pix_diff (pal, bpp, i0, i0 + 1));
swapd = (pix_diff (pal, bpp, i1 - 1, i0) +
pix_diff (pal, bpp, i1, i0 + 1));
}
else if (1 == (i1 - i0))
{
as_is = (pix_diff (pal, bpp, i0 - 1, i0) +
pix_diff (pal, bpp, i1, i1 + 1));
swapd = (pix_diff (pal, bpp, i0 - 1, i1) +
pix_diff (pal, bpp, i0, i1 + 1));
}
else
{
as_is = (pix_diff (pal, bpp, i0, i0 + 1) +
pix_diff (pal, bpp, i0, i0 - 1) +
pix_diff (pal, bpp, i1, i1 + 1) +
pix_diff (pal, bpp, i1, i1 - 1));
swapd = (pix_diff (pal, bpp, i1, i0 + 1) +
pix_diff (pal, bpp, i1, i0 - 1) +
pix_diff (pal, bpp, i0, i1 + 1) +
pix_diff (pal, bpp, i0, i1 - 1));
}
if (swapd < as_is)
{
pix_swap (pal, bpp, i0, i1);
len += swapd - as_is;
}
}
/* best? */
if (0 == try || len < len_best)
{
memcpy (pal_best, pal, bpp * psize);
len_best = len;
}
}
2016-11-21 11:25:10 +01:00
gimp_progress_update (1.0);
memcpy (pal, pal_best, bpp * psize);
g_free (pal_best);
g_free (original);
2016-11-21 11:25:10 +01:00
/* clean */
for (i = 1; i < 4 * psize; i++)
{
2016-11-21 11:25:10 +01:00
gfloat as_is, swapd;
gint i0 = 1 + g_rand_int_range (gr, 0, psize - 2);
gint i1 = i0 + 1;
as_is = (pix_diff (pal, bpp, i0 - 1, i0) +
pix_diff (pal, bpp, i1, i1 + 1));
swapd = (pix_diff (pal, bpp, i0 - 1, i1) +
pix_diff (pal, bpp, i0, i1 + 1));
2016-11-21 11:25:10 +01:00
if (swapd < as_is)
{
pix_swap (pal, bpp, i0, i1);
len_best += swapd - as_is;
}
}
1997-11-24 22:05:25 +00:00
}
/* store smooth palette */
2016-11-21 11:25:10 +01:00
buffer = gimp_drawable_get_buffer (GIMP_DRAWABLE (*layer));
2016-11-21 11:25:10 +01:00
for (j = 0; j < config_height; j++)
2016-11-21 11:25:10 +01:00
{
GeglRectangle row = {0, j, config_width, 1};
2016-11-21 11:25:10 +01:00
gegl_buffer_set (buffer, &row, 0, format, pal, GEGL_AUTO_ROWSTRIDE);
}
1997-11-24 22:05:25 +00:00
2016-11-21 11:25:10 +01:00
gegl_buffer_flush (buffer);
gimp_drawable_update (GIMP_DRAWABLE (*layer), 0, 0,
config_width, config_height);
gimp_image_undo_enable (new_image);
1997-11-24 22:05:25 +00:00
2016-11-21 11:25:10 +01:00
g_object_unref (buffer);
g_free (pal);
g_rand_free (gr);
return new_image;
1997-11-24 22:05:25 +00:00
}
static gboolean
dialog (GimpProcedure *procedure,
GimpProcedureConfig *config,
GimpDrawable *drawable)
1997-11-24 22:05:25 +00:00
{
GtkWidget *dlg;
GtkWidget *sizeentry;
GimpImage *image;
GimpUnit unit;
gdouble xres, yres;
gint width;
gint height;
gboolean run;
1997-11-24 22:05:25 +00:00
g_object_get (config,
"width", &width,
"height", &height,
NULL);
gimp_ui_init (PLUG_IN_BINARY);
dlg = gimp_procedure_dialog_new (procedure,
GIMP_PROCEDURE_CONFIG (config),
_("Smooth Palette"));
gimp_dialog_set_alternative_button_order (GTK_DIALOG (dlg),
GTK_RESPONSE_OK,
GTK_RESPONSE_CANCEL,
-1);
Added parent window API to the GimpProgress interface and to the libgimp 2005-09-09 Michael Natterer <mitch@gimp.org> Added parent window API to the GimpProgress interface and to the libgimp progress stuff. Might look strange, but does the right thing in almost all cases (image window, file dialog, script-fu dialog etc). Fixes bug #62988. * app/core/gimpprogress.[ch]: added GimpProgress::get_window() which should return a toplevel window ID if the progress is in a window that wants to be the transient parent of plug-in dialogs. * app/widgets/gimpwidgets-utils.[ch] (gimp_window_get_native): new function which returns the window handle of a GtkWindow's GdkWindow. * app/widgets/gimpfiledialog.c: implement ::get_window(). * app/display/gimpdisplay.[ch]: ditto. Removed window handle API. * app/gui/gui-vtable.c: changed accordingly. * libgimpbase/gimpbaseenums.[ch] (enum GimpProgressCommand): added GIMP_PROGRESS_COMMAND_GET_WINDOW. * app/plug-in/plug-in-progress.[ch] (plug_in_progress_get_window): new function. Also renamed some functions to match the GimpProgress interface, and not the legacy PDB procedure names. * tools/pdbgen/pdb/progress.pdb * app/core/gimppdbprogress.c: implement get_window() on both sides of the wire, keeping backward compatibility (hopefully). * libgimp/gimpprogress.[ch]: deprecated gimp_progress_install() and added gimp_progress_install_vtable() which takes a vtable with padding to be extensible. Added get_window() vtable entry and dispatch it accordingly. Also added pulse() which was implemented in a hackish way before. Everything is of course backward compatible. * libgimp/gimpprogressbar.c: inmplement the get_window() stuff so a plug-in dialog containing a progress can be the transient parent of another dialog in another plug-in. * libgimp/gimpui.[ch] (gimp_ui_get_progress_window): new function which returns a foreign GdkWindow of this plug-ins progress window. Renamed gimp_window_set_transient_for_default_display() to gimp_window_set_transient() and make it use the progress' window handle instead of the display's (which is the right thing to do in almost all cases). * libgimp/gimp.def * libgimp/gimpui.def: add the new functions. * tools/pdbgen/enums.pl * app/pdb/internal_procs.c * app/pdb/progress_cmds.c * libgimp/gimpprogress_pdb.[ch]: regenerated. * libgimp/gimpexport.c * plug-ins/*/*.c: follow API change.
2005-09-09 18:07:31 +00:00
gimp_window_set_transient (GTK_WINDOW (dlg));
image = gimp_item_get_image (GIMP_ITEM (drawable));
unit = gimp_image_get_unit (image);
gimp_image_get_resolution (image, &xres, &yres);
sizeentry = gimp_coordinates_new (unit, "%a", TRUE, FALSE, 6,
GIMP_SIZE_ENTRY_UPDATE_SIZE,
FALSE, FALSE,
_("_Width:"),
width, xres,
2, GIMP_MAX_IMAGE_SIZE,
2, GIMP_MAX_IMAGE_SIZE,
_("_Height:"),
height, yres,
1, GIMP_MAX_IMAGE_SIZE,
1, GIMP_MAX_IMAGE_SIZE);
gtk_container_set_border_width (GTK_CONTAINER (sizeentry), 12);
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dlg))),
sizeentry, FALSE, FALSE, 0);
gtk_widget_show (sizeentry);
/* We don't want to have the "Show Image" option in the GUI */
gimp_procedure_dialog_fill (GIMP_PROCEDURE_DIALOG (dlg), "n-tries", NULL);
gtk_widget_show (dlg);
run = (gimp_dialog_run (GIMP_DIALOG (dlg)) == GTK_RESPONSE_OK);
1997-11-24 22:05:25 +00:00
removed our own action_area API and use GtkDialog's one. Create all 2003-11-06 Michael Natterer <mitch@gimp.org> * libgimpwidgets/gimpdialog.[ch]: removed our own action_area API and use GtkDialog's one. Create all dialogs without separator. Changed almost everything else too. Fixes bug #125143. * libgimpwidgets/gimpquerybox.c * libgimpwidgets/gimpunitmenu.c: changed accordingly. * libgimp/gimpexport.[ch]: ditto. Renamed enum GimpExportReturnType to GimpExportReturn. * libgimp/gimpcompat.h: added a #define for the old name. * themes/Default/gtkrc: increased action_area border to 6 pixels. * app/display/gimpdisplayshell-filter-dialog.c * app/display/gimpdisplayshell-scale.c * app/display/gimpprogress.c * app/gui/brush-select.c * app/gui/channels-commands.c * app/gui/color-notebook.c * app/gui/convert-dialog.c * app/gui/file-new-dialog.c * app/gui/font-select.c * app/gui/gradient-editor-commands.c * app/gui/gradient-select.c * app/gui/grid-dialog.c * app/gui/image-commands.c * app/gui/info-window.c * app/gui/layers-commands.c * app/gui/module-browser.c * app/gui/offset-dialog.c * app/gui/palette-import-dialog.c * app/gui/palette-select.c * app/gui/pattern-select.c * app/gui/preferences-dialog.c * app/gui/qmask-commands.c * app/gui/resize-dialog.c * app/gui/resolution-calibrate-dialog.c * app/gui/stroke-dialog.c * app/gui/templates-commands.c * app/gui/user-install-dialog.c * app/gui/vectors-commands.c * app/tools/gimpcolorpickertool.c * app/tools/gimpcroptool.c * app/tools/gimpimagemaptool.c * app/tools/gimpmeasuretool.c * app/tools/gimptransformtool.c * app/widgets/gimptexteditor.c * app/widgets/gimptooldialog.[ch] * app/widgets/gimpviewabledialog.[ch] * app/widgets/gimpwidgets-utils.c: changed accordingly and increased the dialogs' outer borders to 6 pixels all over the place. * plug-ins/*/*.c: changed accordingly. The plug-ins may be arbitrarily broken, I tested none of them.
2003-11-06 15:27:05 +00:00
if (run)
{
width = gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (sizeentry), 0);
height = gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (sizeentry), 1);
g_object_set (config,
"width", width,
"height", height,
NULL);
removed our own action_area API and use GtkDialog's one. Create all 2003-11-06 Michael Natterer <mitch@gimp.org> * libgimpwidgets/gimpdialog.[ch]: removed our own action_area API and use GtkDialog's one. Create all dialogs without separator. Changed almost everything else too. Fixes bug #125143. * libgimpwidgets/gimpquerybox.c * libgimpwidgets/gimpunitmenu.c: changed accordingly. * libgimp/gimpexport.[ch]: ditto. Renamed enum GimpExportReturnType to GimpExportReturn. * libgimp/gimpcompat.h: added a #define for the old name. * themes/Default/gtkrc: increased action_area border to 6 pixels. * app/display/gimpdisplayshell-filter-dialog.c * app/display/gimpdisplayshell-scale.c * app/display/gimpprogress.c * app/gui/brush-select.c * app/gui/channels-commands.c * app/gui/color-notebook.c * app/gui/convert-dialog.c * app/gui/file-new-dialog.c * app/gui/font-select.c * app/gui/gradient-editor-commands.c * app/gui/gradient-select.c * app/gui/grid-dialog.c * app/gui/image-commands.c * app/gui/info-window.c * app/gui/layers-commands.c * app/gui/module-browser.c * app/gui/offset-dialog.c * app/gui/palette-import-dialog.c * app/gui/palette-select.c * app/gui/pattern-select.c * app/gui/preferences-dialog.c * app/gui/qmask-commands.c * app/gui/resize-dialog.c * app/gui/resolution-calibrate-dialog.c * app/gui/stroke-dialog.c * app/gui/templates-commands.c * app/gui/user-install-dialog.c * app/gui/vectors-commands.c * app/tools/gimpcolorpickertool.c * app/tools/gimpcroptool.c * app/tools/gimpimagemaptool.c * app/tools/gimpmeasuretool.c * app/tools/gimptransformtool.c * app/widgets/gimptexteditor.c * app/widgets/gimptooldialog.[ch] * app/widgets/gimpviewabledialog.[ch] * app/widgets/gimpwidgets-utils.c: changed accordingly and increased the dialogs' outer borders to 6 pixels all over the place. * plug-ins/*/*.c: changed accordingly. The plug-ins may be arbitrarily broken, I tested none of them.
2003-11-06 15:27:05 +00:00
}
gtk_widget_destroy (dlg);
return run;
1997-11-24 22:05:25 +00:00
}