1997-11-25 06:05:25 +08:00
|
|
|
/*
|
|
|
|
* ZealousCrop plug-in version 1.00
|
|
|
|
* by Adam D. Moss <adam@foxbox.org>
|
2005-06-01 03:10:46 +08:00
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
2005-06-01 03:10:46 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2005-06-01 03:10:46 +08: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
|
2009-01-18 06:28:01 +08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
1997-11-25 06:05:25 +08:00
|
|
|
*/
|
|
|
|
|
2000-05-02 04:22:55 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <libgimp/gimp.h>
|
|
|
|
|
1999-09-09 15:09:33 +08:00
|
|
|
#include "libgimp/stdplugins-intl.h"
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2005-08-16 06:42:34 +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)
|
2005-08-16 06:42:34 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* Declare local functions. */
|
2001-12-04 01:59:48 +08:00
|
|
|
|
2013-03-27 06:37:16 +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,
|
2013-03-27 06:37:16 +08:00
|
|
|
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
|
|
|
|
2006-05-16 20:26:20 +08:00
|
|
|
const GimpPlugInInfo PLUG_IN_INFO =
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-05-02 04:22:55 +08:00
|
|
|
NULL, /* init_proc */
|
|
|
|
NULL, /* quit_proc */
|
1997-11-25 06:05:25 +08:00
|
|
|
query, /* query_proc */
|
2000-05-02 04:22:55 +08:00
|
|
|
run, /* run_proc */
|
1997-11-25 06:05:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-05-02 04:22:55 +08:00
|
|
|
MAIN ()
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-05-02 04:22:55 +08:00
|
|
|
static void
|
|
|
|
query (void)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2006-05-16 20:26:20 +08:00
|
|
|
static const GimpParamDef args[] =
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2009-01-20 04:11:36 +08:00
|
|
|
{ GIMP_PDB_INT32, "run-mode", "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
|
2009-04-12 00:57:42 +08:00
|
|
|
{ GIMP_PDB_IMAGE, "image", "Input image" },
|
|
|
|
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" }
|
1997-11-25 06:05:25 +08:00
|
|
|
};
|
1999-09-09 15:09:33 +08:00
|
|
|
|
2005-08-16 06:42:34 +08:00
|
|
|
gimp_install_procedure (PLUG_IN_PROC,
|
2006-03-17 17:29:48 +08:00
|
|
|
N_("Autocrop unused space from edges and middle"),
|
2004-06-26 09:38:22 +08:00
|
|
|
"",
|
|
|
|
"Adam D. Moss",
|
|
|
|
"Adam D. Moss",
|
|
|
|
"1997",
|
|
|
|
N_("_Zealous Crop"),
|
|
|
|
"RGB*, GRAY*, INDEXED*",
|
|
|
|
GIMP_PLUGIN,
|
|
|
|
G_N_ELEMENTS (args), 0,
|
|
|
|
args, NULL);
|
2004-05-07 21:15:52 +08:00
|
|
|
|
2005-08-16 06:42:34 +08:00
|
|
|
gimp_plugin_menu_register (PLUG_IN_PROC, "<Image>/Image/Crop");
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2000-05-02 04:22:55 +08:00
|
|
|
static void
|
2003-07-02 21:00:16 +08:00
|
|
|
run (const gchar *name,
|
|
|
|
gint n_params,
|
|
|
|
const GimpParam *param,
|
|
|
|
gint *nreturn_vals,
|
|
|
|
GimpParam **return_vals)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-07-07 22:53:42 +08:00
|
|
|
static GimpParam values[1];
|
2002-01-08 17:20:02 +08:00
|
|
|
GimpRunMode run_mode;
|
2001-07-07 22:53:42 +08:00
|
|
|
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
|
2014-12-11 17:00:00 +08:00
|
|
|
gint32 drawable_id;
|
2001-07-07 22:53:42 +08:00
|
|
|
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);
|
1999-09-09 15:09:33 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
*nreturn_vals = 1;
|
2005-08-16 06:42:34 +08:00
|
|
|
*return_vals = values;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
run_mode = param[0].data.d_int32;
|
|
|
|
|
2000-08-22 09:26:57 +08:00
|
|
|
if (run_mode == GIMP_RUN_NONINTERACTIVE)
|
2000-05-02 04:22:55 +08:00
|
|
|
{
|
|
|
|
if (n_params != 3)
|
2004-06-26 09:38:22 +08:00
|
|
|
{
|
|
|
|
status = GIMP_PDB_CALLING_ERROR;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2000-08-22 09:26:57 +08:00
|
|
|
if (status == GIMP_PDB_SUCCESS)
|
2000-05-02 04:22:55 +08:00
|
|
|
{
|
|
|
|
/* 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;
|
2000-05-02 04:22:55 +08:00
|
|
|
|
|
|
|
/* 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))
|
2004-06-26 09:38:22 +08:00
|
|
|
{
|
2005-09-30 16:16:10 +08:00
|
|
|
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
|
|
|
|
2004-06-26 09:38:22 +08:00
|
|
|
if (run_mode != GIMP_RUN_NONINTERACTIVE)
|
|
|
|
gimp_displays_flush ();
|
|
|
|
}
|
2000-05-02 04:22:55 +08:00
|
|
|
else
|
2004-06-26 09:38:22 +08:00
|
|
|
{
|
|
|
|
status = GIMP_PDB_EXECUTION_ERROR;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2001-07-07 22:53:42 +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
|
|
|
}
|
|
|
|
|
2013-03-27 06:37:16 +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)
|
2013-03-27 06:37:16 +08:00
|
|
|
{
|
|
|
|
if (has_alpha &&
|
2014-12-11 17:00:00 +08:00
|
|
|
FLOAT_IS_ZERO (col1[components - 1]) &&
|
|
|
|
FLOAT_IS_ZERO (col2[components - 1]))
|
2013-03-27 06:37:16 +08:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gint b;
|
|
|
|
|
2014-12-11 17:00:00 +08:00
|
|
|
for (b = 0; b < components; b++)
|
2013-03-27 06:37:16 +08:00
|
|
|
{
|
2014-12-11 17:00:00 +08:00
|
|
|
if (! FLOAT_EQUAL (col1[b], col2[b]))
|
2013-03-27 06:37:16 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-05-02 04:22:55 +08:00
|
|
|
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
|
|
|
|
2001-07-07 22:53:42 +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 */
|
2013-03-27 06:37:16 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
livingrows = 0;
|
2005-06-01 03:10:46 +08:00
|
|
|
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)
|
2004-06-26 09:38:22 +08:00
|
|
|
{
|
2014-12-11 17:00:00 +08:00
|
|
|
if (! colors_equal (linear_buf, &linear_buf[x], components, has_alpha))
|
2004-06-26 09:38:22 +08:00
|
|
|
{
|
|
|
|
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;
|
2005-06-01 03:10:46 +08:00
|
|
|
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)
|
2004-06-26 09:38:22 +08:00
|
|
|
{
|
2014-12-11 17:00:00 +08:00
|
|
|
if (! colors_equal (linear_buf, &linear_buf[y], components, has_alpha))
|
2004-06-26 09:38:22 +08:00
|
|
|
{
|
|
|
|
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) ||
|
2005-06-01 03:10:46 +08:00
|
|
|
(livingcols == width && livingrows == height))
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2003-06-13 22:37:00 +08:00
|
|
|
g_message (_("Nothing to crop."));
|
2017-12-24 07:39:02 +08:00
|
|
|
|
|
|
|
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;
|
2005-06-01 03:10:46 +08:00
|
|
|
for (y = 0; y < height; y++)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
if (!killrows[y])
|
2004-06-26 09:38:22 +08:00
|
|
|
{
|
2014-12-11 17:00:00 +08:00
|
|
|
gegl_buffer_copy (drawable_buffer,
|
|
|
|
GEGL_RECTANGLE (0, y, width, 1),
|
2015-05-25 04:32:55 +08:00
|
|
|
GEGL_ABYSS_NONE,
|
2014-12-11 17:00:00 +08:00
|
|
|
shadow_buffer,
|
|
|
|
GEGL_RECTANGLE (0, destrow, width, 1));
|
|
|
|
|
2004-06-26 09:38:22 +08:00
|
|
|
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;
|
2005-06-01 03:10:46 +08:00
|
|
|
for (x = 0; x < width; x++)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
if (!killcols[x])
|
2004-06-26 09:38:22 +08:00
|
|
|
{
|
2014-12-11 17:00:00 +08:00
|
|
|
gegl_buffer_copy (shadow_buffer,
|
|
|
|
GEGL_RECTANGLE (x, 0, 1, height),
|
2015-05-25 04:32:55 +08:00
|
|
|
GEGL_ABYSS_NONE,
|
2014-12-11 17:00:00 +08:00
|
|
|
shadow_buffer,
|
|
|
|
GEGL_RECTANGLE (destcol, 0, 1, height));
|
|
|
|
|
2004-06-26 09:38:22 +08:00
|
|
|
destcol++;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2014-12-11 17:00:00 +08:00
|
|
|
gimp_progress_update (1.00);
|
2003-12-05 22:18:47 +08:00
|
|
|
|
|
|
|
gimp_image_undo_group_start (image_id);
|
|
|
|
|
2014-12-11 17:00:00 +08:00
|
|
|
selection_copy_id = gimp_selection_save (image_id);
|
2014-09-10 06:09:28 +08:00
|
|
|
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-09-10 06:09:28 +08:00
|
|
|
|
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);
|
2014-09-10 06:09:28 +08:00
|
|
|
|
2003-12-05 22:18:47 +08:00
|
|
|
gimp_image_crop (image_id, livingcols, livingrows, 0, 0);
|
|
|
|
|
|
|
|
gimp_image_undo_group_end (image_id);
|
2014-09-10 06:09:28 +08:00
|
|
|
|
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
|
|
|
}
|