1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
|
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <glib-object.h>
#include "libgimpcolor/gimpcolor.h"
#include "libgimpmath/gimpmath.h"
#include "libgimpbase/gimpbase.h"
#include "core-types.h"
#include "base/pixel-region.h"
#include "base/temp-buf.h"
#include "base/tile-manager.h"
#include "paint-funcs/paint-funcs.h"
#include "vectors/gimpvectors.h"
#include "gimp.h"
#include "gimpcontext.h"
#include "gimpimage.h"
#include "gimpimage-colorhash.h"
#include "gimpimage-merge.h"
#include "gimpimage-undo.h"
#include "gimplayer.h"
#include "gimplayer-floating-sel.h"
#include "gimplayermask.h"
#include "gimplist.h"
#include "gimpmarshal.h"
#include "gimpparasitelist.h"
#include "gimpundostack.h"
#include "gimp-intl.h"
/* public functions */
GimpLayer *
gimp_image_merge_visible_layers (GimpImage *gimage,
GimpContext *context,
GimpMergeType merge_type)
{
GList *list;
GSList *merge_list = NULL;
gboolean had_floating_sel = FALSE;
GimpLayer *layer = NULL;
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
/* if there's a floating selection, anchor it */
if (gimp_image_floating_sel (gimage))
{
floating_sel_anchor (gimage->floating_sel);
had_floating_sel = TRUE;
}
for (list = GIMP_LIST (gimage->layers)->list;
list;
list = g_list_next (list))
{
layer = (GimpLayer *) list->data;
if (gimp_item_get_visible (GIMP_ITEM (layer)))
merge_list = g_slist_append (merge_list, layer);
}
if (merge_list && merge_list->next)
{
gimp_set_busy (gimage->gimp);
layer = gimp_image_merge_layers (gimage, merge_list, context, merge_type,
_("Merge Visible Layers"));
g_slist_free (merge_list);
gimp_unset_busy (gimage->gimp);
return layer;
}
else
{
g_slist_free (merge_list);
/* If there was a floating selection, we have done something.
No need to warn the user. Return the active layer instead */
if (had_floating_sel)
return gimage->active_layer;
else
g_message (_("Not enough visible layers for a merge. "
"There must be at least two."));
return NULL;
}
}
GimpLayer *
gimp_image_flatten (GimpImage *gimage,
GimpContext *context)
{
GList *list;
GSList *merge_list = NULL;
GimpLayer *layer;
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
gimp_set_busy (gimage->gimp);
/* if there's a floating selection, anchor it */
if (gimp_image_floating_sel (gimage))
floating_sel_anchor (gimage->floating_sel);
for (list = GIMP_LIST (gimage->layers)->list;
list;
list = g_list_next (list))
{
layer = (GimpLayer *) list->data;
if (gimp_item_get_visible (GIMP_ITEM (layer)))
merge_list = g_slist_append (merge_list, layer);
}
layer = gimp_image_merge_layers (gimage, merge_list, context,
GIMP_FLATTEN_IMAGE, _("Flatten Image"));
g_slist_free (merge_list);
gimp_image_alpha_changed (gimage);
gimp_unset_busy (gimage->gimp);
return layer;
}
GimpLayer *
gimp_image_merge_down (GimpImage *gimage,
GimpLayer *current_layer,
GimpContext *context,
GimpMergeType merge_type)
{
GimpLayer *layer;
GList *list;
GList *layer_list;
GSList *merge_list;
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
for (list = GIMP_LIST (gimage->layers)->list, layer_list = NULL;
list && !layer_list;
list = g_list_next (list))
{
layer = (GimpLayer *) list->data;
if (layer == current_layer)
break;
}
for (layer_list = g_list_next (list), merge_list = NULL;
layer_list && !merge_list;
layer_list = g_list_next (layer_list))
{
layer = (GimpLayer *) layer_list->data;
if (gimp_item_get_visible (GIMP_ITEM (layer)))
merge_list = g_slist_append (NULL, layer);
}
if (merge_list)
{
merge_list = g_slist_prepend (merge_list, current_layer);
gimp_set_busy (gimage->gimp);
layer = gimp_image_merge_layers (gimage, merge_list, context, merge_type,
_("Merge Down"));
g_slist_free (merge_list);
gimp_unset_busy (gimage->gimp);
return layer;
}
else
{
g_message (_("There are not enough visible layers for a merge down."));
return NULL;
}
}
GimpLayer *
gimp_image_merge_layers (GimpImage *gimage,
GSList *merge_list,
GimpContext *context,
GimpMergeType merge_type,
const gchar *undo_desc)
{
GList *list;
GSList *reverse_list = NULL;
PixelRegion src1PR, src2PR, maskPR;
PixelRegion *mask;
GimpLayer *merge_layer;
GimpLayer *layer;
GimpLayer *bottom_layer;
guchar bg[4] = {0, 0, 0, 0};
GimpImageType type;
gint count;
gint x1, y1, x2, y2;
gint x3, y3, x4, y4;
CombinationMode operation;
gint position;
gboolean active[MAX_CHANNELS] = { TRUE, TRUE, TRUE, TRUE };
gint off_x, off_y;
gchar *name;
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
layer = NULL;
type = GIMP_RGBA_IMAGE;
x1 = y1 = 0;
x2 = y2 = 0;
bottom_layer = NULL;
/* Get the layer extents */
count = 0;
while (merge_list)
{
layer = (GimpLayer *) merge_list->data;
gimp_item_offsets (GIMP_ITEM (layer), &off_x, &off_y);
switch (merge_type)
{
case GIMP_EXPAND_AS_NECESSARY:
case GIMP_CLIP_TO_IMAGE:
if (!count)
{
x1 = off_x;
y1 = off_y;
x2 = off_x + gimp_item_width (GIMP_ITEM (layer));
y2 = off_y + gimp_item_height (GIMP_ITEM (layer));
}
else
{
if (off_x < x1)
x1 = off_x;
if (off_y < y1)
y1 = off_y;
if ((off_x + gimp_item_width (GIMP_ITEM (layer))) > x2)
x2 = (off_x + gimp_item_width (GIMP_ITEM (layer)));
if ((off_y + gimp_item_height (GIMP_ITEM (layer))) > y2)
y2 = (off_y + gimp_item_height (GIMP_ITEM (layer)));
}
if (merge_type == GIMP_CLIP_TO_IMAGE)
{
x1 = CLAMP (x1, 0, gimage->width);
y1 = CLAMP (y1, 0, gimage->height);
x2 = CLAMP (x2, 0, gimage->width);
y2 = CLAMP (y2, 0, gimage->height);
}
break;
case GIMP_CLIP_TO_BOTTOM_LAYER:
if (merge_list->next == NULL)
{
x1 = off_x;
y1 = off_y;
x2 = off_x + gimp_item_width (GIMP_ITEM (layer));
y2 = off_y + gimp_item_height (GIMP_ITEM (layer));
}
break;
case GIMP_FLATTEN_IMAGE:
if (merge_list->next == NULL)
{
x1 = 0;
y1 = 0;
x2 = gimage->width;
y2 = gimage->height;
}
break;
}
count ++;
reverse_list = g_slist_prepend (reverse_list, layer);
merge_list = g_slist_next (merge_list);
}
if ((x2 - x1) == 0 || (y2 - y1) == 0)
return NULL;
/* Start a merge undo group. */
gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_IMAGE_LAYERS_MERGE,
undo_desc);
name = g_strdup (gimp_object_get_name (GIMP_OBJECT (layer)));
if (merge_type == GIMP_FLATTEN_IMAGE ||
gimp_drawable_type (GIMP_DRAWABLE (layer)) == GIMP_INDEXED_IMAGE)
{
type = GIMP_IMAGE_TYPE_FROM_BASE_TYPE (gimp_image_base_type (gimage));
merge_layer = gimp_layer_new (gimage, (x2 - x1), (y2 - y1),
type,
gimp_object_get_name (GIMP_OBJECT (layer)),
GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
if (!merge_layer)
{
g_warning ("%s: could not allocate merge layer.", G_STRFUNC);
return NULL;
}
GIMP_ITEM (merge_layer)->offset_x = x1;
GIMP_ITEM (merge_layer)->offset_y = y1;
/* get the background for compositing */
gimp_image_get_background (gimage, GIMP_DRAWABLE (merge_layer),
context, bg);
/* init the pixel region */
pixel_region_init (&src1PR,
gimp_drawable_data (GIMP_DRAWABLE (merge_layer)),
0, 0,
gimage->width, gimage->height,
TRUE);
/* set the region to the background color */
color_region (&src1PR, bg);
position = 0;
}
else
{
/* The final merged layer inherits the name of the bottom most layer
* and the resulting layer has an alpha channel
* whether or not the original did
* Opacity is set to 100% and the MODE is set to normal
*/
merge_layer =
gimp_layer_new (gimage, (x2 - x1), (y2 - y1),
gimp_drawable_type_with_alpha (GIMP_DRAWABLE (layer)),
"merged layer",
GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
if (!merge_layer)
{
g_warning ("%s: could not allocate merge layer", G_STRFUNC);
return NULL;
}
GIMP_ITEM (merge_layer)->offset_x = x1;
GIMP_ITEM (merge_layer)->offset_y = y1;
/* Set the layer to transparent */
pixel_region_init (&src1PR,
gimp_drawable_data (GIMP_DRAWABLE (merge_layer)),
0, 0,
(x2 - x1), (y2 - y1),
TRUE);
/* set the region to 0's */
color_region (&src1PR, bg);
/* Find the index in the layer list of the bottom layer--we need this
* in order to add the final, merged layer to the layer list correctly
*/
layer = (GimpLayer *) reverse_list->data;
position =
gimp_container_num_children (gimage->layers) -
gimp_container_get_child_index (gimage->layers, GIMP_OBJECT (layer));
}
bottom_layer = layer;
/* Copy the tattoo and parasites of the bottom layer to the new layer */
gimp_item_set_tattoo (GIMP_ITEM (merge_layer),
gimp_item_get_tattoo (GIMP_ITEM (bottom_layer)));
g_object_unref (GIMP_ITEM (merge_layer)->parasites);
GIMP_ITEM (merge_layer)->parasites =
gimp_parasite_list_copy (GIMP_ITEM (bottom_layer)->parasites);
while (reverse_list)
{
GimpLayerModeEffects mode;
layer = (GimpLayer *) reverse_list->data;
/* determine what sort of operation is being attempted and
* if it's actually legal...
*/
operation =
gimp_image_get_combination_mode (gimp_drawable_type (GIMP_DRAWABLE (merge_layer)),
gimp_drawable_bytes (GIMP_DRAWABLE (layer)));
if (operation == -1)
{
g_warning ("%s: attempting to merge incompatible layers.", G_STRFUNC);
return NULL;
}
gimp_item_offsets (GIMP_ITEM (layer), &off_x, &off_y);
x3 = CLAMP (off_x, x1, x2);
y3 = CLAMP (off_y, y1, y2);
x4 = CLAMP (off_x + gimp_item_width (GIMP_ITEM (layer)), x1, x2);
y4 = CLAMP (off_y + gimp_item_height (GIMP_ITEM (layer)), y1, y2);
/* configure the pixel regions */
pixel_region_init (&src1PR,
gimp_drawable_data (GIMP_DRAWABLE (merge_layer)),
(x3 - x1), (y3 - y1), (x4 - x3), (y4 - y3),
TRUE);
pixel_region_init (&src2PR,
gimp_drawable_data (GIMP_DRAWABLE (layer)),
(x3 - off_x), (y3 - off_y),
(x4 - x3), (y4 - y3),
FALSE);
if (layer->mask && layer->mask->apply_mask)
{
pixel_region_init (&maskPR,
gimp_drawable_data (GIMP_DRAWABLE (layer->mask)),
(x3 - off_x), (y3 - off_y),
(x4 - x3), (y4 - y3),
FALSE);
mask = &maskPR;
}
else
{
mask = NULL;
}
/* DISSOLVE_MODE is special since it is the only mode that does not
* work on the projection with the lower layer, but only locally on
* the layers alpha channel.
*/
mode = layer->mode;
if (layer == bottom_layer && mode != GIMP_DISSOLVE_MODE)
mode = GIMP_NORMAL_MODE;
combine_regions (&src1PR, &src2PR, &src1PR, mask, NULL,
layer->opacity * 255.999,
mode,
active,
operation);
gimp_image_remove_layer (gimage, layer);
reverse_list = g_slist_next (reverse_list);
}
g_slist_free (reverse_list);
/* if the type is flatten, remove all the remaining layers */
if (merge_type == GIMP_FLATTEN_IMAGE)
{
list = GIMP_LIST (gimage->layers)->list;
while (list)
{
layer = (GimpLayer *) list->data;
list = g_list_next (list);
gimp_image_remove_layer (gimage, layer);
}
gimp_image_add_layer (gimage, merge_layer, position);
}
else
{
/* Add the layer to the gimage */
gimp_image_add_layer (gimage, merge_layer,
gimp_container_num_children (gimage->layers) - position + 1);
}
/* set the name after the original layers have been removed so we
* don't end up with #2 appended to the name
*/
gimp_object_set_name (GIMP_OBJECT (merge_layer), name);
g_free (name);
gimp_item_set_visible (GIMP_ITEM (merge_layer), TRUE, TRUE);
/* End the merge undo group */
gimp_image_undo_group_end (gimage);
gimp_drawable_update (GIMP_DRAWABLE (merge_layer),
0, 0,
gimp_item_width (GIMP_ITEM (merge_layer)),
gimp_item_height (GIMP_ITEM (merge_layer)));
return merge_layer;
}
/* merging vectors */
GimpVectors *
gimp_image_merge_visible_vectors (GimpImage *gimage)
{
GList *list = NULL;
GSList *merge_list = NULL;
GSList *cur_item = NULL;
GimpVectors *vectors = NULL;
GimpVectors *target_vectors = NULL;
guchar *name = NULL;
gint pos = 0;
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
for (list = GIMP_LIST (gimage->vectors)->list;
list;
list = g_list_next (list))
{
vectors = (GimpVectors *) list->data;
if (gimp_item_get_visible (GIMP_ITEM (vectors)))
merge_list = g_slist_append (merge_list, vectors);
}
if (merge_list && merge_list->next)
{
gimp_set_busy (gimage->gimp);
gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_IMAGE_VECTORS_MERGE,
_("Merge Visible Paths"));
cur_item = merge_list;
vectors = GIMP_VECTORS (cur_item->data);
name = g_strdup (gimp_object_get_name (GIMP_OBJECT (vectors)));
target_vectors = GIMP_VECTORS (
gimp_item_duplicate (GIMP_ITEM (vectors),
GIMP_TYPE_VECTORS,
FALSE));
pos = gimp_image_get_vectors_index (gimage, vectors);
gimp_image_remove_vectors (gimage, vectors);
cur_item = cur_item->next;
while (cur_item)
{
vectors = GIMP_VECTORS (cur_item->data);
gimp_vectors_add_strokes (vectors, target_vectors);
gimp_image_remove_vectors (gimage, vectors);
cur_item = g_slist_next (cur_item);
}
gimp_object_set_name (GIMP_OBJECT (target_vectors), name);
g_free (name);
g_slist_free (merge_list);
gimp_image_add_vectors (gimage, target_vectors, pos);
gimp_unset_busy (gimage->gimp);
gimp_image_undo_group_end (gimage);
return target_vectors;
}
else
{
g_message (_("Not enough visible paths for a merge. "
"There must be at least two."));
return NULL;
}
}
|