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).
This commit is contained in:
Michael Natterer 2019-09-03 10:24:24 +02:00
parent 71f767efc0
commit fd5740e70b
13 changed files with 216 additions and 43 deletions

View file

@ -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

View file

@ -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.

View file

@ -66,14 +66,16 @@ struct _GimpChannelClass
};
GType gimp_channel_get_type (void) G_GNUC_CONST;
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);
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

View file

@ -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:

View file

@ -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);
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);
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_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);
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 */

View file

@ -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.

View file

@ -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,

View file

@ -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;
}

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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;
}

View file

@ -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