1997-11-24 22:05:25 +00:00
|
|
|
/*
|
2009-01-17 22:28:01 +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
|
2018-07-11 23:27:07 +02:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2009-01-17 22:28:01 +00:00
|
|
|
*/
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2000-01-08 15:23:28 +00:00
|
|
|
#include "config.h"
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2000-09-30 20:13:06 +00:00
|
|
|
#include <string.h>
|
2000-01-08 15:23:28 +00:00
|
|
|
|
|
|
|
#include <libgimp/gimp.h>
|
|
|
|
#include <libgimp/gimpui.h>
|
|
|
|
|
2000-01-07 17:18:44 +00:00
|
|
|
#include "libgimp/stdplugins-intl.h"
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2000-05-01 20:22:55 +00:00
|
|
|
|
2005-08-15 19:39:02 +00:00
|
|
|
#define PLUG_IN_PROC "plug-in-smooth-palette"
|
2008-03-24 15:29:55 +00:00
|
|
|
#define PLUG_IN_BINARY "smooth-palette"
|
2011-04-08 20:31:34 +02:00
|
|
|
#define PLUG_IN_ROLE "gimp-smooth-palette"
|
2005-08-15 19:39:02 +00:00
|
|
|
|
|
|
|
|
2019-09-02 16:10:12 +02:00
|
|
|
typedef struct _Palette Palette;
|
|
|
|
typedef struct _PaletteClass PaletteClass;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-09-02 16:10:12 +02:00
|
|
|
struct _Palette
|
|
|
|
{
|
|
|
|
GimpPlugIn parent_instance;
|
|
|
|
};
|
2003-11-06 15:27:05 +00:00
|
|
|
|
2019-09-02 16:10:12 +02:00
|
|
|
struct _PaletteClass
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2019-09-02 16:10:12 +02:00
|
|
|
GimpPlugInClass parent_class;
|
1997-11-24 22:05:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-09-02 16:10:12 +02:00
|
|
|
#define PALETTE_TYPE (palette_get_type ())
|
2023-10-18 18:29:37 +02:00
|
|
|
#define PALETTE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PALETTE_TYPE, Palette))
|
2019-09-02 16:10:12 +02:00
|
|
|
|
|
|
|
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,
|
2021-04-02 02:55:46 +02:00
|
|
|
GimpDrawable **drawables,
|
2023-07-27 12:47:15 +00:00
|
|
|
GimpProcedureConfig *config,
|
2019-09-02 16:10:12 +02:00
|
|
|
gpointer run_data);
|
|
|
|
|
2023-07-27 12:47:15 +00:00
|
|
|
static gboolean dialog (GimpProcedure *procedure,
|
|
|
|
GimpProcedureConfig *config,
|
|
|
|
GimpDrawable *drawable);
|
2019-09-02 16:10:12 +02:00
|
|
|
|
2023-07-27 12:47:15 +00:00
|
|
|
static GimpImage * smooth_palette (GimpProcedureConfig *config,
|
|
|
|
GimpDrawable *drawable,
|
2019-09-02 16:10:12 +02:00
|
|
|
GimpLayer **layer);
|
|
|
|
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (Palette, palette, GIMP_TYPE_PLUG_IN)
|
|
|
|
|
|
|
|
GIMP_MAIN (PALETTE_TYPE)
|
2022-05-26 00:59:36 +02:00
|
|
|
DEFINE_STD_SET_I18N
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2023-07-27 12:47:15 +00:00
|
|
|
#define TRY_SIZE 10000
|
2019-09-02 16:10:12 +02:00
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
static void
|
2019-09-02 16:10:12 +02:00
|
|
|
palette_class_init (PaletteClass *klass)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2019-09-02 16:10:12 +02:00
|
|
|
GimpPlugInClass *plug_in_class = GIMP_PLUG_IN_CLASS (klass);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-09-02 16:10:12 +02:00
|
|
|
plug_in_class->query_procedures = palette_query_procedures;
|
|
|
|
plug_in_class->create_procedure = palette_create_procedure;
|
2022-05-26 00:59:36 +02:00
|
|
|
plug_in_class->set_i18n = STD_SET_I18N;
|
2019-09-02 16:10:12 +02:00
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-09-02 16:10:12 +02:00
|
|
|
static void
|
|
|
|
palette_init (Palette *palette)
|
|
|
|
{
|
|
|
|
}
|
2003-03-25 16:38:19 +00:00
|
|
|
|
2019-09-02 16:10:12 +02:00
|
|
|
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
|
|
|
|
2019-09-02 16:10:12 +02:00
|
|
|
static GimpProcedure *
|
|
|
|
palette_create_procedure (GimpPlugIn *plug_in,
|
|
|
|
const gchar *name)
|
|
|
|
{
|
|
|
|
GimpProcedure *procedure = NULL;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-09-02 16:10:12 +02:00
|
|
|
if (! strcmp (name, PLUG_IN_PROC))
|
|
|
|
{
|
2023-10-01 18:23:57 +02:00
|
|
|
procedure = gimp_image_procedure_new (plug_in, name,
|
|
|
|
GIMP_PDB_PROC_TYPE_PLUGIN,
|
|
|
|
palette_run, NULL, NULL);
|
2019-09-02 16:10:12 +02:00
|
|
|
|
|
|
|
gimp_procedure_set_image_types (procedure, "RGB*");
|
2021-04-02 02:55:46 +02:00
|
|
|
gimp_procedure_set_sensitivity_mask (procedure,
|
|
|
|
GIMP_PROCEDURE_SENSITIVE_DRAWABLE);
|
2019-09-02 16:10:12 +02:00
|
|
|
|
2022-07-04 22:50:53 +02:00
|
|
|
gimp_procedure_set_menu_label (procedure, _("Smoo_th Palette..."));
|
2019-09-02 16:10:12 +02:00
|
|
|
gimp_procedure_add_menu_path (procedure, "<Image>/Colors/Info");
|
|
|
|
|
|
|
|
gimp_procedure_set_documentation (procedure,
|
2022-07-04 22:50:53 +02:00
|
|
|
_("Derive a smooth color palette "
|
|
|
|
"from the image"),
|
2019-09-02 16:10:12 +02:00
|
|
|
"help!",
|
|
|
|
name);
|
|
|
|
gimp_procedure_set_attribution (procedure,
|
|
|
|
"Scott Draves",
|
|
|
|
"Scott Draves",
|
|
|
|
"1997");
|
|
|
|
|
2024-06-12 16:53:12 +00:00
|
|
|
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);
|
2019-09-02 16:10:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return procedure;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GimpValueArray *
|
|
|
|
palette_run (GimpProcedure *procedure,
|
|
|
|
GimpRunMode run_mode,
|
|
|
|
GimpImage *image,
|
2021-04-02 02:55:46 +02:00
|
|
|
GimpDrawable **drawables,
|
2023-07-27 12:47:15 +00:00
|
|
|
GimpProcedureConfig *config,
|
2019-09-02 16:10:12 +02:00
|
|
|
gpointer run_data)
|
|
|
|
{
|
2023-07-27 12:47:15 +00:00
|
|
|
GimpImage *new_image;
|
|
|
|
GimpLayer *new_layer;
|
|
|
|
GimpDrawable *drawable;
|
|
|
|
gboolean show_image;
|
2019-09-02 16:10:12 +02:00
|
|
|
|
|
|
|
gegl_init (NULL, NULL);
|
2002-12-27 16:25:07 +00:00
|
|
|
|
2024-10-28 12:16:13 +01:00
|
|
|
if (gimp_core_object_array_get_length ((GObject **) drawables) != 1)
|
2021-04-02 02:55:46 +02:00
|
|
|
{
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2023-07-27 12:47:15 +00:00
|
|
|
if (run_mode == GIMP_RUN_INTERACTIVE && ! dialog (procedure, config, drawable))
|
2000-01-08 15:23:28 +00:00
|
|
|
{
|
2023-07-27 12:47:15 +00:00
|
|
|
return gimp_procedure_new_return_values (procedure,
|
|
|
|
GIMP_PDB_CANCEL,
|
|
|
|
NULL);
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
2019-09-02 16:10:12 +02:00
|
|
|
if (gimp_drawable_is_rgb (drawable))
|
2000-01-08 15:23:28 +00:00
|
|
|
{
|
2019-09-02 16:10:12 +02:00
|
|
|
gimp_progress_init (_("Deriving smooth palette"));
|
2003-06-13 14:37:00 +00:00
|
|
|
|
2023-07-27 12:47:15 +00:00
|
|
|
new_image = smooth_palette (config, drawable, &new_layer);
|
2005-08-15 19:39:02 +00:00
|
|
|
|
2023-07-27 12:47:15 +00:00
|
|
|
g_object_get (config, "show-image", &show_image, NULL);
|
|
|
|
if (show_image)
|
2019-09-02 16:10:12 +02:00
|
|
|
gimp_display_new (new_image);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return gimp_procedure_new_return_values (procedure,
|
|
|
|
GIMP_PDB_EXECUTION_ERROR,
|
|
|
|
NULL);
|
2003-06-13 14:37:00 +00:00
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2023-07-27 12:47:15 +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,
|
2004-07-28 12:10:20 +00:00
|
|
|
guint bpp,
|
|
|
|
gint i,
|
|
|
|
gint j)
|
2000-01-08 15:23:28 +00:00
|
|
|
{
|
2016-11-21 11:25:10 +01:00
|
|
|
gfloat r = 0.f;
|
2004-07-28 12:10:20 +00:00
|
|
|
guint k;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2000-01-08 15:23:28 +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];
|
2000-01-08 15:23:28 +00:00
|
|
|
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,
|
2004-07-28 12:10:20 +00:00
|
|
|
guint bpp,
|
|
|
|
gint i,
|
|
|
|
gint j)
|
2000-01-08 15:23:28 +00:00
|
|
|
{
|
2004-07-28 12:10:20 +00:00
|
|
|
guint k;
|
2000-01-08 15:23:28 +00:00
|
|
|
|
|
|
|
for (k = 0; k < bpp; k++)
|
|
|
|
{
|
2016-11-21 11:25:10 +01:00
|
|
|
gfloat t = pal[j * bpp + k];
|
2000-01-08 15:23:28 +00:00
|
|
|
pal[j * bpp + k] = pal[i * bpp + k];
|
|
|
|
pal[i * bpp + k] = t;
|
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
2019-09-02 16:10:12 +02:00
|
|
|
static GimpImage *
|
2023-07-27 12:47:15 +00:00
|
|
|
smooth_palette (GimpProcedureConfig *config,
|
|
|
|
GimpDrawable *drawable,
|
|
|
|
GimpLayer **layer)
|
2000-01-08 15:23:28 +00:00
|
|
|
{
|
2019-09-02 16:10:12 +02:00
|
|
|
GimpImage *new_image;
|
2004-07-28 12:10:20 +00:00
|
|
|
gint psize, i, j;
|
2016-11-21 11:25:10 +01:00
|
|
|
guint bpp;
|
2015-08-29 21:04:09 +10:00
|
|
|
gint sel_x1, sel_y1;
|
2004-07-28 12:10:20 +00:00
|
|
|
gint width, height;
|
2023-07-27 12:47:15 +00:00
|
|
|
gint config_width;
|
|
|
|
gint config_height;
|
|
|
|
gint config_n_tries;
|
2016-11-21 11:25:10 +01:00
|
|
|
GeglBuffer *buffer;
|
2018-05-13 19:39:16 -04:00
|
|
|
GeglSampler *sampler;
|
2016-11-21 11:25:10 +01:00
|
|
|
gfloat *pal;
|
2004-07-28 12:10:20 +00:00
|
|
|
GRand *gr;
|
2003-11-06 15:27:05 +00:00
|
|
|
|
2016-11-21 11:25:10 +01:00
|
|
|
const Babl *format = babl_format ("RGB float");
|
|
|
|
|
2023-07-27 12:47:15 +00:00
|
|
|
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,
|
2019-09-02 16:10:12 +02:00
|
|
|
GIMP_RGB,
|
|
|
|
GIMP_PRECISION_FLOAT_LINEAR);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-09-02 16:10:12 +02:00
|
|
|
gimp_image_undo_disable (new_image);
|
2016-11-21 11:25:10 +01:00
|
|
|
|
2019-09-02 16:10:12 +02:00
|
|
|
*layer = gimp_layer_new (new_image, _("Background"),
|
2023-07-27 12:47:15 +00:00
|
|
|
config_width, config_height,
|
2019-09-02 16:10:12 +02:00
|
|
|
gimp_drawable_type (drawable),
|
|
|
|
100,
|
|
|
|
gimp_image_get_default_new_layer_mode (new_image));
|
2016-11-21 11:25:10 +01:00
|
|
|
|
2019-09-02 16:10:12 +02:00
|
|
|
gimp_image_insert_layer (new_image, *layer, NULL, 0);
|
2016-11-21 11:25:10 +01:00
|
|
|
|
2019-09-02 16:10:12 +02:00
|
|
|
if (! gimp_drawable_mask_intersect (drawable,
|
2016-11-21 11:25:10 +01:00
|
|
|
&sel_x1, &sel_y1, &width, &height))
|
2019-09-02 16:10:12 +02:00
|
|
|
return new_image;
|
2016-11-21 11:25:10 +01:00
|
|
|
|
|
|
|
gr = g_rand_new ();
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2023-07-27 12:47:15 +00:00
|
|
|
psize = config_width;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-09-02 16:10:12 +02:00
|
|
|
buffer = gimp_drawable_get_buffer (drawable);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2018-05-13 19:39:16 -04: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));
|
2002-05-24 14:50:08 +00:00
|
|
|
|
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
|
|
|
|
2000-01-08 15:23:28 +00: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);
|
2000-05-01 20:22:55 +00:00
|
|
|
|
2018-05-13 19:39:16 -04:00
|
|
|
gegl_sampler_get (sampler,
|
|
|
|
(gdouble) x, (gdouble) y, NULL, pal + i * bpp,
|
|
|
|
GEGL_ABYSS_NONE);
|
2000-01-08 15:23:28 +00:00
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2018-05-13 19:39:16 -04: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 */
|
2000-01-08 15:23:28 +00:00
|
|
|
if (1)
|
|
|
|
{
|
2016-11-21 11:25:10 +01:00
|
|
|
gfloat *pal_best;
|
|
|
|
gfloat *original;
|
2000-05-01 20:22:55 +00:00
|
|
|
gdouble len_best = 0;
|
|
|
|
gint try;
|
2000-01-08 15:23:28 +00:00
|
|
|
|
2021-08-26 17:18:32 +02:00
|
|
|
pal_best = g_memdup2 (pal, bpp * psize);
|
|
|
|
original = g_memdup2 (pal, bpp * psize);
|
2000-01-08 15:23:28 +00:00
|
|
|
|
2023-07-27 12:47:15 +00:00
|
|
|
for (try = 0; try < config_n_tries; try++)
|
2004-07-28 12:10:20 +00:00
|
|
|
{
|
|
|
|
gdouble len;
|
|
|
|
|
2023-07-27 12:47:15 +00:00
|
|
|
if (! (try % 5))
|
|
|
|
gimp_progress_update (try / (double) config_n_tries);
|
2004-07-28 12:10:20 +00:00
|
|
|
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 */
|
2023-07-27 12:47:15 +00:00
|
|
|
for (i = 0; i < TRY_SIZE; i++)
|
2004-07-28 12:10:20 +00:00
|
|
|
{
|
|
|
|
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;
|
2004-07-28 12:10:20 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2011-04-10 19:05:08 +02:00
|
|
|
gimp_progress_update (1.0);
|
2000-01-17 17:02:26 +00:00
|
|
|
memcpy (pal, pal_best, bpp * psize);
|
|
|
|
g_free (pal_best);
|
|
|
|
g_free (original);
|
2016-11-21 11:25:10 +01:00
|
|
|
|
2000-01-08 15:23:28 +00:00
|
|
|
/* clean */
|
|
|
|
for (i = 1; i < 4 * psize; i++)
|
2004-07-28 12:10:20 +00:00
|
|
|
{
|
2016-11-21 11:25:10 +01:00
|
|
|
gfloat as_is, swapd;
|
2004-07-28 12:10:20 +00:00
|
|
|
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
|
|
|
|
2004-07-28 12:10:20 +00: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
|
|
|
|
2019-09-02 16:10:12 +02:00
|
|
|
buffer = gimp_drawable_get_buffer (GIMP_DRAWABLE (*layer));
|
2016-11-21 11:25:10 +01:00
|
|
|
|
2023-07-27 12:47:15 +00:00
|
|
|
for (j = 0; j < config_height; j++)
|
2016-11-21 11:25:10 +01:00
|
|
|
{
|
2023-07-27 12:47:15 +00: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);
|
|
|
|
|
2019-09-02 16:10:12 +02:00
|
|
|
gimp_drawable_update (GIMP_DRAWABLE (*layer), 0, 0,
|
2023-07-27 12:47:15 +00:00
|
|
|
config_width, config_height);
|
2019-09-02 16:10:12 +02:00
|
|
|
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);
|
|
|
|
|
2019-09-02 16:10:12 +02:00
|
|
|
return new_image;
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
2002-05-24 14:50:08 +00:00
|
|
|
static gboolean
|
2023-07-27 12:47:15 +00:00
|
|
|
dialog (GimpProcedure *procedure,
|
|
|
|
GimpProcedureConfig *config,
|
|
|
|
GimpDrawable *drawable)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2016-05-18 09:49:22 +02:00
|
|
|
GtkWidget *dlg;
|
|
|
|
GtkWidget *sizeentry;
|
2019-09-02 16:10:12 +02:00
|
|
|
GimpImage *image;
|
Issue #8900 and #9923: reimplementing GimpUnit as a proper class.
This fixes all our GObject Introspection issues with GimpUnit which was
both an enum and an int-derived type of user-defined units *completing*
the enum values. GIR clearly didn't like this!
Now GimpUnit is a proper class and units are unique objects, allowing to
compare them with an identity test (i.e. `unit == gimp_unit_pixel ()`
tells us if unit is the pixel unit or not), which makes it easy to use,
just like with int, yet adding also methods, making for nicer
introspected API.
As an aside, this also fixes #10738, by having all the built-in units
retrievable even if libgimpbase had not been properly initialized with
gimp_base_init().
I haven't checked in details how GIR works to introspect, but it looks
like it loads the library to inspect and runs functions, hence
triggering some CRITICALS because virtual methods (supposed to be
initialized with gimp_base_init() run by libgimp) are not set. This new
code won't trigger any critical because the vtable method are now not
necessary, at least for all built-in units.
Note that GimpUnit is still in libgimpbase. It could have been moved to
libgimp in order to avoid any virtual method table (since we need to
keep core and libgimp side's units in sync, PDB is required), but too
many libgimpwidgets widgets were already using GimpUnit. And technically
most of GimpUnit logic doesn't require PDB (only the creation/sync
part). This is one of the reasons why user-created GimpUnit list is
handled and stored differently from other types of objects.
Globally this simplifies the code a lot too and we don't need separate
implementations of various utils for core and libgimp, which means less
prone to errors.
2024-07-25 20:55:21 +02:00
|
|
|
GimpUnit *unit;
|
2016-05-18 09:49:22 +02:00
|
|
|
gdouble xres, yres;
|
2023-07-27 12:47:15 +00:00
|
|
|
gint width;
|
|
|
|
gint height;
|
2016-05-18 09:49:22 +02:00
|
|
|
gboolean run;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2023-07-27 12:47:15 +00:00
|
|
|
g_object_get (config,
|
|
|
|
"width", &width,
|
|
|
|
"height", &height,
|
|
|
|
NULL);
|
2001-11-29 13:23:44 +00:00
|
|
|
|
2023-07-27 12:47:15 +00:00
|
|
|
gimp_ui_init (PLUG_IN_BINARY);
|
2000-01-08 15:23:28 +00:00
|
|
|
|
2023-07-27 12:47:15 +00:00
|
|
|
dlg = gimp_procedure_dialog_new (procedure,
|
|
|
|
GIMP_PROCEDURE_CONFIG (config),
|
|
|
|
_("Smooth Palette"));
|
2000-01-08 15:23:28 +00:00
|
|
|
|
2018-05-10 17:04:37 +02:00
|
|
|
gimp_dialog_set_alternative_button_order (GTK_DIALOG (dlg),
|
2023-07-27 12:47:15 +00:00
|
|
|
GTK_RESPONSE_OK,
|
|
|
|
GTK_RESPONSE_CANCEL,
|
|
|
|
-1);
|
2005-02-08 20:40:33 +00:00
|
|
|
|
2005-09-09 18:07:31 +00:00
|
|
|
gimp_window_set_transient (GTK_WINDOW (dlg));
|
2005-09-05 21:40:29 +00:00
|
|
|
|
2019-09-02 16:10:12 +02:00
|
|
|
image = gimp_item_get_image (GIMP_ITEM (drawable));
|
|
|
|
unit = gimp_image_get_unit (image);
|
|
|
|
gimp_image_get_resolution (image, &xres, &yres);
|
2002-12-27 16:25:07 +00:00
|
|
|
|
|
|
|
sizeentry = gimp_coordinates_new (unit, "%a", TRUE, FALSE, 6,
|
2004-07-28 12:10:20 +00:00
|
|
|
GIMP_SIZE_ENTRY_UPDATE_SIZE,
|
|
|
|
FALSE, FALSE,
|
|
|
|
|
|
|
|
_("_Width:"),
|
2023-07-27 12:47:15 +00:00
|
|
|
width, xres,
|
2004-07-28 12:10:20 +00:00
|
|
|
2, GIMP_MAX_IMAGE_SIZE,
|
|
|
|
2, GIMP_MAX_IMAGE_SIZE,
|
|
|
|
|
|
|
|
_("_Height:"),
|
2023-07-27 12:47:15 +00:00
|
|
|
height, yres,
|
2004-07-28 12:10:20 +00:00
|
|
|
1, GIMP_MAX_IMAGE_SIZE,
|
|
|
|
1, GIMP_MAX_IMAGE_SIZE);
|
2004-05-19 23:54:40 +00:00
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (sizeentry), 12);
|
2009-07-15 18:57:12 +02:00
|
|
|
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dlg))),
|
|
|
|
sizeentry, FALSE, FALSE, 0);
|
|
|
|
gtk_widget_show (sizeentry);
|
2000-01-17 17:02:26 +00:00
|
|
|
|
2023-07-27 12:47:15 +00:00
|
|
|
/* We don't want to have the "Show Image" option in the GUI */
|
|
|
|
gimp_procedure_dialog_fill (GIMP_PROCEDURE_DIALOG (dlg), "n-tries", NULL);
|
2000-01-08 15:23:28 +00:00
|
|
|
|
|
|
|
gtk_widget_show (dlg);
|
|
|
|
|
2003-11-11 18:11:56 +00:00
|
|
|
run = (gimp_dialog_run (GIMP_DIALOG (dlg)) == GTK_RESPONSE_OK);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2003-11-06 15:27:05 +00:00
|
|
|
if (run)
|
|
|
|
{
|
2023-07-27 12:47:15 +00:00
|
|
|
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);
|
2003-11-06 15:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gtk_widget_destroy (dlg);
|
|
|
|
|
|
|
|
return run;
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|