home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / app / paintbrush.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-20  |  25.0 KB  |  763 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 2 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17.  */
  18.  
  19. #include "config.h"
  20.  
  21. #include <glib.h>
  22.  
  23. #include "apptypes.h"
  24.  
  25. #include "appenv.h"
  26. #include "drawable.h"
  27. #include "gdisplay.h"
  28. #include "gimpbrushlist.h"
  29. #include "gimpui.h"
  30. #include "gradient.h"
  31. #include "paint_funcs.h"
  32. #include "paint_core.h"
  33. #include "paint_options.h"
  34. #include "paintbrush.h"
  35. #include "selection.h"
  36. #include "tools.h"
  37.  
  38. #include "libgimp/gimpmath.h"
  39. #include "libgimp/gimpunitmenu.h"
  40.  
  41. #include "libgimp/gimpintl.h"
  42.  
  43.  
  44. /*  defines  */
  45. #define  PAINT_LEFT_THRESHOLD  0.05
  46.  
  47. /* defaults for the tool options */
  48. #define PAINTBRUSH_DEFAULT_INCREMENTAL       FALSE
  49. #define PAINTBRUSH_DEFAULT_USE_FADE          FALSE
  50. #define PAINTBRUSH_DEFAULT_FADE_OUT          100.0
  51. #define PAINTBRUSH_DEFAULT_FADE_UNIT         GIMP_UNIT_PIXEL
  52. #define PAINTBRUSH_DEFAULT_USE_GRADIENT      FALSE
  53. #define PAINTBRUSH_DEFAULT_GRADIENT_LENGTH   100.0
  54. #define PAINTBRUSH_DEFAULT_GRADIENT_UNIT     GIMP_UNIT_PIXEL
  55. #define PAINTBRUSH_DEFAULT_GRADIENT_TYPE     LOOP_TRIANGLE
  56.  
  57. /*  the paintbrush structures  */
  58.  
  59. typedef struct _PaintbrushOptions PaintbrushOptions;
  60. struct _PaintbrushOptions
  61. {
  62.   PaintOptions  paint_options;
  63.  
  64.   gboolean      use_fade;
  65.   gboolean      use_fade_d;
  66.   GtkWidget    *use_fade_w;
  67.  
  68.   gdouble       fade_out;
  69.   gdouble       fade_out_d;
  70.   GtkObject    *fade_out_w;
  71.  
  72.   GimpUnit      fade_unit;
  73.   GimpUnit      fade_unit_d;
  74.   GtkWidget    *fade_unit_w;
  75.  
  76.   gboolean      use_gradient;
  77.   gboolean      use_gradient_d;
  78.   GtkWidget    *use_gradient_w;
  79.  
  80.   gdouble       gradient_length;
  81.   gdouble       gradient_length_d;
  82.   GtkObject    *gradient_length_w;
  83.  
  84.   GimpUnit      gradient_unit;
  85.   GimpUnit      gradient_unit_d;
  86.   GtkWidget    *gradient_unit_w;
  87.  
  88.   gint          gradient_type;
  89.   gint          gradient_type_d;
  90.   GtkWidget    *gradient_type_w;
  91. };
  92.  
  93. /*  the paint brush tool options  */
  94. static PaintbrushOptions * paintbrush_options = NULL;
  95.  
  96. /*  local variables  */
  97. static gdouble   non_gui_fade_out;
  98. static gdouble   non_gui_gradient_length;
  99. static gint      non_gui_gradient_type;
  100. static gdouble   non_gui_incremental;
  101. static GimpUnit  non_gui_fade_unit;
  102. static GimpUnit  non_gui_gradient_unit;
  103.  
  104.  
  105. /*  forward function declarations  */
  106. static void paintbrush_motion (PaintCore *, GimpDrawable *,
  107.                    PaintPressureOptions *,
  108.                    double, double, PaintApplicationMode,
  109.                    GradientPaintMode);
  110.  
  111. /*  functions  */
  112.  
  113. static void
  114. paintbrush_gradient_toggle_callback (GtkWidget *widget,
  115.                      gpointer   data)
  116. {
  117.   PaintbrushOptions *options = paintbrush_options;
  118.  
  119.   static int incremental_save = FALSE;
  120.  
  121.   gimp_toggle_button_update (widget, data);
  122.  
  123.   if (paintbrush_options->use_gradient)
  124.     {
  125.       incremental_save = options->paint_options.incremental;
  126.       gtk_toggle_button_set_active
  127.     (GTK_TOGGLE_BUTTON (options->paint_options.incremental_w), TRUE);
  128.     }
  129.   else
  130.     {
  131.       gtk_toggle_button_set_active
  132.     (GTK_TOGGLE_BUTTON (options->paint_options.incremental_w),
  133.      incremental_save);
  134.     }
  135. }
  136.  
  137. static void
  138. paintbrush_options_reset (void)
  139. {
  140.   PaintbrushOptions *options = paintbrush_options;
  141.   GtkWidget *spinbutton;
  142.   gint       digits;
  143.  
  144.   paint_options_reset ((PaintOptions *) options);
  145.  
  146.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (options->use_fade_w),
  147.                 options->use_fade_d);
  148.   gtk_adjustment_set_value (GTK_ADJUSTMENT (options->fade_out_w),
  149.                 options->fade_out_d);
  150.   gimp_unit_menu_set_unit (GIMP_UNIT_MENU (options->fade_unit_w),
  151.                options->fade_unit_d);
  152.   digits = ((options->fade_unit_d == GIMP_UNIT_PIXEL) ? 0 :
  153.         ((options->fade_unit_d == GIMP_UNIT_PERCENT) ? 2 :
  154.          (MIN (6, MAX (3, gimp_unit_get_digits (options->fade_unit_d))))));
  155.   spinbutton = gtk_object_get_data (GTK_OBJECT (options->fade_unit_w), "set_digits");
  156.   gtk_spin_button_set_digits (GTK_SPIN_BUTTON (spinbutton), digits);
  157.  
  158.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (options->use_gradient_w),
  159.                 options->use_gradient_d);
  160.   gtk_adjustment_set_value (GTK_ADJUSTMENT (options->gradient_length_w),
  161.                 options->gradient_length_d);
  162.   gimp_unit_menu_set_unit (GIMP_UNIT_MENU (options->gradient_unit_w),
  163.                options->gradient_unit_d);
  164.   digits = ((options->gradient_unit_d == GIMP_UNIT_PIXEL) ? 0 :
  165.         ((options->gradient_unit_d == GIMP_UNIT_PERCENT) ? 2 :
  166.          (MIN (6, MAX (3, gimp_unit_get_digits (options->gradient_unit_d))))));
  167.   spinbutton = gtk_object_get_data (GTK_OBJECT (options->gradient_unit_w), "set_digits");
  168.   gtk_spin_button_set_digits (GTK_SPIN_BUTTON (spinbutton), digits);
  169.  
  170.   options->gradient_type = options->gradient_type_d;
  171.  
  172.   gtk_option_menu_set_history (GTK_OPTION_MENU (options->gradient_type_w), 
  173.                    options->gradient_type_d);
  174. }
  175.  
  176. static PaintbrushOptions *
  177. paintbrush_options_new (void)
  178. {
  179.   PaintbrushOptions *options;
  180.  
  181.   GtkWidget *vbox;
  182.   GtkWidget *abox;
  183.   GtkWidget *table;
  184.   GtkWidget *type_label;
  185.   GtkWidget *spinbutton;
  186.  
  187.   /*  the new paint tool options structure  */
  188.   options = g_new (PaintbrushOptions, 1);
  189.   paint_options_init ((PaintOptions *) options,
  190.               PAINTBRUSH,
  191.               paintbrush_options_reset);
  192.  
  193.   options->use_fade        = 
  194.                   options->use_fade_d        = PAINTBRUSH_DEFAULT_USE_FADE;
  195.   options->fade_out        = 
  196.                   options->fade_out_d        = PAINTBRUSH_DEFAULT_FADE_OUT;
  197.   options->fade_unit        = 
  198.                   options->fade_unit_d       = PAINTBRUSH_DEFAULT_FADE_UNIT;
  199.   options->use_gradient    = 
  200.                   options->use_gradient_d    = PAINTBRUSH_DEFAULT_USE_GRADIENT;
  201.   options->gradient_length = 
  202.                   options->gradient_length_d = PAINTBRUSH_DEFAULT_GRADIENT_LENGTH;
  203.   options->gradient_unit        = 
  204.                   options->gradient_unit_d   = PAINTBRUSH_DEFAULT_GRADIENT_UNIT;
  205.   options->gradient_type   = 
  206.                   options->gradient_type_d   = PAINTBRUSH_DEFAULT_GRADIENT_TYPE;
  207.   
  208.   /*  the main vbox  */
  209.   vbox = ((ToolOptions *) options)->main_vbox;
  210.  
  211.   table = gtk_table_new (3, 3, FALSE);
  212.   gtk_table_set_col_spacing (GTK_TABLE (table), 0, 4);
  213.   gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
  214.   gtk_table_set_row_spacing (GTK_TABLE (table), 1, 3);
  215.   gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
  216.  
  217.   /*  the use fade toggle  */
  218.   abox = gtk_alignment_new (0.5, 1.0, 1.0, 0.0);
  219.   gtk_table_attach (GTK_TABLE (table), abox, 0, 1, 0, 1,
  220.             GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  221.   gtk_widget_show (abox);
  222.  
  223.   options->use_fade_w =
  224.     gtk_check_button_new_with_label (_("Fade Out"));
  225.   gtk_container_add (GTK_CONTAINER (abox), options->use_fade_w);
  226.   gtk_signal_connect (GTK_OBJECT (options->use_fade_w), "toggled",
  227.               GTK_SIGNAL_FUNC (gimp_toggle_button_update),
  228.               &options->use_fade);
  229.   gtk_widget_show (options->use_fade_w);
  230.  
  231.   /*  the fade-out sizeentry  */
  232.   options->fade_out_w =  
  233.     gtk_adjustment_new (options->fade_out_d,  1e-5, 32767.0, 1.0, 50.0, 0.0);
  234.   spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (options->fade_out_w), 1.0, 0.0);
  235.   gtk_spin_button_set_shadow_type (GTK_SPIN_BUTTON (spinbutton), GTK_SHADOW_NONE);
  236.   gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbutton), TRUE);
  237.   gtk_widget_set_usize (spinbutton, 75, 0);
  238.   gtk_signal_connect (GTK_OBJECT (options->fade_out_w), "value_changed",
  239.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  240.               &options->fade_out);
  241.   gtk_table_attach_defaults (GTK_TABLE (table), spinbutton, 1, 2, 0, 1);
  242.   gtk_widget_show (spinbutton);
  243.  
  244.   /*  the fade-out unitmenu  */
  245.   options->fade_unit_w = 
  246.     gimp_unit_menu_new ("%a", options->fade_unit_d, TRUE, TRUE, TRUE);
  247.   gtk_signal_connect (GTK_OBJECT (options->fade_unit_w), "unit_changed",
  248.               GTK_SIGNAL_FUNC (gimp_unit_menu_update),
  249.               &options->fade_unit);
  250.   gtk_object_set_data (GTK_OBJECT (options->fade_unit_w), "set_digits", spinbutton);
  251.   gtk_table_attach (GTK_TABLE (table), options->fade_unit_w, 2, 3, 0, 1,
  252.             GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  253.   gtk_widget_show (options->fade_unit_w);
  254.  
  255.   /*  automatically set the sensitive state of the fadeout stuff  */
  256.   gtk_widget_set_sensitive (spinbutton, options->use_fade_d);
  257.   gtk_widget_set_sensitive (options->fade_unit_w, options->use_fade_d);
  258.   gtk_object_set_data (GTK_OBJECT (options->use_fade_w),
  259.                "set_sensitive", spinbutton);
  260.   gtk_object_set_data (GTK_OBJECT (spinbutton),
  261.                "set_sensitive", options->fade_unit_w);
  262.  
  263.   /*  the use gradient toggle  */
  264.   abox = gtk_alignment_new (0.5, 1.0, 1.0, 0.0);
  265.   gtk_table_attach (GTK_TABLE (table), abox, 0, 1, 1, 2,
  266.             GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  267.   gtk_widget_show (abox);
  268.  
  269.   options->use_gradient_w =
  270.     gtk_check_button_new_with_label (_("Gradient"));
  271.   gtk_container_add (GTK_CONTAINER (abox), options->use_gradient_w);
  272.   gtk_signal_connect (GTK_OBJECT (options->use_gradient_w), "toggled",
  273.               GTK_SIGNAL_FUNC (paintbrush_gradient_toggle_callback),
  274.               &options->use_gradient);
  275.   gtk_widget_show (options->use_gradient_w);
  276.  
  277.   /*  the gradient length scale  */
  278.   options->gradient_length_w =  
  279.     gtk_adjustment_new (options->gradient_length_d,  1e-5, 32767.0, 1.0, 50.0, 0.0);
  280.   spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (options->gradient_length_w), 1.0, 0.0);
  281.   gtk_spin_button_set_shadow_type (GTK_SPIN_BUTTON (spinbutton), GTK_SHADOW_NONE);
  282.   gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbutton), TRUE);
  283.   gtk_widget_set_usize (spinbutton, 75, 0);
  284.   gtk_signal_connect (GTK_OBJECT (options->gradient_length_w), "value_changed",
  285.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  286.               &options->gradient_length);
  287.   gtk_table_attach_defaults (GTK_TABLE (table), spinbutton, 1, 2, 1, 2);
  288.   gtk_widget_show (spinbutton);
  289.  
  290.   /*  the gradient unitmenu  */
  291.   options->gradient_unit_w = 
  292.     gimp_unit_menu_new ("%a", options->gradient_unit_d, TRUE, TRUE, TRUE);
  293.   gtk_signal_connect (GTK_OBJECT (options->gradient_unit_w), "unit_changed",
  294.               GTK_SIGNAL_FUNC (gimp_unit_menu_update),
  295.               &options->gradient_unit);
  296.   gtk_object_set_data (GTK_OBJECT (options->gradient_unit_w), "set_digits",
  297.                spinbutton);
  298.   gtk_table_attach (GTK_TABLE (table), options->gradient_unit_w, 2, 3, 1, 2,
  299.             GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  300.   gtk_widget_show (options->gradient_unit_w);
  301.  
  302.   /*  the gradient type  */
  303.   type_label = gtk_label_new (_("Type:"));
  304.   gtk_misc_set_alignment (GTK_MISC (type_label), 1.0, 0.5);
  305.   gtk_table_attach (GTK_TABLE (table), type_label, 0, 1, 2, 3,
  306.             GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  307.   gtk_widget_show (type_label);
  308.  
  309.   abox = gtk_alignment_new (0.0, 0.5, 0.0, 0.0);
  310.   gtk_table_attach_defaults (GTK_TABLE (table), abox, 1, 3, 2, 3);
  311.   gtk_widget_show (abox);
  312.  
  313.   options->gradient_type_w = gimp_option_menu_new2
  314.     (FALSE, gimp_menu_item_update,
  315.      &options->gradient_type, (gpointer) options->gradient_type_d,
  316.  
  317.      _("Once Forward"),  (gpointer) ONCE_FORWARD, NULL,
  318.      _("Once Backward"), (gpointer) ONCE_BACKWARDS, NULL,
  319.      _("Loop Sawtooth"), (gpointer) LOOP_SAWTOOTH, NULL,
  320.      _("Loop Triangle"), (gpointer) LOOP_TRIANGLE, NULL,
  321.  
  322.      NULL);
  323.   gtk_container_add (GTK_CONTAINER (abox), options->gradient_type_w);
  324.   gtk_widget_show (options->gradient_type_w);
  325.  
  326.   gtk_widget_show (table);
  327.  
  328.   /*  automatically set the sensitive state of the gradient stuff  */
  329.   gtk_widget_set_sensitive (spinbutton, options->use_gradient_d);
  330.   gtk_widget_set_sensitive (spinbutton, options->use_gradient_d);
  331.   gtk_widget_set_sensitive (options->gradient_unit_w, options->use_gradient_d);
  332.   gtk_widget_set_sensitive (options->gradient_type_w, options->use_gradient_d);
  333.   gtk_widget_set_sensitive (type_label, options->use_gradient_d);
  334.   gtk_widget_set_sensitive (options->paint_options.incremental_w,
  335.                 ! options->use_gradient_d);
  336.   gtk_object_set_data (GTK_OBJECT (options->use_gradient_w), "set_sensitive",
  337.                spinbutton);
  338.   gtk_object_set_data (GTK_OBJECT (spinbutton), "set_sensitive",
  339.                options->gradient_unit_w);
  340.   gtk_object_set_data (GTK_OBJECT (options->gradient_unit_w), "set_sensitive",
  341.                options->gradient_type_w);
  342.   gtk_object_set_data (GTK_OBJECT (options->gradient_type_w), "set_sensitive",
  343.                type_label);
  344.   gtk_object_set_data (GTK_OBJECT (options->use_gradient_w), "inverse_sensitive",
  345.                options->paint_options.incremental_w);
  346.  
  347.   return options;
  348. }
  349.  
  350. #define TIMED_BRUSH 0
  351.  
  352. void *
  353. paintbrush_paint_func (PaintCore    *paint_core,
  354.                GimpDrawable *drawable,
  355.                int           state)
  356. {  
  357.   GDisplay *gdisp = gdisplay_active ();
  358.   double fade_out;
  359.   double gradient_length;    
  360.   double unit_factor;
  361.   
  362.   g_return_val_if_fail (gdisp != NULL, NULL);
  363.  
  364. #if TIMED_BRUSH
  365.   static GTimer *timer = NULL;
  366. #endif
  367.   switch (state)
  368.     {
  369.     case INIT_PAINT :
  370. #if TIMED_BRUSH
  371.       timer = g_timer_new();
  372.       g_timer_start(timer);
  373. #endif /* TIMED_BRUSH */
  374.       break;
  375.  
  376.     case MOTION_PAINT :
  377.       switch (paintbrush_options->fade_unit)
  378.     {
  379.     case GIMP_UNIT_PIXEL:
  380.       fade_out = paintbrush_options->fade_out;
  381.       break;
  382.     case GIMP_UNIT_PERCENT:
  383.       fade_out = MAX (gdisp->gimage->width, gdisp->gimage->height) * 
  384.         paintbrush_options->fade_out / 100;
  385.       break;
  386.     default:
  387.       unit_factor = gimp_unit_get_factor (paintbrush_options->fade_unit);
  388.       fade_out = paintbrush_options->fade_out * 
  389.         MAX (gdisp->gimage->xresolution, gdisp->gimage->yresolution) / unit_factor;
  390.       break;
  391.     }
  392.       
  393.       switch (paintbrush_options->gradient_unit)
  394.     {
  395.     case GIMP_UNIT_PIXEL:
  396.       gradient_length = paintbrush_options->gradient_length;
  397.       break;
  398.     case GIMP_UNIT_PERCENT:
  399.       gradient_length = MAX (gdisp->gimage->width, gdisp->gimage->height) * 
  400.         paintbrush_options->gradient_length / 100;
  401.       break;
  402.     default:
  403.       unit_factor = gimp_unit_get_factor (paintbrush_options->gradient_unit);
  404.       gradient_length = paintbrush_options->gradient_length * 
  405.         MAX (gdisp->gimage->xresolution, gdisp->gimage->yresolution) / unit_factor;
  406.       break;
  407.     }
  408.       
  409.       paintbrush_motion (paint_core, drawable, 
  410.              paintbrush_options->paint_options.pressure_options,
  411.              paintbrush_options->use_fade ? fade_out : 0, 
  412.              paintbrush_options->use_gradient ? gradient_length : 0,
  413.              paintbrush_options->paint_options.incremental,
  414.              paintbrush_options->gradient_type);
  415.       break;
  416.       
  417.     case FINISH_PAINT :
  418. #if TIMED_BRUSH
  419.       if (timer)
  420.       {
  421.     g_timer_stop(timer);
  422.     g_print ("painting took %f:\n", g_timer_elapsed(timer, NULL));
  423.     g_timer_destroy(timer);
  424.     timer = NULL;
  425.       }
  426. #endif /* TIMED_BRUSH */
  427.       break;
  428.  
  429.     default :
  430.       break;
  431.     }
  432.  
  433.   return NULL;
  434. }
  435.  
  436.  
  437. Tool *
  438. tools_new_paintbrush ()
  439. {
  440.   Tool * tool;
  441.   PaintCore * private;
  442.  
  443.   /*  The tool options  */
  444.   if (! paintbrush_options)
  445.     {
  446.       paintbrush_options = paintbrush_options_new ();
  447.       tools_register (PAINTBRUSH, (ToolOptions *) paintbrush_options);
  448.  
  449.       /*  press all default buttons  */
  450.       paintbrush_options_reset ();
  451.     }
  452.  
  453.   tool = paint_core_new (PAINTBRUSH);
  454.  
  455.   private = (PaintCore *) tool->private;
  456.   private->paint_func = paintbrush_paint_func;
  457.   private->pick_colors = TRUE;
  458.   private->flags |= TOOL_CAN_HANDLE_CHANGING_BRUSH;
  459.  
  460.   return tool;
  461. }
  462.  
  463.  
  464. void
  465. tools_free_paintbrush (Tool *tool)
  466. {
  467.   paint_core_free (tool);
  468. }
  469.  
  470.  
  471. static void
  472. paintbrush_motion (PaintCore            *paint_core,
  473.            GimpDrawable         *drawable,
  474.            PaintPressureOptions *pressure_options,
  475.            double                fade_out,
  476.            double                gradient_length,
  477.            PaintApplicationMode  incremental,
  478.            GradientPaintMode     gradient_type)
  479. {
  480.   GImage *gimage;
  481.   TempBuf * area;
  482.   gdouble x, paint_left;
  483.   gdouble position;
  484.   guchar local_blend = OPAQUE_OPACITY;
  485.   guchar temp_blend = OPAQUE_OPACITY;
  486.   guchar col[MAX_CHANNELS];
  487.   gdouble r,g,b,a;
  488.   gint mode;
  489.   gint opacity;
  490.   gdouble scale;
  491.   PaintApplicationMode paint_appl_mode = incremental ? INCREMENTAL : CONSTANT;
  492.  
  493.   position = 0.0;
  494.   if (! (gimage = drawable_gimage (drawable)))
  495.     return;
  496.  
  497.   if (pressure_options->size)
  498.     scale = paint_core->curpressure;
  499.   else
  500.     scale = 1.0;
  501.  
  502.   if (pressure_options->color)
  503.     gradient_length = 1.0; /* not really used, only for if cases */
  504.  
  505.   /*  Get a region which can be used to paint to  */
  506.   if (! (area = paint_core_get_paint_area (paint_core, drawable, scale)))
  507.     return;
  508.  
  509.   /*  factor in the fade out value  */
  510.   if (fade_out)
  511.     {
  512.       /*  Model the amount of paint left as a gaussian curve  */
  513.       x = ((double) paint_core->pixel_dist / fade_out);
  514.       paint_left = exp (- x * x * 5.541);    /*  ln (1/255)  */
  515.       local_blend = (int) (255 * paint_left);
  516.     }
  517.  
  518.   if (local_blend)
  519.     {
  520.       /*  set the alpha channel  */
  521.       temp_blend = local_blend;
  522.       mode = gradient_type;
  523.  
  524.       if (gradient_length)
  525.     {
  526.       if (pressure_options->color)
  527.         gradient_get_color_at (gimp_context_get_gradient (NULL),
  528.                    paint_core->curpressure, &r, &g, &b, &a);
  529.       else
  530.         paint_core_get_color_from_gradient (paint_core, gradient_length, 
  531.                         &r, &g, &b, &a, mode);
  532.       r = r * 255.0;
  533.       g = g * 255.0;
  534.       b = b * 255.0;
  535.        a = a * 255.0;
  536.       temp_blend =  (gint)((a * local_blend) / 255);
  537.       col[0] = (gint)r;
  538.       col[1] = (gint)g;
  539.       col[2] = (gint)b;
  540.       col[3] = OPAQUE_OPACITY;
  541.       /* always use incremental mode with gradients */
  542.       /* make the gui cool later */
  543.       paint_appl_mode = INCREMENTAL;
  544.       color_pixels (temp_buf_data (area), col,
  545.             area->width * area->height, area->bytes);
  546.     }
  547.       /* we check to see if this is a pixmap, if so composite the
  548.      pixmap image into the are instead of the color */
  549.       else if (paint_core->brush && paint_core->brush->pixmap)
  550.     {
  551.       paint_core_color_area_with_pixmap (paint_core, gimage, drawable, 
  552.                          area, 
  553.                          scale, SOFT);
  554.       paint_appl_mode = INCREMENTAL;
  555.     }
  556.       else 
  557.     {
  558.       gimage_get_foreground (gimage, drawable, col);
  559.       col[area->bytes - 1] = OPAQUE_OPACITY;
  560.       color_pixels (temp_buf_data (area), col,
  561.             area->width * area->height, area->bytes);
  562.     }
  563.  
  564.       opacity = (gdouble)temp_blend;
  565.       if (pressure_options->opacity)
  566.     opacity = opacity * 2.0 * paint_core->curpressure;
  567.  
  568.       paint_core_paste_canvas (paint_core, drawable, 
  569.                    MIN (opacity, 255), 
  570.                    gimp_context_get_opacity (NULL) * 255,
  571.                    gimp_context_get_paint_mode (NULL),
  572.                    pressure_options->pressure ? PRESSURE : SOFT,
  573.                    scale, paint_appl_mode);
  574.     }
  575. }
  576.  
  577.  
  578. static void *
  579. paintbrush_non_gui_paint_func (PaintCore    *paint_core,
  580.                    GimpDrawable *drawable,
  581.                    int           state)
  582. {
  583.   GImage *gimage;
  584.   double fade_out;
  585.   double gradient_length;    
  586.   double unit_factor;
  587.  
  588.   if (! (gimage = drawable_gimage (drawable)))
  589.     return NULL;
  590.  
  591.   switch (non_gui_fade_unit)
  592.     {
  593.     case GIMP_UNIT_PIXEL:
  594.       fade_out = non_gui_fade_out;
  595.       break;
  596.     case GIMP_UNIT_PERCENT:
  597.       fade_out = MAX (gimage->width, gimage->height) * 
  598.     non_gui_fade_out / 100;
  599.       break;
  600.     default:
  601.       unit_factor = gimp_unit_get_factor (non_gui_fade_unit);
  602.       fade_out = non_gui_fade_out * 
  603.     MAX (gimage->xresolution, gimage->yresolution) / unit_factor;
  604.       break;
  605.     }
  606.   
  607.   switch (non_gui_gradient_unit)
  608.     {
  609.     case GIMP_UNIT_PIXEL:
  610.       gradient_length = non_gui_gradient_length;
  611.       break;
  612.     case GIMP_UNIT_PERCENT:
  613.       gradient_length = MAX (gimage->width, gimage->height) * 
  614.     non_gui_gradient_length / 100;
  615.       break;
  616.     default:
  617.       unit_factor = gimp_unit_get_factor (non_gui_gradient_unit);
  618.       gradient_length = non_gui_gradient_length * 
  619.     MAX (gimage->xresolution, gimage->yresolution) / unit_factor;
  620.       break;
  621.     }
  622.  
  623.   paintbrush_motion (paint_core,drawable, 
  624.              &non_gui_pressure_options,
  625.              fade_out,
  626.              gradient_length,
  627.              non_gui_incremental, 
  628.              non_gui_gradient_type);
  629.  
  630.   return NULL;
  631. }
  632.  
  633. gboolean
  634. paintbrush_non_gui_default (GimpDrawable *drawable,
  635.                 int            num_strokes,
  636.                 double        *stroke_array)
  637. {
  638.   PaintbrushOptions *options = paintbrush_options;
  639.   gdouble            fade_out        = PAINTBRUSH_DEFAULT_FADE_OUT;
  640.   gboolean           incremental     = PAINTBRUSH_DEFAULT_INCREMENTAL;
  641.   gboolean           use_gradient    = PAINTBRUSH_DEFAULT_USE_GRADIENT;
  642.   gboolean           use_fade        = PAINTBRUSH_DEFAULT_USE_FADE;
  643.   gdouble            gradient_length = PAINTBRUSH_DEFAULT_GRADIENT_LENGTH;
  644.   gint               gradient_type   = PAINTBRUSH_DEFAULT_GRADIENT_TYPE;
  645.   GimpUnit           fade_unit       = PAINTBRUSH_DEFAULT_FADE_UNIT;
  646.   GimpUnit           gradient_unit   = PAINTBRUSH_DEFAULT_GRADIENT_UNIT;
  647.   gint               i;
  648.  
  649.   if (options)
  650.     {
  651.       fade_out        = options->fade_out;
  652.       incremental     = options->paint_options.incremental;
  653.       use_gradient    = options->use_gradient;
  654.       gradient_length = options->gradient_length;
  655.       gradient_type   = options->gradient_type;
  656.       use_fade        = options->use_fade;
  657.       fade_unit       = options->fade_unit;
  658.       gradient_unit   = options->gradient_unit;
  659.     }
  660.  
  661.   if (use_gradient == FALSE)
  662.     gradient_length = 0.0;
  663.  
  664.   if (use_fade == FALSE)
  665.     fade_out = 0.0;
  666.  
  667.   /* Hmmm... PDB paintbrush should have gradient type added to it!*/
  668.   /* thats why the code below is duplicated.
  669.    */ 
  670.   if (paint_core_init (&non_gui_paint_core, drawable,
  671.                stroke_array[0], stroke_array[1]))
  672.     {
  673.       non_gui_fade_out        = fade_out;
  674.       non_gui_gradient_length = gradient_length;
  675.       non_gui_gradient_type   = gradient_type;
  676.       non_gui_incremental     = incremental;
  677.       non_gui_fade_unit       = fade_unit;
  678.       non_gui_gradient_unit   = gradient_unit;
  679.  
  680.       /* Set the paint core's paint func */
  681.       non_gui_paint_core.paint_func = paintbrush_non_gui_paint_func;
  682.  
  683.       non_gui_paint_core.startx = non_gui_paint_core.lastx = stroke_array[0];
  684.       non_gui_paint_core.starty = non_gui_paint_core.lasty = stroke_array[1];
  685.  
  686.       non_gui_paint_core.flags |= TOOL_CAN_HANDLE_CHANGING_BRUSH;
  687.  
  688.       paintbrush_non_gui_paint_func (&non_gui_paint_core, drawable, 0);
  689.  
  690.       for (i = 1; i < num_strokes; i++)
  691.        {
  692.          non_gui_paint_core.curx = stroke_array[i * 2 + 0];
  693.          non_gui_paint_core.cury = stroke_array[i * 2 + 1];
  694.  
  695.          paint_core_interpolate (&non_gui_paint_core, drawable);
  696.  
  697.          non_gui_paint_core.lastx = non_gui_paint_core.curx;
  698.          non_gui_paint_core.lasty = non_gui_paint_core.cury;
  699.        }
  700.  
  701.       /* Finish the painting */
  702.       paint_core_finish (&non_gui_paint_core, drawable, -1);
  703.  
  704.       /* Cleanup */
  705.       paint_core_cleanup ();
  706.       return TRUE;
  707.     }
  708.   else
  709.     return FALSE;
  710. }
  711.  
  712. gboolean
  713. paintbrush_non_gui (GimpDrawable *drawable,
  714.                    int            num_strokes,
  715.                    double        *stroke_array,
  716.                    double         fade_out,
  717.                    int            method,
  718.                    double         gradient_length)
  719. {
  720.   int i;
  721.  
  722.   /* Code duplicated above */
  723.   if (paint_core_init (&non_gui_paint_core, drawable,
  724.                       stroke_array[0], stroke_array[1]))
  725.     {
  726.       non_gui_fade_out        = fade_out;
  727.       non_gui_gradient_length = gradient_length;
  728.       non_gui_gradient_type   = LOOP_TRIANGLE;
  729.       non_gui_incremental     = method;
  730.  
  731.       /* Set the paint core's paint func */
  732.       non_gui_paint_core.paint_func = paintbrush_non_gui_paint_func;
  733.  
  734.       non_gui_paint_core.startx = non_gui_paint_core.lastx = stroke_array[0];
  735.       non_gui_paint_core.starty = non_gui_paint_core.lasty = stroke_array[1];
  736.  
  737.       non_gui_paint_core.flags |= TOOL_CAN_HANDLE_CHANGING_BRUSH;
  738.  
  739.       if (num_strokes == 1)
  740.        paintbrush_non_gui_paint_func (&non_gui_paint_core, drawable, 0);
  741.  
  742.       for (i = 1; i < num_strokes; i++)
  743.        {
  744.          non_gui_paint_core.curx = stroke_array[i * 2 + 0];
  745.          non_gui_paint_core.cury = stroke_array[i * 2 + 1];
  746.  
  747.          paint_core_interpolate (&non_gui_paint_core, drawable);
  748.  
  749.          non_gui_paint_core.lastx = non_gui_paint_core.curx;
  750.          non_gui_paint_core.lasty = non_gui_paint_core.cury;
  751.        }
  752.  
  753.       /* Finish the painting */
  754.       paint_core_finish (&non_gui_paint_core, drawable, -1);
  755.  
  756.       /* Cleanup */
  757.       paint_core_cleanup ();
  758.       return TRUE;
  759.     }
  760.   else
  761.     return FALSE;
  762. }
  763.