[gimp] Bug 735516 - Crop to content fails



commit bda1cd049d9269c678c025dd2e2065e35cb34d45
Author: Michael Natterer <mitch gimp org>
Date:   Sun Aug 31 17:38:49 2014 +0200

    Bug 735516 - Crop to content fails
    
    Return a new enum GimpAutoShrink { SHRINK, EMPTY, UNSHRINKABLE } from
    gimp_pickable_auto_shrink() because a simply boolean FALSE doesn't
    distinguish between "not further shrinkable" and "no content". Change
    the callers accordingly add a special "already cropped" message where
    we only had a "no content" message before.

 app/actions/image-commands.c        |   31 ++++++++++-----
 app/actions/layers-commands.c       |   40 ++++++++++++--------
 app/core/gimppickable-auto-shrink.c |   15 ++++---
 app/core/gimppickable-auto-shrink.h |   26 ++++++++----
 app/pdb/plug-in-compat-cmds.c       |   68 ++++++++++++++++++++-------------
 app/tools/gimprectangletool.c       |   45 +++++++++++++---------
 tools/pdbgen/pdb/plug_in_compat.pdb |   70 +++++++++++++++++++++--------------
 7 files changed, 179 insertions(+), 116 deletions(-)
---
diff --git a/app/actions/image-commands.c b/app/actions/image-commands.c
index c8dc8d4..8677c30 100644
--- a/app/actions/image-commands.c
+++ b/app/actions/image-commands.c
@@ -525,21 +525,30 @@ image_crop_to_content_cmd_callback (GtkAction *action,
   return_if_no_image (image, data);
   return_if_no_widget (widget, data);
 
-  if (! gimp_pickable_auto_shrink (GIMP_PICKABLE (image),
-                                   0, 0,
-                                   gimp_image_get_width  (image),
-                                   gimp_image_get_height (image),
-                                   &x1, &y1, &x2, &y2))
+  switch (gimp_pickable_auto_shrink (GIMP_PICKABLE (image),
+                                     0, 0,
+                                     gimp_image_get_width  (image),
+                                     gimp_image_get_height (image),
+                                     &x1, &y1, &x2, &y2))
     {
+    case GIMP_AUTO_SHRINK_SHRINK:
+      gimp_image_crop (image, action_data_get_context (data),
+                       x1, y1, x2, y2, TRUE);
+      gimp_image_flush (image);
+      break;
+
+    case GIMP_AUTO_SHRINK_EMPTY:
       gimp_message_literal (image->gimp,
-                            G_OBJECT (widget), GIMP_MESSAGE_WARNING,
+                            G_OBJECT (widget), GIMP_MESSAGE_INFO,
                             _("Cannot crop because the image has no content."));
-      return;
-    }
+      break;
 
-  gimp_image_crop (image, action_data_get_context (data),
-                   x1, y1, x2, y2, TRUE);
-  gimp_image_flush (image);
+    case GIMP_AUTO_SHRINK_UNSHRINKABLE:
+      gimp_message_literal (image->gimp,
+                            G_OBJECT (widget), GIMP_MESSAGE_INFO,
+                            _("Cannot crop because the image is already cropped to its content."));
+      break;
+    }
 }
 
 void
diff --git a/app/actions/layers-commands.c b/app/actions/layers-commands.c
index 92f1cf0..fc8c482 100644
--- a/app/actions/layers-commands.c
+++ b/app/actions/layers-commands.c
@@ -721,27 +721,35 @@ layers_crop_to_content_cmd_callback (GtkAction *action,
   return_if_no_layer (image, layer, data);
   return_if_no_widget (widget, data);
 
-  if (! gimp_pickable_auto_shrink (GIMP_PICKABLE (layer),
-                                   0, 0,
-                                   gimp_item_get_width  (GIMP_ITEM (layer)),
-                                   gimp_item_get_height (GIMP_ITEM (layer)),
-                                   &x1, &y1, &x2, &y2))
+  switch (gimp_pickable_auto_shrink (GIMP_PICKABLE (layer),
+                                     0, 0,
+                                     gimp_item_get_width  (GIMP_ITEM (layer)),
+                                     gimp_item_get_height (GIMP_ITEM (layer)),
+                                     &x1, &y1, &x2, &y2))
     {
-      gimp_message_literal (image->gimp,
-                            G_OBJECT (widget), GIMP_MESSAGE_WARNING,
-                            _("Cannot crop because the active layer has no content."));
-      return;
-    }
+    case GIMP_AUTO_SHRINK_SHRINK:
+      gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_ITEM_RESIZE,
+                                   _("Crop Layer to Content"));
 
-  gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_ITEM_RESIZE,
-                               _("Crop Layer to Content"));
+      gimp_item_resize (GIMP_ITEM (layer), action_data_get_context (data),
+                        x2 - x1, y2 - y1, -x1, -y1);
 
-  gimp_item_resize (GIMP_ITEM (layer), action_data_get_context (data),
-                    x2 - x1, y2 - y1, -x1, -y1);
+      gimp_image_undo_group_end (image);
+      gimp_image_flush (image);
+      break;
 
-  gimp_image_undo_group_end (image);
+    case GIMP_AUTO_SHRINK_EMPTY:
+      gimp_message_literal (image->gimp,
+                            G_OBJECT (widget), GIMP_MESSAGE_INFO,
+                            _("Cannot crop because the active layer has no content."));
+      break;
 
-  gimp_image_flush (image);
+    case GIMP_AUTO_SHRINK_UNSHRINKABLE:
+      gimp_message_literal (image->gimp,
+                            G_OBJECT (widget), GIMP_MESSAGE_INFO,
+                            _("Cannot crop because the active layer is already cropped to its content."));
+      break;
+    }
 }
 
 void
diff --git a/app/core/gimppickable-auto-shrink.c b/app/core/gimppickable-auto-shrink.c
index 15d5af0..ac7d39c 100644
--- a/app/core/gimppickable-auto-shrink.c
+++ b/app/core/gimppickable-auto-shrink.c
@@ -58,7 +58,7 @@ static gboolean         gimp_pickable_colors_alpha  (guchar       *col1,
 
 /*  public functions  */
 
-gboolean
+GimpAutoShrink
 gimp_pickable_auto_shrink (GimpPickable *pickable,
                            gint          start_x1,
                            gint          start_y1,
@@ -78,7 +78,7 @@ gimp_pickable_auto_shrink (GimpPickable *pickable,
   gint             width, height;
   const Babl      *format;
   gint             x, y, abort;
-  gboolean         retval = FALSE;
+  GimpAutoShrink   retval = GIMP_AUTO_SHRINK_UNSHRINKABLE;
 
   g_return_val_if_fail (GIMP_IS_PICKABLE (pickable), FALSE);
   g_return_val_if_fail (shrunk_x1 != NULL, FALSE);
@@ -144,7 +144,10 @@ gimp_pickable_auto_shrink (GimpPickable *pickable,
         abort = ! colors_equal_func (bgcolor, buf + x * 4);
     }
   if (y == y2 && !abort)
-    goto FINISH;
+    {
+      retval = GIMP_AUTO_SHRINK_EMPTY;
+      goto FINISH;
+    }
   y1 = y - 1;
 
   /* Check how many of the bottom lines are uniform/transparent. */
@@ -201,8 +204,6 @@ gimp_pickable_auto_shrink (GimpPickable *pickable,
     }
   x2 = x + 1;
 
- FINISH:
-
   if (x1 != start_x1 || y1 != start_y1 ||
       x2 != start_x2 || y2 != start_y2)
     {
@@ -211,9 +212,11 @@ gimp_pickable_auto_shrink (GimpPickable *pickable,
       *shrunk_x2 = x2;
       *shrunk_y2 = y2;
 
-      retval = TRUE;
+      retval = GIMP_AUTO_SHRINK_SHRINK;
     }
 
+ FINISH:
+
   g_free (buf);
   gimp_unset_busy (gimp_pickable_get_image (pickable)->gimp);
 
diff --git a/app/core/gimppickable-auto-shrink.h b/app/core/gimppickable-auto-shrink.h
index eab5dd9..f94f62c 100644
--- a/app/core/gimppickable-auto-shrink.h
+++ b/app/core/gimppickable-auto-shrink.h
@@ -19,15 +19,23 @@
 #define __GIMP_PICKABLE_AUTO_SHRINK_H__
 
 
-gboolean   gimp_pickable_auto_shrink (GimpPickable *pickable,
-                                      gint         x1,
-                                      gint         y1,
-                                      gint         x2,
-                                      gint         y2,
-                                      gint        *shrunk_x1,
-                                      gint        *shrunk_y1,
-                                      gint        *shrunk_x2,
-                                      gint        *shrunk_y2);
+typedef enum
+{
+  GIMP_AUTO_SHRINK_SHRINK,
+  GIMP_AUTO_SHRINK_EMPTY,
+  GIMP_AUTO_SHRINK_UNSHRINKABLE
+} GimpAutoShrink;
+
+
+GimpAutoShrink   gimp_pickable_auto_shrink (GimpPickable *pickable,
+                                            gint         x1,
+                                            gint         y1,
+                                            gint         x2,
+                                            gint         y2,
+                                            gint        *shrunk_x1,
+                                            gint        *shrunk_y1,
+                                            gint        *shrunk_x2,
+                                            gint        *shrunk_y2);
 
 
 #endif  /* __GIMP_PICKABLE_AUTO_SHRINK_H__ */
diff --git a/app/pdb/plug-in-compat-cmds.c b/app/pdb/plug-in-compat-cmds.c
index bee9ec1..ce23c3e 100644
--- a/app/pdb/plug-in-compat-cmds.c
+++ b/app/pdb/plug-in-compat-cmds.c
@@ -430,26 +430,33 @@ plug_in_autocrop_invoker (GimpProcedure         *procedure,
         {
           gint x1, y1, x2, y2;
 
-          if (gimp_pickable_auto_shrink (GIMP_PICKABLE (drawable),
-                                         0, 0,
-                                         gimp_item_get_width  (GIMP_ITEM (drawable)),
-                                         gimp_item_get_height (GIMP_ITEM (drawable)),
-                                         &x1, &y1, &x2, &y2))
+          switch (gimp_pickable_auto_shrink (GIMP_PICKABLE (drawable),
+                                             0, 0,
+                                             gimp_item_get_width  (GIMP_ITEM (drawable)),
+                                             gimp_item_get_height (GIMP_ITEM (drawable)),
+                                             &x1, &y1, &x2, &y2))
             {
-              gint off_x, off_y;
+            case GIMP_AUTO_SHRINK_SHRINK:
+              {
+                gint off_x, off_y;
 
-              gimp_item_get_offset (GIMP_ITEM (drawable), &off_x, &off_y);
+                gimp_item_get_offset (GIMP_ITEM (drawable), &off_x, &off_y);
 
-              x1 += off_x; x2 += off_x;
-              y1 += off_y; y2 += off_y;
+                x1 += off_x; x2 += off_x;
+                y1 += off_y; y2 += off_y;
 
-              gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_ITEM_RESIZE,
-                                           _("Autocrop image"));
+                gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_ITEM_RESIZE,
+                                             _("Autocrop image"));
 
-              gimp_image_crop (image, context,
-                               x2 - x1, y2 - y1, -x1, -y1, TRUE);
+                gimp_image_crop (image, context,
+                                 x2 - x1, y2 - y1, -x1, -y1, TRUE);
 
-              gimp_image_undo_group_end (image);
+                gimp_image_undo_group_end (image);
+              }
+              break;
+
+            default:
+              break;
             }
         }
       else
@@ -483,20 +490,27 @@ plug_in_autocrop_layer_invoker (GimpProcedure         *procedure,
           GimpLayer *layer = gimp_image_get_active_layer (image);
           gint       x1, y1, x2, y2;
 
-          if (layer &&
-              gimp_pickable_auto_shrink (GIMP_PICKABLE (drawable),
-                                         0, 0,
-                                         gimp_item_get_width  (GIMP_ITEM (drawable)),
-                                         gimp_item_get_height (GIMP_ITEM (drawable)),
-                                         &x1, &y1, &x2, &y2))
+          if (layer)
             {
-              gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_ITEM_RESIZE,
-                                           _("Autocrop layer"));
-
-              gimp_item_resize (GIMP_ITEM (layer), context,
-                                x2 - x1, y2 - y1, -x1, -y1);
-
-              gimp_image_undo_group_end (image);
+              switch (gimp_pickable_auto_shrink (GIMP_PICKABLE (drawable),
+                                                 0, 0,
+                                                 gimp_item_get_width  (GIMP_ITEM (drawable)),
+                                                 gimp_item_get_height (GIMP_ITEM (drawable)),
+                                                 &x1, &y1, &x2, &y2))
+                {
+                case GIMP_AUTO_SHRINK_SHRINK:
+                  gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_ITEM_RESIZE,
+                                               _("Autocrop layer"));
+
+                  gimp_item_resize (GIMP_ITEM (layer), context,
+                                    x2 - x1, y2 - y1, -x1, -y1);
+
+                  gimp_image_undo_group_end (image);
+                  break;
+
+                default:
+                  break;
+                }
             }
         }
       else
diff --git a/app/tools/gimprectangletool.c b/app/tools/gimprectangletool.c
index 3971fd3..99d26e1 100644
--- a/app/tools/gimprectangletool.c
+++ b/app/tools/gimprectangletool.c
@@ -2546,32 +2546,39 @@ gimp_rectangle_tool_auto_shrink (GimpRectangleTool *rect_tool)
       y2 = private->y2 - offset_y;
     }
 
-  if (gimp_pickable_auto_shrink (pickable,
-                                 x1, y1, x2, y2,
-                                 &shrunk_x1,
-                                 &shrunk_y1,
-                                 &shrunk_x2,
-                                 &shrunk_y2))
+  switch (gimp_pickable_auto_shrink (pickable,
+                                     x1, y1, x2, y2,
+                                     &shrunk_x1,
+                                     &shrunk_y1,
+                                     &shrunk_x2,
+                                     &shrunk_y2))
     {
-      GimpRectangleFunction original_function = private->function;
+    case GIMP_AUTO_SHRINK_SHRINK:
+      {
+        GimpRectangleFunction original_function = private->function;
 
-      gimp_draw_tool_pause (GIMP_DRAW_TOOL (rect_tool));
-      private->function = GIMP_RECTANGLE_TOOL_AUTO_SHRINK;
+        gimp_draw_tool_pause (GIMP_DRAW_TOOL (rect_tool));
+        private->function = GIMP_RECTANGLE_TOOL_AUTO_SHRINK;
 
-      private->x1 = offset_x + shrunk_x1;
-      private->y1 = offset_y + shrunk_y1;
-      private->x2 = offset_x + shrunk_x2;
-      private->y2 = offset_y + shrunk_y2;
+        private->x1 = offset_x + shrunk_x1;
+        private->y1 = offset_y + shrunk_y1;
+        private->x2 = offset_x + shrunk_x2;
+        private->y2 = offset_y + shrunk_y2;
 
-      gimp_rectangle_tool_update_int_rect (rect_tool);
+        gimp_rectangle_tool_update_int_rect (rect_tool);
 
-      gimp_rectangle_tool_rectangle_change_complete (rect_tool);
+        gimp_rectangle_tool_rectangle_change_complete (rect_tool);
 
-      gimp_rectangle_tool_update_handle_sizes (rect_tool);
-      gimp_rectangle_tool_update_highlight (rect_tool);
+        gimp_rectangle_tool_update_handle_sizes (rect_tool);
+        gimp_rectangle_tool_update_highlight (rect_tool);
 
-      private->function = original_function;
-      gimp_draw_tool_resume (GIMP_DRAW_TOOL (rect_tool));
+        private->function = original_function;
+        gimp_draw_tool_resume (GIMP_DRAW_TOOL (rect_tool));
+      }
+      break;
+
+    default:
+      break;
     }
 
   gimp_rectangle_tool_update_options (rect_tool, tool->display);
diff --git a/tools/pdbgen/pdb/plug_in_compat.pdb b/tools/pdbgen/pdb/plug_in_compat.pdb
index 7fca071..f7f587a 100644
--- a/tools/pdbgen/pdb/plug_in_compat.pdb
+++ b/tools/pdbgen/pdb/plug_in_compat.pdb
@@ -276,26 +276,33 @@ HELP
     {
       gint x1, y1, x2, y2;
 
-      if (gimp_pickable_auto_shrink (GIMP_PICKABLE (drawable),
-                                     0, 0,
-                                     gimp_item_get_width  (GIMP_ITEM (drawable)),
-                                     gimp_item_get_height (GIMP_ITEM (drawable)),
-                                     &x1, &y1, &x2, &y2))
+      switch (gimp_pickable_auto_shrink (GIMP_PICKABLE (drawable),
+                                         0, 0,
+                                         gimp_item_get_width  (GIMP_ITEM (drawable)),
+                                         gimp_item_get_height (GIMP_ITEM (drawable)),
+                                         &x1, &y1, &x2, &y2))
         {
-          gint off_x, off_y;
+        case GIMP_AUTO_SHRINK_SHRINK:
+         {
+            gint off_x, off_y;
 
-          gimp_item_get_offset (GIMP_ITEM (drawable), &off_x, &off_y);
+            gimp_item_get_offset (GIMP_ITEM (drawable), &off_x, &off_y);
 
-          x1 += off_x; x2 += off_x;
-          y1 += off_y; y2 += off_y;
+            x1 += off_x; x2 += off_x;
+            y1 += off_y; y2 += off_y;
 
-          gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_ITEM_RESIZE,
-                                       _("Autocrop image"));
+            gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_ITEM_RESIZE,
+                                         _("Autocrop image"));
 
-          gimp_image_crop (image, context,
-                           x2 - x1, y2 - y1, -x1, -y1, TRUE);
+            gimp_image_crop (image, context,
+                             x2 - x1, y2 - y1, -x1, -y1, TRUE);
 
-          gimp_image_undo_group_end (image);
+            gimp_image_undo_group_end (image);
+          }
+         break;
+
+        default:
+         break;
         }
     }
   else
@@ -334,21 +341,28 @@ HELP
       GimpLayer *layer = gimp_image_get_active_layer (image);
       gint       x1, y1, x2, y2;
 
-      if (layer &&
-          gimp_pickable_auto_shrink (GIMP_PICKABLE (drawable),
-                                     0, 0,
-                                     gimp_item_get_width  (GIMP_ITEM (drawable)),
-                                     gimp_item_get_height (GIMP_ITEM (drawable)),
-                                     &x1, &y1, &x2, &y2))
+      if (layer)
         {
-          gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_ITEM_RESIZE,
-                                       _("Autocrop layer"));
-
-          gimp_item_resize (GIMP_ITEM (layer), context,
-                            x2 - x1, y2 - y1, -x1, -y1);
-
-          gimp_image_undo_group_end (image);
-        }
+          switch (gimp_pickable_auto_shrink (GIMP_PICKABLE (drawable),
+                                             0, 0,
+                                             gimp_item_get_width  (GIMP_ITEM (drawable)),
+                                             gimp_item_get_height (GIMP_ITEM (drawable)),
+                                             &x1, &y1, &x2, &y2))
+            {
+            case GIMP_AUTO_SHRINK_SHRINK:
+              gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_ITEM_RESIZE,
+                                           _("Autocrop layer"));
+
+              gimp_item_resize (GIMP_ITEM (layer), context,
+                                x2 - x1, y2 - y1, -x1, -y1);
+
+              gimp_image_undo_group_end (image);
+             break;
+
+           default:
+             break;
+            }
+       }
     }
   else
     success = FALSE;


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