[gimp] libgimp: add GIMP_EXPORT_NEEDS_CROP export capability



commit 4c8ee413eb06ca5584c7fae0a250f9560b69e7ad
Author: Ell <ell_se yahoo com>
Date:   Thu Jun 25 17:50:21 2020 +0300

    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.

 libgimp/gimpexport.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 libgimp/gimpexport.h |  4 +++-
 2 files changed, 66 insertions(+), 1 deletion(-)
---
diff --git a/libgimp/gimpexport.c b/libgimp/gimpexport.c
index e948ef7006..c54e2c5834 100644
--- a/libgimp/gimpexport.c
+++ b/libgimp/gimpexport.c
@@ -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                        &&
diff --git a/libgimp/gimpexport.h b/libgimp/gimpexport.h
index 3bd674ffec..3c0e6ff24a 100644
--- a/libgimp/gimpexport.h
+++ b/libgimp/gimpexport.h
@@ -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;
 
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]