libgimp: add GIMP_EXPORT_NEEDS_CROP export capability

Add a new GIMP_EXPORT_NEEDS_CROP export capability, which causes
gimp_export_image() to crop the exported image content to the image
bounds; this is useful for formats that support layers, but have no
concept of global image bounds, hence cropping is the only way to
enforce the image bounds.

When showing the export dialog, give an option to either crop the
layers to the image bounds, or to resize the image to fit the
layers.
This commit is contained in:
Ell 2020-06-25 17:50:21 +03:00
parent 536bf771b7
commit 4c8ee413eb
2 changed files with 66 additions and 1 deletions

View File

@ -282,6 +282,23 @@ export_add_alpha (GimpImage *image,
g_list_free (layers);
}
static void
export_crop_image (GimpImage *image,
GList **drawables)
{
gimp_image_crop (image,
gimp_image_width (image),
gimp_image_height (image),
0, 0);
}
static void
export_resize_image (GimpImage *image,
GList **drawables)
{
gimp_image_resize_to_layers (image);
}
static void
export_void (GimpImage *image,
GList **drawables)
@ -441,6 +458,15 @@ static ExportAction export_action_add_alpha =
0
};
static ExportAction export_action_crop_or_resize =
{
export_crop_image,
export_resize_image,
N_("%s plug-in needs to crop the layers to the image bounds"),
{ N_("Crop Layers"), N_("Resize Image to Layers")},
0
};
static ExportFunc
export_action_get_func (const ExportAction *action)
@ -885,6 +911,43 @@ gimp_export_image (GimpImage **image,
children = gimp_item_list_children (GIMP_ITEM (layer));
if ((capabilities & GIMP_EXPORT_CAN_HANDLE_LAYERS) &&
(capabilities & GIMP_EXPORT_NEEDS_CROP))
{
GeglRectangle image_bounds;
gboolean needs_crop = FALSE;
image_bounds.x = 0;
image_bounds.y = 0;
image_bounds.width = gimp_image_width (*image);
image_bounds.height = gimp_image_height (*image);
for (iter = layers; iter; iter = iter->next)
{
GimpDrawable *drawable = iter->data;
GeglRectangle layer_bounds;
gimp_drawable_offsets (drawable,
&layer_bounds.x, &layer_bounds.y);
layer_bounds.width = gimp_drawable_width (drawable);
layer_bounds.height = gimp_drawable_height (drawable);
if (! gegl_rectangle_contains (&image_bounds, &layer_bounds))
{
needs_crop = TRUE;
break;
}
}
if (needs_crop)
{
actions = g_slist_prepend (actions,
&export_action_crop_or_resize);
}
}
/* check if layer size != canvas size, opacity != 100%, or offsets != 0 */
if (g_list_length (layers) == 1 &&
! children &&

View File

@ -42,6 +42,7 @@ G_BEGIN_DECLS
* @GIMP_EXPORT_CAN_HANDLE_LAYERS_AS_ANIMATION: Handles animation of layers
* @GIMP_EXPORT_CAN_HANDLE_LAYER_MASKS: Handles layer masks
* @GIMP_EXPORT_NEEDS_ALPHA: Needs alpha channels
* @GIMP_EXPORT_NEEDS_CROP: Needs to crop content to image bounds
*
* The types of images and layers an export procedure can handle
**/
@ -55,7 +56,8 @@ typedef enum
GIMP_EXPORT_CAN_HANDLE_LAYERS = 1 << 5,
GIMP_EXPORT_CAN_HANDLE_LAYERS_AS_ANIMATION = 1 << 6,
GIMP_EXPORT_CAN_HANDLE_LAYER_MASKS = 1 << 7,
GIMP_EXPORT_NEEDS_ALPHA = 1 << 8
GIMP_EXPORT_NEEDS_ALPHA = 1 << 8,
GIMP_EXPORT_NEEDS_CROP = 1 << 9
} GimpExportCapabilities;