From 1f370f19b5be971ac41f3660b443b4b43ba49c8b Mon Sep 17 00:00:00 2001 From: Alx Sa Date: Wed, 9 Aug 2023 02:43:03 +0000 Subject: [PATCH] config: Add options to auto-scale imported layers If enabled in Preferences, any image that's dragged into an existing canvas (or explicitly opened as a layer) will be scaled to fit the current canvas size. --- app/config/gimpcoreconfig.c | 14 +++++++++ app/config/gimpcoreconfig.h | 1 + app/config/gimprc-blurbs.h | 4 +++ app/dialogs/preferences-dialog.c | 5 +++ app/file/file-open.c | 54 +++++++++++++++++++++++++++++++- 5 files changed, 77 insertions(+), 1 deletion(-) diff --git a/app/config/gimpcoreconfig.c b/app/config/gimpcoreconfig.c index 3d3ab88c87..2406e90df2 100644 --- a/app/config/gimpcoreconfig.c +++ b/app/config/gimpcoreconfig.c @@ -112,6 +112,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, @@ -724,6 +725,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", @@ -1142,6 +1150,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); @@ -1408,6 +1419,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; diff --git a/app/config/gimpcoreconfig.h b/app/config/gimpcoreconfig.h index 3ebc62bea9..568888f0c5 100644 --- a/app/config/gimpcoreconfig.h +++ b/app/config/gimpcoreconfig.h @@ -96,6 +96,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; diff --git a/app/config/gimprc-blurbs.h b/app/config/gimprc-blurbs.h index e7ff1be250..8dabd2bb41 100644 --- a/app/config/gimprc-blurbs.h +++ b/app/config/gimprc-blurbs.h @@ -223,6 +223,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.") diff --git a/app/dialogs/preferences-dialog.c b/app/dialogs/preferences-dialog.c index 999078b315..7018139a8c 100644 --- a/app/dialogs/preferences-dialog.c +++ b/app/dialogs/preferences-dialog.c @@ -1591,6 +1591,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:"), diff --git a/app/file/file-open.c b/app/file/file-open.c index de2f2dd027..8bc819031e 100644 --- a/app/file/file-open.c +++ b/app/file/file-open.c @@ -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" @@ -630,7 +632,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);