1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 1995, 1996, 1997 Spencer Kimball and Peter Mattis
3
* Copyright (C) 1997 Josh MacDonald
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28
#ifdef HAVE_SYS_PARAM_H
29
#include <sys/param.h>
36
#include <glib-object.h>
37
#include <glib/gstdio.h>
44
#include "libgimpconfig/gimpconfig.h"
46
#include "core/core-types.h"
48
#include "config/gimpcoreconfig.h"
50
#include "core/gimp.h"
51
#include "core/gimpcontext.h"
52
#include "core/gimpdocumentlist.h"
53
#include "core/gimpimage.h"
54
#include "core/gimpimage-merge.h"
55
#include "core/gimpimage-undo.h"
56
#include "core/gimpimagefile.h"
57
#include "core/gimplayer.h"
58
#include "core/gimpparamspecs.h"
59
#include "core/gimpprogress.h"
61
#include "pdb/gimppdb.h"
63
#include "plug-in/gimppluginmanager.h"
64
#include "plug-in/gimppluginprocedure.h"
65
#include "plug-in/gimppluginerror.h"
66
#include "plug-in/plug-in-icc-profile.h"
68
#include "file-open.h"
69
#include "file-procedure.h"
70
#include "file-utils.h"
72
#include "gimp-intl.h"
75
static void file_open_sanitize_image (GimpImage *image,
77
static void file_open_convert_items (GimpImage *dest_image,
78
const gchar *basename,
80
static void file_open_handle_color_profile (GimpImage *image,
82
GimpProgress *progress,
83
GimpRunMode run_mode);
86
/* public functions */
89
file_open_image (Gimp *gimp,
91
GimpProgress *progress,
93
const gchar *entered_filename,
95
GimpPlugInProcedure *file_proc,
97
GimpPDBStatusType *status,
98
const gchar **mime_type,
101
GValueArray *return_vals;
103
GimpImage *image = NULL;
105
g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
106
g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
107
g_return_val_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress), NULL);
108
g_return_val_if_fail (status != NULL, NULL);
109
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
111
*status = GIMP_PDB_EXECUTION_ERROR;
114
file_proc = file_procedure_find (gimp->plug_in_manager->load_procs, uri,
120
filename = file_utils_filename_from_uri (uri);
124
/* check if we are opening a file */
125
if (g_file_test (filename, G_FILE_TEST_EXISTS))
127
if (! g_file_test (filename, G_FILE_TEST_IS_REGULAR))
130
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
131
_("Not a regular file"));
135
if (g_access (filename, R_OK) != 0)
138
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_ACCES,
146
filename = g_strdup (uri);
150
gimp_pdb_execute_procedure_by_name (gimp->pdb,
151
context, progress, error,
152
GIMP_OBJECT (file_proc)->name,
153
GIMP_TYPE_INT32, run_mode,
154
G_TYPE_STRING, filename,
155
G_TYPE_STRING, entered_filename,
160
*status = g_value_get_enum (&return_vals->values[0]);
162
if (*status == GIMP_PDB_SUCCESS)
164
image = gimp_value_get_image (&return_vals->values[1], gimp);
168
file_open_sanitize_image (image, as_new);
170
/* Only set the load procedure if it hasn't already been set. */
171
if (! gimp_image_get_load_proc (image))
172
gimp_image_set_load_proc (image, file_proc);
174
file_proc = gimp_image_get_load_proc (image);
177
*mime_type = file_proc->mime_type;
181
if (error && ! *error)
182
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
183
_("%s plug-in returned SUCCESS but did not "
185
gimp_plug_in_procedure_get_label (file_proc));
187
*status = GIMP_PDB_EXECUTION_ERROR;
190
else if (*status != GIMP_PDB_CANCEL)
192
if (error && ! *error)
193
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
194
_("%s plug-In could not open image"),
195
gimp_plug_in_procedure_get_label (file_proc));
198
g_value_array_free (return_vals);
202
file_open_handle_color_profile (image, context, progress, run_mode);
208
/* Attempts to load a thumbnail by using a registered thumbnail loader. */
210
file_open_thumbnail (Gimp *gimp,
211
GimpContext *context,
212
GimpProgress *progress,
215
const gchar **mime_type,
220
GimpPlugInProcedure *file_proc;
221
GimpProcedure *procedure;
223
g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
224
g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
225
g_return_val_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress), NULL);
226
g_return_val_if_fail (mime_type != NULL, NULL);
227
g_return_val_if_fail (image_width != NULL, NULL);
228
g_return_val_if_fail (image_height != NULL, NULL);
229
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
234
file_proc = file_procedure_find (gimp->plug_in_manager->load_procs, uri,
237
if (! file_proc || ! file_proc->thumb_loader)
240
procedure = gimp_pdb_lookup_procedure (gimp->pdb, file_proc->thumb_loader);
242
if (procedure && procedure->num_args >= 2 && procedure->num_values >= 1)
244
GimpPDBStatusType status;
245
GValueArray *return_vals;
247
GimpImage *image = NULL;
249
filename = file_utils_filename_from_uri (uri);
252
filename = g_strdup (uri);
255
gimp_pdb_execute_procedure_by_name (gimp->pdb,
256
context, progress, error,
257
GIMP_OBJECT (procedure)->name,
258
G_TYPE_STRING, filename,
259
GIMP_TYPE_INT32, size,
264
status = g_value_get_enum (&return_vals->values[0]);
266
if (status == GIMP_PDB_SUCCESS)
268
image = gimp_value_get_image (&return_vals->values[1], gimp);
270
if (return_vals->n_values >= 3)
272
*image_width = MAX (0,
273
g_value_get_int (&return_vals->values[2]));
274
*image_height = MAX (0,
275
g_value_get_int (&return_vals->values[3]));
280
file_open_sanitize_image (image, FALSE);
282
*mime_type = file_proc->mime_type;
285
g_printerr ("opened thumbnail at %d x %d\n",
286
gimp_image_get_width (image),
287
gimp_image_get_height (image));
292
g_value_array_free (return_vals);
301
file_open_with_display (Gimp *gimp,
302
GimpContext *context,
303
GimpProgress *progress,
306
GimpPDBStatusType *status,
309
return file_open_with_proc_and_display (gimp, context, progress,
310
uri, uri, as_new, NULL,
315
file_open_with_proc_and_display (Gimp *gimp,
316
GimpContext *context,
317
GimpProgress *progress,
319
const gchar *entered_filename,
321
GimpPlugInProcedure *file_proc,
322
GimpPDBStatusType *status,
326
const gchar *mime_type = NULL;
328
g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
329
g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
330
g_return_val_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress), NULL);
331
g_return_val_if_fail (status != NULL, NULL);
333
image = file_open_image (gimp, context, progress,
338
GIMP_RUN_INTERACTIVE,
345
gimp_create_display (image->gimp, image, GIMP_UNIT_PIXEL, 1.0);
349
GimpDocumentList *documents = GIMP_DOCUMENT_LIST (gimp->documents);
350
GimpImagefile *imagefile;
352
imagefile = gimp_document_list_add_uri (documents, uri, mime_type);
354
/* can only create a thumbnail if the passed uri and the
355
* resulting image's uri match.
357
if (strcmp (uri, gimp_image_get_uri (image)) == 0)
359
/* no need to save a thumbnail if there's a good one already */
360
if (! gimp_imagefile_check_thumbnail (imagefile))
362
gimp_imagefile_save_thumbnail (imagefile, mime_type, image);
367
/* the display owns the image now */
368
g_object_unref (image);
370
/* announce that we opened this image */
371
gimp_image_opened (image->gimp, uri);
378
file_open_layers (Gimp *gimp,
379
GimpContext *context,
380
GimpProgress *progress,
381
GimpImage *dest_image,
382
gboolean merge_visible,
384
GimpRunMode run_mode,
385
GimpPlugInProcedure *file_proc,
386
GimpPDBStatusType *status,
389
GimpImage *new_image;
390
GList *layers = NULL;
391
const gchar *mime_type = NULL;
393
g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
394
g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
395
g_return_val_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress), NULL);
396
g_return_val_if_fail (GIMP_IS_IMAGE (dest_image), NULL);
397
g_return_val_if_fail (uri != NULL, NULL);
398
g_return_val_if_fail (status != NULL, NULL);
399
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
401
new_image = file_open_image (gimp, context, progress,
405
status, &mime_type, error);
412
gimp_image_undo_disable (new_image);
414
for (list = GIMP_LIST (gimp_image_get_layers (new_image))->list;
416
list = g_list_next (list))
419
layers = g_list_prepend (layers, list->data);
421
if (gimp_item_get_visible (list->data))
426
layers = g_list_prepend (layers, list->data);
430
if (merge_visible && n_visible > 1)
434
g_list_free (layers);
436
layer = gimp_image_merge_visible_layers (new_image, context,
437
GIMP_CLIP_TO_IMAGE, FALSE);
439
layers = g_list_prepend (NULL, layer);
444
gchar *basename = file_utils_uri_display_basename (uri);
446
file_open_convert_items (dest_image, basename, layers);
449
gimp_document_list_add_uri (GIMP_DOCUMENT_LIST (gimp->documents),
454
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
455
_("Image doesn't contain any layers"));
456
*status = GIMP_PDB_EXECUTION_ERROR;
459
g_object_unref (new_image);
462
return g_list_reverse (layers);
466
/* This function is called for filenames passed on the command-line
467
* or from the D-Bus service.
470
file_open_from_command_line (Gimp *gimp,
471
const gchar *filename,
474
GError *error = NULL;
476
gboolean success = FALSE;
478
g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);
479
g_return_val_if_fail (filename != NULL, FALSE);
481
/* we accept URI or filename */
482
uri = file_utils_any_to_uri (gimp, filename, &error);
487
GimpObject *display = gimp_get_empty_display (gimp);
488
GimpPDBStatusType status;
490
image = file_open_with_display (gimp,
491
gimp_get_user_context (gimp),
492
GIMP_PROGRESS (display),
500
else if (status != GIMP_PDB_CANCEL)
502
gchar *filename = file_utils_uri_display_name (uri);
504
gimp_message (gimp, G_OBJECT (display), GIMP_MESSAGE_ERROR,
505
_("Opening '%s' failed: %s"),
506
filename, error->message);
507
g_clear_error (&error);
516
g_printerr ("conversion filename -> uri failed: %s\n",
518
g_clear_error (&error);
525
/* private functions */
528
file_open_sanitize_image (GimpImage *image,
532
gimp_object_set_name (GIMP_OBJECT (image), NULL);
534
/* clear all undo steps */
535
gimp_image_undo_free (image);
537
/* make sure that undo is enabled */
538
while (image->undo_freeze_count)
539
gimp_image_undo_thaw (image);
541
/* set the image to clean */
542
gimp_image_clean_all (image);
544
/* make sure the entire projection is properly constructed, because
545
* load plug-ins are not required to call gimp_drawable_update() or
548
gimp_image_update (image,
550
gimp_image_get_width (image),
551
gimp_image_get_height (image));
552
gimp_image_flush (image);
554
/* same for drawable previews */
555
gimp_image_invalidate_layer_previews (image);
556
gimp_image_invalidate_channel_previews (image);
559
/* Converts items from one image to another */
561
file_open_convert_items (GimpImage *dest_image,
562
const gchar *basename,
567
for (list = items; list; list = g_list_next (list))
569
GimpItem *src = list->data;
572
item = gimp_item_convert (src, dest_image, G_TYPE_FROM_INSTANCE (src));
574
if (g_list_length (items) == 1)
576
gimp_object_set_name (GIMP_OBJECT (item), basename);
580
gchar *name = g_strdup_printf ("%s - %s", basename,
581
GIMP_OBJECT (src)->name);
583
gimp_object_take_name (GIMP_OBJECT (item), name);
591
file_open_profile_apply_rgb (GimpImage *image,
592
GimpContext *context,
593
GimpProgress *progress,
594
GimpRunMode run_mode)
596
GimpColorConfig *config = image->gimp->config->color_management;
597
GError *error = NULL;
599
if (gimp_image_base_type (image) == GIMP_GRAY)
602
if (config->mode == GIMP_COLOR_MANAGEMENT_OFF)
605
if (! plug_in_icc_profile_apply_rgb (image, context, progress, run_mode,
608
if (error->domain == GIMP_PLUG_IN_ERROR &&
609
error->code == GIMP_PLUG_IN_NOT_FOUND)
611
gchar *msg = g_strdup_printf ("%s\n\n%s",
613
_("Color management has been disabled. "
614
"It can be enabled again in the "
615
"Preferences dialog."));
617
g_object_set (config, "mode", GIMP_COLOR_MANAGEMENT_OFF, NULL);
619
gimp_message (image->gimp, G_OBJECT (progress),
620
GIMP_MESSAGE_WARNING, msg);
625
gimp_message (image->gimp, G_OBJECT (progress),
626
GIMP_MESSAGE_ERROR, error->message);
629
g_error_free (error);
634
file_open_handle_color_profile (GimpImage *image,
635
GimpContext *context,
636
GimpProgress *progress,
637
GimpRunMode run_mode)
639
if (gimp_image_parasite_find (image, "icc-profile"))
641
gimp_image_undo_disable (image);
643
switch (image->gimp->config->color_profile_policy)
645
case GIMP_COLOR_PROFILE_POLICY_ASK:
646
if (run_mode == GIMP_RUN_INTERACTIVE)
647
file_open_profile_apply_rgb (image, context, progress,
648
GIMP_RUN_INTERACTIVE);
651
case GIMP_COLOR_PROFILE_POLICY_KEEP:
654
case GIMP_COLOR_PROFILE_POLICY_CONVERT:
655
file_open_profile_apply_rgb (image, context, progress,
656
GIMP_RUN_NONINTERACTIVE);
660
gimp_image_clean_all (image);
661
gimp_image_undo_enable (image);