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

292 lines
7.5 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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"
1997-11-25 06:05:25 +08: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-04 01:59:48 +08: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-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
};
static gint bytes;
1997-11-25 06:05:25 +08:00
MAIN ()
1997-11-25 06:05:25 +08:00
Fix a bunch of warnings from Sparse: 2004-11-13 Manish Singh <yosh@gimp.org> Fix a bunch of warnings from Sparse: * app/actions/dockable-commands.c * app/actions/layers-actions.c * app/actions/view-commands.c * app/base/pixel-surround.c * app/config/gimpconfig-utils.c * app/config/gimpscanner.c * app/core/gimpbrushgenerated.c * app/core/gimpcontainer.c * app/core/gimpimage.c * app/dialogs/palette-import-dialog.c * app/file/gimprecentlist.c * app/plug-in/plug-in-params.c * app/text/gimptext-compat.c * app/text/gimptext-parasite.c * app/vectors/gimpbezierstroke.c * app/vectors/gimpstroke.c * app/widgets/gimpcellrendereraccel.c * app/widgets/gimpselectiondata.c * app/xcf/xcf.c * libgimp/gimp.c * libgimpthumb/gimpthumb-utils.c * libgimpthumb/gimpthumbnail.c * modules/cdisplay_proof.c * plug-ins/Lighting/lighting_ui.c * plug-ins/common/csource.c * plug-ins/common/glasstile.c * plug-ins/common/nova.c * plug-ins/common/pcx.c * plug-ins/common/pnm.c * plug-ins/common/randomize.c * plug-ins/common/screenshot.c * plug-ins/common/sel_gauss.c * plug-ins/common/spheredesigner.c * plug-ins/common/wind.c * plug-ins/gfig/gfig-dialog.c * plug-ins/gfig/gfig-dobject.c * plug-ins/gimpressionist/gimpressionist.c * plug-ins/ifscompose/ifscompose.c * plug-ins/print/gimp_main_window.c * plug-ins/print/print.c: Cleanup integer vs. pointer confusion. * app/base/temp-buf.c * app/dialogs/about-dialog.c * plug-ins/common/bumpmap.c * plug-ins/common/jigsaw.c * plug-ins/gfig/gfig-dobject.c: Cosmetic cleanups. * app/config/gimpconfig-deserialize.c * app/config/gimpconfig-path.c * app/config/gimpconfigwriter.c * app/core/gimpgradient.c * app/tools/gimpdrawtool.c * plug-ins/common/nlfilt.c * plug-ins/common/unsharp.c * plug-ins/common/zealouscrop.c: Define inline functions before they are used. * app/core/gimpdrawable-blend.c: PixelRegion definition was changed some time ago, but the initialization here didn't change. Fix it. * app/plug-in/plug-in-rc.c (plug_in_extra_deserialize): No need to assign token twice in a row. * libgimpbase/gimpdatafiles.c (gimp_datafiles_read_directories): No need to initialize file_data, since the code fills out all the fields. * plug-ins/common/CML_explorer.c * plug-ins/common/vpropagate.c: Declare function pointers fully. * plug-ins/common/grid.c (pix_composite): G_INLINE_FUNC isn't needed, we assume we can use the "inline" keyword always. * plug-ins/common/psd_save.c * plug-ins/common/vinvert.c * plug-ins/gfig/gfig-arc.c * plug-ins/gfig/gfig-bezier.c * plug-ins/gfig/gfig-circle.c * plug-ins/gfig/gfig-dialog.c * plug-ins/gfig/gfig-dobject.c * plug-ins/gfig/gfig-ellipse.c * plug-ins/gfig/gfig-line.c * plug-ins/gfig/gfig-poly.c * plug-ins/gfig/gfig-spiral.c * plug-ins/gfig/gfig-star.c * plug-ins/gfig/gfig.c * plug-ins/gimpressionist/orientmap.c * plug-ins/gimpressionist/placement.c * plug-ins/gimpressionist/sizemap.c * plug-ins/imagemap/imap_grid.c * plug-ins/imagemap/imap_main.c * plug-ins/imagemap/imap_preferences.c * plug-ins/imagemap/imap_settings.c * plug-ins/maze/maze.c * plug-ins/sel2path/curve.c * plug-ins/sel2path/fit.c * plug-ins/sel2path/pxl-outline.c * plug-ins/sel2path/spline.c * plug-ins/xjt/xjt.c: Functions with no args should be declared with (void). * plug-ins/common/retinex.c (MSRCR): Initialize max_preview to quiet the compiler.
2004-11-14 10:50:33 +08:00
static inline gint
colours_equal (const guchar *col1,
const guchar *col2,
gint bytes)
{
gint b;
for (b = 0; b < bytes; b++)
{
if (col1[b] != col2[b])
return FALSE;
}
return TRUE;
}
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", "Interactive, non-interactive" },
{ 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];
GimpDrawable *drawable;
GimpRunMode run_mode;
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
gint32 image_id;
1997-11-25 06:05:25 +08:00
INIT_I18N();
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 */
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 (_("Zealous cropping"));
1997-11-25 06:05:25 +08:00
gimp_tile_cache_ntiles (1 +
2 * (drawable->width > drawable->height ?
(drawable->width / gimp_tile_width()) :
(drawable->height / gimp_tile_height())));
1997-11-25 06:05:25 +08:00
do_zcrop(drawable, image_id);
1997-11-25 06:05:25 +08:00
if (run_mode != GIMP_RUN_NONINTERACTIVE)
gimp_displays_flush ();
1997-11-25 06:05:25 +08:00
gimp_drawable_detach (drawable);
}
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;
}
static void
do_zcrop (GimpDrawable *drawable,
gint32 image_id)
1997-11-25 06:05:25 +08: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-25 06:05:25 +08:00
height = drawable->height;
bytes = drawable->bpp;
1997-11-25 06:05:25 +08:00
total_area = width * height * 4;
area = 0;
killrows = g_new (gint8, height);
killcols = g_new (gint8, width);
1997-11-25 06:05:25 +08:00
buffer = g_malloc ((width > height ? width : height) * bytes);
1997-11-25 06:05:25 +08: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-25 06:05:25 +08:00
livingrows = 0;
for (y = 0; y < height; y++)
1997-11-25 06:05:25 +08:00
{
gimp_pixel_rgn_get_row (&srcPR, buffer, 0, y, width);
1997-11-25 06:05:25 +08:00
killrows[y] = TRUE;
for (x = 0; x < width * bytes; x += bytes)
{
if (!colours_equal (buffer, &buffer[x], bytes))
{
livingrows++;
killrows[y] = FALSE;
break;
}
}
1997-11-25 06:05:25 +08:00
area += width;
if (y % 20 == 0)
gimp_progress_update ((double) area / (double) total_area);
1997-11-25 06:05:25 +08:00
}
livingcols = 0;
for (x = 0; x < width; x++)
1997-11-25 06:05:25 +08:00
{
gimp_pixel_rgn_get_col (&srcPR, buffer, x, 0, height);
1997-11-25 06:05:25 +08:00
killcols[x] = TRUE;
for (y = 0; y < height * bytes; y += bytes)
{
if (!colours_equal(buffer, &buffer[y], bytes))
{
livingcols++;
killcols[x] = FALSE;
break;
}
}
1997-11-25 06:05:25 +08:00
area += height;
if (x % 20 == 0)
gimp_progress_update ((double) area / (double) total_area);
1997-11-25 06:05:25 +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."));
1997-11-25 06:05:25 +08:00
return;
}
destrow = 0;
for (y = 0; y < height; y++)
1997-11-25 06:05:25 +08:00
{
if (!killrows[y])
{
gimp_pixel_rgn_get_row (&srcPR, buffer, 0, y, width);
gimp_pixel_rgn_set_row (&destPR, buffer, 0, destrow, width);
destrow++;
}
1997-11-25 06:05:25 +08:00
area += width;
if (y % 20 == 0)
gimp_progress_update ((double) area / (double) total_area);
1997-11-25 06:05:25 +08:00
}
destcol = 0;
gimp_pixel_rgn_init(&srcPR, drawable, 0, 0, width, height, FALSE, TRUE);
for (x = 0; x < width; x++)
1997-11-25 06:05:25 +08:00
{
if (!killcols[x])
{
gimp_pixel_rgn_get_col (&srcPR, buffer, x, 0, height);
gimp_pixel_rgn_set_col (&destPR, buffer, destcol, 0, height);
destcol++;
}
1997-11-25 06:05:25 +08:00
area += height;
if (x % 20 == 0)
gimp_progress_update ((double) area / (double) total_area);
1997-11-25 06:05:25 +08:00
}
g_free (buffer);
1997-11-25 06:05:25 +08:00
g_free (killrows);
g_free (killcols);
1997-11-25 06:05:25 +08: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-25 06:05:25 +08:00
}