2004-04-20 20:21:17 +08:00
|
|
|
/* align_layers.c
|
1997-11-25 06:05:25 +08:00
|
|
|
* Author: Shuji Narazaki <narazaki@InetQ.or.jp>
|
1998-01-25 17:29:29 +08:00
|
|
|
* Version: 0.26
|
1997-11-25 06:05:25 +08:00
|
|
|
*
|
1998-01-25 17:29:29 +08:00
|
|
|
* Copyright (C) 1997-1998 Shuji Narazaki <narazaki@InetQ.or.jp>
|
1997-11-25 06:05:25 +08:00
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
1997-11-25 06:05:25 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
1997-11-25 06:05:25 +08:00
|
|
|
* (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-12 05:27:07 +08:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
1997-11-25 06:05:25 +08:00
|
|
|
*/
|
|
|
|
|
1999-12-18 05:24:24 +08:00
|
|
|
#include "config.h"
|
2000-01-07 00:40:17 +08:00
|
|
|
|
2000-01-13 23:39:26 +08:00
|
|
|
#include <libgimp/gimp.h>
|
|
|
|
#include <libgimp/gimpui.h>
|
|
|
|
|
2000-01-07 00:40:17 +08:00
|
|
|
#include "libgimp/stdplugins-intl.h"
|
|
|
|
|
2005-08-14 02:29:14 +08:00
|
|
|
#define PLUG_IN_PROC "plug-in-align-layers"
|
2008-03-24 23:29:55 +08:00
|
|
|
#define PLUG_IN_BINARY "align-layers"
|
2011-04-09 02:31:34 +08:00
|
|
|
#define PLUG_IN_ROLE "gimp-align-layers"
|
2000-01-28 03:26:00 +08:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
H_NONE,
|
|
|
|
H_COLLECT,
|
|
|
|
LEFT2RIGHT,
|
|
|
|
RIGHT2LEFT,
|
|
|
|
SNAP2HGRID
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
H_BASE_LEFT,
|
|
|
|
H_BASE_CENTER,
|
|
|
|
H_BASE_RIGHT
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-01-28 03:26:00 +08:00
|
|
|
V_NONE,
|
|
|
|
V_COLLECT,
|
|
|
|
TOP2BOTTOM,
|
|
|
|
BOTTOM2TOP,
|
|
|
|
SNAP2VGRID
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
V_BASE_TOP,
|
|
|
|
V_BASE_CENTER,
|
|
|
|
V_BASE_BOTTOM
|
|
|
|
};
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2013-02-22 01:31:12 +08:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
gint step_x;
|
|
|
|
gint step_y;
|
|
|
|
gint base_x;
|
|
|
|
gint base_y;
|
|
|
|
} AlignData;
|
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
typedef struct _AlignLayers AlignLayers;
|
|
|
|
typedef struct _AlignLayersClass AlignLayersClass;
|
2013-02-22 01:31:12 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
struct _AlignLayers
|
|
|
|
{
|
|
|
|
GimpPlugIn parent_instance;
|
|
|
|
};
|
2013-02-22 01:31:12 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
struct _AlignLayersClass
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2019-08-28 17:50:38 +08:00
|
|
|
GimpPlugInClass parent_class;
|
1997-11-25 06:05:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
#define ALIGN_LAYERS_TYPE (align_layers_get_type ())
|
2023-10-19 00:29:37 +08:00
|
|
|
#define ALIGN_LAYERS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ALIGN_LAYERS_TYPE, AlignLayers))
|
2019-08-28 17:50:38 +08:00
|
|
|
|
|
|
|
GType align_layers_get_type (void) G_GNUC_CONST;
|
|
|
|
|
|
|
|
static GList * align_layers_query_procedures (GimpPlugIn *plug_in);
|
|
|
|
static GimpProcedure * align_layers_create_procedure (GimpPlugIn *plug_in,
|
|
|
|
const gchar *name);
|
|
|
|
|
|
|
|
static GimpValueArray * align_layers_run (GimpProcedure *procedure,
|
|
|
|
GimpRunMode run_mode,
|
|
|
|
GimpImage *image,
|
2021-04-02 08:55:46 +08:00
|
|
|
gint n_drawables,
|
|
|
|
GimpDrawable **drawables,
|
2023-06-21 07:09:41 +08:00
|
|
|
GimpProcedureConfig *config,
|
2019-08-28 17:50:38 +08:00
|
|
|
gpointer run_data);
|
|
|
|
|
|
|
|
|
|
|
|
/* Main function */
|
2023-03-16 11:23:26 +08:00
|
|
|
static GimpPDBStatusType align_layers (GimpImage *image,
|
|
|
|
GObject *config);
|
2019-08-28 17:50:38 +08:00
|
|
|
|
|
|
|
/* Helpers and internal functions */
|
|
|
|
static gint align_layers_count_visibles_layers (GList *layers);
|
|
|
|
static GimpLayer * align_layers_find_last_layer (GList *layers,
|
|
|
|
gboolean *found);
|
|
|
|
static gint align_layers_spread_visibles_layers (GList *layers,
|
|
|
|
GimpLayer **layers_array);
|
|
|
|
static GimpLayer ** align_layers_spread_image (GimpImage *image,
|
|
|
|
gint *layer_num);
|
|
|
|
static GimpLayer * align_layers_find_background (GimpImage *image);
|
|
|
|
static AlignData align_layers_gather_data (GimpLayer **layers,
|
|
|
|
gint layer_num,
|
2023-03-16 11:23:26 +08:00
|
|
|
GimpLayer *background,
|
|
|
|
GObject *config);
|
2019-08-28 17:50:38 +08:00
|
|
|
static void align_layers_perform_alignment (GimpLayer **layers,
|
|
|
|
gint layer_num,
|
2023-03-16 11:23:26 +08:00
|
|
|
AlignData data,
|
|
|
|
GObject *config);
|
2019-08-28 17:50:38 +08:00
|
|
|
static void align_layers_get_align_offsets (GimpDrawable *drawable,
|
|
|
|
gint *x,
|
2023-03-16 11:23:26 +08:00
|
|
|
gint *y,
|
|
|
|
GObject *config);
|
|
|
|
static gint align_layers_dialog (GimpProcedure *procedure,
|
|
|
|
GObject *config);
|
2019-08-28 17:50:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (AlignLayers, align_layers, GIMP_TYPE_PLUG_IN)
|
|
|
|
|
|
|
|
GIMP_MAIN (ALIGN_LAYERS_TYPE)
|
2022-05-26 06:59:36 +08:00
|
|
|
DEFINE_STD_SET_I18N
|
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
align_layers_class_init (AlignLayersClass *klass)
|
|
|
|
{
|
|
|
|
GimpPlugInClass *plug_in_class = GIMP_PLUG_IN_CLASS (klass);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
plug_in_class->query_procedures = align_layers_query_procedures;
|
|
|
|
plug_in_class->create_procedure = align_layers_create_procedure;
|
2022-05-26 06:59:36 +08:00
|
|
|
plug_in_class->set_i18n = STD_SET_I18N;
|
2019-08-28 17:50:38 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
static void
|
2019-08-28 17:50:38 +08:00
|
|
|
align_layers_init (AlignLayers *film)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
static GList *
|
|
|
|
align_layers_query_procedures (GimpPlugIn *plug_in)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2019-08-28 17:50:38 +08:00
|
|
|
return g_list_append (NULL, g_strdup (PLUG_IN_PROC));
|
|
|
|
}
|
2001-12-29 21:26:29 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
static GimpProcedure *
|
|
|
|
align_layers_create_procedure (GimpPlugIn *plug_in,
|
|
|
|
const gchar *name)
|
|
|
|
{
|
|
|
|
GimpProcedure *procedure = NULL;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
if (! strcmp (name, PLUG_IN_PROC))
|
|
|
|
{
|
2023-10-02 00:23:57 +08:00
|
|
|
procedure = gimp_image_procedure_new (plug_in, name,
|
|
|
|
GIMP_PDB_PROC_TYPE_PLUGIN,
|
|
|
|
align_layers_run, NULL, NULL);
|
2019-08-28 17:50:38 +08:00
|
|
|
|
|
|
|
gimp_procedure_set_image_types (procedure, "*");
|
2021-04-02 08:55:46 +08:00
|
|
|
gimp_procedure_set_sensitivity_mask (procedure,
|
|
|
|
GIMP_PROCEDURE_SENSITIVE_DRAWABLE |
|
|
|
|
GIMP_PROCEDURE_SENSITIVE_DRAWABLES |
|
|
|
|
GIMP_PROCEDURE_SENSITIVE_NO_DRAWABLES);
|
2019-08-28 17:50:38 +08:00
|
|
|
|
2022-07-05 04:50:53 +08:00
|
|
|
gimp_procedure_set_menu_label (procedure, _("Align Visi_ble Layers..."));
|
2023-07-13 21:34:40 +08:00
|
|
|
gimp_procedure_add_menu_path (procedure, "<Image>/Image/[Arrange]");
|
2019-08-28 17:50:38 +08:00
|
|
|
|
|
|
|
gimp_procedure_set_documentation (procedure,
|
2022-07-05 04:50:53 +08:00
|
|
|
_("Align all visible layers of the image"),
|
2024-07-27 02:56:53 +08:00
|
|
|
_("Align visible layers"),
|
2019-08-28 17:50:38 +08:00
|
|
|
name);
|
|
|
|
gimp_procedure_set_attribution (procedure,
|
|
|
|
"Shuji Narazaki <narazaki@InetQ.or.jp>",
|
|
|
|
"Shuji Narazaki",
|
|
|
|
"1997");
|
|
|
|
|
2024-07-27 02:56:53 +08:00
|
|
|
gimp_procedure_add_choice_argument (procedure, "horizontal-style",
|
|
|
|
_("_Horizontal style"),
|
|
|
|
"",
|
|
|
|
gimp_choice_new_with_values ("none", H_NONE, _("None"), NULL,
|
|
|
|
"collect", H_COLLECT, _("Collect"), NULL,
|
|
|
|
"fill-left-to-right", LEFT2RIGHT, _("Fill (left to right)"), NULL,
|
|
|
|
"fill-right-to-left", RIGHT2LEFT, _("Fill (right to left)"), NULL,
|
|
|
|
"snap-to-grid", SNAP2HGRID, _("Snap to grid"), NULL,
|
|
|
|
NULL),
|
|
|
|
"none",
|
|
|
|
G_PARAM_READWRITE);
|
|
|
|
|
|
|
|
gimp_procedure_add_choice_argument (procedure, "horizontal-base",
|
|
|
|
_("Hori_zontal base"),
|
|
|
|
"",
|
|
|
|
gimp_choice_new_with_values ("left-edge", H_BASE_LEFT, _("Left edge"), NULL,
|
|
|
|
"center", H_BASE_CENTER, _("Center"), NULL,
|
|
|
|
"right-edge", H_BASE_RIGHT, _("Right edge"), NULL,
|
|
|
|
NULL),
|
|
|
|
"left-edge",
|
|
|
|
G_PARAM_READWRITE);
|
|
|
|
|
|
|
|
gimp_procedure_add_choice_argument (procedure, "vertical-style",
|
|
|
|
_("_Vertical style"),
|
|
|
|
"",
|
|
|
|
gimp_choice_new_with_values ("none", V_NONE, _("None"), NULL,
|
|
|
|
"collect", V_COLLECT, _("Collect"), NULL,
|
|
|
|
"fill-left-to-right", TOP2BOTTOM, _("Fill (top to bottom)"), NULL,
|
|
|
|
"fill-right-to-left", BOTTOM2TOP, _("Fill (bottom to top)"), NULL,
|
|
|
|
"snap-to-grid", SNAP2VGRID, _("Snap to grid"), NULL,
|
|
|
|
NULL),
|
|
|
|
"none",
|
|
|
|
G_PARAM_READWRITE);
|
|
|
|
|
|
|
|
gimp_procedure_add_choice_argument (procedure, "vertical-base",
|
|
|
|
_("Ver_tical base"),
|
|
|
|
"",
|
|
|
|
gimp_choice_new_with_values ("top-edge", V_BASE_TOP, _("Top edge"), NULL,
|
|
|
|
"center", V_BASE_CENTER, _("Center"), NULL,
|
|
|
|
"bottom-edge", V_BASE_BOTTOM, _("Bottom edge"), NULL,
|
|
|
|
NULL),
|
|
|
|
"top-edge",
|
|
|
|
G_PARAM_READWRITE);
|
2024-06-13 00:53:12 +08:00
|
|
|
|
|
|
|
gimp_procedure_add_int_argument (procedure, "grid-size",
|
|
|
|
_("_Grid"),
|
|
|
|
_("Grid"),
|
|
|
|
1, 200, 10,
|
|
|
|
GIMP_PARAM_READWRITE);
|
|
|
|
|
|
|
|
gimp_procedure_add_boolean_argument (procedure,
|
|
|
|
"ignore-bottom-layer",
|
|
|
|
_("Ignore the _bottom layer even if visible"),
|
|
|
|
_("Ignore the bottom layer even if visible"),
|
|
|
|
TRUE,
|
|
|
|
G_PARAM_READWRITE);
|
|
|
|
|
|
|
|
gimp_procedure_add_boolean_argument (procedure,
|
|
|
|
"use-bottom-layer",
|
|
|
|
_("_Use the (invisible) bottom layer as the base"),
|
|
|
|
_("Use the (invisible) bottom layer as the base"),
|
|
|
|
FALSE,
|
|
|
|
G_PARAM_READWRITE);
|
2023-03-16 11:23:26 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
/* TODO: these 2 arguments existed in the original procedure but
|
|
|
|
* were actually unused. If we want to keep them, let's at least
|
|
|
|
* implement the documented behavior, or else let's just drop
|
|
|
|
* them.
|
|
|
|
*/
|
|
|
|
/*
|
2024-06-13 00:53:12 +08:00
|
|
|
gimp_procedure_add_boolean_argument (procedure,
|
|
|
|
"link-after-alignment",
|
|
|
|
"Link the visible layers after alignment",
|
|
|
|
FALSE,
|
|
|
|
G_PARAM_READWRITE);
|
|
|
|
gimp_procedure_add_boolean_argument (procedure,
|
|
|
|
"use-bottom",
|
|
|
|
"use the bottom layer as the base of alignment",
|
|
|
|
TRUE,
|
|
|
|
G_PARAM_READWRITE);
|
2019-08-28 17:50:38 +08:00
|
|
|
*/
|
|
|
|
}
|
2003-03-26 00:38:19 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
return procedure;
|
|
|
|
}
|
2003-10-21 07:32:20 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
static GimpValueArray *
|
|
|
|
align_layers_run (GimpProcedure *procedure,
|
|
|
|
GimpRunMode run_mode,
|
|
|
|
GimpImage *image,
|
2021-04-02 08:55:46 +08:00
|
|
|
gint n_drawables,
|
|
|
|
GimpDrawable **drawables,
|
2023-06-21 07:09:41 +08:00
|
|
|
GimpProcedureConfig *config,
|
2019-08-28 17:50:38 +08:00
|
|
|
gpointer run_data)
|
|
|
|
{
|
2023-06-21 07:09:41 +08:00
|
|
|
GimpValueArray *return_vals = NULL;
|
|
|
|
GimpPDBStatusType status = GIMP_PDB_EXECUTION_ERROR;
|
|
|
|
GError *error = NULL;
|
|
|
|
GList *layers;
|
|
|
|
gint layer_num;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
switch ( run_mode )
|
|
|
|
{
|
2000-08-22 09:26:57 +08:00
|
|
|
case GIMP_RUN_INTERACTIVE:
|
2019-08-28 17:50:38 +08:00
|
|
|
layers = gimp_image_list_layers (image);
|
|
|
|
layer_num = align_layers_count_visibles_layers (layers);
|
|
|
|
g_list_free (layers);
|
1997-11-25 06:05:25 +08:00
|
|
|
if (layer_num < 2)
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
2023-03-16 11:23:26 +08:00
|
|
|
g_set_error (&error, GIMP_PLUG_IN_ERROR, 0,
|
|
|
|
_("There are not enough layers to align."));
|
|
|
|
|
|
|
|
return gimp_procedure_new_return_values (procedure,
|
|
|
|
GIMP_PDB_CALLING_ERROR,
|
|
|
|
error);
|
2003-12-24 06:07:06 +08:00
|
|
|
}
|
2023-03-16 11:23:26 +08:00
|
|
|
|
|
|
|
if (! align_layers_dialog (procedure, G_OBJECT (config)))
|
2019-08-28 17:50:38 +08:00
|
|
|
status = GIMP_PDB_CANCEL;
|
1997-11-25 06:05:25 +08:00
|
|
|
break;
|
2003-03-26 00:38:19 +08:00
|
|
|
|
2000-08-22 09:26:57 +08:00
|
|
|
case GIMP_RUN_NONINTERACTIVE:
|
|
|
|
case GIMP_RUN_WITH_LAST_VALS:
|
1997-11-25 06:05:25 +08:00
|
|
|
break;
|
|
|
|
}
|
2000-01-28 03:26:00 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
if (status != GIMP_PDB_CANCEL)
|
|
|
|
{
|
2023-03-16 11:23:26 +08:00
|
|
|
status = align_layers (image, G_OBJECT (config));
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
if (run_mode != GIMP_RUN_NONINTERACTIVE)
|
|
|
|
gimp_displays_flush ();
|
|
|
|
}
|
2013-02-22 01:31:12 +08:00
|
|
|
|
2023-06-21 07:09:41 +08:00
|
|
|
return_vals = gimp_procedure_new_return_values (procedure, status, error);
|
2019-08-28 17:50:38 +08:00
|
|
|
|
|
|
|
return return_vals;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2013-02-22 01:31:12 +08:00
|
|
|
/*
|
|
|
|
* Main function
|
|
|
|
*/
|
2000-08-22 09:26:57 +08:00
|
|
|
static GimpPDBStatusType
|
2023-03-16 11:23:26 +08:00
|
|
|
align_layers (GimpImage *image,
|
|
|
|
GObject *config)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2023-03-16 11:23:26 +08:00
|
|
|
gint layer_num = 0;
|
2019-08-28 17:50:38 +08:00
|
|
|
GimpLayer **layers = NULL;
|
|
|
|
GimpLayer *background = 0;
|
2023-03-16 11:23:26 +08:00
|
|
|
AlignData data;
|
|
|
|
gboolean ignore_bottom_layer;
|
|
|
|
|
|
|
|
g_object_get (config,
|
|
|
|
"ignore-bottom-layer", &ignore_bottom_layer,
|
|
|
|
NULL);
|
2000-04-21 03:37:39 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
layers = align_layers_spread_image (image, &layer_num);
|
2013-02-22 01:31:12 +08:00
|
|
|
if (layer_num < 2)
|
2000-04-21 03:37:39 +08:00
|
|
|
{
|
2013-02-22 01:31:12 +08:00
|
|
|
g_free (layers);
|
|
|
|
return GIMP_PDB_EXECUTION_ERROR;
|
2000-04-21 03:37:39 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
background = align_layers_find_background (image);
|
2013-02-22 01:31:12 +08:00
|
|
|
|
|
|
|
/* If we want to ignore the bottom layer and if it's visible */
|
2023-03-16 11:23:26 +08:00
|
|
|
if (ignore_bottom_layer && background == layers[layer_num - 1])
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
layer_num--;
|
|
|
|
}
|
|
|
|
|
2013-02-22 01:31:12 +08:00
|
|
|
data = align_layers_gather_data (layers,
|
|
|
|
layer_num,
|
2023-03-16 11:23:26 +08:00
|
|
|
background,
|
|
|
|
config);
|
2013-02-22 01:31:12 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
gimp_image_undo_group_start (image);
|
2013-02-22 01:31:12 +08:00
|
|
|
|
|
|
|
align_layers_perform_alignment (layers,
|
|
|
|
layer_num,
|
2023-03-16 11:23:26 +08:00
|
|
|
data,
|
|
|
|
config);
|
2013-02-22 01:31:12 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
gimp_image_undo_group_end (image);
|
2013-02-22 01:31:12 +08:00
|
|
|
|
|
|
|
g_free (layers);
|
|
|
|
|
|
|
|
return GIMP_PDB_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the bottommost layer, visible or not
|
|
|
|
* The image must contain at least one layer.
|
|
|
|
*/
|
2019-08-28 17:50:38 +08:00
|
|
|
static GimpLayer *
|
|
|
|
align_layers_find_last_layer (GList *layers,
|
2013-02-22 01:31:12 +08:00
|
|
|
gboolean *found)
|
|
|
|
{
|
2019-08-28 17:50:38 +08:00
|
|
|
GList *last = g_list_last (layers);
|
2013-02-22 01:31:12 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
for (; last; last = last->prev)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2019-08-28 17:50:38 +08:00
|
|
|
GimpItem *item = last->data;
|
2003-10-21 07:32:20 +08:00
|
|
|
|
2013-02-22 01:31:12 +08:00
|
|
|
if (gimp_item_is_group (item))
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
2019-08-28 17:50:38 +08:00
|
|
|
GList *children;
|
|
|
|
GimpLayer *last_layer;
|
2013-02-22 01:31:12 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
children = gimp_item_list_children (item);
|
2013-02-22 01:31:12 +08:00
|
|
|
last_layer = align_layers_find_last_layer (children,
|
|
|
|
found);
|
2019-08-28 17:50:38 +08:00
|
|
|
g_list_free (children);
|
2013-02-22 01:31:12 +08:00
|
|
|
if (*found)
|
|
|
|
return last_layer;
|
|
|
|
}
|
|
|
|
else if (gimp_item_is_layer (item))
|
|
|
|
{
|
|
|
|
*found = TRUE;
|
2019-08-28 17:50:38 +08:00
|
|
|
return GIMP_LAYER (item);
|
2003-12-24 06:07:06 +08:00
|
|
|
}
|
2013-02-22 01:31:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* should never happen */
|
2019-08-28 17:50:38 +08:00
|
|
|
return NULL;
|
2013-02-22 01:31:12 +08:00
|
|
|
}
|
2000-04-21 03:37:39 +08:00
|
|
|
|
2013-02-22 01:31:12 +08:00
|
|
|
/*
|
|
|
|
* Return the bottom layer.
|
|
|
|
*/
|
2019-08-28 17:50:38 +08:00
|
|
|
static GimpLayer *
|
|
|
|
align_layers_find_background (GimpImage *image)
|
2013-02-22 01:31:12 +08:00
|
|
|
{
|
2019-08-28 17:50:38 +08:00
|
|
|
GList *layers;
|
|
|
|
GimpLayer *background;
|
|
|
|
gboolean found = FALSE;
|
|
|
|
|
|
|
|
layers = gimp_image_list_layers (image);
|
|
|
|
background = align_layers_find_last_layer (layers, &found);
|
|
|
|
g_list_free (layers);
|
2013-02-22 01:31:12 +08:00
|
|
|
|
|
|
|
return background;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill layers_array with all visible layers.
|
|
|
|
* layers_array needs to be allocated before the call
|
|
|
|
*/
|
|
|
|
static gint
|
2019-08-28 17:50:38 +08:00
|
|
|
align_layers_spread_visibles_layers (GList *layers,
|
|
|
|
GimpLayer **layers_array)
|
2013-02-22 01:31:12 +08:00
|
|
|
{
|
2019-08-28 17:50:38 +08:00
|
|
|
GList *iter;
|
|
|
|
gint index = 0;
|
2013-02-22 01:31:12 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
for (iter = layers; iter; iter = iter->next)
|
2013-02-22 01:31:12 +08:00
|
|
|
{
|
2019-08-28 17:50:38 +08:00
|
|
|
GimpItem *item = iter->data;
|
2013-02-22 01:31:12 +08:00
|
|
|
|
|
|
|
if (gimp_item_get_visible (item))
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
2013-02-22 01:31:12 +08:00
|
|
|
if (gimp_item_is_group (item))
|
|
|
|
{
|
2019-08-28 17:50:38 +08:00
|
|
|
GList *children;
|
2013-02-22 01:31:12 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
children = gimp_item_list_children (item);
|
2013-02-22 01:31:12 +08:00
|
|
|
index += align_layers_spread_visibles_layers (children,
|
|
|
|
&(layers_array[index]));
|
2019-08-28 17:50:38 +08:00
|
|
|
g_list_free (children);
|
2013-02-22 01:31:12 +08:00
|
|
|
}
|
|
|
|
else if (gimp_item_is_layer (item))
|
|
|
|
{
|
2019-08-28 17:50:38 +08:00
|
|
|
layers_array[index] = GIMP_LAYER (item);
|
2013-02-22 01:31:12 +08:00
|
|
|
index++;
|
|
|
|
}
|
2003-12-24 06:07:06 +08:00
|
|
|
}
|
2013-02-22 01:31:12 +08:00
|
|
|
}
|
2000-04-21 03:37:39 +08:00
|
|
|
|
2013-02-22 01:31:12 +08:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a contiguous array of all visible layers
|
|
|
|
*/
|
2019-08-28 17:50:38 +08:00
|
|
|
static GimpLayer **
|
|
|
|
align_layers_spread_image (GimpImage *image,
|
|
|
|
gint *layer_num)
|
2013-02-22 01:31:12 +08:00
|
|
|
{
|
2019-08-28 17:50:38 +08:00
|
|
|
GList *layers;
|
|
|
|
GimpLayer **layers_array;
|
2013-02-22 01:31:12 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
layers = gimp_image_list_layers (image);
|
|
|
|
*layer_num = align_layers_count_visibles_layers (layers);
|
2013-02-22 01:31:12 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
layers_array = g_malloc (sizeof (GimpLayer *) * *layer_num);
|
2013-02-22 01:31:12 +08:00
|
|
|
|
|
|
|
align_layers_spread_visibles_layers (layers,
|
|
|
|
layers_array);
|
2019-08-28 17:50:38 +08:00
|
|
|
g_list_free (layers);
|
2013-02-22 01:31:12 +08:00
|
|
|
|
|
|
|
return layers_array;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
2019-08-28 17:50:38 +08:00
|
|
|
align_layers_count_visibles_layers (GList *layers)
|
2013-02-22 01:31:12 +08:00
|
|
|
{
|
2019-08-28 17:50:38 +08:00
|
|
|
GList *iter;
|
|
|
|
gint count = 0;
|
2013-02-22 01:31:12 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
for (iter = layers; iter; iter = iter->next)
|
2013-02-22 01:31:12 +08:00
|
|
|
{
|
2019-08-28 17:50:38 +08:00
|
|
|
GimpItem *item = iter->data;
|
2013-02-22 01:31:12 +08:00
|
|
|
|
|
|
|
if (gimp_item_get_visible (item))
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
2013-02-22 01:31:12 +08:00
|
|
|
if (gimp_item_is_group (item))
|
|
|
|
{
|
2019-08-28 17:50:38 +08:00
|
|
|
GList *children;
|
2013-02-22 01:31:12 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
children = gimp_item_list_children (item);
|
|
|
|
count += align_layers_count_visibles_layers (children);
|
|
|
|
g_list_free (children);
|
2013-02-22 01:31:12 +08:00
|
|
|
}
|
|
|
|
else if (gimp_item_is_layer (item))
|
|
|
|
{
|
|
|
|
count += 1;
|
|
|
|
}
|
2003-12-24 06:07:06 +08:00
|
|
|
}
|
2013-02-22 01:31:12 +08:00
|
|
|
}
|
2000-04-21 03:37:39 +08:00
|
|
|
|
2013-02-22 01:31:12 +08:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static AlignData
|
2019-08-28 17:50:38 +08:00
|
|
|
align_layers_gather_data (GimpLayer **layers,
|
|
|
|
gint layer_num,
|
2023-03-16 11:23:26 +08:00
|
|
|
GimpLayer *background,
|
|
|
|
GObject *config)
|
2013-02-22 01:31:12 +08:00
|
|
|
{
|
|
|
|
AlignData data;
|
2023-03-16 11:23:26 +08:00
|
|
|
gint min_x = G_MAXINT;
|
|
|
|
gint min_y = G_MAXINT;
|
|
|
|
gint max_x = G_MININT;
|
|
|
|
gint max_y = G_MININT;
|
|
|
|
gint index;
|
|
|
|
gint orig_x = 0;
|
|
|
|
gint orig_y = 0;
|
|
|
|
gint offset_x = 0;
|
|
|
|
gint offset_y = 0;
|
|
|
|
gint horizontal_style;
|
|
|
|
gint vertical_style;
|
|
|
|
gboolean use_bottom_layer;
|
|
|
|
|
|
|
|
g_object_get (config,
|
|
|
|
"use-bottom-layer", &use_bottom_layer,
|
|
|
|
NULL);
|
2024-07-27 02:56:53 +08:00
|
|
|
horizontal_style = gimp_procedure_config_get_choice_id (GIMP_PROCEDURE_CONFIG (config),
|
|
|
|
"horizontal-style");
|
|
|
|
vertical_style = gimp_procedure_config_get_choice_id (GIMP_PROCEDURE_CONFIG (config),
|
|
|
|
"vertical-style");
|
|
|
|
|
2013-02-22 01:31:12 +08:00
|
|
|
|
|
|
|
data.step_x = 0;
|
|
|
|
data.step_y = 0;
|
|
|
|
data.base_x = 0;
|
|
|
|
data.base_y = 0;
|
|
|
|
|
|
|
|
/* 0 is the top layer */
|
|
|
|
for (index = 0; index < layer_num; index++)
|
|
|
|
{
|
2021-04-06 20:28:40 +08:00
|
|
|
gimp_drawable_get_offsets (GIMP_DRAWABLE (layers[index]), &orig_x, &orig_y);
|
2000-04-21 03:37:39 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
align_layers_get_align_offsets (GIMP_DRAWABLE (layers[index]),
|
2013-02-22 01:31:12 +08:00
|
|
|
&offset_x,
|
2023-03-16 11:23:26 +08:00
|
|
|
&offset_y,
|
|
|
|
config);
|
2013-02-22 01:31:12 +08:00
|
|
|
orig_x += offset_x;
|
|
|
|
orig_y += offset_y;
|
|
|
|
|
|
|
|
min_x = MIN (min_x, orig_x);
|
|
|
|
max_x = MAX (max_x, orig_x);
|
|
|
|
min_y = MIN (min_y, orig_y);
|
|
|
|
max_y = MAX (max_y, orig_y);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
2003-10-21 07:32:20 +08:00
|
|
|
|
2023-03-16 11:23:26 +08:00
|
|
|
if (use_bottom_layer)
|
2013-02-22 01:31:12 +08:00
|
|
|
{
|
2021-04-06 20:28:40 +08:00
|
|
|
gimp_drawable_get_offsets (GIMP_DRAWABLE (background), &orig_x, &orig_y);
|
2013-02-22 01:31:12 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
align_layers_get_align_offsets (GIMP_DRAWABLE (background),
|
2013-02-22 01:31:12 +08:00
|
|
|
&offset_x,
|
2023-03-16 11:23:26 +08:00
|
|
|
&offset_y,
|
|
|
|
config);
|
2013-02-22 01:31:12 +08:00
|
|
|
orig_x += offset_x;
|
|
|
|
orig_y += offset_y;
|
|
|
|
data.base_x = min_x = orig_x;
|
|
|
|
data.base_y = min_y = orig_y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (layer_num > 1)
|
|
|
|
{
|
|
|
|
data.step_x = (max_x - min_x) / (layer_num - 1);
|
|
|
|
data.step_y = (max_y - min_y) / (layer_num - 1);
|
|
|
|
}
|
|
|
|
|
2023-03-16 11:23:26 +08:00
|
|
|
if ((horizontal_style == LEFT2RIGHT) || (horizontal_style == RIGHT2LEFT))
|
2013-02-22 01:31:12 +08:00
|
|
|
data.base_x = min_x;
|
|
|
|
|
2023-03-16 11:23:26 +08:00
|
|
|
if ((vertical_style == TOP2BOTTOM) || (vertical_style == BOTTOM2TOP))
|
2013-02-22 01:31:12 +08:00
|
|
|
data.base_y = min_y;
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
2003-10-21 07:32:20 +08:00
|
|
|
|
2013-02-22 01:31:12 +08:00
|
|
|
/*
|
|
|
|
* Modifies position of each visible layers
|
|
|
|
* according to data.
|
|
|
|
*/
|
|
|
|
static void
|
2019-08-28 17:50:38 +08:00
|
|
|
align_layers_perform_alignment (GimpLayer **layers,
|
|
|
|
gint layer_num,
|
2023-03-16 11:23:26 +08:00
|
|
|
AlignData data,
|
|
|
|
GObject *config)
|
2013-02-22 01:31:12 +08:00
|
|
|
{
|
|
|
|
gint index;
|
2023-03-16 11:23:26 +08:00
|
|
|
gint horizontal_style;
|
|
|
|
gint vertical_style;
|
|
|
|
gint grid_size;
|
|
|
|
|
|
|
|
g_object_get (config,
|
2024-07-27 02:56:53 +08:00
|
|
|
"grid-size", &grid_size,
|
2023-03-16 11:23:26 +08:00
|
|
|
NULL);
|
2024-07-27 02:56:53 +08:00
|
|
|
horizontal_style = gimp_procedure_config_get_choice_id (GIMP_PROCEDURE_CONFIG (config),
|
|
|
|
"horizontal-style");
|
|
|
|
vertical_style = gimp_procedure_config_get_choice_id (GIMP_PROCEDURE_CONFIG (config),
|
|
|
|
"vertical-style");
|
2013-02-22 01:31:12 +08:00
|
|
|
|
|
|
|
for (index = 0; index < layer_num; index++)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2013-02-22 01:31:12 +08:00
|
|
|
gint x = 0;
|
|
|
|
gint y = 0;
|
|
|
|
gint orig_x;
|
|
|
|
gint orig_y;
|
|
|
|
gint offset_x;
|
|
|
|
gint offset_y;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2021-04-06 20:28:40 +08:00
|
|
|
gimp_drawable_get_offsets (GIMP_DRAWABLE (layers[index]), &orig_x, &orig_y);
|
2003-10-21 07:32:20 +08:00
|
|
|
|
2019-08-28 17:50:38 +08:00
|
|
|
align_layers_get_align_offsets (GIMP_DRAWABLE (layers[index]),
|
2013-02-22 01:31:12 +08:00
|
|
|
&offset_x,
|
2023-03-16 11:23:26 +08:00
|
|
|
&offset_y,
|
|
|
|
config);
|
|
|
|
switch (horizontal_style)
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
|
|
|
case H_NONE:
|
|
|
|
x = orig_x;
|
|
|
|
break;
|
|
|
|
case H_COLLECT:
|
2013-02-22 01:31:12 +08:00
|
|
|
x = data.base_x - offset_x;
|
2003-12-24 06:07:06 +08:00
|
|
|
break;
|
|
|
|
case LEFT2RIGHT:
|
2013-02-22 01:31:12 +08:00
|
|
|
x = (data.base_x + index * data.step_x) - offset_x;
|
2003-12-24 06:07:06 +08:00
|
|
|
break;
|
|
|
|
case RIGHT2LEFT:
|
2013-02-22 01:31:12 +08:00
|
|
|
x = (data.base_x + (layer_num - index - 1) * data.step_x) - offset_x;
|
2003-12-24 06:07:06 +08:00
|
|
|
break;
|
|
|
|
case SNAP2HGRID:
|
2023-03-16 11:23:26 +08:00
|
|
|
x = grid_size
|
|
|
|
* (int) ((orig_x + offset_x + grid_size /2) / grid_size)
|
2003-12-24 06:07:06 +08:00
|
|
|
- offset_x;
|
|
|
|
break;
|
|
|
|
}
|
2013-02-22 01:31:12 +08:00
|
|
|
|
2023-03-16 11:23:26 +08:00
|
|
|
switch (vertical_style)
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
|
|
|
case V_NONE:
|
|
|
|
y = orig_y;
|
|
|
|
break;
|
|
|
|
case V_COLLECT:
|
2013-02-22 01:31:12 +08:00
|
|
|
y = data.base_y - offset_y;
|
2003-12-24 06:07:06 +08:00
|
|
|
break;
|
|
|
|
case TOP2BOTTOM:
|
2013-02-22 01:31:12 +08:00
|
|
|
y = (data.base_y + index * data.step_y) - offset_y;
|
2003-12-24 06:07:06 +08:00
|
|
|
break;
|
|
|
|
case BOTTOM2TOP:
|
2013-02-22 01:31:12 +08:00
|
|
|
y = (data.base_y + (layer_num - index - 1) * data.step_y) - offset_y;
|
2003-12-24 06:07:06 +08:00
|
|
|
break;
|
|
|
|
case SNAP2VGRID:
|
2023-03-16 11:23:26 +08:00
|
|
|
y = grid_size
|
|
|
|
* (int) ((orig_y + offset_y + grid_size / 2) / grid_size)
|
2003-12-24 06:07:06 +08:00
|
|
|
- offset_y;
|
|
|
|
break;
|
|
|
|
}
|
2013-02-22 01:31:12 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
gimp_layer_set_offsets (layers[index], x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-08-28 17:50:38 +08:00
|
|
|
align_layers_get_align_offsets (GimpDrawable *drawable,
|
|
|
|
gint *x,
|
2023-03-16 11:23:26 +08:00
|
|
|
gint *y,
|
|
|
|
GObject *config)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2021-04-06 20:28:40 +08:00
|
|
|
gint width = gimp_drawable_get_width (drawable);
|
|
|
|
gint height = gimp_drawable_get_height (drawable);
|
2023-03-16 11:23:26 +08:00
|
|
|
gint horizontal_base;
|
|
|
|
gint vertical_base;
|
|
|
|
|
2024-07-27 02:56:53 +08:00
|
|
|
horizontal_base = gimp_procedure_config_get_choice_id (GIMP_PROCEDURE_CONFIG (config),
|
|
|
|
"horizontal-base");
|
|
|
|
vertical_base = gimp_procedure_config_get_choice_id (GIMP_PROCEDURE_CONFIG (config),
|
|
|
|
"vertical-base");
|
2003-10-21 07:32:20 +08:00
|
|
|
|
2023-03-16 11:23:26 +08:00
|
|
|
switch (horizontal_base)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
case H_BASE_LEFT:
|
|
|
|
*x = 0;
|
|
|
|
break;
|
|
|
|
case H_BASE_CENTER:
|
2012-09-16 03:54:42 +08:00
|
|
|
*x = width / 2;
|
1997-11-25 06:05:25 +08:00
|
|
|
break;
|
|
|
|
case H_BASE_RIGHT:
|
2012-09-16 03:54:42 +08:00
|
|
|
*x = width;
|
1997-11-25 06:05:25 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*x = 0;
|
|
|
|
break;
|
|
|
|
}
|
2004-04-20 20:21:17 +08:00
|
|
|
|
2023-03-16 11:23:26 +08:00
|
|
|
switch (vertical_base)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
case V_BASE_TOP:
|
|
|
|
*y = 0;
|
|
|
|
break;
|
|
|
|
case V_BASE_CENTER:
|
2012-09-16 03:54:42 +08:00
|
|
|
*y = height / 2;
|
1997-11-25 06:05:25 +08:00
|
|
|
break;
|
|
|
|
case V_BASE_BOTTOM:
|
2012-09-16 03:54:42 +08:00
|
|
|
*y = height;
|
1997-11-25 06:05:25 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*y = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2023-03-16 11:23:26 +08:00
|
|
|
align_layers_dialog (GimpProcedure *procedure,
|
|
|
|
GObject *config)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2024-07-27 02:56:53 +08:00
|
|
|
GtkWidget *dialog;
|
|
|
|
gboolean run;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2019-09-21 01:39:00 +08:00
|
|
|
gimp_ui_init (PLUG_IN_BINARY);
|
2000-01-16 23:38:38 +08:00
|
|
|
|
2023-03-16 11:23:26 +08:00
|
|
|
dialog = gimp_procedure_dialog_new (procedure,
|
|
|
|
GIMP_PROCEDURE_CONFIG (config),
|
|
|
|
_("Align Visible Layers"));
|
2000-01-13 23:39:26 +08:00
|
|
|
|
2023-03-16 11:23:26 +08:00
|
|
|
gimp_procedure_dialog_get_scale_entry (GIMP_PROCEDURE_DIALOG (dialog),
|
|
|
|
"grid-size", 1.0);
|
|
|
|
|
|
|
|
gimp_procedure_dialog_fill (GIMP_PROCEDURE_DIALOG (dialog),
|
|
|
|
NULL);
|
2001-12-29 21:26:29 +08:00
|
|
|
|
2005-09-10 02:07:31 +08:00
|
|
|
gtk_widget_show (dialog);
|
2003-10-21 07:32:20 +08:00
|
|
|
|
2023-03-16 11:23:26 +08:00
|
|
|
run = gimp_procedure_dialog_run (GIMP_PROCEDURE_DIALOG (dialog));
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2005-09-10 02:07:31 +08:00
|
|
|
gtk_widget_destroy (dialog);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
return run;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|