1997-11-25 06:05:25 +08:00
|
|
|
/*
|
|
|
|
* Autocrop plug-in version 1.00
|
|
|
|
* by Tim Newsome <drz@froody.bloke.com>
|
|
|
|
* thanks to quartic for finding a nasty bug for me
|
|
|
|
*/
|
|
|
|
|
1999-04-09 07:54:14 +08:00
|
|
|
/* 1999/04/09 -- Sven Neumann <sven@gimp.org>
|
|
|
|
* Fixed bad crash that occured when running on an entirely blank image.
|
|
|
|
* Cleaned up the code a bit, while I was at it.
|
|
|
|
*/
|
|
|
|
|
2000-05-01 05:03:44 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
1999-05-29 09:28:24 +08:00
|
|
|
#include <string.h>
|
1997-11-25 06:05:25 +08:00
|
|
|
#include <time.h>
|
2000-05-01 05:03:44 +08:00
|
|
|
|
|
|
|
#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
|
|
|
|
2000-05-01 05:03:44 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* Declare local functions. */
|
2001-07-07 22:53:42 +08:00
|
|
|
static void query (void);
|
2003-07-02 02:54:28 +08:00
|
|
|
static void run (const gchar *name,
|
2003-12-24 06:07:06 +08:00
|
|
|
gint nparams,
|
|
|
|
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 gint colors_equal (guchar *col1,
|
2003-12-24 06:07:06 +08:00
|
|
|
guchar *col2,
|
|
|
|
gint bytes);
|
2000-08-22 09:26:57 +08:00
|
|
|
static gint guess_bgcolor (GimpPixelRgn *pr,
|
2003-12-24 06:07:06 +08:00
|
|
|
gint width,
|
|
|
|
gint height,
|
|
|
|
gint bytes,
|
|
|
|
guchar *color);
|
2000-03-25 05:57:32 +08:00
|
|
|
|
2000-08-22 09:26:57 +08:00
|
|
|
static void doit (GimpDrawable *drawable,
|
2003-12-24 06:07:06 +08:00
|
|
|
gint32 image_id,
|
|
|
|
gboolean show_progress);
|
2001-07-07 22:53:42 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-08-22 09:26:57 +08:00
|
|
|
GimpPlugInInfo PLUG_IN_INFO =
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2003-12-24 06:07:06 +08:00
|
|
|
NULL, /* init_proc */
|
2000-03-25 05:57:32 +08:00
|
|
|
NULL, /* quit_proc */
|
|
|
|
query, /* query_proc */
|
2003-12-24 06:07:06 +08:00
|
|
|
run, /* run_proc */
|
1997-11-25 06:05:25 +08:00
|
|
|
};
|
|
|
|
|
2000-03-25 05:57:32 +08:00
|
|
|
static gint bytes;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-07-07 22:53:42 +08:00
|
|
|
|
2000-05-01 05:03:44 +08:00
|
|
|
MAIN ()
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-03-25 05:57:32 +08:00
|
|
|
static void
|
|
|
|
query (void)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-08-22 09:26:57 +08:00
|
|
|
static GimpParamDef args[] =
|
1999-04-09 07:54:14 +08:00
|
|
|
{
|
2000-08-22 09:26:57 +08:00
|
|
|
{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
|
|
|
|
{ GIMP_PDB_IMAGE, "image", "Input image" },
|
|
|
|
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" }
|
1999-04-09 07:54:14 +08:00
|
|
|
};
|
1999-09-09 15:09:33 +08:00
|
|
|
|
2000-03-25 05:57:32 +08:00
|
|
|
gimp_install_procedure ("plug_in_autocrop",
|
2003-12-24 06:07:06 +08:00
|
|
|
"Automagically crops a picture.",
|
|
|
|
"",
|
|
|
|
"Tim Newsome",
|
|
|
|
"Tim Newsome",
|
|
|
|
"1997",
|
|
|
|
N_("<Image>/Image/Transform/_Autocrop"),
|
|
|
|
"RGB*, GRAY*, INDEXED*",
|
|
|
|
GIMP_PLUGIN,
|
|
|
|
G_N_ELEMENTS (args), 0,
|
|
|
|
args, NULL);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2000-03-25 05:57:32 +08:00
|
|
|
static void
|
2003-07-02 02:54:28 +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];
|
|
|
|
GimpDrawable *drawable;
|
2003-07-02 02:54:28 +08:00
|
|
|
GimpRunMode run_mode;
|
2001-07-07 22:53:42 +08:00
|
|
|
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
|
|
|
|
gint32 image_id;
|
|
|
|
gboolean interactive;
|
2000-12-12 19:53:47 +08:00
|
|
|
|
1999-04-09 07:54:14 +08:00
|
|
|
*nreturn_vals = 1;
|
|
|
|
*return_vals = values;
|
|
|
|
|
|
|
|
run_mode = param[0].data.d_int32;
|
2000-12-12 19:53:47 +08:00
|
|
|
interactive = (run_mode != GIMP_RUN_NONINTERACTIVE);
|
1999-09-09 15:09:33 +08:00
|
|
|
|
|
|
|
INIT_I18N();
|
1999-04-09 07:54:14 +08:00
|
|
|
|
2000-12-12 19:53:47 +08:00
|
|
|
if (n_params != 3)
|
2000-03-25 05:57:32 +08:00
|
|
|
{
|
2000-12-12 19:53:47 +08:00
|
|
|
status = GIMP_PDB_CALLING_ERROR;
|
|
|
|
goto out;
|
2000-03-25 05:57:32 +08:00
|
|
|
}
|
|
|
|
|
2000-12-12 19:53:47 +08:00
|
|
|
/* Get the specified drawable */
|
|
|
|
drawable = gimp_drawable_get (param[2].data.d_drawable);
|
|
|
|
image_id = param[1].data.d_image;
|
2000-03-25 05:57:32 +08:00
|
|
|
|
2000-12-12 19:53:47 +08:00
|
|
|
/* Make sure that the drawable is gray or RGB color */
|
2001-06-15 04:07:38 +08:00
|
|
|
if (gimp_drawable_is_rgb (drawable->drawable_id) ||
|
|
|
|
gimp_drawable_is_gray (drawable->drawable_id) ||
|
|
|
|
gimp_drawable_is_indexed (drawable->drawable_id))
|
2000-12-12 19:53:47 +08:00
|
|
|
{
|
|
|
|
if (interactive)
|
|
|
|
gimp_progress_init (_("Cropping..."));
|
|
|
|
|
|
|
|
gimp_tile_cache_ntiles (2 * (drawable->width / gimp_tile_width() + 1));
|
|
|
|
|
|
|
|
doit (drawable, image_id, interactive);
|
|
|
|
|
|
|
|
if (interactive)
|
|
|
|
gimp_displays_flush ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = GIMP_PDB_EXECUTION_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
values[0].type = GIMP_PDB_STATUS;
|
|
|
|
values[0].data.d_status = status;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2000-03-25 05:57:32 +08:00
|
|
|
static void
|
2000-08-22 09:26:57 +08:00
|
|
|
doit (GimpDrawable *drawable,
|
2000-12-12 19:53:47 +08:00
|
|
|
gint32 image_id,
|
|
|
|
gboolean show_progress)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-07-07 22:53:42 +08:00
|
|
|
GimpPixelRgn srcPR;
|
|
|
|
gint width, height;
|
|
|
|
gint x, y, abort;
|
|
|
|
gint32 nx, ny, nw, nh;
|
|
|
|
guchar *buffer;
|
|
|
|
guchar color[4] = {0, 0, 0, 0};
|
1999-04-09 07:54:14 +08:00
|
|
|
|
2001-07-07 22:53:42 +08:00
|
|
|
width = drawable->width;
|
1999-04-09 07:54:14 +08:00
|
|
|
height = drawable->height;
|
2001-07-07 22:53:42 +08:00
|
|
|
bytes = drawable->bpp;
|
1999-04-09 07:54:14 +08:00
|
|
|
|
|
|
|
nx = 0;
|
|
|
|
ny = 0;
|
|
|
|
nw = width;
|
|
|
|
nh = height;
|
|
|
|
|
|
|
|
/* initialize the pixel regions */
|
2000-03-25 05:57:32 +08:00
|
|
|
gimp_pixel_rgn_init (&srcPR, drawable, 0, 0, width, height, FALSE, FALSE);
|
1999-04-09 07:54:14 +08:00
|
|
|
|
|
|
|
/* First, let's figure out what exactly to crop. */
|
2000-03-25 05:57:32 +08:00
|
|
|
buffer = g_malloc ((width > height ? width : height) * bytes);
|
1999-04-09 07:54:14 +08:00
|
|
|
|
2000-03-25 05:57:32 +08:00
|
|
|
guess_bgcolor (&srcPR, width, height, bytes, color);
|
1999-04-09 07:54:14 +08:00
|
|
|
|
|
|
|
/* Check how many of the top lines are uniform. */
|
|
|
|
abort = 0;
|
2000-03-25 05:57:32 +08:00
|
|
|
for (y = 0; y < height && !abort; y++)
|
|
|
|
{
|
|
|
|
gimp_pixel_rgn_get_row (&srcPR, buffer, 0, y, width);
|
|
|
|
for (x = 0; x < width && !abort; x++)
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
|
|
|
abort = !colors_equal (color, buffer + x * bytes, bytes);
|
|
|
|
}
|
2000-03-25 05:57:32 +08:00
|
|
|
}
|
|
|
|
if (y == height && !abort)
|
|
|
|
{
|
|
|
|
g_free (buffer);
|
|
|
|
gimp_drawable_detach (drawable);
|
|
|
|
return;
|
1999-04-09 07:54:14 +08:00
|
|
|
}
|
|
|
|
y--;
|
|
|
|
ny = y;
|
|
|
|
nh = height - y;
|
2000-12-12 19:53:47 +08:00
|
|
|
|
|
|
|
if (show_progress)
|
|
|
|
gimp_progress_update (0.25);
|
1999-04-09 07:54:14 +08:00
|
|
|
|
|
|
|
/* Check how many of the bottom lines are uniform. */
|
|
|
|
abort = 0;
|
2000-03-25 05:57:32 +08:00
|
|
|
for (y = height - 1; y >= 0 && !abort; y--)
|
|
|
|
{
|
|
|
|
gimp_pixel_rgn_get_row (&srcPR, buffer, 0, y, width);
|
|
|
|
for (x = 0; x < width && !abort; x++)
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
|
|
|
abort = !colors_equal (color, buffer + x * bytes, bytes);
|
|
|
|
}
|
1999-04-09 07:54:14 +08:00
|
|
|
}
|
|
|
|
nh = y - ny + 2;
|
|
|
|
|
2000-12-12 19:53:47 +08:00
|
|
|
if (show_progress)
|
|
|
|
gimp_progress_update (0.5);
|
1999-04-09 07:54:14 +08:00
|
|
|
|
|
|
|
/* Check how many of the left lines are uniform. */
|
|
|
|
abort = 0;
|
2000-03-25 05:57:32 +08:00
|
|
|
for (x = 0; x < width && !abort; x++)
|
|
|
|
{
|
|
|
|
gimp_pixel_rgn_get_col (&srcPR, buffer, x, ny, nh);
|
|
|
|
for (y = 0; y < nh && !abort; y++)
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
|
|
|
abort = !colors_equal (color, buffer + y * bytes, bytes);
|
|
|
|
}
|
1999-04-09 07:54:14 +08:00
|
|
|
}
|
|
|
|
x--;
|
|
|
|
nx = x;
|
|
|
|
nw = width - x;
|
|
|
|
|
2000-12-12 19:53:47 +08:00
|
|
|
if (show_progress)
|
|
|
|
gimp_progress_update (0.75);
|
1999-04-09 07:54:14 +08:00
|
|
|
|
|
|
|
/* Check how many of the right lines are uniform. */
|
|
|
|
abort = 0;
|
2000-03-25 05:57:32 +08:00
|
|
|
for (x = width - 1; x >= 0 && !abort; x--)
|
|
|
|
{
|
|
|
|
gimp_pixel_rgn_get_col (&srcPR, buffer, x, ny, nh);
|
|
|
|
for (y = 0; y < nh && !abort; y++)
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
|
|
|
abort = !colors_equal (color, buffer + y * bytes, bytes);
|
|
|
|
}
|
1999-04-09 07:54:14 +08:00
|
|
|
}
|
|
|
|
nw = x - nx + 2;
|
|
|
|
|
2000-03-25 05:57:32 +08:00
|
|
|
g_free (buffer);
|
1999-04-09 07:54:14 +08:00
|
|
|
|
2000-03-25 05:57:32 +08:00
|
|
|
gimp_drawable_detach (drawable);
|
2000-08-01 08:38:38 +08:00
|
|
|
|
2000-03-25 05:57:32 +08:00
|
|
|
if (nw != width || nh != height)
|
2001-07-07 22:53:42 +08:00
|
|
|
gimp_image_crop (image_id, nw, nh, nx, ny);
|
2000-12-12 19:53:47 +08:00
|
|
|
|
|
|
|
if (show_progress)
|
|
|
|
gimp_progress_update (1.00);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2000-03-25 05:57:32 +08:00
|
|
|
static gint
|
2000-08-22 09:26:57 +08:00
|
|
|
guess_bgcolor (GimpPixelRgn *pr,
|
2003-12-24 06:07:06 +08:00
|
|
|
gint width,
|
|
|
|
gint height,
|
|
|
|
gint bytes,
|
|
|
|
guchar *color)
|
2000-03-25 05:57:32 +08:00
|
|
|
{
|
1999-04-09 07:54:14 +08:00
|
|
|
guchar tl[4], tr[4], bl[4], br[4];
|
|
|
|
|
2000-03-25 05:57:32 +08:00
|
|
|
gimp_pixel_rgn_get_pixel (pr, tl, 0, 0);
|
|
|
|
gimp_pixel_rgn_get_pixel (pr, tr, width - 1, 0);
|
|
|
|
gimp_pixel_rgn_get_pixel (pr, bl, 0, height - 1);
|
|
|
|
gimp_pixel_rgn_get_pixel (pr, br, width - 1, height - 1);
|
1999-04-09 07:54:14 +08:00
|
|
|
|
|
|
|
/* Algorithm pinched from pnmcrop.
|
|
|
|
* To guess the background, first see if 3 corners are equal.
|
|
|
|
* Then if two are equal.
|
|
|
|
* Otherwise average the colors.
|
|
|
|
*/
|
|
|
|
|
2000-03-25 05:57:32 +08:00
|
|
|
if (colors_equal (tr, bl, bytes) && colors_equal (tr, br, bytes))
|
|
|
|
{
|
|
|
|
memcpy (color, tr, bytes);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
else if (colors_equal (tl, bl, bytes) && colors_equal (tl, br, bytes))
|
|
|
|
{
|
|
|
|
memcpy (color, tl, bytes);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
else if (colors_equal (tl, tr, bytes) && colors_equal (tl, br, bytes))
|
|
|
|
{
|
|
|
|
memcpy (color, tl, bytes);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
else if (colors_equal (tl, tr, bytes) && colors_equal (tl, bl, bytes))
|
|
|
|
{
|
|
|
|
memcpy (color, tl, bytes);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
else if (colors_equal (tl, tr, bytes) || colors_equal (tl, bl, bytes) ||
|
2003-12-24 06:07:06 +08:00
|
|
|
colors_equal (tl, br, bytes))
|
2000-03-25 05:57:32 +08:00
|
|
|
{
|
|
|
|
memcpy (color, tl, bytes);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else if (colors_equal (tr, bl, bytes) || colors_equal (tr, bl, bytes))
|
|
|
|
{
|
|
|
|
memcpy (color, tr, bytes);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else if (colors_equal (br, bl, bytes))
|
|
|
|
{
|
|
|
|
memcpy (color, br, bytes);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (bytes--)
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
|
|
|
color[bytes] = (tl[bytes] + tr[bytes] + bl[bytes] + br[bytes]) / 4;
|
|
|
|
}
|
2000-03-25 05:57:32 +08:00
|
|
|
return 0;
|
1999-04-09 07:54:14 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2000-03-25 05:57:32 +08:00
|
|
|
static gint
|
|
|
|
colors_equal (guchar *col1,
|
2003-12-24 06:07:06 +08:00
|
|
|
guchar *col2,
|
|
|
|
gint bytes)
|
2000-03-25 05:57:32 +08:00
|
|
|
{
|
|
|
|
gint equal = 1;
|
|
|
|
gint b;
|
1999-10-07 23:05:49 +08:00
|
|
|
|
2003-12-24 06:07:06 +08:00
|
|
|
if ((bytes == 2 || bytes == 4) && /* HACK! */
|
1999-10-07 23:05:49 +08:00
|
|
|
col1[bytes-1] == 0 &&
|
2000-03-25 05:57:32 +08:00
|
|
|
col2[bytes-1] == 0)
|
|
|
|
{
|
2003-12-24 06:07:06 +08:00
|
|
|
return 1; /* handle zero alpha */
|
2000-03-25 05:57:32 +08:00
|
|
|
}
|
1999-04-09 07:54:14 +08:00
|
|
|
|
2000-03-25 05:57:32 +08:00
|
|
|
for (b = 0; b < bytes; b++)
|
|
|
|
{
|
|
|
|
if (col1[b] != col2[b])
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
|
|
|
equal = 0;
|
|
|
|
break;
|
|
|
|
}
|
1999-04-09 07:54:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return equal;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|