From 16749cd395b1abef07f756dc4d029d28fffe4e08 Mon Sep 17 00:00:00 2001 From: Michael Natterer Date: Sun, 7 Jun 2015 01:38:50 +0200 Subject: [PATCH] pdb, app, libgimp: add gimp_image_get_effective_color_profile() which returns the profile that is actually used for the image. And some cleanup and fixes in image_color_profile.pdb. --- app/pdb/image-color-profile-cmds.c | 103 ++++++++++++++++++++--- app/pdb/internal-procs.c | 2 +- libgimp/gimp.def | 1 + libgimp/gimpimagecolorprofile.c | 42 ++++++++- libgimp/gimpimagecolorprofile.h | 7 +- libgimp/gimpimagecolorprofile_pdb.c | 54 +++++++++++- libgimp/gimpimagecolorprofile_pdb.h | 12 +-- tools/pdbgen/pdb/image_color_profile.pdb | 79 ++++++++++++++--- 8 files changed, 265 insertions(+), 35 deletions(-) diff --git a/app/pdb/image-color-profile-cmds.c b/app/pdb/image-color-profile-cmds.c index 3e4c1325cb..f9274300c1 100644 --- a/app/pdb/image-color-profile-cmds.c +++ b/app/pdb/image-color-profile-cmds.c @@ -106,23 +106,72 @@ image_set_color_profile_invoker (GimpProcedure *procedure, if (success) { - GimpColorProfile profile; - - profile = gimp_color_profile_open_from_data (color_profile, num_bytes, error); - - if (profile) + if (color_profile) { - success = gimp_image_set_color_profile (image, profile, error); - gimp_color_profile_close (profile); + GimpColorProfile profile; + + profile = gimp_color_profile_open_from_data (color_profile, num_bytes, + error); + + if (profile) + { + success = gimp_image_set_color_profile (image, profile, error); + gimp_color_profile_close (profile); + } + else + success = FALSE; } else - success = FALSE; + { + success = gimp_image_set_color_profile (image, NULL, error); + } } return gimp_procedure_get_return_values (procedure, success, error ? *error : NULL); } +static GimpValueArray * +image_get_effective_color_profile_invoker (GimpProcedure *procedure, + Gimp *gimp, + GimpContext *context, + GimpProgress *progress, + const GimpValueArray *args, + GError **error) +{ + gboolean success = TRUE; + GimpValueArray *return_vals; + GimpImage *image; + gint32 num_bytes = 0; + guint8 *profile_data = NULL; + + image = gimp_value_get_image (gimp_value_array_index (args, 0), gimp); + + if (success) + { + GimpColorProfile profile; + gsize length; + + profile = gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (image)); + + profile_data = gimp_color_profile_save_to_data (profile, &length, NULL); + num_bytes = length; + + gimp_color_profile_close (profile); + } + + return_vals = gimp_procedure_get_return_values (procedure, success, + error ? *error : NULL); + + if (success) + { + g_value_set_int (gimp_value_array_index (return_vals, 1), num_bytes); + gimp_value_take_int8array (gimp_value_array_index (return_vals, 2), profile_data, num_bytes); + } + + return return_vals; +} + void register_image_color_profile_procs (GimpPDB *pdb) { @@ -137,7 +186,7 @@ register_image_color_profile_procs (GimpPDB *pdb) gimp_procedure_set_static_strings (procedure, "gimp-image-get-color-profile", "Returns the image's color profile", - "This procedure returns the image's color profile.", + "This procedure returns the image's color profile, or NULL if the image has no color profile assigned.", "Michael Natterer ", "Michael Natterer", "2015", @@ -171,7 +220,7 @@ register_image_color_profile_procs (GimpPDB *pdb) gimp_procedure_set_static_strings (procedure, "gimp-image-set-color-profile", "Sets the image's color profile", - "This procedure sets the image's color profile.", + "This procedure sets the image's color profile, or unsets it if NULL is passed as 'color_profile'.", "Michael Natterer ", "Michael Natterer", "2015", @@ -195,4 +244,38 @@ register_image_color_profile_procs (GimpPDB *pdb) GIMP_PARAM_READWRITE)); gimp_pdb_register_procedure (pdb, procedure); g_object_unref (procedure); + + /* + * gimp-image-get-effective-color-profile + */ + procedure = gimp_procedure_new (image_get_effective_color_profile_invoker); + gimp_object_set_static_name (GIMP_OBJECT (procedure), + "gimp-image-get-effective-color-profile"); + gimp_procedure_set_static_strings (procedure, + "gimp-image-get-effective-color-profile", + "Returns the color profile that is used for the image", + "This procedure returns the color profile that is actually used for this image, which is the profile returned by 'gimp-image-get-color-profile' if the image has a profile assigned, or the default RGB profile from preferences if no profile is assigned to the image. If there is no default RGB profile configured in preferences either, a generated default RGB profile is returned.", + "Michael Natterer ", + "Michael Natterer", + "2015", + NULL); + gimp_procedure_add_argument (procedure, + gimp_param_spec_image_id ("image", + "image", + "The image", + pdb->gimp, FALSE, + GIMP_PARAM_READWRITE)); + gimp_procedure_add_return_value (procedure, + gimp_param_spec_int32 ("num-bytes", + "num bytes", + "Number of bytes in the color_profile array", + 0, G_MAXINT32, 0, + GIMP_PARAM_READWRITE)); + gimp_procedure_add_return_value (procedure, + gimp_param_spec_int8_array ("profile-data", + "profile data", + "The image's serialized color profile. The returned value must be freed with g_free()", + GIMP_PARAM_READWRITE)); + gimp_pdb_register_procedure (pdb, procedure); + g_object_unref (procedure); } diff --git a/app/pdb/internal-procs.c b/app/pdb/internal-procs.c index 239cfcc5ab..3cc35b68e0 100644 --- a/app/pdb/internal-procs.c +++ b/app/pdb/internal-procs.c @@ -28,7 +28,7 @@ #include "internal-procs.h" -/* 757 procedures registered total */ +/* 758 procedures registered total */ void internal_procs_init (GimpPDB *pdb) diff --git a/libgimp/gimp.def b/libgimp/gimp.def index 80e0aee6da..a244b05fcc 100644 --- a/libgimp/gimp.def +++ b/libgimp/gimp.def @@ -406,6 +406,7 @@ EXPORTS gimp_image_get_colormap gimp_image_get_component_active gimp_image_get_component_visible + gimp_image_get_effective_color_profile gimp_image_get_exported_uri gimp_image_get_filename gimp_image_get_floating_sel diff --git a/libgimp/gimpimagecolorprofile.c b/libgimp/gimpimagecolorprofile.c index 65b1485e70..bec5c8ea4d 100644 --- a/libgimp/gimpimagecolorprofile.c +++ b/libgimp/gimpimagecolorprofile.c @@ -29,7 +29,8 @@ * * Returns the image's color profile * - * This procedure returns the image's color profile. + * This procedure returns the image's color profile, or NULL if the + * image has no color profile assigned. * * Returns: The image's color profile. The returned value * must be freed with gimp_color_profile_close(). @@ -94,3 +95,42 @@ gimp_image_set_color_profile (gint32 image_ID, return success; } + +/** + * gimp_image_get_effective_color_profile: + * @image_ID: The image. + * + * Returns the color profile that is used for the image. + * + * This procedure returns the color profile that is actually used for + * this image, which is the profile returned by + * gimp_image_get_color_profile() if the image has a profile assigned, + * or the default RGB profile from preferences if no profile is + * assigned to the image. If there is no default RGB profile configured + * in preferences either, a generated default RGB profile is returned. + * + * Returns: The color profile. The returned value + * must be freed with gimp_color_profile_close(). + * + * Since: 2.10 + **/ +GimpColorProfile +gimp_image_get_effective_color_profile (gint32 image_ID) +{ + guint8 *data; + gint length; + + data = _gimp_image_get_effective_color_profile (image_ID, &length); + + if (data) + { + GimpColorProfile profile; + + profile = gimp_color_profile_open_from_data (data, length, NULL); + g_free (data); + + return profile; + } + + return NULL; +} diff --git a/libgimp/gimpimagecolorprofile.h b/libgimp/gimpimagecolorprofile.h index e489b5297c..d6eac9fa3f 100644 --- a/libgimp/gimpimagecolorprofile.h +++ b/libgimp/gimpimagecolorprofile.h @@ -30,10 +30,11 @@ G_BEGIN_DECLS /* For information look into the C source or the html documentation */ -GimpColorProfile gimp_image_get_color_profile (gint32 image_ID); -gboolean gimp_image_set_color_profile (gint32 image_ID, - GimpColorProfile profile); +GimpColorProfile gimp_image_get_color_profile (gint32 image_ID); +gboolean gimp_image_set_color_profile (gint32 image_ID, + GimpColorProfile profile); +GimpColorProfile gimp_image_get_effective_color_profile (gint32 image_ID); G_END_DECLS diff --git a/libgimp/gimpimagecolorprofile_pdb.c b/libgimp/gimpimagecolorprofile_pdb.c index 1979d0e26f..0318a13a31 100644 --- a/libgimp/gimpimagecolorprofile_pdb.c +++ b/libgimp/gimpimagecolorprofile_pdb.c @@ -43,7 +43,8 @@ * * Returns the image's color profile * - * This procedure returns the image's color profile. + * This procedure returns the image's color profile, or NULL if the + * image has no color profile assigned. * * Returns: The image's serialized color profile. The returned value * must be freed with g_free(). @@ -87,7 +88,8 @@ _gimp_image_get_color_profile (gint32 image_ID, * * Sets the image's color profile * - * This procedure sets the image's color profile. + * This procedure sets the image's color profile, or unsets it if NULL + * is passed as 'color_profile'. * * Returns: TRUE on success. * @@ -115,3 +117,51 @@ _gimp_image_set_color_profile (gint32 image_ID, return success; } + +/** + * _gimp_image_get_effective_color_profile: + * @image_ID: The image. + * @num_bytes: Number of bytes in the color_profile array. + * + * Returns the color profile that is used for the image + * + * This procedure returns the color profile that is actually used for + * this image, which is the profile returned by + * gimp_image_get_color_profile() if the image has a profile assigned, + * or the default RGB profile from preferences if no profile is + * assigned to the image. If there is no default RGB profile configured + * in preferences either, a generated default RGB profile is returned. + * + * Returns: The image's serialized color profile. The returned value + * must be freed with g_free(). + * + * Since: 2.10 + **/ +guint8 * +_gimp_image_get_effective_color_profile (gint32 image_ID, + gint *num_bytes) +{ + GimpParam *return_vals; + gint nreturn_vals; + guint8 *profile_data = NULL; + + return_vals = gimp_run_procedure ("gimp-image-get-effective-color-profile", + &nreturn_vals, + GIMP_PDB_IMAGE, image_ID, + GIMP_PDB_END); + + *num_bytes = 0; + + if (return_vals[0].data.d_status == GIMP_PDB_SUCCESS) + { + *num_bytes = return_vals[1].data.d_int32; + profile_data = g_new (guint8, *num_bytes); + memcpy (profile_data, + return_vals[2].data.d_int8array, + *num_bytes * sizeof (guint8)); + } + + gimp_destroy_params (return_vals, nreturn_vals); + + return profile_data; +} diff --git a/libgimp/gimpimagecolorprofile_pdb.h b/libgimp/gimpimagecolorprofile_pdb.h index c52daba14c..17ec0899f2 100644 --- a/libgimp/gimpimagecolorprofile_pdb.h +++ b/libgimp/gimpimagecolorprofile_pdb.h @@ -32,11 +32,13 @@ G_BEGIN_DECLS /* For information look into the C source or the html documentation */ -G_GNUC_INTERNAL guint8* _gimp_image_get_color_profile (gint32 image_ID, - gint *num_bytes); -G_GNUC_INTERNAL gboolean _gimp_image_set_color_profile (gint32 image_ID, - gint num_bytes, - const guint8 *color_profile); +G_GNUC_INTERNAL guint8* _gimp_image_get_color_profile (gint32 image_ID, + gint *num_bytes); +G_GNUC_INTERNAL gboolean _gimp_image_set_color_profile (gint32 image_ID, + gint num_bytes, + const guint8 *color_profile); +G_GNUC_INTERNAL guint8* _gimp_image_get_effective_color_profile (gint32 image_ID, + gint *num_bytes); G_END_DECLS diff --git a/tools/pdbgen/pdb/image_color_profile.pdb b/tools/pdbgen/pdb/image_color_profile.pdb index f9624d805b..0b14690503 100644 --- a/tools/pdbgen/pdb/image_color_profile.pdb +++ b/tools/pdbgen/pdb/image_color_profile.pdb @@ -20,7 +20,8 @@ sub image_get_color_profile { $blurb = "Returns the image's color profile"; $help = <<'HELP'; -This procedure returns the image's color profile. +This procedure returns the image's color profile, or NULL if the image +has no color profile assigned. HELP &mitch_pdb_misc('2015', '2.10'); @@ -38,7 +39,6 @@ HELP ); %invoke = ( - headers => [ qw("core/gimpimage-profile.h") ], code => <<'CODE' { GimpColorProfile profile; @@ -63,7 +63,8 @@ sub image_set_color_profile { $blurb = "Sets the image's color profile"; $help = <<'HELP'; -This procedure sets the image's color profile. +This procedure sets the image's color profile, or unsets it if NULL is +passed as 'color_profile'. HELP &mitch_pdb_misc('2015', '2.10'); @@ -78,20 +79,70 @@ HELP ); %invoke = ( - headers => [ qw("core/gimpimage-profile.h") ], + code => <<'CODE' +{ + if (color_profile) + { + GimpColorProfile profile; + + profile = gimp_color_profile_open_from_data (color_profile, num_bytes, + error); + + if (profile) + { + success = gimp_image_set_color_profile (image, profile, error); + gimp_color_profile_close (profile); + } + else + success = FALSE; + } + else + { + success = gimp_image_set_color_profile (image, NULL, error); + } +} +CODE + ); +} + +sub image_get_effective_color_profile { + $blurb = "Returns the color profile that is used for the image"; + + $help = <<'HELP'; +This procedure returns the color profile that is actually used for +this image, which is the profile returned by +gimp_image_get_color_profile() if the image has a profile assigned, or +the default RGB profile from preferences if no profile is assigned to +the image. If there is no default RGB profile configured in +preferences either, a generated default RGB profile is returned. +HELP + + &mitch_pdb_misc('2015', '2.10'); + + @inargs = ( + { name => 'image', type => 'image', + desc => 'The image' } + ); + + @outargs = ( + { name => 'profile_data', type => 'int8array', wrap => 1, + desc => "The image's serialized color profile. The returned value must be freed with g_free()", + array => { name => 'num_bytes', + desc => 'Number of bytes in the color_profile array' } } + ); + + %invoke = ( code => <<'CODE' { GimpColorProfile profile; + gsize length; - profile = gimp_color_profile_open_from_data (color_profile, num_bytes, error); + profile = gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (image)); - if (profile) - { - success = gimp_image_set_color_profile (image, profile, error); - gimp_color_profile_close (profile); - } - else - success = FALSE; + profile_data = gimp_color_profile_save_to_data (profile, &length, NULL); + num_bytes = length; + + gimp_color_profile_close (profile); } CODE ); @@ -99,10 +150,12 @@ CODE @headers = qw( "libgimpcolor/gimpcolor.h" + "core/gimpimage-profile.h" "gimp-intl.h"); @procs = qw(image_get_color_profile - image_set_color_profile); + image_set_color_profile + image_get_effective_color_profile); %exports = (app => [@procs], lib => [@procs]);