gimpink.c 43.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
18 19
#include <math.h>
#include <stdlib.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
20
#include <string.h>
21 22 23 24
#include "appenv.h"
#include "drawable.h"
#include "draw_core.h"
#include "gimage_mask.h"
25
#include "gimprc.h"
26
#include "ink.h"
27
#include "paint_options.h"
28
#include "tool_options_ui.h"
29 30 31
#include "tools.h"
#include "undo.h"
#include "blob.h"
32
#include "gdisplay.h"
33

34 35
#include "libgimp/gimpintl.h"

scott's avatar
scott committed
36
#include "tile.h"			/* ick. */
37

38 39
#define SUBSAMPLE 8

40 41 42
#define DIST_SMOOTHER_BUFFER 10
#define TIME_SMOOTHER_BUFFER 10

43 44
/*  the Ink structures  */

45 46
typedef Blob *(*BlobFunc) (double, double, double, double, double, double);

47 48 49
typedef struct _InkTool InkTool;
struct _InkTool
{
Michael Natterer's avatar
Michael Natterer committed
50
  DrawCore * core;         /*  Core select object             */
51
  
Michael Natterer's avatar
Michael Natterer committed
52
  Blob     * last_blob;	   /*  blob for last cursor position  */
53

Michael Natterer's avatar
Michael Natterer committed
54 55
  int        x1, y1;       /*  image space coordinate         */
  int        x2, y2;       /*  image space coords             */
56

Michael Natterer's avatar
Michael Natterer committed
57 58 59
  /* circular distance history buffer */
  gdouble    dt_buffer[DIST_SMOOTHER_BUFFER];
  gint       dt_index;
60

Michael Natterer's avatar
Michael Natterer committed
61 62 63
  /* circular timing history buffer */
  guint32    ts_buffer[TIME_SMOOTHER_BUFFER];
  gint       ts_index;
64

Michael Natterer's avatar
Michael Natterer committed
65 66
  gdouble    last_time;    /*  previous time of a motion event      */
  gdouble    lastx, lasty; /*  previous position of a motion event  */
67

Michael Natterer's avatar
Michael Natterer committed
68
  gboolean   init_velocity;
69 70 71 72 73
};

typedef struct _BrushWidget BrushWidget;
struct _BrushWidget
{
74
  GtkWidget   *widget;
75 76 77
  gboolean     state;
};

78 79 80
typedef struct _InkOptions InkOptions;
struct _InkOptions
{
81
  PaintOptions  paint_options;
82

83 84 85
  double        size;
  double        size_d;
  GtkObject    *size_w;
86

87 88 89
  double        sensitivity;
  double        sensitivity_d;
  GtkObject    *sensitivity_w;
90

91 92 93
  double        vel_sensitivity;
  double        vel_sensitivity_d;
  GtkObject    *vel_sensitivity_w;
94

95 96 97
  double        tilt_sensitivity;
  double        tilt_sensitivity_d;
  GtkObject    *tilt_sensitivity_w;
98

99 100 101
  double        tilt_angle;
  double        tilt_angle_d;
  GtkObject    *tilt_angle_w;
102

103 104 105
  BlobFunc      function;
  BlobFunc      function_d;
  GtkWidget    *function_w[3];  /* 3 radio buttons */
106

107 108 109 110 111
  double        aspect;
  double        aspect_d;
  double        angle;
  double        angle_d;
  BrushWidget  *brush_w;
112 113 114
};


115 116 117 118 119
/* the ink tool options  */
static InkOptions *ink_options = NULL;

/* local variables */

120 121
/*  undo blocks variables  */
static TileManager *  undo_tiles = NULL;
122 123

/* Tiles used to render the stroke at 1 byte/pp */
124 125
static TileManager *  canvas_tiles = NULL;

126 127 128
/* Flat buffer that is used to used to render the dirty region
 * for composition onto the destination drawable
 */
129 130
static TempBuf *  canvas_buf = NULL;

131

132
/*  local function prototypes  */
133

Michael Natterer's avatar
Michael Natterer committed
134 135 136 137 138
static void   ink_button_press   (Tool *, GdkEventButton *, gpointer);
static void   ink_button_release (Tool *, GdkEventButton *, gpointer);
static void   ink_motion         (Tool *, GdkEventMotion *, gpointer);
static void   ink_cursor_update  (Tool *, GdkEventMotion *, gpointer);
static void   ink_control        (Tool *, ToolAction,       gpointer);
139

140 141 142 143 144 145 146
static void    time_smoother_add    (InkTool* ink_tool, guint32 value);
static gdouble time_smoother_result (InkTool* ink_tool);
static void    time_smoother_init   (InkTool* ink_tool, guint32 initval);
static void    dist_smoother_add    (InkTool* ink_tool, gdouble value);
static gdouble dist_smoother_result (InkTool* ink_tool);
static void    dist_smoother_init   (InkTool* ink_tool, gdouble initval);

147 148 149 150 151 152 153 154 155
static void ink_init   (InkTool      *ink_tool, 
			GimpDrawable *drawable, 
			double        x, 
			double        y);
static void ink_finish (InkTool      *ink_tool, 
			GimpDrawable *drawable, 
			int           tool_id);
static void ink_cleanup (void);

156 157 158 159 160 161 162 163 164
static void ink_type_update               (GtkWidget      *radio_button,
                                           BlobFunc        function);
static GdkPixmap *blob_pixmap (GdkColormap *colormap,
			       GdkVisual   *visual,
			       BlobFunc     function);
static void paint_blob (GdkDrawable  *drawable, 
			GdkGC        *gc,
			Blob         *blob);

Michael Natterer's avatar
Michael Natterer committed
165
/*  Rendering functions  */
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
static void ink_set_paint_area  (InkTool      *ink_tool, 
				 GimpDrawable *drawable, 
				 Blob         *blob);
static void ink_paste           (InkTool      *ink_tool, 
				 GimpDrawable *drawable,
				 Blob         *blob);

static void ink_to_canvas_tiles (InkTool      *ink_tool,
				 Blob         *blob,
				 guchar       *color);

static void ink_set_undo_tiles  (GimpDrawable *drawable,
				 int           x, 
				 int           y,
				 int           w, 
				 int           h);
static void ink_set_canvas_tiles(int           x, 
				 int           y,
				 int           w, 
				 int           h);

Michael Natterer's avatar
Michael Natterer committed
187
/*  Brush pseudo-widget callbacks  */
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
static void brush_widget_active_rect      (BrushWidget    *brush_widget,
					   GtkWidget      *w, 
					   GdkRectangle   *rect);
static void brush_widget_realize          (GtkWidget      *w);
static void brush_widget_expose           (GtkWidget      *w, 
					   GdkEventExpose *event,
					   BrushWidget    *brush_widget);
static void brush_widget_button_press     (GtkWidget      *w, 
					   GdkEventButton *event,
					   BrushWidget    *brush_widget);
static void brush_widget_button_release   (GtkWidget      *w,  
					   GdkEventButton *event,
					   BrushWidget    *brush_widget);
static void brush_widget_motion_notify    (GtkWidget      *w, 
					   GdkEventMotion *event,
					   BrushWidget    *brush_widget);


206 207
/*  functions  */

208 209 210 211 212 213 214 215 216 217 218 219
static void 
ink_type_update (GtkWidget      *radio_button,
		 BlobFunc        function)
{
  InkOptions *options = ink_options;

  if (GTK_TOGGLE_BUTTON (radio_button)->active)
    options->function = function;

  gtk_widget_queue_draw (options->brush_w->widget);
}

220
static void
221
ink_options_reset (void)
222 223 224
{
  InkOptions *options = ink_options;

225 226
  paint_options_reset ((PaintOptions *) options);

227 228 229 230 231 232
  gtk_adjustment_set_value (GTK_ADJUSTMENT (options->size_w),
			    options->size_d);
  gtk_adjustment_set_value (GTK_ADJUSTMENT (options->sensitivity_w),
			    options->sensitivity_d);
  gtk_adjustment_set_value (GTK_ADJUSTMENT (options->tilt_sensitivity_w),
			    options->tilt_sensitivity_d);
233 234
  gtk_adjustment_set_value (GTK_ADJUSTMENT (options->vel_sensitivity_w),
			    options->vel_sensitivity_d);
235 236
  gtk_adjustment_set_value (GTK_ADJUSTMENT (options->tilt_angle_w),
			    options->tilt_angle_d);
237 238 239 240 241 242
  gtk_toggle_button_set_active (((options->function_d == blob_ellipse) ?
				 GTK_TOGGLE_BUTTON (options->function_w[0]) :
				 ((options->function_d == blob_square) ?
				  GTK_TOGGLE_BUTTON (options->function_w[1]) :
				  GTK_TOGGLE_BUTTON (options->function_w[2]))),
				TRUE);
243 244
  options->aspect = options->aspect_d;
  options->angle  = options->angle_d;
245
  gtk_widget_queue_draw (options->brush_w->widget);
246 247
}

248
static InkOptions *
249
ink_options_new (void)
250
{
251
  InkOptions *options;
252

253
  GtkWidget *table;
254
  GtkWidget *vbox;
255
  GtkWidget *abox;
256
  GtkWidget *label;
257 258
  GtkWidget *radio_button;
  GtkWidget *pixmap_widget;
259
  GtkWidget *slider;
260
  GtkWidget *frame;
261
  GtkWidget *darea;
262 263
  GdkPixmap *pixmap;

264
  /*  the new ink tool options structure  */
265
  options = (InkOptions *) g_malloc (sizeof (InkOptions));
266 267 268
  paint_options_init ((PaintOptions *) options,
		      INK,
		      ink_options_reset);
269
  options->size             = options->size_d             = 4.4;
270
  options->sensitivity      = options->sensitivity_d      = 1.0;
271 272
  options->vel_sensitivity  = options->vel_sensitivity_d  = 0.8;
  options->tilt_sensitivity = options->tilt_sensitivity_d = 0.4;
273 274 275 276 277 278
  options->tilt_angle       = options->tilt_angle_d       = 0.0;
  options->function         = options->function_d         = blob_ellipse;
  options->aspect           = options->aspect_d           = 1.0;
  options->angle            = options->angle_d            = 0.0;

  /*  the main table  */
279 280 281
  table = gtk_table_new (9, 2, FALSE);
  gtk_table_set_col_spacing (GTK_TABLE (table), 0, 4);
  gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
282 283
  gtk_table_set_row_spacing (GTK_TABLE (table), 1, 2);
  gtk_table_set_row_spacing (GTK_TABLE (table), 3, 2);
284 285 286
  gtk_table_set_row_spacing (GTK_TABLE (table), 5, 2);
  gtk_table_set_row_spacing (GTK_TABLE (table), 7, 4);
  gtk_box_pack_start (GTK_BOX (((ToolOptions *) options)->main_vbox), table,
287
		      FALSE, FALSE, 0);
288 289

  /*  size slider  */
290
  label = gtk_label_new (_("Size:"));
291 292 293 294
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1,
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  gtk_widget_show (label);
295
  
296
  options->size_w =
297
    gtk_adjustment_new (options->size_d, 0.0, 20.0, 1.0, 2.0, 0.0);
298
  slider = gtk_hscale_new (GTK_ADJUSTMENT (options->size_w));
299
  gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
300
  gtk_table_attach_defaults (GTK_TABLE (table), slider, 1, 2, 0, 1);
301
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
302
  gtk_signal_connect (GTK_OBJECT (options->size_w), "value_changed",
303
		      (GtkSignalFunc) tool_options_double_adjustment_update,
304
		      &options->size);
305 306
  gtk_widget_show (slider);

307
  /* sens slider */
308
  label = gtk_label_new (_("Sensitivity:"));
309 310 311 312
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 1, 2,
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  gtk_widget_show (label);
313
  
314 315 316
  options->sensitivity_w =
    gtk_adjustment_new (options->sensitivity_d, 0.0, 1.0, 0.01, 0.1, 0.0);
  slider = gtk_hscale_new (GTK_ADJUSTMENT (options->sensitivity_w));
317
  gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
318
  gtk_table_attach_defaults (GTK_TABLE (table), slider, 1, 2, 1, 2);
319
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
320
  gtk_signal_connect (GTK_OBJECT (options->sensitivity_w), "value_changed",
321
		      (GtkSignalFunc) tool_options_double_adjustment_update,
322
		      &options->sensitivity);
323
  gtk_widget_show (slider);
324
  
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
  /* tilt sens slider */
  label = gtk_label_new (_("Tilt"));
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 1.0);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 2, 3,
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
  gtk_widget_show (label);

  label = gtk_label_new (_("Sensitivity:"));
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 3, 4,
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
  gtk_widget_show (label);

  abox = gtk_alignment_new (0.5, 1.0, 1.0, 0.0);
  gtk_table_attach (GTK_TABLE (table), abox, 1, 2, 2, 4,
		    GTK_EXPAND | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  gtk_widget_show (abox);

  options->tilt_sensitivity_w =
    gtk_adjustment_new (options->tilt_sensitivity_d, 0.0, 1.0, 0.01, 0.1, 0.0);
  slider = gtk_hscale_new (GTK_ADJUSTMENT (options->tilt_sensitivity_w));
346
  gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
347
  gtk_container_add (GTK_CONTAINER (abox), slider);
348
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
349
  gtk_signal_connect (GTK_OBJECT (options->tilt_sensitivity_w), "value_changed",
350
		      (GtkSignalFunc) tool_options_double_adjustment_update,
351
		      &options->tilt_sensitivity);
352
  gtk_widget_show (slider);
353

354 355 356

  /* velocity sens slider */
  label = gtk_label_new (_("Speed"));
357 358 359 360 361
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 1.0);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 4, 5,
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
  gtk_widget_show (label);

362
  label = gtk_label_new (_("Sensitivity:"));
363 364 365 366 367 368 369 370 371 372
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 5, 6,
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
  gtk_widget_show (label);

  abox = gtk_alignment_new (0.5, 1.0, 1.0, 0.0);
  gtk_table_attach (GTK_TABLE (table), abox, 1, 2, 4, 6,
		    GTK_EXPAND | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  gtk_widget_show (abox);

373 374 375 376 377 378 379
  options->vel_sensitivity_w =
    gtk_adjustment_new (options->vel_sensitivity_d, 0.0, 1.0, 0.01, 0.1, 0.0);
  slider = gtk_hscale_new (GTK_ADJUSTMENT (options->vel_sensitivity_w));
  gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  gtk_container_add (GTK_CONTAINER (abox), slider);
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
  gtk_signal_connect (GTK_OBJECT (options->vel_sensitivity_w), "value_changed",
380
		      (GtkSignalFunc) tool_options_double_adjustment_update,
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
		      &options->vel_sensitivity);
  gtk_widget_show (slider);


  /* angle adjust slider */
  label = gtk_label_new (_("Angle"));
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 1.0);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 6, 7,
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
  gtk_widget_show (label);

  label = gtk_label_new (_("Adjust:"));
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 7, 8,
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
  gtk_widget_show (label);

  abox = gtk_alignment_new (0.5, 1.0, 1.0, 0.0);
  gtk_table_attach (GTK_TABLE (table), abox, 1, 2, 6, 8,
		    GTK_EXPAND | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  gtk_widget_show (abox);

403 404 405
  options->tilt_angle_w =
    gtk_adjustment_new (options->tilt_angle_d, -90.0, 90.0, 1, 10.0, 0.0);
  slider = gtk_hscale_new (GTK_ADJUSTMENT (options->tilt_angle_w));
Raph Levien's avatar
Raph Levien committed
406
  gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
407
  gtk_container_add (GTK_CONTAINER (abox), slider);
Raph Levien's avatar
Raph Levien committed
408
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
409
  gtk_signal_connect (GTK_OBJECT (options->tilt_angle_w), "value_changed",
410
		      (GtkSignalFunc) tool_options_double_adjustment_update,
Raph Levien's avatar
Raph Levien committed
411 412
		      &options->tilt_angle);

413
  /* Brush type radiobuttons */
414

415
  frame = gtk_frame_new (_("Type"));
416
  gtk_table_attach (GTK_TABLE (table), frame, 0, 1, 8, 9,
417 418 419 420 421 422 423 424 425 426
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  gtk_widget_show (frame);

  vbox = gtk_vbox_new (FALSE, 2);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 2);
  gtk_container_add (GTK_CONTAINER (frame), vbox);
  gtk_widget_show (vbox);

  pixmap = blob_pixmap (gtk_widget_get_colormap (vbox),
			gtk_widget_get_visual (vbox),
427 428 429 430 431 432 433 434 435 436 437
			blob_ellipse);

  pixmap_widget = gtk_pixmap_new (pixmap, NULL);
  gdk_pixmap_unref (pixmap);

  radio_button = gtk_radio_button_new (NULL);
  gtk_signal_connect (GTK_OBJECT (radio_button), "toggled",
		      GTK_SIGNAL_FUNC (ink_type_update), 
		      (gpointer)blob_ellipse);

  gtk_container_add (GTK_CONTAINER (radio_button), pixmap_widget);
438
  gtk_box_pack_start (GTK_BOX (vbox), radio_button, FALSE, FALSE, 0);
439

440
  options->function_w[0] = radio_button;
441 442 443

  pixmap = blob_pixmap (gtk_widget_get_colormap (vbox),
			gtk_widget_get_visual (vbox),
444 445 446 447 448
			blob_square);

  pixmap_widget = gtk_pixmap_new (pixmap, NULL);
  gdk_pixmap_unref (pixmap);

449 450
  radio_button =
    gtk_radio_button_new_from_widget (GTK_RADIO_BUTTON (radio_button));
451 452 453 454 455
  gtk_signal_connect (GTK_OBJECT (radio_button), "toggled",
		      GTK_SIGNAL_FUNC (ink_type_update), 
		      (gpointer)blob_square);
  
  gtk_container_add (GTK_CONTAINER (radio_button), pixmap_widget);
456
  gtk_box_pack_start (GTK_BOX (vbox), radio_button, FALSE, FALSE, 0);
457

458 459
  options->function_w[1] = radio_button;

460 461
  pixmap = blob_pixmap (gtk_widget_get_colormap (vbox),
			gtk_widget_get_visual (vbox),
462 463 464 465 466
			blob_diamond);

  pixmap_widget = gtk_pixmap_new (pixmap, NULL);
  gdk_pixmap_unref (pixmap);

467 468
  radio_button =
    gtk_radio_button_new_from_widget (GTK_RADIO_BUTTON (radio_button));
469 470 471 472 473
  gtk_signal_connect (GTK_OBJECT (radio_button), "toggled",
		      GTK_SIGNAL_FUNC (ink_type_update), 
		      (gpointer)blob_diamond);

  gtk_container_add (GTK_CONTAINER (radio_button), pixmap_widget);
474
  gtk_box_pack_start (GTK_BOX (vbox), radio_button, FALSE, FALSE, 0);
475

476 477
  options->function_w[2] = radio_button;

478
  /* Brush shape widget */
479
  frame = gtk_frame_new (_("Shape"));
480
  gtk_table_attach_defaults (GTK_TABLE (table), frame, 1, 2, 8, 9);
481 482 483 484 485 486
  gtk_widget_show (frame);

  vbox = gtk_vbox_new (FALSE, 2);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 2);
  gtk_container_add (GTK_CONTAINER (frame), vbox);
  gtk_widget_show (vbox);
487

488 489
  options->brush_w = g_new (BrushWidget, 1);
  options->brush_w->state = FALSE;
490

491 492 493
  frame = gtk_aspect_frame_new (NULL, 0.5, 0.5, 1.0, FALSE);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
494

495
  darea = gtk_drawing_area_new();
496
  options->brush_w->widget = darea;
497

498 499
  gtk_drawing_area_size (GTK_DRAWING_AREA (darea), 60, 60);
  gtk_container_add (GTK_CONTAINER (frame), darea);
500

501 502 503 504 505
  gtk_widget_set_events (darea, 
			 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
			 | GDK_POINTER_MOTION_MASK | GDK_EXPOSURE_MASK);
  gtk_signal_connect (GTK_OBJECT (darea), "button_press_event",
		      GTK_SIGNAL_FUNC (brush_widget_button_press),
506
		      options->brush_w);
507 508
  gtk_signal_connect (GTK_OBJECT (darea), "button_release_event",
		      GTK_SIGNAL_FUNC (brush_widget_button_release),
509
		      options->brush_w);
510 511
  gtk_signal_connect (GTK_OBJECT (darea), "motion_notify_event",
		      GTK_SIGNAL_FUNC (brush_widget_motion_notify),
512
		      options->brush_w);
513 514
  gtk_signal_connect (GTK_OBJECT (darea), "expose_event",
		      GTK_SIGNAL_FUNC (brush_widget_expose), 
515
		      options->brush_w);
516 517
  gtk_signal_connect (GTK_OBJECT (darea), "realize",
		      GTK_SIGNAL_FUNC (brush_widget_realize),
518
		      options->brush_w);
519 520

  gtk_widget_show_all (table);
521 522 523 524

  return options;
}

525 526 527

/*  the brush widget functions  */

528
static void
529 530
brush_widget_active_rect (BrushWidget  *brush_widget,
			  GtkWidget    *widget,
531 532 533 534 535
			  GdkRectangle *rect)
{
  int x,y;
  int r;

536
  r = MIN (widget->allocation.width, widget->allocation.height) / 2;
537

538
  x = widget->allocation.width / 2 + 0.85 * r * ink_options->aspect / 10.0 *
539
    cos (ink_options->angle);
540
  y = widget->allocation.height / 2 + 0.85 * r * ink_options->aspect / 10.0 *
541 542
    sin (ink_options->angle);

543 544
  rect->x = x - 5;
  rect->y = y - 5;
545 546 547 548 549
  rect->width = 10;
  rect->height = 10;
}

static void
550
brush_widget_realize (GtkWidget *widget)
551
{
552
  gtk_style_set_background (widget->style, widget->window, GTK_STATE_ACTIVE);
553 554 555
}

static void
556 557 558 559 560
brush_widget_draw_brush (BrushWidget *brush_widget,
			 GtkWidget   *widget,
			 gdouble      xc,
			 gdouble      yc,
			 gdouble      radius)
561
{
562 563 564 565 566 567 568 569 570 571 572 573
  Blob *blob;

  blob = ink_options->function (xc, yc,
				radius * cos (ink_options->angle),
				radius * sin (ink_options->angle),
				(- (radius / ink_options->aspect) *
				 sin (ink_options->angle)),
				((radius / ink_options->aspect) *
				 cos (ink_options->angle)));

  paint_blob (widget->window, widget->style->fg_gc[widget->state], blob);
  g_free (blob);
574 575 576
}

static void
577 578 579
brush_widget_expose (GtkWidget      *widget,
		     GdkEventExpose *event,
		     BrushWidget    *brush_widget)
580 581 582
{
  GdkRectangle rect;
  int r0;
583 584

  r0 = MIN (widget->allocation.width, widget->allocation.height) / 2;
585 586 587 588

  if (r0 < 2)
    return;

589 590 591 592 593 594 595
  gdk_window_clear_area (widget->window, 0, 0,
			 widget->allocation.width,
			 widget->allocation.height);
  brush_widget_draw_brush (brush_widget, widget,
			   widget->allocation.width / 2,
			   widget->allocation.height / 2,
			   0.9 * r0);
596

597 598
  brush_widget_active_rect (brush_widget, widget, &rect);
  gdk_draw_rectangle (widget->window, widget->style->bg_gc[GTK_STATE_NORMAL],
599 600 601
		      TRUE,	/* filled */
		      rect.x, rect.y, 
		      rect.width, rect.height);
602
  gtk_draw_shadow (widget->style, widget->window, widget->state, GTK_SHADOW_OUT,
603 604 605 606 607
		   rect.x, rect.y,
		   rect.width, rect.height);
}

static void
608 609 610
brush_widget_button_press (GtkWidget      *widget,
			   GdkEventButton *event,
			   BrushWidget    *brush_widget)
611 612 613
{
  GdkRectangle rect;

614
  brush_widget_active_rect (brush_widget, widget, &rect);
615 616 617 618 619
  
  if ((event->x >= rect.x) && (event->x-rect.x < rect.width) &&
      (event->y >= rect.y) && (event->y-rect.y < rect.height))
    {
      brush_widget->state = TRUE;
620

621
      gtk_grab_add (brush_widget->widget);
622 623 624 625
    }
}

static void
626 627 628
brush_widget_button_release (GtkWidget      *widget,
			     GdkEventButton *event,
			     BrushWidget    *brush_widget)
629 630
{
  brush_widget->state = FALSE;
631

632
  gtk_grab_remove (brush_widget->widget);
633 634 635
}

static void
636 637 638
brush_widget_motion_notify (GtkWidget      *widget,
			    GdkEventMotion *event,
			    BrushWidget    *brush_widget)
639 640 641 642 643 644 645 646
{
  int x;
  int y;
  int r0;
  int rsquare;

  if (brush_widget->state)
    {
647 648
      x = event->x - widget->allocation.width / 2;
      y = event->y - widget->allocation.height / 2;
649 650 651 652
      rsquare = x*x + y*y;

      if (rsquare != 0)
	{
653
	  ink_options->angle = atan2 (y, x);
654

655
	  r0 = MIN (widget->allocation.width, widget->allocation.height) / 2;
656
	  ink_options->aspect =
657
	    10.0 * sqrt ((double) rsquare / (r0 * r0)) / 0.85;
658 659 660 661 662 663

	  if (ink_options->aspect < 1.0)
	    ink_options->aspect = 1.0;
	  if (ink_options->aspect > 10.0)
	    ink_options->aspect = 10.0;

664
	  gtk_widget_draw (widget, NULL);
665 666 667 668
	}
    }
}

669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
/*
 * Return a black-on white pixmap in the given colormap and
 * visual that represents the BlobFunc 'function'
 */
static GdkPixmap *
blob_pixmap (GdkColormap *colormap,
	     GdkVisual *visual,
	     BlobFunc function)
{
  GdkPixmap *pixmap;
  GdkGC *black_gc, *white_gc;
  GdkColor tmp_color;
  Blob *blob;

  pixmap = gdk_pixmap_new (NULL, 22, 21, visual->depth);

  black_gc = gdk_gc_new (pixmap);
  gdk_color_black (colormap, &tmp_color);
  gdk_gc_set_foreground (black_gc, &tmp_color);

  white_gc = gdk_gc_new (pixmap);
  gdk_color_white (colormap, &tmp_color);
  gdk_gc_set_foreground (white_gc, &tmp_color);

  gdk_draw_rectangle (pixmap, white_gc, TRUE, 0, 0, 21, 20);
  gdk_draw_rectangle (pixmap, black_gc, FALSE, 0, 0, 21, 20);
  blob = (*function) (10, 10, 8, 0, 0, 8);
  paint_blob (pixmap, black_gc, blob);

  gdk_gc_unref (white_gc);
  gdk_gc_unref (black_gc);

  return pixmap;
}

/*
 * Draw a blob onto a drawable with the specified graphics context
 */
static void
paint_blob (GdkDrawable *drawable, GdkGC *gc,
	    Blob *blob)
{
  int i;

  for (i=0;i<blob->height;i++)
    {
      if (blob->data[i].left <= blob->data[i].right)
	gdk_draw_line (drawable, gc,
		       blob->data[i].left,i+blob->y,
		       blob->data[i].right+1,i+blob->y);
    }
}

722

723 724
static Blob *
ink_pen_ellipse (gdouble x_center, gdouble y_center,
725 726
		 gdouble pressure, gdouble xtilt, gdouble ytilt,
		 gdouble velocity)
727 728 729 730 731
{
  double size;
  double tsin, tcos;
  double aspect, radmin;
  double x,y;
732
  double tscale;
Raph Levien's avatar
Raph Levien committed
733 734
  double tscale_c;
  double tscale_s;
735
  
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
  /* Adjust the size depending on pressure. */

  size = ink_options->size * (1.0 + ink_options->sensitivity *
			      (2.0 * pressure - 1.0) );

  /* Adjust the size further depending on pointer velocity
     and velocity-sensitivity.  These 'magic constants' are
     'feels natural' tigert-approved. --ADM */

  if (velocity < 3.0)
    velocity = 3.0;

#ifdef VERBOSE
  g_print("%f (%f) -> ", (float)size, (float)velocity);
#endif  

  size = ink_options->vel_sensitivity *
    ((4.5 * size) / (1.0 + ink_options->vel_sensitivity * (2.0*(velocity))))
    + (1.0 - ink_options->vel_sensitivity) * size;

#ifdef VERBOSE
  g_print("%f\n", (float)size);
#endif

  /* Clamp resulting size to sane limits */

  if (size > ink_options->size * (1.0 + ink_options->sensitivity))
    size = ink_options->size * (1.0 + ink_options->sensitivity);
764

765 766 767
  if (size*SUBSAMPLE < 1.0) size = 1.0/SUBSAMPLE;

  /* Add brush angle/aspect to tilt vectorially */
768

Raph Levien's avatar
Raph Levien committed
769 770 771 772
  /* I'm not happy with the way the brush widget info is combined with
     tilt info from the brush. My personal feeling is that representing
     both as affine transforms would make the most sense. -RLL */

773
  tscale = ink_options->tilt_sensitivity * 10.0;
774 775
  tscale_c = tscale * cos (ink_options->tilt_angle * G_PI / 180);
  tscale_s = tscale * sin (ink_options->tilt_angle * G_PI / 180);
Raph Levien's avatar
Raph Levien committed
776 777 778 779 780 781 782 783
  x = ink_options->aspect*cos(ink_options->angle) +
    xtilt * tscale_c - ytilt * tscale_s;
  y = ink_options->aspect*sin(ink_options->angle) +
    ytilt * tscale_c + xtilt * tscale_s;
#ifdef VERBOSE
  g_print ("angle %g aspect %g; %g %g; %g %g\n",
	   ink_options->angle, ink_options->aspect, tscale_c, tscale_s, x, y);
#endif
784 785 786 787 788 789 790 791 792 793 794 795
  aspect = sqrt(x*x+y*y);

  if (aspect != 0)
    {
      tcos = x/aspect;
      tsin = y/aspect;
    }
  else
    {
      tsin = sin(ink_options->angle);
      tcos = cos(ink_options->angle);
    }
796

797 798 799 800 801 802 803 804
  if (aspect < 1.0) 
    aspect = 1.0;
  else if (aspect > 10.0) 
    aspect = 10.0;

  radmin = SUBSAMPLE * size/aspect;
  if (radmin < 1.0) radmin = 1.0;
  
805 806 807
  return ink_options->function (x_center * SUBSAMPLE, y_center * SUBSAMPLE,
				radmin*aspect*tcos, radmin*aspect*tsin,  
				-radmin*tsin, radmin*tcos);
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
}

static void
ink_button_press (Tool           *tool,
		  GdkEventButton *bevent,
		  gpointer        gdisp_ptr)
{
  gdouble       x, y;
  GDisplay     *gdisp;
  InkTool      *ink_tool;
  GimpDrawable *drawable;
  Blob         *b;

  gdisp = (GDisplay *) gdisp_ptr;
  ink_tool = (InkTool *) tool->private;

  /*  Keep the coordinates of the target  */
  gdisplay_untransform_coords_f (gdisp, bevent->x, bevent->y,
				 &x, &y, TRUE);
  drawable = gimage_active_drawable (gdisp->gimage);

  ink_init (ink_tool, drawable, x, y);

  tool->state = ACTIVE;
  tool->gdisp_ptr = gdisp_ptr;
  tool->paused_count = 0;

  /*  pause the current selection and grab the pointer  */
836
  gdisplays_selection_visibility (gdisp->gimage, SelectionPause);
837

838 839
  /* add motion memory if you press mod1 first ^ perfectmouse */
  if (((bevent->state & GDK_MOD1_MASK) != 0) != (perfectmouse != 0))
840 841 842 843 844
    gdk_pointer_grab (gdisp->canvas->window, FALSE,
		      GDK_BUTTON1_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
		      NULL, NULL, bevent->time);
  else
    gdk_pointer_grab (gdisp->canvas->window, FALSE,
845 846
		      GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON1_MOTION_MASK |
		      GDK_BUTTON_RELEASE_MASK,
847 848 849 850 851 852
		      NULL, NULL, bevent->time);
  
  tool->gdisp_ptr = gdisp_ptr;
  tool->state = ACTIVE;

  b = ink_pen_ellipse (x, y, 
853
		       bevent->pressure, bevent->xtilt, bevent->ytilt, 10.0);
854 855 856 857

  ink_paste (ink_tool, drawable, b);
  ink_tool->last_blob = b;

858 859 860 861 862 863 864
  time_smoother_init (ink_tool, bevent->time);
  ink_tool->last_time = bevent->time;
  dist_smoother_init (ink_tool, 0.0);
  ink_tool->init_velocity = TRUE;
  ink_tool->lastx = x;
  ink_tool->lasty = y;

865
  gdisplay_flush_now (gdisp);
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
}

static void
ink_button_release (Tool           *tool,
		    GdkEventButton *bevent,
		    gpointer        gdisp_ptr)
{
  GDisplay * gdisp;
  GImage * gimage;
  InkTool * ink_tool;

  gdisp = (GDisplay *) gdisp_ptr;
  gimage = gdisp->gimage;
  ink_tool = (InkTool *) tool->private;

  /*  resume the current selection and ungrab the pointer  */
882
  gdisplays_selection_visibility (gdisp->gimage, SelectionResume);
883 884 885 886 887 888 889

  gdk_pointer_ungrab (bevent->time);
  gdk_flush ();

  /*  Set tool state to inactive -- no longer painting */
  tool->state = INACTIVE;

890 891 892 893
  /*  free the last blob  */
  g_free (ink_tool->last_blob);
  ink_tool->last_blob = NULL;

894 895 896 897
  ink_finish (ink_tool, gimage_active_drawable (gdisp->gimage), tool->ID);
  gdisplays_flush ();
}

898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959

static void
dist_smoother_init (InkTool* ink_tool, gdouble initval)
{
  gint i;

  ink_tool->dt_index = 0;

  for (i=0; i<DIST_SMOOTHER_BUFFER; i++)
    {
      ink_tool->dt_buffer[i] = initval;
    }
}

static gdouble
dist_smoother_result (InkTool* ink_tool)
{
  gint i;
  gdouble result = 0.0;

  for (i=0; i<DIST_SMOOTHER_BUFFER; i++)
    {
      result += ink_tool->dt_buffer[i];
    }

  return (result / (gdouble)DIST_SMOOTHER_BUFFER);
}

static void
dist_smoother_add (InkTool* ink_tool, gdouble value)
{
  ink_tool->dt_buffer[ink_tool->dt_index] = value;

  if ((++ink_tool->dt_index) == DIST_SMOOTHER_BUFFER)
    ink_tool->dt_index = 0;
}


static void
time_smoother_init (InkTool* ink_tool, guint32 initval)
{
  gint i;

  ink_tool->ts_index = 0;

  for (i=0; i<TIME_SMOOTHER_BUFFER; i++)
    {
      ink_tool->ts_buffer[i] = initval;
    }
}

static gdouble
time_smoother_result (InkTool* ink_tool)
{
  gint i;
  guint64 result = 0;

  for (i=0; i<TIME_SMOOTHER_BUFFER; i++)
    {
      result += ink_tool->ts_buffer[i];
    }

Tor Lillqvist's avatar
Tor Lillqvist committed
960 961 962
#ifdef _MSC_VER
  return (gdouble) (gint64) (result / TIME_SMOOTHER_BUFFER);
#else
963
  return (result / TIME_SMOOTHER_BUFFER);
Tor Lillqvist's avatar
Tor Lillqvist committed
964
#endif
965 966 967 968 969 970 971 972 973 974 975 976
}

static void
time_smoother_add (InkTool* ink_tool, guint32 value)
{
  ink_tool->ts_buffer[ink_tool->ts_index] = value;

  if ((++ink_tool->ts_index) == TIME_SMOOTHER_BUFFER)
    ink_tool->ts_index = 0;
}


977 978 979 980 981 982 983 984 985 986 987
static void
ink_motion (Tool           *tool,
	    GdkEventMotion *mevent,
	    gpointer        gdisp_ptr)
{
  GDisplay *gdisp;
  InkTool  *ink_tool;
  GimpDrawable *drawable;
  Blob *b, *blob_union;

  double x, y;
988 989 990 991 992
  double pressure;
  double velocity;
  double dist;
  gdouble lasttime, thistime;
  
993 994 995 996 997 998 999

  gdisp = (GDisplay *) gdisp_ptr;
  ink_tool = (InkTool *) tool->private;

  gdisplay_untransform_coords_f (gdisp, mevent->x, mevent->y, &x, &y, TRUE);
  drawable = gimage_active_drawable (gdisp->gimage);

1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
  lasttime = ink_tool->last_time;

  time_smoother_add (ink_tool, mevent->time);
  thistime = ink_tool->last_time =
    time_smoother_result(ink_tool);

  /* The time resolution on X-based GDK motion events is
     bloody awful, hence the use of the smoothing function.
     Sadly this also means that there is always the chance of
     having an indeterminite velocity since this event and
     the previous several may still appear to issue at the same
     instant. -ADM */

  if (thistime == lasttime)
    thistime = lasttime + 1;

  if (ink_tool->init_velocity)
    {
      dist_smoother_init (ink_tool, dist = sqrt((ink_tool->lastx-x)*(ink_tool->lastx-x) +
						(ink_tool->lasty-y)*(ink_tool->lasty-y)));
      ink_tool->init_velocity = FALSE;
    }
  else
    {
      dist_smoother_add (ink_tool, sqrt((ink_tool->lastx-x)*(ink_tool->lastx-x) +
					(ink_tool->lasty-y)*(ink_tool->lasty-y)));
      dist = dist_smoother_result(ink_tool);
    }

  ink_tool->lastx = x;
  ink_tool->lasty = y;

  pressure = mevent->pressure;
  velocity = 10.0 * sqrt((dist) / (double)(thistime - lasttime));
  
  b = ink_pen_ellipse (x, y, pressure, mevent->xtilt, mevent->ytilt, velocity);
1036 1037 1038 1039 1040 1041 1042
  blob_union = blob_convex_union (ink_tool->last_blob, b);
  g_free (ink_tool->last_blob);
  ink_tool->last_blob = b;

  ink_paste (ink_tool, drawable, blob_union);  
  g_free (blob_union);
  
1043
  gdisplay_flush_now (gdisp);
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
}

static void
ink_cursor_update (Tool           *tool,
		   GdkEventMotion *mevent,
		   gpointer        gdisp_ptr)
{
  GDisplay *gdisp;
  Layer *layer;
  GdkCursorType ctype = GDK_TOP_LEFT_ARROW;
  int x, y;

  gdisp = (GDisplay *) gdisp_ptr;

  gdisplay_untransform_coords (gdisp, mevent->x, mevent->y, &x, &y, FALSE, FALSE);
  if ((layer = gimage_get_active_layer (gdisp->gimage))) 
    {
      int off_x, off_y;
      drawable_offsets (GIMP_DRAWABLE(layer), &off_x, &off_y);
    if (x >= off_x && y >= off_y &&
	x < (off_x + drawable_width (GIMP_DRAWABLE(layer))) &&
	y < (off_y + drawable_height (GIMP_DRAWABLE(layer))))
      {
	/*  One more test--is there a selected region?
	 *  if so, is cursor inside?
	 */
	if (gimage_mask_is_empty (gdisp->gimage))
	  ctype = GDK_PENCIL;
	else if (gimage_mask_value (gdisp->gimage, x, y))
	  ctype = GDK_PENCIL;
      }
    }
  gdisplay_install_tool_cursor (gdisp, ctype);
}

static void
Michael Natterer's avatar
Michael Natterer committed
1080 1081 1082
ink_control (Tool       *tool,
	     ToolAction  action,
	     gpointer    gdisp_ptr)
1083 1084 1085 1086 1087 1088 1089
{
  InkTool *ink_tool;

  ink_tool = (InkTool *) tool->private;

  switch (action)
    {
Michael Natterer's avatar
Michael Natterer committed
1090
    case PAUSE:
1091 1092
      draw_core_pause (ink_tool->core, tool);
      break;
Michael Natterer's avatar
Michael Natterer committed
1093 1094

    case RESUME:
1095 1096
      draw_core_resume (ink_tool->core, tool);
      break;
Michael Natterer's avatar
Michael Natterer committed
1097 1098

    case HALT:
1099 1100
      ink_cleanup ();
      break;
Michael Natterer's avatar
Michael Natterer committed
1101 1102 1103

    default:
      break;
1104 1105 1106 1107 1108
    }
}

static void
ink_init (InkTool *ink_tool, GimpDrawable *drawable, 
1109
	  double x, double y)
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
{
  /*  free the block structures  */
  if (undo_tiles)
    tile_manager_destroy (undo_tiles);
  if (canvas_tiles)
    tile_manager_destroy (canvas_tiles);

  /*  Allocate the undo structure  */
  undo_tiles = tile_manager_new (drawable_width (drawable),
				 drawable_height (drawable),
				 drawable_bytes (drawable));

  /*  Allocate the canvas blocks structure  */
  canvas_tiles = tile_manager_new (drawable_width (drawable),
				   drawable_height (drawable), 1);

  /*  Get the initial undo extents  */
  ink_tool->x1 = ink_tool->x2 = x;
  ink_tool->y1 = ink_tool->y2 = y;
}

static void
ink_finish (InkTool *ink_tool, GimpDrawable *drawable, int tool_id)
{
  /*  push an undo  */
  drawable_apply_image (drawable, ink_tool->x1, ink_tool->y1,
			ink_tool->x2, ink_tool->y2, undo_tiles, TRUE);
  undo_tiles = NULL;

  /*  invalidate the drawable--have to do it here, because
   *  it is not done during the actual painting.
   */
  drawable_invalidate_preview (drawable);
}

static void
ink_cleanup (void)
{
  /*  CLEANUP  */
  /*  If the undo tiles exist, nuke them  */
  if (undo_tiles)
    {
      tile_manager_destroy (undo_tiles);
      undo_tiles = NULL;
    }

  /*  If the canvas blocks exist, nuke them  */
  if (canvas_tiles)
    {
      tile_manager_destroy (canvas_tiles);
      canvas_tiles = NULL;
    }

  /*  Free the temporary buffer if it exist  */
  if (canvas_buf)
    temp_buf_free (canvas_buf);
  canvas_buf = NULL;
}

/*********************************
 *  Rendering functions          *
 *********************************/

/* Some of this stuff should probably be combined with the 
 * code it was copied from in paint_core.c; but I wanted
 * to learn this stuff, so I've kept it simple.
 *
 * The following only supports CONSTANT mode. Incremental
 * would, I think, interact strangely with the way we
 * do things. But it wouldn't be hard to implement at all.
 */

static void
ink_set_paint_area (InkTool      *ink_tool, 
		    GimpDrawable *drawable, 
		    Blob         *blob)
{
  int x, y, width, height;
  int x1, y1, x2, y2;
  int bytes;
  
  blob_bounds (blob, &x, &y, &width, &height);

  bytes = drawable_has_alpha (drawable) ?
    drawable_bytes (drawable) : drawable_bytes (drawable) + 1;

  x1 = CLAMP (x/SUBSAMPLE - <