2006-12-09 21:33:38 +00:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
2001-07-07 14:53:42 +00:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
2009-01-17 22:28:01 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
2001-07-07 14:53:42 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-17 22:28:01 +00:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2001-07-07 14:53:42 +00:00
|
|
|
* (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
|
2018-07-11 23:27:07 +02:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2001-07-07 14:53:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2013-10-15 01:58:39 +02:00
|
|
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
2008-10-09 20:24:04 +00:00
|
|
|
#include <gegl.h>
|
2001-07-07 14:53:42 +00:00
|
|
|
|
|
|
|
#include "core-types.h"
|
|
|
|
|
2001-07-11 12:39:49 +00:00
|
|
|
#include "gimp.h"
|
2004-04-14 23:37:34 +00:00
|
|
|
#include "gimpcontext.h"
|
2006-06-06 22:48:57 +00:00
|
|
|
#include "gimpguide.h"
|
2001-07-07 14:53:42 +00:00
|
|
|
#include "gimpimage.h"
|
|
|
|
#include "gimpimage-crop.h"
|
2003-03-28 13:52:01 +00:00
|
|
|
#include "gimpimage-guides.h"
|
2005-03-04 16:34:59 +00:00
|
|
|
#include "gimpimage-sample-points.h"
|
2003-02-13 11:23:50 +00:00
|
|
|
#include "gimpimage-undo.h"
|
2003-02-14 14:14:29 +00:00
|
|
|
#include "gimpimage-undo-push.h"
|
2001-07-07 14:53:42 +00:00
|
|
|
#include "gimplayer.h"
|
2007-01-30 10:34:59 +00:00
|
|
|
#include "gimpsamplepoint.h"
|
2001-07-07 14:53:42 +00:00
|
|
|
|
2003-03-25 16:38:19 +00:00
|
|
|
#include "gimp-intl.h"
|
2003-02-13 11:23:50 +00:00
|
|
|
|
2001-07-07 14:53:42 +00:00
|
|
|
|
|
|
|
/* public functions */
|
|
|
|
|
|
|
|
void
|
2016-10-10 00:02:16 +02:00
|
|
|
gimp_image_crop (GimpImage *image,
|
|
|
|
GimpContext *context,
|
|
|
|
GimpFillType fill_type,
|
|
|
|
gint x,
|
|
|
|
gint y,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
|
|
|
gboolean crop_layers)
|
2001-07-07 14:53:42 +00:00
|
|
|
{
|
2012-09-22 15:06:35 +02:00
|
|
|
GList *list;
|
2015-07-03 18:56:59 +02:00
|
|
|
gint previous_width;
|
|
|
|
gint previous_height;
|
2003-05-09 11:27:21 +00:00
|
|
|
|
2006-03-28 17:08:36 +00:00
|
|
|
g_return_if_fail (GIMP_IS_IMAGE (image));
|
2004-04-14 23:37:34 +00:00
|
|
|
g_return_if_fail (GIMP_IS_CONTEXT (context));
|
2001-07-07 14:53:42 +00:00
|
|
|
|
2015-07-03 18:56:59 +02:00
|
|
|
previous_width = gimp_image_get_width (image);
|
2008-08-28 19:12:03 +00:00
|
|
|
previous_height = gimp_image_get_height (image);
|
|
|
|
|
2001-07-07 14:53:42 +00:00
|
|
|
/* Make sure new width and height are non-zero */
|
2003-05-09 11:27:21 +00:00
|
|
|
if (width < 1 || height < 1)
|
|
|
|
return;
|
|
|
|
|
2006-03-28 17:08:36 +00:00
|
|
|
gimp_set_busy (image->gimp);
|
2001-07-07 14:53:42 +00:00
|
|
|
|
2012-09-22 15:06:35 +02:00
|
|
|
g_object_freeze_notify (G_OBJECT (image));
|
2001-07-07 14:53:42 +00:00
|
|
|
|
2012-09-22 15:06:35 +02:00
|
|
|
if (crop_layers)
|
|
|
|
gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_IMAGE_CROP,
|
|
|
|
C_("undo-type", "Crop Image"));
|
2003-05-09 11:27:21 +00:00
|
|
|
else
|
2012-09-22 15:06:35 +02:00
|
|
|
gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_IMAGE_RESIZE,
|
|
|
|
C_("undo-type", "Resize Image"));
|
|
|
|
|
|
|
|
/* Push the image size to the stack */
|
|
|
|
gimp_image_undo_push_image_size (image, NULL,
|
2015-07-03 18:56:59 +02:00
|
|
|
x, y, width, height);
|
2012-09-22 15:06:35 +02:00
|
|
|
|
|
|
|
/* Set the new width and height */
|
|
|
|
g_object_set (image,
|
|
|
|
"width", width,
|
|
|
|
"height", height,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* Resize all channels */
|
|
|
|
for (list = gimp_image_get_channel_iter (image);
|
|
|
|
list;
|
|
|
|
list = g_list_next (list))
|
2003-05-09 11:27:21 +00:00
|
|
|
{
|
2012-09-22 15:06:35 +02:00
|
|
|
GimpItem *item = list->data;
|
2009-08-02 17:44:05 +02:00
|
|
|
|
2016-10-10 00:02:16 +02:00
|
|
|
gimp_item_resize (item, context, GIMP_FILL_TRANSPARENT,
|
|
|
|
width, height, -x, -y);
|
2012-09-22 15:06:35 +02:00
|
|
|
}
|
2004-07-14 12:12:50 +00:00
|
|
|
|
2025-07-07 13:17:06 +02:00
|
|
|
/* Resize all paths */
|
2024-07-11 00:07:44 +00:00
|
|
|
for (list = gimp_image_get_path_iter (image);
|
2012-09-22 15:06:35 +02:00
|
|
|
list;
|
|
|
|
list = g_list_next (list))
|
|
|
|
{
|
|
|
|
GimpItem *item = list->data;
|
2001-07-07 14:53:42 +00:00
|
|
|
|
2016-10-10 00:02:16 +02:00
|
|
|
gimp_item_resize (item, context, GIMP_FILL_TRANSPARENT,
|
|
|
|
width, height, -x, -y);
|
2012-09-22 15:06:35 +02:00
|
|
|
}
|
2001-07-07 14:53:42 +00:00
|
|
|
|
2012-09-22 15:06:35 +02:00
|
|
|
/* Don't forget the selection mask! */
|
2016-10-10 00:02:16 +02:00
|
|
|
gimp_item_resize (GIMP_ITEM (gimp_image_get_mask (image)),
|
|
|
|
context, GIMP_FILL_TRANSPARENT,
|
2015-07-03 18:56:59 +02:00
|
|
|
width, height, -x, -y);
|
2003-05-09 00:49:56 +00:00
|
|
|
|
2012-09-22 15:06:35 +02:00
|
|
|
/* crop all layers */
|
|
|
|
list = gimp_image_get_layer_iter (image);
|
2003-05-09 00:49:56 +00:00
|
|
|
|
2012-09-22 15:06:35 +02:00
|
|
|
while (list)
|
|
|
|
{
|
|
|
|
GimpItem *item = list->data;
|
|
|
|
|
|
|
|
list = g_list_next (list);
|
2001-07-07 14:53:42 +00:00
|
|
|
|
2015-07-03 18:56:59 +02:00
|
|
|
gimp_item_translate (item, -x, -y, TRUE);
|
2009-08-27 19:31:55 +02:00
|
|
|
|
2022-02-15 15:42:44 +01:00
|
|
|
if (crop_layers && ! gimp_item_is_content_locked (item, NULL))
|
2003-05-09 11:27:21 +00:00
|
|
|
{
|
2012-09-22 15:06:35 +02:00
|
|
|
gint off_x, off_y;
|
|
|
|
gint lx1, ly1, lx2, ly2;
|
|
|
|
|
|
|
|
gimp_item_get_offset (item, &off_x, &off_y);
|
2001-07-07 14:53:42 +00:00
|
|
|
|
2012-09-22 15:06:35 +02:00
|
|
|
lx1 = CLAMP (off_x, 0, gimp_image_get_width (image));
|
|
|
|
ly1 = CLAMP (off_y, 0, gimp_image_get_height (image));
|
|
|
|
lx2 = CLAMP (gimp_item_get_width (item) + off_x,
|
|
|
|
0, gimp_image_get_width (image));
|
|
|
|
ly2 = CLAMP (gimp_item_get_height (item) + off_y,
|
|
|
|
0, gimp_image_get_height (image));
|
2009-08-25 15:46:53 +02:00
|
|
|
|
2012-09-22 15:06:35 +02:00
|
|
|
width = lx2 - lx1;
|
|
|
|
height = ly2 - ly1;
|
2001-07-07 14:53:42 +00:00
|
|
|
|
2024-05-26 00:18:56 +05:30
|
|
|
gimp_drawable_enable_resize_undo (GIMP_DRAWABLE (item));
|
|
|
|
|
2012-09-22 15:06:35 +02:00
|
|
|
if (width > 0 && height > 0)
|
2003-05-09 11:27:21 +00:00
|
|
|
{
|
2016-10-10 00:02:16 +02:00
|
|
|
gimp_item_resize (item, context, fill_type,
|
|
|
|
width, height,
|
2012-09-22 15:06:35 +02:00
|
|
|
-(lx1 - off_x),
|
|
|
|
-(ly1 - off_y));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gimp_image_remove_layer (image, GIMP_LAYER (item),
|
|
|
|
TRUE, NULL);
|
2003-05-09 11:27:21 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-22 15:06:35 +02:00
|
|
|
}
|
2001-07-07 14:53:42 +00:00
|
|
|
|
2016-01-04 22:06:27 +01:00
|
|
|
/* Reposition or remove guides */
|
2012-09-22 15:06:35 +02:00
|
|
|
list = gimp_image_get_guides (image);
|
2001-07-07 14:53:42 +00:00
|
|
|
|
2012-09-22 15:06:35 +02:00
|
|
|
while (list)
|
|
|
|
{
|
|
|
|
GimpGuide *guide = list->data;
|
|
|
|
gboolean remove_guide = FALSE;
|
|
|
|
gint position = gimp_guide_get_position (guide);
|
2001-07-07 14:53:42 +00:00
|
|
|
|
2012-09-22 15:06:35 +02:00
|
|
|
list = g_list_next (list);
|
2001-07-07 14:53:42 +00:00
|
|
|
|
2012-09-22 15:06:35 +02:00
|
|
|
switch (gimp_guide_get_orientation (guide))
|
|
|
|
{
|
|
|
|
case GIMP_ORIENTATION_HORIZONTAL:
|
2016-01-04 22:06:27 +01:00
|
|
|
position -= y;
|
|
|
|
if ((position < 0) || (position > height))
|
2012-09-22 15:06:35 +02:00
|
|
|
remove_guide = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_ORIENTATION_VERTICAL:
|
2016-01-04 22:06:27 +01:00
|
|
|
position -= x;
|
|
|
|
if ((position < 0) || (position > width))
|
2012-09-22 15:06:35 +02:00
|
|
|
remove_guide = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2003-05-09 11:27:21 +00:00
|
|
|
}
|
2001-07-07 14:53:42 +00:00
|
|
|
|
2012-09-22 15:06:35 +02:00
|
|
|
if (remove_guide)
|
|
|
|
gimp_image_remove_guide (image, guide, TRUE);
|
|
|
|
else if (position != gimp_guide_get_position (guide))
|
|
|
|
gimp_image_move_guide (image, guide, position, TRUE);
|
|
|
|
}
|
2009-08-27 19:31:55 +02:00
|
|
|
|
2012-09-22 15:06:35 +02:00
|
|
|
/* Reposition or remove sample points */
|
|
|
|
list = gimp_image_get_sample_points (image);
|
2005-03-04 16:34:59 +00:00
|
|
|
|
2012-09-22 15:06:35 +02:00
|
|
|
while (list)
|
|
|
|
{
|
|
|
|
GimpSamplePoint *sample_point = list->data;
|
|
|
|
gboolean remove_sample_point = FALSE;
|
2016-01-04 22:06:27 +01:00
|
|
|
gint old_x;
|
|
|
|
gint old_y;
|
|
|
|
gint new_x;
|
|
|
|
gint new_y;
|
2012-09-22 15:06:35 +02:00
|
|
|
|
|
|
|
list = g_list_next (list);
|
|
|
|
|
2016-01-04 22:06:27 +01:00
|
|
|
gimp_sample_point_get_position (sample_point, &old_x, &old_y);
|
|
|
|
new_x = old_x;
|
|
|
|
new_y = old_y;
|
|
|
|
|
2015-07-03 18:56:59 +02:00
|
|
|
new_y -= y;
|
2016-01-04 22:06:27 +01:00
|
|
|
if ((new_y < 0) || (new_y > height))
|
|
|
|
remove_sample_point = TRUE;
|
2012-09-22 15:06:35 +02:00
|
|
|
|
2015-07-03 18:56:59 +02:00
|
|
|
new_x -= x;
|
2016-01-04 22:06:27 +01:00
|
|
|
if ((new_x < 0) || (new_x > width))
|
2012-09-22 15:06:35 +02:00
|
|
|
remove_sample_point = TRUE;
|
|
|
|
|
|
|
|
if (remove_sample_point)
|
|
|
|
gimp_image_remove_sample_point (image, sample_point, TRUE);
|
2016-01-04 22:06:27 +01:00
|
|
|
else if (new_x != old_x || new_y != old_y)
|
2012-09-22 15:06:35 +02:00
|
|
|
gimp_image_move_sample_point (image, sample_point,
|
|
|
|
new_x, new_y, TRUE);
|
|
|
|
}
|
2001-07-07 14:53:42 +00:00
|
|
|
|
2012-09-22 15:06:35 +02:00
|
|
|
gimp_image_undo_group_end (image);
|
2001-10-31 21:18:57 +00:00
|
|
|
|
2012-09-22 15:06:35 +02:00
|
|
|
gimp_image_size_changed_detailed (image,
|
2015-07-03 18:56:59 +02:00
|
|
|
-x, -y,
|
2012-09-22 16:34:14 +02:00
|
|
|
previous_width, previous_height);
|
2012-09-22 15:06:35 +02:00
|
|
|
|
|
|
|
g_object_thaw_notify (G_OBJECT (image));
|
2003-05-09 11:27:21 +00:00
|
|
|
|
2006-03-28 17:08:36 +00:00
|
|
|
gimp_unset_busy (image->gimp);
|
2001-07-07 14:53:42 +00:00
|
|
|
}
|