1 /* GIMP - The GNU Image Manipulation Program
2 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18 #include "config.h"
19
20 #include <gdk-pixbuf/gdk-pixbuf.h>
21 #include <gegl.h>
22
23 #include "libgimpbase/gimpbase.h"
24
25 #include "core-types.h"
26
27 #include "gegl/gimp-gegl-loops.h"
28
29 #include "vectors/gimpvectors.h"
30
31 #include "gimp.h"
32 #include "gimpchannel.h"
33 #include "gimpguide.h"
34 #include "gimpimage.h"
35 #include "gimpimage-color-profile.h"
36 #include "gimpimage-colormap.h"
37 #include "gimpimage-duplicate.h"
38 #include "gimpimage-grid.h"
39 #include "gimpimage-guides.h"
40 #include "gimpimage-metadata.h"
41 #include "gimpimage-private.h"
42 #include "gimpimage-undo.h"
43 #include "gimpimage-sample-points.h"
44 #include "gimpitemstack.h"
45 #include "gimplayer.h"
46 #include "gimplayermask.h"
47 #include "gimplayer-floating-selection.h"
48 #include "gimpparasitelist.h"
49 #include "gimpsamplepoint.h"
50
51
52 static void gimp_image_duplicate_resolution (GimpImage *image,
53 GimpImage *new_image);
54 static void gimp_image_duplicate_save_source_file (GimpImage *image,
55 GimpImage *new_image);
56 static void gimp_image_duplicate_colormap (GimpImage *image,
57 GimpImage *new_image);
58 static GimpItem * gimp_image_duplicate_item (GimpItem *item,
59 GimpImage *new_image);
60 static GimpLayer * gimp_image_duplicate_layers (GimpImage *image,
61 GimpImage *new_image);
62 static GimpChannel * gimp_image_duplicate_channels (GimpImage *image,
63 GimpImage *new_image);
64 static GimpVectors * gimp_image_duplicate_vectors (GimpImage *image,
65 GimpImage *new_image);
66 static void gimp_image_duplicate_floating_sel (GimpImage *image,
67 GimpImage *new_image);
68 static void gimp_image_duplicate_mask (GimpImage *image,
69 GimpImage *new_image);
70 static void gimp_image_duplicate_components (GimpImage *image,
71 GimpImage *new_image);
72 static void gimp_image_duplicate_guides (GimpImage *image,
73 GimpImage *new_image);
74 static void gimp_image_duplicate_sample_points (GimpImage *image,
75 GimpImage *new_image);
76 static void gimp_image_duplicate_grid (GimpImage *image,
77 GimpImage *new_image);
78 static void gimp_image_duplicate_metadata (GimpImage *image,
79 GimpImage *new_image);
80 static void gimp_image_duplicate_quick_mask (GimpImage *image,
81 GimpImage *new_image);
82 static void gimp_image_duplicate_parasites (GimpImage *image,
83 GimpImage *new_image);
84 static void gimp_image_duplicate_color_profile (GimpImage *image,
85 GimpImage *new_image);
86
87
88 GimpImage *
gimp_image_duplicate(GimpImage * image)89 gimp_image_duplicate (GimpImage *image)
90 {
91 GimpImage *new_image;
92 GimpLayer *active_layer;
93 GimpChannel *active_channel;
94 GimpVectors *active_vectors;
95
96 g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
97
98 gimp_set_busy_until_idle (image->gimp);
99
100 /* Create a new image */
101 new_image = gimp_create_image (image->gimp,
102 gimp_image_get_width (image),
103 gimp_image_get_height (image),
104 gimp_image_get_base_type (image),
105 gimp_image_get_precision (image),
106 FALSE);
107 gimp_image_undo_disable (new_image);
108
109 /* Store the source uri to be used by the save dialog */
110 gimp_image_duplicate_save_source_file (image, new_image);
111
112 /* Copy the colormap if necessary */
113 gimp_image_duplicate_colormap (image, new_image);
114
115 /* Copy resolution information */
116 gimp_image_duplicate_resolution (image, new_image);
117
118 /* Copy parasites first so we have a color profile */
119 gimp_image_duplicate_parasites (image, new_image);
120 gimp_image_duplicate_color_profile (image, new_image);
121
122 /* Copy the layers */
123 active_layer = gimp_image_duplicate_layers (image, new_image);
124
125 /* Copy the channels */
126 active_channel = gimp_image_duplicate_channels (image, new_image);
127
128 /* Copy any vectors */
129 active_vectors = gimp_image_duplicate_vectors (image, new_image);
130
131 /* Copy floating layer */
132 gimp_image_duplicate_floating_sel (image, new_image);
133
134 /* Copy the selection mask */
135 gimp_image_duplicate_mask (image, new_image);
136
137 /* Set active layer, active channel, active vectors */
138 if (active_layer)
139 gimp_image_set_active_layer (new_image, active_layer);
140
141 if (active_channel)
142 gimp_image_set_active_channel (new_image, active_channel);
143
144 if (active_vectors)
145 gimp_image_set_active_vectors (new_image, active_vectors);
146
147 /* Copy state of all color components */
148 gimp_image_duplicate_components (image, new_image);
149
150 /* Copy any guides */
151 gimp_image_duplicate_guides (image, new_image);
152
153 /* Copy any sample points */
154 gimp_image_duplicate_sample_points (image, new_image);
155
156 /* Copy the grid */
157 gimp_image_duplicate_grid (image, new_image);
158
159 /* Copy the metadata */
160 gimp_image_duplicate_metadata (image, new_image);
161
162 /* Copy the quick mask info */
163 gimp_image_duplicate_quick_mask (image, new_image);
164
165 gimp_image_undo_enable (new_image);
166
167 /* Explicitly mark image as dirty, so that its dirty time is set */
168 gimp_image_dirty (new_image, GIMP_DIRTY_ALL);
169
170 return new_image;
171 }
172
173 static void
gimp_image_duplicate_resolution(GimpImage * image,GimpImage * new_image)174 gimp_image_duplicate_resolution (GimpImage *image,
175 GimpImage *new_image)
176 {
177 gdouble xres;
178 gdouble yres;
179
180 gimp_image_get_resolution (image, &xres, &yres);
181 gimp_image_set_resolution (new_image, xres, yres);
182 gimp_image_set_unit (new_image, gimp_image_get_unit (image));
183 }
184
185 static void
gimp_image_duplicate_save_source_file(GimpImage * image,GimpImage * new_image)186 gimp_image_duplicate_save_source_file (GimpImage *image,
187 GimpImage *new_image)
188 {
189 GFile *file = gimp_image_get_file (image);
190
191 if (file)
192 g_object_set_data_full (G_OBJECT (new_image), "gimp-image-source-file",
193 g_object_ref (file),
194 (GDestroyNotify) g_object_unref);
195 }
196
197 static void
gimp_image_duplicate_colormap(GimpImage * image,GimpImage * new_image)198 gimp_image_duplicate_colormap (GimpImage *image,
199 GimpImage *new_image)
200 {
201 if (gimp_image_get_base_type (new_image) == GIMP_INDEXED)
202 gimp_image_set_colormap (new_image,
203 gimp_image_get_colormap (image),
204 gimp_image_get_colormap_size (image),
205 FALSE);
206 }
207
208 static GimpItem *
gimp_image_duplicate_item(GimpItem * item,GimpImage * new_image)209 gimp_image_duplicate_item (GimpItem *item,
210 GimpImage *new_image)
211 {
212 GimpItem *new_item;
213
214 new_item = gimp_item_convert (item, new_image,
215 G_TYPE_FROM_INSTANCE (item));
216
217 /* Make sure the copied item doesn't say: "<old item> copy" */
218 gimp_object_set_name (GIMP_OBJECT (new_item),
219 gimp_object_get_name (item));
220
221 return new_item;
222 }
223
224 static GimpLayer *
gimp_image_duplicate_layers(GimpImage * image,GimpImage * new_image)225 gimp_image_duplicate_layers (GimpImage *image,
226 GimpImage *new_image)
227 {
228 GimpLayer *active_layer = NULL;
229 GList *list;
230 gint count;
231
232 for (list = gimp_image_get_layer_iter (image), count = 0;
233 list;
234 list = g_list_next (list))
235 {
236 GimpLayer *layer = list->data;
237 GimpLayer *new_layer;
238
239 if (gimp_layer_is_floating_sel (layer))
240 continue;
241
242 new_layer = GIMP_LAYER (gimp_image_duplicate_item (GIMP_ITEM (layer),
243 new_image));
244
245 /* Make sure that if the layer has a layer mask,
246 * its name isn't screwed up
247 */
248 if (new_layer->mask)
249 gimp_object_set_name (GIMP_OBJECT (new_layer->mask),
250 gimp_object_get_name (layer->mask));
251
252 if (gimp_image_get_active_layer (image) == layer)
253 active_layer = new_layer;
254
255 gimp_image_add_layer (new_image, new_layer,
256 NULL, count++, FALSE);
257 }
258
259 return active_layer;
260 }
261
262 static GimpChannel *
gimp_image_duplicate_channels(GimpImage * image,GimpImage * new_image)263 gimp_image_duplicate_channels (GimpImage *image,
264 GimpImage *new_image)
265 {
266 GimpChannel *active_channel = NULL;
267 GList *list;
268 gint count;
269
270 for (list = gimp_image_get_channel_iter (image), count = 0;
271 list;
272 list = g_list_next (list))
273 {
274 GimpChannel *channel = list->data;
275 GimpChannel *new_channel;
276
277 new_channel = GIMP_CHANNEL (gimp_image_duplicate_item (GIMP_ITEM (channel),
278 new_image));
279
280 if (gimp_image_get_active_channel (image) == channel)
281 active_channel = new_channel;
282
283 gimp_image_add_channel (new_image, new_channel,
284 NULL, count++, FALSE);
285 }
286
287 return active_channel;
288 }
289
290 static GimpVectors *
gimp_image_duplicate_vectors(GimpImage * image,GimpImage * new_image)291 gimp_image_duplicate_vectors (GimpImage *image,
292 GimpImage *new_image)
293 {
294 GimpVectors *active_vectors = NULL;
295 GList *list;
296 gint count;
297
298 for (list = gimp_image_get_vectors_iter (image), count = 0;
299 list;
300 list = g_list_next (list))
301 {
302 GimpVectors *vectors = list->data;
303 GimpVectors *new_vectors;
304
305 new_vectors = GIMP_VECTORS (gimp_image_duplicate_item (GIMP_ITEM (vectors),
306 new_image));
307
308 if (gimp_image_get_active_vectors (image) == vectors)
309 active_vectors = new_vectors;
310
311 gimp_image_add_vectors (new_image, new_vectors,
312 NULL, count++, FALSE);
313 }
314
315 return active_vectors;
316 }
317
318 static void
gimp_image_duplicate_floating_sel(GimpImage * image,GimpImage * new_image)319 gimp_image_duplicate_floating_sel (GimpImage *image,
320 GimpImage *new_image)
321 {
322 GimpLayer *floating_sel;
323 GimpDrawable *floating_sel_drawable;
324 GList *floating_sel_path;
325 GimpItemStack *new_item_stack;
326 GimpLayer *new_floating_sel;
327 GimpDrawable *new_floating_sel_drawable;
328
329 floating_sel = gimp_image_get_floating_selection (image);
330
331 if (! floating_sel)
332 return;
333
334 floating_sel_drawable = gimp_layer_get_floating_sel_drawable (floating_sel);
335
336 if (GIMP_IS_LAYER_MASK (floating_sel_drawable))
337 {
338 GimpLayer *layer;
339
340 layer = gimp_layer_mask_get_layer (GIMP_LAYER_MASK (floating_sel_drawable));
341
342 floating_sel_path = gimp_item_get_path (GIMP_ITEM (layer));
343
344 new_item_stack = GIMP_ITEM_STACK (gimp_image_get_layers (new_image));
345 }
346 else
347 {
348 floating_sel_path = gimp_item_get_path (GIMP_ITEM (floating_sel_drawable));
349
350 if (GIMP_IS_LAYER (floating_sel_drawable))
351 new_item_stack = GIMP_ITEM_STACK (gimp_image_get_layers (new_image));
352 else
353 new_item_stack = GIMP_ITEM_STACK (gimp_image_get_channels (new_image));
354 }
355
356 /* adjust path[0] for the floating layer missing in new_image */
357 floating_sel_path->data =
358 GUINT_TO_POINTER (GPOINTER_TO_UINT (floating_sel_path->data) - 1);
359
360 if (GIMP_IS_LAYER (floating_sel_drawable))
361 {
362 new_floating_sel =
363 GIMP_LAYER (gimp_image_duplicate_item (GIMP_ITEM (floating_sel),
364 new_image));
365 }
366 else
367 {
368 /* can't use gimp_item_convert() for floating selections of channels
369 * or layer masks because they maybe don't have a normal layer's type
370 */
371 new_floating_sel =
372 GIMP_LAYER (gimp_item_duplicate (GIMP_ITEM (floating_sel),
373 G_TYPE_FROM_INSTANCE (floating_sel)));
374 gimp_item_set_image (GIMP_ITEM (new_floating_sel), new_image);
375
376 gimp_object_set_name (GIMP_OBJECT (new_floating_sel),
377 gimp_object_get_name (floating_sel));
378 }
379
380 /* Make sure the copied layer doesn't say: "<old layer> copy" */
381 gimp_object_set_name (GIMP_OBJECT (new_floating_sel),
382 gimp_object_get_name (floating_sel));
383
384 new_floating_sel_drawable =
385 GIMP_DRAWABLE (gimp_item_stack_get_item_by_path (new_item_stack,
386 floating_sel_path));
387
388 if (GIMP_IS_LAYER_MASK (floating_sel_drawable))
389 new_floating_sel_drawable =
390 GIMP_DRAWABLE (gimp_layer_get_mask (GIMP_LAYER (new_floating_sel_drawable)));
391
392 floating_sel_attach (new_floating_sel, new_floating_sel_drawable);
393
394 g_list_free (floating_sel_path);
395 }
396
397 static void
gimp_image_duplicate_mask(GimpImage * image,GimpImage * new_image)398 gimp_image_duplicate_mask (GimpImage *image,
399 GimpImage *new_image)
400 {
401 GimpDrawable *mask;
402 GimpDrawable *new_mask;
403
404 mask = GIMP_DRAWABLE (gimp_image_get_mask (image));
405 new_mask = GIMP_DRAWABLE (gimp_image_get_mask (new_image));
406
407 gimp_gegl_buffer_copy (gimp_drawable_get_buffer (mask), NULL, GEGL_ABYSS_NONE,
408 gimp_drawable_get_buffer (new_mask), NULL);
409
410 GIMP_CHANNEL (new_mask)->bounds_known = FALSE;
411 GIMP_CHANNEL (new_mask)->boundary_known = FALSE;
412 }
413
414 static void
gimp_image_duplicate_components(GimpImage * image,GimpImage * new_image)415 gimp_image_duplicate_components (GimpImage *image,
416 GimpImage *new_image)
417 {
418 GimpImagePrivate *private = GIMP_IMAGE_GET_PRIVATE (image);
419 GimpImagePrivate *new_private = GIMP_IMAGE_GET_PRIVATE (new_image);
420 gint count;
421
422 for (count = 0; count < MAX_CHANNELS; count++)
423 {
424 new_private->visible[count] = private->visible[count];
425 new_private->active[count] = private->active[count];
426 }
427 }
428
429 static void
gimp_image_duplicate_guides(GimpImage * image,GimpImage * new_image)430 gimp_image_duplicate_guides (GimpImage *image,
431 GimpImage *new_image)
432 {
433 GList *list;
434
435 for (list = gimp_image_get_guides (image);
436 list;
437 list = g_list_next (list))
438 {
439 GimpGuide *guide = list->data;
440 gint position = gimp_guide_get_position (guide);
441
442 switch (gimp_guide_get_orientation (guide))
443 {
444 case GIMP_ORIENTATION_HORIZONTAL:
445 gimp_image_add_hguide (new_image, position, FALSE);
446 break;
447
448 case GIMP_ORIENTATION_VERTICAL:
449 gimp_image_add_vguide (new_image, position, FALSE);
450 break;
451
452 default:
453 g_error ("Unknown guide orientation.\n");
454 }
455 }
456 }
457
458 static void
gimp_image_duplicate_sample_points(GimpImage * image,GimpImage * new_image)459 gimp_image_duplicate_sample_points (GimpImage *image,
460 GimpImage *new_image)
461 {
462 GList *list;
463
464 for (list = gimp_image_get_sample_points (image);
465 list;
466 list = g_list_next (list))
467 {
468 GimpSamplePoint *sample_point = list->data;
469 gint x;
470 gint y;
471
472 gimp_sample_point_get_position (sample_point, &x, &y);
473
474 gimp_image_add_sample_point_at_pos (new_image, x, y, FALSE);
475 }
476 }
477
478 static void
gimp_image_duplicate_grid(GimpImage * image,GimpImage * new_image)479 gimp_image_duplicate_grid (GimpImage *image,
480 GimpImage *new_image)
481 {
482 if (gimp_image_get_grid (image))
483 gimp_image_set_grid (new_image, gimp_image_get_grid (image), FALSE);
484 }
485
486 static void
gimp_image_duplicate_metadata(GimpImage * image,GimpImage * new_image)487 gimp_image_duplicate_metadata (GimpImage *image,
488 GimpImage *new_image)
489 {
490 GimpMetadata *metadata = gimp_image_get_metadata (image);
491
492 if (metadata)
493 {
494 metadata = gimp_metadata_duplicate (metadata);
495 gimp_image_set_metadata (new_image, metadata, FALSE);
496 g_object_unref (metadata);
497 }
498 }
499
500 static void
gimp_image_duplicate_quick_mask(GimpImage * image,GimpImage * new_image)501 gimp_image_duplicate_quick_mask (GimpImage *image,
502 GimpImage *new_image)
503 {
504 GimpImagePrivate *private = GIMP_IMAGE_GET_PRIVATE (image);
505 GimpImagePrivate *new_private = GIMP_IMAGE_GET_PRIVATE (new_image);
506
507 new_private->quick_mask_state = private->quick_mask_state;
508 new_private->quick_mask_inverted = private->quick_mask_inverted;
509 new_private->quick_mask_color = private->quick_mask_color;
510 }
511
512 static void
gimp_image_duplicate_parasites(GimpImage * image,GimpImage * new_image)513 gimp_image_duplicate_parasites (GimpImage *image,
514 GimpImage *new_image)
515 {
516 GimpImagePrivate *private = GIMP_IMAGE_GET_PRIVATE (image);
517 GimpImagePrivate *new_private = GIMP_IMAGE_GET_PRIVATE (new_image);
518
519 if (private->parasites)
520 {
521 g_object_unref (new_private->parasites);
522 new_private->parasites = gimp_parasite_list_copy (private->parasites);
523 }
524 }
525
526 static void
gimp_image_duplicate_color_profile(GimpImage * image,GimpImage * new_image)527 gimp_image_duplicate_color_profile (GimpImage *image,
528 GimpImage *new_image)
529 {
530 GimpColorProfile *profile = gimp_image_get_color_profile (image);
531 gboolean is_color_managed = gimp_image_get_is_color_managed (image);
532
533 gimp_image_set_color_profile (new_image, profile, NULL);
534 gimp_image_set_is_color_managed (new_image, is_color_managed, FALSE);
535 }
536