1999-10-04 13:51:40 +08:00
|
|
|
/* The GIMP -- an image manipulation program
|
|
|
|
* Copyright (C) 1995-1999 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2000-12-17 05:37:03 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
2000-12-29 23:22:01 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2001-08-14 22:53:55 +08:00
|
|
|
#include <glib-object.h>
|
2000-12-29 23:22:01 +08:00
|
|
|
|
2002-03-07 04:25:31 +08:00
|
|
|
#include <libart_lgpl/art_gray_svp.h>
|
|
|
|
#include <libart_lgpl/art_misc.h>
|
|
|
|
#include <libart_lgpl/art_pathcode.h>
|
|
|
|
#include <libart_lgpl/art_svp.h>
|
|
|
|
#include <libart_lgpl/art_svp_vpath.h>
|
|
|
|
#include <libart_lgpl/art_svp_wind.h>
|
|
|
|
#include <libart_lgpl/art_vpath.h>
|
|
|
|
|
2001-01-24 07:56:18 +08:00
|
|
|
#include "libgimpmath/gimpmath.h"
|
|
|
|
|
2001-05-26 02:10:38 +08:00
|
|
|
#include "core-types.h"
|
1999-10-04 13:51:40 +08:00
|
|
|
|
2001-05-15 19:25:25 +08:00
|
|
|
#include "base/pixel-region.h"
|
|
|
|
|
2001-05-26 02:10:38 +08:00
|
|
|
#include "gimpchannel.h"
|
|
|
|
#include "gimpimage.h"
|
|
|
|
#include "gimpscanconvert.h"
|
2000-12-29 23:22:01 +08:00
|
|
|
|
1999-10-04 13:51:40 +08:00
|
|
|
|
2001-05-26 02:10:38 +08:00
|
|
|
struct _GimpScanConvert
|
1999-10-04 13:51:40 +08:00
|
|
|
{
|
2001-05-26 02:10:38 +08:00
|
|
|
guint width;
|
|
|
|
guint height;
|
1999-10-04 13:51:40 +08:00
|
|
|
|
2001-05-26 02:10:38 +08:00
|
|
|
guint antialias; /* how much to oversample by */
|
2002-03-07 04:25:31 +08:00
|
|
|
/* currently only used as boolean value */
|
1999-10-04 13:51:40 +08:00
|
|
|
|
2000-12-29 23:22:01 +08:00
|
|
|
/* record the first and last points so we can close the curve */
|
2001-05-26 02:10:38 +08:00
|
|
|
gboolean got_first;
|
|
|
|
GimpVector2 first;
|
|
|
|
gboolean got_last;
|
|
|
|
GimpVector2 last;
|
2001-12-16 21:33:58 +08:00
|
|
|
|
2002-03-07 04:25:31 +08:00
|
|
|
guint num_nodes;
|
|
|
|
ArtVpath *vpath;
|
|
|
|
};
|
1999-10-04 13:51:40 +08:00
|
|
|
|
|
|
|
|
2001-05-26 02:10:38 +08:00
|
|
|
/* public functions */
|
1999-10-04 13:51:40 +08:00
|
|
|
|
2001-05-26 02:10:38 +08:00
|
|
|
GimpScanConvert *
|
|
|
|
gimp_scan_convert_new (guint width,
|
|
|
|
guint height,
|
|
|
|
guint antialias)
|
1999-10-04 13:51:40 +08:00
|
|
|
{
|
2001-05-26 02:10:38 +08:00
|
|
|
GimpScanConvert *sc;
|
1999-10-04 13:51:40 +08:00
|
|
|
|
2000-12-29 23:22:01 +08:00
|
|
|
g_return_val_if_fail (width > 0, NULL);
|
|
|
|
g_return_val_if_fail (height > 0, NULL);
|
|
|
|
g_return_val_if_fail (antialias > 0, NULL);
|
1999-10-04 13:51:40 +08:00
|
|
|
|
2001-05-26 02:10:38 +08:00
|
|
|
sc = g_new0 (GimpScanConvert, 1);
|
1999-10-04 13:51:40 +08:00
|
|
|
|
2000-12-29 23:22:01 +08:00
|
|
|
sc->antialias = antialias;
|
|
|
|
sc->width = width;
|
|
|
|
sc->height = height;
|
2002-03-07 04:25:31 +08:00
|
|
|
|
|
|
|
sc->num_nodes = 0;
|
|
|
|
sc->vpath = NULL;
|
1999-10-04 13:51:40 +08:00
|
|
|
|
2000-12-29 23:22:01 +08:00
|
|
|
return sc;
|
1999-10-04 13:51:40 +08:00
|
|
|
}
|
|
|
|
|
2000-12-29 23:22:01 +08:00
|
|
|
void
|
2001-05-26 02:10:38 +08:00
|
|
|
gimp_scan_convert_free (GimpScanConvert *sc)
|
2000-12-29 23:22:01 +08:00
|
|
|
{
|
2002-03-07 04:25:31 +08:00
|
|
|
art_free (sc->vpath);
|
2000-12-29 23:22:01 +08:00
|
|
|
g_free (sc);
|
|
|
|
}
|
1999-10-04 13:51:40 +08:00
|
|
|
|
2001-05-26 02:10:38 +08:00
|
|
|
/* Add "n_points" from "points" to the polygon currently being
|
|
|
|
* described by "scan_converter".
|
|
|
|
*/
|
1999-10-04 13:51:40 +08:00
|
|
|
void
|
2001-05-26 02:10:38 +08:00
|
|
|
gimp_scan_convert_add_points (GimpScanConvert *sc,
|
|
|
|
guint n_points,
|
|
|
|
GimpVector2 *points)
|
1999-10-04 13:51:40 +08:00
|
|
|
{
|
2000-12-29 23:22:01 +08:00
|
|
|
gint i;
|
|
|
|
guint antialias;
|
1999-10-04 13:51:40 +08:00
|
|
|
|
2000-12-29 23:22:01 +08:00
|
|
|
g_return_if_fail (sc != NULL);
|
2001-05-26 02:10:38 +08:00
|
|
|
g_return_if_fail (points != NULL);
|
1999-10-04 13:51:40 +08:00
|
|
|
|
2000-12-29 23:22:01 +08:00
|
|
|
antialias = sc->antialias;
|
1999-10-04 13:51:40 +08:00
|
|
|
|
2001-05-26 02:10:38 +08:00
|
|
|
if (!sc->got_first && n_points > 0)
|
1999-10-04 13:51:40 +08:00
|
|
|
{
|
2000-12-29 23:22:01 +08:00
|
|
|
sc->got_first = TRUE;
|
2001-05-26 02:10:38 +08:00
|
|
|
sc->first = points[0];
|
1999-10-04 13:51:40 +08:00
|
|
|
}
|
|
|
|
|
2002-03-07 04:25:31 +08:00
|
|
|
/* We need up to two extra nodes later to close and finish the path */
|
|
|
|
sc->vpath = art_renew (sc->vpath, ArtVpath, sc->num_nodes + n_points + 2);
|
1999-10-06 02:05:34 +08:00
|
|
|
|
2002-03-07 04:25:31 +08:00
|
|
|
for (i = 0; i < n_points; i++)
|
1999-10-04 13:51:40 +08:00
|
|
|
{
|
2002-03-07 04:25:31 +08:00
|
|
|
sc->vpath[sc->num_nodes + i].code = (sc->num_nodes + i) ? ART_LINETO : ART_MOVETO;
|
|
|
|
sc->vpath[sc->num_nodes + i].x = points[i].x;
|
|
|
|
sc->vpath[sc->num_nodes + i].y = points[i].y;
|
1999-10-04 13:51:40 +08:00
|
|
|
}
|
2002-03-07 04:25:31 +08:00
|
|
|
sc->num_nodes += n_points;
|
1999-10-06 02:05:34 +08:00
|
|
|
|
2001-05-26 02:10:38 +08:00
|
|
|
if (n_points > 0)
|
1999-10-04 13:51:40 +08:00
|
|
|
{
|
2000-12-29 23:22:01 +08:00
|
|
|
sc->got_last = TRUE;
|
2001-05-26 02:10:38 +08:00
|
|
|
sc->last = points[n_points - 1];
|
1999-10-04 13:51:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Scan convert the polygon described by the list of points passed to
|
|
|
|
* scan_convert_add_points, and return a channel with a bits set if
|
|
|
|
* they fall within the polygon defined. The polygon is filled
|
|
|
|
* according to the even-odd rule. The polygon is closed by
|
2000-12-29 23:22:01 +08:00
|
|
|
* joining the final point to the initial point.
|
|
|
|
*/
|
2001-01-29 10:45:02 +08:00
|
|
|
GimpChannel *
|
2001-05-26 02:10:38 +08:00
|
|
|
gimp_scan_convert_to_channel (GimpScanConvert *sc,
|
|
|
|
GimpImage *gimage)
|
1999-10-04 13:51:40 +08:00
|
|
|
{
|
2001-01-29 10:45:02 +08:00
|
|
|
GimpChannel *mask;
|
2000-12-29 23:22:01 +08:00
|
|
|
PixelRegion maskPR;
|
|
|
|
gint i, j;
|
|
|
|
|
2002-03-07 04:25:31 +08:00
|
|
|
gpointer pr;
|
|
|
|
ArtVpath *pert_vpath;
|
|
|
|
ArtSVP *svp, *svp2, *svp3;
|
|
|
|
guchar *dest, *d;
|
2000-12-29 23:22:01 +08:00
|
|
|
|
|
|
|
/* do we need to close the polygon? */
|
|
|
|
if (sc->got_first && sc->got_last &&
|
|
|
|
(sc->first.x != sc->last.x || sc->first.y != sc->last.y))
|
1999-10-04 13:51:40 +08:00
|
|
|
{
|
2002-03-07 04:25:31 +08:00
|
|
|
sc->vpath[sc->num_nodes].code = ART_LINETO;
|
|
|
|
sc->vpath[sc->num_nodes].x = sc->first.x;
|
|
|
|
sc->vpath[sc->num_nodes].y = sc->first.y;
|
|
|
|
sc->num_nodes++;
|
1999-10-04 13:51:40 +08:00
|
|
|
}
|
|
|
|
|
2001-01-29 10:45:02 +08:00
|
|
|
mask = gimp_channel_new_mask (gimage, sc->width, sc->height);
|
1999-10-04 13:51:40 +08:00
|
|
|
|
2002-03-07 04:25:31 +08:00
|
|
|
sc->vpath[sc->num_nodes].code = ART_END;
|
|
|
|
sc->vpath[sc->num_nodes].x = sc->first.x;
|
|
|
|
sc->vpath[sc->num_nodes].y = sc->first.y;
|
|
|
|
sc->num_nodes++;
|
1999-10-04 13:51:40 +08:00
|
|
|
|
2002-03-07 04:25:31 +08:00
|
|
|
/*
|
|
|
|
* Current Libart (2.3.8) recommends a slight random distorsion
|
|
|
|
* of the path, because art_svp_uncross and art_svp_rewind_uncrossed
|
|
|
|
* are not yet numerically stable. It is actually possible to construct
|
|
|
|
* worst case scenarios. The slight perturbation should not have any
|
|
|
|
* visible effect.
|
|
|
|
*/
|
|
|
|
pert_vpath = art_vpath_perturb (sc->vpath);
|
|
|
|
|
|
|
|
svp = art_svp_from_vpath (pert_vpath);
|
|
|
|
svp2 = art_svp_uncross (svp);
|
|
|
|
svp3 = art_svp_rewind_uncrossed (svp2, ART_WIND_RULE_ODDEVEN);
|
1999-10-04 13:51:40 +08:00
|
|
|
|
2001-01-30 01:54:02 +08:00
|
|
|
pixel_region_init (&maskPR, gimp_drawable_data (GIMP_DRAWABLE (mask)), 0, 0,
|
|
|
|
gimp_drawable_width (GIMP_DRAWABLE (mask)),
|
|
|
|
gimp_drawable_height (GIMP_DRAWABLE (mask)), TRUE);
|
1999-10-04 13:51:40 +08:00
|
|
|
|
2002-03-07 04:25:31 +08:00
|
|
|
g_return_val_if_fail (maskPR.bytes == 1, NULL);
|
|
|
|
|
|
|
|
for (pr = pixel_regions_register (1, &maskPR);
|
|
|
|
pr != NULL;
|
|
|
|
pr = pixel_regions_process (pr))
|
1999-10-04 13:51:40 +08:00
|
|
|
{
|
2002-03-07 04:25:31 +08:00
|
|
|
art_gray_svp_aa (svp3, maskPR.x, maskPR.y,
|
|
|
|
maskPR.x + maskPR.w, maskPR.y + maskPR.h,
|
|
|
|
maskPR.data, maskPR.rowstride);
|
|
|
|
if (!sc->antialias)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Ok, the user didn't want to have antialiasing, so just
|
|
|
|
* remove the results from lots of CPU-Power...
|
|
|
|
*/
|
|
|
|
dest = maskPR.data;
|
|
|
|
|
|
|
|
for (j = 0; j < maskPR.h; j++)
|
|
|
|
{
|
|
|
|
d = dest;
|
|
|
|
for (i = 0; i < maskPR.w; i++)
|
|
|
|
{
|
|
|
|
d[0] = (d[0] >= 127) ? 255 : 0;
|
|
|
|
d += maskPR.bytes;
|
|
|
|
}
|
|
|
|
dest += maskPR.rowstride;
|
|
|
|
}
|
|
|
|
}
|
1999-10-04 13:51:40 +08:00
|
|
|
}
|
|
|
|
|
2002-03-07 04:25:31 +08:00
|
|
|
art_free (svp3);
|
|
|
|
art_free (svp2);
|
|
|
|
art_free (svp);
|
|
|
|
art_free (pert_vpath);
|
1999-10-04 13:51:40 +08:00
|
|
|
|
2000-12-29 23:22:01 +08:00
|
|
|
return mask;
|
1999-10-04 13:51:40 +08:00
|
|
|
}
|