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

355 lines
9.8 KiB
C
Raw Normal View History

1997-11-24 22:05:25 +00: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 <https://www.gnu.org/licenses/>.
1997-11-24 22:05:25 +00:00
*/
#include "config.h"
#include <libgimp/gimp.h>
#include "libgimp/stdplugins-intl.h"
1997-11-24 22:05:25 +00:00
#define PLUG_IN_PROC "plug-in-zealouscrop"
2014-12-11 10:00:00 +01: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)
typedef struct _Crop Crop;
typedef struct _CropClass CropClass;
struct _Crop
{
GimpPlugIn parent_instance;
};
1997-11-24 22:05:25 +00:00
struct _CropClass
1997-11-24 22:05:25 +00:00
{
GimpPlugInClass parent_class;
1997-11-24 22:05:25 +00:00
};
#define CROP_TYPE (crop_get_type ())
#define CROP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CROP_TYPE, Crop))
GType crop_get_type (void) G_GNUC_CONST;
static GList * crop_query_procedures (GimpPlugIn *plug_in);
static GimpProcedure * crop_create_procedure (GimpPlugIn *plug_in,
const gchar *name);
static GimpValueArray * crop_run (GimpProcedure *procedure,
GimpRunMode run_mode,
GimpImage *image,
GimpDrawable **drawables,
GimpProcedureConfig *config,
gpointer run_data);
static inline gboolean colors_equal (const gfloat *col1,
const gfloat *col2,
gint components,
gboolean has_alpha);
static void do_zcrop (GimpDrawable *drawable,
GimpImage *image);
G_DEFINE_TYPE (Crop, crop, GIMP_TYPE_PLUG_IN)
GIMP_MAIN (CROP_TYPE)
DEFINE_STD_SET_I18N
1997-11-24 22:05:25 +00:00
static void
crop_class_init (CropClass *klass)
1997-11-24 22:05:25 +00:00
{
GimpPlugInClass *plug_in_class = GIMP_PLUG_IN_CLASS (klass);
plug_in_class->query_procedures = crop_query_procedures;
plug_in_class->create_procedure = crop_create_procedure;
plug_in_class->set_i18n = STD_SET_I18N;
1997-11-24 22:05:25 +00:00
}
static void
crop_init (Crop *crop)
1997-11-24 22:05:25 +00:00
{
}
static GList *
crop_query_procedures (GimpPlugIn *plug_in)
{
return g_list_append (NULL, g_strdup (PLUG_IN_PROC));
}
1997-11-24 22:05:25 +00:00
static GimpProcedure *
crop_create_procedure (GimpPlugIn *plug_in,
const gchar *name)
{
GimpProcedure *procedure = NULL;
1997-11-24 22:05:25 +00:00
if (! strcmp (name, PLUG_IN_PROC))
{
procedure = gimp_image_procedure_new (plug_in, name,
GIMP_PDB_PROC_TYPE_PLUGIN,
crop_run, NULL, NULL);
gimp_procedure_set_image_types (procedure, "*");
gimp_procedure_set_sensitivity_mask (procedure,
GIMP_PROCEDURE_SENSITIVE_DRAWABLE);
gimp_procedure_set_menu_label (procedure, _("_Zealous Crop"));
gimp_procedure_add_menu_path (procedure, "<Image>/Image/[Crop]");
gimp_procedure_set_documentation (procedure,
_("Autocrop unused space from "
"edges and middle"),
NULL,
name);
gimp_procedure_set_attribution (procedure,
"Adam D. Moss",
"Adam D. Moss",
"1997");
1997-11-24 22:05:25 +00:00
}
return procedure;
}
1997-11-24 22:05:25 +00:00
static GimpValueArray *
crop_run (GimpProcedure *procedure,
GimpRunMode run_mode,
GimpImage *image,
GimpDrawable **drawables,
GimpProcedureConfig *config,
gpointer run_data)
{
GimpDrawable *drawable;
gegl_init (NULL, NULL);
1997-11-24 22:05:25 +00:00
gimp_progress_init (_("Zealous cropping"));
if (gimp_core_object_array_get_length ((GObject **) drawables) != 1)
{
GError *error = NULL;
g_set_error (&error, GIMP_PLUG_IN_ERROR, 0,
_("Procedure '%s' only works with one drawable."),
PLUG_IN_PROC);
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_CALLING_ERROR,
error);
}
else
{
drawable = drawables[0];
}
do_zcrop (drawable, image);
1997-11-24 22:05:25 +00:00
if (run_mode != GIMP_RUN_NONINTERACTIVE)
gimp_displays_flush ();
2014-12-11 10:00:00 +01:00
return gimp_procedure_new_return_values (procedure, GIMP_PDB_SUCCESS, NULL);
1997-11-24 22:05:25 +00:00
}
static inline gboolean
2014-12-11 10:00:00 +01:00
colors_equal (const gfloat *col1,
const gfloat *col2,
gint components,
gboolean has_alpha)
{
if (has_alpha &&
2014-12-11 10:00:00 +01:00
FLOAT_IS_ZERO (col1[components - 1]) &&
FLOAT_IS_ZERO (col2[components - 1]))
{
return TRUE;
}
else
{
gint b;
2014-12-11 10:00:00 +01:00
for (b = 0; b < components; b++)
{
2014-12-11 10:00:00 +01:00
if (! FLOAT_EQUAL (col1[b], col2[b]))
return FALSE;
}
return TRUE;
}
}
static void
do_zcrop (GimpDrawable *drawable,
GimpImage *image)
1997-11-24 22:05:25 +00: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;
GimpChannel *selection_copy;
gboolean has_alpha;
drawable_buffer = gimp_drawable_get_buffer (drawable);
shadow_buffer = gimp_drawable_get_shadow_buffer (drawable);
2014-12-11 10:00:00 +01:00
width = gegl_buffer_get_width (drawable_buffer);
height = gegl_buffer_get_height (drawable_buffer);
has_alpha = gimp_drawable_has_alpha (drawable);
2014-12-11 10:00:00 +01:00
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-24 22:05:25 +00:00
killrows = g_new (gint8, height);
killcols = g_new (gint8, width);
1997-11-24 22:05:25 +00:00
2014-12-11 10:00:00 +01:00
linear_buf = g_new (gfloat, (width > height ? width : height) * components);
1997-11-24 22:05:25 +00:00
2014-12-11 10:00:00 +01:00
/* search which rows to remove */
1997-11-24 22:05:25 +00:00
livingrows = 0;
for (y = 0; y < height; y++)
1997-11-24 22:05:25 +00:00
{
2014-12-11 10:00:00 +01: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-24 22:05:25 +00:00
killrows[y] = TRUE;
2014-12-11 10:00:00 +01:00
for (x = components; x < width * components; x += components)
{
2014-12-11 10:00:00 +01:00
if (! colors_equal (linear_buf, &linear_buf[x], components, has_alpha))
{
livingrows++;
killrows[y] = FALSE;
break;
}
}
1997-11-24 22:05:25 +00:00
}
2014-12-11 10:00:00 +01:00
gimp_progress_update (0.25);
/* search which columns to remove */
1997-11-24 22:05:25 +00:00
livingcols = 0;
for (x = 0; x < width; x++)
1997-11-24 22:05:25 +00:00
{
2014-12-11 10:00:00 +01: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-24 22:05:25 +00:00
killcols[x] = TRUE;
2014-12-11 10:00:00 +01:00
for (y = components; y < height * components; y += components)
{
2014-12-11 10:00:00 +01:00
if (! colors_equal (linear_buf, &linear_buf[y], components, has_alpha))
{
livingcols++;
killcols[x] = FALSE;
break;
}
}
1997-11-24 22:05:25 +00:00
}
2014-12-11 10:00:00 +01:00
gimp_progress_update (0.5);
1997-11-24 22:05:25 +00:00
2014-12-11 10:00:00 +01:00
if ((livingcols == 0 || livingrows == 0) ||
(livingcols == width && livingrows == height))
1997-11-24 22:05:25 +00: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 14:37:00 +00:00
g_message (_("Nothing to crop."));
g_object_unref (shadow_buffer);
g_object_unref (drawable_buffer);
2014-12-11 10:00:00 +01:00
g_free (linear_buf);
2010-11-09 16:03:47 -02:00
g_free (killrows);
g_free (killcols);
1997-11-24 22:05:25 +00:00
return;
}
2014-12-11 10:00:00 +01:00
/* restitute living rows */
1997-11-24 22:05:25 +00:00
2014-12-11 10:00:00 +01:00
destrow = 0;
for (y = 0; y < height; y++)
1997-11-24 22:05:25 +00:00
{
if (!killrows[y])
{
2014-12-11 10:00:00 +01:00
gegl_buffer_copy (drawable_buffer,
GEGL_RECTANGLE (0, y, width, 1),
GEGL_ABYSS_NONE,
2014-12-11 10:00:00 +01:00
shadow_buffer,
GEGL_RECTANGLE (0, destrow, width, 1));
destrow++;
}
1997-11-24 22:05:25 +00:00
}
2014-12-11 10:00:00 +01:00
gimp_progress_update (0.75);
1997-11-24 22:05:25 +00:00
2014-12-11 10:00:00 +01:00
/* restitute living columns */
1997-11-24 22:05:25 +00:00
2014-12-11 10:00:00 +01:00
destcol = 0;
for (x = 0; x < width; x++)
1997-11-24 22:05:25 +00:00
{
if (!killcols[x])
{
2014-12-11 10:00:00 +01:00
gegl_buffer_copy (shadow_buffer,
GEGL_RECTANGLE (x, 0, 1, height),
GEGL_ABYSS_NONE,
2014-12-11 10:00:00 +01:00
shadow_buffer,
GEGL_RECTANGLE (destcol, 0, 1, height));
destcol++;
}
1997-11-24 22:05:25 +00:00
}
2014-12-11 10:00:00 +01:00
gimp_progress_update (1.00);
gimp_image_undo_group_start (image);
selection_copy = GIMP_CHANNEL (gimp_selection_save (image));
gimp_selection_none (image);
2014-12-11 10:00:00 +01:00
gegl_buffer_flush (shadow_buffer);
gimp_drawable_merge_shadow (drawable, TRUE);
2014-12-11 10:00:00 +01:00
gegl_buffer_flush (drawable_buffer);
gimp_image_select_item (image, GIMP_CHANNEL_OP_REPLACE,
GIMP_ITEM (selection_copy));
gimp_image_remove_channel (image, selection_copy);
gimp_image_crop (image, livingcols, livingrows, 0, 0);
gimp_image_undo_group_end (image);
2014-12-11 10:00:00 +01:00
g_object_unref (shadow_buffer);
g_object_unref (drawable_buffer);
g_free (linear_buf);
g_free (killrows);
g_free (killcols);
1997-11-24 22:05:25 +00:00
}