home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / app / xinput_airbrush.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-01  |  78.9 KB  |  3,272 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. #include "config.h"
  19.  
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <stdio.h>
  23.  
  24. #include "appenv.h"
  25.  
  26. #include "airbrush_blob.h"
  27. #include "drawable.h"
  28. #include "draw_core.h"
  29. #include "gdisplay.h"
  30. #include "gimage_mask.h"
  31. #include "gimprc.h"
  32. #include "gimpui.h"
  33. #include "paint_options.h"
  34. #include "tools.h"
  35. #include "undo.h"
  36. #include "xinput_airbrush.h"
  37.  
  38. #include "libgimp/gimpintl.h"
  39. #include "libgimp/gimpmath.h"
  40.  
  41. #include "tile.h"            /* ick. */
  42.  
  43.  
  44. #define SUBSAMPLE 8
  45.  
  46. #define DIST_SMOOTHER_BUFFER 10
  47. #define TIME_SMOOTHER_BUFFER 10
  48.  
  49. /*  the XinputAirbrush structures  */
  50.  
  51. typedef struct _XinputAirbrushTool XinputAirbrushTool;
  52. struct _XinputAirbrushTool
  53.   {
  54.     DrawCore * core;         /*  Core select object             */
  55.  
  56.     AirBrush         * last_airbrush;
  57.     AirBrushBlob     * last_airbrush_blob;       /*  airbrush_blob for last cursor position  */
  58.     AirBlob          * last_airblob;
  59.  
  60.  
  61.     int        x1, y1;       /*  image space coordinate         */
  62.     int        x2, y2;       /*  image space coords             */
  63.  
  64.     /* circular distance history buffer */
  65.     gdouble    dt_buffer[DIST_SMOOTHER_BUFFER];
  66.     gint       dt_index;
  67.  
  68.     /* circular timing history buffer */
  69.     guint32    ts_buffer[TIME_SMOOTHER_BUFFER];
  70.     gint       ts_index;
  71.  
  72.     /* Direction and center point */
  73.     double     xcenter, ycenter;
  74.     double     direction_abs;
  75.     double     direction;
  76.     double     c_direction;
  77.     double     c_direction_abs;
  78.  
  79.  
  80.     gdouble    last_time;    /*  previous time of a motion event      */
  81.     gdouble    lastx, lasty; /*  previous position of a motion event  */
  82.     gdouble    last_lastx, last_lasty; /* The previous previous position of a motion event i.e pos of the blob */
  83.  
  84.     gboolean   init_velocity;
  85.     gboolean   init_prepre;
  86.     
  87.     double     last_value;
  88.  
  89.   };
  90.  
  91.  
  92. typedef struct _XinputAirbrushOptions XinputAirbrushOptions;
  93. struct _XinputAirbrushOptions
  94.   {
  95.     PaintOptions  paint_options;
  96.  
  97.     double        flow;
  98.     double        flow_d;
  99.     GtkObject    *flow_w;
  100.  
  101.     double        sensitivity;
  102.     double        sensitivity_d;
  103.     GtkObject    *sensitivity_w;
  104.  
  105.     double        starttilt;
  106.     double        starttilt_d;
  107.     GtkObject    *starttilt_w;
  108.  
  109.     double        vel_sensitivity;
  110.     double        vel_sensitivity_d;
  111.     GtkObject    *vel_sensitivity_w;
  112.  
  113.     double        tilt_sensitivity;
  114.     double        tilt_sensitivity_d;
  115.     GtkObject    *tilt_sensitivity_w;
  116.  
  117. #ifdef GTK_HAVE_SIX_VALUATORS
  118.     double        minheight;
  119.     double        minheight_d;
  120.     GtkObject    *minheight_w;
  121.  
  122.  
  123.     double        maxheight;
  124.     double        maxheight_d;
  125.     GtkObject    *maxheight_w;
  126. #else /* !GTK_HAVE_SIX_VALUATORS */
  127.     double        height;
  128.     double        height_d;
  129.     GtkObject    *height_w;
  130. #endif /* GTK_HAVE_SIX_VALUATORS */
  131.  
  132.   };
  133.  
  134.  
  135. /* the xinput_airbrush tool options  */
  136. static XinputAirbrushOptions *xinput_airbrush_options = NULL;
  137.  
  138. /* local variables */
  139.  
  140. /*  undo blocks variables  */
  141. static TileManager *  undo_tiles = NULL;
  142.  
  143. /* Tiles used to render the stroke at 1 byte/pp */
  144. static TileManager *  canvas_tiles = NULL;
  145.  
  146. /* Flat buffer that is used to used to render the dirty region
  147.  * for composition onto the destination drawable
  148.  */
  149. static TempBuf *  canvas_buf = NULL;
  150.  
  151.  
  152. /*  local function prototypes  */
  153.  
  154. static void   xinput_airbrush_button_press   (Tool *, GdkEventButton *, gpointer);
  155. static void   xinput_airbrush_button_release (Tool *, GdkEventButton *, gpointer);
  156. static void   xinput_airbrush_motion         (Tool *, GdkEventMotion *, gpointer);
  157. static void   xinput_airbrush_cursor_update  (Tool *, GdkEventMotion *, gpointer);
  158. static void   xinput_airbrush_control        (Tool *, ToolAction,       gpointer);
  159.  
  160. static void    time_smoother_add    (XinputAirbrushTool* xinput_airbrush_tool, guint32 value);
  161. static gdouble time_smoother_result (XinputAirbrushTool* xinput_airbrush_tool);
  162. static void    time_smoother_init   (XinputAirbrushTool* xinput_airbrush_tool, guint32 initval);
  163. static void    dist_smoother_add    (XinputAirbrushTool* xinput_airbrush_tool, gdouble value);
  164. static gdouble dist_smoother_result (XinputAirbrushTool* xinput_airbrush_tool);
  165. static void    dist_smoother_init   (XinputAirbrushTool* xinput_airbrush_tool, gdouble initval);
  166.  
  167. static void xinput_airbrush_init   (XinputAirbrushTool      *xinput_airbrush_tool,
  168.                                     GimpDrawable *drawable,
  169.                                     double        x,
  170.                     double        y);
  171.  
  172. static void xinput_airbrush_finish (XinputAirbrushTool      *xinput_airbrush_tool,
  173.                                     GimpDrawable *drawable,
  174.                                     int           tool_id);
  175.  
  176. static void xinput_airbrush_cleanup (void);
  177.  
  178.  
  179. /*Mask functions*/
  180.  
  181. static void calc_angle (AirBrushBlob *airbrush_blob,
  182.             double xcenter,
  183.             double ycenter);
  184.  
  185. static void calc_width (AirBrushBlob *airbrush_blob);
  186.  
  187.  
  188. static void render_airbrush_line (AirBrushBlob *airbrush_blob,
  189.                                   guchar *dest,
  190.                                   int y,
  191.                                   int width,
  192.                                   XinputAirbrushTool *xinput_airbrush_tool);
  193.  
  194.  
  195.  
  196. static void make_single_mask (AirBrushBlob *airbrush_blob,
  197.                                 XinputAirbrushTool *xinput_airbrush_tool,
  198.                                 MaskBuf *dest);
  199.  
  200. static void make_stroke(AirBlob *airblob,
  201.             XinputAirbrushTool *xinput_airbrush_tool,
  202.             GimpDrawable *drawable,
  203.             guint last_value,
  204.             guint present_value);
  205.  
  206.  
  207.  
  208. static void make_mask(AirLine *airline,
  209.               MaskBuf *brush_mask,
  210.               guint value);
  211.  
  212.  
  213.  
  214.  
  215. static void print_mask(MaskBuf *dest);
  216.  
  217.  
  218.  
  219.  
  220. /*  Rendering functions  */
  221.  
  222. static void xinput_airbrush_set_paint_area  (XinputAirbrushTool      *xinput_airbrush_tool,
  223.                          GimpDrawable *drawable,
  224.                          int x,
  225.                          int y,
  226.                          int width,
  227.                          int height);
  228.  
  229. static void xinput_airbrush_paste (XinputAirbrushTool      *xinput_airbrush_tool,
  230.                    GimpDrawable *drawable,
  231.                    MaskBuf *brush_mask,
  232.                    int x,
  233.                    int y,
  234.                    int width,
  235.                    int height);
  236.  
  237. static void xinput_airbrush_to_canvas_tiles (XinputAirbrushTool      *xinput_airbrush_tool,
  238.                          MaskBuf *brush_mask,
  239.                          int brush_opacity);
  240.  
  241. static void xinput_airbrush_canvas_tiles_to_canvas_buf(XinputAirbrushTool *xinput_airbrush_tool);
  242.  
  243. static void xinput_airbrush_set_undo_tiles  (GimpDrawable *drawable,
  244.                          int           x,
  245.                          int           y,
  246.                          int           w,
  247.                          int           h);
  248.  
  249. static void xinput_airbrush_set_canvas_tiles(int           x,
  250.                          int           y,
  251.                          int           w,
  252.                          int           h);
  253.  
  254.  
  255.  
  256. /* Start of functions */
  257.  
  258.  
  259. static void
  260.   xinput_airbrush_options_reset (void)
  261.   {
  262.     XinputAirbrushOptions *options = xinput_airbrush_options;
  263.  
  264.     paint_options_reset ((PaintOptions *) options);
  265.  
  266.     gtk_adjustment_set_value (GTK_ADJUSTMENT (options->flow_w),
  267.                               options->flow_d);
  268.     gtk_adjustment_set_value (GTK_ADJUSTMENT (options->sensitivity_w),
  269.                               options->sensitivity_d);
  270.     gtk_adjustment_set_value (GTK_ADJUSTMENT (options->starttilt_w),
  271.                   options->starttilt_d);
  272.     gtk_adjustment_set_value (GTK_ADJUSTMENT (options->tilt_sensitivity_w),
  273.                               options->tilt_sensitivity_d);
  274.     gtk_adjustment_set_value (GTK_ADJUSTMENT (options->vel_sensitivity_w),
  275.                               options->vel_sensitivity_d);
  276. #ifdef GTK_HAVE_SIX_VALUATORS
  277.     gtk_adjustment_set_value (GTK_ADJUSTMENT (options->minheight_w),
  278.                               options->minheight_d);
  279.     gtk_adjustment_set_value (GTK_ADJUSTMENT (options->maxheight_w),
  280.                               options->maxheight_d);
  281. #else /* !GTK_HAVE_SIX_VALUATORS */
  282.     gtk_adjustment_set_value (GTK_ADJUSTMENT (options->height_w),
  283.                               options->height_d);
  284. #endif /* GTK_HAVE_SIX_VALUATORS */
  285.  
  286.   }
  287.  
  288. static XinputAirbrushOptions *
  289.   xinput_airbrush_options_new (void)
  290.   {
  291.     XinputAirbrushOptions *options;
  292.  
  293.     GtkWidget *table;
  294.     GtkWidget *abox;
  295.     GtkWidget *label;
  296.     GtkWidget *slider;
  297.  
  298.     /*  the new xinput_airbrush tool options structure  */
  299.     options = (XinputAirbrushOptions *) g_malloc (sizeof (XinputAirbrushOptions));
  300.     paint_options_init ((PaintOptions *) options,
  301.                         XINPUT_AIRBRUSH,
  302.                         xinput_airbrush_options_reset);
  303.     options->flow               = options->flow_d                = 100;
  304.     options->sensitivity        = options->sensitivity_d         = 1.0;
  305.     options->starttilt          = options->starttilt_d           = 90;
  306.     options->vel_sensitivity    = options->vel_sensitivity_d     = 0.8;
  307.     options->tilt_sensitivity   = options->tilt_sensitivity_d    = 0.4;
  308. #ifdef GTK_HAVE_SIX_VALUATORS
  309.     options->minheight          = options->minheight_d           = 25.0;
  310.     options->maxheight          = options->maxheight_d           = 50.0;
  311. #else /* !GTK_HAVE_SIX_VALUATORS */
  312.     options->height             = options->height_d              = 45.0;
  313. #endif /* GTK_HAVE_SIX_VALUATORS */
  314.  
  315.     /*the main table*/  
  316.  
  317.     table = gtk_table_new (6, 2, FALSE);
  318.     gtk_box_pack_start (GTK_BOX (((ToolOptions *) options)->main_vbox), table,
  319.                         FALSE, FALSE, 0);
  320.  
  321.     /*flow slider*/ 
  322.     label = gtk_label_new (_("Flow Relation:"));
  323.     gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
  324.     gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1,
  325.                       GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  326.     gtk_widget_show (label);
  327.  
  328.     options->flow_w =
  329.       gtk_adjustment_new (options->flow_d, 50.0, 201.0, 1.0, 10.0, 1.0);
  330.     slider = gtk_hscale_new (GTK_ADJUSTMENT (options->flow_w));
  331.     gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  332.     gtk_table_attach_defaults (GTK_TABLE (table), slider, 1, 2, 0, 1);
  333.     gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  334.     gtk_signal_connect (GTK_OBJECT (options->flow_w), "value_changed",
  335.                         GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  336.                         &options->flow);
  337.     gtk_widget_show (slider);
  338.     gtk_scale_set_digits (GTK_SCALE (slider), 0);
  339.  
  340.     /*flow sens slider*/
  341.     label = gtk_label_new (_("Flow Sensitivity:"));
  342.     gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
  343.     gtk_table_attach (GTK_TABLE (table), label, 0, 1, 1, 2,
  344.                       GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  345.     gtk_widget_show (label);
  346.  
  347.     options->sensitivity_w =
  348.       gtk_adjustment_new (options->sensitivity_d, 0.0, 1.0, 0.01, 0.1, 0.0);
  349.     slider = gtk_hscale_new (GTK_ADJUSTMENT (options->sensitivity_w));
  350.     gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  351.     gtk_table_attach_defaults (GTK_TABLE (table), slider, 1, 2, 1, 2);
  352.     gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  353.     gtk_signal_connect (GTK_OBJECT (options->sensitivity_w), "value_changed",
  354.                         GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  355.                         &options->sensitivity);
  356.     gtk_widget_show (slider);
  357.  
  358.  
  359.     /*base tilt slider*/
  360.  
  361.     label = gtk_label_new (_("Base Tilt:"));
  362.     gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
  363.     gtk_table_attach (GTK_TABLE (table), label, 0, 1, 2, 3,
  364.                       GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  365.     gtk_widget_show (label);
  366.  
  367.     options->starttilt_w =
  368.       gtk_adjustment_new (options->starttilt_d, 30.0 , 91.0, 1.0, 1.0, 1.0);
  369.     slider = gtk_hscale_new (GTK_ADJUSTMENT (options->starttilt_w));
  370.     gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  371.     gtk_table_attach_defaults (GTK_TABLE (table), slider, 1, 2, 2, 3);
  372.     gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  373.     gtk_signal_connect (GTK_OBJECT (options->starttilt_w), "value_changed",
  374.                         GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  375.                         &options->starttilt);
  376.     gtk_widget_show (slider);
  377.     gtk_scale_set_digits (GTK_SCALE (slider), 0);
  378.     
  379.  
  380.  
  381.     /*tilt sens slider*/
  382.  
  383.  
  384.     label = gtk_label_new (_("Tilt Sensitivity:"));
  385.     gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
  386.     gtk_table_attach (GTK_TABLE (table), label, 0, 1, 3, 4,
  387.                       GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  388.     gtk_widget_show (label);
  389.  
  390.     abox = gtk_alignment_new (0.5, 1.0, 1.0, 0.0);
  391.     gtk_table_attach (GTK_TABLE (table), abox, 1, 2, 3, 4,
  392.                       GTK_EXPAND | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  393.     gtk_widget_show (abox);
  394.  
  395.     options->tilt_sensitivity_w =
  396.       gtk_adjustment_new (options->tilt_sensitivity_d, 0.0, 1.0, 0.01, 0.1, 0.0);
  397.     slider = gtk_hscale_new (GTK_ADJUSTMENT (options->tilt_sensitivity_w));
  398.     gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  399.     gtk_container_add (GTK_CONTAINER (abox), slider);
  400.     gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  401.     gtk_signal_connect (GTK_OBJECT (options->tilt_sensitivity_w), "value_changed",
  402.                         GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  403.                         &options->tilt_sensitivity);
  404.     gtk_widget_show (slider);
  405.  
  406.  
  407.     /*velocity sens slider*/
  408.  
  409.  
  410.     label = gtk_label_new (_("Speed Sensitivity:"));
  411.     gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
  412.     gtk_table_attach (GTK_TABLE (table), label, 0, 1, 5, 6,
  413.                       GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  414.     gtk_widget_show (label);
  415.  
  416.     abox = gtk_alignment_new (0.5, 1.0, 1.0, 0.0);
  417.     gtk_table_attach (GTK_TABLE (table), abox, 1, 2, 5, 6,
  418.                       GTK_EXPAND | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  419.     gtk_widget_show (abox);
  420.  
  421.     options->vel_sensitivity_w =
  422.       gtk_adjustment_new (options->vel_sensitivity_d, 0.0, 1.0, 0.01, 0.1, 0.0);
  423.     slider = gtk_hscale_new (GTK_ADJUSTMENT (options->vel_sensitivity_w));
  424.     gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  425.     gtk_container_add (GTK_CONTAINER (abox), slider);
  426.     gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  427.     gtk_signal_connect (GTK_OBJECT (options->vel_sensitivity_w), "value_changed",
  428.                         GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  429.                         &options->vel_sensitivity);
  430.     gtk_widget_show (slider);
  431.  
  432. #ifdef GTK_HAVE_SIX_VALUATORS
  433.  
  434.  
  435.     /*min height slider*/
  436.  
  437.     label = gtk_label_new (_("Min Height:"));
  438.     gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
  439.     gtk_table_attach (GTK_TABLE (table), label, 0, 1, 7, 8,
  440.                       GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  441.     gtk_widget_show (label);
  442.  
  443.     options->minheight_w =
  444.       gtk_adjustment_new (options->minheight_d, 25.0 , 41.0, 1.0, 1.0, 1.0);
  445.     slider = gtk_hscale_new (GTK_ADJUSTMENT (options->minheight_w));
  446.     gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  447.     gtk_table_attach_defaults (GTK_TABLE (table), slider, 1, 2, 7, 8);
  448.     gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  449.     gtk_signal_connect (GTK_OBJECT (options->minheight_w), "value_changed",
  450.                         GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  451.                         &options->minheight);
  452.     gtk_widget_show (slider);
  453.     gtk_scale_set_digits (GTK_SCALE (slider), 0);
  454.  
  455.  
  456.     /*max height slider*/
  457.  
  458.     label = gtk_label_new (_("Max Height:"));
  459.     gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
  460.     gtk_table_attach (GTK_TABLE (table), label, 0, 1, 9, 10,
  461.                       GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  462.     gtk_widget_show (label);
  463.  
  464.     options->maxheight_w =
  465.       gtk_adjustment_new (options->maxheight_d, 41.0 , 81.0, 1.0, 1.0, 1.0);
  466.     slider = gtk_hscale_new (GTK_ADJUSTMENT (options->maxheight_w));
  467.     gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  468.     gtk_table_attach_defaults (GTK_TABLE (table), slider, 1, 2, 9, 10);
  469.     gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  470.     gtk_signal_connect (GTK_OBJECT (options->maxheight_w), "value_changed",
  471.                         GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  472.                         &options->maxheight);
  473.     gtk_widget_show (slider);
  474.     gtk_scale_set_digits (GTK_SCALE (slider), 0);
  475.  
  476.  
  477. #else /* !GTK_HAVE_SIX_VALUATORS */
  478.  
  479.     label = gtk_label_new (_("Height:"));
  480.     gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
  481.     gtk_table_attach (GTK_TABLE (table), label, 0, 1, 7, 8,
  482.                       GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  483.     gtk_widget_show (label);
  484.  
  485.     options->height_w =
  486.       gtk_adjustment_new (options->height_d, 25.0 , 81.0, 1.0, 1.0, 1.0);
  487.     slider = gtk_hscale_new (GTK_ADJUSTMENT (options->height_w));
  488.     gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  489.     gtk_table_attach_defaults (GTK_TABLE (table), slider, 1, 2, 7, 8);
  490.     gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  491.     gtk_signal_connect (GTK_OBJECT (options->height_w), "value_changed",
  492.                         GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  493.                         &options->height);
  494.     gtk_widget_show (slider);
  495.     gtk_scale_set_digits (GTK_SCALE (slider), 0);
  496.  
  497. #endif /* GTK_HAVE_SIX_VALUATORS */
  498.  
  499.     gtk_widget_show_all (table);
  500.  
  501.     return options;
  502.   }
  503.  
  504.  
  505. static AirBlob *
  506. #ifdef GTK_HAVE_SIX_VALUATORS
  507. xinput_airbrush_pen_ellipse (XinputAirbrushTool      *xinput_airbrush_tool, 
  508.                  gdouble x_center, gdouble y_center,
  509.                  gdouble pressure, gdouble xtiltv,
  510.                  gdouble ytiltv, gdouble wheel)
  511. #else /* !GTK_HAVE_SIX_VALUATORS */
  512. xinput_airbrush_pen_ellipse (XinputAirbrushTool      *xinput_airbrush_tool, 
  513.                  gdouble x_center, gdouble y_center,
  514.                  gdouble pressure, gdouble xtiltv,
  515.                  gdouble ytiltv)
  516. #endif /* GTK_HAVE_SIX_VALUATORS */
  517. {
  518.  
  519.     gdouble xtilt, ytilt;
  520.     double height;
  521. #ifdef GTK_HAVE_SIX_VALUATORS
  522.     double inter_height;
  523.     double min_height;
  524.     double max_height;
  525. #endif /* GTK_HAVE_SIX_VALUATORS */
  526.     double tanx, tany;
  527.     double tanytop, tanxright, tanybot, tanxleft;
  528.     double sprayangle;
  529.     double distx, disty;
  530.     double ytop, xright, ybot, xleft;
  531.  
  532.  
  533.  
  534.  
  535.  
  536.     /* Virtual height over the drawing */
  537.  
  538. #ifdef GTK_HAVE_SIX_VALUATORS
  539.     min_height=xinput_airbrush_options->minheight;
  540.     max_height=xinput_airbrush_options->maxheight;
  541.  
  542.     inter_height = CLAMP(wheel, 0.0, 1.0) * (max_height - min_height);
  543.     height = min_height + inter_height;
  544.  
  545.     /*
  546.       printf("Height: %f\n", height);
  547.     */
  548.  
  549. #else /* !GTK_HAVE_SIX_VALUATORS */
  550.     height = xinput_airbrush_options->height;
  551. #endif /* GTK_HAVE_SIX_VALUATORS */
  552.  
  553.  
  554.     /*
  555.  
  556.       Fix av tilt negative when it should be positive
  557.       Also a clamp of to big tilts compared to the height
  558.       i.e you will not get big brushes
  559.  
  560.     */
  561.  
  562.     xtilt = xtiltv * -1.0 * (1 - ((0.5 * (height - 15))/100));
  563.     ytilt = ytiltv * -1.0 * (1 - ((0.5 * (height - 15))/100));
  564.  
  565.  
  566.  
  567.     /* The airflow controls the spray angel 
  568.        High airflow renders in a big spray angel
  569.        but if the inkflow is low the blob will be 
  570.        very thin.
  571.     */
  572.  
  573.     sprayangle = G_PI/6 * (xinput_airbrush_options->flow/100.0) * MAX(pressure, 0.1);
  574.  
  575.     sprayangle = MAX(sprayangle, 0.0);
  576.  
  577.     /*printf("Angle: %f\n", sprayangle);*/
  578.  
  579.     /*Tan of x and y tilt plus spray angles x r/l and y t/b tan*/
  580.  
  581.     tanx = tan(xtilt * G_PI / 2.0);
  582.     tany = tan(ytilt * G_PI / 2.0);
  583.  
  584.  
  585.     tanytop = tan((ytilt * G_PI / 2.0) + (sprayangle/2));
  586.     tanxright = tan((xtilt * G_PI / 2.0) +  (sprayangle/2));
  587.     tanybot = tan((ytilt * G_PI / 2.0) - (sprayangle/2));
  588.     tanxleft = tan((xtilt * G_PI / 2.0) - (sprayangle/2));
  589.  
  590.     /* Offset from cursor due to tilt in x and y  depening on the hight*/
  591.  
  592.     distx = tanx * height;
  593.     disty = tany * height;
  594.  
  595.     /* Offset from center of blob in all for axies */
  596.  
  597.     ytop =  fabs(fabs(tanytop * height) - fabs(disty));
  598.     xright = fabs(fabs(tanxright * height) - fabs(distx));
  599.     ybot = fabs(fabs(tanybot * height) - fabs(disty));
  600.     xleft = fabs(fabs(tanxleft * height) - fabs(distx));
  601.  
  602.     x_center = x_center + distx;
  603.     y_center = y_center + disty;
  604.  
  605.  
  606.     xinput_airbrush_tool->xcenter=x_center;
  607.     xinput_airbrush_tool->ycenter=y_center;
  608.     xinput_airbrush_tool->direction_abs=atan2(ytiltv, xtiltv) + G_PI;
  609.     xinput_airbrush_tool->direction=atan(ytiltv/xtiltv);
  610.     xinput_airbrush_tool->c_direction=atan(ytiltv/xtilt);
  611.     xinput_airbrush_tool->c_direction_abs=atan2(ytiltv, xtilt) + G_PI;
  612.  
  613.     return create_air_blob(x_center * SUBSAMPLE, y_center * SUBSAMPLE,
  614.                0., ytop * SUBSAMPLE, xright * SUBSAMPLE, 0.,
  615.                0., ybot * SUBSAMPLE, xleft * SUBSAMPLE, 0.,
  616.                (xinput_airbrush_tool->c_direction_abs - G_PI),
  617.                xinput_airbrush_tool->c_direction);
  618. }
  619.  
  620. static void
  621. xinput_airbrush_button_press (Tool           *tool,
  622.                   GdkEventButton *bevent,
  623.                   gpointer        gdisp_ptr)
  624. {
  625.   gdouble             x, y;
  626.   GDisplay            *gdisp;
  627.   XinputAirbrushTool  *xinput_airbrush_tool;
  628.   GimpDrawable        *drawable;
  629.   AirBlob             *airblob;
  630.   AirLine             *airline;
  631.   MaskBuf             *brush_mask;
  632.   int                 width, height;
  633.   int                 x_min, y_min;
  634.   guint               value;
  635.  
  636.  
  637.  
  638.   gdisp = (GDisplay *) gdisp_ptr;
  639.   xinput_airbrush_tool = (XinputAirbrushTool *) tool->private;
  640.   
  641.   /*  Keep the coordinates of the target  */
  642.   gdisplay_untransform_coords_f (gdisp, bevent->x, bevent->y,
  643.                  &x, &y, TRUE);
  644.   drawable = gimage_active_drawable (gdisp->gimage);
  645.   
  646.   xinput_airbrush_init (xinput_airbrush_tool, drawable, x, y);
  647.   
  648.   tool->state = ACTIVE;
  649.   tool->gdisp_ptr = gdisp_ptr;
  650.   tool->paused_count = 0;
  651.  
  652.   /*  pause the current selection and grab the pointer  */
  653.   gdisplays_selection_visibility (gdisp->gimage, SelectionPause);
  654.  
  655.   /* add motion memory if you press mod1 first ^ perfectmouse */
  656.   if (((bevent->state & GDK_MOD1_MASK) != 0) != (perfectmouse != 0))
  657.     gdk_pointer_grab (gdisp->canvas->window, FALSE,
  658.               GDK_BUTTON1_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
  659.               NULL, NULL, bevent->time);
  660.   else
  661.     gdk_pointer_grab (gdisp->canvas->window, FALSE,
  662.               GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON1_MOTION_MASK |
  663.               GDK_BUTTON_RELEASE_MASK,
  664.               NULL, NULL, bevent->time);
  665.  
  666.   tool->gdisp_ptr = gdisp_ptr;
  667.   tool->state = ACTIVE;
  668.  
  669.  
  670.   /* Get the AirBlob from xinput_airbrush_pen_ellipse */
  671.   
  672. #ifdef GTK_HAVE_SIX_VALUATORS
  673.  
  674.   airblob = xinput_airbrush_pen_ellipse (xinput_airbrush_tool, x, y,
  675.                      bevent->pressure, bevent->xtilt,
  676.                      bevent->ytilt, bevent->wheel);
  677. #else /* !GTK_HAVE_SIX_VALUATORS */  
  678.  
  679.   airblob = xinput_airbrush_pen_ellipse (xinput_airbrush_tool, x, y,
  680.                      bevent->pressure, bevent->xtilt,
  681.                      bevent->ytilt);
  682. #endif /* GTK_HAVE_SIX_VALUATORS */
  683.  
  684.  
  685.   /* Make the AirBlob mask */
  686.  
  687.   value = 256 * bevent->pressure;
  688.     
  689.   airline = create_air_line(airblob);
  690.   x_min = airline->min_x;
  691.   y_min = airline->min_y;
  692.   width = airline->width;
  693.   height = airline->height;
  694.   
  695.   brush_mask =  mask_buf_new(width, height);
  696.   
  697.   make_mask(airline, brush_mask, value);
  698.  
  699.   /*print_mask(brush_mask);*/
  700.  
  701.     /*Paint the mask on the drawable*/
  702.  
  703.   xinput_airbrush_paste (xinput_airbrush_tool, drawable, brush_mask, x_min, y_min, width, height);
  704.  
  705.     /*Prepare for next stroke*/
  706.  
  707.   xinput_airbrush_tool->last_airblob = airblob;
  708.  
  709.   time_smoother_init (xinput_airbrush_tool, bevent->time);
  710.   xinput_airbrush_tool->last_time = bevent->time;
  711.   dist_smoother_init (xinput_airbrush_tool, 0.0);
  712.   xinput_airbrush_tool->init_velocity = TRUE;
  713.   xinput_airbrush_tool->init_prepre = FALSE;
  714.   xinput_airbrush_tool->lastx = xinput_airbrush_tool->xcenter;
  715.   xinput_airbrush_tool->lasty = xinput_airbrush_tool->ycenter;
  716.   xinput_airbrush_tool->last_value = value;
  717.   
  718.   /*Free the maks_buf and airline*/
  719.   
  720.   mask_buf_free(brush_mask);
  721.   g_free(airline);
  722.  
  723.   gdisplay_flush_now (gdisp);
  724.  
  725. }
  726.  
  727. static void
  728.   xinput_airbrush_button_release (Tool           *tool,
  729.                                   GdkEventButton *bevent,
  730.                                   gpointer        gdisp_ptr)
  731.   {
  732.     GDisplay * gdisp;
  733.     GImage * gimage;
  734.     XinputAirbrushTool * xinput_airbrush_tool;
  735.  
  736.     gdisp = (GDisplay *) gdisp_ptr;
  737.     gimage = gdisp->gimage;
  738.     xinput_airbrush_tool = (XinputAirbrushTool *) tool->private;
  739.  
  740.     /*  resume the current selection and ungrab the pointer  */
  741.     gdisplays_selection_visibility (gdisp->gimage, SelectionResume);
  742.  
  743.     gdk_pointer_ungrab (bevent->time);
  744.     gdk_flush ();
  745.  
  746.     /*  Set tool state to inactive -- no longer painting */
  747.     tool->state = INACTIVE;
  748.  
  749.     xinput_airbrush_finish (xinput_airbrush_tool, gimage_active_drawable (gdisp->gimage), tool->ID);
  750.     gdisplays_flush ();
  751.   }
  752.  
  753.  
  754. static void
  755.   dist_smoother_init (XinputAirbrushTool* xinput_airbrush_tool, gdouble initval)
  756.   {
  757.     gint i;
  758.  
  759.     xinput_airbrush_tool->dt_index = 0;
  760.  
  761.     for (i=0; i<DIST_SMOOTHER_BUFFER; i++)
  762.       {
  763.         xinput_airbrush_tool->dt_buffer[i] = initval;
  764.       }
  765.   }
  766.  
  767. static gdouble
  768.   dist_smoother_result (XinputAirbrushTool* xinput_airbrush_tool)
  769.   {
  770.     gint i;
  771.     gdouble result = 0.0;
  772.  
  773.     for (i=0; i<DIST_SMOOTHER_BUFFER; i++)
  774.       {
  775.         result += xinput_airbrush_tool->dt_buffer[i];
  776.       }
  777.  
  778.     return (result / (gdouble)DIST_SMOOTHER_BUFFER);
  779.   }
  780.  
  781. static void
  782.   dist_smoother_add (XinputAirbrushTool* xinput_airbrush_tool, gdouble value)
  783.   {
  784.     xinput_airbrush_tool->dt_buffer[xinput_airbrush_tool->dt_index] = value;
  785.  
  786.     if ((++xinput_airbrush_tool->dt_index) == DIST_SMOOTHER_BUFFER)
  787.       xinput_airbrush_tool->dt_index = 0;
  788.   }
  789.  
  790.  
  791. static void
  792.   time_smoother_init (XinputAirbrushTool* xinput_airbrush_tool, guint32 initval)
  793.   {
  794.     gint i;
  795.  
  796.     xinput_airbrush_tool->ts_index = 0;
  797.  
  798.     for (i=0; i<TIME_SMOOTHER_BUFFER; i++)
  799.       {
  800.         xinput_airbrush_tool->ts_buffer[i] = initval;
  801.       }
  802.   }
  803.  
  804. static gdouble
  805.   time_smoother_result (XinputAirbrushTool* xinput_airbrush_tool)
  806.   {
  807.     gint i;
  808.     guint64 result = 0;
  809.  
  810.     for (i=0; i<TIME_SMOOTHER_BUFFER; i++)
  811.       {
  812.         result += xinput_airbrush_tool->ts_buffer[i];
  813.       }
  814.  
  815. #ifdef _MSC_VER
  816.     return (gdouble) (gint64) (result / TIME_SMOOTHER_BUFFER);
  817. #else
  818.     return (result / TIME_SMOOTHER_BUFFER);
  819. #endif
  820.   }
  821.  
  822. static void
  823.   time_smoother_add (XinputAirbrushTool* xinput_airbrush_tool, guint32 value)
  824.   {
  825.     xinput_airbrush_tool->ts_buffer[xinput_airbrush_tool->ts_index] = value;
  826.  
  827.     if ((++xinput_airbrush_tool->ts_index) == TIME_SMOOTHER_BUFFER)
  828.       xinput_airbrush_tool->ts_index = 0;
  829.   }
  830.  
  831.  
  832. static void
  833.   xinput_airbrush_motion (Tool           *tool,
  834.                           GdkEventMotion *mevent,
  835.                           gpointer        gdisp_ptr)
  836.   {
  837.     GDisplay *gdisp;
  838.     XinputAirbrushTool  *xinput_airbrush_tool;
  839.     GimpDrawable *drawable;
  840.     AirBlob *airblob;
  841.  
  842.     double x, y;
  843.     double pressure;
  844.     double velocity;
  845.     double dist;
  846.     gdouble lasttime, thistime;
  847.     guint last_value, present_value;
  848.     double a, b, c, A;
  849.  
  850.     gboolean turn_around;
  851.  
  852. #ifdef GTK_HAVE_SIX_VALUATORS
  853.     double min_height;
  854.     double max_height;
  855.     double inter_height;
  856. #endif /* GTK_HAVE_SIX_VALUATORS */
  857.     double height;
  858.  
  859.     gdisp = (GDisplay *) gdisp_ptr;
  860.     xinput_airbrush_tool = (XinputAirbrushTool *) tool->private;
  861.  
  862.     gdisplay_untransform_coords_f (gdisp, mevent->x, mevent->y, &x, &y, TRUE);
  863.     drawable = gimage_active_drawable (gdisp->gimage);
  864.  
  865.     pressure = mevent->pressure;
  866.  
  867. #ifdef GTK_HAVE_SIX_VALUATORS
  868.     airblob = xinput_airbrush_pen_ellipse (xinput_airbrush_tool, x, y, pressure, mevent->xtilt, mevent->ytilt, mevent->wheel);
  869. #else /* !GTK_HAVE_SIX_VALUATORS */
  870.     airblob = xinput_airbrush_pen_ellipse (xinput_airbrush_tool, x, y, pressure, mevent->xtilt, mevent->ytilt);
  871. #endif /* GTK_HAVE_SIX_VALUATORS */
  872.  
  873.     x = xinput_airbrush_tool->xcenter;
  874.     y = xinput_airbrush_tool->ycenter;
  875.  
  876.     lasttime = xinput_airbrush_tool->last_time;
  877.  
  878.     time_smoother_add (xinput_airbrush_tool, mevent->time);
  879.     thistime = xinput_airbrush_tool->last_time =
  880.                  time_smoother_result(xinput_airbrush_tool);
  881.  
  882.     /* The time resolution on X-based GDK motion events is
  883.        bloody awful, hence the use of the smoothing function.
  884.        Sadly this also means that there is always the chance of
  885.        having an indeterminite velocity since this event and
  886.        the previous several may still appear to issue at the same
  887.        instant. -ADM */
  888.  
  889.     if (thistime == lasttime)
  890.       thistime = lasttime + 1;
  891.  
  892.     if (xinput_airbrush_tool->init_velocity)
  893.       {
  894.         dist_smoother_init (xinput_airbrush_tool, dist = sqrt((xinput_airbrush_tool->lastx-x)*(xinput_airbrush_tool->lastx-x) +
  895.                             (xinput_airbrush_tool->lasty-y)*(xinput_airbrush_tool->lasty-y)));
  896.         xinput_airbrush_tool->init_velocity = FALSE;
  897.       }
  898.     else
  899.       {
  900.         dist_smoother_add (xinput_airbrush_tool, sqrt((xinput_airbrush_tool->lastx-x)*(xinput_airbrush_tool->lastx-x) +
  901.                            (xinput_airbrush_tool->lasty-y)*(xinput_airbrush_tool->lasty-y)));
  902.         dist = dist_smoother_result(xinput_airbrush_tool);
  903.       }
  904.  
  905.     turn_around = FALSE;
  906.  
  907.     if (xinput_airbrush_tool->init_prepre)
  908.       {
  909.         a = hypot(xinput_airbrush_tool->last_lastx - x, xinput_airbrush_tool->last_lasty - y);
  910.         b = hypot(xinput_airbrush_tool->lastx - x,  xinput_airbrush_tool->lasty - y);
  911.         c = hypot(xinput_airbrush_tool->last_lastx - xinput_airbrush_tool->lastx, xinput_airbrush_tool->last_lasty - xinput_airbrush_tool->lasty);
  912.  
  913.         /* Maybe fix the fact that a, b or c can be zero i.e that we had a perfectly strait turn around or continue*/
  914.         A = acos((b*b + c*c - a*a)/2*b*c);
  915.         /* 1.65806 RAD == 95deg*/
  916.         if ( (A >= 0.0) && (A < 1.65806) )
  917.           {
  918.             turn_around = TRUE;
  919.         /* printf("TURN_AROUND\n");*/
  920.           }
  921.  
  922.       }
  923.  
  924.     velocity = 10.0 * sqrt((dist) / (double)(thistime - lasttime));
  925.  
  926.  
  927.     /* Normal Speed is 2.0, Break point for zero ink is 18.
  928.        Slow speed is between 2.0 and 0.5 */
  929.  
  930.  
  931.     /*printf("Speed: %f\n", velocity);*/
  932.  
  933. #ifdef GTK_HAVE_SIX_VALUATORS
  934.     min_height=xinput_airbrush_options->minheight;
  935.     max_height=xinput_airbrush_options->maxheight;
  936.  
  937.     inter_height = CLAMP(mevent->wheel, 0.0, 1.0) * (max_height - min_height);
  938.     height = min_height + inter_height;
  939.  
  940.     /*
  941.       printf("Height: %f\n", height);
  942.     */
  943.  
  944. #else /* !GTK_HAVE_SIX_VALUATORS */
  945.     height = xinput_airbrush_options->height;
  946. #endif /* GTK_HAVE_SIX_VALUATORS */
  947.  
  948.     last_value =  xinput_airbrush_tool->last_value; 
  949.  
  950.     present_value = 256 * pressure * (1 - ((height - 25)/100));
  951.     
  952.     make_stroke(airblob, xinput_airbrush_tool, drawable, last_value, present_value);
  953.  
  954.     xinput_airbrush_tool->last_lastx = xinput_airbrush_tool->lastx;
  955.     xinput_airbrush_tool->last_lasty = xinput_airbrush_tool->lasty;
  956.     xinput_airbrush_tool->lastx = x;
  957.     xinput_airbrush_tool->lasty = y;
  958.     xinput_airbrush_tool->last_value = present_value;
  959.  
  960.     g_free(xinput_airbrush_tool->last_airblob);
  961.     xinput_airbrush_tool->last_airblob = airblob;
  962.  
  963.     xinput_airbrush_tool->init_velocity = TRUE;
  964.     xinput_airbrush_tool->init_prepre = TRUE;
  965.     
  966.     gdisplay_flush_now (gdisp);
  967.   }
  968.  
  969. static void
  970.   xinput_airbrush_cursor_update (Tool           *tool,
  971.                                  GdkEventMotion *mevent,
  972.                                  gpointer        gdisp_ptr)
  973.   {
  974.     GDisplay *gdisp;
  975.     Layer *layer;
  976.     GdkCursorType ctype = GDK_TOP_LEFT_ARROW;
  977.     int x, y;
  978.  
  979.     gdisp = (GDisplay *) gdisp_ptr;
  980.  
  981.     gdisplay_untransform_coords (gdisp, mevent->x, mevent->y, &x, &y, FALSE, FALSE);
  982.     if ((layer = gimage_get_active_layer (gdisp->gimage)))
  983.       {
  984.         int off_x, off_y;
  985.         drawable_offsets (GIMP_DRAWABLE(layer), &off_x, &off_y);
  986.         if (x >= off_x && y >= off_y &&
  987.             x < (off_x + drawable_width (GIMP_DRAWABLE(layer))) &&
  988.             y < (off_y + drawable_height (GIMP_DRAWABLE(layer))))
  989.           {
  990.             /*  One more test--is there a selected region?
  991.              *  if so, is cursor inside?
  992.              */
  993.             if (gimage_mask_is_empty (gdisp->gimage))
  994.               ctype = GDK_PENCIL;
  995.             else if (gimage_mask_value (gdisp->gimage, x, y))
  996.               ctype = GDK_PENCIL;
  997.           }
  998.       }
  999.     gdisplay_install_tool_cursor (gdisp, ctype);
  1000.   }
  1001.  
  1002. static void
  1003.   xinput_airbrush_control (Tool       *tool,
  1004.                            ToolAction  action,
  1005.                            gpointer    gdisp_ptr)
  1006.   {
  1007.     XinputAirbrushTool *xinput_airbrush_tool;
  1008.  
  1009.     xinput_airbrush_tool = (XinputAirbrushTool *) tool->private;
  1010.  
  1011.     switch (action)
  1012.       {
  1013.       case PAUSE:
  1014.         draw_core_pause (xinput_airbrush_tool->core, tool);
  1015.         break;
  1016.  
  1017.       case RESUME:
  1018.         draw_core_resume (xinput_airbrush_tool->core, tool);
  1019.         break;
  1020.  
  1021.       case HALT:
  1022.         xinput_airbrush_cleanup ();
  1023.         break;
  1024.  
  1025.       default:
  1026.         break;
  1027.       }
  1028.   }
  1029.  
  1030. static void
  1031.   xinput_airbrush_init (XinputAirbrushTool *xinput_airbrush_tool, GimpDrawable *drawable,
  1032.                         double x, double y)
  1033.   {
  1034.     /*  free the block structures  */
  1035.     if (undo_tiles)
  1036.       tile_manager_destroy (undo_tiles);
  1037.     if (canvas_tiles)
  1038.       tile_manager_destroy (canvas_tiles);
  1039.  
  1040.     /*  Allocate the undo structure  */
  1041.     undo_tiles = tile_manager_new (drawable_width (drawable),
  1042.                                    drawable_height (drawable),
  1043.                                    drawable_bytes (drawable));
  1044.  
  1045.     /*  Allocate the canvas blocks structure  */
  1046.     canvas_tiles = tile_manager_new (drawable_width (drawable),
  1047.                                      drawable_height (drawable), 1);
  1048.  
  1049.     /*  Get the initial undo extents  */
  1050.     xinput_airbrush_tool->x1 = xinput_airbrush_tool->x2 = x;
  1051.     xinput_airbrush_tool->y1 = xinput_airbrush_tool->y2 = y;
  1052.   }
  1053.  
  1054. static void
  1055.   xinput_airbrush_finish (XinputAirbrushTool *xinput_airbrush_tool, GimpDrawable *drawable, int tool_id)
  1056.   {
  1057.     /*  push an undo  */
  1058.     drawable_apply_image (drawable, xinput_airbrush_tool->x1, xinput_airbrush_tool->y1,
  1059.                           xinput_airbrush_tool->x2, xinput_airbrush_tool->y2, undo_tiles, TRUE);
  1060.     undo_tiles = NULL;
  1061.  
  1062.     /*  invalidate the drawable--have to do it here, because
  1063.      *  it is not done during the actual painting.
  1064.      */
  1065.     gimp_drawable_invalidate_preview (drawable, TRUE);
  1066.   }
  1067.  
  1068. static void
  1069.   xinput_airbrush_cleanup (void)
  1070.   {
  1071.     /*  CLEANUP  */
  1072.     /*  If the undo tiles exist, nuke them  */
  1073.     if (undo_tiles)
  1074.       {
  1075.         tile_manager_destroy (undo_tiles);
  1076.         undo_tiles = NULL;
  1077.       }
  1078.  
  1079.     /*  If the canvas blocks exist, nuke them  */
  1080.     if (canvas_tiles)
  1081.       {
  1082.         tile_manager_destroy (canvas_tiles);
  1083.         canvas_tiles = NULL;
  1084.       }
  1085.  
  1086.     /*  Free the temporary buffer if it exist  */
  1087.     if (canvas_buf)
  1088.       temp_buf_free (canvas_buf);
  1089.     canvas_buf = NULL;
  1090.   }
  1091.  
  1092. /*********************************
  1093.  *  Rendering functions          *
  1094.  *********************************/
  1095.  
  1096. /* Some of this stuff should probably be combined with the
  1097.  * code it was copied from in paint_core.c; but I wanted
  1098.  * to learn this stuff, so I've kept it simple.
  1099.  */
  1100.  
  1101. static inline int
  1102. number_of_steps(int x0, int y0, int x1, int y1)
  1103. {
  1104.  
  1105.  
  1106.   int dx, dy;
  1107.  
  1108.   dx = abs(x0 - x1);
  1109.   
  1110.   dy = abs(y0 - y1);
  1111.  
  1112.   if (dy > dx)
  1113.     {
  1114.       return dy + 1;
  1115.     }
  1116.   else
  1117.     {
  1118.       return dx + 1;
  1119.     }
  1120. }
  1121.  
  1122.  
  1123.  
  1124. static void
  1125. make_stroke(AirBlob *airblob,
  1126.         XinputAirbrushTool *xinput_airbrush_tool,
  1127.         GimpDrawable *drawable,
  1128.         guint last_value,
  1129.         guint present_value)
  1130. {
  1131.  
  1132.   int steps;
  1133.   int i, j, k;
  1134.  
  1135.  
  1136.   int dx, dy;
  1137.   int x, y;
  1138.  
  1139.  
  1140.   int x0, x1, y0, y1;
  1141.  
  1142.   double x0d, x1d, y0d, y1d;
  1143.  
  1144.   int x_min, x_max, y_min, y_max, width, height; 
  1145.   int number, ystart, xstart;
  1146.  
  1147.   guint ivalue;
  1148.   double lv, pv, mv;
  1149.  
  1150.   double dist;
  1151.  
  1152.   int slopeterm;
  1153.  
  1154.   AirBlob *last_airblob;
  1155.   AirBlob *trans_blob;
  1156.   AirLine *air_line;
  1157.   MaskBuf *brush_mask;
  1158.   MaskBuf **bufs;
  1159.   guchar *source;
  1160.   guchar *dest;
  1161.   int *xpoints;
  1162.   int *ypoints;
  1163.  
  1164.   gboolean something;
  1165.  
  1166.   something = FALSE;
  1167.  
  1168.   x0 = xinput_airbrush_tool->lastx;
  1169.   x1 = xinput_airbrush_tool->xcenter;
  1170.   y0 = xinput_airbrush_tool->lasty;
  1171.   y1 = xinput_airbrush_tool->ycenter;
  1172.  
  1173.   x0d = xinput_airbrush_tool->lastx;
  1174.   x1d = xinput_airbrush_tool->xcenter;
  1175.   y0d = xinput_airbrush_tool->lasty;
  1176.   y1d = xinput_airbrush_tool->ycenter;
  1177.  
  1178.   last_airblob = xinput_airbrush_tool->last_airblob;
  1179.  
  1180.   steps = number_of_steps(x0, y0, x1, y1);
  1181.  
  1182.   /*
  1183.     printf("Stoke Steps: %d\n", steps);
  1184.   */
  1185.  
  1186.   ypoints = g_new(int, steps);
  1187.   xpoints = g_new(int, steps);
  1188.   
  1189.   bufs = g_new (MaskBuf*, steps);
  1190.   
  1191.   dx = abs(x0 - x1);
  1192.   dy = abs(y0 - y1);
  1193.       
  1194.   lv = last_value;
  1195.   pv = present_value;
  1196.   mv = pv - lv;
  1197.  
  1198.  
  1199.   /*
  1200.     Yes I know this is bulky code :-),
  1201.     --> see comment on make_mask.
  1202.     
  1203.     But I wanted to keep it simple while I 
  1204.     implemented the tool :-). I will make it 
  1205.     more effective later on. And yes I could 
  1206.     look for the three special cases.
  1207.   */
  1208.  
  1209.  
  1210.   /*
  1211.     There is also a possiblity of a bug in the
  1212.     scanline function that will explain the 
  1213.     "jumping lines".
  1214.   */
  1215.  
  1216.     
  1217.   
  1218.   if (dy > dx)
  1219.     {
  1220.       /* Step in the y direction*/
  1221.       if (y0 < y1)
  1222.     {
  1223.       /* 
  1224.          Step from y0 to y1 
  1225.       */
  1226.       if(x1 < x0)
  1227.         {
  1228.           /*
  1229.         We have to x--
  1230.           */
  1231.           x = x0;
  1232.           y = y0;
  1233.  
  1234.           number = 0;
  1235.         
  1236.           j = 1;
  1237.  
  1238.           slopeterm = dx;
  1239.  
  1240.           while(y < y1)
  1241.         {
  1242.           if (slopeterm > dy)
  1243.             {
  1244.               slopeterm -= dy;
  1245.               x--;
  1246.               y++;
  1247.             }
  1248.           else
  1249.             {
  1250.               slopeterm += dx;
  1251.               y++;
  1252.              }
  1253.               
  1254.           j++;
  1255.         
  1256.           dist = 1.0 - (double)j/(double)steps;                
  1257.           ivalue = lv + mv * dist;        
  1258.         
  1259.           trans_blob = trans_air_blob(last_airblob, airblob, dist, x, y);
  1260.           air_line = create_air_line(trans_blob);
  1261.  
  1262.           x_min = air_line->min_x;
  1263.           y_min = air_line->min_y;
  1264.           width = air_line->width;
  1265.           height = air_line->height;
  1266.           
  1267.           bufs[number] =  mask_buf_new(width, height);
  1268.           ypoints[number] = y_min;
  1269.           xpoints[number] = x_min;
  1270.           make_mask(air_line, bufs[number], ivalue);
  1271.           number++;
  1272.           something = TRUE;
  1273.         }
  1274.  
  1275.         }
  1276.       else
  1277.         {
  1278.           /* 
  1279.          We have to x++
  1280.           */
  1281.           x = x0;
  1282.           y = y0;
  1283.           
  1284.           number = 0;
  1285.     
  1286.           slopeterm = dx;
  1287.           
  1288.           j = 1;
  1289.           
  1290.           while(y < y1)
  1291.         {
  1292.           if (slopeterm > dy)
  1293.             {
  1294.               slopeterm -= dy;
  1295.               x++;
  1296.               y++;
  1297.             }
  1298.           else
  1299.             {
  1300.               slopeterm += dx;
  1301.               y++;
  1302.             }
  1303.           
  1304.           j++;
  1305.           dist = 1.0 - (double)j/(double)steps;                
  1306.           ivalue = lv + mv * dist;        
  1307.         
  1308.           trans_blob = trans_air_blob(last_airblob, airblob, dist, x, y);
  1309.           air_line = create_air_line(trans_blob);
  1310.  
  1311.           x_min = air_line->min_x;
  1312.           y_min = air_line->min_y;
  1313.           width = air_line->width;
  1314.           height = air_line->height;
  1315.  
  1316.           bufs[number] =  mask_buf_new(width, height);
  1317.           ypoints[number] = y_min;
  1318.           xpoints[number] = x_min;
  1319.            make_mask(air_line, bufs[number], ivalue);
  1320.           number++;
  1321.           something = TRUE;
  1322.         }
  1323.         }
  1324.     }
  1325.       else
  1326.     {
  1327.       /* Step from y0 to y1 with neg steps */
  1328.       if(x1 < x0)
  1329.         {
  1330.           /*
  1331.         We have to x--
  1332.            */
  1333.           x = x0;
  1334.           y = y0;
  1335.  
  1336.           number = 0;
  1337.  
  1338.           slopeterm = dx;
  1339.           
  1340.           j = 1;
  1341.           
  1342.           while(y > y1)
  1343.         {
  1344.           if (slopeterm > dy)
  1345.             {
  1346.               slopeterm -= dy;
  1347.               x--;
  1348.               y--;
  1349.             }
  1350.           else
  1351.             {
  1352.               slopeterm += dx;
  1353.               y--;
  1354.             }
  1355.  
  1356.           j++;
  1357.           dist = 1.0 - (double)j/(double)steps;                
  1358.           ivalue = lv + mv * dist;        
  1359.         
  1360.           trans_blob = trans_air_blob(last_airblob, airblob, dist, x, y);
  1361.           air_line = create_air_line(trans_blob);
  1362.  
  1363.           x_min = air_line->min_x;
  1364.           y_min = air_line->min_y;
  1365.           width = air_line->width;
  1366.           height = air_line->height;
  1367.  
  1368.  
  1369.           bufs[number] =  mask_buf_new(width, height);
  1370.           ypoints[number] = y_min;
  1371.           xpoints[number] = x_min;
  1372.           make_mask(air_line, bufs[number], ivalue);
  1373.           number++;
  1374.           something = TRUE;
  1375.         }
  1376.         }
  1377.       else
  1378.         {
  1379.           /* 
  1380.          We have to x++
  1381.           */
  1382.           x = x0;
  1383.           y = y0;
  1384.  
  1385.           number = 0;
  1386.  
  1387.           slopeterm = dx;
  1388.           
  1389.           j = 1;
  1390.           
  1391.           while(y > y1)
  1392.         {
  1393.           if (slopeterm > dy)
  1394.             {
  1395.               slopeterm -= dy;
  1396.               x++;
  1397.               y--;
  1398.             }
  1399.           else
  1400.             {
  1401.               slopeterm += dx;
  1402.               y--;
  1403.             }
  1404.  
  1405.           j++;
  1406.           dist = 1.0 - (double)j/(double)steps;                
  1407.           ivalue = lv + mv * dist;        
  1408.         
  1409.           trans_blob = trans_air_blob(last_airblob, airblob, dist, x, y);
  1410.           air_line = create_air_line(trans_blob);
  1411.  
  1412.           x_min = air_line->min_x;
  1413.           y_min = air_line->min_y;
  1414.           width = air_line->width;
  1415.           height = air_line->height;
  1416.           
  1417.           bufs[number] =  mask_buf_new(width, height);
  1418.           ypoints[number] = y_min;
  1419.           xpoints[number] = x_min;
  1420.           make_mask(air_line, bufs[number], ivalue);
  1421.           number++;
  1422.           something = TRUE;
  1423.         }
  1424.         }
  1425.     }
  1426.  
  1427.     }
  1428.   else 
  1429.     {
  1430.       /* Step in the X direction*/
  1431.       if (x0 < x1)
  1432.     {
  1433.       /* Step from x0 to x1 */
  1434.       if(y1 < y0)
  1435.         {
  1436.           /*
  1437.         We have to y--
  1438.           */
  1439.           x = x0;
  1440.           y = y0;
  1441.  
  1442.           number = 0;
  1443.  
  1444.           slopeterm = dy;
  1445.           
  1446.           j = 1;
  1447.           
  1448.           while(x < x1)
  1449.         {
  1450.           if (slopeterm > dx)
  1451.             {
  1452.               slopeterm -= dx;
  1453.               y--;
  1454.               x++;
  1455.             }
  1456.           else
  1457.             {
  1458.               slopeterm += dy;
  1459.               x++;
  1460.             }
  1461.           
  1462.           j++;
  1463.           dist = 1.0 - (double)j/(double)steps;                
  1464.           ivalue = lv + mv * dist;        
  1465.         
  1466.           trans_blob = trans_air_blob(last_airblob, airblob, dist, x, y);
  1467.           air_line = create_air_line(trans_blob);
  1468.  
  1469.           x_min = air_line->min_x;
  1470.           y_min = air_line->min_y;
  1471.           width = air_line->width;
  1472.           height = air_line->height;
  1473.  
  1474.           bufs[number] =  mask_buf_new(width, height);
  1475.           ypoints[number] = y_min;
  1476.           xpoints[number] = x_min;
  1477.           make_mask(air_line, bufs[number], ivalue);
  1478.           number++;
  1479.           something = TRUE;
  1480.         }
  1481.         
  1482.         }
  1483.       else
  1484.         {
  1485.           /* 
  1486.          We have to y++
  1487.           */
  1488.           x = x0;
  1489.           y = y0;
  1490.           
  1491.           number = 0;
  1492.  
  1493.           slopeterm = dy;
  1494.           
  1495.           j = 1;
  1496.           
  1497.           while(x < x1)
  1498.         {
  1499.           if (slopeterm > dx)
  1500.             {
  1501.               slopeterm -= dx;
  1502.               y++;
  1503.               x++;
  1504.             }
  1505.           else
  1506.             {
  1507.               slopeterm += dy;
  1508.               x++;
  1509.             }
  1510.  
  1511.           j++;
  1512.           dist = 1.0 - (double)j/(double)steps;                
  1513.           ivalue = lv + mv * dist;        
  1514.         
  1515.           trans_blob = trans_air_blob(last_airblob, airblob, dist, x, y);
  1516.           air_line = create_air_line(trans_blob);
  1517.  
  1518.           x_min = air_line->min_x;
  1519.           y_min = air_line->min_y;
  1520.           width = air_line->width;
  1521.           height = air_line->height;
  1522.           
  1523.           bufs[number] =  mask_buf_new(width, height);
  1524.           ypoints[number] = y_min;
  1525.           xpoints[number] = x_min;
  1526.           make_mask(air_line, bufs[number], ivalue);
  1527.           number++;
  1528.           something = TRUE;
  1529.         }
  1530.         }
  1531.     }
  1532.       else
  1533.     {
  1534.       /* Step from x0 to x1 neg direction */
  1535.       if(y1 < y0)
  1536.         {
  1537.           /*
  1538.         We have to y--
  1539.           */
  1540.           x = x0;
  1541.           y = y0;
  1542.  
  1543.           number = 0;
  1544.  
  1545.           slopeterm = dy;
  1546.           
  1547.           j = 1;
  1548.           
  1549.           while(x > x1)
  1550.         { 
  1551.           if (slopeterm > dx)
  1552.             {
  1553.               slopeterm -= dx;
  1554.               y--;
  1555.               x--;
  1556.             }
  1557.           else
  1558.             {
  1559.               slopeterm += dy;
  1560.               x--;
  1561.             }
  1562.  
  1563.           j++;
  1564.           dist = 1.0 - (double)j/(double)steps;                
  1565.           ivalue = lv + mv * dist;        
  1566.         
  1567.           trans_blob = trans_air_blob(last_airblob, airblob, dist, x, y);
  1568.           air_line = create_air_line(trans_blob);
  1569.  
  1570.           x_min = air_line->min_x;
  1571.           y_min = air_line->min_y;
  1572.           width = air_line->width;
  1573.           height = air_line->height;
  1574.           
  1575.           bufs[number] =  mask_buf_new(width, height);
  1576.           ypoints[number] = y_min;
  1577.           xpoints[number] = x_min;
  1578.           make_mask(air_line, bufs[number], ivalue);
  1579.           number++;
  1580.           something = TRUE;
  1581.         }
  1582.         }
  1583.       else
  1584.         {
  1585.           /* 
  1586.          We have to y++
  1587.           */
  1588.           x = x0;
  1589.           y = y0;
  1590.  
  1591.           number = 0;
  1592.  
  1593.           slopeterm = dy;
  1594.           
  1595.           j = 1;
  1596.           
  1597.           while(x > x1)
  1598.         {
  1599.           if (slopeterm > dx)
  1600.             {
  1601.               slopeterm -= dx;
  1602.               y++;
  1603.               x--;
  1604.             }
  1605.           else
  1606.             {
  1607.               slopeterm += dy;
  1608.               x--;
  1609.             }
  1610.  
  1611.           j++;
  1612.  
  1613.           dist = 1.0 - (double)j/(double)steps;                
  1614.           ivalue = lv + mv * dist;        
  1615.         
  1616.           trans_blob = trans_air_blob(last_airblob, airblob, dist, x, y);
  1617.           air_line = create_air_line(trans_blob);
  1618.  
  1619.           x_min = air_line->min_x;
  1620.           y_min = air_line->min_y;
  1621.           width = air_line->width;
  1622.           height = air_line->height;
  1623.           
  1624.           bufs[number] =  mask_buf_new(width, height);
  1625.           ypoints[number] = y_min;
  1626.           xpoints[number] = x_min;
  1627.           make_mask(air_line, bufs[number], ivalue);
  1628.           number++;
  1629.           something = TRUE;
  1630.         }
  1631.         }
  1632.     }
  1633.     } 
  1634.  
  1635.   if(!something)
  1636.     {
  1637.       /*
  1638.     printf("Nothing \n");
  1639.       */
  1640.       number = 0;
  1641.  
  1642.       dist = 1.0;                
  1643.  
  1644.       trans_blob = trans_air_blob(last_airblob, airblob, 0.5 , x1, y1);
  1645.       air_line = create_air_line(trans_blob);
  1646.       
  1647.       x_min = air_line->min_x;
  1648.       y_min = air_line->min_y;
  1649.       width = air_line->width;
  1650.       height = air_line->height;
  1651.           
  1652.       bufs[number] =  mask_buf_new(width, height);
  1653.       ypoints[number] = y_min;
  1654.       xpoints[number] = x_min;
  1655.       make_mask(air_line, bufs[number], pv);
  1656.       /*
  1657.     print_mask(bufs[number]);
  1658.       */
  1659.       brush_mask = mask_buf_new(width, height);
  1660.       dest = brush_mask->data;
  1661.       source = bufs[number]->data;
  1662.  
  1663.  
  1664.       for(k = 0; k < bufs[number]->height; k++)
  1665.     {       
  1666.       for(j = 0; j < bufs[number]->width; j++ )
  1667.         {
  1668.           if(((int)dest[k * width + j] + (int)source[k * bufs[number]->width + j]) > 255)
  1669.         {
  1670.           dest[k * width + j] = 255;
  1671.         }
  1672.           else
  1673.         {
  1674.           dest[k * width + j] += source[k * bufs[number]->width + j];
  1675.         }
  1676.         }
  1677.     }
  1678.  
  1679.  
  1680.       xinput_airbrush_paste (xinput_airbrush_tool, drawable, brush_mask, x_min, y_min, width, height);
  1681.  
  1682.       /*
  1683.     print_mask(brush_mask);
  1684.       */
  1685.       mask_buf_free(brush_mask);
  1686.  
  1687.       mask_buf_free(bufs[number]);
  1688.       g_free(bufs);
  1689.       /*
  1690.     printf("Nothing again\n");
  1691.       */
  1692.     }
  1693.  
  1694.   if(something)
  1695.     {
  1696.  
  1697.       y_min = y_max = ypoints[0];
  1698.       height = bufs[0]->height;
  1699.   
  1700.       x_min = x_max= xpoints[0];
  1701.       width = bufs[0]->width;
  1702.  
  1703.       /*printf("xmin %d, ymin %d, width %d, weight %d\n", x_min, y_min, width, height);*/
  1704.       
  1705.  
  1706.       for (i = 1; i < steps -1; i++)
  1707.     {
  1708.       /*printf("xmin %d, ymin %d, width %d, weight %d\n", xpoints[i], ypoints[i], bufs[i]->width, bufs[i]->height);*/
  1709.  
  1710.      
  1711.       x_min = MIN(xpoints[i], x_min);
  1712.       y_min = MIN(ypoints[i], y_min);
  1713.       if ((x_max + width) < (xpoints[i] + bufs[i]->width))
  1714.         {
  1715.           x_max = xpoints[i];
  1716.           width = bufs[i]->width;
  1717.         }
  1718.       if ((y_max + height) < (ypoints[i] + bufs[i]->height))
  1719.         {
  1720.           y_max = ypoints[i];
  1721.           height = bufs[i]->height;
  1722.         }
  1723.     }
  1724.   
  1725.  
  1726.       width = x_max - x_min + width;
  1727.       height = y_max - y_min + height;
  1728.  
  1729.       /*printf("Xmin %d, Ymin %d, Width %d, Height %d\n", x_min, y_min, width, height);
  1730.     printf("\n");*/
  1731.  
  1732.  
  1733.       brush_mask = mask_buf_new(width, height);
  1734.  
  1735.  
  1736.       dest = brush_mask->data;
  1737.  
  1738.       for (i = 0; i < steps - 1; i++)
  1739.     {
  1740.       
  1741.       
  1742.       ystart = ypoints[i] - y_min;
  1743.       xstart = xpoints[i] - x_min;
  1744.       /*printf("xstart %d, ystart %d\n", xstart, ystart);*/
  1745.  
  1746.       y = width * ystart;
  1747.  
  1748.       source = bufs[i]->data;
  1749.  
  1750.        for(k = 0; k < bufs[i]->height; k++)
  1751.         {       
  1752.           for(j = 0; j < bufs[i]->width; j++ )
  1753.         {
  1754.           if(((int)dest[y + k * width + xstart + j] + (int)source[k * bufs[i]->width + j]) > 255)
  1755.             {
  1756.               dest[y + k * width + xstart + j] = 255;
  1757.             }
  1758.           else
  1759.             {
  1760.               dest[y + k * width + xstart + j] += source[k * bufs[i]->width + j];
  1761.             }
  1762.         }
  1763.         }      
  1764.     }
  1765.  
  1766.       /*printf("\n\n");*/
  1767.  
  1768.       for (i = steps - 2; i >= 0; i--)
  1769.     {
  1770.       mask_buf_free(bufs[i]);
  1771.     }
  1772.  
  1773.       g_free(bufs);
  1774.       xinput_airbrush_paste (xinput_airbrush_tool, drawable, brush_mask, x_min, y_min, width, height);
  1775.       mask_buf_free(brush_mask);
  1776.  
  1777.     }
  1778.   else
  1779.     {
  1780.       /*
  1781.     printf("Hmm something was FALSE\n");
  1782.       */
  1783.     }
  1784.  
  1785. }
  1786.  
  1787.  
  1788.  
  1789. /**************************************************
  1790. ***************************************************
  1791. */
  1792.  
  1793.  
  1794. static void
  1795. make_mask (AirLine *airline,
  1796.        MaskBuf *dest,      
  1797.        guint value)
  1798. {
  1799.  
  1800.   int steps;
  1801.   int total_steps;
  1802.   int i;
  1803.   int j;
  1804.  
  1805.  
  1806.   int dx, dy;
  1807.   int x, y;
  1808.  
  1809.   int x0, x1, y0, y1;
  1810.  
  1811.  
  1812.   unsigned char *s;
  1813.   guint midvalue;
  1814.   double ivalue;
  1815.  
  1816.   int rowlength; 
  1817.   int slopeterm;
  1818.  
  1819.   gboolean nothing;
  1820.  
  1821.  
  1822.   rowlength = dest->width  * dest->bytes; 
  1823.  
  1824.  
  1825.   nothing = TRUE;
  1826.  
  1827.  
  1828.   total_steps=0;
  1829.  
  1830.   for (i=0; i < airline->nlines ; i++)
  1831.     {
  1832.  
  1833.       steps = number_of_steps(airline->xcenter, airline->ycenter,
  1834.                   airline->line[i].x, airline->line[i].y);
  1835.       total_steps += steps;
  1836.     }
  1837.  
  1838.   /*
  1839.     printf("Total Steps: %d\n", total_steps);
  1840.   */
  1841.  
  1842.   for (i=0; i < airline->nlines ; i++)
  1843.     {
  1844.  
  1845.       steps = number_of_steps(airline->xcenter, airline->ycenter,
  1846.                   airline->line[i].x, airline->line[i].y);
  1847.  
  1848.       x0 = airline->xcenter - airline->min_x ;
  1849.       x1 = airline->line[i].x - airline->min_x ;
  1850.  
  1851.       y0 = airline->ycenter - airline->min_y ;
  1852.       y1 = airline->line[i].y -airline->min_y ;
  1853.  
  1854.  
  1855.       dx = abs(x0 - x1);
  1856.       dy = abs(y0 - y1);
  1857.       
  1858.  
  1859.  
  1860.       /*
  1861.     Yes I know this is bulky code :-),
  1862.     you could insted set the x direction
  1863.     to 1 or -1, and y to - or + rowside.
  1864.     Then set the first X and Y and of you 
  1865.     go.
  1866.     
  1867.     But I wanted to keep it simple while I 
  1868.     implemented the tool :-). I will make it 
  1869.     more effective later on. And yes I could 
  1870.     look for the three special cases.
  1871.       */
  1872.  
  1873.      
  1874.       midvalue = value;
  1875.      
  1876.  
  1877.       if (dy > dx)
  1878.     {
  1879.        /* Step in the y direction*/
  1880.       if (y0 < y1)
  1881.         {
  1882.           /* 
  1883.          Step from y0 to y1 
  1884.           */
  1885.           if(x1 < x0)
  1886.         {
  1887.           /*
  1888.             We have to x--
  1889.           */
  1890.  
  1891.           s = dest->data;
  1892.  
  1893.           x = x0;
  1894.           y = y0;
  1895.  
  1896.           s += y * rowlength + x;
  1897.           
  1898.           if (((int)*s + (int)midvalue) > 255)
  1899.             {
  1900.               *s = 255;
  1901.             }
  1902.           else
  1903.             {
  1904.               *s = midvalue + *s;
  1905.             }
  1906.  
  1907.           slopeterm = dx;
  1908.           
  1909.           j = 1;
  1910.           
  1911.           while(y < y1)
  1912.             {
  1913.               if (slopeterm > dy)
  1914.             {
  1915.               slopeterm -= dy;
  1916.               x--;
  1917.               y++;
  1918.             }
  1919.               else
  1920.             {
  1921.               slopeterm += dx;
  1922.               y++;
  1923.             }
  1924.  
  1925.               j++;
  1926.  
  1927.               s = dest->data;
  1928.               s += y * rowlength + x;
  1929.               
  1930.               ivalue = value * (1.0 - (double)j/(double)steps);
  1931.               
  1932.               if(((int)*s + ivalue) > 255)
  1933.             {
  1934.               *s = 255;  
  1935.             }
  1936.               else
  1937.             {
  1938.             *s = *s + ivalue;
  1939.             }
  1940.  
  1941.               nothing = FALSE;
  1942.             }
  1943.  
  1944.         }
  1945.           else
  1946.         {
  1947.           /* 
  1948.              We have to x++
  1949.           */
  1950.           s = dest->data;
  1951.  
  1952.           x = x0;
  1953.           y = y0;
  1954.  
  1955.           s += y * rowlength + x;
  1956.           
  1957.           
  1958.           if (((int)*s + (int)midvalue) > 255)
  1959.             {
  1960.               *s = 255;
  1961.             }
  1962.           else
  1963.             {
  1964.               *s = midvalue + *s;
  1965.              }
  1966.           slopeterm = dx;
  1967.           
  1968.           j = 1;
  1969.           
  1970.           while(y < y1)
  1971.             {
  1972.               if (slopeterm > dy)
  1973.             {
  1974.               slopeterm -= dy;
  1975.               x++;
  1976.               y++;
  1977.             }
  1978.               else
  1979.             {
  1980.               slopeterm += dx;
  1981.               y++;
  1982.             }
  1983.  
  1984.               j++;
  1985.  
  1986.               s = dest->data;
  1987.               s += y * rowlength + x;
  1988.               
  1989.               ivalue = value * (1.0 - (double)j/(double)steps);
  1990.  
  1991.               if(((int)*s + ivalue) > 255)
  1992.             {
  1993.               *s = 255;  
  1994.             }
  1995.               else
  1996.             {
  1997.             *s = *s + ivalue;
  1998.             }
  1999.               nothing = FALSE;
  2000.             }
  2001.         }
  2002.         }
  2003.       else
  2004.         {
  2005.           /* Step from y0 to y1 with neg steps */
  2006.           if(x1 < x0)
  2007.         {
  2008.           /*
  2009.             We have to x--
  2010.           */
  2011.           s = dest->data;
  2012.  
  2013.           x = x0;
  2014.           y = y0;
  2015.  
  2016.           s += y * rowlength + x;
  2017.  
  2018.           
  2019.           if (((int)*s + (int)midvalue) > 255)
  2020.             {
  2021.               *s = 255;
  2022.             }
  2023.           else
  2024.             {
  2025.               *s = midvalue + *s;
  2026.             }
  2027.  
  2028.           slopeterm = dx;
  2029.           
  2030.           j = 1;
  2031.           
  2032.           while(y > y1)
  2033.             {
  2034.               if (slopeterm > dy)
  2035.             {
  2036.               slopeterm -= dy;
  2037.               x--;
  2038.               y--;
  2039.             }
  2040.               else
  2041.             {
  2042.               slopeterm += dx;
  2043.               y--;
  2044.             }
  2045.  
  2046.               j++;
  2047.  
  2048.               s = dest->data;
  2049.               s += y * rowlength + x;
  2050.               
  2051.               ivalue = value * (1.0 - (double)j/(double)steps);
  2052.  
  2053.               if(((int)*s + ivalue) > 255)
  2054.             {
  2055.               *s = 255;  
  2056.             }
  2057.               else
  2058.             {
  2059.             *s = *s + ivalue;
  2060.             }
  2061.               nothing = FALSE;
  2062.  
  2063.             }
  2064.         }
  2065.           else
  2066.         {
  2067.           /* 
  2068.              We have to x++
  2069.           */
  2070.           s = dest->data;
  2071.  
  2072.           x = x0;
  2073.           y = y0;
  2074.  
  2075.           s += y * rowlength + x;
  2076.           
  2077.  
  2078.           
  2079.           if (((int)*s + (int)midvalue) > 255)
  2080.             {
  2081.               *s = 255;
  2082.             }
  2083.           else
  2084.             {
  2085.               *s = midvalue + *s;
  2086.             }
  2087.           
  2088.           slopeterm = dx;
  2089.           
  2090.           j = 1;
  2091.           
  2092.           while(y > y1)
  2093.             {
  2094.               if (slopeterm > dy)
  2095.             {
  2096.               slopeterm -= dy;
  2097.               x++;
  2098.               y--;
  2099.             }
  2100.               else
  2101.             {
  2102.               slopeterm += dx;
  2103.               y--;
  2104.             }
  2105.  
  2106.               j++;
  2107.  
  2108.               s = dest->data;
  2109.               s += y * rowlength + x;
  2110.               
  2111.               ivalue = value * (1.0 - (double)j/(double)steps);
  2112.  
  2113.               if(((int)*s + ivalue) > 255)
  2114.             {
  2115.               *s = 255;  
  2116.             }
  2117.               else
  2118.             {
  2119.             *s = *s + ivalue;
  2120.             }
  2121.  
  2122.               nothing = FALSE;
  2123.             }
  2124.         }
  2125.         }
  2126.  
  2127.     }
  2128.       else 
  2129.     {
  2130.       /* Step in the X direction*/
  2131.       if (x0 < x1)
  2132.         {
  2133.           /* Step from x0 to x1 */
  2134.           if(y1 < y0)
  2135.         {
  2136.           /*
  2137.             We have to y--
  2138.           */
  2139.           s = dest->data;
  2140.  
  2141.           x = x0;
  2142.           y = y0;
  2143.  
  2144.           s += y * rowlength + x;
  2145.           
  2146.  
  2147.           
  2148.           if (((int)*s + (int)midvalue) > 255)
  2149.             {
  2150.               *s = 255;
  2151.             }
  2152.           else
  2153.             {
  2154.               *s = midvalue + *s;
  2155.             }
  2156.           slopeterm = dy;
  2157.           
  2158.           j = 1;
  2159.           
  2160.           while(x < x1)
  2161.             {
  2162.               if (slopeterm > dx)
  2163.             {
  2164.               slopeterm -= dx;
  2165.               y--;
  2166.               x++;
  2167.             }
  2168.               else
  2169.             {
  2170.               slopeterm += dy;
  2171.               x++;
  2172.             }
  2173.  
  2174.               j++;
  2175.  
  2176.               s = dest->data;
  2177.               s += y * rowlength + x;
  2178.               
  2179.               ivalue = value * (1.0 - (double)j/(double)steps);
  2180.  
  2181.               if(((int)*s + ivalue) > 255)
  2182.             {
  2183.               *s = 255;  
  2184.             }
  2185.               else
  2186.             {
  2187.             *s = *s + ivalue;
  2188.             }
  2189.               nothing = FALSE;
  2190.             }
  2191.  
  2192.         }
  2193.           else
  2194.         {
  2195.           /* 
  2196.              We have to y++
  2197.           */
  2198.           s = dest->data;
  2199.  
  2200.           x = x0;
  2201.           y = y0;
  2202.  
  2203.           s += y * rowlength + x;
  2204.           
  2205.  
  2206.           
  2207.           if (((int)*s + (int)midvalue) > 255)
  2208.             {
  2209.               *s = 255;
  2210.             }
  2211.           else
  2212.             {
  2213.               *s = midvalue + *s;
  2214.             }
  2215.  
  2216.           
  2217.           slopeterm = dy;
  2218.           
  2219.           j = 1;
  2220.           
  2221.           while(x < x1)
  2222.             {
  2223.               if (slopeterm > dx)
  2224.             {
  2225.               slopeterm -= dx;
  2226.               y++;
  2227.               x++;
  2228.             }
  2229.               else
  2230.             {
  2231.               slopeterm += dy;
  2232.               x++;
  2233.             }
  2234.  
  2235.               j++;
  2236.  
  2237.               s = dest->data;
  2238.               s += y * rowlength + x;
  2239.               
  2240.                 ivalue = value * (1.0 - (double)j/(double)steps);
  2241.  
  2242.               if(((int)*s + ivalue) > 255)
  2243.             {
  2244.               *s = 255;  
  2245.             }
  2246.               else
  2247.             {
  2248.             *s = *s + ivalue;
  2249.             }
  2250.  
  2251.               nothing = FALSE;
  2252.             }
  2253.         }
  2254.         }
  2255.       else
  2256.         {
  2257.           /* Step from x0 to x1 neg direction */
  2258.           if(y1 < y0)
  2259.         {
  2260.           /*
  2261.             We have to y--
  2262.           */
  2263.           s = dest->data;
  2264.  
  2265.           x = x0;
  2266.           y = y0;
  2267.  
  2268.           s += y * rowlength + x;
  2269.           
  2270.           if (((int)*s + (int)midvalue) > 255)
  2271.             {
  2272.               *s = 255;
  2273.             }
  2274.           else
  2275.             {
  2276.               *s = midvalue + *s;
  2277.             }
  2278.  
  2279.  
  2280.           slopeterm = dy;
  2281.           
  2282.           j = 1;
  2283.           
  2284.           while(x > x1)
  2285.             {
  2286.               if (slopeterm > dx)
  2287.             {
  2288.               slopeterm -= dx;
  2289.               y--;
  2290.               x--;
  2291.             }
  2292.               else
  2293.             {
  2294.               slopeterm += dy;
  2295.               x--;
  2296.             }
  2297.  
  2298.               j++;
  2299.  
  2300.               s = dest->data;
  2301.               s += y * rowlength + x;
  2302.               
  2303.                 ivalue = value * (1.0 - (double)j/(double)steps);
  2304.  
  2305.               if(((int)*s + ivalue) > 255)
  2306.             {
  2307.               *s = 255;  
  2308.             }
  2309.               else
  2310.             {
  2311.             *s = *s + ivalue;
  2312.             }
  2313.  
  2314.               nothing = FALSE;
  2315.             }
  2316.         }
  2317.           else
  2318.         {
  2319.           /* 
  2320.              We have to y++
  2321.           */
  2322.           s = dest->data;
  2323.  
  2324.           x = x0;
  2325.           y = y0;
  2326.  
  2327.           s += y * rowlength + x;
  2328.           
  2329.           
  2330.           if (((int)*s + (int)midvalue) > 255)
  2331.             {
  2332.               *s = 255;
  2333.             }
  2334.           else
  2335.             {
  2336.               *s = midvalue + *s;
  2337.             }
  2338.           slopeterm = dy;
  2339.           
  2340.           j = 1;
  2341.           
  2342.           while(x > x1)
  2343.             {
  2344.               if (slopeterm > dx)
  2345.             {
  2346.               slopeterm -= dx;
  2347.               y++;
  2348.               x--;
  2349.             }
  2350.               else
  2351.             {
  2352.               slopeterm += dy;
  2353.               x--;
  2354.             }
  2355.  
  2356.               j++;
  2357.  
  2358.               s = dest->data;
  2359.               s += y * rowlength + x;
  2360.               
  2361.                 ivalue = value * (1.0 - (double)j/(double)steps);
  2362.  
  2363.               if(((int)*s + ivalue) > 255)
  2364.             {
  2365.               *s = 255;  
  2366.             }
  2367.               else
  2368.             {
  2369.             *s = *s + ivalue;
  2370.             }
  2371.  
  2372.               nothing = FALSE;
  2373.             }
  2374.  
  2375.         }
  2376.         }
  2377.     }
  2378.     }
  2379.   
  2380.   if (nothing)
  2381.     {
  2382.       printf("Hmm I retured a nothing brush\n");
  2383.     }
  2384.  
  2385. }
  2386.  
  2387.  
  2388.  
  2389.  
  2390. static void
  2391. xinput_airbrush_set_paint_area (XinputAirbrushTool      *xinput_airbrush_tool,
  2392.                                 GimpDrawable            *drawable,
  2393.                 int x, int y, int width, int height)
  2394. {
  2395.     int iwidth, iheight;
  2396.     int x1, y1, x2, y2;
  2397.     int bytes;
  2398.     int dwidth, dheight;
  2399.  
  2400.     bytes = drawable_has_alpha (drawable) ?
  2401.             drawable_bytes (drawable) : drawable_bytes (drawable) + 1;
  2402.  
  2403.     dwidth = drawable_width (drawable);
  2404.     dheight = drawable_height (drawable);
  2405.  
  2406.  
  2407.     x1 = CLAMP (x,            0, dwidth);
  2408.     y1 = CLAMP (y,            0, dheight);
  2409.     x2 = CLAMP ((x + width),  0, dwidth);
  2410.     y2 = CLAMP ((y + height), 0, dheight);
  2411.  
  2412.     iwidth = MIN((x2 - x1),  width);
  2413.     iheight = MIN((y2 - y1), height);
  2414.  
  2415.  
  2416.  
  2417.     /*  configure the canvas buffer  */
  2418.  
  2419.     if ((x2 - x1) && (y2 - y1))
  2420.       canvas_buf = temp_buf_resize (canvas_buf, bytes, x1, y1,
  2421.                                     iwidth, iheight);
  2422.  
  2423.  
  2424.  
  2425.  
  2426. }
  2427.  
  2428.  
  2429. static void
  2430. render_airbrush_line (AirBrushBlob *airbrush_blob, guchar *dest,
  2431.                         int y, int width, XinputAirbrushTool *xinput_airbrush_tool)
  2432. {
  2433.   int i, j, k, l, m, g;
  2434.   
  2435.   int left, right;
  2436.  
  2437.   int brush_width;
  2438.  
  2439.   double x_dest;
  2440.   double xdist, ydist, dist, angle;
  2441.   double i_value;
  2442.   guchar value;
  2443.  
  2444.  
  2445.  
  2446.   if (airbrush_blob->height <= 6)
  2447.     {
  2448.       return;
  2449.     }
  2450.   left = 0;
  2451.   right = 0;
  2452.  
  2453.   j = y * SUBSAMPLE;
  2454.   g = y * SUBSAMPLE;
  2455.  
  2456.   for (i=0; i<SUBSAMPLE; i++, j++)
  2457.     {
  2458.       if (j >= airbrush_blob->height)
  2459.     {
  2460.       if ( i == 0)
  2461.         {
  2462.           return;
  2463.         }
  2464.       else
  2465.         {
  2466.           left = airbrush_blob->data[g].left - airbrush_blob->min_x;
  2467.           right = airbrush_blob->data[g].right - airbrush_blob->min_x;
  2468.           break;
  2469.         }
  2470.     }
  2471.       if ( i == SUBSAMPLE/2)
  2472.     {
  2473.       left = airbrush_blob->data[j].left - airbrush_blob->min_x;
  2474.       right = airbrush_blob->data[j].right - airbrush_blob->min_x;
  2475.       break;
  2476.     }
  2477.  
  2478.     }
  2479.  
  2480.   brush_width = right - left;
  2481.  
  2482.   if (brush_width <= SUBSAMPLE*2)
  2483.     {
  2484.       return;
  2485.     }
  2486.  
  2487.   dest = dest + (left/SUBSAMPLE);
  2488.  
  2489.   for (i=4; i <= brush_width; i = i + SUBSAMPLE)
  2490.     {
  2491.       x_dest = (left + i)/SUBSAMPLE;
  2492.       xdist  =  (airbrush_blob->min_x/SUBSAMPLE) + x_dest - xinput_airbrush_tool->xcenter;
  2493.       ydist  =  xinput_airbrush_tool->ycenter - (airbrush_blob->y/SUBSAMPLE) - y;
  2494.       angle     =  atan2(ydist, xdist) + G_PI;
  2495.       dist   =  hypot(xdist, ydist);
  2496.       for (j=1; j< (airbrush_blob->height - 1) ; j++)
  2497.     {
  2498.       k = j - 1;
  2499.       l = j + 1;
  2500.       m = 0;
  2501.       if ( (fabs(airbrush_blob->data[k].angle_right_abs - airbrush_blob->data[l].angle_right_abs) >
  2502.         fabs(airbrush_blob->data[k].angle_right_abs - angle)) &&
  2503.            (fabs(airbrush_blob->data[k].angle_right_abs - airbrush_blob->data[l].angle_right_abs) >
  2504.         fabs(airbrush_blob->data[l].angle_right_abs - angle))
  2505.              )
  2506.         {
  2507.           i_value = MIN(1., (dist*SUBSAMPLE)/(airbrush_blob->data[j].dist_right));
  2508.           value = 255 * (1 - i_value);
  2509.           *dest = value;
  2510.           dest++;
  2511.           m = 1;
  2512.           break;
  2513.         }
  2514.  
  2515.       else if ( (fabs(airbrush_blob->data[k].angle_left_abs - airbrush_blob->data[l].angle_left_abs) >
  2516.              fabs(airbrush_blob->data[k].angle_left_abs - angle)) &&
  2517.             (fabs(airbrush_blob->data[k].angle_left_abs - airbrush_blob->data[l].angle_left_abs) >
  2518.              fabs(airbrush_blob->data[l].angle_left_abs - angle))
  2519.           )
  2520.         {
  2521.           i_value = MIN(1., (dist*SUBSAMPLE)/(airbrush_blob->data[j].dist_left));
  2522.           value = 255  * (1 - i_value);
  2523.           *dest = value;
  2524.           dest++;
  2525.           m = 1;
  2526.           break;
  2527.         }
  2528.       else if ((j == (airbrush_blob->height - 2)) && ( m == 0))
  2529.         {
  2530.           *dest = 0;
  2531.           dest++;
  2532.         }
  2533.     }
  2534.     }
  2535.  
  2536.  
  2537. }
  2538.  
  2539.  
  2540.  
  2541. static void
  2542. calc_width (AirBrushBlob *airbrush_blob)
  2543. {
  2544.   int i;
  2545.   int min_x, max_x;
  2546.  
  2547.   i = (airbrush_blob->height)/2;
  2548.  
  2549.   min_x = airbrush_blob->data[i].left;
  2550.   max_x = airbrush_blob->data[i].right;
  2551.  
  2552.   for (i=0; i< (airbrush_blob->height) ; i++)
  2553.  
  2554.     {
  2555.  
  2556.       if ( airbrush_blob->data[i].left <= airbrush_blob->data[i].right)
  2557.     {
  2558.       min_x = MIN(airbrush_blob->data[i].left, min_x);
  2559.     }
  2560.  
  2561.  
  2562.       if ( airbrush_blob->data[i].left <= airbrush_blob->data[i].right)
  2563.     {
  2564.       max_x = MAX(airbrush_blob->data[i].right, max_x );
  2565.     }
  2566.     }
  2567.  
  2568.   airbrush_blob->width = max_x - min_x;
  2569.   airbrush_blob->min_x = min_x;
  2570.   airbrush_blob->max_x = max_x;
  2571. }
  2572.  
  2573.  
  2574. static void
  2575. calc_angle (AirBrushBlob *airbrush_blob, double xcenter, double ycenter)
  2576. {
  2577.  
  2578.  
  2579.   int i;
  2580.   double y_center, x_center;
  2581.  
  2582.   double y_dist, x_dist;
  2583.   double left_ang, right_ang;
  2584.   double left_ang_abs, right_ang_abs;
  2585.   int min_x, max_x;
  2586.  
  2587.  
  2588.  
  2589.   x_center = xcenter * SUBSAMPLE;
  2590.   y_center = ycenter * SUBSAMPLE - airbrush_blob->y;
  2591.  
  2592.   i = (airbrush_blob->height)/2;
  2593.  
  2594.   min_x = airbrush_blob->data[i].left;
  2595.   max_x = airbrush_blob->data[i].right;
  2596.  
  2597.   for (i=0; i< (airbrush_blob->height) ; i++)
  2598.  
  2599.     {
  2600.  
  2601.       y_dist = y_center - i;
  2602.       x_dist = airbrush_blob->data[i].left - x_center;
  2603.       left_ang_abs = atan2(y_dist, x_dist) + G_PI;
  2604.       left_ang = atan(y_dist/x_dist);
  2605.       airbrush_blob->data[i].angle_left = left_ang;
  2606.       airbrush_blob->data[i].angle_left_abs = left_ang_abs;
  2607.       airbrush_blob->data[i].dist_left = hypot(x_dist, y_dist);
  2608.       if ( airbrush_blob->data[i].left <= airbrush_blob->data[i].right)
  2609.     {
  2610.       min_x = MIN(airbrush_blob->data[i].left, min_x);
  2611.     }
  2612.  
  2613.       x_dist = airbrush_blob->data[i].right - x_center;
  2614.       right_ang_abs = atan2(y_dist, x_dist) + G_PI;
  2615.       right_ang = atan(y_dist/x_dist);
  2616.       airbrush_blob->data[i].angle_right = right_ang;
  2617.       airbrush_blob->data[i].angle_right_abs = right_ang_abs;
  2618.       airbrush_blob->data[i].dist_right = hypot(x_dist, y_dist);
  2619.       if ( airbrush_blob->data[i].left <= airbrush_blob->data[i].right)
  2620.     {
  2621.       max_x = MAX(airbrush_blob->data[i].right, max_x );
  2622.     }
  2623.  
  2624.  
  2625.  
  2626.     }
  2627.  
  2628.   airbrush_blob->width = max_x - min_x;
  2629.   airbrush_blob->min_x = min_x;
  2630.   airbrush_blob->max_x = max_x;
  2631.   
  2632.   
  2633. }
  2634.  
  2635.  
  2636.  
  2637. static void
  2638. make_single_mask(AirBrushBlob *airbrush_blob, XinputAirbrushTool *xinput_airbrush_tool, MaskBuf *dest)
  2639. {
  2640.   unsigned char * s;
  2641.   int h;
  2642.   int i;
  2643.  
  2644.   h = dest->height;
  2645.   s = dest->data;
  2646.  
  2647.   for (i=0; i<h; i++)
  2648.     {
  2649.       render_airbrush_line(airbrush_blob, s,
  2650.                i, dest->width, xinput_airbrush_tool);
  2651.       s += (dest->width * dest->bytes);
  2652.     }
  2653.   
  2654. }
  2655.  
  2656. static void
  2657. print_line (guchar *dest,
  2658.         int width)
  2659. {
  2660.   
  2661.   int i;
  2662.  
  2663.   for (i=0; i < width; i++)
  2664.     {
  2665.       printf("%d       ", *dest);
  2666.       dest++;
  2667.     }
  2668. }
  2669.  
  2670. static void
  2671. print_mask(MaskBuf *dest)
  2672. {
  2673.   unsigned char * s;
  2674.   int h;
  2675.   int i;
  2676.  
  2677.   h = dest->height;
  2678.   s = dest->data;
  2679.   printf("\nBrush_Mask\n\n");
  2680.   for (i=0; i<h; i++)
  2681.     {
  2682.       print_line( s,
  2683.           dest->width);
  2684.       s += (dest->width * dest->bytes);
  2685.       printf("\n");
  2686.     }
  2687.    printf("\n");
  2688. }
  2689.  
  2690. static inline MaskBuf * 
  2691. blur_mask_1(MaskBuf *mask)
  2692. {
  2693.   MaskBuf *retur;
  2694.   int y, x;
  2695.   unsigned char *source;
  2696.   unsigned char *dest;
  2697.   int height, width;  
  2698.  
  2699.   retur = mask_buf_new(mask->width, mask->height);
  2700.  
  2701.   source = mask->data;
  2702.   dest = retur->data;
  2703.   
  2704.   width = mask->width;
  2705.   height = mask->height;
  2706.  
  2707.   for (y = 1; y < (height -1); y++)
  2708.     {
  2709.       for(x = 1; x < (width -1); x++)
  2710.     {
  2711.     
  2712.       dest[(y * retur->width + x)] = (
  2713.                       2 * source[((y-1) * width) + x - 1] +
  2714.                       2 * source[((y-1) * width) + x] +
  2715.                       2 * source[((y-1) * width) + x + 1] +
  2716.                       2 * source[(y * width) + x - 1] +
  2717.                       source[(y * width) + x] +
  2718.                       2 * source[(y * width) + x + 1] +
  2719.                       2 * source[((y+1) * width) + x - 1] +
  2720.                       2 * source[((y+1) * width) + x] +
  2721.                       source[((y+1) * width) + x + 1])/16;
  2722.     }
  2723.     }
  2724.   
  2725.   mask_buf_free(mask);
  2726.   return retur;
  2727. }
  2728.  
  2729.  
  2730. static inline MaskBuf * 
  2731. blur_mask_2(MaskBuf *mask)
  2732. {
  2733.   MaskBuf *retur;
  2734.   int y, x;
  2735.   unsigned char *source;
  2736.   unsigned char *dest;
  2737.   int height, width;  
  2738.  
  2739.   retur = mask_buf_new(mask->width, mask->height);
  2740.  
  2741.   source = mask->data;
  2742.   dest = retur->data;
  2743.   
  2744.   width = mask->width;
  2745.   height = mask->height;
  2746.  
  2747.   for (y = 2; y < (height -2); y++)
  2748.     {
  2749.       for(x = 2; x < (width -2); x++)
  2750.     {
  2751.     
  2752.       dest[(y * retur->width + x)] = (
  2753.  
  2754.                       
  2755.  
  2756.                       2 * source[((y-2) * width) + x - 2] +
  2757.                       2 * source[((y-2) * width) + x - 1] +
  2758.                       2 * source[((y-2) * width) + x] +
  2759.                       2 * source[((y-2) * width) + x + 1] +
  2760.                       2 * source[((y-2) * width) + x + 2] +
  2761.                       
  2762.                        2 * source[((y-1) * width) + x - 2] +
  2763.                       4 * source[((y-1) * width) + x - 1] +
  2764.                       4 * source[((y-1) * width) + x] +
  2765.                       4 * source[((y-1) * width) + x + 1] +
  2766.                       2 * source[((y-1) * width) + x + 2] +
  2767.  
  2768.                       2 * source[(y * width) + x - 2] +
  2769.                       4 * source[(y * width) + x - 1] +
  2770.                       1 * source[(y * width) + x] +
  2771.                       4 * source[(y * width) + x + 1] +
  2772.                       1 * source[(y * width) + x + 2] +
  2773.  
  2774.                        2 * source[((y+1) * width) + x - 2] +
  2775.                       4 * source[((y+1) * width) + x - 1] +
  2776.                       4 * source[((y+1) * width) + x] +
  2777.                       4 * source[((y+1) * width) + x + 1] +
  2778.                       2 * source[((y+1) * width) + x + 2] +
  2779.  
  2780.                       2 * source[((y+2) * width) + x - 2] +
  2781.                       2 * source[((y+2) * width) + x - 1] +
  2782.                       2 * source[((y+2) * width) + x] +
  2783.                       2 * source[((y+2) * width) + x + 1] +
  2784.                       2 * source[((y+2) * width) + x + 2]
  2785.                       
  2786.                       )/64;
  2787.  
  2788.     }
  2789.     }
  2790.   
  2791.   mask_buf_free(mask);
  2792.   return retur;
  2793.   }
  2794.  
  2795.  
  2796.  
  2797. static inline MaskBuf * 
  2798. blur_mask_3(MaskBuf *mask)
  2799. {
  2800.   MaskBuf *retur;
  2801.   int y, x, y_3, y_2, y_1, y1, y2, y3, x_3, x_2,x_1, x1, x2,  x3;
  2802.   unsigned char *source;
  2803.   unsigned char *dest;
  2804.   int height, width, max_height, max_width;  
  2805.  
  2806.   retur = mask_buf_new(mask->width, mask->height);
  2807.  
  2808.   source = mask->data;
  2809.   dest = retur->data;
  2810.   
  2811.   width = mask->width;
  2812.   height = mask->height;
  2813.  
  2814.   max_height = width * height - 3 * width;
  2815.   max_width = width  - 3;
  2816.  
  2817.  
  2818.   y_3 = 0;
  2819.   y_2 = width;
  2820.   y_1 = 2 * width;
  2821.   y  = 3 * width;
  2822.   y1 = 4 * width;
  2823.   y2 = 5 * width;
  2824.   y3 = 6 * width;
  2825.  
  2826.  
  2827.   for (; y < max_height; y_3 += width, y_2 += width, y_1 += width, y += width, y1 += width, y2 += width, y3 += width)
  2828.     {
  2829.       for( x_3 = 0, x_2 = 1, x_1 = 2, x = 3, x1 = 4, x2 = 5 , x3 = 6; x < max_width; x_3++, x_2++, x_1++, x++, x1++, x2++, x3++)
  2830.     {
  2831.       
  2832.       dest[y + x] = (
  2833.             
  2834.                       1 * source[y_3 + x_3] +
  2835.                       1 * source[y_3 + x_2] +
  2836.                       1 * source[y_3 + x_1] +
  2837.                       1 * source[y_3 + x]  +
  2838.                       1 * source[y_3 + x1] +
  2839.                       1 * source[y_3 + x2] +
  2840.                       1 * source[y_3 + x3] +
  2841.                                            
  2842.                       2 * source[y_2 + x_3] +
  2843.                       2 * source[y_2 + x_2] +
  2844.                       2 * source[y_2 + x_1] +
  2845.                       2 * source[y_2 + x]  +
  2846.                       2 * source[y_2 + x1] +
  2847.                       2 * source[y_2 + x2] +
  2848.                       2 * source[y_2 + x3] +
  2849.  
  2850.                       2 * source[y_1 + x_3] +
  2851.                        2 * source[y_1 + x_2] +
  2852.                       8 * source[y_1 + x_1] +
  2853.                       8 * source[y_1 + x]  +
  2854.                       8 * source[y_1 + x1] +
  2855.                       2 * source[y_1 + x2] +
  2856.                       2 * source[y_1 + x3] +
  2857.  
  2858.                       1 * source[y + x_3] +
  2859.                       2 * source[y + x_2] +
  2860.                       8 * source[y + x_1] +
  2861.                       1 * source[y + x]  +
  2862.                       8 * source[y + x1] +
  2863.                       2 * source[y + x2] +
  2864.                       1 * source[y + x3] +
  2865.  
  2866.                       2 * source[y1 + x_3] +
  2867.                        2 * source[y1 + x_2] +
  2868.                       8 * source[y1 + x_1] +
  2869.                       8 * source[y1 + x]  +
  2870.                       8 * source[y1 + x1] +
  2871.                       2 * source[y1 + x2] +
  2872.                       2 * source[y1 + x3] +
  2873.  
  2874.                       2 * source[y2 + x_3] +
  2875.                       2 * source[y2 + x_2] +
  2876.                       2 * source[y2 + x_1] +
  2877.                       1 * source[y2 + x]  +
  2878.                       2 * source[y2 + x1] +
  2879.                       2 * source[y2 + x2] +
  2880.                       2 * source[y2 + x3] +
  2881.                       
  2882.                       1 * source[y3 + x_3] +
  2883.                       1 * source[y3 + x_2] +
  2884.                       1 * source[y3 + x_1] +
  2885.                       1 * source[y3 + x]  +
  2886.                       1 * source[y3 + x1] +
  2887.                       1 * source[y3 + x2] +
  2888.                       1 * source[y3 + x3] 
  2889.  
  2890.                       )/128;
  2891.  
  2892.     }
  2893.     }
  2894.   
  2895.   mask_buf_free(mask);
  2896.   return retur;
  2897. }
  2898.  
  2899.  
  2900. MaskBuf *
  2901. blur_mask_4(MaskBuf *mask)
  2902. {
  2903.   MaskBuf *retur;
  2904.   int y, x, x_4, x_3, x_2,x_1, x1, x2, x3, x4,x5;
  2905.   unsigned char *source;
  2906.   unsigned char *dest;
  2907.   int height, width, max_height, max_width;  
  2908.  
  2909.   retur = mask_buf_new(mask->width, mask->height);
  2910.  
  2911.   source = mask->data;
  2912.   dest = retur->data;
  2913.   
  2914.   width = mask->width;
  2915.   height = mask->height;
  2916.  
  2917.   max_height = width * height - 3 * width;
  2918.   max_width = width  - 5;
  2919.  
  2920.  
  2921.   y  = width;
  2922.  
  2923.  
  2924.   for (y=0 ; y < max_height; y += width)
  2925.     {
  2926.       for( x_4 = 0, x_3 = 1, x_2 = 2, x_1 = 3, x = 4, x1 = 5, x2 = 6 , x3 = 7, x4 = 8, x5 = 9;  x < max_width; x_3++, x_2++, x_1++, x++, x1++, x2++, x3++)
  2927.     {
  2928.       
  2929.       dest[y + x] = (
  2930.             
  2931.              4  * source[y + x_4] +
  2932.              4 * source[y + x_3] +
  2933.              8 * source[y + x_2] +
  2934.              16 * source[y + x_1] +
  2935.              1 * source[y + x]  +
  2936.              16 * source[y + x1] +
  2937.              8 * source[y + x2] +
  2938.              4 * source[y + x3] +
  2939.              2 * source[y + x4] +
  2940.              1 * source[y + x5] 
  2941.              
  2942.  
  2943.              )/64;
  2944.  
  2945.     }
  2946.     }
  2947.   
  2948.   mask_buf_free(mask);
  2949.   return retur;
  2950. }
  2951.  
  2952. static void
  2953. xinput_airbrush_paste (XinputAirbrushTool      *xinput_airbrush_tool,
  2954.                GimpDrawable *drawable,
  2955.                MaskBuf *brush_mask_in,
  2956.                int x,
  2957.                int y,
  2958.                int width,
  2959.                int height)
  2960. {
  2961.   GImage *gimage;
  2962.   PixelRegion srcPR;
  2963.   int offx, offy;
  2964.   int size;
  2965.   unsigned char col[MAX_CHANNELS];
  2966.   MaskBuf *brush_mask1;
  2967.   MaskBuf *brush_mask3; 
  2968.   MaskBuf *brush_mask2;
  2969.   MaskBuf *brush_mask;
  2970.   int i;
  2971.  
  2972.  
  2973.   if (! (gimage = drawable_gimage (drawable)))
  2974.     return;
  2975.  
  2976.   
  2977.   size = height * width;
  2978.   brush_mask1 = mask_buf_new(brush_mask_in->width, brush_mask_in->height);
  2979.   brush_mask1 = temp_buf_copy(brush_mask_in, brush_mask1); 
  2980.  
  2981.   /* printf("Size: %d\n", size); */
  2982.  
  2983.   if (size <= 40)
  2984.     {
  2985.       brush_mask1 = blur_mask_1(brush_mask1);
  2986.     }
  2987.   else if ((size >= 40) && (size <= 90))
  2988.     {
  2989.    
  2990.    
  2991.       for(i = 0 ; i < 2; i++)
  2992.     {
  2993.       brush_mask2 = blur_mask_1(brush_mask1);
  2994.       brush_mask3 = blur_mask_1(brush_mask2);
  2995.       brush_mask1 = blur_mask_1(brush_mask3);
  2996.     }
  2997.     }
  2998.   else if ((size >= 91) && (size <= 1000))
  2999.     {  
  3000.       
  3001.       
  3002.       for(i = 0 ; i < 2; i++)
  3003.     {
  3004.       brush_mask2 = blur_mask_3(brush_mask1);
  3005.       brush_mask3 = blur_mask_3(brush_mask2);
  3006.       brush_mask1 = blur_mask_3(brush_mask3);
  3007.           
  3008.     }
  3009.       
  3010.     }
  3011.   else if ((size >= 1001) && (size <= 5000))
  3012.     {
  3013.       for(i = 0 ; i < 4; i++)
  3014.     {
  3015.       brush_mask2 = blur_mask_4(brush_mask1);
  3016.       brush_mask3 = blur_mask_4(brush_mask2);
  3017.       brush_mask1 = blur_mask_3(brush_mask3);
  3018.           
  3019.     }
  3020.     }
  3021.  
  3022.   else if (size >= 5001)
  3023.     {
  3024.       for(i = 0 ; i < 5; i++)
  3025.     {
  3026.       brush_mask2 = blur_mask_3(brush_mask1);
  3027.       brush_mask3 = blur_mask_4(brush_mask2);
  3028.       brush_mask1 = blur_mask_3(brush_mask3);
  3029.           
  3030.     }
  3031.     }
  3032.      
  3033.  
  3034.   brush_mask = brush_mask1;  
  3035.  
  3036.   /* Get the the buffer */
  3037.   xinput_airbrush_set_paint_area (xinput_airbrush_tool, drawable, x - 1, y - 1, width, height);
  3038.  
  3039.   /* check to make sure there is actually a canvas to draw on */
  3040.   if (!canvas_buf)
  3041.     return;  
  3042.   
  3043.   gimage_get_foreground (gimage, drawable, col);
  3044.  
  3045.   /*  set the alpha channel  */
  3046.   col[canvas_buf->bytes - 1] = OPAQUE_OPACITY;
  3047.  
  3048.   /*  color the pixels  */
  3049.   color_pixels (temp_buf_data (canvas_buf), col,
  3050.         canvas_buf->width * canvas_buf->height, canvas_buf->bytes);
  3051.  
  3052.   /*  set undo blocks  */
  3053.   xinput_airbrush_set_undo_tiles (drawable,
  3054.                   canvas_buf->x, canvas_buf->y,
  3055.                   canvas_buf->width, canvas_buf->height);
  3056.  
  3057.   /*  initialize any invalid canvas tiles  */
  3058.   xinput_airbrush_set_canvas_tiles (canvas_buf->x, canvas_buf->y,
  3059.                     canvas_buf->width, canvas_buf->height);
  3060.  
  3061.   /*DON'T FORGETT THE 100 VALUE, I.E. THE BRUSH OPACITY WHICH SHOULD BE 255*/
  3062.  
  3063.   xinput_airbrush_to_canvas_tiles (xinput_airbrush_tool, brush_mask, 255);
  3064.   
  3065.   xinput_airbrush_canvas_tiles_to_canvas_buf(xinput_airbrush_tool);
  3066.  
  3067.  
  3068.   /*  initialize canvas buf source pixel regions  */
  3069.   srcPR.bytes = canvas_buf->bytes;
  3070.   srcPR.x = 0;
  3071.   srcPR.y = 0;
  3072.   srcPR.w = canvas_buf->width;
  3073.   srcPR.h = canvas_buf->height;
  3074.   srcPR.rowstride = canvas_buf->width * canvas_buf->bytes;
  3075.   srcPR.data = temp_buf_data (canvas_buf);
  3076.  
  3077.  
  3078.   /*  apply the paint area to the gimage  */
  3079.   gimage_apply_image (gimage, drawable, &srcPR,
  3080.               FALSE,
  3081.               (int) (gimp_context_get_opacity (NULL) * 255),
  3082.               gimp_context_get_paint_mode (NULL),
  3083.               undo_tiles,  /*  specify an alternative src1  */
  3084.               canvas_buf->x, canvas_buf->y);
  3085.  
  3086.   /*  Update the undo extents  */
  3087.   xinput_airbrush_tool->x1 = MIN (xinput_airbrush_tool->x1, canvas_buf->x);
  3088.   xinput_airbrush_tool->y1 = MIN (xinput_airbrush_tool->y1, canvas_buf->y);
  3089.   xinput_airbrush_tool->x2 = MAX (xinput_airbrush_tool->x2, (canvas_buf->x + canvas_buf->width));
  3090.   xinput_airbrush_tool->y2 = MAX (xinput_airbrush_tool->y2, (canvas_buf->y + canvas_buf->height));
  3091.  
  3092.   /*  Update the gimage--it is important to call gdisplays_update_area
  3093.    *  instead of drawable_update because we don't want the drawable
  3094.    *  preview to be constantly invalidated
  3095.    */
  3096.   drawable_offsets (drawable, &offx, &offy);
  3097.   gdisplays_update_area (gimage, canvas_buf->x + offx, canvas_buf->y + offy,
  3098.                            canvas_buf->width, canvas_buf->height);
  3099.  
  3100.   mask_buf_free(brush_mask);
  3101.  
  3102. }
  3103.  
  3104.  
  3105.  
  3106. static void
  3107. xinput_airbrush_to_canvas_tiles (XinputAirbrushTool *xinput_airbrush_tool,
  3108.                  MaskBuf *brush_mask,
  3109.                  int brush_opacity)
  3110. {
  3111.   PixelRegion srcPR, maskPR;
  3112.  
  3113.   /*   combine the brush mask and the canvas tiles  */
  3114.   pixel_region_init (&srcPR, canvas_tiles,
  3115.              canvas_buf->x, canvas_buf->y,
  3116.              canvas_buf->width, canvas_buf->height, TRUE);
  3117.  
  3118.   maskPR.bytes = 1;
  3119.   maskPR.x = 0;
  3120.   maskPR.y = 0;
  3121.   maskPR.w = srcPR.w;
  3122.   maskPR.h = srcPR.h;
  3123.   maskPR.rowstride = maskPR.bytes * brush_mask->width;
  3124.   maskPR.data = mask_buf_data (brush_mask);
  3125.  
  3126.   /*  combine the mask to the canvas tiles  */
  3127.   combine_mask_and_region (&srcPR, &maskPR, brush_opacity);
  3128. }
  3129.  
  3130.  
  3131.  
  3132.  
  3133. static void
  3134. xinput_airbrush_canvas_tiles_to_canvas_buf(XinputAirbrushTool *xinput_airbrush_tool)
  3135. {
  3136.   PixelRegion srcPR, maskPR;
  3137.  
  3138.   /*  combine the canvas tiles and the canvas buf  */
  3139.   srcPR.bytes = canvas_buf->bytes;
  3140.   srcPR.x = 0;
  3141.   srcPR.y = 0;
  3142.   srcPR.w = canvas_buf->width;
  3143.   srcPR.h = canvas_buf->height;
  3144.   srcPR.rowstride = canvas_buf->width * canvas_buf->bytes;
  3145.   srcPR.data = temp_buf_data (canvas_buf);
  3146.  
  3147.   pixel_region_init (&maskPR, canvas_tiles,
  3148.              canvas_buf->x, canvas_buf->y,
  3149.              canvas_buf->width, canvas_buf->height, FALSE);
  3150.  
  3151.   /*  apply the canvas tiles to the canvas buf  */
  3152.   apply_mask_to_region (&srcPR, &maskPR, OPAQUE_OPACITY);
  3153. }
  3154.  
  3155.  
  3156.  
  3157. static void
  3158. xinput_airbrush_set_undo_tiles (GimpDrawable *drawable,
  3159.                 int x, int y, int w, int h)
  3160. {
  3161.   int i, j;
  3162.   Tile *src_tile;
  3163.   Tile *dest_tile;
  3164.  
  3165.   for (i = y; i < (y + h); i += (TILE_HEIGHT - (i % TILE_HEIGHT)))
  3166.     {
  3167.       for (j = x; j < (x + w); j += (TILE_WIDTH - (j % TILE_WIDTH)))
  3168.     {
  3169.       dest_tile = tile_manager_get_tile (undo_tiles, j, i, FALSE, FALSE);
  3170.       if (tile_is_valid (dest_tile) == FALSE)
  3171.         {
  3172.           src_tile = tile_manager_get_tile (drawable_data (drawable), j, i, TRUE, FALSE);
  3173.           tile_manager_map_tile (undo_tiles, j, i, src_tile);
  3174.           tile_release (src_tile, FALSE);
  3175.         }
  3176.     }
  3177.     }
  3178. }
  3179.  
  3180.  
  3181. static void
  3182. xinput_airbrush_set_canvas_tiles (int x, int y, int w, int h)
  3183. {
  3184.   int i, j;
  3185.   Tile *tile;
  3186.  
  3187.   for (i = y; i < (y + h); i += (TILE_HEIGHT - (i % TILE_HEIGHT)))
  3188.     {
  3189.       for (j = x; j < (x + w); j += (TILE_WIDTH - (j % TILE_WIDTH)))
  3190.     {
  3191.       tile = tile_manager_get_tile (canvas_tiles, j, i, FALSE, FALSE);
  3192.       if (tile_is_valid (tile) == FALSE)
  3193.         {
  3194.           tile = tile_manager_get_tile (canvas_tiles, j, i, TRUE, TRUE);
  3195.           memset (tile_data_pointer (tile, 0, 0),
  3196.               0,
  3197.               tile_size (tile));
  3198.           tile_release (tile, TRUE);
  3199.         }
  3200.     }
  3201.     }
  3202. }
  3203.  
  3204. /**************************/
  3205. /*  Global xinput_airbrush functions  */
  3206. /**************************/
  3207.  
  3208. void
  3209. xinput_airbrush_no_draw (Tool *tool)
  3210. {
  3211.   return;
  3212. }
  3213.  
  3214. Tool *
  3215. tools_new_xinput_airbrush (void)
  3216. {
  3217.   Tool * tool;
  3218.   XinputAirbrushTool * private;
  3219.  
  3220.   /*  The tool options  */
  3221.   if (! xinput_airbrush_options)
  3222.     {
  3223.       xinput_airbrush_options = xinput_airbrush_options_new ();
  3224.       tools_register (XINPUT_AIRBRUSH, (ToolOptions *) xinput_airbrush_options);
  3225.  
  3226.       /*  press all default buttons  */
  3227.       xinput_airbrush_options_reset ();
  3228.     }
  3229.  
  3230.   tool = tools_new_tool (XINPUT_AIRBRUSH);
  3231.   private = g_new0 (XinputAirbrushTool, 1);
  3232.  
  3233.   private->core = draw_core_new (xinput_airbrush_no_draw);
  3234.   private->last_airbrush_blob = NULL;
  3235.  
  3236.   tool->private = private;
  3237.  
  3238.   tool->button_press_func   = xinput_airbrush_button_press;
  3239.   tool->button_release_func = xinput_airbrush_button_release;
  3240.   tool->motion_func         = xinput_airbrush_motion;
  3241.   tool->cursor_update_func  = xinput_airbrush_cursor_update;
  3242.   tool->control_func        = xinput_airbrush_control;
  3243.  
  3244.   return tool;
  3245. }
  3246.  
  3247. void
  3248. tools_free_xinput_airbrush(Tool *tool)
  3249. {
  3250.   XinputAirbrushTool * xinput_airbrush_tool;
  3251.  
  3252.   xinput_airbrush_tool = (XinputAirbrushTool *) tool->private;
  3253.  
  3254.   /*  Make sure the selection core is not visible  */
  3255.   if (tool->state == ACTIVE && xinput_airbrush_tool->core)
  3256.     draw_core_stop (xinput_airbrush_tool->core, tool);
  3257.  
  3258.   /*  Free the selection core  */
  3259.   if (xinput_airbrush_tool->core)
  3260.     draw_core_free (xinput_airbrush_tool->core);
  3261.   
  3262.   /*  Free the last airbrush_blob, if any */
  3263.   if (xinput_airbrush_tool->last_airbrush_blob)
  3264.     g_free (xinput_airbrush_tool->last_airbrush_blob);
  3265.  
  3266.   /*  Cleanup memory  */
  3267.   xinput_airbrush_cleanup ();
  3268.  
  3269.   /*  Free the paint core  */
  3270.   g_free (xinput_airbrush_tool);
  3271. }
  3272.