2003-09-21 03:58:26 +08:00
|
|
|
/* The GIMP -- an image manipulation program
|
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* gimpdrawable-stroke.c
|
|
|
|
* Copyright (C) 2003 Simon Budig <simon@gimp.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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <glib-object.h>
|
|
|
|
|
|
|
|
#include "libgimpcolor/gimpcolor.h"
|
|
|
|
|
|
|
|
#include "core-types.h"
|
|
|
|
|
2003-09-30 23:16:51 +08:00
|
|
|
#include "base/boundary.h"
|
2003-09-21 03:58:26 +08:00
|
|
|
#include "base/pixel-region.h"
|
|
|
|
#include "base/temp-buf.h"
|
|
|
|
#include "base/tile-manager.h"
|
|
|
|
|
|
|
|
#include "paint-funcs/paint-funcs.h"
|
|
|
|
|
|
|
|
#include "gimp.h"
|
2003-10-01 02:06:19 +08:00
|
|
|
#include "gimpchannel.h"
|
2003-09-30 07:45:06 +08:00
|
|
|
#include "gimpcontext.h"
|
2003-09-21 03:58:26 +08:00
|
|
|
#include "gimpdrawable-stroke.h"
|
|
|
|
#include "gimpimage.h"
|
2003-09-28 04:04:07 +08:00
|
|
|
#include "gimppattern.h"
|
|
|
|
#include "gimpscanconvert.h"
|
|
|
|
#include "gimpstrokeoptions.h"
|
|
|
|
#include "gimpunit.h"
|
|
|
|
|
|
|
|
#include "vectors/gimpstroke.h"
|
|
|
|
#include "vectors/gimpvectors.h"
|
2003-09-21 03:58:26 +08:00
|
|
|
|
|
|
|
#include "gimp-intl.h"
|
|
|
|
|
|
|
|
|
2003-10-01 02:06:19 +08:00
|
|
|
/* local function prototypes */
|
|
|
|
|
|
|
|
static void gimp_drawable_stroke_scan_convert (GimpDrawable *drawable,
|
|
|
|
GimpStrokeOptions *options,
|
|
|
|
GimpScanConvert *scan_convert);
|
|
|
|
|
2003-09-30 23:16:51 +08:00
|
|
|
|
2003-09-30 10:44:17 +08:00
|
|
|
/* public functions */
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_drawable_stroke_boundary (GimpDrawable *drawable,
|
|
|
|
GimpStrokeOptions *options,
|
|
|
|
const BoundSeg *bound_segs,
|
2003-09-30 23:16:51 +08:00
|
|
|
gint n_bound_segs,
|
|
|
|
gint offset_x,
|
|
|
|
gint offset_y)
|
2003-09-30 10:44:17 +08:00
|
|
|
{
|
2003-09-30 23:16:51 +08:00
|
|
|
GimpScanConvert *scan_convert;
|
2003-10-01 08:02:48 +08:00
|
|
|
BoundSeg *sorted_segs;
|
2003-09-30 23:16:51 +08:00
|
|
|
BoundSeg *stroke_segs;
|
|
|
|
gint n_stroke_segs;
|
|
|
|
GimpVector2 *points;
|
|
|
|
gint n_points;
|
|
|
|
gint seg;
|
2003-10-01 02:06:19 +08:00
|
|
|
gint i;
|
2003-09-30 23:16:51 +08:00
|
|
|
|
2003-09-30 10:44:17 +08:00
|
|
|
g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
|
|
|
|
g_return_if_fail (GIMP_IS_STROKE_OPTIONS (options));
|
|
|
|
g_return_if_fail (bound_segs != NULL);
|
|
|
|
g_return_if_fail (n_bound_segs > 0);
|
|
|
|
|
2003-10-01 08:02:48 +08:00
|
|
|
sorted_segs = sort_boundary (bound_segs, n_bound_segs, &n_stroke_segs);
|
|
|
|
stroke_segs = simplify_boundary (sorted_segs, n_stroke_segs, &n_bound_segs);
|
|
|
|
|
|
|
|
g_free (sorted_segs);
|
2003-10-01 02:06:19 +08:00
|
|
|
|
2003-09-30 23:16:51 +08:00
|
|
|
if (n_stroke_segs == 0)
|
|
|
|
return;
|
|
|
|
|
2003-10-01 02:06:19 +08:00
|
|
|
scan_convert = gimp_scan_convert_new ();
|
|
|
|
|
2003-09-30 23:16:51 +08:00
|
|
|
points = g_new0 (GimpVector2, n_bound_segs + 4);
|
|
|
|
|
|
|
|
seg = 0;
|
|
|
|
n_points = 0;
|
|
|
|
|
|
|
|
/* we offset all coordinates by 0.5 to align the brush with the path */
|
|
|
|
|
|
|
|
points[n_points].x = (gdouble) (stroke_segs[0].x1 + offset_y + 0.5);
|
|
|
|
points[n_points].y = (gdouble) (stroke_segs[0].y1 + offset_y + 0.5);
|
|
|
|
|
|
|
|
n_points++;
|
|
|
|
|
|
|
|
for (i = 0; i < n_stroke_segs; i++)
|
|
|
|
{
|
|
|
|
while (stroke_segs[seg].x1 != -1 ||
|
|
|
|
stroke_segs[seg].x2 != -1 ||
|
|
|
|
stroke_segs[seg].y1 != -1 ||
|
|
|
|
stroke_segs[seg].y2 != -1)
|
|
|
|
{
|
|
|
|
points[n_points].x = (gdouble) (stroke_segs[seg].x1 + offset_x + 0.5);
|
|
|
|
points[n_points].y = (gdouble) (stroke_segs[seg].y1 + offset_y + 0.5);
|
|
|
|
|
|
|
|
n_points++;
|
|
|
|
seg++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the stroke points up */
|
|
|
|
points[n_points] = points[0];
|
|
|
|
|
|
|
|
n_points++;
|
|
|
|
|
|
|
|
gimp_scan_convert_add_polyline (scan_convert, n_points, points, TRUE);
|
|
|
|
|
|
|
|
n_points = 0;
|
|
|
|
seg++;
|
|
|
|
|
|
|
|
points[n_points].x = (gdouble) (stroke_segs[seg].x1 + offset_x + 0.5);
|
|
|
|
points[n_points].y = (gdouble) (stroke_segs[seg].y1 + offset_y + 0.5);
|
|
|
|
|
|
|
|
n_points++;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (points);
|
|
|
|
g_free (stroke_segs);
|
|
|
|
|
2003-10-01 02:06:19 +08:00
|
|
|
gimp_drawable_stroke_scan_convert (drawable, options, scan_convert);
|
2003-09-30 23:16:51 +08:00
|
|
|
|
|
|
|
gimp_scan_convert_free (scan_convert);
|
2003-09-30 10:44:17 +08:00
|
|
|
}
|
2003-09-21 03:58:26 +08:00
|
|
|
|
|
|
|
void
|
2003-09-27 23:29:21 +08:00
|
|
|
gimp_drawable_stroke_vectors (GimpDrawable *drawable,
|
2003-09-30 10:44:17 +08:00
|
|
|
GimpStrokeOptions *options,
|
|
|
|
GimpVectors *vectors)
|
2003-09-21 03:58:26 +08:00
|
|
|
{
|
2003-09-27 23:29:21 +08:00
|
|
|
GimpScanConvert *scan_convert;
|
2003-10-01 02:06:19 +08:00
|
|
|
GimpStroke *stroke;
|
|
|
|
gint num_coords = 0;
|
2003-09-27 23:29:21 +08:00
|
|
|
|
2003-09-28 04:04:07 +08:00
|
|
|
g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
|
|
|
|
g_return_if_fail (GIMP_IS_STROKE_OPTIONS (options));
|
2003-09-30 10:44:17 +08:00
|
|
|
g_return_if_fail (GIMP_IS_VECTORS (vectors));
|
2003-09-27 23:29:21 +08:00
|
|
|
|
2003-10-01 02:06:19 +08:00
|
|
|
scan_convert = gimp_scan_convert_new ();
|
2003-09-21 18:45:19 +08:00
|
|
|
|
2003-09-21 03:58:26 +08:00
|
|
|
/* For each Stroke in the vector, interpolate it, and add it to the
|
2003-09-28 04:04:07 +08:00
|
|
|
* ScanConvert
|
|
|
|
*/
|
2003-09-21 03:58:26 +08:00
|
|
|
for (stroke = gimp_vectors_stroke_get_next (vectors, NULL);
|
|
|
|
stroke;
|
|
|
|
stroke = gimp_vectors_stroke_get_next (vectors, stroke))
|
|
|
|
{
|
|
|
|
GimpVector2 *points;
|
|
|
|
gboolean closed;
|
|
|
|
GArray *coords;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
/* Get the interpolated version of this stroke, and add it to our
|
2003-09-28 04:04:07 +08:00
|
|
|
* scanconvert.
|
|
|
|
*/
|
2003-09-28 12:00:50 +08:00
|
|
|
coords = gimp_stroke_interpolate (stroke, 0.2, &closed);
|
2003-09-21 03:58:26 +08:00
|
|
|
|
|
|
|
if (coords && coords->len)
|
|
|
|
{
|
|
|
|
points = g_new0 (GimpVector2, coords->len);
|
2003-09-21 18:45:19 +08:00
|
|
|
|
2003-09-21 03:58:26 +08:00
|
|
|
for (i = 0; i < coords->len; i++)
|
|
|
|
{
|
|
|
|
points[i].x = g_array_index (coords, GimpCoords, i).x;
|
|
|
|
points[i].y = g_array_index (coords, GimpCoords, i).y;
|
|
|
|
num_coords++;
|
|
|
|
}
|
2003-09-21 18:45:19 +08:00
|
|
|
|
2003-09-21 03:58:26 +08:00
|
|
|
gimp_scan_convert_add_polyline (scan_convert, coords->len,
|
|
|
|
points, closed);
|
|
|
|
|
|
|
|
g_free (points);
|
|
|
|
}
|
|
|
|
|
2003-10-01 02:06:19 +08:00
|
|
|
if (coords)
|
|
|
|
g_array_free (coords, TRUE);
|
2003-09-21 03:58:26 +08:00
|
|
|
}
|
|
|
|
|
2003-10-01 02:06:19 +08:00
|
|
|
if (num_coords > 0)
|
|
|
|
gimp_drawable_stroke_scan_convert (drawable, options, scan_convert);
|
2003-09-30 23:16:51 +08:00
|
|
|
|
|
|
|
gimp_scan_convert_free (scan_convert);
|
|
|
|
}
|
|
|
|
|
2003-10-01 02:06:19 +08:00
|
|
|
|
|
|
|
/* private functions */
|
|
|
|
|
2003-09-30 23:16:51 +08:00
|
|
|
static void
|
2003-10-01 02:06:19 +08:00
|
|
|
gimp_drawable_stroke_scan_convert (GimpDrawable *drawable,
|
|
|
|
GimpStrokeOptions *options,
|
|
|
|
GimpScanConvert *scan_convert)
|
2003-09-30 23:16:51 +08:00
|
|
|
{
|
|
|
|
/* Stroke options */
|
|
|
|
gdouble width;
|
|
|
|
GArray *dash_array = NULL;
|
|
|
|
TileManager *base;
|
|
|
|
TileManager *mask;
|
|
|
|
gint x1, x2, y1, y2, bytes, w, h;
|
|
|
|
guchar bg[1] = { 0, };
|
|
|
|
PixelRegion maskPR, basePR;
|
|
|
|
GimpContext *context;
|
2003-10-01 02:06:19 +08:00
|
|
|
GimpImage *gimage;
|
2003-09-30 23:16:51 +08:00
|
|
|
|
|
|
|
context = GIMP_CONTEXT (options);
|
2003-10-01 02:06:19 +08:00
|
|
|
|
2003-09-30 23:16:51 +08:00
|
|
|
gimage = gimp_item_get_image (GIMP_ITEM (drawable));
|
|
|
|
|
|
|
|
/* what area do we operate on? */
|
2003-10-01 02:06:19 +08:00
|
|
|
if (! gimp_channel_is_empty (gimp_image_get_mask (gimage)))
|
2003-09-30 23:16:51 +08:00
|
|
|
{
|
|
|
|
gimp_drawable_mask_bounds (drawable, &x1, &y1, &x2, &y2);
|
|
|
|
|
|
|
|
w = x2 - x1;
|
|
|
|
h = y2 - y1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x1 = y1 = 0;
|
|
|
|
w = gimp_item_width (GIMP_ITEM (drawable));
|
|
|
|
h = gimp_item_height (GIMP_ITEM (drawable));
|
|
|
|
}
|
|
|
|
|
|
|
|
gimp_item_offsets (GIMP_ITEM (drawable), &x2, &y2);
|
|
|
|
|
2003-09-30 07:45:06 +08:00
|
|
|
width = options->width;
|
|
|
|
|
2003-09-30 08:15:00 +08:00
|
|
|
if (options->unit != GIMP_UNIT_PIXEL)
|
2003-09-30 07:45:06 +08:00
|
|
|
{
|
2003-09-30 09:03:25 +08:00
|
|
|
gimp_scan_convert_set_pixel_ratio (scan_convert,
|
|
|
|
gimage->yresolution /
|
|
|
|
gimage->xresolution);
|
2003-09-30 07:45:06 +08:00
|
|
|
|
2003-09-30 09:03:25 +08:00
|
|
|
width *= (gimage->yresolution /
|
2003-09-30 08:15:00 +08:00
|
|
|
_gimp_unit_get_factor (gimage->gimp, options->unit));
|
2003-09-30 07:45:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
gimp_scan_convert_stroke (scan_convert, width,
|
|
|
|
options->join_style,
|
|
|
|
options->cap_style,
|
|
|
|
options->miter,
|
2003-09-26 08:41:10 +08:00
|
|
|
0.0, dash_array);
|
2003-09-21 03:58:26 +08:00
|
|
|
|
2003-09-21 08:58:04 +08:00
|
|
|
/* fill a 1-bpp Tilemanager with black, this will describe the shape
|
2003-09-28 04:04:07 +08:00
|
|
|
* of the stroke.
|
|
|
|
*/
|
2003-09-21 08:58:04 +08:00
|
|
|
mask = tile_manager_new (w, h, 1);
|
|
|
|
pixel_region_init (&maskPR, mask, 0, 0, w, h, TRUE);
|
|
|
|
color_region (&maskPR, bg);
|
2003-09-21 18:45:19 +08:00
|
|
|
|
2003-09-21 08:58:04 +08:00
|
|
|
/* render the stroke into it */
|
2003-10-07 00:43:05 +08:00
|
|
|
gimp_scan_convert_render (scan_convert, mask,
|
|
|
|
x1 + x2, y1 + y2,
|
|
|
|
options->antialias);
|
2003-09-21 18:45:19 +08:00
|
|
|
|
2003-09-21 03:58:26 +08:00
|
|
|
bytes = drawable->bytes;
|
|
|
|
if (!gimp_drawable_has_alpha (drawable))
|
|
|
|
bytes++;
|
|
|
|
|
2003-09-28 04:04:07 +08:00
|
|
|
base = tile_manager_new (w, h, bytes);
|
|
|
|
tile_manager_set_offsets (base, x1 + x2, y1 + y2);
|
|
|
|
pixel_region_init (&basePR, base, 0, 0, w, h, TRUE);
|
|
|
|
pixel_region_init (&maskPR, mask, 0, 0, w, h, FALSE);
|
2003-09-23 07:19:22 +08:00
|
|
|
|
2003-09-30 07:45:06 +08:00
|
|
|
switch (options->style)
|
2003-09-28 04:04:07 +08:00
|
|
|
{
|
|
|
|
case GIMP_STROKE_STYLE_SOLID:
|
|
|
|
{
|
2003-09-30 07:45:06 +08:00
|
|
|
guchar tmp_col[MAX_CHANNELS] = { 0, };
|
|
|
|
guchar col[MAX_CHANNELS] = { 0, };
|
2003-09-27 23:29:21 +08:00
|
|
|
|
2003-09-30 07:45:06 +08:00
|
|
|
gimp_rgb_get_uchar (&(context->foreground),
|
2003-09-28 04:42:31 +08:00
|
|
|
&tmp_col[RED_PIX],
|
|
|
|
&tmp_col[GREEN_PIX],
|
|
|
|
&tmp_col[BLUE_PIX]);
|
2003-09-27 23:29:21 +08:00
|
|
|
|
2003-09-30 07:45:06 +08:00
|
|
|
gimp_image_transform_color (gimage, drawable,
|
2003-09-28 04:42:31 +08:00
|
|
|
col, GIMP_RGB, tmp_col);
|
|
|
|
col[bytes - 1] = OPAQUE_OPACITY;
|
2003-09-28 04:04:07 +08:00
|
|
|
|
2003-09-28 04:42:31 +08:00
|
|
|
color_region_mask (&basePR, &maskPR, col);
|
2003-09-28 04:04:07 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_STROKE_STYLE_PATTERN:
|
|
|
|
{
|
|
|
|
GimpPattern *pattern;
|
|
|
|
TempBuf *pat_buf;
|
|
|
|
gboolean new_buf;
|
|
|
|
|
|
|
|
g_object_get (options, "pattern", &pattern, NULL);
|
|
|
|
pat_buf = gimp_image_transform_temp_buf (gimage, drawable,
|
|
|
|
pattern->mask, &new_buf);
|
|
|
|
g_object_unref (pattern);
|
|
|
|
|
|
|
|
pattern_region (&basePR, &maskPR, pat_buf, x1, y1);
|
|
|
|
|
|
|
|
if (new_buf)
|
|
|
|
temp_buf_free (pat_buf);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2003-09-21 03:58:26 +08:00
|
|
|
|
2003-09-21 08:58:04 +08:00
|
|
|
/* Apply to drawable */
|
|
|
|
pixel_region_init (&basePR, base, 0, 0, w, h, FALSE);
|
2003-10-06 22:40:12 +08:00
|
|
|
gimp_drawable_apply_region (drawable, &basePR,
|
|
|
|
TRUE, _("Render Stroke"),
|
|
|
|
gimp_context_get_opacity (context),
|
|
|
|
gimp_context_get_paint_mode (context),
|
|
|
|
NULL, x1, y1);
|
2003-09-21 03:58:26 +08:00
|
|
|
|
|
|
|
tile_manager_unref (mask);
|
|
|
|
tile_manager_unref (base);
|
|
|
|
|
2003-09-21 08:58:04 +08:00
|
|
|
gimp_drawable_update (drawable, x1, y1, w, h);
|
2003-09-21 03:58:26 +08:00
|
|
|
}
|