libgimpcolor: change gimp_lcms_profile_open_from_file() from filename to GFile

and change most of the lcms plug-in to using GFile too.
This commit is contained in:
Michael Natterer 2014-07-04 23:57:27 +02:00
parent c885af6c3e
commit b7863269f0
6 changed files with 163 additions and 86 deletions

View File

@ -154,16 +154,20 @@ gimp_image_get_profile (GimpImage *image,
} }
else if (config->rgb_profile) else if (config->rgb_profile)
{ {
profile = gimp_lcms_profile_open_from_file (config->rgb_profile, error); GFile *file = g_file_new_for_path (config->rgb_profile);
profile = gimp_lcms_profile_open_from_file (file, error);
if (profile && ! gimp_lcms_profile_is_rgb (profile)) if (profile && ! gimp_lcms_profile_is_rgb (profile))
{ {
g_set_error (error, GIMP_ERROR, GIMP_FAILED, g_set_error (error, GIMP_ERROR, GIMP_FAILED,
_("Color profile '%s' is not for RGB color space"), _("Color profile '%s' is not for RGB color space"),
gimp_filename_to_utf8 (config->rgb_profile)); gimp_file_get_utf8_name (file));
cmsCloseProfile (profile); cmsCloseProfile (profile);
profile = NULL; profile = NULL;
} }
g_object_unref (file);
} }
return profile; return profile;

View File

@ -62,33 +62,75 @@ gimp_lcms_error_quark (void)
} }
GimpColorProfile GimpColorProfile
gimp_lcms_profile_open_from_file (const gchar *filename, gimp_lcms_profile_open_from_file (GFile *file,
GError **error) GError **error)
{ {
GimpColorProfile profile; GimpColorProfile profile = NULL;
GMappedFile *file; gchar *path;
const guint8 *data;
gsize length;
g_return_val_if_fail (filename != NULL, NULL); g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL);
file = g_mapped_file_new (filename, FALSE, error); path = g_file_get_path (file);
if (! file) if (path)
return NULL; {
GMappedFile *mapped;
const guint8 *data;
gsize length;
data = (const guint8 *) g_mapped_file_get_contents (file); mapped = g_mapped_file_new (path, FALSE, error);
length = g_mapped_file_get_length (file);
profile = cmsOpenProfileFromMem (data, length); if (! mapped)
return NULL;
if (! profile) data = (const guint8 *) g_mapped_file_get_contents (mapped);
length = g_mapped_file_get_length (mapped);
profile = cmsOpenProfileFromMem (data, length);
g_mapped_file_unref (mapped);
}
else
{
GFileInfo *info;
info = g_file_query_info (file,
G_FILE_ATTRIBUTE_STANDARD_SIZE,
G_FILE_QUERY_INFO_NONE,
NULL, error);
if (info)
{
GInputStream *input;
goffset length = g_file_info_get_size (info);
guint8 *data = g_malloc (length);
g_object_unref (info);
input = G_INPUT_STREAM (g_file_read (file, NULL, error));
if (input)
{
gsize bytes_read;
if (g_input_stream_read_all (input, data, length,
&bytes_read, NULL, error) &&
bytes_read == length)
{
profile = cmsOpenProfileFromMem (data, length);
}
g_object_unref (input);
}
g_free (data);
}
}
if (! profile && error && *error == NULL)
g_set_error (error, gimp_lcms_error_quark (), 0, g_set_error (error, gimp_lcms_error_quark (), 0,
_("'%s' does not appear to be an ICC color profile"), _("'%s' does not appear to be an ICC color profile"),
gimp_filename_to_utf8 (filename)); gimp_file_get_utf8_name (file));
g_mapped_file_unref (file);
return profile; return profile;
} }

View File

@ -32,7 +32,7 @@ G_BEGIN_DECLS
/* For information look into the C source or the html documentation */ /* For information look into the C source or the html documentation */
GimpColorProfile gimp_lcms_profile_open_from_file (const gchar *filename, GimpColorProfile gimp_lcms_profile_open_from_file (GFile *file,
GError **error); GError **error);
GimpColorProfile gimp_lcms_profile_open_from_data (const guint8 *data, GimpColorProfile gimp_lcms_profile_open_from_data (const guint8 *data,
gsize length, gsize length,

View File

@ -186,18 +186,18 @@ static void
gimp_color_profile_chooser_dialog_update_preview (GimpColorProfileChooserDialog *dialog) gimp_color_profile_chooser_dialog_update_preview (GimpColorProfileChooserDialog *dialog)
{ {
GimpColorProfile profile; GimpColorProfile profile;
gchar *filename; GFile *file;
GError *error = NULL; GError *error = NULL;
filename = gtk_file_chooser_get_preview_filename (GTK_FILE_CHOOSER (dialog)); file = gtk_file_chooser_get_preview_file (GTK_FILE_CHOOSER (dialog));
if (! filename) if (! file)
{ {
gimp_color_profile_view_set_profile (dialog->priv->profile_view, NULL); gimp_color_profile_view_set_profile (dialog->priv->profile_view, NULL);
return; return;
} }
profile = gimp_lcms_profile_open_from_file (filename, &error); profile = gimp_lcms_profile_open_from_file (file, &error);
if (! profile) if (! profile)
{ {
@ -212,5 +212,5 @@ gimp_color_profile_chooser_dialog_update_preview (GimpColorProfileChooserDialog
cmsCloseProfile (profile); cmsCloseProfile (profile);
} }
g_free (filename); g_object_unref (file);
} }

View File

@ -425,10 +425,13 @@ gimp_color_profile_combo_box_set_active (GimpColorProfileComboBox *combo,
if (filename && ! (label && *label)) if (filename && ! (label && *label))
{ {
GFile *file;
cmsHPROFILE profile; cmsHPROFILE profile;
GError *error = NULL; GError *error = NULL;
profile = gimp_lcms_profile_open_from_file (filename, &error); file = g_file_new_for_path (filename);
profile = gimp_lcms_profile_open_from_file (file, &error);
g_object_unref (file);
if (! profile) if (! profile)
{ {

View File

@ -85,11 +85,11 @@ static void run (const gchar *name,
static GimpPDBStatusType lcms_icc_set (GimpColorConfig *config, static GimpPDBStatusType lcms_icc_set (GimpColorConfig *config,
gint32 image, gint32 image,
const gchar *filename); GFile *file);
static GimpPDBStatusType lcms_icc_apply (GimpColorConfig *config, static GimpPDBStatusType lcms_icc_apply (GimpColorConfig *config,
GimpRunMode run_mode, GimpRunMode run_mode,
gint32 image, gint32 image,
const gchar *filename, GFile *file,
GimpColorRenderingIntent intent, GimpColorRenderingIntent intent,
gboolean bpc, gboolean bpc,
gboolean *dont_ask); gboolean *dont_ask);
@ -98,7 +98,7 @@ static GimpPDBStatusType lcms_icc_info (GimpColorConfig *config,
gchar **name, gchar **name,
gchar **desc, gchar **desc,
gchar **info); gchar **info);
static GimpPDBStatusType lcms_icc_file_info (const gchar *filename, static GimpPDBStatusType lcms_icc_file_info (GFile *file,
gchar **name, gchar **name,
gchar **desc, gchar **desc,
gchar **info, gchar **info,
@ -109,11 +109,11 @@ static cmsHPROFILE lcms_image_get_profile (GimpColorConfig *config,
GError **error); GError **error);
static gboolean lcms_image_set_profile (gint32 image, static gboolean lcms_image_set_profile (gint32 image,
cmsHPROFILE profile, cmsHPROFILE profile,
const gchar *filename); GFile *file);
static gboolean lcms_image_apply_profile (gint32 image, static gboolean lcms_image_apply_profile (gint32 image,
cmsHPROFILE src_profile, cmsHPROFILE src_profile,
cmsHPROFILE dest_profile, cmsHPROFILE dest_profile,
const gchar *filename, GFile *file,
GimpColorRenderingIntent intent, GimpColorRenderingIntent intent,
gboolean bpc); gboolean bpc);
static void lcms_image_transform_rgb (gint32 image, static void lcms_image_transform_rgb (gint32 image,
@ -320,7 +320,7 @@ run (const gchar *name,
gint proc = NONE; gint proc = NONE;
GimpRunMode run_mode = GIMP_RUN_NONINTERACTIVE; GimpRunMode run_mode = GIMP_RUN_NONINTERACTIVE;
gint32 image = -1; gint32 image = -1;
const gchar *filename = NULL; GFile *file = NULL;
GimpColorConfig *config = NULL; GimpColorConfig *config = NULL;
gboolean dont_ask = FALSE; gboolean dont_ask = FALSE;
GimpColorRenderingIntent intent; GimpColorRenderingIntent intent;
@ -365,14 +365,14 @@ run (const gchar *name,
run_mode = param[0].data.d_int32; run_mode = param[0].data.d_int32;
image = param[1].data.d_image; image = param[1].data.d_image;
if (nparams > 2) if (nparams > 2)
filename = param[2].data.d_string; file = g_file_new_for_path (param[2].data.d_string);
break; break;
case PROC_APPLY: case PROC_APPLY:
run_mode = param[0].data.d_int32; run_mode = param[0].data.d_int32;
image = param[1].data.d_image; image = param[1].data.d_image;
if (nparams > 2) if (nparams > 2)
filename = param[2].data.d_string; file = g_file_new_for_path (param[2].data.d_string);
if (nparams > 3) if (nparams > 3)
intent = param[3].data.d_int32; intent = param[3].data.d_int32;
if (nparams > 4) if (nparams > 4)
@ -398,7 +398,7 @@ run (const gchar *name,
break; break;
case PROC_FILE_INFO: case PROC_FILE_INFO:
filename = param[0].data.d_string; file = g_file_new_for_path (param[0].data.d_string);
break; break;
} }
@ -430,13 +430,13 @@ run (const gchar *name,
{ {
case PROC_SET: case PROC_SET:
case PROC_SET_RGB: case PROC_SET_RGB:
status = lcms_icc_set (config, image, filename); status = lcms_icc_set (config, image, file);
break; break;
case PROC_APPLY: case PROC_APPLY:
case PROC_APPLY_RGB: case PROC_APPLY_RGB:
status = lcms_icc_apply (config, run_mode, status = lcms_icc_apply (config, run_mode,
image, filename, intent, bpc, image, file, intent, bpc,
&dont_ask); &dont_ask);
if (run_mode == GIMP_RUN_INTERACTIVE) if (run_mode == GIMP_RUN_INTERACTIVE)
@ -459,7 +459,7 @@ run (const gchar *name,
if (proc == PROC_INFO) if (proc == PROC_INFO)
status = lcms_icc_info (config, image, &name, &desc, &info); status = lcms_icc_info (config, image, &name, &desc, &info);
else else
status = lcms_icc_file_info (filename, &name, &desc, &info, &error); status = lcms_icc_file_info (file, &name, &desc, &info, &error);
if (status == GIMP_PDB_SUCCESS) if (status == GIMP_PDB_SUCCESS)
{ {
@ -492,23 +492,31 @@ run (const gchar *name,
if (config) if (config)
g_object_unref (config); g_object_unref (config);
if (file)
g_object_unref (file);
values[0].data.d_status = status; values[0].data.d_status = status;
} }
static GimpPDBStatusType static GimpPDBStatusType
lcms_icc_set (GimpColorConfig *config, lcms_icc_set (GimpColorConfig *config,
gint32 image, gint32 image,
const gchar *filename) GFile *file)
{ {
gboolean success; gboolean success;
g_return_val_if_fail (GIMP_IS_COLOR_CONFIG (config), GIMP_PDB_CALLING_ERROR); g_return_val_if_fail (GIMP_IS_COLOR_CONFIG (config), GIMP_PDB_CALLING_ERROR);
g_return_val_if_fail (image != -1, GIMP_PDB_CALLING_ERROR); g_return_val_if_fail (image != -1, GIMP_PDB_CALLING_ERROR);
if (! filename) if (file)
filename = config->rgb_profile; g_object_ref (file);
else if (config->rgb_profile)
file = g_file_new_for_path (config->rgb_profile);
success = lcms_image_set_profile (image, NULL, filename); success = lcms_image_set_profile (image, NULL, file);
if (file)
g_object_unref (file);
return success ? GIMP_PDB_SUCCESS : GIMP_PDB_EXECUTION_ERROR; return success ? GIMP_PDB_SUCCESS : GIMP_PDB_EXECUTION_ERROR;
} }
@ -517,7 +525,7 @@ static GimpPDBStatusType
lcms_icc_apply (GimpColorConfig *config, lcms_icc_apply (GimpColorConfig *config,
GimpRunMode run_mode, GimpRunMode run_mode,
gint32 image, gint32 image,
const gchar *filename, GFile *file,
GimpColorRenderingIntent intent, GimpColorRenderingIntent intent,
gboolean bpc, gboolean bpc,
gboolean *dont_ask) gboolean *dont_ask)
@ -530,14 +538,16 @@ lcms_icc_apply (GimpColorConfig *config,
g_return_val_if_fail (GIMP_IS_COLOR_CONFIG (config), GIMP_PDB_CALLING_ERROR); g_return_val_if_fail (GIMP_IS_COLOR_CONFIG (config), GIMP_PDB_CALLING_ERROR);
g_return_val_if_fail (image != -1, GIMP_PDB_CALLING_ERROR); g_return_val_if_fail (image != -1, GIMP_PDB_CALLING_ERROR);
if (! filename) if (file)
filename = config->rgb_profile; g_object_ref (file);
else if (config->rgb_profile)
file = g_file_new_for_path (config->rgb_profile);
if (filename) if (file)
{ {
GError *error = NULL; GError *error = NULL;
dest_profile = gimp_lcms_profile_open_from_file (filename, &error); dest_profile = gimp_lcms_profile_open_from_file (file, &error);
if (! dest_profile) if (! dest_profile)
{ {
@ -550,9 +560,10 @@ lcms_icc_apply (GimpColorConfig *config,
if (! gimp_lcms_profile_is_rgb (dest_profile)) if (! gimp_lcms_profile_is_rgb (dest_profile))
{ {
g_message (_("Color profile '%s' is not for RGB color space."), g_message (_("Color profile '%s' is not for RGB color space."),
gimp_filename_to_utf8 (filename)); gimp_file_get_utf8_name (file));
cmsCloseProfile (dest_profile); cmsCloseProfile (dest_profile);
g_object_unref (file);
return GIMP_PDB_EXECUTION_ERROR; return GIMP_PDB_EXECUTION_ERROR;
} }
} }
@ -589,6 +600,9 @@ lcms_icc_apply (GimpColorConfig *config,
g_free (src_label); g_free (src_label);
g_free (dest_label); g_free (dest_label);
if (file)
g_object_unref (file);
return GIMP_PDB_SUCCESS; return GIMP_PDB_SUCCESS;
} }
@ -600,7 +614,7 @@ lcms_icc_apply (GimpColorConfig *config,
if (status == GIMP_PDB_SUCCESS && if (status == GIMP_PDB_SUCCESS &&
! lcms_image_apply_profile (image, ! lcms_image_apply_profile (image,
src_profile, dest_profile, filename, src_profile, dest_profile, file,
intent, bpc)) intent, bpc))
{ {
status = GIMP_PDB_EXECUTION_ERROR; status = GIMP_PDB_EXECUTION_ERROR;
@ -609,6 +623,9 @@ lcms_icc_apply (GimpColorConfig *config,
cmsCloseProfile (src_profile); cmsCloseProfile (src_profile);
cmsCloseProfile (dest_profile); cmsCloseProfile (dest_profile);
if (file)
g_object_unref (file);
return status; return status;
} }
@ -646,15 +663,15 @@ lcms_icc_info (GimpColorConfig *config,
} }
static GimpPDBStatusType static GimpPDBStatusType
lcms_icc_file_info (const gchar *filename, lcms_icc_file_info (GFile *file,
gchar **name, gchar **name,
gchar **desc, gchar **desc,
gchar **info, gchar **info,
GError **error) GError **error)
{ {
cmsHPROFILE profile; cmsHPROFILE profile;
profile = gimp_lcms_profile_open_from_file (filename, error); profile = gimp_lcms_profile_open_from_file (file, error);
if (! profile) if (! profile)
return GIMP_PDB_EXECUTION_ERROR; return GIMP_PDB_EXECUTION_ERROR;
@ -692,16 +709,20 @@ lcms_image_get_profile (GimpColorConfig *config,
} }
else if (config->rgb_profile) else if (config->rgb_profile)
{ {
profile = gimp_lcms_profile_open_from_file (config->rgb_profile, error); GFile *file = g_file_new_for_path (config->rgb_profile);
profile = gimp_lcms_profile_open_from_file (file, error);
if (profile && ! gimp_lcms_profile_is_rgb (profile)) if (profile && ! gimp_lcms_profile_is_rgb (profile))
{ {
g_set_error (error, 0, 0, g_set_error (error, 0, 0,
_("Color profile '%s' is not for RGB color space"), _("Color profile '%s' is not for RGB color space"),
gimp_filename_to_utf8 (config->rgb_profile)); gimp_file_get_utf8_name (file));
cmsCloseProfile (profile); cmsCloseProfile (profile);
profile = NULL; profile = NULL;
} }
g_object_unref (file);
} }
return profile; return profile;
@ -710,19 +731,22 @@ lcms_image_get_profile (GimpColorConfig *config,
static gboolean static gboolean
lcms_image_set_profile (gint32 image, lcms_image_set_profile (gint32 image,
cmsHPROFILE profile, cmsHPROFILE profile,
const gchar *filename) GFile *file)
{ {
g_return_val_if_fail (image != -1, FALSE); g_return_val_if_fail (image != -1, FALSE);
if (filename) if (file)
{ {
cmsHPROFILE file_profile;
GimpParasite *parasite; GimpParasite *parasite;
GMappedFile *file; guint8 *profile_data;
gsize profile_length;
GError *error = NULL; GError *error = NULL;
file = g_mapped_file_new (filename, FALSE, &error); /* check that this file is actually an ICC profile */
file_profile = gimp_lcms_profile_open_from_file (file, &error);
if (! file) if (! file_profile)
{ {
g_message ("%s", error->message); g_message ("%s", error->message);
g_clear_error (&error); g_clear_error (&error);
@ -730,22 +754,17 @@ lcms_image_set_profile (gint32 image,
return FALSE; return FALSE;
} }
/* check that this file is actually an ICC profile */ profile_data = gimp_lcms_profile_save_to_data (file_profile,
if (! profile) &profile_length,
{ &error);
profile = cmsOpenProfileFromMem (g_mapped_file_get_contents (file), cmsCloseProfile (file_profile);
g_mapped_file_get_length (file));
if (profile) if (! profile_data)
{ {
cmsCloseProfile (profile); g_message ("%s", error->message);
} g_clear_error (&error);
else
{ return FALSE;
g_message (_("'%s' does not appear to be an ICC color profile"),
gimp_filename_to_utf8 (filename));
return FALSE;
}
} }
gimp_image_undo_group_start (image); gimp_image_undo_group_start (image);
@ -753,10 +772,9 @@ lcms_image_set_profile (gint32 image,
parasite = gimp_parasite_new ("icc-profile", parasite = gimp_parasite_new ("icc-profile",
GIMP_PARASITE_PERSISTENT | GIMP_PARASITE_PERSISTENT |
GIMP_PARASITE_UNDOABLE, GIMP_PARASITE_UNDOABLE,
g_mapped_file_get_length (file), profile_length, profile_data);
g_mapped_file_get_contents (file));
g_mapped_file_unref (file); g_free (profile_data);
gimp_image_attach_parasite (image, parasite); gimp_image_attach_parasite (image, parasite);
gimp_parasite_free (parasite); gimp_parasite_free (parasite);
@ -779,7 +797,7 @@ static gboolean
lcms_image_apply_profile (gint32 image, lcms_image_apply_profile (gint32 image,
cmsHPROFILE src_profile, cmsHPROFILE src_profile,
cmsHPROFILE dest_profile, cmsHPROFILE dest_profile,
const gchar *filename, GFile *file,
GimpColorRenderingIntent intent, GimpColorRenderingIntent intent,
gboolean bpc) gboolean bpc)
{ {
@ -789,7 +807,7 @@ lcms_image_apply_profile (gint32 image,
gimp_image_undo_group_start (image); gimp_image_undo_group_start (image);
if (! lcms_image_set_profile (image, dest_profile, filename)) if (! lcms_image_set_profile (image, dest_profile, file))
{ {
gimp_image_undo_group_end (image); gimp_image_undo_group_end (image);
@ -1255,9 +1273,10 @@ lcms_icc_combo_box_new (GimpColorConfig *config,
if (config->rgb_profile) if (config->rgb_profile)
{ {
GFile *file = g_file_new_for_path (config->rgb_profile);
GError *error = NULL; GError *error = NULL;
profile = gimp_lcms_profile_open_from_file (config->rgb_profile, &error); profile = gimp_lcms_profile_open_from_file (file, &error);
if (! profile) if (! profile)
{ {
@ -1275,6 +1294,8 @@ lcms_icc_combo_box_new (GimpColorConfig *config,
{ {
rgb_filename = config->rgb_profile; rgb_filename = config->rgb_profile;
} }
g_object_unref (file);
} }
if (! profile) if (! profile)
@ -1421,15 +1442,19 @@ lcms_dialog (GimpColorConfig *config,
while ((run = gimp_dialog_run (GIMP_DIALOG (dialog))) == GTK_RESPONSE_OK) while ((run = gimp_dialog_run (GIMP_DIALOG (dialog))) == GTK_RESPONSE_OK)
{ {
gchar *filename = gimp_color_profile_combo_box_get_active (box); gchar *filename = gimp_color_profile_combo_box_get_active (box);
GFile *file = NULL;
cmsHPROFILE dest_profile; cmsHPROFILE dest_profile;
gtk_widget_set_sensitive (dialog, FALSE); gtk_widget_set_sensitive (dialog, FALSE);
if (filename) if (filename)
file = g_file_new_for_path (filename);
if (file)
{ {
GError *error = NULL; GError *error = NULL;
dest_profile = gimp_lcms_profile_open_from_file (filename, &error); dest_profile = gimp_lcms_profile_open_from_file (file, &error);
if (! dest_profile) if (! dest_profile)
{ {
@ -1449,12 +1474,12 @@ lcms_dialog (GimpColorConfig *config,
if (apply) if (apply)
success = lcms_image_apply_profile (image, success = lcms_image_apply_profile (image,
src_profile, dest_profile, src_profile, dest_profile,
filename, file,
values->intent, values->intent,
values->bpc); values->bpc);
else else
success = lcms_image_set_profile (image, success = lcms_image_set_profile (image,
dest_profile, filename); dest_profile, file);
} }
else else
{ {
@ -1464,6 +1489,9 @@ lcms_dialog (GimpColorConfig *config,
cmsCloseProfile (dest_profile); cmsCloseProfile (dest_profile);
} }
if (file)
g_object_unref (file);
if (success) if (success)
break; break;
else else