[gimp] app: keep the src image's resolution and unit in copied/cut GimpBuffers



commit 536cc8531a76e8daf8e499c8b2200fc3feef0283
Author: Michael Natterer <mitch gimp org>
Date:   Mon May 1 18:36:33 2017 +0200

    app: keep the src image's resolution and unit in copied/cut GimpBuffers
    
    and use them for the new image in "Paste as new image". We were using
    the resolution and unit of the image the paste command was invoked
    from, which is entirely random and useless.

 app/actions/buffers-commands.c     |    2 +-
 app/actions/edit-commands.c        |    4 +--
 app/core/gimp-edit.c               |   12 +++++++-
 app/core/gimpbuffer.c              |   51 ++++++++++++++++++++++++++++++++++++
 app/core/gimpbuffer.h              |   15 ++++++++++
 app/core/gimpimage-new.c           |   28 ++++++++-----------
 app/core/gimpimage-new.h           |    3 +-
 app/display/gimpdisplayshell-dnd.c |    2 +-
 app/pdb/edit-cmds.c                |    4 +-
 app/widgets/gimptoolbox-dnd.c      |    2 +-
 tools/pdbgen/pdb/edit.pdb          |    4 +-
 11 files changed, 97 insertions(+), 30 deletions(-)
---
diff --git a/app/actions/buffers-commands.c b/app/actions/buffers-commands.c
index 11c14a0..4dc559f 100644
--- a/app/actions/buffers-commands.c
+++ b/app/actions/buffers-commands.c
@@ -92,7 +92,7 @@ buffers_paste_as_new_image_cmd_callback (GtkAction *action,
       GtkWidget *widget = GTK_WIDGET (editor);
       GimpImage *new_image;
 
-      new_image = gimp_image_new_from_buffer (context->gimp, NULL, buffer);
+      new_image = gimp_image_new_from_buffer (context->gimp, buffer);
       gimp_create_display (context->gimp, new_image,
                            GIMP_UNIT_PIXEL, 1.0,
                            G_OBJECT (gtk_widget_get_screen (widget)),
diff --git a/app/actions/edit-commands.c b/app/actions/edit-commands.c
index a171b66..897c08d 100644
--- a/app/actions/edit-commands.c
+++ b/app/actions/edit-commands.c
@@ -384,9 +384,7 @@ edit_paste_as_new_image_cmd_callback (GtkAction *action,
         }
       else if (GIMP_IS_BUFFER (paste))
         {
-          image = gimp_image_new_from_buffer (gimp,
-                                              action_data_get_image (data),
-                                              GIMP_BUFFER (paste));
+          image = gimp_image_new_from_buffer (gimp, GIMP_BUFFER (paste));
         }
 
       g_object_unref (paste);
diff --git a/app/core/gimp-edit.c b/app/core/gimp-edit.c
index e7fb960..d024961 100644
--- a/app/core/gimp-edit.c
+++ b/app/core/gimp-edit.c
@@ -707,10 +707,18 @@ gimp_edit_extract (GimpImage     *image,
 
   if (buffer)
     {
-      GimpBuffer *gimp_buffer = gimp_buffer_new (buffer, _("Global Buffer"),
-                                                 offset_x, offset_y, FALSE);
+      GimpBuffer *gimp_buffer;
+      gdouble     res_x;
+      gdouble     res_y;
+
+      gimp_buffer = gimp_buffer_new (buffer, _("Global Buffer"),
+                                     offset_x, offset_y, FALSE);
       g_object_unref (buffer);
 
+      gimp_image_get_resolution (image, &res_x, &res_y);
+      gimp_buffer_set_resolution (gimp_buffer, res_x, res_y);
+      gimp_buffer_set_unit (gimp_buffer, gimp_image_get_unit (image));
+
       if (GIMP_IS_COLOR_MANAGED (pickable))
         {
           GimpColorProfile *profile =
diff --git a/app/core/gimpbuffer.c b/app/core/gimpbuffer.c
index 800408d..4f8a179 100644
--- a/app/core/gimpbuffer.c
+++ b/app/core/gimpbuffer.c
@@ -21,6 +21,7 @@
 #include <gegl.h>
 #include <gdk-pixbuf/gdk-pixbuf.h>
 
+#include "libgimpbase/gimpbase.h"
 #include "libgimpcolor/gimpcolor.h"
 
 #include "core-types.h"
@@ -469,6 +470,56 @@ gimp_buffer_get_buffer (GimpBuffer *buffer)
 }
 
 void
+gimp_buffer_set_resolution (GimpBuffer *buffer,
+                            gdouble     resolution_x,
+                            gdouble     resolution_y)
+{
+  g_return_if_fail (GIMP_IS_BUFFER (buffer));
+  g_return_if_fail (resolution_x >= 0.0 && resolution_x <= GIMP_MAX_RESOLUTION);
+  g_return_if_fail (resolution_y >= 0.0 && resolution_y <= GIMP_MAX_RESOLUTION);
+
+  buffer->resolution_x = resolution_x;
+  buffer->resolution_y = resolution_y;
+}
+
+gboolean
+gimp_buffer_get_resolution (GimpBuffer *buffer,
+                            gdouble    *resolution_x,
+                            gdouble    *resolution_y)
+{
+  g_return_val_if_fail (GIMP_IS_BUFFER (buffer), FALSE);
+
+  if (buffer->resolution_x > 0.0 &&
+      buffer->resolution_y > 0.0)
+    {
+      if (resolution_x) *resolution_x = buffer->resolution_x;
+      if (resolution_y) *resolution_y = buffer->resolution_y;
+
+      return TRUE;
+    }
+
+  return FALSE;
+}
+
+void
+gimp_buffer_set_unit (GimpBuffer *buffer,
+                      GimpUnit    unit)
+{
+  g_return_if_fail (GIMP_IS_BUFFER (buffer));
+  g_return_if_fail (unit > GIMP_UNIT_PIXEL);
+
+  buffer->unit = unit;
+}
+
+GimpUnit
+gimp_buffer_get_unit (GimpBuffer *buffer)
+{
+  g_return_val_if_fail (GIMP_IS_BUFFER (buffer), GIMP_UNIT_PIXEL);
+
+  return buffer->unit;
+}
+
+void
 gimp_buffer_set_color_profile (GimpBuffer       *buffer,
                                GimpColorProfile *profile)
 {
diff --git a/app/core/gimpbuffer.h b/app/core/gimpbuffer.h
index 5df440c..5741e8d 100644
--- a/app/core/gimpbuffer.h
+++ b/app/core/gimpbuffer.h
@@ -40,6 +40,10 @@ struct _GimpBuffer
   gint              offset_x;
   gint              offset_y;
 
+  gdouble           resolution_x;
+  gdouble           resolution_y;
+  GimpUnit          unit;
+
   GimpColorProfile *color_profile;
 };
 
@@ -67,6 +71,17 @@ const Babl       * gimp_buffer_get_format        (GimpBuffer       *buffer);
 
 GeglBuffer       * gimp_buffer_get_buffer        (GimpBuffer       *buffer);
 
+void               gimp_buffer_set_resolution    (GimpBuffer       *buffer,
+                                                  gdouble           resolution_x,
+                                                  gdouble           resolution_y);
+gboolean           gimp_buffer_get_resolution    (GimpBuffer       *buffer,
+                                                  gdouble          *resolution_x,
+                                                  gdouble          *resolution_y);
+
+void               gimp_buffer_set_unit          (GimpBuffer       *buffer,
+                                                  GimpUnit          unit);
+GimpUnit           gimp_buffer_get_unit          (GimpBuffer       *buffer);
+
 void               gimp_buffer_set_color_profile (GimpBuffer       *buffer,
                                                   GimpColorProfile *profile);
 GimpColorProfile * gimp_buffer_get_color_profile (GimpBuffer       *buffer);
diff --git a/app/core/gimpimage-new.c b/app/core/gimpimage-new.c
index b74243a..4cc5b44 100644
--- a/app/core/gimpimage-new.c
+++ b/app/core/gimpimage-new.c
@@ -284,44 +284,40 @@ gimp_image_new_from_component (Gimp            *gimp,
 
 GimpImage *
 gimp_image_new_from_buffer (Gimp       *gimp,
-                            GimpImage  *invoke,
-                            GimpBuffer *paste)
+                            GimpBuffer *buffer)
 {
   GimpImage        *image;
   GimpLayer        *layer;
   const Babl       *format;
   gboolean          has_alpha;
+  gdouble           res_x;
+  gdouble           res_y;
   GimpColorProfile *profile;
 
   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
-  g_return_val_if_fail (invoke == NULL || GIMP_IS_IMAGE (invoke), NULL);
-  g_return_val_if_fail (GIMP_IS_BUFFER (paste), NULL);
+  g_return_val_if_fail (GIMP_IS_BUFFER (buffer), NULL);
 
-  format    = gimp_buffer_get_format (paste);
+  format    = gimp_buffer_get_format (buffer);
   has_alpha = babl_format_has_alpha (format);
 
   image = gimp_create_image (gimp,
-                             gimp_buffer_get_width  (paste),
-                             gimp_buffer_get_height (paste),
+                             gimp_buffer_get_width  (buffer),
+                             gimp_buffer_get_height (buffer),
                              gimp_babl_format_get_base_type (format),
                              gimp_babl_format_get_precision (format),
                              TRUE);
   gimp_image_undo_disable (image);
 
-  if (invoke)
+  if (gimp_buffer_get_resolution (buffer, &res_x, &res_y))
     {
-      gdouble xres;
-      gdouble yres;
-
-      gimp_image_get_resolution (invoke, &xres, &yres);
-      gimp_image_set_resolution (image, xres, yres);
-      gimp_image_set_unit (image, gimp_image_get_unit (invoke));
+      gimp_image_set_resolution (image, res_x, res_y);
+      gimp_image_set_unit (image, gimp_buffer_get_unit (buffer));
     }
 
-  profile = gimp_buffer_get_color_profile (paste);
+  profile = gimp_buffer_get_color_profile (buffer);
   gimp_image_set_color_profile (image, profile, NULL);
 
-  layer = gimp_layer_new_from_buffer (paste, image,
+  layer = gimp_layer_new_from_buffer (buffer, image,
                                       gimp_image_get_layer_format (image,
                                                                    has_alpha),
                                       _("Pasted Layer"),
diff --git a/app/core/gimpimage-new.h b/app/core/gimpimage-new.h
index d07e780..0bf9347 100644
--- a/app/core/gimpimage-new.h
+++ b/app/core/gimpimage-new.h
@@ -33,8 +33,7 @@ GimpImage    * gimp_image_new_from_component    (Gimp            *gimp,
                                                  GimpImage       *image,
                                                  GimpChannelType  component);
 GimpImage    * gimp_image_new_from_buffer       (Gimp            *gimp,
-                                                 GimpImage       *invoke,
-                                                 GimpBuffer      *paste);
+                                                 GimpBuffer      *buffer);
 GimpImage    * gimp_image_new_from_pixbuf       (Gimp            *gimp,
                                                  GdkPixbuf       *pixbuf,
                                                  const gchar     *layer_name);
diff --git a/app/display/gimpdisplayshell-dnd.c b/app/display/gimpdisplayshell-dnd.c
index b5f70be..dd20f6a 100644
--- a/app/display/gimpdisplayshell-dnd.c
+++ b/app/display/gimpdisplayshell-dnd.c
@@ -458,7 +458,7 @@ gimp_display_shell_drop_buffer (GtkWidget    *widget,
 
   if (! image)
     {
-      image = gimp_image_new_from_buffer (shell->display->gimp, NULL,
+      image = gimp_image_new_from_buffer (shell->display->gimp,
                                           GIMP_BUFFER (viewable));
       gimp_create_display (image->gimp, image, GIMP_UNIT_PIXEL, 1.0,
                            G_OBJECT (gtk_widget_get_screen (widget)),
diff --git a/app/pdb/edit-cmds.c b/app/pdb/edit-cmds.c
index 51f19d2..4be7543 100644
--- a/app/pdb/edit-cmds.c
+++ b/app/pdb/edit-cmds.c
@@ -257,7 +257,7 @@ edit_paste_as_new_image_invoker (GimpProcedure         *procedure,
         }
       else if (GIMP_IS_BUFFER (paste))
         {
-          image = gimp_image_new_from_buffer (gimp, NULL, GIMP_BUFFER (paste));
+          image = gimp_image_new_from_buffer (gimp, GIMP_BUFFER (paste));
         }
 
       if (! image)
@@ -500,7 +500,7 @@ edit_named_paste_as_new_image_invoker (GimpProcedure         *procedure,
 
       if (buffer)
         {
-          image = gimp_image_new_from_buffer (gimp, NULL, buffer);
+          image = gimp_image_new_from_buffer (gimp, buffer);
 
           if (! image)
             success = FALSE;
diff --git a/app/widgets/gimptoolbox-dnd.c b/app/widgets/gimptoolbox-dnd.c
index 7361746..1a422c9 100644
--- a/app/widgets/gimptoolbox-dnd.c
+++ b/app/widgets/gimptoolbox-dnd.c
@@ -225,7 +225,7 @@ gimp_toolbox_drop_buffer (GtkWidget    *widget,
   if (context->gimp->busy)
     return;
 
-  image = gimp_image_new_from_buffer (context->gimp, NULL,
+  image = gimp_image_new_from_buffer (context->gimp,
                                       GIMP_BUFFER (viewable));
   gimp_create_display (image->gimp, image, GIMP_UNIT_PIXEL, 1.0,
                        G_OBJECT (gtk_widget_get_screen (widget)),
diff --git a/tools/pdbgen/pdb/edit.pdb b/tools/pdbgen/pdb/edit.pdb
index 62f7066..02b469e 100644
--- a/tools/pdbgen/pdb/edit.pdb
+++ b/tools/pdbgen/pdb/edit.pdb
@@ -254,7 +254,7 @@ HELP
         }
       else if (GIMP_IS_BUFFER (paste))
         {
-         image = gimp_image_new_from_buffer (gimp, NULL, GIMP_BUFFER (paste));
+         image = gimp_image_new_from_buffer (gimp, GIMP_BUFFER (paste));
         }
 
       if (! image)
@@ -505,7 +505,7 @@ HELP
 
   if (buffer)
     {
-      image = gimp_image_new_from_buffer (gimp, NULL, buffer);
+      image = gimp_image_new_from_buffer (gimp, buffer);
 
       if (! image)
         success = FALSE;


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