[gimp/gimp-2-10] libgimp: add GIMP_EXPORT_NEEDS_CROP export capability



commit 6c44e51b832b48cb7e8056317e2bde1766260a89
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 | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 libgimp/gimpexport.h |  4 +++-
 2 files changed, 65 insertions(+), 1 deletion(-)
---
diff --git a/libgimp/gimpexport.c b/libgimp/gimpexport.c
index b51b01a7e5..e5621da3d7 100644
--- a/libgimp/gimpexport.c
+++ b/libgimp/gimpexport.c
@@ -261,6 +261,23 @@ export_add_alpha (gint32  image_ID,
   g_free (layers);
 }
 
+static void
+export_crop_image (gint32  image_ID,
+                   gint32 *drawable_ID)
+{
+  gimp_image_crop (image_ID,
+                   gimp_image_width  (image_ID),
+                   gimp_image_height (image_ID),
+                   0, 0);
+}
+
+static void
+export_resize_image (gint32  image_ID,
+                     gint32 *drawable_ID)
+{
+  gimp_image_resize_to_layers (image_ID);
+}
+
 static void
 export_void (gint32  image_ID,
              gint32 *drawable_ID)
@@ -420,6 +437,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)
@@ -846,6 +872,42 @@ gimp_export_image (gint32                 *image_ID,
 
       children = gimp_item_get_children (layers[0], &n_children);
 
+      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_ID);
+          image_bounds.height = gimp_image_height (*image_ID);
+
+          for (i = 0; i < n_layers; i++)
+            {
+              GeglRectangle layer_bounds;
+
+              gimp_drawable_offsets (layers[i],
+                                     &layer_bounds.x, &layer_bounds.y);
+
+              layer_bounds.width  = gimp_drawable_width  (layers[i]);
+              layer_bounds.height = gimp_drawable_height (layers[i]);
+
+              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 (n_layers == 1                     &&
           ! children                        &&
diff --git a/libgimp/gimpexport.h b/libgimp/gimpexport.h
index 8b8c7545b1..bc8c38575a 100644
--- a/libgimp/gimpexport.h
+++ b/libgimp/gimpexport.h
@@ -42,6 +42,7 @@ G_BEGIN_DECLS
  * @GIMP_EXPORT_CAN_HANDLE_LAYERS_AS_ANIMATION: Handles aminations 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]