gimp-edit.c 10.7 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17
 */
Sven Neumann's avatar
Sven Neumann committed
18 19 20

#include "config.h"

Elliot Lee's avatar
Elliot Lee committed
21
#include <stdlib.h>
Sven Neumann's avatar
Sven Neumann committed
22

23
#include <glib-object.h>
24

25
#include "core-types.h"
Sven Neumann's avatar
Sven Neumann committed
26

Michael Natterer's avatar
Michael Natterer committed
27 28
#include "base/pixel-region.h"
#include "base/tile-manager.h"
Michael Natterer's avatar
Michael Natterer committed
29
#include "base/tile-manager-crop.h"
Michael Natterer's avatar
Michael Natterer committed
30

31 32
#include "paint-funcs/paint-funcs.h"

33
#include "gimp.h"
Michael Natterer's avatar
Michael Natterer committed
34 35 36 37 38 39
#include "gimpbuffer.h"
#include "gimpchannel.h"
#include "gimpcontext.h"
#include "gimpedit.h"
#include "gimpimage.h"
#include "gimpimage-mask.h"
40
#include "gimpimage-undo.h"
Michael Natterer's avatar
Michael Natterer committed
41
#include "gimplayer.h"
Michael Natterer's avatar
Michael Natterer committed
42
#include "gimplayer-floating-sel.h"
Michael Natterer's avatar
Michael Natterer committed
43 44
#include "gimplist.h"

Elliot Lee's avatar
Elliot Lee committed
45
#include "undo.h"
46

47 48
#include "libgimp/gimpintl.h"

49

50
GimpBuffer *
Michael Natterer's avatar
Michael Natterer committed
51 52
gimp_edit_cut (GimpImage    *gimage,
	       GimpDrawable *drawable)
Elliot Lee's avatar
Elliot Lee committed
53 54 55
{
  TileManager *cut;
  TileManager *cropped_cut;
Michael Natterer's avatar
Michael Natterer committed
56
  gboolean     empty;
Elliot Lee's avatar
Elliot Lee committed
57

Michael Natterer's avatar
Michael Natterer committed
58 59
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
Elliot Lee's avatar
Elliot Lee committed
60 61

  /*  Start a group undo  */
62 63
  gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_EDIT_CUT,
                               _("Cut"));
Elliot Lee's avatar
Elliot Lee committed
64 65

  /*  See if the gimage mask is empty  */
66
  empty = gimp_image_mask_is_empty (gimage);
Elliot Lee's avatar
Elliot Lee committed
67 68

  /*  Next, cut the mask portion from the gimage  */
69
  cut = gimp_image_mask_extract (gimage, drawable, TRUE, FALSE, TRUE);
Elliot Lee's avatar
Elliot Lee committed
70

Michael Natterer's avatar
Michael Natterer committed
71
  if (cut)
72
    gimage->gimp->have_current_cut_buffer = TRUE;
Michael Natterer's avatar
Michael Natterer committed
73

Elliot Lee's avatar
Elliot Lee committed
74
  /*  Only crop if the gimage mask wasn't empty  */
Michael Natterer's avatar
Michael Natterer committed
75
  if (cut && ! empty)
Elliot Lee's avatar
Elliot Lee committed
76
    {
Michael Natterer's avatar
Michael Natterer committed
77
      cropped_cut = tile_manager_crop (cut, 0);
Elliot Lee's avatar
Elliot Lee committed
78 79

      if (cropped_cut != cut)
Michael Natterer's avatar
Michael Natterer committed
80 81 82 83
	{
	  tile_manager_destroy (cut);
	  cut = NULL;
	}
Elliot Lee's avatar
Elliot Lee committed
84 85 86 87 88 89 90
    }
  else if (cut)
    cropped_cut = cut;
  else
    cropped_cut = NULL;

  /*  end the group undo  */
91
  gimp_image_undo_group_end (gimage);
Elliot Lee's avatar
Elliot Lee committed
92 93 94 95

  if (cropped_cut)
    {
      /*  Free the old global edit buffer  */
96
      if (gimage->gimp->global_buffer)
97
	g_object_unref (gimage->gimp->global_buffer);
Elliot Lee's avatar
Elliot Lee committed
98

Michael Natterer's avatar
Michael Natterer committed
99
      /*  Set the global edit buffer  */
100 101 102
      gimage->gimp->global_buffer = gimp_buffer_new (cropped_cut,
                                                     "Global Buffer",
                                                     TRUE);
Elliot Lee's avatar
Elliot Lee committed
103
    }
Michael Natterer's avatar
Michael Natterer committed
104

105
  return gimp_buffer_new (cropped_cut, "Cut Pixels", FALSE);
Elliot Lee's avatar
Elliot Lee committed
106 107
}

108
GimpBuffer *
Michael Natterer's avatar
Michael Natterer committed
109 110
gimp_edit_copy (GimpImage    *gimage,
		GimpDrawable *drawable)
Elliot Lee's avatar
Elliot Lee committed
111
{
Michael Natterer's avatar
Michael Natterer committed
112 113
  TileManager *copy;
  TileManager *cropped_copy;
Michael Natterer's avatar
Michael Natterer committed
114
  gboolean     empty;
Elliot Lee's avatar
Elliot Lee committed
115

Michael Natterer's avatar
Michael Natterer committed
116 117
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
Elliot Lee's avatar
Elliot Lee committed
118

119
  /*  Start a group undo  */
120 121
  gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_EDIT_COPY,
                               _("Copy"));
122

Elliot Lee's avatar
Elliot Lee committed
123
  /*  See if the gimage mask is empty  */
124
  empty = gimp_image_mask_is_empty (gimage);
Elliot Lee's avatar
Elliot Lee committed
125 126

  /*  First, copy the masked portion of the gimage  */
127
  copy = gimp_image_mask_extract (gimage, drawable, FALSE, FALSE, TRUE);
Elliot Lee's avatar
Elliot Lee committed
128

Michael Natterer's avatar
Michael Natterer committed
129
  if (copy)
130
    gimage->gimp->have_current_cut_buffer = TRUE;
Michael Natterer's avatar
Michael Natterer committed
131

Elliot Lee's avatar
Elliot Lee committed
132
  /*  Only crop if the gimage mask wasn't empty  */
Michael Natterer's avatar
Michael Natterer committed
133
  if (copy && ! empty)
Elliot Lee's avatar
Elliot Lee committed
134
    {
Michael Natterer's avatar
Michael Natterer committed
135
      cropped_copy = tile_manager_crop (copy, 0);
Elliot Lee's avatar
Elliot Lee committed
136 137

      if (cropped_copy != copy)
Michael Natterer's avatar
Michael Natterer committed
138 139 140 141
	{
	  tile_manager_destroy (copy);
	  copy = NULL;
	}
Elliot Lee's avatar
Elliot Lee committed
142 143 144 145 146 147
    }
  else if (copy)
    cropped_copy = copy;
  else
    cropped_copy = NULL;

148
  /*  end the group undo  */
149
  gimp_image_undo_group_end (gimage);
150

Elliot Lee's avatar
Elliot Lee committed
151 152 153
  if (cropped_copy)
    {
      /*  Free the old global edit buffer  */
154
      if (gimage->gimp->global_buffer)
155
	g_object_unref (gimage->gimp->global_buffer);
Elliot Lee's avatar
Elliot Lee committed
156

Michael Natterer's avatar
Michael Natterer committed
157
      /*  Set the global edit buffer  */
158 159 160
      gimage->gimp->global_buffer = gimp_buffer_new (cropped_copy,
                                                     "Global Buffer",
                                                     TRUE);
Elliot Lee's avatar
Elliot Lee committed
161
    }
Michael Natterer's avatar
Michael Natterer committed
162

163
  return gimp_buffer_new (cropped_copy, "Copied Pixels", FALSE);
Elliot Lee's avatar
Elliot Lee committed
164 165
}

Michael Natterer's avatar
Michael Natterer committed
166
GimpLayer *
Michael Natterer's avatar
Michael Natterer committed
167 168
gimp_edit_paste (GimpImage    *gimage,
		 GimpDrawable *drawable,
169
		 GimpBuffer   *paste,
Michael Natterer's avatar
Michael Natterer committed
170
		 gboolean      paste_into)
Elliot Lee's avatar
Elliot Lee committed
171
{
172 173 174 175
  GimpLayer     *layer;
  GimpImageType  type;
  gint           x1, y1, x2, y2;
  gint           cx, cy;
Michael Natterer's avatar
Michael Natterer committed
176

Michael Natterer's avatar
Michael Natterer committed
177 178 179 180 181
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  g_return_val_if_fail (! drawable || GIMP_IS_DRAWABLE (drawable), NULL);

  /*  Make a new layer: if drawable == NULL,
   *  user is pasting into an empty image.
Michael Natterer's avatar
Michael Natterer committed
182 183
   */

184 185
  if (drawable)
    type = gimp_drawable_type_with_alpha (drawable);
186
  else
187 188 189 190 191 192 193 194
    type = gimp_image_base_type_with_alpha (gimage);

  layer = gimp_layer_new_from_tiles (paste->tiles,
                                     gimage,
                                     type,
                                     _("Pasted Layer"),
                                     GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);

195

Michael Natterer's avatar
Michael Natterer committed
196 197
  if (! layer)
    return NULL;
Elliot Lee's avatar
Elliot Lee committed
198

Michael Natterer's avatar
Michael Natterer committed
199
  /*  Start a group undo  */
200 201
  gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_EDIT_PASTE,
                               _("Paste"));
Elliot Lee's avatar
Elliot Lee committed
202

Michael Natterer's avatar
Michael Natterer committed
203
  /*  Set the offsets to the center of the image  */
204
  if (drawable)
Michael Natterer's avatar
Michael Natterer committed
205 206 207 208 209 210 211 212 213 214 215
    {
      gimp_drawable_offsets (drawable, &cx, &cy);
      gimp_drawable_mask_bounds (drawable, &x1, &y1, &x2, &y2);
      cx += (x1 + x2) >> 1;
      cy += (y1 + y2) >> 1;
    }
  else
    {
      cx = gimage->width  >> 1;
      cy = gimage->height >> 1;
    }
Elliot Lee's avatar
Elliot Lee committed
216

Michael Natterer's avatar
Michael Natterer committed
217 218
  GIMP_DRAWABLE (layer)->offset_x = cx - (GIMP_DRAWABLE (layer)->width  >> 1);
  GIMP_DRAWABLE (layer)->offset_y = cy - (GIMP_DRAWABLE (layer)->height >> 1);
219

Michael Natterer's avatar
Michael Natterer committed
220 221 222 223
  /*  If there is a selection mask clear it--
   *  this might not always be desired, but in general,
   *  it seems like the correct behavior.
   */
224
  if (! gimp_image_mask_is_empty (gimage) && ! paste_into)
225
    gimp_image_mask_clear (gimage);
Elliot Lee's avatar
Elliot Lee committed
226

Michael Natterer's avatar
Michael Natterer committed
227 228 229 230 231 232 233
  /*  if there's a drawable, add a new floating selection  */
  if (drawable != NULL)
    {
      floating_sel_attach (layer, drawable);
    }
  else
    {
234
      gimp_item_set_image (GIMP_ITEM (layer), gimage);
Michael Natterer's avatar
Michael Natterer committed
235
      gimp_image_add_layer (gimage, layer, 0);
Elliot Lee's avatar
Elliot Lee committed
236
    }
Michael Natterer's avatar
Michael Natterer committed
237

Michael Natterer's avatar
Michael Natterer committed
238
  /*  end the group undo  */
239
  gimp_image_undo_group_end (gimage);
Michael Natterer's avatar
Michael Natterer committed
240 241

  return layer;
Elliot Lee's avatar
Elliot Lee committed
242
}
243

Michael Natterer's avatar
Michael Natterer committed
244
GimpImage *
245 246 247
gimp_edit_paste_as_new (Gimp       *gimp,
			GimpImage  *invoke,
			GimpBuffer *paste)
248
{
249 250
  GimpImage    *gimage;
  GimpLayer    *layer;
251

252
  /*  create a new image  (always of type GIMP_RGB)  */
253
  gimage = gimp_create_image (gimp,
254 255
			      gimp_buffer_get_width (paste), 
                              gimp_buffer_get_height (paste), 
256
			      GIMP_RGB,
257
			      TRUE);
258
  gimp_image_undo_disable (gimage);
259 260 261 262 263 264 265

  if (invoke)
    {
      gimp_image_set_resolution (gimage,
				 invoke->xresolution, invoke->yresolution);
      gimp_image_set_unit (gimage, invoke->unit);
    }
Michael Natterer's avatar
Michael Natterer committed
266

267 268
  layer = gimp_layer_new_from_tiles (paste->tiles,
                                     gimage,
269
                                     gimp_image_base_type_with_alpha (gimage),
270
				     _("Pasted Layer"),
271
				     GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
272

Michael Natterer's avatar
Michael Natterer committed
273
  if (layer)
274
    {
275
      gimp_item_set_image (GIMP_ITEM (layer), gimage);
276
      gimp_image_add_layer (gimage, layer, 0);
277

278
      gimp_image_undo_enable (gimage);
279

280 281
      gimp_create_display (gimp, gimage, 0x0101);

282
      g_object_unref (gimage);
283

Michael Natterer's avatar
Michael Natterer committed
284
      return gimage;
285
    }
Michael Natterer's avatar
Michael Natterer committed
286

Michael Natterer's avatar
Michael Natterer committed
287
  return NULL;
288
}
Elliot Lee's avatar
Elliot Lee committed
289

290
gboolean
Michael Natterer's avatar
Michael Natterer committed
291 292
gimp_edit_clear (GimpImage    *gimage,
		 GimpDrawable *drawable)
Elliot Lee's avatar
Elliot Lee committed
293 294
{
  TileManager *buf_tiles;
Michael Natterer's avatar
Michael Natterer committed
295 296 297
  PixelRegion  bufPR;
  gint         x1, y1, x2, y2;
  guchar       col[MAX_CHANNELS];
Elliot Lee's avatar
Elliot Lee committed
298

Michael Natterer's avatar
Michael Natterer committed
299 300
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), FALSE);
Elliot Lee's avatar
Elliot Lee committed
301

302
  gimp_image_get_background (gimage, drawable, col);
303 304
  if (gimp_drawable_has_alpha (drawable))
    col [gimp_drawable_bytes (drawable) - 1] = OPAQUE_OPACITY;
Elliot Lee's avatar
Elliot Lee committed
305

306
  gimp_drawable_mask_bounds (drawable, &x1, &y1, &x2, &y2);
Elliot Lee's avatar
Elliot Lee committed
307 308

  if (!(x2 - x1) || !(y2 - y1))
309
    return TRUE;  /*  nothing to do, but the clear succeded  */
Elliot Lee's avatar
Elliot Lee committed
310

311 312
  buf_tiles = tile_manager_new ((x2 - x1), (y2 - y1),
				gimp_drawable_bytes (drawable));
Elliot Lee's avatar
Elliot Lee committed
313 314 315 316
  pixel_region_init (&bufPR, buf_tiles, 0, 0, (x2 - x1), (y2 - y1), TRUE);
  color_region (&bufPR, col);

  pixel_region_init (&bufPR, buf_tiles, 0, 0, (x2 - x1), (y2 - y1), FALSE);
317 318 319
  gimp_image_apply_image (gimage, drawable, &bufPR, TRUE,
                          GIMP_OPACITY_OPAQUE, GIMP_ERASE_MODE,
                          NULL, x1, y1);
Elliot Lee's avatar
Elliot Lee committed
320 321

  /*  update the image  */
Michael Natterer's avatar
Michael Natterer committed
322 323 324
  gimp_drawable_update (drawable,
			x1, y1,
			(x2 - x1), (y2 - y1));
Elliot Lee's avatar
Elliot Lee committed
325 326 327 328 329 330 331

  /*  free the temporary tiles  */
  tile_manager_destroy (buf_tiles);

  return TRUE;
}

332
gboolean
Michael Natterer's avatar
Michael Natterer committed
333 334 335
gimp_edit_fill (GimpImage    *gimage,
		GimpDrawable *drawable,
		GimpFillType  fill_type)
Elliot Lee's avatar
Elliot Lee committed
336 337
{
  TileManager *buf_tiles;
Michael Natterer's avatar
Michael Natterer committed
338 339 340
  PixelRegion  bufPR;
  gint         x1, y1, x2, y2;
  guchar       col[MAX_CHANNELS];
Elliot Lee's avatar
Elliot Lee committed
341

Michael Natterer's avatar
Michael Natterer committed
342 343
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), FALSE);
Elliot Lee's avatar
Elliot Lee committed
344

345 346
  if (gimp_drawable_has_alpha (drawable))
    col [gimp_drawable_bytes (drawable) - 1] = OPAQUE_OPACITY;
Elliot Lee's avatar
Elliot Lee committed
347

348 349
  switch (fill_type)
    {
350
    case GIMP_FOREGROUND_FILL:
351
      gimp_image_get_foreground (gimage, drawable, col);
352 353
      break;

354
    case GIMP_BACKGROUND_FILL:
355
      gimp_image_get_background (gimage, drawable, col);
356 357
      break;

358
    case GIMP_WHITE_FILL:
359
      col[RED_PIX]   = 255;
360
      col[GREEN_PIX] = 255;
361
      col[BLUE_PIX]  = 255;
362 363
      break;

364
    case GIMP_TRANSPARENT_FILL:
365
      col[RED_PIX]   = 0;
366
      col[GREEN_PIX] = 0;
367
      col[BLUE_PIX]  = 0;
368 369
      if (gimp_drawable_has_alpha (drawable))
	col [gimp_drawable_bytes (drawable) - 1] = TRANSPARENT_OPACITY;
370 371
      break;

372
    case GIMP_NO_FILL:
373 374 375
      return TRUE;  /*  nothing to do, but the fill succeded  */

    default:
376
      g_warning ("%s: unknown fill type", G_GNUC_PRETTY_FUNCTION);
377
      gimp_image_get_background (gimage, drawable, col);
378 379 380
      break;
    }

381
  gimp_drawable_mask_bounds (drawable, &x1, &y1, &x2, &y2);
Elliot Lee's avatar
Elliot Lee committed
382 383

  if (!(x2 - x1) || !(y2 - y1))
384
    return TRUE;  /*  nothing to do, but the fill succeded  */
385

386 387
  buf_tiles = tile_manager_new ((x2 - x1), (y2 - y1),
				gimp_drawable_bytes (drawable));
Elliot Lee's avatar
Elliot Lee committed
388 389 390 391
  pixel_region_init (&bufPR, buf_tiles, 0, 0, (x2 - x1), (y2 - y1), TRUE);
  color_region (&bufPR, col);

  pixel_region_init (&bufPR, buf_tiles, 0, 0, (x2 - x1), (y2 - y1), FALSE);
392 393 394
  gimp_image_apply_image (gimage, drawable, &bufPR, TRUE,
                          GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE,
                          NULL, x1, y1);
Elliot Lee's avatar
Elliot Lee committed
395 396

  /*  update the image  */
Michael Natterer's avatar
Michael Natterer committed
397 398 399
  gimp_drawable_update (drawable,
			x1, y1,
			(x2 - x1), (y2 - y1));
Elliot Lee's avatar
Elliot Lee committed
400 401 402 403 404 405

  /*  free the temporary tiles  */
  tile_manager_destroy (buf_tiles);

  return TRUE;
}