Gimp/pdb/groups/image_autocrop.pdb
Jehan b01ecf80e6 app, libgimp, pdb: allow NULL input drawable to the new crop procedures.
Cropping relatively to the full image contents is a common usage (it is
in fact the "Crop to Content" feature we have in the Image menu) so I
found it a bit frustrating that these 2 functions only allow to crop
relatively to a single drawable.

Therefore, I make NULL an acceptable input which will default to
cropping to the full image content instead.
2025-02-09 19:24:41 +01:00

224 lines
6.6 KiB
Text

# GIMP - The GNU 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 3 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, see <https://www.gnu.org/licenses/>.
# "Perlized" from C source by Manish Singh <yosh@gimp.org>
sub image_autocrop {
$blurb = 'Remove empty borders from the image';
$help = <<'HELP';
Remove empty borders from the @image based on empty borders of the input @drawable.
The input drawable serves as a base for detecting cropping extents
(transparency or background color).
With a %NULL input drawable, the image itself will serve as a base for
detecting cropping extents.
HELP
&std_pdb_misc;
$date = '1997';
@inargs = (
{ name => 'image', type => 'image',
desc => 'Input image)' },
{ name => 'drawable', type => 'drawable', none_ok => 1,
desc => 'Input drawable' }
);
%invoke = (
code => <<'CODE'
{
gint x, y, width, height;
if (drawable != NULL)
{
if (gimp_pdb_item_is_attached (GIMP_ITEM (drawable), NULL,
GIMP_PDB_ITEM_CONTENT, error))
{
gint off_x, off_y;
gimp_pickable_auto_shrink (GIMP_PICKABLE (drawable),
0, 0,
gimp_item_get_width (GIMP_ITEM (drawable)),
gimp_item_get_height (GIMP_ITEM (drawable)),
&x, &y, &width, &height);
gimp_item_get_offset (GIMP_ITEM (drawable), &off_x, &off_y);
x += off_x;
y += off_y;
}
else
{
success = FALSE;
}
}
else
{
gimp_pickable_auto_shrink (GIMP_PICKABLE (image),
0, 0,
gimp_image_get_width (image),
gimp_image_get_height (image),
&x, &y, &width, &height);
}
if (success)
{
gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_ITEM_RESIZE,
_("Autocrop image"));
if (x < 0 ||
y < 0 ||
x + width > gimp_image_get_width (image) ||
y + height > gimp_image_get_height (image))
{
/*
* partially outside the image area, we need to
* resize the image to be able to crop properly.
*/
gimp_image_resize (image, context, width, height, -x, -y, NULL);
x = y = 0;
}
gimp_image_crop (image, context, GIMP_FILL_TRANSPARENT,
x, y, width, height, TRUE);
gimp_image_undo_group_end (image);
}
}
CODE
);
}
sub image_autocrop_selected_layers {
$blurb = 'Crop the selected layers based on empty borders of the input drawable';
$help = <<'HELP';
Crop the selected layers of the input @image based on empty borders of the input @drawable.
The input drawable serves as a base for detecting cropping extents
(transparency or background color), and is not necessarily among the
cropped layers (the current selected layers).
With a %NULL input drawable, the image itself will serve as a base for
detecting cropping extents.
HELP
&std_pdb_misc;
$date = '1997';
@inargs = (
{ name => 'image', type => 'image',
desc => 'Input image' },
{ name => 'drawable', type => 'drawable', none_ok => 1,
desc => 'Input drawable' }
);
%invoke = (
code => <<'CODE'
{
GimpAutoShrink shrink;
gint x, y, width, height;
if (drawable != NULL)
{
if (gimp_pdb_item_is_attached (GIMP_ITEM (drawable), NULL,
GIMP_PDB_ITEM_CONTENT, error))
{
gint off_x, off_y;
shrink = gimp_pickable_auto_shrink (GIMP_PICKABLE (drawable),
0, 0,
gimp_item_get_width (GIMP_ITEM (drawable)),
gimp_item_get_height (GIMP_ITEM (drawable)),
&x, &y, &width, &height);
gimp_item_get_offset (GIMP_ITEM (drawable), &off_x, &off_y);
x += off_x;
y += off_y;
}
else
{
success = FALSE;
}
}
else
{
shrink = gimp_pickable_auto_shrink (GIMP_PICKABLE (image),
0, 0,
gimp_image_get_width (image),
gimp_image_get_height (image),
&x, &y, &width, &height);
}
if (success && shrink != GIMP_AUTO_SHRINK_SHRINK)
success = FALSE;
if (success)
{
GList *layers = gimp_image_get_selected_layers (image);
if (layers)
{
GList *iter;
gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_ITEM_RESIZE,
_("Autocrop layer"));
for (iter = layers; iter; iter = iter->next)
{
gint layer_off_x, layer_off_y;
gimp_item_get_offset (GIMP_ITEM (iter->data), &layer_off_x, &layer_off_y);
gimp_item_resize (GIMP_ITEM (iter->data),
context, GIMP_FILL_TRANSPARENT,
width, height, layer_off_x - x, layer_off_y - y);
}
gimp_image_undo_group_end (image);
}
else
{
success = FALSE;
}
}
}
CODE
);
}
@headers = qw("core/gimpimage-crop.h"
"core/gimpimage-resize.h"
"core/gimpimage-undo.h"
"core/gimppickable.h"
"core/gimppickable-auto-shrink.h"
"gimppdb-utils.h"
"gimp-intl.h");
@procs = qw(image_autocrop
image_autocrop_selected_layers);
%exports = (app => [@procs], lib => [@procs]);
$desc = 'Image Autocrop';
$doc_title = 'gimpimageautocrop';
$doc_short_desc = 'Functions that crop images or layers.';
$doc_long_desc = 'Functions that crop images or layers.';
1;