gimp/plug-ins/common/crop-zealous.c

327 lines
8.7 KiB
C
Raw Normal View History

1997-11-25 06:05:25 +08:00
/*
* ZealousCrop plug-in version 1.00
* by Adam D. Moss <adam@foxbox.org>
*
* 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 <http://www.gnu.org/licenses/>.
1997-11-25 06:05:25 +08:00
*/
#include "config.h"
#include <libgimp/gimp.h>
#include "libgimp/stdplugins-intl.h"
1997-11-25 06:05:25 +08:00
#define PLUG_IN_PROC "plug-in-zealouscrop"
2014-12-11 17:00:00 +08:00
#define EPSILON (1e-5)
#define FLOAT_IS_ZERO(value) (value > -EPSILON && value < EPSILON)
#define FLOAT_EQUAL(v1, v2) ((v1 - v2) > -EPSILON && (v1 - v2) < EPSILON)
1997-11-25 06:05:25 +08:00
/* Declare local functions. */
use the passed Gimp pointer instead of using "the_gimp". 2001-12-03 Michael Natterer <mitch@gimp.org> * app/devices.c: use the passed Gimp pointer instead of using "the_gimp". * app/base/temp-buf.c: indentation. * app/gui/preferences-dialog.c: prefs_toggle_callback(): fixed segfault when trying to find the prefs_dlg widget from a menu item callback (Fixes #65757). * app/gui/offset-dialog.[ch]: fixed public prototype, include the header in the .c file. * app/gui/menus.c: some menu cleanup: moved all functions which operate on the active layer/drawable to <Image>/Layer. Renamed "Layers" to "Layer". * app/display/gimpdisplayshell.c: changed menu update function accordingly. * app/gui/image-commands.[ch] * app/gui/layers-commands.[ch]: moved stuff from image-commands.* to layers-commads.*- * app/tools/gimpblendtool.c * app/tools/gimpbrightnesscontrasttool.c * app/tools/gimpcolorbalancetool.c * app/tools/gimpcurvestool.c * app/tools/gimphistogramtool.c * app/tools/gimphuesaturationtool.c * app/tools/gimplevelstool.c * app/tools/gimpposterizetool.c * app/tools/gimpthresholdtool.c * app/tools/paint_options.c * app/tools/transform_options.c * plug-ins/common/align_layers.c * plug-ins/common/autocrop.c * plug-ins/common/autostretch_hsv.c * plug-ins/common/c_astretch.c * plug-ins/common/color_enhance.c * plug-ins/common/guillotine.c * plug-ins/common/normalize.c * plug-ins/common/rotate.c * plug-ins/common/threshold_alpha.c * plug-ins/common/zealouscrop.c * plug-ins/rcm/rcm.c * plug-ins/fp/fp.c: register under <Image>/Layer, some cosmetic fixes.
2001-12-04 01:59:48 +08:00
static void query (void);
static void run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
2014-12-11 17:00:00 +08:00
static inline gboolean colors_equal (const gfloat *col1,
const gfloat *col2,
gint components,
gboolean has_alpha);
2014-12-11 17:00:00 +08:00
static void do_zcrop (gint32 drawable_id,
gint32 image_id);
1997-11-25 06:05:25 +08:00
const GimpPlugInInfo PLUG_IN_INFO =
1997-11-25 06:05:25 +08:00
{
NULL, /* init_proc */
NULL, /* quit_proc */
1997-11-25 06:05:25 +08:00
query, /* query_proc */
run, /* run_proc */
1997-11-25 06:05:25 +08:00
};
MAIN ()
1997-11-25 06:05:25 +08:00
static void
query (void)
1997-11-25 06:05:25 +08:00
{
static const GimpParamDef args[] =
1997-11-25 06:05:25 +08:00
{
{ GIMP_PDB_INT32, "run-mode", "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
{ GIMP_PDB_IMAGE, "image", "Input image" },
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" }
1997-11-25 06:05:25 +08:00
};
gimp_install_procedure (PLUG_IN_PROC,
N_("Autocrop unused space from edges and middle"),
"",
"Adam D. Moss",
"Adam D. Moss",
"1997",
N_("_Zealous Crop"),
"RGB*, GRAY*, INDEXED*",
GIMP_PLUGIN,
G_N_ELEMENTS (args), 0,
args, NULL);
gimp_plugin_menu_register (PLUG_IN_PROC, "<Image>/Image/Crop");
1997-11-25 06:05:25 +08:00
}
static void
run (const gchar *name,
gint n_params,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals)
1997-11-25 06:05:25 +08:00
{
static GimpParam values[1];
GimpRunMode run_mode;
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
2014-12-11 17:00:00 +08:00
gint32 drawable_id;
gint32 image_id;
1997-11-25 06:05:25 +08:00
2014-12-11 17:00:00 +08:00
INIT_I18N ();
gegl_init (NULL, NULL);
1997-11-25 06:05:25 +08:00
*nreturn_vals = 1;
*return_vals = values;
1997-11-25 06:05:25 +08:00
run_mode = param[0].data.d_int32;
if (run_mode == GIMP_RUN_NONINTERACTIVE)
{
if (n_params != 3)
{
status = GIMP_PDB_CALLING_ERROR;
}
1997-11-25 06:05:25 +08:00
}
if (status == GIMP_PDB_SUCCESS)
{
/* Get the specified drawable */
2014-12-11 17:00:00 +08:00
image_id = param[1].data.d_int32;
drawable_id = param[2].data.d_int32;
/* Make sure that the drawable is gray or RGB or indexed */
2014-12-11 17:00:00 +08:00
if (gimp_drawable_is_rgb (drawable_id) ||
gimp_drawable_is_gray (drawable_id) ||
gimp_drawable_is_indexed (drawable_id))
{
gimp_progress_init (_("Zealous cropping"));
1997-11-25 06:05:25 +08:00
2014-12-11 17:00:00 +08:00
do_zcrop (drawable_id, image_id);
1997-11-25 06:05:25 +08:00
if (run_mode != GIMP_RUN_NONINTERACTIVE)
gimp_displays_flush ();
}
else
{
status = GIMP_PDB_EXECUTION_ERROR;
}
1997-11-25 06:05:25 +08:00
}
values[0].type = GIMP_PDB_STATUS;
1997-11-25 06:05:25 +08:00
values[0].data.d_status = status;
2014-12-11 17:00:00 +08:00
gegl_exit ();
1997-11-25 06:05:25 +08:00
}
static inline gboolean
2014-12-11 17:00:00 +08:00
colors_equal (const gfloat *col1,
const gfloat *col2,
gint components,
gboolean has_alpha)
{
if (has_alpha &&
2014-12-11 17:00:00 +08:00
FLOAT_IS_ZERO (col1[components - 1]) &&
FLOAT_IS_ZERO (col2[components - 1]))
{
return TRUE;
}
else
{
gint b;
2014-12-11 17:00:00 +08:00
for (b = 0; b < components; b++)
{
2014-12-11 17:00:00 +08:00
if (! FLOAT_EQUAL (col1[b], col2[b]))
return FALSE;
}
return TRUE;
}
}
static void
2014-12-11 17:00:00 +08:00
do_zcrop (gint32 drawable_id,
gint32 image_id)
1997-11-25 06:05:25 +08:00
{
2014-12-11 17:00:00 +08:00
GeglBuffer *drawable_buffer;
GeglBuffer *shadow_buffer;
gfloat *linear_buf;
const Babl *format;
gint x, y, width, height;
gint components;
gint8 *killrows;
gint8 *killcols;
gint32 livingrows, livingcols, destrow, destcol;
gint32 selection_copy_id;
gboolean has_alpha;
drawable_buffer = gimp_drawable_get_buffer (drawable_id);
shadow_buffer = gimp_drawable_get_shadow_buffer (drawable_id);
width = gegl_buffer_get_width (drawable_buffer);
height = gegl_buffer_get_height (drawable_buffer);
has_alpha = gimp_drawable_has_alpha (drawable_id);
if (has_alpha)
format = babl_format ("R'G'B'A float");
else
format = babl_format ("R'G'B' float");
components = babl_format_get_n_components (format);
1997-11-25 06:05:25 +08:00
killrows = g_new (gint8, height);
killcols = g_new (gint8, width);
1997-11-25 06:05:25 +08:00
2014-12-11 17:00:00 +08:00
linear_buf = g_new (gfloat, (width > height ? width : height) * components);
1997-11-25 06:05:25 +08:00
2014-12-11 17:00:00 +08:00
/* search which rows to remove */
1997-11-25 06:05:25 +08:00
livingrows = 0;
for (y = 0; y < height; y++)
1997-11-25 06:05:25 +08:00
{
2014-12-11 17:00:00 +08:00
gegl_buffer_get (drawable_buffer, GEGL_RECTANGLE (0, y, width, 1),
1.0, format, linear_buf,
GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
1997-11-25 06:05:25 +08:00
killrows[y] = TRUE;
2014-12-11 17:00:00 +08:00
for (x = components; x < width * components; x += components)
{
2014-12-11 17:00:00 +08:00
if (! colors_equal (linear_buf, &linear_buf[x], components, has_alpha))
{
livingrows++;
killrows[y] = FALSE;
break;
}
}
1997-11-25 06:05:25 +08:00
}
2014-12-11 17:00:00 +08:00
gimp_progress_update (0.25);
/* search which columns to remove */
1997-11-25 06:05:25 +08:00
livingcols = 0;
for (x = 0; x < width; x++)
1997-11-25 06:05:25 +08:00
{
2014-12-11 17:00:00 +08:00
gegl_buffer_get (drawable_buffer, GEGL_RECTANGLE (x, 0, 1, height),
1.0, format, linear_buf,
GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
1997-11-25 06:05:25 +08:00
killcols[x] = TRUE;
2014-12-11 17:00:00 +08:00
for (y = components; y < height * components; y += components)
{
2014-12-11 17:00:00 +08:00
if (! colors_equal (linear_buf, &linear_buf[y], components, has_alpha))
{
livingcols++;
killcols[x] = FALSE;
break;
}
}
1997-11-25 06:05:25 +08:00
}
2014-12-11 17:00:00 +08:00
gimp_progress_update (0.5);
1997-11-25 06:05:25 +08:00
2014-12-11 17:00:00 +08:00
if ((livingcols == 0 || livingrows == 0) ||
(livingcols == width && livingrows == height))
1997-11-25 06:05:25 +08:00
{
Cleaned up and improved the message system: 2003-06-13 Michael Natterer <mitch@gimp.org> Cleaned up and improved the message system: * app/core/gimp.[ch]: added "const gchar *domain" to GimpMessageFunc (a NULL domain means the message is from the GIMP core, everything else is a plug-in). * app/errors.c: pass "domain == NULL" to gimp_message(). * tools/pdbgen/pdb/message.pdb: derive the message domain from the current plug-in's menu_path (evil hack but works reasonably well). * app/pdb/message_cmds.c: regenerated. * app/widgets/gimpwidgets-utils.[ch] (gimp_message_box): added a header showing the message domain and changed the dialog layout to follow the HIG more closely. * app/gui/error-console-dialog.[ch]: removed. * app/widgets/gimperrorconsole.[ch] * app/gui/error-console-commands.[ch] * app/gui/error-console-menu.[ch]: new files containing a re-implementation of the error console dialog. * app/gui/Makefile.am * app/gui/dialogs-constructors.c * app/gui/gui.c * app/gui/menus.c * app/widgets/Makefile.am * app/widgets/widgets-types.h: changed accordingly. * app/display/gimpprogress.c: added more spacing and removed the separator (more HIG compliant). * plug-ins/[most plug-ins].c: Changed lots of messages and progress strings: - Removed plug-in names from messages since that's automatically covered by "domain" now. - Put all filenames in ''. - Changed "Loading" to "Opening". - Added "..." to all progress messages. - Cleaned up all file open/save error messages to look the same and include g_strerror(errno). - Removed special casing for progress bars and *always* show them, not only if run_mode != GIMP_RUN_NONINTERACTIVE (we can't expect all plug-ins to do this correctly but need to hack the core to sort out unwanted progress bars). Unrelated: - Cleaned up indentation, spacing, #includes, coding style and other stuff while I was at all these files.
2003-06-13 22:37:00 +08:00
g_message (_("Nothing to crop."));
g_object_unref (shadow_buffer);
g_object_unref (drawable_buffer);
2014-12-11 17:00:00 +08:00
g_free (linear_buf);
2010-11-10 02:03:47 +08:00
g_free (killrows);
g_free (killcols);
1997-11-25 06:05:25 +08:00
return;
}
2014-12-11 17:00:00 +08:00
/* restitute living rows */
1997-11-25 06:05:25 +08:00
2014-12-11 17:00:00 +08:00
destrow = 0;
for (y = 0; y < height; y++)
1997-11-25 06:05:25 +08:00
{
if (!killrows[y])
{
2014-12-11 17:00:00 +08:00
gegl_buffer_copy (drawable_buffer,
GEGL_RECTANGLE (0, y, width, 1),
GEGL_ABYSS_NONE,
2014-12-11 17:00:00 +08:00
shadow_buffer,
GEGL_RECTANGLE (0, destrow, width, 1));
destrow++;
}
1997-11-25 06:05:25 +08:00
}
2014-12-11 17:00:00 +08:00
gimp_progress_update (0.75);
1997-11-25 06:05:25 +08:00
2014-12-11 17:00:00 +08:00
/* restitute living columns */
1997-11-25 06:05:25 +08:00
2014-12-11 17:00:00 +08:00
destcol = 0;
for (x = 0; x < width; x++)
1997-11-25 06:05:25 +08:00
{
if (!killcols[x])
{
2014-12-11 17:00:00 +08:00
gegl_buffer_copy (shadow_buffer,
GEGL_RECTANGLE (x, 0, 1, height),
GEGL_ABYSS_NONE,
2014-12-11 17:00:00 +08:00
shadow_buffer,
GEGL_RECTANGLE (destcol, 0, 1, height));
destcol++;
}
1997-11-25 06:05:25 +08:00
}
2014-12-11 17:00:00 +08:00
gimp_progress_update (1.00);
gimp_image_undo_group_start (image_id);
2014-12-11 17:00:00 +08:00
selection_copy_id = gimp_selection_save (image_id);
gimp_selection_none (image_id);
2014-12-11 17:00:00 +08:00
gegl_buffer_flush (shadow_buffer);
gimp_drawable_merge_shadow (drawable_id, TRUE);
gegl_buffer_flush (drawable_buffer);
2014-12-11 17:00:00 +08:00
gimp_image_select_item (image_id, GIMP_CHANNEL_OP_REPLACE, selection_copy_id);
gimp_image_remove_channel (image_id, selection_copy_id);
gimp_image_crop (image_id, livingcols, livingrows, 0, 0);
gimp_image_undo_group_end (image_id);
2014-12-11 17:00:00 +08:00
g_object_unref (shadow_buffer);
g_object_unref (drawable_buffer);
g_free (linear_buf);
g_free (killrows);
g_free (killcols);
1997-11-25 06:05:25 +08:00
}