[gimp] libgimp: add gimp_layer,chanel,vectors,etc,_get_by_id()



commit fd5740e70ba4c5c1a2324592f2a4f49d4187a69e
Author: Michael Natterer <mitch gimp org>
Date:   Tue Sep 3 10:24:24 2019 +0200

    libgimp: add gimp_layer,chanel,vectors,etc,_get_by_id()
    
    which call gimp_item_get_by_id() and additionally check if the
    returned item has the right type, and return NULL if not.
    
    This is both shorter and more readable than
    
    layer = GIMP_LAYER (gimp_item_get_by_id (id));
    
    and additionally makes sure we don't cast e.g. a non-layer with
    GIMP_LAYER(), which will give criticals but shouldn't, because the
    wrong IDs can come from anywhere and are an input problem and not a
    programming error (criticals are for programming errors).

 libgimp/gimp.def        |  6 +++++
 libgimp/gimpchannel.c   | 26 +++++++++++++++++++
 libgimp/gimpchannel.h   | 18 ++++++++------
 libgimp/gimpdrawable.c  | 25 +++++++++++++++++++
 libgimp/gimpdrawable.h  | 66 +++++++++++++++++++++++++------------------------
 libgimp/gimplayer.c     | 26 +++++++++++++++++++
 libgimp/gimplayer.h     |  2 ++
 libgimp/gimplayermask.c | 26 +++++++++++++++++++
 libgimp/gimplayermask.h |  4 ++-
 libgimp/gimpselection.c | 26 +++++++++++++++++++
 libgimp/gimpselection.h | 12 +++++----
 libgimp/gimpvectors.c   | 26 +++++++++++++++++++
 libgimp/gimpvectors.h   |  4 ++-
 13 files changed, 220 insertions(+), 47 deletions(-)
---
diff --git a/libgimp/gimp.def b/libgimp/gimp.def
index 109bb43063..3e85ee6959 100644
--- a/libgimp/gimp.def
+++ b/libgimp/gimp.def
@@ -41,6 +41,7 @@ EXPORTS
        gimp_buffers_get_list
        gimp_channel_combine_masks
        gimp_channel_copy
+       gimp_channel_get_by_id
        gimp_channel_get_color
        gimp_channel_get_opacity
        gimp_channel_get_show_masked
@@ -206,6 +207,7 @@ EXPORTS
        gimp_drawable_free_shadow
        gimp_drawable_get_buffer
        gimp_drawable_get_buffer_deprecated
+       gimp_drawable_get_by_id
        gimp_drawable_get_format
        gimp_drawable_get_format_deprecated
        gimp_drawable_get_pixel
@@ -555,6 +557,7 @@ EXPORTS
        gimp_layer_from_mask
        gimp_layer_get_apply_mask
        gimp_layer_get_blend_space
+       gimp_layer_get_by_id
        gimp_layer_get_composite_mode
        gimp_layer_get_composite_space
        gimp_layer_get_edit_mask
@@ -566,6 +569,7 @@ EXPORTS
        gimp_layer_get_type
        gimp_layer_group_new
        gimp_layer_is_floating_sel
+       gimp_layer_mask_get_by_id
        gimp_layer_mask_get_type
        gimp_layer_mode_get_type
        gimp_layer_new
@@ -738,6 +742,7 @@ EXPORTS
        gimp_selection_feather
        gimp_selection_float
        gimp_selection_flood
+       gimp_selection_get_by_id
        gimp_selection_get_type
        gimp_selection_grow
        gimp_selection_invert
@@ -796,6 +801,7 @@ EXPORTS
        gimp_vectors_copy
        gimp_vectors_export_to_file
        gimp_vectors_export_to_string
+       gimp_vectors_get_by_id
        gimp_vectors_get_strokes
        gimp_vectors_get_type
        gimp_vectors_import_from_file
diff --git a/libgimp/gimpchannel.c b/libgimp/gimpchannel.c
index 6bb657b5eb..d8e9ebc7c4 100644
--- a/libgimp/gimpchannel.c
+++ b/libgimp/gimpchannel.c
@@ -45,6 +45,32 @@ gimp_channel_init (GimpChannel *channel)
   channel->priv = gimp_channel_get_instance_private (channel);
 }
 
+/**
+ * gimp_channel_get_by_id:
+ * @channel_id: The channel id.
+ *
+ * Returns a #GimpChannel representing @channel_id. This function
+ * calls gimp_item_get_by_id() and returns the item if it is channel
+ * or %NULL otherwise.
+ *
+ * Returns: (nullable) (transfer none): a #GimpChannel for @channel_id
+ *          or %NULL if @channel_id does not represent a valid
+ *          channel. The object belongs to libgimp and you must not
+ *          modify or unref it.
+ *
+ * Since: 3.0
+ **/
+GimpChannel *
+gimp_channel_get_by_id (gint32 channel_id)
+{
+  GimpItem *item = gimp_item_get_by_id (channel_id);
+
+  if (GIMP_IS_CHANNEL (item))
+    return (GimpChannel *) item;
+
+  return NULL;
+}
+
 /**
  * gimp_channel_new:
  * @image:   The image to which to add the channel.
diff --git a/libgimp/gimpchannel.h b/libgimp/gimpchannel.h
index 4b9557fbe3..3e02fc0111 100644
--- a/libgimp/gimpchannel.h
+++ b/libgimp/gimpchannel.h
@@ -66,14 +66,16 @@ struct _GimpChannelClass
 };
 
 
-GType         gimp_channel_get_type (void) G_GNUC_CONST;
-
-GimpChannel * gimp_channel_new      (GimpImage     *image,
-                                     const gchar   *name,
-                                     guint          width,
-                                     guint          height,
-                                     gdouble        opacity,
-                                     const GimpRGB *color);
+GType         gimp_channel_get_type  (void) G_GNUC_CONST;
+
+GimpChannel * gimp_channel_get_by_id (gint32         channel_id);
+
+GimpChannel * gimp_channel_new       (GimpImage     *image,
+                                      const gchar   *name,
+                                      guint          width,
+                                      guint          height,
+                                      gdouble        opacity,
+                                      const GimpRGB *color);
 
 
 G_END_DECLS
diff --git a/libgimp/gimpdrawable.c b/libgimp/gimpdrawable.c
index ce7bea3648..d5b803a210 100644
--- a/libgimp/gimpdrawable.c
+++ b/libgimp/gimpdrawable.c
@@ -51,6 +51,31 @@ gimp_drawable_init (GimpDrawable *drawable)
 
 /* Public API. */
 
+/**
+ * gimp_drawable_get_by_id:
+ * @drawable_id: The drawable id.
+ *
+ * Returns a #GimpDrawable representing @drawable_id. This function
+ * calls gimp_item_get_by_id() and returns the item if it is drawable
+ * or %NULL otherwise.
+ *
+ * Returns: (nullable) (transfer none): a #GimpDrawable for
+ *          @drawable_id or %NULL if @drawable_id does not represent a
+ *          valid drawable. The object belongs to libgimp and you must
+ *          not modify or unref it.
+ *
+ * Since: 3.0
+ **/
+GimpDrawable *
+gimp_drawable_get_by_id (gint32 drawable_id)
+{
+  GimpItem *item = gimp_item_get_by_id (drawable_id);
+
+  if (GIMP_IS_DRAWABLE (item))
+    return (GimpDrawable *) item;
+
+  return NULL;
+}
 
 /**
  * gimp_drawable_get_thumbnail_data:
diff --git a/libgimp/gimpdrawable.h b/libgimp/gimpdrawable.h
index 8af39b851f..2041fb1c5d 100644
--- a/libgimp/gimpdrawable.h
+++ b/libgimp/gimpdrawable.h
@@ -67,41 +67,43 @@ struct _GimpDrawableClass
 };
 
 
-GType        gimp_drawable_get_type               (void) G_GNUC_CONST;
+GType          gimp_drawable_get_type               (void) G_GNUC_CONST;
+
+GimpDrawable * gimp_drawable_get_by_id              (gint32        drawable_id);
 
 #ifndef GIMP_DEPRECATED_REPLACE_NEW_API
 
-GeglBuffer * gimp_drawable_get_buffer             (GimpDrawable  *drawable);
-GeglBuffer * gimp_drawable_get_shadow_buffer      (GimpDrawable  *drawable);
-
-const Babl * gimp_drawable_get_format             (GimpDrawable  *drawable);
-const Babl * gimp_drawable_get_thumbnail_format   (GimpDrawable  *drawable);
-
-guchar     * gimp_drawable_get_thumbnail_data     (GimpDrawable  *drawable,
-                                                   gint          *width,
-                                                   gint          *height,
-                                                   gint          *bpp);
-GdkPixbuf  * gimp_drawable_get_thumbnail          (GimpDrawable  *drawable,
-                                                   gint           width,
-                                                   gint           height,
-                                                   GimpPixbufTransparency alpha);
-
-guchar     * gimp_drawable_get_sub_thumbnail_data (GimpDrawable  *drawable,
-                                                   gint           src_x,
-                                                   gint           src_y,
-                                                   gint           src_width,
-                                                   gint           src_height,
-                                                   gint          *dest_width,
-                                                   gint          *dest_height,
-                                                   gint          *bpp);
-GdkPixbuf  * gimp_drawable_get_sub_thumbnail      (GimpDrawable  *drawable,
-                                                   gint           src_x,
-                                                   gint           src_y,
-                                                   gint           src_width,
-                                                   gint           src_height,
-                                                   gint           dest_width,
-                                                   gint           dest_height,
-                                                   GimpPixbufTransparency alpha);
+GeglBuffer   * gimp_drawable_get_buffer             (GimpDrawable  *drawable);
+GeglBuffer   * gimp_drawable_get_shadow_buffer      (GimpDrawable  *drawable);
+
+const Babl   * gimp_drawable_get_format             (GimpDrawable  *drawable);
+const Babl   * gimp_drawable_get_thumbnail_format   (GimpDrawable  *drawable);
+
+guchar       * gimp_drawable_get_thumbnail_data     (GimpDrawable  *drawable,
+                                                     gint          *width,
+                                                     gint          *height,
+                                                     gint          *bpp);
+GdkPixbuf    * gimp_drawable_get_thumbnail          (GimpDrawable  *drawable,
+                                                     gint           width,
+                                                     gint           height,
+                                                     GimpPixbufTransparency alpha);
+
+guchar       * gimp_drawable_get_sub_thumbnail_data (GimpDrawable  *drawable,
+                                                     gint           src_x,
+                                                     gint           src_y,
+                                                     gint           src_width,
+                                                     gint           src_height,
+                                                     gint          *dest_width,
+                                                     gint          *dest_height,
+                                                     gint          *bpp);
+GdkPixbuf    * gimp_drawable_get_sub_thumbnail      (GimpDrawable  *drawable,
+                                                     gint           src_x,
+                                                     gint           src_y,
+                                                     gint           src_width,
+                                                     gint           src_height,
+                                                     gint           dest_width,
+                                                     gint           dest_height,
+                                                     GimpPixbufTransparency alpha);
 
 #else /* GIMP_DEPRECATED_REPLACE_NEW_API */
 
diff --git a/libgimp/gimplayer.c b/libgimp/gimplayer.c
index 677df53587..63a6f3026f 100644
--- a/libgimp/gimplayer.c
+++ b/libgimp/gimplayer.c
@@ -50,6 +50,32 @@ gimp_layer_init (GimpLayer *layer)
 
 /* Public API. */
 
+/**
+ * gimp_layer_get_by_id:
+ * @layer_id: The layer id.
+ *
+ * Returns a #GimpLayer representing @layer_id. This function calls
+ * gimp_item_get_by_id() and returns the item if it is layer or %NULL
+ * otherwise.
+ *
+ * Returns: (nullable) (transfer none): a #GimpLayer for @layer_id or
+ *          %NULL if @layer_id does not represent a valid layer. The
+ *          object belongs to libgimp and you must not modify or unref
+ *          it.
+ *
+ * Since: 3.0
+ **/
+GimpLayer *
+gimp_layer_get_by_id (gint32 layer_id)
+{
+  GimpItem *item = gimp_item_get_by_id (layer_id);
+
+  if (GIMP_IS_LAYER (item))
+    return (GimpLayer *) item;
+
+  return NULL;
+}
+
 /**
  * gimp_layer_new:
  * @image:   The image to which to add the layer.
diff --git a/libgimp/gimplayer.h b/libgimp/gimplayer.h
index 9885a856a1..b032a8eb9c 100644
--- a/libgimp/gimplayer.h
+++ b/libgimp/gimplayer.h
@@ -66,6 +66,8 @@ struct _GimpLayerClass
 
 GType       gimp_layer_get_type           (void) G_GNUC_CONST;
 
+GimpLayer * gimp_layer_get_by_id          (gint32           layer_id);
+
 #ifndef GIMP_DEPRECATED_REPLACE_NEW_API
 
 GimpLayer * gimp_layer_new                (GimpImage       *image,
diff --git a/libgimp/gimplayermask.c b/libgimp/gimplayermask.c
index d955facbde..f1ca5c3353 100644
--- a/libgimp/gimplayermask.c
+++ b/libgimp/gimplayermask.c
@@ -45,3 +45,29 @@ gimp_layer_mask_init (GimpLayerMask *layer_mask)
 {
   layer_mask->priv = gimp_layer_mask_get_instance_private (layer_mask);
 }
+
+/**
+ * gimp_layer_mask_get_by_id:
+ * @layer_mask_id: The layer_mask id.
+ *
+ * Returns a #GimpLayerMask representing @layer_mask_id. This function
+ * calls gimp_item_get_by_id() and returns the item if it is
+ * layer_mask or %NULL otherwise.
+ *
+ * Returns: (nullable) (transfer none): a #GimpLayerMask for
+ *          @layer_mask_id or %NULL if @layer_mask_id does not
+ *          represent a valid layer_mask. The object belongs to
+ *          libgimp and you must not modify or unref it.
+ *
+ * Since: 3.0
+ **/
+GimpLayerMask *
+gimp_layer_mask_get_by_id (gint32 layer_mask_id)
+{
+  GimpItem *item = gimp_item_get_by_id (layer_mask_id);
+
+  if (GIMP_IS_LAYER_MASK (item))
+    return (GimpLayerMask *) item;
+
+  return NULL;
+}
diff --git a/libgimp/gimplayermask.h b/libgimp/gimplayermask.h
index 90fa951e9b..b7560ee148 100644
--- a/libgimp/gimplayermask.h
+++ b/libgimp/gimplayermask.h
@@ -64,7 +64,9 @@ struct _GimpLayerMaskClass
   void (*_gimp_reserved8) (void);
 };
 
-GType   gimp_layer_mask_get_type (void) G_GNUC_CONST;
+GType           gimp_layer_mask_get_type  (void) G_GNUC_CONST;
+
+GimpLayerMask * gimp_layer_mask_get_by_id (gint32 layer_mask_id);
 
 
 G_END_DECLS
diff --git a/libgimp/gimpselection.c b/libgimp/gimpselection.c
index e4a8aa7176..f3b3fa1187 100644
--- a/libgimp/gimpselection.c
+++ b/libgimp/gimpselection.c
@@ -45,6 +45,32 @@ gimp_selection_init (GimpSelection *selection)
   selection->priv = gimp_selection_get_instance_private (selection);
 }
 
+/**
+ * gimp_selection_get_by_id:
+ * @selection_id: The selection id.
+ *
+ * Returns a #GimpSelection representing @selection_id. This function
+ * calls gimp_item_get_by_id() and returns the item if it is selection
+ * or %NULL otherwise.
+ *
+ * Returns: (nullable) (transfer none): a #GimpSelection for
+ *          @selection_id or %NULL if @selection_id does not represent
+ *          a valid selection. The object belongs to libgimp and you
+ *          must not modify or unref it.
+ *
+ * Since: 3.0
+ **/
+GimpSelection *
+gimp_selection_get_by_id (gint32 selection_id)
+{
+  GimpItem *item = gimp_item_get_by_id (selection_id);
+
+  if (GIMP_IS_SELECTION (item))
+    return (GimpSelection *) item;
+
+  return NULL;
+}
+
 /**
  * gimp_selection_float:
  * @image:    ignored
diff --git a/libgimp/gimpselection.h b/libgimp/gimpselection.h
index 509ee01410..c2a1525c56 100644
--- a/libgimp/gimpselection.h
+++ b/libgimp/gimpselection.h
@@ -64,12 +64,14 @@ struct _GimpSelectionClass
 };
 
 
-GType       gimp_selection_get_type (void) G_GNUC_CONST;
+GType           gimp_selection_get_type (void) G_GNUC_CONST;
 
-GimpLayer * gimp_selection_float    (GimpImage    *image,
-                                     GimpDrawable *drawable,
-                                     gint          offx,
-                                     gint          offy);
+GimpSelection * gimp_selection_get_by_id (gint32        selection_id);
+
+GimpLayer     * gimp_selection_float     (GimpImage    *image,
+                                          GimpDrawable *drawable,
+                                          gint          offx,
+                                          gint          offy);
 
 
 G_END_DECLS
diff --git a/libgimp/gimpvectors.c b/libgimp/gimpvectors.c
index 13402a6669..e11b4d6902 100644
--- a/libgimp/gimpvectors.c
+++ b/libgimp/gimpvectors.c
@@ -45,3 +45,29 @@ gimp_vectors_init (GimpVectors *vectors)
 {
   vectors->priv = gimp_vectors_get_instance_private (vectors);
 }
+
+/**
+ * gimp_vectors_get_by_id:
+ * @vectors_id: The vectors id.
+ *
+ * Returns a #GimpVectors representing @vectors_id. This function
+ * calls gimp_item_get_by_id() and returns the item if it is vectors
+ * or %NULL otherwise.
+ *
+ * Returns: (nullable) (transfer none): a #GimpVectors for @vectors_id
+ *          or %NULL if @vectors_id does not represent a valid
+ *          vectors. The object belongs to libgimp and you must not
+ *          modify or unref it.
+ *
+ * Since: 3.0
+ **/
+GimpVectors *
+gimp_vectors_get_by_id (gint32 vectors_id)
+{
+  GimpItem *item = gimp_item_get_by_id (vectors_id);
+
+  if (GIMP_IS_VECTORS (item))
+    return (GimpVectors *) item;
+
+  return NULL;
+}
diff --git a/libgimp/gimpvectors.h b/libgimp/gimpvectors.h
index ff76d9a27a..90889a3a71 100644
--- a/libgimp/gimpvectors.h
+++ b/libgimp/gimpvectors.h
@@ -65,7 +65,9 @@ struct _GimpVectorsClass
 };
 
 
-GType   gimp_vectors_get_type (void) G_GNUC_CONST;
+GType         gimp_vectors_get_type  (void) G_GNUC_CONST;
+
+GimpVectors * gimp_vectors_get_by_id (gint32 vectors_id);
 
 
 G_END_DECLS


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