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

499 lines
14 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"
1997-11-24 22:05:25 +00:00
/* Declare local functions. */
static void query (void);
static void run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
1997-11-24 22:05:25 +00:00
2016-11-21 11:25:10 +01:00
static gboolean dialog (gint32 drawable_id);
2016-11-21 11:25:10 +01:00
static gint32 smooth_palette (gint32 drawable_id,
gint32 *layer_id);
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
const GimpPlugInInfo PLUG_IN_INFO =
1997-11-24 22:05:25 +00:00
{
NULL, /* init_proc */
NULL, /* quit_proc */
1997-11-24 22:05:25 +00:00
query, /* query_proc */
run, /* run_proc */
1997-11-24 22:05:25 +00:00
};
MAIN ()
1997-11-24 22:05:25 +00:00
static void
query (void)
1997-11-24 22:05:25 +00:00
{
static const GimpParamDef args[] =
1997-11-24 22:05:25 +00:00
{
{ GIMP_PDB_INT32, "run-mode", "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
{ GIMP_PDB_IMAGE, "image", "Input image (unused)" },
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
{ GIMP_PDB_INT32, "width", "Width" },
{ GIMP_PDB_INT32, "height", "Height" },
{ GIMP_PDB_INT32, "ntries", "Search Depth" },
{ GIMP_PDB_INT32, "show-image", "Show Image?" }
1997-11-24 22:05:25 +00:00
};
static const GimpParamDef return_vals[] =
{
{ GIMP_PDB_IMAGE, "new-image", "Output image" },
{ GIMP_PDB_LAYER, "new-layer", "Output layer" }
};
gimp_install_procedure (PLUG_IN_PROC,
N_("Derive a smooth color palette from the image"),
"help!",
"Scott Draves",
"Scott Draves",
"1997",
N_("Smoo_th Palette..."),
"RGB*",
GIMP_PLUGIN,
G_N_ELEMENTS (args), G_N_ELEMENTS (return_vals),
args, return_vals);
gimp_plugin_menu_register (PLUG_IN_PROC, "<Image>/Colors/Info");
1997-11-24 22:05:25 +00:00
}
static struct
{
1997-11-24 22:05:25 +00:00
gint width;
gint height;
gint ntries;
gint try_size;
gint show_image;
} config =
{
1997-11-24 22:05:25 +00:00
256,
64,
50,
10000,
1
1997-11-24 22:05:25 +00:00
};
static void
run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals)
1997-11-24 22:05:25 +00:00
{
static GimpParam values[3];
GimpRunMode run_mode;
2016-11-21 11:25:10 +01:00
gint32 drawable_id;
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
1997-11-24 22:05:25 +00:00
run_mode = param[0].data.d_int32;
INIT_I18N ();
1997-11-24 22:05:25 +00:00
*nreturn_vals = 3;
*return_vals = values;
1997-11-24 22:05:25 +00:00
values[0].type = GIMP_PDB_STATUS;
1997-11-24 22:05:25 +00:00
values[0].data.d_status = status;
values[1].type = GIMP_PDB_IMAGE;
values[2].type = GIMP_PDB_LAYER;
1997-11-24 22:05:25 +00:00
2016-11-21 11:25:10 +01:00
drawable_id = param[2].data.d_drawable;
switch (run_mode)
{
case GIMP_RUN_INTERACTIVE:
gimp_get_data (PLUG_IN_PROC, &config);
2016-11-21 11:25:10 +01:00
if (! dialog (drawable_id))
return;
1997-11-24 22:05:25 +00:00
break;
case GIMP_RUN_NONINTERACTIVE:
if (nparams != 7)
{
status = GIMP_PDB_CALLING_ERROR;
}
else
{
config.width = param[3].data.d_int32;
config.height = param[4].data.d_int32;
config.ntries = param[5].data.d_int32;
config.show_image = param[6].data.d_int32 ? TRUE : FALSE;
}
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 (status == GIMP_PDB_SUCCESS &&
((config.width <= 0) || (config.height <= 0) || config.ntries <= 0))
status = GIMP_PDB_CALLING_ERROR;
1997-11-24 22:05:25 +00:00
break;
case GIMP_RUN_WITH_LAST_VALS:
1997-11-24 22:05:25 +00:00
/* Possibly retrieve data */
gimp_get_data (PLUG_IN_PROC, &config);
1997-11-24 22:05:25 +00:00
break;
default:
break;
}
if (status == GIMP_PDB_SUCCESS)
{
2016-11-21 11:25:10 +01:00
if (gimp_drawable_is_rgb (drawable_id))
{
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
2016-11-21 11:25:10 +01:00
gegl_init (NULL, NULL);
values[1].data.d_image = smooth_palette (drawable_id,
&values[2].data.d_layer);
2016-11-21 11:25:10 +01:00
gegl_exit ();
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
if (run_mode == GIMP_RUN_INTERACTIVE)
gimp_set_data (PLUG_IN_PROC, &config, sizeof (config));
if (config.show_image)
gimp_display_new (values[1].data.d_image);
}
else
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
{
status = GIMP_PDB_EXECUTION_ERROR;
}
}
1997-11-24 22:05:25 +00:00
values[0].data.d_status = status;
}
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 gint32
2016-11-21 11:25:10 +01:00
smooth_palette (gint32 drawable_id,
gint32 *layer_id)
{
gint32 new_image_id;
gint psize, i, j;
2016-11-21 11:25:10 +01:00
guint bpp;
gint sel_x1, sel_y1;
gint width, height;
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");
new_image_id = 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_id);
2016-11-21 11:25:10 +01:00
*layer_id = gimp_layer_new (new_image_id, _("Background"),
config.width, config.height,
2016-11-21 11:25:10 +01:00
gimp_drawable_type (drawable_id),
100,
gimp_image_get_default_new_layer_mode (new_image_id));
2016-11-21 11:25:10 +01:00
gimp_image_insert_layer (new_image_id, *layer_id, -1, 0);
2016-11-21 11:25:10 +01:00
if (! gimp_drawable_mask_intersect (drawable_id,
&sel_x1, &sel_y1, &width, &height))
return new_image_id;
gr = g_rand_new ();
1997-11-24 22:05:25 +00:00
psize = config.width;
2016-11-21 11:25:10 +01:00
buffer = gimp_drawable_get_buffer (drawable_id);
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_memdup (pal, bpp * psize);
original = g_memdup (pal, bpp * psize);
for (try = 0; try < config.ntries; try++)
{
gdouble len;
if (!(try%5))
gimp_progress_update (try / (double) config.ntries);
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 < config.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 (*layer_id);
1997-11-24 22:05:25 +00:00
for (j = 0; j < config.height; j++)
2016-11-21 11:25:10 +01:00
{
GeglRectangle row = {0, j, config.width, 1};
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 (*layer_id, 0, 0,
config.width, config.height);
gimp_image_undo_enable (new_image_id);
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);
1997-11-24 22:05:25 +00:00
return new_image_id;
}
static gboolean
2016-11-21 11:25:10 +01:00
dialog (gint32 drawable_id)
1997-11-24 22:05:25 +00:00
{
GtkWidget *dlg;
GtkWidget *spinbutton;
GtkAdjustment *adj;
GtkWidget *sizeentry;
guint32 image_id;
GimpUnit unit;
gdouble xres, yres;
gboolean run;
1997-11-24 22:05:25 +00:00
gimp_ui_init (PLUG_IN_BINARY, FALSE);
1997-11-24 22:05:25 +00:00
dlg = gimp_dialog_new (_("Smooth Palette"), PLUG_IN_ROLE,
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
NULL, 0,
gimp_standard_help_func, PLUG_IN_PROC,
_("_Cancel"), GTK_RESPONSE_CANCEL,
_("_OK"), GTK_RESPONSE_OK,
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
NULL);
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));
2016-11-21 11:25:10 +01:00
image_id = gimp_item_get_image (drawable_id);
unit = gimp_image_get_unit (image_id);
gimp_image_get_resolution (image_id, &xres, &yres);
sizeentry = gimp_coordinates_new (unit, "%a", TRUE, FALSE, 6,
GIMP_SIZE_ENTRY_UPDATE_SIZE,
FALSE, FALSE,
_("_Width:"),
config.width, xres,
2, GIMP_MAX_IMAGE_SIZE,
2, GIMP_MAX_IMAGE_SIZE,
_("_Height:"),
config.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);
adj = gtk_adjustment_new (config.ntries, 1, 1024, 1, 10, 0);
spinbutton = gimp_spin_button_new (adj, 1, 0);
gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbutton), TRUE);
gimp_grid_attach_aligned (GTK_GRID (sizeentry), 0, 2,
_("_Search depth:"), 0.0, 0.5,
spinbutton, 1);
g_signal_connect (adj, "value-changed",
G_CALLBACK (gimp_int_adjustment_update),
&config.ntries);
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)
{
config.width = gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (sizeentry),
0);
config.height = gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (sizeentry),
1);
}
gtk_widget_destroy (dlg);
return run;
1997-11-24 22:05:25 +00:00
}