Blame operations/external/vector-stroke.c

Packit bc1512
/* This file is an image processing operation for GEGL
Packit bc1512
 *
Packit bc1512
 * GEGL is free software; you can redistribute it and/or
Packit bc1512
 * modify it under the terms of the GNU Lesser General Public
Packit bc1512
 * License as published by the Free Software Foundation; either
Packit bc1512
 * version 3 of the License, or (at your option) any later version.
Packit bc1512
 *
Packit bc1512
 * GEGL is distributed in the hope that it will be useful,
Packit bc1512
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit bc1512
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit bc1512
 * Lesser General Public License for more details.
Packit bc1512
 *
Packit bc1512
 * You should have received a copy of the GNU Lesser General Public
Packit bc1512
 * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
Packit bc1512
 *
Packit bc1512
 * Copyright 2006 Øyvind Kolås <pippin@gimp.org>
Packit bc1512
 */
Packit bc1512
Packit bc1512
Packit bc1512
#include "config.h"
Packit bc1512
#include <glib/gi18n-lib.h>
Packit bc1512
Packit bc1512
Packit bc1512
#ifdef GEGL_CHANT_PROPERTIES
Packit bc1512
Packit bc1512
Packit bc1512
gegl_chant_color  (color,    _("Color"),      "rgba(0.0,0.0,0.0,0.0)",
Packit bc1512
                             _("Color of paint to use for stroking."))
Packit bc1512
Packit bc1512
gegl_chant_double (width,    _("Width"),  0.0, 200.0, 2.0,
Packit bc1512
                             _("The width of the brush used to stroke the path."))
Packit bc1512
Packit bc1512
gegl_chant_double (opacity,  _("Opacity"),  -2.0, 2.0, 1.0,
Packit bc1512
                             _("Opacity of stroke, note, does not behave like SVG since at the moment stroking is done using an airbrush tool."))
Packit bc1512
Packit bc1512
gegl_chant_string (transform,_("Transform"), "",
Packit bc1512
                             _("svg style description of transform."))
Packit bc1512
Packit bc1512
gegl_chant_path   (d,        _("Vector"),
Packit bc1512
                             _("A GeglVector representing the path of the stroke"))
Packit bc1512
Packit bc1512
#else
Packit bc1512
Packit bc1512
#define GEGL_CHANT_TYPE_FILTER
Packit bc1512
#define GEGL_CHANT_C_FILE "vector-stroke.c"
Packit bc1512
Packit bc1512
#include "gegl-plugin.h"
Packit bc1512
Packit bc1512
/* the path api isn't public yet */
Packit bc1512
#include "property-types/gegl-path.h"
Packit bc1512
static void path_changed (GeglPath *path,
Packit bc1512
                          const GeglRectangle *roi,
Packit bc1512
                          gpointer userdata);
Packit bc1512
Packit bc1512
#include "gegl-chant.h"
Packit bc1512
#include <cairo.h>
Packit bc1512
Packit bc1512
static void path_changed (GeglPath *path,
Packit bc1512
                          const GeglRectangle *roi,
Packit bc1512
                          gpointer userdata)
Packit bc1512
{
Packit bc1512
  GeglRectangle rect = *roi;
Packit bc1512
  GeglChantO    *o   = GEGL_CHANT_PROPERTIES (userdata);
Packit bc1512
  /* invalidate the incoming rectangle */
Packit bc1512
Packit bc1512
  rect.x -= o->width/2;
Packit bc1512
  rect.y -= o->width/2;
Packit bc1512
  rect.width += o->width;
Packit bc1512
  rect.height += o->width;
Packit bc1512
Packit bc1512
  gegl_operation_invalidate (userdata, &rect, FALSE);
Packit bc1512
};
Packit bc1512
Packit bc1512
static void
Packit bc1512
prepare (GeglOperation *operation)
Packit bc1512
{
Packit bc1512
  GeglChantO *o = GEGL_CHANT_PROPERTIES (operation);
Packit bc1512
  gegl_operation_set_format (operation, "output", babl_format ("RaGaBaA float"));
Packit bc1512
  if (o->transform && o->transform[0] != '\0')
Packit bc1512
    {
Packit bc1512
      GeglMatrix3 matrix;
Packit bc1512
      gegl_matrix3_parse_string (&matrix, o->transform);
Packit bc1512
      gegl_path_set_matrix (o->d, &matrix);
Packit bc1512
    }
Packit bc1512
}
Packit bc1512
Packit bc1512
static GeglRectangle
Packit bc1512
get_bounding_box (GeglOperation *operation)
Packit bc1512
{
Packit bc1512
  GeglChantO    *o       = GEGL_CHANT_PROPERTIES (operation);
Packit bc1512
  GeglRectangle  defined = { 0, 0, 512, 512 };
Packit bc1512
  GeglRectangle *in_rect;
Packit bc1512
  gdouble        x0, x1, y0, y1;
Packit bc1512
Packit bc1512
  in_rect =  gegl_operation_source_get_bounding_box (operation, "input");
Packit bc1512
Packit bc1512
  gegl_path_get_bounds (o->d, &x0, &x1, &y0, &y1;;
Packit bc1512
  defined.x      = x0 - o->width/2;
Packit bc1512
  defined.y      = y0 - o->width/2;
Packit bc1512
  defined.width  = x1 - x0 + o->width;
Packit bc1512
  defined.height = y1 - y0 + o->width;
Packit bc1512
Packit bc1512
  if (in_rect)
Packit bc1512
    {
Packit bc1512
      gegl_rectangle_bounding_box (&defined, &defined, in_rect);
Packit bc1512
    }
Packit bc1512
Packit bc1512
  return defined;
Packit bc1512
}
Packit bc1512
Packit bc1512
Packit bc1512
Packit bc1512
#if 0
Packit bc1512
static gboolean gegl_path_is_closed (GeglPath *path)
Packit bc1512
{
Packit bc1512
  const GeglPathItem *knot;
Packit bc1512
Packit bc1512
  if (!path)
Packit bc1512
    return FALSE;
Packit bc1512
  knot = gegl_path_get_node (path, -1);
Packit bc1512
  if (!knot)
Packit bc1512
    return FALSE;
Packit bc1512
  if (knot->type == 'z')
Packit bc1512
    {
Packit bc1512
      return TRUE;
Packit bc1512
    }
Packit bc1512
  return FALSE;
Packit bc1512
}
Packit bc1512
#endif
Packit bc1512
Packit bc1512
Packit bc1512
#if 0
Packit bc1512
static GeglRectangle
Packit bc1512
get_cached_region (GeglOperation *operation)
Packit bc1512
{
Packit bc1512
  return get_bounding_box (operation);
Packit bc1512
}
Packit bc1512
#endif
Packit bc1512
static void gegl_path_cairo_play (GeglPath *path,
Packit bc1512
                                    cairo_t *cr);
Packit bc1512
Packit bc1512
static gboolean
Packit bc1512
process (GeglOperation       *operation,
Packit bc1512
         GeglBuffer          *input,
Packit bc1512
         GeglBuffer          *output,
Packit bc1512
         const GeglRectangle *result,
Packit bc1512
         gint                 level)
Packit bc1512
{
Packit bc1512
  GeglChantO *o = GEGL_CHANT_PROPERTIES (operation);
Packit bc1512
  gboolean need_stroke = FALSE;
Packit bc1512
  gdouble r,g,b,a;
Packit bc1512
Packit bc1512
  if (input)
Packit bc1512
    {
Packit bc1512
      gegl_buffer_copy (input, result, output, result);
Packit bc1512
    }
Packit bc1512
  else
Packit bc1512
    {
Packit bc1512
      gegl_buffer_clear (output, result);
Packit bc1512
    }
Packit bc1512
Packit bc1512
  if (o->width > 0.1 && o->opacity > 0.0001)
Packit bc1512
    {
Packit bc1512
      gegl_color_get_rgba (o->color, &r,&g,&b,&a);
Packit bc1512
      a *= o->opacity;
Packit bc1512
      if (a>0.001)
Packit bc1512
          need_stroke=TRUE;
Packit bc1512
    }
Packit bc1512
Packit bc1512
  if (need_stroke)
Packit bc1512
    {
Packit bc1512
      GStaticMutex mutex = G_STATIC_MUTEX_INIT;
Packit bc1512
      cairo_t *cr;
Packit bc1512
      cairo_surface_t *surface;
Packit bc1512
      guchar *data;
Packit bc1512
Packit bc1512
      g_static_mutex_lock (&mutex);
Packit bc1512
      data = (void*)gegl_buffer_linear_open (output, result, NULL, babl_format ("B'aG'aR'aA u8"));
Packit bc1512
      surface = cairo_image_surface_create_for_data (data,
Packit bc1512
                                                     CAIRO_FORMAT_ARGB32,
Packit bc1512
                                                     result->width,
Packit bc1512
                                                     result->height,
Packit bc1512
                                                     result->width * 4);
Packit bc1512
Packit bc1512
      cr = cairo_create (surface);
Packit bc1512
Packit bc1512
      cairo_translate (cr, -result->x, -result->y);
Packit bc1512
Packit bc1512
      cairo_set_line_width  (cr, o->width);
Packit bc1512
      cairo_set_line_cap    (cr, CAIRO_LINE_CAP_ROUND);
Packit bc1512
      cairo_set_line_join   (cr, CAIRO_LINE_JOIN_ROUND);
Packit bc1512
Packit bc1512
      gegl_path_cairo_play (o->d, cr);
Packit bc1512
      cairo_set_source_rgba (cr, r,g,b,a);
Packit bc1512
      cairo_stroke (cr);
Packit bc1512
      cairo_destroy (cr);
Packit bc1512
Packit bc1512
      gegl_buffer_linear_close (output, data);
Packit bc1512
      g_static_mutex_unlock (&mutex);
Packit bc1512
    }
Packit bc1512
  return  TRUE;
Packit bc1512
}
Packit bc1512
static void foreach_cairo (const GeglPathItem *knot,
Packit bc1512
                           gpointer              cr)
Packit bc1512
{
Packit bc1512
  switch (knot->type)
Packit bc1512
    {
Packit bc1512
      case 'M':
Packit bc1512
        cairo_move_to (cr, knot->point[0].x, knot->point[0].y);
Packit bc1512
        break;
Packit bc1512
      case 'L':
Packit bc1512
        cairo_line_to (cr, knot->point[0].x, knot->point[0].y);
Packit bc1512
        break;
Packit bc1512
      case 'C':
Packit bc1512
        cairo_curve_to (cr, knot->point[0].x, knot->point[0].y,
Packit bc1512
                            knot->point[1].x, knot->point[1].y,
Packit bc1512
                            knot->point[2].x, knot->point[2].y);
Packit bc1512
        break;
Packit bc1512
      case 'z':
Packit bc1512
        cairo_close_path (cr);
Packit bc1512
        break;
Packit bc1512
      default:
Packit bc1512
        g_print ("%s uh?:%c\n", G_STRLOC, knot->type);
Packit bc1512
    }
Packit bc1512
}
Packit bc1512
Packit bc1512
static void gegl_path_cairo_play (GeglPath *path,
Packit bc1512
                                    cairo_t *cr)
Packit bc1512
{
Packit bc1512
  gegl_path_foreach_flat (path, foreach_cairo, cr);
Packit bc1512
}
Packit bc1512
Packit bc1512
static GeglNode *detect (GeglOperation *operation,
Packit bc1512
                         gint           x,
Packit bc1512
                         gint           y)
Packit bc1512
{
Packit bc1512
  GeglChantO *o = GEGL_CHANT_PROPERTIES (operation);
Packit bc1512
  cairo_t *cr;
Packit bc1512
  cairo_surface_t *surface;
Packit bc1512
  gchar *data = "     ";
Packit bc1512
  gboolean result = FALSE;
Packit bc1512
Packit bc1512
  surface = cairo_image_surface_create_for_data ((guchar*)data,
Packit bc1512
                                                 CAIRO_FORMAT_ARGB32,
Packit bc1512
                                                 1,1,4);
Packit bc1512
  cr = cairo_create (surface);
Packit bc1512
  gegl_path_cairo_play (o->d, cr);
Packit bc1512
  cairo_set_line_width (cr, o->width);
Packit bc1512
Packit bc1512
Packit bc1512
  if (o->width > 0.1 && o->opacity > 0.0001)
Packit bc1512
    result = cairo_in_stroke (cr, x, y);
Packit bc1512
Packit bc1512
  cairo_destroy (cr);
Packit bc1512
Packit bc1512
  if (result)
Packit bc1512
    return operation->node;
Packit bc1512
Packit bc1512
  return NULL;
Packit bc1512
}
Packit bc1512
Packit bc1512
static void
Packit bc1512
gegl_chant_class_init (GeglChantClass *klass)
Packit bc1512
{
Packit bc1512
  GeglOperationClass       *operation_class;
Packit bc1512
  GeglOperationFilterClass *filter_class;
Packit bc1512
Packit bc1512
  operation_class = GEGL_OPERATION_CLASS (klass);
Packit bc1512
  filter_class    = GEGL_OPERATION_FILTER_CLASS (klass);
Packit bc1512
Packit bc1512
  filter_class->process = process;
Packit bc1512
  operation_class->get_bounding_box = get_bounding_box;
Packit bc1512
  operation_class->prepare = prepare;
Packit bc1512
  operation_class->detect = detect;
Packit bc1512
  /*operation_class->no_cache = TRUE;*/
Packit bc1512
Packit bc1512
  gegl_operation_class_set_keys (operation_class,
Packit bc1512
    "name"        , "gegl:vector-stroke",
Packit bc1512
    "categories"  , "render",
Packit bc1512
    "description" , _("Renders a vector stroke"),
Packit bc1512
    NULL);
Packit bc1512
}
Packit bc1512
Packit bc1512
Packit bc1512
#endif