~ubuntu-branches/ubuntu/maverick/gimp/maverick-updates

« back to all changes in this revision

Viewing changes to app/core/gimp-edit.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-12-09 19:44:52 UTC
  • Revision ID: james.westby@ubuntu.com-20051209194452-yggpemjlofpjqyf4
Tags: upstream-2.2.9
ImportĀ upstreamĀ versionĀ 2.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an 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 2 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, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 */
 
18
 
 
19
#include "config.h"
 
20
 
 
21
#include <stdlib.h>
 
22
 
 
23
#include <glib-object.h>
 
24
 
 
25
#include "core-types.h"
 
26
 
 
27
#include "base/pixel-region.h"
 
28
#include "base/temp-buf.h"
 
29
#include "base/tile-manager.h"
 
30
#include "base/tile-manager-crop.h"
 
31
 
 
32
#include "paint-funcs/paint-funcs.h"
 
33
 
 
34
#include "gimp.h"
 
35
#include "gimp-edit.h"
 
36
#include "gimp-utils.h"
 
37
#include "gimpbuffer.h"
 
38
#include "gimpchannel.h"
 
39
#include "gimpcontext.h"
 
40
#include "gimpimage.h"
 
41
#include "gimpimage-undo.h"
 
42
#include "gimplayer.h"
 
43
#include "gimplayer-floating-sel.h"
 
44
#include "gimplist.h"
 
45
#include "gimppattern.h"
 
46
#include "gimpprojection.h"
 
47
#include "gimpselection.h"
 
48
 
 
49
#include "gimp-intl.h"
 
50
 
 
51
 
 
52
/*  local function protypes  */
 
53
 
 
54
static const GimpBuffer * gimp_edit_extract       (GimpImage    *gimage,
 
55
                                                   GimpDrawable *drawable,
 
56
                                                   GimpContext  *context,
 
57
                                                   gboolean      cut_pixels);
 
58
static gboolean           gimp_edit_fill_internal (GimpImage    *gimage,
 
59
                                                   GimpDrawable *drawable,
 
60
                                                   GimpContext  *context,
 
61
                                                   GimpFillType  fill_type,
 
62
                                                   const gchar  *undo_desc);
 
63
 
 
64
 
 
65
/*  public functions  */
 
66
 
 
67
const GimpBuffer *
 
68
gimp_edit_cut (GimpImage    *gimage,
 
69
               GimpDrawable *drawable,
 
70
               GimpContext  *context)
 
71
{
 
72
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
73
  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
 
74
  g_return_val_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)), NULL);
 
75
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
 
76
 
 
77
  return gimp_edit_extract (gimage, drawable, context, TRUE);
 
78
}
 
79
 
 
80
const GimpBuffer *
 
81
gimp_edit_copy (GimpImage    *gimage,
 
82
                GimpDrawable *drawable,
 
83
                GimpContext  *context)
 
84
{
 
85
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
86
  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
 
87
  g_return_val_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)), NULL);
 
88
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
 
89
 
 
90
  return gimp_edit_extract (gimage, drawable, context, FALSE);
 
91
}
 
92
 
 
93
const GimpBuffer *
 
94
gimp_edit_copy_visible (GimpImage   *gimage,
 
95
                        GimpContext *context)
 
96
{
 
97
  PixelRegion  srcPR, destPR;
 
98
  TileManager *tiles;
 
99
  gboolean     non_empty;
 
100
  gint         x1, y1, x2, y2;
 
101
 
 
102
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
103
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
 
104
 
 
105
  non_empty = gimp_channel_bounds (gimp_image_get_mask (gimage),
 
106
                                   &x1, &y1, &x2, &y2);
 
107
  if ((x1 == x2) || (y1 == y2))
 
108
    {
 
109
      g_message (_("Unable to cut or copy because the "
 
110
                   "selected region is empty."));
 
111
      return NULL;
 
112
    }
 
113
 
 
114
  tiles = tile_manager_new (x2 - x1, y2 - y1,
 
115
                            gimp_projection_get_bytes (gimage->projection));
 
116
  tile_manager_set_offsets (tiles, x1, y1);
 
117
 
 
118
  pixel_region_init (&srcPR, gimp_projection_get_tiles (gimage->projection),
 
119
                     x1, y1,
 
120
                     x2 - x1, y2 - y1,
 
121
                     FALSE);
 
122
  pixel_region_init (&destPR, tiles,
 
123
                     0, 0,
 
124
                     x2 - x1, y2 - y1,
 
125
                     TRUE);
 
126
 
 
127
  /*  use EEKy no-COW copying because sharing tiles with the projection
 
128
   *  is buggy as hell, probably because tile_invalidate() doesn't
 
129
   *  do what it should  --mitch
 
130
   */
 
131
  copy_region_nocow (&srcPR, &destPR);
 
132
 
 
133
  /*  Only crop if the gimage mask wasn't empty  */
 
134
  if (non_empty)
 
135
    {
 
136
      TileManager *crop = tile_manager_crop (tiles, 0);
 
137
 
 
138
      if (crop != tiles)
 
139
        {
 
140
          tile_manager_unref (tiles);
 
141
          tiles = crop;
 
142
        }
 
143
    }
 
144
 
 
145
  if (tiles)
 
146
    {
 
147
      GimpBuffer *buffer = gimp_buffer_new (tiles, "Global Buffer", FALSE);
 
148
 
 
149
      gimp_set_global_buffer (gimage->gimp, buffer);
 
150
      g_object_unref (buffer);
 
151
 
 
152
      return gimage->gimp->global_buffer;
 
153
    }
 
154
 
 
155
  return NULL;
 
156
}
 
157
 
 
158
GimpLayer *
 
159
gimp_edit_paste (GimpImage    *gimage,
 
160
                 GimpDrawable *drawable,
 
161
                 GimpBuffer   *paste,
 
162
                 gboolean      paste_into,
 
163
                 gint          viewport_x,
 
164
                 gint          viewport_y,
 
165
                 gint          viewport_width,
 
166
                 gint          viewport_height)
 
167
{
 
168
  GimpLayer     *layer;
 
169
  GimpImageType  type;
 
170
  gint           center_x;
 
171
  gint           center_y;
 
172
  gint           offset_x;
 
173
  gint           offset_y;
 
174
  gint           width;
 
175
  gint           height;
 
176
 
 
177
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
178
  g_return_val_if_fail (drawable == NULL || GIMP_IS_DRAWABLE (drawable), NULL);
 
179
  g_return_val_if_fail (drawable == NULL ||
 
180
                        gimp_item_is_attached (GIMP_ITEM (drawable)), NULL);
 
181
  g_return_val_if_fail (GIMP_IS_BUFFER (paste), NULL);
 
182
 
 
183
  /*  Make a new layer: if drawable == NULL,
 
184
   *  user is pasting into an empty image.
 
185
   */
 
186
 
 
187
  if (drawable)
 
188
    type = gimp_drawable_type_with_alpha (drawable);
 
189
  else
 
190
    type = gimp_image_base_type_with_alpha (gimage);
 
191
 
 
192
  layer = gimp_layer_new_from_tiles (paste->tiles,
 
193
                                     gimage,
 
194
                                     type,
 
195
                                     _("Pasted Layer"),
 
196
                                     GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
 
197
 
 
198
  if (! layer)
 
199
    return NULL;
 
200
 
 
201
  if (drawable)
 
202
    {
 
203
      /*  if pasting to a drawable  */
 
204
 
 
205
      gint     off_x, off_y;
 
206
      gint     x1, y1, x2, y2;
 
207
      gint     paste_x, paste_y;
 
208
      gint     paste_width, paste_height;
 
209
      gboolean have_mask;
 
210
 
 
211
      gimp_item_offsets (GIMP_ITEM (drawable), &off_x, &off_y);
 
212
      have_mask = gimp_drawable_mask_bounds (drawable, &x1, &y1, &x2, &y2);
 
213
 
 
214
      if (! have_mask         &&
 
215
          viewport_width  > 0 &&
 
216
          viewport_height > 0 &&
 
217
          gimp_rectangle_intersect (viewport_x, viewport_y,
 
218
                                    viewport_width, viewport_height,
 
219
                                    off_x, off_y,
 
220
                                    x2 - x1, y2 - y1,
 
221
                                    &paste_x, &paste_y,
 
222
                                    &paste_width, &paste_height))
 
223
        {
 
224
          center_x = paste_x + paste_width  / 2;
 
225
          center_y = paste_y + paste_height / 2;
 
226
        }
 
227
      else
 
228
        {
 
229
          center_x = off_x + (x1 + x2) / 2;
 
230
          center_y = off_y + (y1 + y2) / 2;
 
231
        }
 
232
    }
 
233
  else if (viewport_width > 0 && viewport_height > 0)
 
234
    {
 
235
      /*  if we got a viewport set the offsets to the center of the viewport  */
 
236
 
 
237
      center_x = viewport_x + viewport_width  / 2;
 
238
      center_y = viewport_y + viewport_height / 2;
 
239
    }
 
240
  else
 
241
    {
 
242
      /*  otherwise the offsets to the center of the image  */
 
243
 
 
244
      center_x = gimage->width  / 2;
 
245
      center_y = gimage->height / 2;
 
246
    }
 
247
 
 
248
  width  = gimp_item_width  (GIMP_ITEM (layer));
 
249
  height = gimp_item_height (GIMP_ITEM (layer));
 
250
 
 
251
  offset_x = center_x - width  / 2;
 
252
  offset_y = center_y - height / 2;
 
253
 
 
254
  /*  Ensure that the pasted layer is always within the image, if it
 
255
   *  fits and aligned at top left if it doesn't. (See bug #142944).
 
256
   */
 
257
  offset_x = MIN (offset_x, gimage->width  - width);
 
258
  offset_y = MIN (offset_y, gimage->height - height);
 
259
  offset_x = MAX (offset_x, 0);
 
260
  offset_y = MAX (offset_y, 0);
 
261
 
 
262
  GIMP_ITEM (layer)->offset_x = offset_x;
 
263
  GIMP_ITEM (layer)->offset_y = offset_y;
 
264
 
 
265
  /*  Start a group undo  */
 
266
  gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_EDIT_PASTE,
 
267
                               _("Paste"));
 
268
 
 
269
  /*  If there is a selection mask clear it--
 
270
   *  this might not always be desired, but in general,
 
271
   *  it seems like the correct behavior.
 
272
   */
 
273
  if (! gimp_channel_is_empty (gimp_image_get_mask (gimage)) && ! paste_into)
 
274
    gimp_channel_clear (gimp_image_get_mask (gimage), NULL, TRUE);
 
275
 
 
276
  /*  if there's a drawable, add a new floating selection  */
 
277
  if (drawable)
 
278
    floating_sel_attach (layer, drawable);
 
279
  else
 
280
    gimp_image_add_layer (gimage, layer, 0);
 
281
 
 
282
  /*  end the group undo  */
 
283
  gimp_image_undo_group_end (gimage);
 
284
 
 
285
  return layer;
 
286
}
 
287
 
 
288
GimpImage *
 
289
gimp_edit_paste_as_new (Gimp       *gimp,
 
290
                        GimpImage  *invoke,
 
291
                        GimpBuffer *paste)
 
292
{
 
293
  GimpImage *gimage;
 
294
  GimpLayer *layer;
 
295
 
 
296
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
 
297
  g_return_val_if_fail (invoke == NULL || GIMP_IS_IMAGE (invoke), NULL);
 
298
  g_return_val_if_fail (GIMP_IS_BUFFER (paste), NULL);
 
299
 
 
300
  /*  create a new image  (always of type GIMP_RGB)  */
 
301
  gimage = gimp_create_image (gimp,
 
302
                              gimp_buffer_get_width (paste),
 
303
                              gimp_buffer_get_height (paste),
 
304
                              GIMP_RGB,
 
305
                              TRUE);
 
306
  gimp_image_undo_disable (gimage);
 
307
 
 
308
  if (invoke)
 
309
    {
 
310
      gimp_image_set_resolution (gimage,
 
311
                                 invoke->xresolution, invoke->yresolution);
 
312
      gimp_image_set_unit (gimage,
 
313
                           gimp_image_get_unit (invoke));
 
314
    }
 
315
 
 
316
  layer = gimp_layer_new_from_tiles (paste->tiles,
 
317
                                     gimage,
 
318
                                     gimp_image_base_type_with_alpha (gimage),
 
319
                                     _("Pasted Layer"),
 
320
                                     GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
 
321
 
 
322
  if (! layer)
 
323
    {
 
324
      g_object_unref (gimage);
 
325
      return NULL;
 
326
    }
 
327
 
 
328
  gimp_image_add_layer (gimage, layer, 0);
 
329
 
 
330
  gimp_image_undo_enable (gimage);
 
331
 
 
332
  gimp_create_display (gimp, gimage, GIMP_UNIT_PIXEL, 1.0);
 
333
  g_object_unref (gimage);
 
334
 
 
335
  return gimage;
 
336
}
 
337
 
 
338
gboolean
 
339
gimp_edit_clear (GimpImage    *gimage,
 
340
                 GimpDrawable *drawable,
 
341
                 GimpContext  *context)
 
342
{
 
343
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
344
  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), FALSE);
 
345
  g_return_val_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)), FALSE);
 
346
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), FALSE);
 
347
 
 
348
  return gimp_edit_fill_internal (gimage, drawable, context,
 
349
                                  GIMP_TRANSPARENT_FILL,
 
350
                                  _("Clear"));
 
351
}
 
352
 
 
353
gboolean
 
354
gimp_edit_fill (GimpImage    *gimage,
 
355
                GimpDrawable *drawable,
 
356
                GimpContext  *context,
 
357
                GimpFillType  fill_type)
 
358
{
 
359
  const gchar *undo_desc;
 
360
 
 
361
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
362
  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), FALSE);
 
363
  g_return_val_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)), FALSE);
 
364
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), FALSE);
 
365
 
 
366
  switch (fill_type)
 
367
    {
 
368
    case GIMP_FOREGROUND_FILL:
 
369
      undo_desc = _("Fill with FG Color");
 
370
      break;
 
371
 
 
372
    case GIMP_BACKGROUND_FILL:
 
373
      undo_desc = _("Fill with BG Color");
 
374
      break;
 
375
 
 
376
    case GIMP_WHITE_FILL:
 
377
      undo_desc = _("Fill with White");
 
378
      break;
 
379
 
 
380
    case GIMP_TRANSPARENT_FILL:
 
381
      undo_desc = _("Fill with Transparency");
 
382
      break;
 
383
 
 
384
    case GIMP_PATTERN_FILL:
 
385
      undo_desc = _("Fill with Pattern");
 
386
      break;
 
387
 
 
388
    case GIMP_NO_FILL:
 
389
      return TRUE;  /*  nothing to do, but the fill succeded  */
 
390
 
 
391
    default:
 
392
      g_warning ("%s: unknown fill type", G_STRFUNC);
 
393
      fill_type = GIMP_BACKGROUND_FILL;
 
394
      undo_desc = _("Fill with BG Color");
 
395
      break;
 
396
    }
 
397
 
 
398
  return gimp_edit_fill_internal (gimage, drawable, context,
 
399
                                  fill_type, undo_desc);
 
400
}
 
401
 
 
402
 
 
403
/*  private functions  */
 
404
 
 
405
const GimpBuffer *
 
406
gimp_edit_extract (GimpImage    *gimage,
 
407
                   GimpDrawable *drawable,
 
408
                   GimpContext  *context,
 
409
                   gboolean      cut_pixels)
 
410
{
 
411
  TileManager *tiles;
 
412
  gboolean     empty;
 
413
 
 
414
  /*  See if the gimage mask is empty  */
 
415
  empty = gimp_channel_is_empty (gimp_image_get_mask (gimage));
 
416
 
 
417
  if (cut_pixels)
 
418
    gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_EDIT_CUT, _("Cut"));
 
419
 
 
420
  /*  Cut/copy the mask portion from the gimage  */
 
421
  tiles = gimp_selection_extract (gimp_image_get_mask (gimage),
 
422
                                  drawable, context, cut_pixels, FALSE, TRUE);
 
423
 
 
424
  if (cut_pixels)
 
425
    gimp_image_undo_group_end (gimage);
 
426
 
 
427
  /*  Only crop if the gimage mask wasn't empty  */
 
428
  if (tiles && ! empty)
 
429
    {
 
430
      TileManager *crop = tile_manager_crop (tiles, 0);
 
431
 
 
432
      if (crop != tiles)
 
433
        {
 
434
          tile_manager_unref (tiles);
 
435
          tiles = crop;
 
436
        }
 
437
    }
 
438
 
 
439
  if (tiles)
 
440
    {
 
441
      GimpBuffer *buffer = gimp_buffer_new (tiles, "Global Buffer", FALSE);
 
442
 
 
443
      gimp_set_global_buffer (gimage->gimp, buffer);
 
444
      g_object_unref (buffer);
 
445
 
 
446
      return gimage->gimp->global_buffer;
 
447
    }
 
448
 
 
449
  return NULL;
 
450
}
 
451
 
 
452
static gboolean
 
453
gimp_edit_fill_internal (GimpImage    *gimage,
 
454
                         GimpDrawable *drawable,
 
455
                         GimpContext  *context,
 
456
                         GimpFillType  fill_type,
 
457
                         const gchar  *undo_desc)
 
458
{
 
459
  TileManager *buf_tiles;
 
460
  PixelRegion  bufPR;
 
461
  gint         x, y, width, height;
 
462
  gint         tiles_bytes;
 
463
  guchar       col[MAX_CHANNELS];
 
464
  TempBuf     *pat_buf = NULL;
 
465
  gboolean     new_buf;
 
466
 
 
467
  if (! gimp_drawable_mask_intersect (drawable, &x, &y, &width, &height))
 
468
    return TRUE;  /*  nothing to do, but the fill succeded  */
 
469
 
 
470
  tiles_bytes = gimp_drawable_bytes (drawable);
 
471
 
 
472
  switch (fill_type)
 
473
    {
 
474
    case GIMP_FOREGROUND_FILL:
 
475
      gimp_image_get_foreground (gimage, drawable, context, col);
 
476
      break;
 
477
 
 
478
    case GIMP_BACKGROUND_FILL:
 
479
    case GIMP_TRANSPARENT_FILL:
 
480
      gimp_image_get_background (gimage, drawable, context, col);
 
481
      break;
 
482
 
 
483
    case GIMP_WHITE_FILL:
 
484
      {
 
485
        guchar tmp_col[MAX_CHANNELS];
 
486
 
 
487
        tmp_col[RED_PIX]   = 255;
 
488
        tmp_col[GREEN_PIX] = 255;
 
489
        tmp_col[BLUE_PIX]  = 255;
 
490
        gimp_image_transform_color (gimage, drawable, col, GIMP_RGB, tmp_col);
 
491
      }
 
492
      break;
 
493
 
 
494
    case GIMP_PATTERN_FILL:
 
495
      {
 
496
        GimpPattern *pattern = gimp_context_get_pattern (context);
 
497
 
 
498
        pat_buf = gimp_image_transform_temp_buf (gimage, drawable,
 
499
                                                 pattern->mask, &new_buf);
 
500
 
 
501
        if (! gimp_drawable_has_alpha (drawable) &&
 
502
            (pat_buf->bytes == 2 || pat_buf->bytes == 4))
 
503
          tiles_bytes++;
 
504
      }
 
505
      break;
 
506
 
 
507
    case GIMP_NO_FILL:
 
508
      return TRUE;  /*  nothing to do, but the fill succeded  */
 
509
    }
 
510
 
 
511
  buf_tiles = tile_manager_new (width, height, tiles_bytes);
 
512
 
 
513
  pixel_region_init (&bufPR, buf_tiles, 0, 0, width, height, TRUE);
 
514
 
 
515
  if (pat_buf)
 
516
    {
 
517
      pattern_region (&bufPR, NULL, pat_buf, 0, 0);
 
518
 
 
519
      if (new_buf)
 
520
        temp_buf_free (pat_buf);
 
521
    }
 
522
  else
 
523
    {
 
524
      if (gimp_drawable_has_alpha (drawable))
 
525
        col[gimp_drawable_bytes (drawable) - 1] = OPAQUE_OPACITY;
 
526
 
 
527
      color_region (&bufPR, col);
 
528
    }
 
529
 
 
530
  pixel_region_init (&bufPR, buf_tiles, 0, 0, width, height, FALSE);
 
531
  gimp_drawable_apply_region (drawable, &bufPR,
 
532
                              TRUE, undo_desc,
 
533
                              GIMP_OPACITY_OPAQUE,
 
534
                              (fill_type == GIMP_TRANSPARENT_FILL) ?
 
535
                              GIMP_ERASE_MODE : GIMP_NORMAL_MODE,
 
536
                              NULL, x, y);
 
537
 
 
538
  tile_manager_unref (buf_tiles);
 
539
 
 
540
  gimp_drawable_update (drawable, x, y, width, height);
 
541
 
 
542
  return TRUE;
 
543
}