gimp/plug-ins/common/zealouscrop.c
Manish Singh 5d01581069 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 02:50:33 +00:00

287 lines
7 KiB
C

/*
* 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"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <libgimp/gimp.h>
#include "libgimp/stdplugins-intl.h"
/* Declare local functions. */
static void query (void);
static void run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
static inline gint colours_equal (const guchar *col1,
const guchar *col2,
gint bytes);
static void do_zcrop (GimpDrawable *drawable,
gint32 image_id);
GimpPlugInInfo PLUG_IN_INFO =
{
NULL, /* init_proc */
NULL, /* quit_proc */
query, /* query_proc */
run, /* run_proc */
};
static gint bytes;
MAIN ()
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)
{
static GimpParamDef args[] =
{
{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
{ GIMP_PDB_IMAGE, "image", "Input image" },
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" }
};
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_("_Zealous Crop"),
"RGB*, GRAY*, INDEXED*",
GIMP_PLUGIN,
G_N_ELEMENTS (args), 0,
args, NULL);
gimp_plugin_menu_register ("plug_in_zealouscrop", "<Image>/Image/Crop");
}
static void
run (const gchar *name,
gint n_params,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals)
{
static GimpParam values[1];
GimpDrawable *drawable;
GimpRunMode run_mode;
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
gint32 image_id;
INIT_I18N();
*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;
}
}
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)..."));
gimp_tile_cache_ntiles (1 +
2 * (drawable->width > drawable->height ?
(drawable->width / gimp_tile_width()) :
(drawable->height / gimp_tile_height())));
do_zcrop(drawable, image_id);
if (run_mode != GIMP_RUN_NONINTERACTIVE)
gimp_displays_flush ();
gimp_drawable_detach (drawable);
}
else
{
status = GIMP_PDB_EXECUTION_ERROR;
}
}
values[0].type = GIMP_PDB_STATUS;
values[0].data.d_status = status;
}
static void
do_zcrop (GimpDrawable *drawable,
gint32 image_id)
{
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;
height = drawable->height;
bytes = drawable->bpp;
total_area = width * height * 4;
area = 0;
killrows = g_new (gint8, height);
killcols = g_new (gint8, width);
buffer = g_malloc ((width > height ? width : height) * bytes);
/* 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);
livingrows = 0;
for (y=0; y<height; y++)
{
gimp_pixel_rgn_get_row (&srcPR, buffer, 0, y, width);
killrows[y] = TRUE;
for (x=0; x<width*bytes; x+=bytes)
{
if (!colours_equal (buffer, &buffer[x], bytes))
{
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);
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)))
{
g_message (_("Nothing to crop."));
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);
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);
destcol++;
}
area += height;
if (x % 20)
gimp_progress_update ((double) area / (double) total_area);
}
g_free (buffer);
g_free (killrows);
g_free (killcols);
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);
}