# The GIMP -- an image manipulation program
# Copyright (C) 1995 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.

# "Perlized" from C source by Manish Singh <yosh@gimp.org>

# Tools

# shortcuts

sub drawable_arg () {{
    name => 'drawable',
    type => 'drawable',
    desc => 'The affected drawable',
}}

sub drawable_out_arg {
    my $type = shift;
    my $arg = &drawable_arg;
    $arg->{desc} = "The $type drawable";
    $arg->{no_declare} = 1;
    $arg;
}

sub stroke_arg () {
    { name => 'strokes', type => 'floatarray',
      desc => 'Array of stroke coordinates: { s1.x, s1.y, s2.x, s2.y, ...,
	       sn.x, sn.y }',
      array => { type => '2 <= int32', on_success => 'num_strokes /= 2;',
		 desc => 'Number of stroke control points (count each
			  coordinate as 2 points)' } }
}

# The defs

sub airbrush {
    $blurb  = <<'BLURB';
Paint in the current brush with varying pressure. Paint application is
time-dependent.
BLURB

    $help = <<'HELP';
This tool simulates the use of an airbrush. Paint pressure represents the
relative intensity of the paint application. High pressure results in a thicker
layer of paint while low pressure results in a thinner layer.
HELP

    &std_pdb_misc;

    @inargs = (
	&drawable_arg,
	 { name => 'pressure', type => '0 <= float <= 100',
	   desc => 'The pressure of the airbrush strokes (%%desc%%)' },
	&stroke_arg
    );

    %invoke = (
	headers => [ qw("paint/gimpairbrush.h" "paint/gimpairbrushoptions.h") ],
	vars => [ "GimpPaintOptions *options" ],
	code => <<'CODE'
{
  options = gimp_paint_options_new (gimp, GIMP_TYPE_AIRBRUSH_OPTIONS);

  GIMP_AIRBRUSH_OPTIONS (options)->pressure = pressure;

  success = paint_tools_stroke (gimp,
                                GIMP_TYPE_AIRBRUSH,
                                options,
                                drawable,
                                num_strokes, strokes);
}
CODE
    );
}

sub airbrush_default {
    $blurb  = <<'BLURB';
Paint in the current brush with varying pressure. Paint application is
time-dependent.
BLURB

    $help = <<'HELP';
This tool simulates the use of an airbrush. It is similar to gimp_airbrush 
except that the pressure is derived from the airbrush tools options box. 
It the option has not been set the default for the option will be used.
HELP

    $author = $copyright = 'Andy Thomas';
    $date = '1999';

    @inargs = (
	&drawable_arg,
	&stroke_arg
    );

    %invoke = (
	headers => [ qw("paint/gimpairbrush.h") ],
	vars => [ "GimpPaintOptions *options" ],
	code => <<'CODE'
{
  options = gimp_paint_options_new (gimp, GIMP_TYPE_AIRBRUSH_OPTIONS);

  success = paint_tools_stroke (gimp,
                                GIMP_TYPE_AIRBRUSH,
                                options,
                                drawable,
                                num_strokes, strokes);
}
CODE
    );
}


sub clone {
    $blurb = <<'BLURB';
Clone from the source to the dest drawable using the current brush
BLURB

    $help = <<'HELP';
This tool clones (copies) from the source drawable starting at the specified 
source coordinates to the dest drawable. If the "clone_type" argument is set 
to PATTERN-CLONE, then the current pattern is used as the source and the
"src_drawable" argument is ignored. Pattern cloning assumes a tileable
pattern and mods the sum of the src coordinates and subsequent stroke offsets
with the width and height of the pattern. For image cloning, if the sum of the
src coordinates and subsequent stroke offsets exceeds the extents of the src
drawable, then no paint is transferred. The clone tool is capable of
transforming between any image types including RGB->Indexed--although
converting from any type to indexed is significantly slower.
HELP

    &std_pdb_misc;

    @inargs = (
	&drawable_arg,
	{ name => 'src_drawable', type => 'drawable',
	  desc => 'The source drawable' },
	{ name => 'clone_type', type => 'enum GimpCloneType',
	  desc => 'The type of clone: { %%desc%% }' },
	{ name => 'src_x', type => 'float',
	  desc => 'The x coordinate in the source image' },
	{ name => 'src_y', type => 'float',
	  desc => 'The y coordinate in the source image' },
	&stroke_arg
    );

    %invoke = (
	headers => [ qw("paint/gimpclone.h" "paint/gimpcloneoptions.h") ],
	vars => [ "GimpPaintOptions *options" ],
	code => <<'CODE'
{
  options = gimp_paint_options_new (gimp, GIMP_TYPE_CLONE_OPTIONS);

  GIMP_CLONE_OPTIONS (options)->clone_type = clone_type;

#ifdef __GNUC__
#warning FIXME: re-enable clone src_drawable
#endif
#if 0
  FIXME

  core->src_drawable = src_drawable;
  core->src_x        = srx_x;
  core->src_y        = src_y;
#endif

  success = paint_tools_stroke (gimp,
                                GIMP_TYPE_CLONE,
                                options,
                                drawable,
                                num_strokes, strokes);
}
CODE
    );
}

sub clone_default {
    $blurb = <<'BLURB';
Clone from the source to the dest drawable using the current brush
BLURB

    $help = <<'HELP';
This tool clones (copies) from the source drawable starting at the specified 
source coordinates to the dest drawable. This function performs exactly
the same as the gimp_clone function except that the tools arguments are 
obtained from the clones option dialog. It this dialog has not been activated
then the dialogs default values will be used.
HELP

    $author = $copyright = 'Andy Thomas';
    $date = '1999';

    @inargs = (
	&drawable_arg,
	&stroke_arg
    );

    %invoke = (
	headers => [ qw("paint/gimpclone.h" "paint/gimpcloneoptions.h") ],
	vars => [ "GimpPaintOptions *options" ],
	code => <<'CODE'
{
  options = gimp_paint_options_new (gimp, GIMP_TYPE_CLONE_OPTIONS);

  success = paint_tools_stroke (gimp,
                                GIMP_TYPE_CLONE,
                                options,
                                drawable,
                                num_strokes, strokes);
}
CODE
    );
}


sub convolve_default {
    $blurb = 'Convolve (Blur, Sharpen) using the current brush.';

    $help = <<'HELP';
This tool convolves the specified drawable with either a sharpening or blurring
kernel. This function performs exactly the same as the gimp_convolve
function except that the tools arguments are obtained from the convolve 
option dialog. It this dialog has not been activated then the dialogs 
default values will be used.
HELP

    $author = $copyright = 'Andy Thomas';
    $date = '1999';

    @inargs = (
	&drawable_arg,
	&stroke_arg
    );

    %invoke = (
	headers => [ qw("paint/gimpconvolve.h" "paint/gimpconvolveoptions.h") ],
	vars => [ "GimpPaintOptions *options" ],
	code => <<'CODE'
{
  options = gimp_paint_options_new (gimp, GIMP_TYPE_CONVOLVE_OPTIONS);

  success = paint_tools_stroke (gimp,
                                GIMP_TYPE_CONVOLVE,
                                options,
                                drawable,
                                num_strokes, strokes);
}
CODE
    );
}

sub convolve {
    $blurb = 'Convolve (Blur, Sharpen) using the current brush.';

    $help = <<'HELP';
This tool convolves the specified drawable with either a sharpening or blurring
kernel. The pressure parameter controls the magnitude of the operation. Like
the paintbrush, this tool linearly interpolates between the specified stroke
coordinates.
HELP

    &std_pdb_misc;

    @inargs = (
	&drawable_arg,
	{ name => 'pressure', type => '0 <= float <= 100',
	  desc => 'The pressure: %%desc%%' },
	{ name => 'convolve_type', type => 'enum GimpConvolveType',
	  desc => 'Convolve type: { %%desc%% }' },
	&stroke_arg
    );

    %invoke = (
	headers => [ qw("paint/gimpconvolve.h" "paint/gimpconvolveoptions.h") ],
	vars => [ "GimpPaintOptions *options" ],
	code => <<'CODE'
{
  options = gimp_paint_options_new (gimp, GIMP_TYPE_CONVOLVE_OPTIONS);

  g_object_set (options,
                "type", convolve_type,
                "rate", pressure,
                NULL);

  success = paint_tools_stroke (gimp,
                                GIMP_TYPE_CONVOLVE,
                                options,
                                drawable,
                                num_strokes, strokes);
}
CODE
    );
}

sub eraser_default {
    $blurb = 'Erase using the current brush.';

    $help = <<'HELP';
This tool erases using the current brush mask. This function performs exactly
the same as the gimp_eraser function except that the tools arguments are 
obtained from the eraser option dialog. It this dialog has not been activated
then the dialogs default values will be used.
HELP

    $author = $copyright = 'Andy Thomas';
    $date = '1999';

    @inargs = (
	&drawable_arg,
	&stroke_arg
    );

    %invoke = (
	headers => [ qw("paint/gimperaser.h" "paint/gimperaseroptions.h") ],
	vars => [ "GimpPaintOptions *options" ],
	code => <<'CODE'
{
  options = gimp_paint_options_new (gimp, GIMP_TYPE_ERASER_OPTIONS);

  success = paint_tools_stroke (gimp,
                                GIMP_TYPE_ERASER,
                                options,
                                drawable,
                                num_strokes, strokes);
}
CODE
    );
}

sub eraser {
    $blurb = 'Erase using the current brush.';

    $help = <<'HELP';
This tool erases using the current brush mask. If the specified drawable
contains an alpha channel, then the erased pixels will become transparent.
Otherwise, the eraser tool replaces the contents of the drawable with the
background color. Like paintbrush, this tool linearly interpolates between the
specified stroke coordinates.
HELP

    &std_pdb_misc;

    @inargs = (
	&drawable_arg,
	&stroke_arg,
	{ name => 'hardness', type => 'enum GimpBrushApplicationMode',
	  desc => '%%desc%%' },
	{ name => 'method', type => 'enum GimpPaintApplicationMode',
	  desc => '%%desc%%' }
    );

    %invoke = (
	headers => [ qw("paint/gimperaser.h" "paint/gimperaseroptions.h") ],
	vars => [ "GimpPaintOptions *options" ],
	code => <<'CODE'
{
  options = gimp_paint_options_new (gimp, GIMP_TYPE_ERASER_OPTIONS);

  g_object_set (options,
                "application-mode", method,
                "hard",             hardness,
                NULL);

  success = paint_tools_stroke (gimp,
                                GIMP_TYPE_ERASER,
                                options,
                                drawable,
                                num_strokes, strokes);
}
CODE
    );
}

sub anti_eraser {
    $blurb = 'Anti-erase using the current brush.';

    $help = <<'HELP';
This tool anti-erases using the current brush mask. If the specified drawable
contains an alpha channel, then the erased pixels will become opaque.
Otherwise, the eraser tool replaces the contents of the drawable with the
background color. Like paintbrush, this tool linearly interpolates between the
specified stroke coordinates.
HELP

    &std_pdb_misc;

    @inargs = (
	&drawable_arg,
	&stroke_arg,
	{ name => 'hardness', type => 'enum GimpBrushApplicationMode',
	  desc => '%%desc%%' },
	{ name => 'method', type => 'enum GimpPaintApplicationMode',
	  desc => '%%desc%%' }
    );

    %invoke = (
	headers => [ qw("paint/gimperaser.h" "paint/gimperaseroptions.h") ],
	vars => [ "GimpPaintOptions *options" ],
	code => <<'CODE'
{
  options = gimp_paint_options_new (gimp, GIMP_TYPE_ERASER_OPTIONS);

  g_object_set (options,
                "application-mode", method,
                "hard",             hardness,
                "anti-erase",       TRUE,
                NULL);

  success = paint_tools_stroke (gimp,
                                GIMP_TYPE_ERASER,
                                options,
                                drawable,
                                num_strokes, strokes);
}
CODE
    );
}

sub paintbrush {
    $blurb = <<'BLURB';
Paint in the current brush with optional fade out parameter and pull colors
from a gradient.
BLURB

    $help = <<'HELP';
This tool is the standard paintbrush. It draws linearly interpolated lines
through the specified stroke coordinates. It operates on the specified drawable
in the foreground color with the active brush. The "fade_out" parameter is
measured in pixels and allows the brush stroke to linearly fall off. The
pressure is set to the maximum at the beginning of the stroke. As the distance
of the stroke nears the fade_out value, the pressure will approach zero. The
gradient_length is the distance to spread the gradient over. It is measured in
pixels. If the gradient_length is 0, no gradient is used.
HELP

    &std_pdb_misc;

    @inargs = (
	&drawable_arg,
	{ name => 'fade_out', type => '0 <= float',
	  desc => 'Fade out parameter: %%desc%%' },
	&stroke_arg,
	{ name => 'method', type => 'enum GimpPaintApplicationMode',
	  desc => '%%desc%%' },
	{ name => 'gradient_length', type => '0 <= float',
	  desc => 'Length of gradient to draw: %%desc%%' }
    );

    %invoke = (
	headers => [ qw("paint/gimppaintbrush.h") ],
	vars => [ "GimpPaintOptions *options" ],
	code => <<'CODE'
{
  options = gimp_paint_options_new (gimp, GIMP_TYPE_PAINT_OPTIONS);

  g_object_set (options,
                "application-mode", method,
                "fade-length",      fade_out,
                "gradient-length",  gradient_length,
                NULL);

  success = paint_tools_stroke (gimp,
                                GIMP_TYPE_PAINTBRUSH,
                                options,
                                drawable,
                                num_strokes, strokes);
}
CODE
    );
}

sub paintbrush_default {
    $blurb = <<'BLURB';
Paint in the current brush. The fade out parameter and pull colors
from a gradient parameter are set from the paintbrush options dialog. If this
dialog has not been activated then the dialog defaults will be used.
BLURB

    $help = <<'HELP';
This tool is similar to the standard paintbrush. It draws linearly interpolated lines
through the specified stroke coordinates. It operates on the specified drawable
in the foreground color with the active brush. The "fade_out" parameter is
measured in pixels and allows the brush stroke to linearly fall 
off (value obtained from the option dialog). The pressure is set 
to the maximum at the beginning of the stroke. As the distance
of the stroke nears the fade_out value, the pressure will approach zero. The
gradient_length (value obtained from the option dialog) is the 
distance to spread the gradient over. It is measured in pixels. If 
the gradient_length is 0, no gradient is used.
HELP

    $author = $copyright = 'Andy Thomas';
    $date = '1999';

    @inargs = (
	&drawable_arg,
	&stroke_arg
    );

    %invoke = (
	headers => [ qw("paint/gimppaintbrush.h") ],
	vars => [ "GimpPaintOptions *options" ],
	code => <<'CODE'
{
  options = gimp_paint_options_new (gimp, GIMP_TYPE_PAINT_OPTIONS);

  success = paint_tools_stroke (gimp,
                                GIMP_TYPE_PAINTBRUSH,
                                options,
                                drawable,
                                num_strokes, strokes);
}
CODE
    );
}

sub pencil {
    $blurb = 'Paint in the current brush without sub-pixel sampling.';

    $help = <<'HELP';
This tool is the standard pencil. It draws linearly interpolated lines through
the specified stroke coordinates. It operates on the specified drawable in the
foreground color with the active brush. The brush mask is treated as though it
contains only black and white values. Any value below half is treated as black;
any above half, as white.
HELP

    &std_pdb_misc;

    @inargs = (
	&drawable_arg,
	&stroke_arg
    );

    %invoke = (
	headers => [ qw("paint/gimppencil.h") ],
	vars => [ "GimpPaintOptions *options" ],
	code => <<'CODE'
{
  options = gimp_paint_options_new (gimp, GIMP_TYPE_PAINT_OPTIONS);

  success = paint_tools_stroke (gimp,
                                GIMP_TYPE_PENCIL,
                                options,
                                drawable,
                                num_strokes, strokes);
}
CODE
    );
}

sub smudge {
    $blurb  = <<'BLURB';
Smudge image with varying pressure.
BLURB

    $help = <<'HELP';
This tool simulates a smudge using the current brush. High pressure results 
in a greater smudge of paint while low pressure results in a lesser smudge.
HELP

    &std_pdb_misc;

    @inargs = (
	&drawable_arg,
	 { name => 'pressure', type => '0 <= float <= 100',
	   desc => 'The pressure of the smudge strokes (%%desc%%)' },
	&stroke_arg
    );

    %invoke = (
	headers => [ qw("paint/gimpsmudge.h" "paint/gimpsmudgeoptions.h") ],
	vars => [ "GimpPaintOptions *options" ],
	code => <<'CODE'
{
  options = gimp_paint_options_new (gimp, GIMP_TYPE_SMUDGE_OPTIONS);

  g_object_set (options,
                "rate", pressure,
                NULL);

  success = paint_tools_stroke (gimp,
                                GIMP_TYPE_SMUDGE,
                                options,
                                drawable,
                                num_strokes, strokes);
}
CODE
    );
}

sub smudge_default {
    $blurb  = <<'BLURB';
Smudge image with varying pressure.
BLURB

    $help = <<'HELP';
This tool simulates a smudge using the current brush. It behaves exactly 
the same as gimp_smudge except that the pressure value is taken from the
smudge tool options or the options default if the tools option dialog has
not been activated.
HELP

    $author = $copyright = 'Andy Thomas';
    $date = '1999';

    @inargs = (
	&drawable_arg,
	&stroke_arg
    );

    %invoke = (
	headers => [ qw("paint/gimpsmudge.h" "paint/gimpsmudgeoptions.h") ],
	vars => [ "GimpPaintOptions *options" ],
	code => <<'CODE'
{
  options = gimp_paint_options_new (gimp, GIMP_TYPE_SMUDGE_OPTIONS);

  success = paint_tools_stroke (gimp,
                                GIMP_TYPE_SMUDGE,
                                options,
                                drawable,
                                num_strokes, strokes);
}
CODE
    );
}

sub dodgeburn {
    $blurb  = <<'BLURB';
Dodgeburn image with varying exposure.
BLURB

    $help = <<'HELP';
Dodgebure. More details here later.
HELP


    $author = $copyright = 'Andy Thomas';
    $date = '1999';

    @inargs = (
	&drawable_arg,
	 { name => 'exposure', type => '0 <= float <= 100',
	   desc => 'The exposer of the  strokes (%%desc%%)' },
        { name => 'dodgeburn_type', type => 'enum GimpDodgeBurnType',
	  desc => 'The type either dodge or burn: { %%desc%% }' },
        { name => 'dodgeburn_mode', type => 'enum GimpTransferMode',
	  desc => 'The mode: { %%desc%% }' },
	&stroke_arg
    );

    %invoke = (
	headers => [ qw("paint/gimpdodgeburn.h" "paint/gimpdodgeburnoptions.h") ],
	vars => [ "GimpPaintOptions *options" ],
	code => <<'CODE'
{
  options = gimp_paint_options_new (gimp, GIMP_TYPE_DODGE_BURN_OPTIONS);

  g_object_set (options,
                "type",     dodgeburn_type,
                "mode",     dodgeburn_mode,
                "exposure", exposure,
                NULL);

  success = paint_tools_stroke (gimp,
                                GIMP_TYPE_DODGE_BURN,
                                options,
                                drawable,
                                num_strokes, strokes);
}
CODE
    );
}

sub dodgeburn_default {
    $blurb  = <<'BLURB';
Dodgeburn image with varying exposure. This is the same as the gimp_dodgeburn
function except that the exposure, type and mode are taken from the tools
option dialog. If the dialog has not been activated then the defaults 
as used by the dialog will be used.
BLURB

    $help = <<'HELP';
Dodgeburn. More details here later.
HELP

    &std_pdb_misc;

    @inargs = (
	&drawable_arg,
	&stroke_arg
    );

    %invoke = (
	headers => [ qw("paint/gimpdodgeburn.h" "paint/gimpdodgeburnoptions.h") ],
	vars => [ "GimpPaintOptions *options" ],
	code => <<'CODE'
{
  options = gimp_paint_options_new (gimp, GIMP_TYPE_DODGE_BURN_OPTIONS);

  success = paint_tools_stroke (gimp,
                                GIMP_TYPE_DODGE_BURN,
                                options,
                                drawable,
                                num_strokes, strokes);
}
CODE
    );
}


# Incomplete

sub ink {
    $blurb = 'Paint in the current brush without sub-pixel sampling.';

    $help = 'fixme fixme';

    &std_pdb_misc;

    @inargs = (
	&drawable_arg,
	&stroke_arg
    );
}

@headers = qw("libgimpmath/gimpmath.h" "paint/paint-types.h"
              "paint/gimppaintcore-stroke.h" "core/gimp.h");

$extra{app}->{code} = <<'CODE';
static gboolean
paint_tools_stroke (Gimp             *gimp,
                    GType             core_type,
                    GimpPaintOptions *options,
                    GimpDrawable     *drawable,
                    gint              n_strokes,
                    gdouble          *strokes)
{
  GimpPaintCore *core;
  GimpCoords    *coords;
  gboolean       retval;
  gint           i;

  /*  undefine the paint-relevant context properties and get them
   *  from the current context
   */
  gimp_context_define_properties (GIMP_CONTEXT (options),
                                  GIMP_CONTEXT_PAINT_PROPS_MASK,
                                  FALSE);
  gimp_context_set_parent (GIMP_CONTEXT (options),
                           gimp_get_current_context (gimp));

  core = g_object_new (core_type, NULL);

  coords = g_new (GimpCoords, n_strokes);

  for (i = 0; i < n_strokes; i++)
    {
      coords[i].x        = strokes[2 * i];
      coords[i].y        = strokes[2 * i + 1];
      coords[i].pressure = 1.0;
      coords[i].xtilt    = 0.5;
      coords[i].ytilt    = 0.5;
      coords[i].wheel    = 0.5;
    }

  retval = gimp_paint_core_stroke (core, drawable, options,
                                   coords, n_strokes);

  g_free (coords);

  g_object_unref (core);
  g_object_unref (options);

  return retval;
}
CODE

@procs = qw(airbrush airbrush_default
            clone clone_default
            convolve convolve_default
            dodgeburn dodgeburn_default
	    eraser eraser_default
            paintbrush paintbrush_default
	    pencil
            smudge smudge_default);

%exports = (app => [@procs], lib => [@procs]);

$desc = 'Paint Tool procedures';

1;
