gimp/plug-ins/common/zealouscrop.c

286 lines
6.1 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>
* loosely based on Autocrop by Tim Newsome <drz@froody.bloke.com>
*/
/*
* BUGS:
* Doesn't undo properly.
* Progress bar doesn't do anything yet.
*/
#include "config.h"
1997-11-24 22:05:25 +00:00
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <libgimp/gimp.h>
#include "libgimp/stdplugins-intl.h"
1997-11-24 22:05:25 +00:00
1997-11-24 22:05:25 +00:00
/* Declare local functions. */
static void query (void);
static void run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
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-03 17:59:48 +00:00
static inline gint colours_equal (const guchar *col1,
const guchar *col2,
gint bytes);
static void do_zcrop (GimpDrawable *drawable,
gint32 image_id);
1997-11-24 22:05:25 +00:00
GimpPlugInInfo PLUG_IN_INFO =
1997-11-24 22:05:25 +00:00
{
NULL, /* init_proc */
NULL, /* quit_proc */
1997-11-24 22:05:25 +00:00
query, /* query_proc */
run, /* run_proc */
1997-11-24 22:05:25 +00:00
};
static gint bytes;
1997-11-24 22:05:25 +00:00
MAIN ()
1997-11-24 22:05:25 +00:00
static void
query (void)
1997-11-24 22:05:25 +00:00
{
static GimpParamDef args[] =
1997-11-24 22:05:25 +00:00
{
{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
{ GIMP_PDB_IMAGE, "image", "Input image" },
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" }
1997-11-24 22:05:25 +00:00
};
gimp_install_procedure ("plug_in_zealouscrop",
"Automagically crops unused space from the edges "
"and middle of a picture.",
"",
"Adam D. Moss",
"Adam D. Moss",
"1997",
N_("<Image>/Layer/Transform/_Zealous Crop"),
"RGB*, GRAY*, INDEXED*",
GIMP_PLUGIN,
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-03 17:59:48 +00:00
G_N_ELEMENTS (args), 0,
args, NULL);
1997-11-24 22:05:25 +00:00
}
static void
run (const gchar *name,
gint n_params,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals)
1997-11-24 22:05:25 +00:00
{
static GimpParam values[1];
GimpDrawable *drawable;
GimpRunMode run_mode;
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
gint32 image_id;
1997-11-24 22:05:25 +00:00
INIT_I18N();
1997-11-24 22:05:25 +00:00
*nreturn_vals = 1;
*return_vals = values;
run_mode = param[0].data.d_int32;
if (run_mode == GIMP_RUN_NONINTERACTIVE)
{
if (n_params != 3)
{
status = GIMP_PDB_CALLING_ERROR;
}
1997-11-24 22:05:25 +00:00
}
if (status == GIMP_PDB_SUCCESS)
{
/* Get the specified drawable */
drawable = gimp_drawable_get(param[2].data.d_drawable);
image_id = param[1].data.d_image;
/* Make sure that the drawable is gray or RGB or indexed */
if (gimp_drawable_is_rgb (drawable->drawable_id) ||
gimp_drawable_is_gray (drawable->drawable_id) ||
gimp_drawable_is_indexed (drawable->drawable_id))
{
gimp_progress_init (_("ZealousCropping(tm)..."));
1997-11-24 22:05:25 +00:00
gimp_tile_cache_ntiles (1 +
2 * (drawable->width > drawable->height ?
(drawable->width / gimp_tile_width()) :
(drawable->height / gimp_tile_height())));
1997-11-24 22:05:25 +00:00
do_zcrop(drawable, image_id);
1997-11-24 22:05:25 +00:00
if (run_mode != GIMP_RUN_NONINTERACTIVE)
gimp_displays_flush ();
1997-11-24 22:05:25 +00:00
gimp_drawable_detach (drawable);
}
else
{
status = GIMP_PDB_EXECUTION_ERROR;
}
1997-11-24 22:05:25 +00:00
}
values[0].type = GIMP_PDB_STATUS;
1997-11-24 22:05:25 +00:00
values[0].data.d_status = status;
}
static void
do_zcrop (GimpDrawable *drawable,
gint32 image_id)
1997-11-24 22:05:25 +00:00
{
GimpPixelRgn srcPR, destPR;
gint width, height, x, y;
guchar *buffer;
gint8 *killrows;
gint8 *killcols;
gint32 livingrows, livingcols, destrow, destcol;
gint total_area, area;
width = drawable->width;
1997-11-24 22:05:25 +00:00
height = drawable->height;
bytes = drawable->bpp;
1997-11-24 22:05:25 +00:00
total_area = width * height * 4;
area = 0;
killrows = g_new (gint8, height);
killcols = g_new (gint8, width);
1997-11-24 22:05:25 +00:00
buffer = g_malloc ((width > height ? width : height) * bytes);
1997-11-24 22:05:25 +00:00
/* initialize the pixel regions */
gimp_pixel_rgn_init (&srcPR, drawable, 0, 0, width, height, FALSE, FALSE);
gimp_pixel_rgn_init (&destPR, drawable, 0, 0, width, height, TRUE, TRUE);
1997-11-24 22:05:25 +00:00
livingrows = 0;
for (y=0; y<height; y++)
{
gimp_pixel_rgn_get_row (&srcPR, buffer, 0, y, width);
1997-11-24 22:05:25 +00:00
killrows[y] = TRUE;
for (x=0; x<width*bytes; x+=bytes)
{
if (!colours_equal (buffer, &buffer[x], bytes))
1997-11-24 22:05:25 +00:00
{
livingrows++;
killrows[y] = FALSE;
break;
}
}
area += width;
if (y % 20)
gimp_progress_update ((double) area / (double) total_area);
}
livingcols = 0;
for (x=0; x<width; x++)
{
gimp_pixel_rgn_get_col (&srcPR, buffer, x, 0, height);
1997-11-24 22:05:25 +00:00
killcols[x] = TRUE;
for (y=0; y<height*bytes; y+=bytes)
{
if (!colours_equal(buffer, &buffer[y], bytes))
{
livingcols++;
killcols[x] = FALSE;
break;
}
}
area += height;
if (x % 20)
gimp_progress_update ((double) area / (double) total_area);
}
if (((livingcols==0) || (livingrows==0)) ||
((livingcols==width) && (livingrows==height)))
{
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."));
1997-11-24 22:05:25 +00:00
return;
}
destrow = 0;
for (y=0; y<height; y++)
{
if (!killrows[y])
{
gimp_pixel_rgn_get_row (&srcPR, buffer, 0, y, width);
gimp_pixel_rgn_set_row (&destPR, buffer, 0, destrow, width);
1997-11-24 22:05:25 +00:00
destrow++;
}
area += width;
if (y % 20)
gimp_progress_update ((double) area / (double) total_area);
}
destcol = 0;
gimp_pixel_rgn_init(&srcPR, drawable, 0, 0, width, height, FALSE, TRUE);
for (x=0; x<width; x++)
{
if (!killcols[x])
{
gimp_pixel_rgn_get_col (&srcPR, buffer, x, 0, height);
gimp_pixel_rgn_set_col (&destPR, buffer, destcol, 0, height);
1997-11-24 22:05:25 +00:00
destcol++;
}
area += height;
if (x % 20)
gimp_progress_update ((double) area / (double) total_area);
}
g_free (buffer);
1997-11-24 22:05:25 +00:00
g_free (killrows);
g_free (killcols);
1997-11-24 22:05:25 +00:00
gimp_progress_update (1.00);
gimp_image_undo_group_start (image_id);
gimp_drawable_flush (drawable);
gimp_drawable_merge_shadow (drawable->drawable_id, TRUE);
gimp_image_crop (image_id, livingcols, livingrows, 0, 0);
gimp_image_undo_group_end (image_id);
1997-11-24 22:05:25 +00:00
}
static inline gint
colours_equal (const guchar *col1,
const guchar *col2,
gint bytes)
1997-11-24 22:05:25 +00:00
{
gint b;
1997-11-24 22:05:25 +00:00
for (b = 0; b < bytes; b++)
{
if (col1[b] != col2[b])
return FALSE;
1997-11-24 22:05:25 +00:00
}
return TRUE;
}