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

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 2 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17.  */
  18.  
  19. #include "config.h"
  20.  
  21. #include <stdlib.h>
  22. #include <string.h>
  23.  
  24. #include <glib.h>
  25.  
  26. #include "apptypes.h"
  27.  
  28. #include "appenv.h"
  29. #include "drawable.h"
  30. #include "draw_core.h"
  31. #include "gimage_mask.h"
  32. #include "gimprc.h"
  33. #include "gimpui.h"
  34. #include "ink.h"
  35. #include "paint_options.h"
  36. #include "tools.h"
  37. #include "undo.h"
  38. #include "blob.h"
  39. #include "gdisplay.h"
  40.  
  41. #include "libgimp/gimpintl.h"
  42. #include "libgimp/gimpmath.h"
  43.  
  44. #include "tile.h"            /* ick. */
  45.  
  46. #define SUBSAMPLE 8
  47.  
  48. #define DIST_SMOOTHER_BUFFER 10
  49. #define TIME_SMOOTHER_BUFFER 10
  50.  
  51. /*  the Ink structures  */
  52.  
  53. typedef Blob *(*BlobFunc) (gdouble, gdouble, gdouble, gdouble, gdouble, gdouble);
  54.  
  55. typedef struct _InkTool InkTool;
  56.  
  57. struct _InkTool
  58. {
  59.   DrawCore *core;         /*  Core select object             */
  60.   
  61.   Blob     *last_blob;       /*  blob for last cursor position  */
  62.  
  63.   gint      x1, y1;       /*  image space coordinate         */
  64.   gint      x2, y2;       /*  image space coords             */
  65.  
  66.   /* circular distance history buffer */
  67.   gdouble   dt_buffer[DIST_SMOOTHER_BUFFER];
  68.   gint      dt_index;
  69.  
  70.   /* circular timing history buffer */
  71.   guint32   ts_buffer[TIME_SMOOTHER_BUFFER];
  72.   gint      ts_index;
  73.  
  74.   gdouble   last_time;    /*  previous time of a motion event      */
  75.   gdouble   lastx, lasty; /*  previous position of a motion event  */
  76.  
  77.   gboolean  init_velocity;
  78. };
  79.  
  80. typedef struct _BrushWidget BrushWidget;
  81.  
  82. struct _BrushWidget
  83. {
  84.   GtkWidget   *widget;
  85.   gboolean     state;
  86. };
  87.  
  88. typedef struct _InkOptions InkOptions;
  89.  
  90. struct _InkOptions
  91. {
  92.   PaintOptions  paint_options;
  93.  
  94.   gdouble       size;
  95.   gdouble       size_d;
  96.   GtkObject    *size_w;
  97.  
  98.   gdouble       sensitivity;
  99.   gdouble       sensitivity_d;
  100.   GtkObject    *sensitivity_w;
  101.  
  102.   gdouble       vel_sensitivity;
  103.   gdouble       vel_sensitivity_d;
  104.   GtkObject    *vel_sensitivity_w;
  105.  
  106.   gdouble       tilt_sensitivity;
  107.   gdouble       tilt_sensitivity_d;
  108.   GtkObject    *tilt_sensitivity_w;
  109.  
  110.   gdouble       tilt_angle;
  111.   gdouble       tilt_angle_d;
  112.   GtkObject    *tilt_angle_w;
  113.  
  114.   BlobFunc      function;
  115.   BlobFunc      function_d;
  116.   GtkWidget    *function_w[3];  /* 3 radio buttons */
  117.  
  118.   gdouble       aspect;
  119.   gdouble       aspect_d;
  120.   gdouble       angle;
  121.   gdouble       angle_d;
  122.   BrushWidget  *brush_w;
  123. };
  124.  
  125.  
  126. /* the ink tool options  */
  127. static InkOptions * ink_options = NULL;
  128.  
  129. /* local variables */
  130.  
  131. /*  undo blocks variables  */
  132. static TileManager *  undo_tiles = NULL;
  133.  
  134. /* Tiles used to render the stroke at 1 byte/pp */
  135. static TileManager *  canvas_tiles = NULL;
  136.  
  137. /* Flat buffer that is used to used to render the dirty region
  138.  * for composition onto the destination drawable
  139.  */
  140. static TempBuf *  canvas_buf = NULL;
  141.  
  142.  
  143. /*  local function prototypes  */
  144.  
  145. static void   ink_button_press   (Tool *, GdkEventButton *, gpointer);
  146. static void   ink_button_release (Tool *, GdkEventButton *, gpointer);
  147. static void   ink_motion         (Tool *, GdkEventMotion *, gpointer);
  148. static void   ink_cursor_update  (Tool *, GdkEventMotion *, gpointer);
  149. static void   ink_control        (Tool *, ToolAction,       gpointer);
  150.  
  151. static void    time_smoother_add    (InkTool* ink_tool, guint32 value);
  152. static gdouble time_smoother_result (InkTool* ink_tool);
  153. static void    time_smoother_init   (InkTool* ink_tool, guint32 initval);
  154. static void    dist_smoother_add    (InkTool* ink_tool, gdouble value);
  155. static gdouble dist_smoother_result (InkTool* ink_tool);
  156. static void    dist_smoother_init   (InkTool* ink_tool, gdouble initval);
  157.  
  158. static void ink_init   (InkTool      *ink_tool, 
  159.             GimpDrawable *drawable, 
  160.             double        x, 
  161.             double        y);
  162. static void ink_finish (InkTool      *ink_tool, 
  163.             GimpDrawable *drawable, 
  164.             int           tool_id);
  165. static void ink_cleanup (void);
  166.  
  167. static void ink_type_update               (GtkWidget      *radio_button,
  168.                                            BlobFunc        function);
  169. static GdkPixmap *blob_pixmap (GdkColormap *colormap,
  170.                    GdkVisual   *visual,
  171.                    BlobFunc     function);
  172. static void paint_blob (GdkDrawable  *drawable, 
  173.             GdkGC        *gc,
  174.             Blob         *blob);
  175.  
  176. /*  Rendering functions  */
  177. static void ink_set_paint_area  (InkTool      *ink_tool, 
  178.                  GimpDrawable *drawable, 
  179.                  Blob         *blob);
  180. static void ink_paste           (InkTool      *ink_tool, 
  181.                  GimpDrawable *drawable,
  182.                  Blob         *blob);
  183.  
  184. static void ink_to_canvas_tiles (InkTool      *ink_tool,
  185.                  Blob         *blob,
  186.                  guchar       *color);
  187.  
  188. static void ink_set_undo_tiles  (GimpDrawable *drawable,
  189.                  int           x, 
  190.                  int           y,
  191.                  int           w, 
  192.                  int           h);
  193. static void ink_set_canvas_tiles(int           x, 
  194.                  int           y,
  195.                  int           w, 
  196.                  int           h);
  197.  
  198. /*  Brush pseudo-widget callbacks  */
  199. static void brush_widget_active_rect      (BrushWidget    *brush_widget,
  200.                        GtkWidget      *w, 
  201.                        GdkRectangle   *rect);
  202. static void brush_widget_realize          (GtkWidget      *w);
  203. static void brush_widget_expose           (GtkWidget      *w, 
  204.                        GdkEventExpose *event,
  205.                        BrushWidget    *brush_widget);
  206. static void brush_widget_button_press     (GtkWidget      *w, 
  207.                        GdkEventButton *event,
  208.                        BrushWidget    *brush_widget);
  209. static void brush_widget_button_release   (GtkWidget      *w,  
  210.                        GdkEventButton *event,
  211.                        BrushWidget    *brush_widget);
  212. static void brush_widget_motion_notify    (GtkWidget      *w, 
  213.                        GdkEventMotion *event,
  214.                        BrushWidget    *brush_widget);
  215.  
  216.  
  217. /*  functions  */
  218.  
  219. static void 
  220. ink_type_update (GtkWidget      *radio_button,
  221.          BlobFunc        function)
  222. {
  223.   InkOptions *options = ink_options;
  224.  
  225.   if (GTK_TOGGLE_BUTTON (radio_button)->active)
  226.     options->function = function;
  227.  
  228.   gtk_widget_queue_draw (options->brush_w->widget);
  229. }
  230.  
  231. static void
  232. ink_options_reset (void)
  233. {
  234.   InkOptions *options = ink_options;
  235.  
  236.   paint_options_reset ((PaintOptions *) options);
  237.  
  238.   gtk_adjustment_set_value (GTK_ADJUSTMENT (options->size_w),
  239.                 options->size_d);
  240.   gtk_adjustment_set_value (GTK_ADJUSTMENT (options->sensitivity_w),
  241.                 options->sensitivity_d);
  242.   gtk_adjustment_set_value (GTK_ADJUSTMENT (options->tilt_sensitivity_w),
  243.                 options->tilt_sensitivity_d);
  244.   gtk_adjustment_set_value (GTK_ADJUSTMENT (options->vel_sensitivity_w),
  245.                 options->vel_sensitivity_d);
  246.   gtk_adjustment_set_value (GTK_ADJUSTMENT (options->tilt_angle_w),
  247.                 options->tilt_angle_d);
  248.   gtk_toggle_button_set_active (((options->function_d == blob_ellipse) ?
  249.                  GTK_TOGGLE_BUTTON (options->function_w[0]) :
  250.                  ((options->function_d == blob_square) ?
  251.                   GTK_TOGGLE_BUTTON (options->function_w[1]) :
  252.                   GTK_TOGGLE_BUTTON (options->function_w[2]))),
  253.                 TRUE);
  254.   options->aspect = options->aspect_d;
  255.   options->angle  = options->angle_d;
  256.   gtk_widget_queue_draw (options->brush_w->widget);
  257. }
  258.  
  259. static InkOptions *
  260. ink_options_new (void)
  261. {
  262.   InkOptions *options;
  263.  
  264.   GtkWidget *table;
  265.   GtkWidget *vbox;
  266.   GtkWidget *hbox;
  267.   GtkWidget *hbox2;
  268.   GtkWidget *radio_button;
  269.   GtkWidget *pixmap_widget;
  270.   GtkWidget *slider;
  271.   GtkWidget *frame;
  272.   GtkWidget *darea;
  273.   GdkPixmap *pixmap;
  274.  
  275.   /*  the new ink tool options structure  */
  276.   options = g_new (InkOptions, 1);
  277.   paint_options_init ((PaintOptions *) options,
  278.               INK,
  279.               ink_options_reset);
  280.   options->size             = options->size_d             = 4.4;
  281.   options->sensitivity      = options->sensitivity_d      = 1.0;
  282.   options->vel_sensitivity  = options->vel_sensitivity_d  = 0.8;
  283.   options->tilt_sensitivity = options->tilt_sensitivity_d = 0.4;
  284.   options->tilt_angle       = options->tilt_angle_d       = 0.0;
  285.   options->function         = options->function_d         = blob_ellipse;
  286.   options->aspect           = options->aspect_d           = 1.0;
  287.   options->angle            = options->angle_d            = 0.0;
  288.  
  289.   /*  the main vbox  */
  290.   vbox = gtk_vbox_new (FALSE, 2);
  291.   gtk_box_pack_start (GTK_BOX (((ToolOptions *) options)->main_vbox), vbox,
  292.               TRUE, TRUE, 0);
  293.   gtk_widget_show (vbox);
  294.  
  295.   /* adjust sliders */
  296.   frame = gtk_frame_new (_("Adjustment"));
  297.   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  298.   gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, TRUE, 0);
  299.   gtk_widget_show (frame);
  300.  
  301.   table = gtk_table_new (2, 2, FALSE);
  302.   gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  303.   gtk_container_set_border_width (GTK_CONTAINER (table), 2);
  304.   gtk_container_add (GTK_CONTAINER (frame), table);
  305.   gtk_widget_show (table);
  306.  
  307.   /*  size slider  */
  308.   options->size_w =
  309.     gtk_adjustment_new (options->size_d, 0.0, 20.0, 1.0, 2.0, 0.0);
  310.   slider = gtk_hscale_new (GTK_ADJUSTMENT (options->size_w));
  311.   gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  312.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
  313.                  _("Size:"), 1.0, 1.0,
  314.                  slider, 1, FALSE);
  315.   gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  316.   gtk_signal_connect (GTK_OBJECT (options->size_w), "value_changed",
  317.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  318.               &options->size);
  319.  
  320.   /* angle adjust slider */
  321.   options->tilt_angle_w =
  322.     gtk_adjustment_new (options->tilt_angle_d, -90.0, 90.0, 1, 10.0, 0.0);
  323.   slider = gtk_hscale_new (GTK_ADJUSTMENT (options->tilt_angle_w));
  324.   gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  325.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
  326.                  _("Angle:"), 1.0, 1.0,
  327.                  slider, 1, FALSE);
  328.   gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  329.   gtk_signal_connect (GTK_OBJECT (options->tilt_angle_w), "value_changed",
  330.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  331.               &options->tilt_angle);
  332.  
  333.   /* sens sliders */
  334.   frame = gtk_frame_new (_("Sensitivity"));
  335.   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  336.   gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, TRUE, 0);
  337.   gtk_widget_show (frame);
  338.  
  339.   table = gtk_table_new (3, 2, FALSE);
  340.   gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  341.   gtk_container_set_border_width (GTK_CONTAINER (table), 2);
  342.   gtk_container_add (GTK_CONTAINER (frame), table);
  343.   gtk_widget_show (table);
  344.  
  345.   /* size sens slider */
  346.   options->sensitivity_w =
  347.     gtk_adjustment_new (options->sensitivity_d, 0.0, 1.0, 0.01, 0.1, 0.0);
  348.   slider = gtk_hscale_new (GTK_ADJUSTMENT (options->sensitivity_w));
  349.   gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  350.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
  351.                  _("Size:"), 1.0, 1.0,
  352.                  slider, 1, FALSE);
  353.   gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  354.   gtk_signal_connect (GTK_OBJECT (options->sensitivity_w), "value_changed",
  355.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  356.               &options->sensitivity);
  357.   
  358.   /* tilt sens slider */
  359.   options->tilt_sensitivity_w =
  360.     gtk_adjustment_new (options->tilt_sensitivity_d, 0.0, 1.0, 0.01, 0.1, 0.0);
  361.   slider = gtk_hscale_new (GTK_ADJUSTMENT (options->tilt_sensitivity_w));
  362.   gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  363.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 2,
  364.                  _("Tilt:"), 1.0, 1.0,
  365.                  slider, 1, FALSE);
  366.   gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  367.   gtk_signal_connect (GTK_OBJECT (options->tilt_sensitivity_w), "value_changed",
  368.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  369.               &options->tilt_sensitivity);
  370.  
  371.   /* velocity sens slider */
  372.   options->vel_sensitivity_w =
  373.     gtk_adjustment_new (options->vel_sensitivity_d, 0.0, 1.0, 0.01, 0.1, 0.0);
  374.   slider = gtk_hscale_new (GTK_ADJUSTMENT (options->vel_sensitivity_w));
  375.   gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  376.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 3,
  377.                  _("Speed:"), 1.0, 1.0,
  378.                  slider, 1, FALSE);
  379.   gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  380.   gtk_signal_connect (GTK_OBJECT (options->vel_sensitivity_w), "value_changed",
  381.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  382.               &options->vel_sensitivity);
  383.  
  384.   /*  bottom hbox */
  385.   hbox = gtk_hbox_new (FALSE, 2);
  386.   gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
  387.   gtk_widget_show (hbox);
  388.  
  389.   /* Brush type radiobuttons */
  390.   frame = gtk_frame_new (_("Type"));
  391.   gtk_box_pack_start (GTK_BOX (hbox), frame, TRUE, TRUE, 0);
  392.   gtk_widget_show (frame);
  393.  
  394.   hbox2 = gtk_hbox_new (FALSE, 0);
  395.   gtk_container_add (GTK_CONTAINER (frame), hbox2);
  396.   
  397.   vbox = gtk_vbox_new (FALSE, 2);
  398.   gtk_container_set_border_width (GTK_CONTAINER (vbox), 2);
  399.   gtk_box_pack_start (GTK_BOX (hbox2), vbox, FALSE, FALSE, 0);
  400.   gtk_widget_show (vbox);
  401.  
  402.   pixmap = blob_pixmap (gtk_widget_get_colormap (vbox),
  403.             gtk_widget_get_visual (vbox),
  404.             blob_ellipse);
  405.  
  406.   pixmap_widget = gtk_pixmap_new (pixmap, NULL);
  407.   gdk_pixmap_unref (pixmap);
  408.   gtk_misc_set_padding (GTK_MISC (pixmap_widget), 6, 0);
  409.  
  410.   radio_button = gtk_radio_button_new (NULL);
  411.   gtk_signal_connect (GTK_OBJECT (radio_button), "toggled",
  412.               GTK_SIGNAL_FUNC (ink_type_update), 
  413.               (gpointer)blob_ellipse);
  414.  
  415.   gtk_container_add (GTK_CONTAINER (radio_button), pixmap_widget);
  416.   gtk_box_pack_start (GTK_BOX (vbox), radio_button, FALSE, FALSE, 0);
  417.  
  418.   options->function_w[0] = radio_button;
  419.  
  420.   pixmap = blob_pixmap (gtk_widget_get_colormap (vbox),
  421.             gtk_widget_get_visual (vbox),
  422.             blob_square);
  423.  
  424.   pixmap_widget = gtk_pixmap_new (pixmap, NULL);
  425.   gdk_pixmap_unref (pixmap);
  426.   gtk_misc_set_padding (GTK_MISC (pixmap_widget), 6, 0);
  427.  
  428.   radio_button =
  429.     gtk_radio_button_new_from_widget (GTK_RADIO_BUTTON (radio_button));
  430.   gtk_signal_connect (GTK_OBJECT (radio_button), "toggled",
  431.               GTK_SIGNAL_FUNC (ink_type_update), 
  432.               (gpointer)blob_square);
  433.   
  434.   gtk_container_add (GTK_CONTAINER (radio_button), pixmap_widget);
  435.   gtk_box_pack_start (GTK_BOX (vbox), radio_button, FALSE, FALSE, 0);
  436.  
  437.   options->function_w[1] = radio_button;
  438.  
  439.   pixmap = blob_pixmap (gtk_widget_get_colormap (vbox),
  440.             gtk_widget_get_visual (vbox),
  441.             blob_diamond);
  442.  
  443.   pixmap_widget = gtk_pixmap_new (pixmap, NULL);
  444.   gdk_pixmap_unref (pixmap);
  445.   gtk_misc_set_padding (GTK_MISC (pixmap_widget), 6, 0);
  446.  
  447.   radio_button =
  448.     gtk_radio_button_new_from_widget (GTK_RADIO_BUTTON (radio_button));
  449.   gtk_signal_connect (GTK_OBJECT (radio_button), "toggled",
  450.               GTK_SIGNAL_FUNC (ink_type_update), 
  451.               (gpointer)blob_diamond);
  452.  
  453.   gtk_container_add (GTK_CONTAINER (radio_button), pixmap_widget);
  454.   gtk_box_pack_start (GTK_BOX (vbox), radio_button, FALSE, FALSE, 0);
  455.  
  456.   options->function_w[2] = radio_button;
  457.  
  458.   /* Brush shape widget */
  459.   frame = gtk_frame_new (_("Shape"));
  460.   gtk_box_pack_start (GTK_BOX (hbox), frame, TRUE, TRUE, 0);
  461.   gtk_widget_show (frame);
  462.  
  463.   vbox = gtk_vbox_new (FALSE, 2);
  464.   gtk_container_set_border_width (GTK_CONTAINER (vbox), 2);
  465.   gtk_container_add (GTK_CONTAINER (frame), vbox);
  466.   gtk_widget_show (vbox);
  467.  
  468.   frame = gtk_aspect_frame_new (NULL, 0.0, 0.5, 1.0, FALSE);
  469.   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  470.   gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
  471.  
  472.   options->brush_w = g_new (BrushWidget, 1);
  473.   options->brush_w->state = FALSE;
  474.  
  475.   darea = gtk_drawing_area_new();
  476.   options->brush_w->widget = darea;
  477.  
  478.   gtk_drawing_area_size (GTK_DRAWING_AREA (darea), 60, 60);
  479.   gtk_container_add (GTK_CONTAINER (frame), darea);
  480.  
  481.   gtk_widget_set_events (darea, 
  482.              GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
  483.              | GDK_POINTER_MOTION_MASK | GDK_EXPOSURE_MASK);
  484.   gtk_signal_connect (GTK_OBJECT (darea), "button_press_event",
  485.               GTK_SIGNAL_FUNC (brush_widget_button_press),
  486.               options->brush_w);
  487.   gtk_signal_connect (GTK_OBJECT (darea), "button_release_event",
  488.               GTK_SIGNAL_FUNC (brush_widget_button_release),
  489.               options->brush_w);
  490.   gtk_signal_connect (GTK_OBJECT (darea), "motion_notify_event",
  491.               GTK_SIGNAL_FUNC (brush_widget_motion_notify),
  492.               options->brush_w);
  493.   gtk_signal_connect (GTK_OBJECT (darea), "expose_event",
  494.               GTK_SIGNAL_FUNC (brush_widget_expose), 
  495.               options->brush_w);
  496.   gtk_signal_connect (GTK_OBJECT (darea), "realize",
  497.               GTK_SIGNAL_FUNC (brush_widget_realize),
  498.               options->brush_w);
  499.  
  500.   gtk_widget_show_all (hbox);
  501.  
  502.   return options;
  503. }
  504.  
  505.  
  506. /*  the brush widget functions  */
  507.  
  508. static void
  509. brush_widget_active_rect (BrushWidget  *brush_widget,
  510.               GtkWidget    *widget,
  511.               GdkRectangle *rect)
  512. {
  513.   int x,y;
  514.   int r;
  515.  
  516.   r = MIN (widget->allocation.width, widget->allocation.height) / 2;
  517.  
  518.   x = widget->allocation.width / 2 + 0.85 * r * ink_options->aspect / 10.0 *
  519.     cos (ink_options->angle);
  520.   y = widget->allocation.height / 2 + 0.85 * r * ink_options->aspect / 10.0 *
  521.     sin (ink_options->angle);
  522.  
  523.   rect->x = x - 5;
  524.   rect->y = y - 5;
  525.   rect->width = 10;
  526.   rect->height = 10;
  527. }
  528.  
  529. static void
  530. brush_widget_realize (GtkWidget *widget)
  531. {
  532.   gtk_style_set_background (widget->style, widget->window, GTK_STATE_ACTIVE);
  533. }
  534.  
  535. static void
  536. brush_widget_draw_brush (BrushWidget *brush_widget,
  537.              GtkWidget   *widget,
  538.              gdouble      xc,
  539.              gdouble      yc,
  540.              gdouble      radius)
  541. {
  542.   Blob *blob;
  543.  
  544.   blob = ink_options->function (xc, yc,
  545.                 radius * cos (ink_options->angle),
  546.                 radius * sin (ink_options->angle),
  547.                 (- (radius / ink_options->aspect) *
  548.                  sin (ink_options->angle)),
  549.                 ((radius / ink_options->aspect) *
  550.                  cos (ink_options->angle)));
  551.  
  552.   paint_blob (widget->window, widget->style->fg_gc[widget->state], blob);
  553.   g_free (blob);
  554. }
  555.  
  556. static void
  557. brush_widget_expose (GtkWidget      *widget,
  558.              GdkEventExpose *event,
  559.              BrushWidget    *brush_widget)
  560. {
  561.   GdkRectangle rect;
  562.   int r0;
  563.  
  564.   r0 = MIN (widget->allocation.width, widget->allocation.height) / 2;
  565.  
  566.   if (r0 < 2)
  567.     return;
  568.  
  569.   gdk_window_clear_area (widget->window, 0, 0,
  570.              widget->allocation.width,
  571.              widget->allocation.height);
  572.   brush_widget_draw_brush (brush_widget, widget,
  573.                widget->allocation.width / 2,
  574.                widget->allocation.height / 2,
  575.                0.9 * r0);
  576.  
  577.   brush_widget_active_rect (brush_widget, widget, &rect);
  578.   gdk_draw_rectangle (widget->window, widget->style->bg_gc[GTK_STATE_NORMAL],
  579.               TRUE,    /* filled */
  580.               rect.x, rect.y, 
  581.               rect.width, rect.height);
  582.   gtk_draw_shadow (widget->style, widget->window, widget->state, GTK_SHADOW_OUT,
  583.            rect.x, rect.y,
  584.            rect.width, rect.height);
  585. }
  586.  
  587. static void
  588. brush_widget_button_press (GtkWidget      *widget,
  589.                GdkEventButton *event,
  590.                BrushWidget    *brush_widget)
  591. {
  592.   GdkRectangle rect;
  593.  
  594.   brush_widget_active_rect (brush_widget, widget, &rect);
  595.   
  596.   if ((event->x >= rect.x) && (event->x-rect.x < rect.width) &&
  597.       (event->y >= rect.y) && (event->y-rect.y < rect.height))
  598.     {
  599.       brush_widget->state = TRUE;
  600.  
  601.       gtk_grab_add (brush_widget->widget);
  602.     }
  603. }
  604.  
  605. static void
  606. brush_widget_button_release (GtkWidget      *widget,
  607.                  GdkEventButton *event,
  608.                  BrushWidget    *brush_widget)
  609. {
  610.   brush_widget->state = FALSE;
  611.  
  612.   gtk_grab_remove (brush_widget->widget);
  613. }
  614.  
  615. static void
  616. brush_widget_motion_notify (GtkWidget      *widget,
  617.                 GdkEventMotion *event,
  618.                 BrushWidget    *brush_widget)
  619. {
  620.   int x;
  621.   int y;
  622.   int r0;
  623.   int rsquare;
  624.  
  625.   if (brush_widget->state)
  626.     {
  627.       x = event->x - widget->allocation.width / 2;
  628.       y = event->y - widget->allocation.height / 2;
  629.       rsquare = x*x + y*y;
  630.  
  631.       if (rsquare != 0)
  632.     {
  633.       ink_options->angle = atan2 (y, x);
  634.  
  635.       r0 = MIN (widget->allocation.width, widget->allocation.height) / 2;
  636.       ink_options->aspect =
  637.         10.0 * sqrt ((double) rsquare / (r0 * r0)) / 0.85;
  638.  
  639.       if (ink_options->aspect < 1.0)
  640.         ink_options->aspect = 1.0;
  641.       if (ink_options->aspect > 10.0)
  642.         ink_options->aspect = 10.0;
  643.  
  644.       gtk_widget_draw (widget, NULL);
  645.     }
  646.     }
  647. }
  648.  
  649. /*
  650.  * Return a black-on white pixmap in the given colormap and
  651.  * visual that represents the BlobFunc 'function'
  652.  */
  653. static GdkPixmap *
  654. blob_pixmap (GdkColormap *colormap,
  655.          GdkVisual *visual,
  656.          BlobFunc function)
  657. {
  658.   GdkPixmap *pixmap;
  659.   GdkGC *black_gc, *white_gc;
  660.   GdkColor tmp_color;
  661.   Blob *blob;
  662.  
  663.   pixmap = gdk_pixmap_new (NULL, 22, 21, visual->depth);
  664.  
  665.   black_gc = gdk_gc_new (pixmap);
  666.   gdk_color_black (colormap, &tmp_color);
  667.   gdk_gc_set_foreground (black_gc, &tmp_color);
  668.  
  669.   white_gc = gdk_gc_new (pixmap);
  670.   gdk_color_white (colormap, &tmp_color);
  671.   gdk_gc_set_foreground (white_gc, &tmp_color);
  672.  
  673.   gdk_draw_rectangle (pixmap, white_gc, TRUE, 0, 0, 21, 20);
  674.   gdk_draw_rectangle (pixmap, black_gc, FALSE, 0, 0, 21, 20);
  675.   blob = (*function) (10, 10, 8, 0, 0, 8);
  676.   paint_blob (pixmap, black_gc, blob);
  677.   g_free (blob);
  678.  
  679.   gdk_gc_unref (white_gc);
  680.   gdk_gc_unref (black_gc);
  681.  
  682.   return pixmap;
  683. }
  684.  
  685. /*
  686.  * Draw a blob onto a drawable with the specified graphics context
  687.  */
  688. static void
  689. paint_blob (GdkDrawable *drawable, GdkGC *gc,
  690.         Blob *blob)
  691. {
  692.   int i;
  693.  
  694.   for (i=0;i<blob->height;i++)
  695.     {
  696.       if (blob->data[i].left <= blob->data[i].right)
  697.     gdk_draw_line (drawable, gc,
  698.                blob->data[i].left,i+blob->y,
  699.                blob->data[i].right+1,i+blob->y);
  700.     }
  701. }
  702.  
  703.  
  704. static Blob *
  705. ink_pen_ellipse (gdouble x_center, gdouble y_center,
  706.          gdouble pressure, gdouble xtilt, gdouble ytilt,
  707.          gdouble velocity)
  708. {
  709.   double size;
  710.   double tsin, tcos;
  711.   double aspect, radmin;
  712.   double x,y;
  713.   double tscale;
  714.   double tscale_c;
  715.   double tscale_s;
  716.   
  717.   /* Adjust the size depending on pressure. */
  718.  
  719.   size = ink_options->size * (1.0 + ink_options->sensitivity *
  720.                   (2.0 * pressure - 1.0) );
  721.  
  722.   /* Adjust the size further depending on pointer velocity
  723.      and velocity-sensitivity.  These 'magic constants' are
  724.      'feels natural' tigert-approved. --ADM */
  725.  
  726.   if (velocity < 3.0)
  727.     velocity = 3.0;
  728.  
  729. #ifdef VERBOSE
  730.   g_print("%f (%f) -> ", (float)size, (float)velocity);
  731. #endif  
  732.  
  733.   size = ink_options->vel_sensitivity *
  734.     ((4.5 * size) / (1.0 + ink_options->vel_sensitivity * (2.0*(velocity))))
  735.     + (1.0 - ink_options->vel_sensitivity) * size;
  736.  
  737. #ifdef VERBOSE
  738.   g_print("%f\n", (float)size);
  739. #endif
  740.  
  741.   /* Clamp resulting size to sane limits */
  742.  
  743.   if (size > ink_options->size * (1.0 + ink_options->sensitivity))
  744.     size = ink_options->size * (1.0 + ink_options->sensitivity);
  745.  
  746.   if (size*SUBSAMPLE < 1.0) size = 1.0/SUBSAMPLE;
  747.  
  748.   /* Add brush angle/aspect to tilt vectorially */
  749.  
  750.   /* I'm not happy with the way the brush widget info is combined with
  751.      tilt info from the brush. My personal feeling is that representing
  752.      both as affine transforms would make the most sense. -RLL */
  753.  
  754.   tscale = ink_options->tilt_sensitivity * 10.0;
  755.   tscale_c = tscale * cos (gimp_deg_to_rad (ink_options->tilt_angle));
  756.   tscale_s = tscale * sin (gimp_deg_to_rad (ink_options->tilt_angle));
  757.   x = ink_options->aspect*cos(ink_options->angle) +
  758.     xtilt * tscale_c - ytilt * tscale_s;
  759.   y = ink_options->aspect*sin(ink_options->angle) +
  760.     ytilt * tscale_c + xtilt * tscale_s;
  761. #ifdef VERBOSE
  762.   g_print ("angle %g aspect %g; %g %g; %g %g\n",
  763.        ink_options->angle, ink_options->aspect, tscale_c, tscale_s, x, y);
  764. #endif
  765.   aspect = sqrt(x*x+y*y);
  766.  
  767.   if (aspect != 0)
  768.     {
  769.       tcos = x/aspect;
  770.       tsin = y/aspect;
  771.     }
  772.   else
  773.     {
  774.       tsin = sin(ink_options->angle);
  775.       tcos = cos(ink_options->angle);
  776.     }
  777.  
  778.   if (aspect < 1.0) 
  779.     aspect = 1.0;
  780.   else if (aspect > 10.0) 
  781.     aspect = 10.0;
  782.  
  783.   radmin = SUBSAMPLE * size/aspect;
  784.   if (radmin < 1.0) radmin = 1.0;
  785.   
  786.   return ink_options->function (x_center * SUBSAMPLE, y_center * SUBSAMPLE,
  787.                 radmin*aspect*tcos, radmin*aspect*tsin,  
  788.                 -radmin*tsin, radmin*tcos);
  789. }
  790.  
  791. static void
  792. ink_button_press (Tool           *tool,
  793.           GdkEventButton *bevent,
  794.           gpointer        gdisp_ptr)
  795. {
  796.   gdouble       x, y;
  797.   GDisplay     *gdisp;
  798.   InkTool      *ink_tool;
  799.   GimpDrawable *drawable;
  800.   Blob         *b;
  801.  
  802.   gdisp = (GDisplay *) gdisp_ptr;
  803.   ink_tool = (InkTool *) tool->private;
  804.  
  805.   /*  Keep the coordinates of the target  */
  806.   gdisplay_untransform_coords_f (gdisp, bevent->x, bevent->y,
  807.                  &x, &y, TRUE);
  808.   drawable = gimage_active_drawable (gdisp->gimage);
  809.  
  810.   ink_init (ink_tool, drawable, x, y);
  811.  
  812.   tool->state = ACTIVE;
  813.   tool->gdisp_ptr = gdisp_ptr;
  814.   tool->paused_count = 0;
  815.  
  816.   /*  pause the current selection and grab the pointer  */
  817.   gdisplays_selection_visibility (gdisp->gimage, SelectionPause);
  818.  
  819.   /* add motion memory if you press mod1 first ^ perfectmouse */
  820.   if (((bevent->state & GDK_MOD1_MASK) != 0) != (perfectmouse != 0))
  821.     gdk_pointer_grab (gdisp->canvas->window, FALSE,
  822.               GDK_BUTTON1_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
  823.               NULL, NULL, bevent->time);
  824.   else
  825.     gdk_pointer_grab (gdisp->canvas->window, FALSE,
  826.               GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON1_MOTION_MASK |
  827.               GDK_BUTTON_RELEASE_MASK,
  828.               NULL, NULL, bevent->time);
  829.   
  830.   tool->gdisp_ptr = gdisp_ptr;
  831.   tool->state = ACTIVE;
  832.  
  833.   b = ink_pen_ellipse (x, y, 
  834.                bevent->pressure, bevent->xtilt, bevent->ytilt, 10.0);
  835.  
  836.   ink_paste (ink_tool, drawable, b);
  837.   ink_tool->last_blob = b;
  838.  
  839.   time_smoother_init (ink_tool, bevent->time);
  840.   ink_tool->last_time = bevent->time;
  841.   dist_smoother_init (ink_tool, 0.0);
  842.   ink_tool->init_velocity = TRUE;
  843.   ink_tool->lastx = x;
  844.   ink_tool->lasty = y;
  845.  
  846.   gdisplay_flush_now (gdisp);
  847. }
  848.  
  849. static void
  850. ink_button_release (Tool           *tool,
  851.             GdkEventButton *bevent,
  852.             gpointer        gdisp_ptr)
  853. {
  854.   GDisplay * gdisp;
  855.   GImage * gimage;
  856.   InkTool * ink_tool;
  857.  
  858.   gdisp = (GDisplay *) gdisp_ptr;
  859.   gimage = gdisp->gimage;
  860.   ink_tool = (InkTool *) tool->private;
  861.  
  862.   /*  resume the current selection and ungrab the pointer  */
  863.   gdisplays_selection_visibility (gdisp->gimage, SelectionResume);
  864.  
  865.   gdk_pointer_ungrab (bevent->time);
  866.   gdk_flush ();
  867.  
  868.   /*  Set tool state to inactive -- no longer painting */
  869.   tool->state = INACTIVE;
  870.  
  871.   /*  free the last blob  */
  872.   g_free (ink_tool->last_blob);
  873.   ink_tool->last_blob = NULL;
  874.  
  875.   ink_finish (ink_tool, gimage_active_drawable (gdisp->gimage), tool->ID);
  876.   gdisplays_flush ();
  877. }
  878.  
  879.  
  880. static void
  881. dist_smoother_init (InkTool* ink_tool, gdouble initval)
  882. {
  883.   gint i;
  884.  
  885.   ink_tool->dt_index = 0;
  886.  
  887.   for (i=0; i<DIST_SMOOTHER_BUFFER; i++)
  888.     {
  889.       ink_tool->dt_buffer[i] = initval;
  890.     }
  891. }
  892.  
  893. static gdouble
  894. dist_smoother_result (InkTool* ink_tool)
  895. {
  896.   gint i;
  897.   gdouble result = 0.0;
  898.  
  899.   for (i=0; i<DIST_SMOOTHER_BUFFER; i++)
  900.     {
  901.       result += ink_tool->dt_buffer[i];
  902.     }
  903.  
  904.   return (result / (gdouble)DIST_SMOOTHER_BUFFER);
  905. }
  906.  
  907. static void
  908. dist_smoother_add (InkTool* ink_tool, gdouble value)
  909. {
  910.   ink_tool->dt_buffer[ink_tool->dt_index] = value;
  911.  
  912.   if ((++ink_tool->dt_index) == DIST_SMOOTHER_BUFFER)
  913.     ink_tool->dt_index = 0;
  914. }
  915.  
  916.  
  917. static void
  918. time_smoother_init (InkTool* ink_tool, guint32 initval)
  919. {
  920.   gint i;
  921.  
  922.   ink_tool->ts_index = 0;
  923.  
  924.   for (i=0; i<TIME_SMOOTHER_BUFFER; i++)
  925.     {
  926.       ink_tool->ts_buffer[i] = initval;
  927.     }
  928. }
  929.  
  930. static gdouble
  931. time_smoother_result (InkTool* ink_tool)
  932. {
  933.   gint i;
  934.   guint64 result = 0;
  935.  
  936.   for (i=0; i<TIME_SMOOTHER_BUFFER; i++)
  937.     {
  938.       result += ink_tool->ts_buffer[i];
  939.     }
  940.  
  941. #ifdef _MSC_VER
  942.   return (gdouble) (gint64) (result / TIME_SMOOTHER_BUFFER);
  943. #else
  944.   return (result / TIME_SMOOTHER_BUFFER);
  945. #endif
  946. }
  947.  
  948. static void
  949. time_smoother_add (InkTool* ink_tool, guint32 value)
  950. {
  951.   ink_tool->ts_buffer[ink_tool->ts_index] = value;
  952.  
  953.   if ((++ink_tool->ts_index) == TIME_SMOOTHER_BUFFER)
  954.     ink_tool->ts_index = 0;
  955. }
  956.  
  957.  
  958. static void
  959. ink_motion (Tool           *tool,
  960.         GdkEventMotion *mevent,
  961.         gpointer        gdisp_ptr)
  962. {
  963.   GDisplay *gdisp;
  964.   InkTool  *ink_tool;
  965.   GimpDrawable *drawable;
  966.   Blob *b, *blob_union;
  967.  
  968.   double x, y;
  969.   double pressure;
  970.   double velocity;
  971.   double dist;
  972.   gdouble lasttime, thistime;
  973.   
  974.  
  975.   gdisp = (GDisplay *) gdisp_ptr;
  976.   ink_tool = (InkTool *) tool->private;
  977.  
  978.   gdisplay_untransform_coords_f (gdisp, mevent->x, mevent->y, &x, &y, TRUE);
  979.   drawable = gimage_active_drawable (gdisp->gimage);
  980.  
  981.   lasttime = ink_tool->last_time;
  982.  
  983.   time_smoother_add (ink_tool, mevent->time);
  984.   thistime = ink_tool->last_time =
  985.     time_smoother_result(ink_tool);
  986.  
  987.   /* The time resolution on X-based GDK motion events is
  988.      bloody awful, hence the use of the smoothing function.
  989.      Sadly this also means that there is always the chance of
  990.      having an indeterminite velocity since this event and
  991.      the previous several may still appear to issue at the same
  992.      instant. -ADM */
  993.  
  994.   if (thistime == lasttime)
  995.     thistime = lasttime + 1;
  996.  
  997.   if (ink_tool->init_velocity)
  998.     {
  999.       dist_smoother_init (ink_tool, dist = sqrt((ink_tool->lastx-x)*(ink_tool->lastx-x) +
  1000.                         (ink_tool->lasty-y)*(ink_tool->lasty-y)));
  1001.       ink_tool->init_velocity = FALSE;
  1002.     }
  1003.   else
  1004.     {
  1005.       dist_smoother_add (ink_tool, sqrt((ink_tool->lastx-x)*(ink_tool->lastx-x) +
  1006.                     (ink_tool->lasty-y)*(ink_tool->lasty-y)));
  1007.       dist = dist_smoother_result(ink_tool);
  1008.     }
  1009.  
  1010.   ink_tool->lastx = x;
  1011.   ink_tool->lasty = y;
  1012.  
  1013.   pressure = mevent->pressure;
  1014.   velocity = 10.0 * sqrt((dist) / (double)(thistime - lasttime));
  1015.   
  1016.   b = ink_pen_ellipse (x, y, pressure, mevent->xtilt, mevent->ytilt, velocity);
  1017.   blob_union = blob_convex_union (ink_tool->last_blob, b);
  1018.   g_free (ink_tool->last_blob);
  1019.   ink_tool->last_blob = b;
  1020.  
  1021.   ink_paste (ink_tool, drawable, blob_union);  
  1022.   g_free (blob_union);
  1023.   
  1024.   gdisplay_flush_now (gdisp);
  1025. }
  1026.  
  1027. static void
  1028. ink_cursor_update (Tool           *tool,
  1029.            GdkEventMotion *mevent,
  1030.            gpointer        gdisp_ptr)
  1031. {
  1032.   GDisplay *gdisp;
  1033.   Layer *layer;
  1034.   GdkCursorType ctype = GDK_TOP_LEFT_ARROW;
  1035.   int x, y;
  1036.  
  1037.   gdisp = (GDisplay *) gdisp_ptr;
  1038.  
  1039.   gdisplay_untransform_coords (gdisp, mevent->x, mevent->y,
  1040.                    &x, &y, FALSE, FALSE);
  1041.   if ((layer = gimage_get_active_layer (gdisp->gimage))) 
  1042.     {
  1043.       int off_x, off_y;
  1044.  
  1045.       drawable_offsets (GIMP_DRAWABLE (layer), &off_x, &off_y);
  1046.  
  1047.       if (x >= off_x && y >= off_y &&
  1048.       x < (off_x + drawable_width (GIMP_DRAWABLE (layer))) &&
  1049.       y < (off_y + drawable_height (GIMP_DRAWABLE (layer))))
  1050.     {
  1051.       /*  One more test--is there a selected region?
  1052.        *  if so, is cursor inside?
  1053.        */
  1054.       if (gimage_mask_is_empty (gdisp->gimage))
  1055.         ctype = GIMP_MOUSE_CURSOR;
  1056.       else if (gimage_mask_value (gdisp->gimage, x, y))
  1057.         ctype = GIMP_MOUSE_CURSOR;
  1058.     }
  1059.     }
  1060.   gdisplay_install_tool_cursor (gdisp, ctype,
  1061.                 INK,
  1062.                 CURSOR_MODIFIER_NONE,
  1063.                 FALSE);
  1064. }
  1065.  
  1066. static void
  1067. ink_control (Tool       *tool,
  1068.          ToolAction  action,
  1069.          gpointer    gdisp_ptr)
  1070. {
  1071.   InkTool *ink_tool;
  1072.  
  1073.   ink_tool = (InkTool *) tool->private;
  1074.  
  1075.   switch (action)
  1076.     {
  1077.     case PAUSE:
  1078.       draw_core_pause (ink_tool->core, tool);
  1079.       break;
  1080.  
  1081.     case RESUME:
  1082.       draw_core_resume (ink_tool->core, tool);
  1083.       break;
  1084.  
  1085.     case HALT:
  1086.       ink_cleanup ();
  1087.       break;
  1088.  
  1089.     default:
  1090.       break;
  1091.     }
  1092. }
  1093.  
  1094. static void
  1095. ink_init (InkTool *ink_tool, GimpDrawable *drawable, 
  1096.       double x, double y)
  1097. {
  1098.   /*  free the block structures  */
  1099.   if (undo_tiles)
  1100.     tile_manager_destroy (undo_tiles);
  1101.   if (canvas_tiles)
  1102.     tile_manager_destroy (canvas_tiles);
  1103.  
  1104.   /*  Allocate the undo structure  */
  1105.   undo_tiles = tile_manager_new (drawable_width (drawable),
  1106.                  drawable_height (drawable),
  1107.                  drawable_bytes (drawable));
  1108.  
  1109.   /*  Allocate the canvas blocks structure  */
  1110.   canvas_tiles = tile_manager_new (drawable_width (drawable),
  1111.                    drawable_height (drawable), 1);
  1112.  
  1113.   /*  Get the initial undo extents  */
  1114.   ink_tool->x1 = ink_tool->x2 = x;
  1115.   ink_tool->y1 = ink_tool->y2 = y;
  1116. }
  1117.  
  1118. static void
  1119. ink_finish (InkTool *ink_tool, GimpDrawable *drawable, int tool_id)
  1120. {
  1121.   /*  push an undo  */
  1122.   drawable_apply_image (drawable, ink_tool->x1, ink_tool->y1,
  1123.             ink_tool->x2, ink_tool->y2, undo_tiles, TRUE);
  1124.   undo_tiles = NULL;
  1125.  
  1126.   /*  invalidate the drawable--have to do it here, because
  1127.    *  it is not done during the actual painting.
  1128.    */
  1129.   gimp_drawable_invalidate_preview (drawable, TRUE);
  1130. }
  1131.  
  1132. static void
  1133. ink_cleanup (void)
  1134. {
  1135.   /*  CLEANUP  */
  1136.   /*  If the undo tiles exist, nuke them  */
  1137.   if (undo_tiles)
  1138.     {
  1139.       tile_manager_destroy (undo_tiles);
  1140.       undo_tiles = NULL;
  1141.     }
  1142.  
  1143.   /*  If the canvas blocks exist, nuke them  */
  1144.   if (canvas_tiles)
  1145.     {
  1146.       tile_manager_destroy (canvas_tiles);
  1147.       canvas_tiles = NULL;
  1148.     }
  1149.  
  1150.   /*  Free the temporary buffer if it exist  */
  1151.   if (canvas_buf)
  1152.     temp_buf_free (canvas_buf);
  1153.   canvas_buf = NULL;
  1154. }
  1155.  
  1156. /*********************************
  1157.  *  Rendering functions          *
  1158.  *********************************/
  1159.  
  1160. /* Some of this stuff should probably be combined with the 
  1161.  * code it was copied from in paint_core.c; but I wanted
  1162.  * to learn this stuff, so I've kept it simple.
  1163.  *
  1164.  * The following only supports CONSTANT mode. Incremental
  1165.  * would, I think, interact strangely with the way we
  1166.  * do things. But it wouldn't be hard to implement at all.
  1167.  */
  1168.  
  1169. static void
  1170. ink_set_paint_area (InkTool      *ink_tool, 
  1171.             GimpDrawable *drawable, 
  1172.             Blob         *blob)
  1173. {
  1174.   int x, y, width, height;
  1175.   int x1, y1, x2, y2;
  1176.   int bytes;
  1177.   
  1178.   blob_bounds (blob, &x, &y, &width, &height);
  1179.  
  1180.   bytes = drawable_has_alpha (drawable) ?
  1181.     drawable_bytes (drawable) : drawable_bytes (drawable) + 1;
  1182.  
  1183.   x1 = CLAMP (x/SUBSAMPLE - 1,            0, drawable_width (drawable));
  1184.   y1 = CLAMP (y/SUBSAMPLE - 1,            0, drawable_height (drawable));
  1185.   x2 = CLAMP ((x + width)/SUBSAMPLE + 2,  0, drawable_width (drawable));
  1186.   y2 = CLAMP ((y + height)/SUBSAMPLE + 2, 0, drawable_height (drawable));
  1187.  
  1188.   /*  configure the canvas buffer  */
  1189.   if ((x2 - x1) && (y2 - y1))
  1190.     canvas_buf = temp_buf_resize (canvas_buf, bytes, x1, y1,
  1191.                   (x2 - x1), (y2 - y1));
  1192. }
  1193.  
  1194. enum { ROW_START, ROW_STOP };
  1195.  
  1196. /* The insertion sort here, for SUBSAMPLE = 8, tends to beat out
  1197.  * qsort() by 4x with CFLAGS=-O2, 2x with CFLAGS=-g
  1198.  */
  1199. static void insert_sort (int *data, int n)
  1200. {
  1201.   int i, j, k;
  1202.   int tmp1, tmp2;
  1203.  
  1204.   for (i=2; i<2*n; i+=2)
  1205.     {
  1206.       tmp1 = data[i];
  1207.       tmp2 = data[i+1];
  1208.       j = 0;
  1209.       while (data[j] < tmp1)
  1210.     j += 2;
  1211.  
  1212.       for (k=i; k>j; k-=2)
  1213.     {
  1214.       data[k] = data[k-2];
  1215.       data[k+1] = data[k-1];
  1216.     }
  1217.  
  1218.       data[j] = tmp1;
  1219.       data[j+1] = tmp2;
  1220.     }
  1221. }
  1222.  
  1223. static void
  1224. fill_run (guchar *dest,
  1225.       guchar  alpha,
  1226.       int     w)
  1227. {
  1228.   if (alpha == 255)
  1229.     {
  1230.       memset (dest, 255, w);
  1231.     }
  1232.   else
  1233.     {
  1234.       while (w--)
  1235.     {
  1236.       *dest = MAX(*dest, alpha);
  1237.       dest++;
  1238.     }
  1239.     }
  1240. }
  1241.  
  1242. static void
  1243. render_blob_line (Blob *blob, guchar *dest,
  1244.           int x, int y, int width)
  1245. {
  1246.   int buf[4*SUBSAMPLE];
  1247.   int *data = buf;
  1248.   int n = 0;
  1249.   int i, j;
  1250.   int current = 0;        /* number of filled rows at this point
  1251.                  * in the scan line */
  1252.  
  1253.   int last_x;
  1254.  
  1255.   /* Sort start and ends for all lines */
  1256.   
  1257.   j = y * SUBSAMPLE - blob->y;
  1258.   for (i=0; i<SUBSAMPLE; i++)
  1259.     {
  1260.       if (j >= blob->height)
  1261.     break;
  1262.       if ((j > 0) && (blob->data[j].left <= blob->data[j].right))
  1263.     {
  1264.       data[2*n] = blob->data[j].left;
  1265.       data[2*n+1] = ROW_START;
  1266.       data[2*SUBSAMPLE+2*n] = blob->data[j].right;
  1267.       data[2*SUBSAMPLE+2*n+1] = ROW_STOP;
  1268.       n++;
  1269.     }
  1270.       j++;
  1271.     }
  1272.  
  1273.   /*   If we have less than SUBSAMPLE rows, compress */
  1274.   if (n < SUBSAMPLE)
  1275.     {
  1276.       for (i=0; i<2*n; i++)
  1277.     data[2*n+i] = data[2*SUBSAMPLE+i];
  1278.     }
  1279.  
  1280.   /*   Now count start and end separately */
  1281.   n *= 2;
  1282.  
  1283.   insert_sort (data, n);
  1284.  
  1285.   /* Discard portions outside of tile */
  1286.  
  1287.   while ((n > 0) && (data[0] < SUBSAMPLE*x))
  1288.     {
  1289.       if (data[1] == ROW_START)
  1290.     current++;
  1291.       else
  1292.     current--;
  1293.       data += 2;
  1294.       n--;
  1295.     }
  1296.  
  1297.   while ((n > 0) && (data[2*(n-1)] >= SUBSAMPLE*(x+width)))
  1298.     n--;
  1299.   
  1300.   /* Render the row */
  1301.  
  1302.   last_x = 0;
  1303.   for (i=0; i<n;)
  1304.     {
  1305.       int cur_x = data[2*i] / SUBSAMPLE - x;
  1306.       int pixel;
  1307.  
  1308.       /* Fill in portion leading up to this pixel */
  1309.       if (current && cur_x != last_x)
  1310.     fill_run (dest + last_x, (255*current)/SUBSAMPLE, cur_x - last_x);
  1311.  
  1312.       /* Compute the value for this pixel */
  1313.       pixel = current * SUBSAMPLE; 
  1314.  
  1315.       while (i<n)
  1316.     {
  1317.       int tmp_x = data[2*i] / SUBSAMPLE;
  1318.       if (tmp_x - x != cur_x)
  1319.         break;
  1320.  
  1321.       if (data[2*i+1] == ROW_START)
  1322.         {
  1323.           current++;
  1324.           pixel += ((tmp_x + 1) * SUBSAMPLE) - data[2*i];
  1325.         }
  1326.       else
  1327.         {
  1328.           current--;
  1329.           pixel -= ((tmp_x + 1) * SUBSAMPLE) - data[2*i];
  1330.         }
  1331.       
  1332.       i++;
  1333.     }
  1334.  
  1335.       dest[cur_x] = MAX(dest[cur_x], (pixel * 255) / (SUBSAMPLE * SUBSAMPLE));
  1336.  
  1337.       last_x = cur_x + 1;
  1338.     }
  1339.  
  1340.   if (current != 0)
  1341.     fill_run (dest + last_x, (255*current)/SUBSAMPLE, width - last_x);
  1342. }
  1343.  
  1344. static void
  1345. render_blob (PixelRegion *dest, Blob *blob)
  1346. {
  1347.   int i;
  1348.   int h;
  1349.   unsigned char * s;
  1350.   void * pr;
  1351.  
  1352.   for (pr = pixel_regions_register (1, dest); 
  1353.        pr != NULL; 
  1354.        pr = pixel_regions_process (pr))
  1355.     {
  1356.       h = dest->h;
  1357.       s = dest->data;
  1358.  
  1359.       for (i=0; i<h; i++)
  1360.     {
  1361.       render_blob_line (blob, s,
  1362.                 dest->x, dest->y + i, dest->w);
  1363.       s += dest->rowstride;
  1364.     }
  1365.     }
  1366. }
  1367.  
  1368. static void
  1369. ink_paste (InkTool      *ink_tool, 
  1370.        GimpDrawable *drawable,
  1371.        Blob         *blob)
  1372. {
  1373.   GImage *gimage;
  1374.   PixelRegion srcPR;
  1375.   int offx, offy;
  1376.   unsigned char col[MAX_CHANNELS];
  1377.  
  1378.   if (! (gimage = drawable_gimage (drawable)))
  1379.     return;
  1380.  
  1381.   /* Get the the buffer */
  1382.   ink_set_paint_area (ink_tool, drawable, blob);
  1383.  
  1384.   /* check to make sure there is actually a canvas to draw on */
  1385.   if (!canvas_buf)
  1386.     return;
  1387.  
  1388.   gimage_get_foreground (gimage, drawable, col);
  1389.  
  1390.   /*  set the alpha channel  */
  1391.   col[canvas_buf->bytes - 1] = OPAQUE_OPACITY;
  1392.  
  1393.   /*  color the pixels  */
  1394.   color_pixels (temp_buf_data (canvas_buf), col,
  1395.         canvas_buf->width * canvas_buf->height, canvas_buf->bytes);
  1396.  
  1397.   /*  set undo blocks  */
  1398.   ink_set_undo_tiles (drawable,
  1399.               canvas_buf->x, canvas_buf->y,
  1400.               canvas_buf->width, canvas_buf->height);
  1401.  
  1402.   /*  initialize any invalid canvas tiles  */
  1403.   ink_set_canvas_tiles (canvas_buf->x, canvas_buf->y,
  1404.             canvas_buf->width, canvas_buf->height);
  1405.  
  1406.   ink_to_canvas_tiles (ink_tool, blob, col);
  1407.  
  1408.   /*  initialize canvas buf source pixel regions  */
  1409.   srcPR.bytes = canvas_buf->bytes;
  1410.   srcPR.x = 0; srcPR.y = 0;
  1411.   srcPR.w = canvas_buf->width;
  1412.   srcPR.h = canvas_buf->height;
  1413.   srcPR.rowstride = canvas_buf->width * canvas_buf->bytes;
  1414.   srcPR.data = temp_buf_data (canvas_buf);
  1415.  
  1416.   /*  apply the paint area to the gimage  */
  1417.   gimage_apply_image (gimage, drawable, &srcPR,
  1418.               FALSE, 
  1419.               (int) (gimp_context_get_opacity (NULL) * 255),
  1420.               gimp_context_get_paint_mode (NULL),
  1421.               undo_tiles,  /*  specify an alternative src1  */
  1422.               canvas_buf->x, canvas_buf->y);
  1423.  
  1424.   /*  Update the undo extents  */
  1425.   ink_tool->x1 = MIN (ink_tool->x1, canvas_buf->x);
  1426.   ink_tool->y1 = MIN (ink_tool->y1, canvas_buf->y);
  1427.   ink_tool->x2 = MAX (ink_tool->x2, (canvas_buf->x + canvas_buf->width));
  1428.   ink_tool->y2 = MAX (ink_tool->y2, (canvas_buf->y + canvas_buf->height));
  1429.  
  1430.   /*  Update the gimage--it is important to call gdisplays_update_area
  1431.    *  instead of drawable_update because we don't want the drawable
  1432.    *  preview to be constantly invalidated
  1433.    */
  1434.   drawable_offsets (drawable, &offx, &offy);
  1435.   gdisplays_update_area (gimage, canvas_buf->x + offx, canvas_buf->y + offy,
  1436.              canvas_buf->width, canvas_buf->height);
  1437. }
  1438.  
  1439. /* This routine a) updates the representation of the stroke
  1440.  * in the canvas tiles, then renders the dirty bit of it
  1441.  * into canvas_buf.
  1442.  */
  1443. static void
  1444. ink_to_canvas_tiles (InkTool *ink_tool,
  1445.              Blob    *blob,
  1446.              guchar  *color)
  1447. {
  1448.   PixelRegion srcPR, maskPR;
  1449.  
  1450.   /*  draw the blob on the canvas tiles  */
  1451.   pixel_region_init (&srcPR, canvas_tiles,
  1452.              canvas_buf->x, canvas_buf->y,
  1453.              canvas_buf->width, canvas_buf->height, TRUE);
  1454.  
  1455.   render_blob (&srcPR, blob);
  1456.  
  1457.   /*  combine the canvas tiles and the canvas buf  */
  1458.   srcPR.bytes = canvas_buf->bytes;
  1459.   srcPR.x = 0; srcPR.y = 0;
  1460.   srcPR.w = canvas_buf->width;
  1461.   srcPR.h = canvas_buf->height;
  1462.   srcPR.rowstride = canvas_buf->width * canvas_buf->bytes;
  1463.   srcPR.data = temp_buf_data (canvas_buf);
  1464.  
  1465.   pixel_region_init (&maskPR, canvas_tiles,
  1466.              canvas_buf->x, canvas_buf->y,
  1467.              canvas_buf->width, canvas_buf->height, FALSE);
  1468.  
  1469.   /*  apply the canvas tiles to the canvas buf  */
  1470.   apply_mask_to_region (&srcPR, &maskPR, OPAQUE_OPACITY);
  1471. }
  1472.  
  1473. static void
  1474. ink_set_undo_tiles (GimpDrawable *drawable,
  1475.             int x, int y, int w, int h)
  1476. {
  1477.   int i, j;
  1478.   Tile *src_tile;
  1479.   Tile *dest_tile;
  1480.  
  1481.   for (i = y; i < (y + h); i += (TILE_HEIGHT - (i % TILE_HEIGHT)))
  1482.     {
  1483.       for (j = x; j < (x + w); j += (TILE_WIDTH - (j % TILE_WIDTH)))
  1484.     {
  1485.       dest_tile = tile_manager_get_tile (undo_tiles, j, i, FALSE, FALSE);
  1486.       if (tile_is_valid (dest_tile) == FALSE)
  1487.         {
  1488.           src_tile = tile_manager_get_tile (drawable_data (drawable), j, i, TRUE, FALSE);
  1489.           tile_manager_map_tile (undo_tiles, j, i, src_tile);
  1490.           tile_release (src_tile, FALSE);
  1491.         }
  1492.     }
  1493.     }
  1494. }
  1495.  
  1496.  
  1497. static void
  1498. ink_set_canvas_tiles (int x, int y, int w, int h)
  1499. {
  1500.   int i, j;
  1501.   Tile *tile;
  1502.  
  1503.   for (i = y; i < (y + h); i += (TILE_HEIGHT - (i % TILE_HEIGHT)))
  1504.     {
  1505.       for (j = x; j < (x + w); j += (TILE_WIDTH - (j % TILE_WIDTH)))
  1506.     {
  1507.       tile = tile_manager_get_tile (canvas_tiles, j, i, FALSE, FALSE);
  1508.       if (tile_is_valid (tile) == FALSE)
  1509.         {
  1510.           tile = tile_manager_get_tile (canvas_tiles, j, i, TRUE, TRUE);
  1511.           memset (tile_data_pointer (tile, 0, 0), 
  1512.               0, 
  1513.               tile_size (tile));
  1514.           tile_release (tile, TRUE);
  1515.         }
  1516.     }
  1517.     }
  1518. }
  1519.  
  1520. /**************************/
  1521. /*  Global ink functions  */
  1522. /**************************/
  1523.  
  1524. void
  1525. ink_no_draw (Tool *tool)
  1526. {
  1527.   return;
  1528. }
  1529.  
  1530. Tool *
  1531. tools_new_ink (void)
  1532. {
  1533.   Tool * tool;
  1534.   InkTool * private;
  1535.  
  1536.   /*  The tool options  */
  1537.   if (! ink_options)
  1538.     {
  1539.       ink_options = ink_options_new ();
  1540.       tools_register (INK, (ToolOptions *) ink_options);
  1541.  
  1542.       /*  press all default buttons  */
  1543.       ink_options_reset ();
  1544.     }
  1545.  
  1546.   tool = tools_new_tool (INK);
  1547.   private = g_new0 (InkTool, 1);
  1548.  
  1549.   private->core = draw_core_new (ink_no_draw);
  1550.   private->last_blob = NULL;
  1551.  
  1552.   tool->private = private;
  1553.  
  1554.   tool->button_press_func   = ink_button_press;
  1555.   tool->button_release_func = ink_button_release;
  1556.   tool->motion_func         = ink_motion;
  1557.   tool->cursor_update_func  = ink_cursor_update;
  1558.   tool->control_func        = ink_control;
  1559.  
  1560.   return tool;
  1561. }
  1562.  
  1563. void
  1564. tools_free_ink (Tool *tool)
  1565. {
  1566.   InkTool * ink_tool;
  1567.  
  1568.   ink_tool = (InkTool *) tool->private;
  1569.  
  1570.   /*  Make sure the selection core is not visible  */
  1571.   if (tool->state == ACTIVE && ink_tool->core)
  1572.     draw_core_stop (ink_tool->core, tool);
  1573.  
  1574.   /*  Free the selection core  */
  1575.   if (ink_tool->core)
  1576.     draw_core_free (ink_tool->core);
  1577.  
  1578.   /*  Free the last blob, if any  */
  1579.   if (ink_tool->last_blob)
  1580.     g_free (ink_tool->last_blob);
  1581.   
  1582.   /*  Cleanup memory  */
  1583.   ink_cleanup ();
  1584.  
  1585.   /*  Free the paint core  */
  1586.   g_free (ink_tool);
  1587. }
  1588.