Merge branch 'alxsa-resize-layers-on-import' into 'master'

Draft: Issue #9833: Add option to automatically scale imported layers to the canvas size

Closes #9833

See merge request GNOME/gimp!1037
This commit is contained in:
Alx Sa 2025-06-29 22:09:06 +00:00
commit 346e7f4608
5 changed files with 77 additions and 1 deletions

View file

@ -113,6 +113,7 @@ enum
PROP_IMPORT_PROMOTE_FLOAT,
PROP_IMPORT_PROMOTE_DITHER,
PROP_IMPORT_ADD_ALPHA,
PROP_IMPORT_RESIZE_LAYERS,
PROP_IMPORT_RAW_PLUG_IN,
PROP_EXPORT_FILE_TYPE,
PROP_EXPORT_COLOR_PROFILE,
@ -734,6 +735,13 @@ gimp_core_config_class_init (GimpCoreConfigClass *klass)
FALSE,
GIMP_PARAM_STATIC_STRINGS);
GIMP_CONFIG_PROP_BOOLEAN (object_class, PROP_IMPORT_RESIZE_LAYERS,
"import-resize-layers",
"Import resize layers",
IMPORT_RESIZE_LAYERS_BLURB,
TRUE,
GIMP_PARAM_STATIC_STRINGS);
GIMP_CONFIG_PROP_PATH (object_class, PROP_IMPORT_RAW_PLUG_IN,
"import-raw-plug-in",
"Import raw plug-in",
@ -1167,6 +1175,9 @@ gimp_core_config_set_property (GObject *object,
case PROP_IMPORT_ADD_ALPHA:
core_config->import_add_alpha = g_value_get_boolean (value);
break;
case PROP_IMPORT_RESIZE_LAYERS:
core_config->import_resize_layers = g_value_get_boolean (value);
break;
case PROP_IMPORT_RAW_PLUG_IN:
g_free (core_config->import_raw_plug_in);
core_config->import_raw_plug_in = g_value_dup_string (value);
@ -1436,6 +1447,9 @@ gimp_core_config_get_property (GObject *object,
case PROP_IMPORT_ADD_ALPHA:
g_value_set_boolean (value, core_config->import_add_alpha);
break;
case PROP_IMPORT_RESIZE_LAYERS:
g_value_set_boolean (value, core_config->import_resize_layers);
break;
case PROP_IMPORT_RAW_PLUG_IN:
g_value_set_string (value, core_config->import_raw_plug_in);
break;

View file

@ -97,6 +97,7 @@ struct _GimpCoreConfig
gboolean import_promote_float;
gboolean import_promote_dither;
gboolean import_add_alpha;
gboolean import_resize_layers;
gchar *import_raw_plug_in;
GimpExportFileType export_file_type;
gboolean export_color_profile;

View file

@ -226,6 +226,10 @@ _("When promoting imported images to floating point precision, also add " \
#define IMPORT_ADD_ALPHA_BLURB \
_("Add an alpha channel to all layers of imported images.")
#define IMPORT_RESIZE_LAYERS_BLURB \
_("Resize imported layers to fit the existing image, retaining the " \
"imported layers' aspect ratio.")
#define IMPORT_RAW_PLUG_IN_BLURB \
_("Which plug-in to use for importing raw digital camera files.")

View file

@ -1547,6 +1547,11 @@ prefs_dialog_new (Gimp *gimp,
_("_Add an alpha channel to imported images"),
GTK_BOX (vbox2));
button = prefs_check_button_add (object, "import-resize-layers",
_("Resi_ze imported layers to fit the "
"existing canvas"),
GTK_BOX (vbox2));
grid = prefs_grid_new (GTK_CONTAINER (vbox2));
button = prefs_enum_combo_box_add (object, "color-profile-policy", 0, 0,
_("Color _profile policy:"),

View file

@ -29,6 +29,8 @@
#include "gegl/gimp-babl.h"
#include "config/gimpcoreconfig.h"
#include "core/gimp.h"
#include "core/gimpcontext.h"
#include "core/gimpdocumentlist.h"
@ -659,7 +661,57 @@ file_open_layers (Gimp *gimp,
if (layers)
{
gchar *basename;
GimpCoreConfig *config = dest_image->gimp->config;
gchar *basename;
/* Scale imported layers to fit existing canvas.
* Adapted from ofnut's ofn-autoscale-layer.py */
if (config->import_resize_layers)
{
GList *list;
gint image_width;
gint image_height;
gdouble image_ratio;
image_width = gimp_image_get_width (dest_image);
image_height = gimp_image_get_height (dest_image);
image_ratio = (gdouble) image_width / (gdouble) image_height;
for (list = layers; list; list = g_list_next (list))
{
gdouble layer_ratio;
gint layer_width;
gint layer_height;
gint resized_width;
gint resized_height;
layer_width = gimp_item_get_width (GIMP_ITEM (list->data));
layer_height = gimp_item_get_height (GIMP_ITEM (list->data));
layer_ratio = (gdouble) layer_width / (gdouble) layer_height;
if (layer_ratio > image_ratio)
{
resized_width = image_width;
resized_height = image_width / layer_ratio;
}
else
{
resized_width = image_height * layer_ratio;
resized_height = image_height;
}
gimp_item_scale_by_origin (GIMP_ITEM (list->data),
resized_width, resized_height,
config->interpolation_type,
progress, FALSE);
gimp_item_set_offset (GIMP_ITEM (list->data),
(image_width / resized_width) / 2,
(image_height / resized_height) / 2);
}
}
basename = g_path_get_basename (gimp_file_get_utf8_name (file));
file_open_convert_items (dest_image, basename, layers);