gchar * file_utils_uri_to_utf8_filename (const gchar *uri) { g_return_val_if_fail (uri != NULL, NULL); if (g_str_has_prefix (uri, "file:")) { gchar *filename = file_utils_filename_from_uri (uri); if (filename) { GError *error = NULL; gchar *utf8; utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, &error); g_free (filename); if (utf8) return utf8; g_warning ("%s: cannot convert filename to UTF-8: %s", G_STRLOC, error->message); g_error_free (error); } } return g_strdup (uri); }
gchar * file_utils_uri_display_basename (const gchar *uri) { gchar *basename = NULL; g_return_val_if_fail (uri != NULL, NULL); if (g_str_has_prefix (uri, "file:")) { gchar *filename = file_utils_filename_from_uri (uri); if (filename) { basename = g_filename_display_basename (filename); g_free (filename); } } else { gchar *name = file_utils_uri_display_name (uri); basename = strrchr (name, '/'); if (basename) basename = g_strdup (basename + 1); g_free (name); } return basename ? basename : file_utils_uri_to_utf8_basename (uri); }
static void gimp_thumb_box_create_thumbnail (GimpThumbBox *box, const gchar *uri, GimpThumbnailSize size, gboolean force, GimpProgress *progress) { gchar *filename = file_utils_filename_from_uri (uri); GimpThumbnail *thumb; gchar *basename; if (filename) { gboolean regular = g_file_test (filename, G_FILE_TEST_IS_REGULAR); g_free (filename); if (! regular) return; } thumb = gimp_imagefile_get_thumbnail (box->imagefile); basename = file_utils_uri_display_basename (uri); gtk_label_set_text (GTK_LABEL (box->filename), basename); g_free (basename); gimp_object_set_name (GIMP_OBJECT (box->imagefile), uri); if (force || (gimp_thumbnail_peek_thumb (thumb, size) < GIMP_THUMB_STATE_FAILED && ! gimp_thumbnail_has_failed (thumb))) { GError *error = NULL; if (! gimp_imagefile_create_thumbnail (box->imagefile, box->context, progress, size, ! force, &error)) { gimp_message_literal (box->context->gimp, G_OBJECT (progress), GIMP_MESSAGE_ERROR, error->message); g_clear_error (&error); } } }
gchar * file_utils_uri_display_name (const gchar *uri) { gchar *name = NULL; g_return_val_if_fail (uri != NULL, NULL); if (g_str_has_prefix (uri, "file:")) { gchar *filename = file_utils_filename_from_uri (uri); if (filename) { name = g_filename_display_name (filename); g_free (filename); } } else { name = file_utils_unescape_uri (uri, -1, "/", FALSE); } return name ? name : g_strdup (uri); }
GimpPDBStatusType file_save (Gimp *gimp, GimpImage *image, GimpProgress *progress, const gchar *uri, GimpPlugInProcedure *file_proc, GimpRunMode run_mode, gboolean change_saved_state, gboolean export_backward, gboolean export_forward, GError **error) { GimpDrawable *drawable; GimpValueArray *return_vals; GimpPDBStatusType status; gchar *filename; gint32 image_ID; gint32 drawable_ID; g_return_val_if_fail (GIMP_IS_GIMP (gimp), GIMP_PDB_CALLING_ERROR); g_return_val_if_fail (GIMP_IS_IMAGE (image), GIMP_PDB_CALLING_ERROR); g_return_val_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress), GIMP_PDB_CALLING_ERROR); g_return_val_if_fail (uri != NULL, GIMP_PDB_CALLING_ERROR); g_return_val_if_fail (GIMP_IS_PLUG_IN_PROCEDURE (file_proc), GIMP_PDB_CALLING_ERROR); g_return_val_if_fail ((export_backward && export_forward) == FALSE, GIMP_PDB_CALLING_ERROR); g_return_val_if_fail (error == NULL || *error == NULL, GIMP_PDB_CALLING_ERROR); drawable = gimp_image_get_active_drawable (image); if (! drawable) return GIMP_PDB_EXECUTION_ERROR; filename = file_utils_filename_from_uri (uri); if (filename) { /* check if we are saving to a file */ if (g_file_test (filename, G_FILE_TEST_EXISTS)) { if (! g_file_test (filename, G_FILE_TEST_IS_REGULAR)) { g_set_error_literal (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Not a regular file")); status = GIMP_PDB_EXECUTION_ERROR; goto out; } if (g_access (filename, W_OK) != 0) { g_set_error_literal (error, G_FILE_ERROR, G_FILE_ERROR_ACCES, g_strerror (errno)); status = GIMP_PDB_EXECUTION_ERROR; goto out; } } if (file_proc->handles_uri) { g_free (filename); filename = g_strdup (uri); } } else { filename = g_strdup (uri); } /* ref the image, so it can't get deleted during save */ g_object_ref (image); image_ID = gimp_image_get_ID (image); drawable_ID = gimp_item_get_ID (GIMP_ITEM (drawable)); return_vals = gimp_pdb_execute_procedure_by_name (image->gimp->pdb, gimp_get_user_context (gimp), progress, error, gimp_object_get_name (file_proc), GIMP_TYPE_INT32, run_mode, GIMP_TYPE_IMAGE_ID, image_ID, GIMP_TYPE_DRAWABLE_ID, drawable_ID, G_TYPE_STRING, filename, G_TYPE_STRING, uri, G_TYPE_NONE); status = g_value_get_enum (gimp_value_array_index (return_vals, 0)); gimp_value_array_unref (return_vals); if (status == GIMP_PDB_SUCCESS) { GimpDocumentList *documents; GimpImagefile *imagefile; if (change_saved_state) { gimp_image_set_uri (image, uri); gimp_image_set_save_proc (image, file_proc); /* Forget the import source when we save. We interpret a * save as that the user is not interested in being able * to quickly export back to the original any longer */ gimp_image_set_imported_uri (image, NULL); gimp_image_clean_all (image); } else if (export_backward) { /* We exported the image back to its imported source, * change nothing about export/import flags, only set * the export state to clean */ gimp_image_export_clean_all (image); gimp_object_name_changed (GIMP_OBJECT (image)); } else if (export_forward) { /* Remember the last entered Export URI for the image. We * only need to do this explicitly when exporting. It * happens implicitly when saving since the GimpObject name * of a GimpImage is the last-save URI */ gimp_image_set_exported_uri (image, uri); /* An image can not be considered both exported and imported * at the same time, so stop consider it as imported now * that we consider it exported. */ gimp_image_set_imported_uri (image, NULL); gimp_image_export_clean_all (image); } if (export_backward || export_forward) gimp_image_exported (image, uri); else gimp_image_saved (image, uri); documents = GIMP_DOCUMENT_LIST (image->gimp->documents); imagefile = gimp_document_list_add_uri (documents, uri, file_proc->mime_type); /* only save a thumbnail if we are saving as XCF, see bug #25272 */ if (GIMP_PROCEDURE (file_proc)->proc_type == GIMP_INTERNAL) gimp_imagefile_save_thumbnail (imagefile, file_proc->mime_type, image); } else if (status != GIMP_PDB_CANCEL) { if (error && *error == NULL) { g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("%s plug-in could not save image"), gimp_plug_in_procedure_get_label (file_proc)); } } gimp_image_flush (image); g_object_unref (image); out: g_free (filename); return status; }
GimpPlugInProcedure * file_procedure_find (GSList *procs, const gchar *uri, GError **error) { GimpPlugInProcedure *file_proc; GSList *all_procs = procs; gchar *filename; g_return_val_if_fail (procs != NULL, NULL); g_return_val_if_fail (uri != NULL, NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); /* First, check magicless prefixes/suffixes */ file_proc = file_proc_find_by_name (all_procs, uri, TRUE); if (file_proc) return file_proc; filename = file_utils_filename_from_uri (uri); /* Then look for magics */ if (filename) { GimpPlugInProcedure *size_matched_proc = NULL; FILE *ifp = NULL; gint head_size = -2; gint size_match_count = 0; guchar head[256]; while (procs) { file_proc = procs->data; procs = procs->next; if (file_proc->magics_list) { if (head_size == -2) { head_size = 0; if ((ifp = g_fopen (filename, "rb")) != NULL) { head_size = fread ((gchar *) head, 1, sizeof (head), ifp); } else { g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno), g_strerror (errno)); } } if (head_size >= 4) { FileMatchType match_val; match_val = file_check_magic_list (file_proc->magics_list, head, head_size, ifp); if (match_val == FILE_MATCH_SIZE) { /* Use it only if no other magic matches */ size_match_count++; size_matched_proc = file_proc; } else if (match_val != FILE_MATCH_NONE) { fclose (ifp); g_free (filename); return file_proc; } } } } if (ifp) { if (ferror (ifp)) g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno), g_strerror (errno)); fclose (ifp); } g_free (filename); if (size_match_count == 1) return size_matched_proc; } /* As a last resort, try matching by name */ file_proc = file_proc_find_by_name (all_procs, uri, FALSE); if (file_proc) { /* we found a procedure, clear error that might have been set */ g_clear_error (error); } else { /* set an error message unless one was already set */ if (error && *error == NULL) g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Unknown file type")); } return file_proc; }
GimpPlugInProcedure * file_procedure_find (GSList *procs, const gchar *uri, GError **error) { GimpPlugInProcedure *file_proc; gchar *filename; g_return_val_if_fail (procs != NULL, NULL); g_return_val_if_fail (uri != NULL, NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); /* First, check magicless prefixes/suffixes: */ if (! file_proc_find_by_extension (procs, uri, FALSE, TRUE)) { /* If there is not any (with or without magic) file proc that * can load the URI by extension directly, try to find a proc * that can load the prefix */ file_proc = file_proc_find_by_prefix (procs, uri, TRUE); } else { /* Otherwise try to find a magicless file proc that handles the * extension */ file_proc = file_proc_find_by_extension (procs, uri, TRUE, FALSE); } if (file_proc) return file_proc; filename = file_utils_filename_from_uri (uri); /* Then look for magics */ if (filename) { GSList *list; GimpPlugInProcedure *size_matched_proc = NULL; FILE *ifp = NULL; gboolean opened = FALSE; gint head_size = 0; gint size_match_count = 0; guchar head[256]; for (list = procs; list; list = g_slist_next (list)) { file_proc = procs->data; if (file_proc->magics_list) { if (G_UNLIKELY (!opened)) { ifp = g_fopen (filename, "rb"); if (ifp != NULL) head_size = fread ((gchar *) head, 1, sizeof (head), ifp); else g_set_error_literal (error, G_FILE_ERROR, g_file_error_from_errno (errno), g_strerror (errno)); opened = TRUE; } if (head_size >= 4) { FileMatchType match_val; match_val = file_check_magic_list (file_proc->magics_list, head, head_size, ifp); if (match_val == FILE_MATCH_SIZE) { /* Use it only if no other magic matches */ size_match_count++; size_matched_proc = file_proc; } else if (match_val != FILE_MATCH_NONE) { fclose (ifp); g_free (filename); return file_proc; } } } } if (ifp) { if (ferror (ifp)) g_set_error_literal (error, G_FILE_ERROR, g_file_error_from_errno (errno), g_strerror (errno)); fclose (ifp); } g_free (filename); if (size_match_count == 1) return size_matched_proc; } /* As a last resort, try matching by name, not skipping magic procs */ file_proc = file_proc_find_by_name (procs, uri, FALSE); if (file_proc) { /* we found a procedure, clear error that might have been set */ g_clear_error (error); } else { /* set an error message unless one was already set */ if (error && *error == NULL) g_set_error_literal (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Unknown file type")); } return file_proc; }
GimpImage * file_open_image (Gimp *gimp, GimpContext *context, GimpProgress *progress, const gchar *uri, const gchar *entered_filename, gboolean as_new, GimpPlugInProcedure *file_proc, GimpRunMode run_mode, GimpPDBStatusType *status, const gchar **mime_type, GError **error) { GValueArray *return_vals; gchar *filename; GimpImage *image = NULL; g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL); g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL); g_return_val_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress), NULL); g_return_val_if_fail (status != NULL, NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); *status = GIMP_PDB_EXECUTION_ERROR; if (! file_proc) file_proc = file_procedure_find (gimp->plug_in_manager->load_procs, uri, error); if (! file_proc) return NULL; filename = file_utils_filename_from_uri (uri); if (filename) { /* check if we are opening a file */ if (g_file_test (filename, G_FILE_TEST_EXISTS)) { if (! g_file_test (filename, G_FILE_TEST_IS_REGULAR)) { g_free (filename); g_set_error_literal (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Not a regular file")); return NULL; } if (g_access (filename, R_OK) != 0) { g_free (filename); g_set_error_literal (error, G_FILE_ERROR, G_FILE_ERROR_ACCES, g_strerror (errno)); return NULL; } } } else { filename = g_strdup (uri); } return_vals = gimp_pdb_execute_procedure_by_name (gimp->pdb, context, progress, error, gimp_object_get_name (file_proc), GIMP_TYPE_INT32, run_mode, G_TYPE_STRING, filename, G_TYPE_STRING, entered_filename, G_TYPE_NONE); g_free (filename); *status = g_value_get_enum (&return_vals->values[0]); if (*status == GIMP_PDB_SUCCESS) { image = gimp_value_get_image (&return_vals->values[1], gimp); if (image) { file_open_sanitize_image (image, as_new); /* Only set the load procedure if it hasn't already been set. */ if (! gimp_image_get_load_proc (image)) gimp_image_set_load_proc (image, file_proc); file_proc = gimp_image_get_load_proc (image); if (mime_type) *mime_type = file_proc->mime_type; } else { if (error && ! *error) g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("%s plug-in returned SUCCESS but did not " "return an image"), gimp_plug_in_procedure_get_label (file_proc)); *status = GIMP_PDB_EXECUTION_ERROR; } } else if (*status != GIMP_PDB_CANCEL) { if (error && ! *error) g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("%s plug-In could not open image"), gimp_plug_in_procedure_get_label (file_proc)); } g_value_array_free (return_vals); if (image) { file_open_handle_color_profile (image, context, progress, run_mode); if (file_open_file_proc_is_import (file_proc)) { /* Remember the import source */ gimp_image_set_imported_uri (image, uri); /* We shall treat this file as an Untitled file */ gimp_image_set_uri (image, NULL); } } return image; }
/** * file_open_thumbnail: * @gimp: * @context: * @progress: * @uri: the URI of the image file * @size: requested size of the thumbnail * @mime_type: return location for image MIME type * @image_width: return location for image width * @image_height: return location for image height * @type: return location for image type (set to -1 if unknown) * @num_layers: return location for number of layers * (set to -1 if the number of layers is not known) * @error: * * Attempts to load a thumbnail by using a registered thumbnail loader. * * Return value: the thumbnail image */ GimpImage * file_open_thumbnail (Gimp *gimp, GimpContext *context, GimpProgress *progress, const gchar *uri, gint size, const gchar **mime_type, gint *image_width, gint *image_height, GimpImageType *type, gint *num_layers, GError **error) { GimpPlugInProcedure *file_proc; GimpProcedure *procedure; g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL); g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL); g_return_val_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress), NULL); g_return_val_if_fail (mime_type != NULL, NULL); g_return_val_if_fail (image_width != NULL, NULL); g_return_val_if_fail (image_height != NULL, NULL); g_return_val_if_fail (type != NULL, NULL); g_return_val_if_fail (num_layers != NULL, NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); *image_width = 0; *image_height = 0; *type = -1; *num_layers = -1; file_proc = file_procedure_find (gimp->plug_in_manager->load_procs, uri, NULL); if (! file_proc || ! file_proc->thumb_loader) return NULL; procedure = gimp_pdb_lookup_procedure (gimp->pdb, file_proc->thumb_loader); if (procedure && procedure->num_args >= 2 && procedure->num_values >= 1) { GimpPDBStatusType status; GValueArray *return_vals; gchar *filename; GimpImage *image = NULL; filename = file_utils_filename_from_uri (uri); if (! filename) filename = g_strdup (uri); return_vals = gimp_pdb_execute_procedure_by_name (gimp->pdb, context, progress, error, gimp_object_get_name (procedure), G_TYPE_STRING, filename, GIMP_TYPE_INT32, size, G_TYPE_NONE); g_free (filename); status = g_value_get_enum (&return_vals->values[0]); if (status == GIMP_PDB_SUCCESS && GIMP_VALUE_HOLDS_IMAGE_ID (&return_vals->values[1])) { image = gimp_value_get_image (&return_vals->values[1], gimp); if (return_vals->n_values >= 3 && G_VALUE_HOLDS_INT (&return_vals->values[2]) && G_VALUE_HOLDS_INT (&return_vals->values[3])) { *image_width = MAX (0, g_value_get_int (&return_vals->values[2])); *image_height = MAX (0, g_value_get_int (&return_vals->values[3])); if (return_vals->n_values >= 5 && G_VALUE_HOLDS_INT (&return_vals->values[4])) { gint value = g_value_get_int (&return_vals->values[4]); if (gimp_enum_get_value (GIMP_TYPE_IMAGE_TYPE, value, NULL, NULL, NULL, NULL)) { *type = value; } } if (return_vals->n_values >= 6 && G_VALUE_HOLDS_INT (&return_vals->values[5])) { *num_layers = MAX (0, g_value_get_int (&return_vals->values[5])); } } if (image) { file_open_sanitize_image (image, FALSE); *mime_type = file_proc->mime_type; #ifdef GIMP_UNSTABLE g_printerr ("opened thumbnail at %d x %d\n", gimp_image_get_width (image), gimp_image_get_height (image)); #endif } } g_value_array_free (return_vals); return image; } return NULL; }
static void file_open_dialog_response (GtkWidget *open_dialog, gint response_id, Gimp *gimp) { GimpFileDialog *dialog = GIMP_FILE_DIALOG (open_dialog); GSList *uris; GSList *list; gboolean success = FALSE; g_object_set_data_full (G_OBJECT (gimp), "gimp-file-open-dialog-state", gimp_file_dialog_get_state (dialog), (GDestroyNotify) gimp_file_dialog_state_destroy); if (response_id != GTK_RESPONSE_OK) { if (! dialog->busy) gtk_widget_destroy (open_dialog); return; } uris = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (open_dialog)); if (uris) g_object_set_data_full (G_OBJECT (gimp), GIMP_FILE_OPEN_LAST_URI_KEY, g_strdup (uris->data), (GDestroyNotify) g_free); gimp_file_dialog_set_sensitive (dialog, FALSE); /* When we are going to open new image windows, unset the transient * window. We don't need it since we will use gdk_window_raise() to * keep the dialog on top. And if we don't do it, then the dialog * will pull the image window it was invoked from on top of all the * new opened image windows, and we don't want that to happen. */ if (! dialog->open_as_layers) gtk_window_set_transient_for (GTK_WINDOW (open_dialog), NULL); for (list = uris; list; list = g_slist_next (list)) { gchar *filename = file_utils_filename_from_uri (list->data); if (filename) { gboolean regular = g_file_test (filename, G_FILE_TEST_IS_REGULAR); g_free (filename); if (! regular) continue; } if (dialog->open_as_layers) { if (! dialog->image) { dialog->image = file_open_dialog_open_image (open_dialog, gimp, list->data, dialog->file_proc); if (dialog->image) success = TRUE; } else if (file_open_dialog_open_layers (open_dialog, dialog->image, list->data, dialog->file_proc)) { success = TRUE; } } else { if (file_open_dialog_open_image (open_dialog, gimp, list->data, dialog->file_proc)) { success = TRUE; /* Make the dialog stay on top of all images we open if * we open say 10 at once */ gdk_window_raise (gtk_widget_get_window (open_dialog)); } } if (dialog->canceled) break; } if (success) { if (dialog->open_as_layers && dialog->image) gimp_image_flush (dialog->image); gtk_widget_destroy (open_dialog); } else { gimp_file_dialog_set_sensitive (dialog, TRUE); } g_slist_free_full (uris, (GDestroyNotify) g_free); }
GimpPDBStatusType file_save (GimpImage *image, GimpContext *context, GimpProgress *progress, const gchar *uri, GimpPlugInProcedure *file_proc, GimpRunMode run_mode, gboolean save_a_copy, GError **error) { GimpDrawable *drawable; GValueArray *return_vals; GimpPDBStatusType status; gchar *filename; gint32 image_ID; gint32 drawable_ID; g_return_val_if_fail (GIMP_IS_IMAGE (image), GIMP_PDB_CALLING_ERROR); g_return_val_if_fail (GIMP_IS_CONTEXT (context), GIMP_PDB_CALLING_ERROR); g_return_val_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress), GIMP_PDB_CALLING_ERROR); g_return_val_if_fail (uri != NULL, GIMP_PDB_CALLING_ERROR); g_return_val_if_fail (GIMP_IS_PLUG_IN_PROCEDURE (file_proc), GIMP_PDB_CALLING_ERROR); g_return_val_if_fail (error == NULL || *error == NULL, GIMP_PDB_CALLING_ERROR); drawable = gimp_image_get_active_drawable (image); if (! drawable) return GIMP_PDB_EXECUTION_ERROR; filename = file_utils_filename_from_uri (uri); if (filename) { /* check if we are saving to a file */ if (g_file_test (filename, G_FILE_TEST_EXISTS)) { if (! g_file_test (filename, G_FILE_TEST_IS_REGULAR)) { g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, "%s", _("Not a regular file")); status = GIMP_PDB_EXECUTION_ERROR; goto out; } if (g_access (filename, W_OK) != 0) { g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_ACCES, "%s", g_strerror (errno)); status = GIMP_PDB_EXECUTION_ERROR; goto out; } } } else { filename = g_strdup (uri); } /* ref the image, so it can't get deleted during save */ g_object_ref (image); image_ID = gimp_image_get_ID (image); drawable_ID = gimp_item_get_ID (GIMP_ITEM (drawable)); return_vals = gimp_pdb_execute_procedure_by_name (image->gimp->pdb, context, progress, error, GIMP_OBJECT (file_proc)->name, GIMP_TYPE_INT32, run_mode, GIMP_TYPE_IMAGE_ID, image_ID, GIMP_TYPE_DRAWABLE_ID, drawable_ID, G_TYPE_STRING, filename, G_TYPE_STRING, uri, G_TYPE_NONE); status = g_value_get_enum (&return_vals->values[0]); g_value_array_free (return_vals); if (status == GIMP_PDB_SUCCESS) { GimpDocumentList *documents; GimpImagefile *imagefile; if (save_a_copy) { /* remember the "save-a-copy" filename for the next invocation */ g_object_set_data_full (G_OBJECT (image), "gimp-image-save-a-copy", g_strdup (uri), (GDestroyNotify) g_free); } else { /* reset the "save-a-copy" filename when the image URI changes */ if (strcmp (uri, gimp_image_get_uri (image))) g_object_set_data (G_OBJECT (image), "gimp-image-save-a-copy", NULL); gimp_image_set_uri (image, uri); gimp_image_set_save_proc (image, file_proc); gimp_image_clean_all (image); } gimp_image_saved (image, uri); documents = GIMP_DOCUMENT_LIST (image->gimp->documents); imagefile = gimp_document_list_add_uri (documents, uri, file_proc->mime_type); /* only save a thumbnail if we are saving as XCF, see bug #25272 */ if (GIMP_PROCEDURE (file_proc)->proc_type == GIMP_INTERNAL) gimp_imagefile_save_thumbnail (imagefile, file_proc->mime_type, image); } else if (status != GIMP_PDB_CANCEL) { if (error && *error == NULL) { g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("%s plug-in could not save image"), gimp_plug_in_procedure_get_label (file_proc)); } } gimp_image_flush (image); g_object_unref (image); out: g_free (filename); return status; }
/** * file_open_thumbnail: * @gimp: * @context: * @progress: * @uri: the URI of the image file * @size: requested size of the thumbnail * @mime_type: return location for image MIME type * @image_width: return location for image width * @image_height: return location for image height * @format: return location for image format (set to NULL if unknown) * @num_layers: return location for number of layers * (set to -1 if the number of layers is not known) * @error: * * Attempts to load a thumbnail by using a registered thumbnail loader. * * Return value: the thumbnail image */ GimpImage * file_open_thumbnail (Gimp *gimp, GimpContext *context, GimpProgress *progress, const gchar *uri, gint size, const gchar **mime_type, gint *image_width, gint *image_height, const Babl **format, gint *num_layers, GError **error) { GimpPlugInProcedure *file_proc; GimpProcedure *procedure; g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL); g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL); g_return_val_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress), NULL); g_return_val_if_fail (mime_type != NULL, NULL); g_return_val_if_fail (image_width != NULL, NULL); g_return_val_if_fail (image_height != NULL, NULL); g_return_val_if_fail (format != NULL, NULL); g_return_val_if_fail (num_layers != NULL, NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); *image_width = 0; *image_height = 0; *format = NULL; *num_layers = -1; file_proc = file_procedure_find (gimp->plug_in_manager->load_procs, uri, NULL); if (! file_proc || ! file_proc->thumb_loader) return NULL; procedure = gimp_pdb_lookup_procedure (gimp->pdb, file_proc->thumb_loader); if (procedure && procedure->num_args >= 2 && procedure->num_values >= 1) { GimpPDBStatusType status; GimpValueArray *return_vals; gchar *filename; GimpImage *image = NULL; filename = file_utils_filename_from_uri (uri); if (! filename) filename = g_strdup (uri); return_vals = gimp_pdb_execute_procedure_by_name (gimp->pdb, context, progress, error, gimp_object_get_name (procedure), G_TYPE_STRING, filename, GIMP_TYPE_INT32, size, G_TYPE_NONE); g_free (filename); status = g_value_get_enum (gimp_value_array_index (return_vals, 0)); if (status == GIMP_PDB_SUCCESS && GIMP_VALUE_HOLDS_IMAGE_ID (gimp_value_array_index (return_vals, 1))) { image = gimp_value_get_image (gimp_value_array_index (return_vals, 1), gimp); if (gimp_value_array_length (return_vals) >= 3 && G_VALUE_HOLDS_INT (gimp_value_array_index (return_vals, 2)) && G_VALUE_HOLDS_INT (gimp_value_array_index (return_vals, 3))) { *image_width = MAX (0, g_value_get_int (gimp_value_array_index (return_vals, 2))); *image_height = MAX (0, g_value_get_int (gimp_value_array_index (return_vals, 3))); if (gimp_value_array_length (return_vals) >= 5 && G_VALUE_HOLDS_INT (gimp_value_array_index (return_vals, 4))) { gint value = g_value_get_int (gimp_value_array_index (return_vals, 4)); switch (value) { case GIMP_RGB_IMAGE: *format = gimp_babl_format (GIMP_RGB, GIMP_PRECISION_U8, FALSE); break; case GIMP_RGBA_IMAGE: *format = gimp_babl_format (GIMP_RGB, GIMP_PRECISION_U8, TRUE); break; case GIMP_GRAY_IMAGE: *format = gimp_babl_format (GIMP_GRAY, GIMP_PRECISION_U8, FALSE); break; case GIMP_GRAYA_IMAGE: *format = gimp_babl_format (GIMP_GRAY, GIMP_PRECISION_U8, TRUE); break; case GIMP_INDEXED_IMAGE: case GIMP_INDEXEDA_IMAGE: { const Babl *rgb; const Babl *rgba; babl_new_palette ("-gimp-indexed-format-dummy", &rgb, &rgba); if (value == GIMP_INDEXED_IMAGE) *format = rgb; else *format = rgba; } break; default: break; } } if (gimp_value_array_length (return_vals) >= 6 && G_VALUE_HOLDS_INT (gimp_value_array_index (return_vals, 5))) { *num_layers = MAX (0, g_value_get_int (gimp_value_array_index (return_vals, 5))); } } if (image) { file_open_sanitize_image (image, FALSE); *mime_type = file_proc->mime_type; #ifdef GIMP_UNSTABLE g_printerr ("opened thumbnail at %d x %d\n", gimp_image_get_width (image), gimp_image_get_height (image)); #endif } } gimp_value_array_unref (return_vals); return image; } return NULL; }