gimpink.c 45.7 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.
 */
Sven Neumann's avatar
Sven Neumann committed
18

19 20
#include "config.h"

21
#include <stdlib.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
22
#include <string.h>
23

24
#include <gtk/gtk.h>
Sven Neumann's avatar
Sven Neumann committed
25

26
#include "libgimpwidgets/gimpwidgets.h"
27

28
#include "tools-types.h"
Sven Neumann's avatar
Sven Neumann committed
29

Michael Natterer's avatar
Michael Natterer committed
30 31 32 33 34
#include "base/pixel-region.h"
#include "base/temp-buf.h"
#include "base/tile.h"
#include "base/tile-manager.h"

35 36
#include "paint-funcs/paint-funcs.h"

Michael Natterer's avatar
Michael Natterer committed
37
#include "core/gimp.h"
38 39 40 41
#include "core/gimpcontext.h"
#include "core/gimpdrawable.h"
#include "core/gimpimage.h"
#include "core/gimpimage-mask.h"
42
#include "core/gimptoolinfo.h"
43

44 45
#include "paint/gimppaintoptions.h"

46 47
#include "display/gimpdisplay.h"

48 49
#include "gimpinktool.h"
#include "gimpinktool-blob.h"
50
#include "paint_options.h"
51
#include "tool_manager.h"
52

Michael Natterer's avatar
Michael Natterer committed
53 54
#include "undo.h"

55 56
#include "libgimp/gimpintl.h"

57

58
#define SUBSAMPLE 8
59

60 61 62 63 64 65 66
typedef Blob * (* BlobFunc) (gdouble,
			     gdouble,
			     gdouble,
			     gdouble,
			     gdouble,
			     gdouble);

67

68
typedef struct _BrushWidget BrushWidget;
69
typedef struct _InkOptions  InkOptions;
Michael Natterer's avatar
Michael Natterer committed
70

71 72
struct _BrushWidget
{
73 74
  GtkWidget  *widget;
  gboolean    state;
75

76 77 78
  /* EEK */
  InkOptions *ink_options;
};
Michael Natterer's avatar
Michael Natterer committed
79

80 81
struct _InkOptions
{
82
  GimpPaintOptions  paint_options;
83

84 85 86
  gdouble           size;
  gdouble           size_d;
  GtkObject        *size_w;
87

88 89 90
  gdouble           sensitivity;
  gdouble           sensitivity_d;
  GtkObject        *sensitivity_w;
91

92 93 94
  gdouble           vel_sensitivity;
  gdouble           vel_sensitivity_d;
  GtkObject        *vel_sensitivity_w;
95

96 97 98
  gdouble           tilt_sensitivity;
  gdouble           tilt_sensitivity_d;
  GtkObject        *tilt_sensitivity_w;
99

100 101 102
  gdouble           tilt_angle;
  gdouble           tilt_angle_d;
  GtkObject        *tilt_angle_w;
103

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

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


116
/*  local function prototypes  */
117

118 119 120
static void        gimp_ink_tool_class_init      (GimpInkToolClass *klass);
static void        gimp_ink_tool_init            (GimpInkTool      *tool);

121
static void        gimp_ink_tool_finalize        (GObject          *object);
122

123
static void        gimp_ink_tool_control         (GimpTool         *tool,
124
                                                  GimpToolAction    action,
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
                                                  GimpDisplay      *gdisp);
static void        gimp_ink_tool_button_press    (GimpTool         *tool,
                                                  GimpCoords       *coords,
                                                  guint32           time,
                                                  GdkModifierType   state,
                                                  GimpDisplay      *gdisp);
static void        gimp_ink_tool_button_release  (GimpTool         *tool,
                                                  GimpCoords       *coords,
                                                  guint32           time,
                                                  GdkModifierType   state,
                                                  GimpDisplay      *gdisp);
static void        gimp_ink_tool_motion          (GimpTool         *tool,
                                                  GimpCoords       *coords,
                                                  guint32           time,
                                                  GdkModifierType   state,
                                                  GimpDisplay      *gdisp);
static void        gimp_ink_tool_cursor_update   (GimpTool         *tool,
                                                  GimpCoords       *coords,
                                                  GdkModifierType   state,
                                                  GimpDisplay      *gdisp);

146 147
static Blob *      ink_pen_ellipse      (InkOptions      *options,
                                         gdouble          x_center,
148 149 150 151 152
                                         gdouble          y_center,
                                         gdouble          pressure,
                                         gdouble          xtilt,
                                         gdouble          ytilt,
                                         gdouble          velocity);
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

static void        time_smoother_add    (GimpInkTool     *ink_tool,
					 guint32          value);
static gdouble     time_smoother_result (GimpInkTool     *ink_tool);
static void        time_smoother_init   (GimpInkTool     *ink_tool,
					 guint32          initval);
static void        dist_smoother_add    (GimpInkTool     *ink_tool,
					 gdouble          value);
static gdouble     dist_smoother_result (GimpInkTool     *ink_tool);
static void        dist_smoother_init   (GimpInkTool     *ink_tool,
					 gdouble          initval);

static void        ink_init             (GimpInkTool     *ink_tool, 
					 GimpDrawable    *drawable, 
					 gdouble          x, 
					 gdouble          y);
static void        ink_finish           (GimpInkTool     *ink_tool, 
					 GimpDrawable    *drawable);
171 172
static void        ink_cleanup          (void);

173 174 175
static gboolean    blob_button_expose   (GtkWidget       *widget,
                                         GdkEventExpose  *event,
                                         BlobFunc         function);
176 177 178
static void        paint_blob           (GdkDrawable     *drawable, 
					 GdkGC           *gc,
					 Blob            *blob);
179

Michael Natterer's avatar
Michael Natterer committed
180
/*  Rendering functions  */
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
static void        ink_set_paint_area   (GimpInkTool     *ink_tool, 
					 GimpDrawable    *drawable, 
					 Blob            *blob);
static void        ink_paste            (GimpInkTool     *ink_tool, 
					 GimpDrawable    *drawable,
					 Blob            *blob);

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

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

Michael Natterer's avatar
Michael Natterer committed
202
/*  Brush pseudo-widget callbacks  */
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
static void     brush_widget_active_rect    (BrushWidget    *brush_widget,
                                             GtkWidget      *widget,
                                             GdkRectangle   *rect);
static void     brush_widget_realize        (GtkWidget      *widget);
static gboolean brush_widget_expose         (GtkWidget      *widget,
                                             GdkEventExpose *event,
                                             BrushWidget    *brush_widget);
static gboolean brush_widget_button_press   (GtkWidget      *widget,
                                             GdkEventButton *event,
                                             BrushWidget    *brush_widget);
static gboolean brush_widget_button_release (GtkWidget      *widget,
                                             GdkEventButton *event,
                                             BrushWidget    *brush_widget);
static gboolean brush_widget_motion_notify  (GtkWidget      *widget,
                                             GdkEventMotion *event,
                                             BrushWidget    *brush_widget);
219

220 221 222
static GimpToolOptions * ink_options_new   (GimpToolInfo    *tool_info);
static void              ink_options_reset (GimpToolOptions *tool_options);
static void              ink_type_update   (GtkWidget       *radio_button,
223
                                            InkOptions      *options);
224

225

226 227 228 229 230 231 232 233 234 235 236 237 238
/* local variables */

/*  undo blocks variables  */
static TileManager *undo_tiles = NULL;

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

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

239
static GimpToolClass *parent_class = NULL;
240

241

242
/*  public functions  */
243

244
void
Nate Summers's avatar
Nate Summers committed
245
gimp_ink_tool_register (GimpToolRegisterCallback  callback,
246
                        gpointer                  data)
247
{
Nate Summers's avatar
Nate Summers committed
248
  (* callback) (GIMP_TYPE_INK_TOOL,
249 250
                ink_options_new,
                TRUE,
251
                "gimp-ink-tool",
252
                _("Ink"),
253 254 255
                _("Draw in ink"),
                N_("/Tools/Paint Tools/Ink"), "K",
                NULL, "tools/ink.html",
Nate Summers's avatar
Nate Summers committed
256
                GIMP_STOCK_TOOL_INK,
257
                data);
258 259
}

260
GType
261 262
gimp_ink_tool_get_type (void)
{
263
  static GType tool_type = 0;
264 265 266

  if (! tool_type)
    {
267
      static const GTypeInfo tool_info =
268
      {
269 270 271 272 273 274
        sizeof (GimpInkToolClass),
	(GBaseInitFunc) NULL,
	(GBaseFinalizeFunc) NULL,
	(GClassInitFunc) gimp_ink_tool_class_init,
	NULL,           /* class_finalize */
	NULL,           /* class_data     */
275
	sizeof (GimpInkTool),
276 277
	0,              /* n_preallocs    */
	(GInstanceInitFunc) gimp_ink_tool_init,
278 279
      };

280 281 282
      tool_type = g_type_register_static (GIMP_TYPE_TOOL,
					  "GimpInkTool", 
                                          &tool_info, 0);
283 284 285 286 287
    }

  return tool_type;
}

288 289 290

/*  private functions  */

291 292 293
static void
gimp_ink_tool_class_init (GimpInkToolClass *klass)
{
294
  GObjectClass   *object_class;
295 296
  GimpToolClass  *tool_class;

297 298
  object_class = G_OBJECT_CLASS (klass);
  tool_class   = GIMP_TOOL_CLASS (klass);
299

300
  parent_class = g_type_class_peek_parent (klass);
301

302
  object_class->finalize     = gimp_ink_tool_finalize;
303

304 305 306 307 308
  tool_class->control        = gimp_ink_tool_control;
  tool_class->button_press   = gimp_ink_tool_button_press;
  tool_class->button_release = gimp_ink_tool_button_release;
  tool_class->motion         = gimp_ink_tool_motion;
  tool_class->cursor_update  = gimp_ink_tool_cursor_update;
309 310 311 312 313 314 315 316
}

static void
gimp_ink_tool_init (GimpInkTool *ink_tool)
{
  GimpTool *tool;

  tool = GIMP_TOOL (ink_tool);
317

318 319
  gimp_tool_control_set_motion_mode (tool->control, GIMP_MOTION_MODE_EXACT);
  gimp_tool_control_set_tool_cursor (tool->control, GIMP_INK_TOOL_CURSOR);
320 321 322
}

static void
323
gimp_ink_tool_finalize (GObject *object)
324 325 326 327 328 329
{
  GimpInkTool *ink_tool;

  ink_tool = GIMP_INK_TOOL (object);

  if (ink_tool->last_blob)
330 331 332 333
    {
      g_free (ink_tool->last_blob);
      ink_tool->last_blob = NULL;
    }
334 335 336

  ink_cleanup ();

337
  G_OBJECT_CLASS (parent_class)->finalize (object);
338 339
}

340
static void
341 342 343
gimp_ink_tool_control (GimpTool       *tool,
                       GimpToolAction  action,
                       GimpDisplay    *gdisp)
344
{
345
  GimpInkTool *ink_tool;
346

347
  ink_tool = GIMP_INK_TOOL (tool);
348

349 350 351 352
  switch (action)
    {
    case PAUSE:
      break;
353

354 355
    case RESUME:
      break;
356

357 358 359
    case HALT:
      ink_cleanup ();
      break;
360

361 362 363
    default:
      break;
    }
364 365

  GIMP_TOOL_CLASS (parent_class)->control (tool, action, gdisp);
366 367
}

368 369 370 371 372 373
static void
gimp_ink_tool_button_press (GimpTool        *tool,
                            GimpCoords      *coords,
                            guint32          time,
                            GdkModifierType  state,
                            GimpDisplay     *gdisp)
374
{
375 376 377 378
  GimpInkTool  *ink_tool;
  InkOptions   *options;
  GimpDrawable *drawable;
  Blob         *b;
379

380
  ink_tool = GIMP_INK_TOOL (tool);
381

382 383
  options = (InkOptions *) tool->tool_info->tool_options;

384
  drawable = gimp_image_active_drawable (gdisp->gimage);
jtl's avatar
jtl committed
385

386
  ink_init (ink_tool, drawable, coords->x, coords->y);
jtl's avatar
jtl committed
387

388
  gimp_tool_control_activate (tool->control);
389
  tool->gdisp = gdisp; 
390

391
  /*  pause the current selection  */
392
  gimp_image_selection_control (gdisp->gimage, GIMP_SELECTION_PAUSE);
393

394 395
  b = ink_pen_ellipse (options,
                       coords->x,
396 397 398 399 400
                       coords->y,
		       coords->pressure,
                       coords->xtilt,
                       coords->ytilt,
		       10.0);
jtl's avatar
jtl committed
401

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

405 406 407 408 409 410
  time_smoother_init (ink_tool, time);
  ink_tool->last_time = time;
  dist_smoother_init (ink_tool, 0.0);
  ink_tool->init_velocity = TRUE;
  ink_tool->lastx = coords->x;
  ink_tool->lasty = coords->y;
jtl's avatar
jtl committed
411

412 413
  gimp_display_flush_now (gdisp);
}
jtl's avatar
jtl committed
414

415 416 417 418 419 420 421 422 423
static void
gimp_ink_tool_button_release (GimpTool        *tool,
                              GimpCoords      *coords,
                              guint32          time,
                              GdkModifierType  state,
                              GimpDisplay     *gdisp)
{
  GimpInkTool *ink_tool;
  GimpImage   *gimage;
jtl's avatar
jtl committed
424

425
  ink_tool = GIMP_INK_TOOL (tool);
426

427
  gimage = gdisp->gimage;
428

429
  /*  resume the current selection  */
430
  gimp_image_selection_control (gdisp->gimage, GIMP_SELECTION_RESUME);
431

432
  /*  Set tool state to inactive -- no longer painting */
433
  gimp_tool_control_halt (tool->control);
434

435 436 437
  /*  free the last blob  */
  g_free (ink_tool->last_blob);
  ink_tool->last_blob = NULL;
Raph Levien's avatar
Raph Levien committed
438

439
  ink_finish (ink_tool, gimp_image_active_drawable (gdisp->gimage));
440
  gimp_image_flush (gdisp->gimage);
441
}
442

443 444 445 446 447 448 449 450
static void
gimp_ink_tool_motion (GimpTool        *tool,
                      GimpCoords      *coords,
                      guint32          time,
                      GdkModifierType  state,
                      GimpDisplay     *gdisp)
{
  GimpInkTool  *ink_tool;
451
  InkOptions   *options;
452 453 454 455 456 457
  GimpDrawable *drawable;
  Blob         *b, *blob_union;

  gdouble velocity;
  gdouble dist;
  gdouble lasttime, thistime;
458

459
  ink_tool = GIMP_INK_TOOL (tool);
460

461 462
  options = (InkOptions *) tool->tool_info->tool_options;

463
  drawable = gimp_image_active_drawable (gdisp->gimage);
464

465
  lasttime = ink_tool->last_time;
466

467 468
  time_smoother_add (ink_tool, time);
  thistime = ink_tool->last_time = time_smoother_result (ink_tool);
469

470 471 472 473 474 475
  /* 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 */
476

477 478
  if (thistime == lasttime)
    thistime = lasttime + 1;
479

480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
  if (ink_tool->init_velocity)
    {
      dist_smoother_init (ink_tool,
			  dist = sqrt ((ink_tool->lastx - coords->x) *
                                       (ink_tool->lastx - coords->x) +
				       (ink_tool->lasty - coords->y) *
                                       (ink_tool->lasty - coords->y)));
      ink_tool->init_velocity = FALSE;
    }
  else
    {
      dist_smoother_add (ink_tool,
			 sqrt ((ink_tool->lastx - coords->x) *
                               (ink_tool->lastx - coords->x) +
			       (ink_tool->lasty - coords->y) *
                               (ink_tool->lasty - coords->y)));
496

497 498
      dist = dist_smoother_result (ink_tool);
    }
499

500 501
  ink_tool->lastx = coords->x;
  ink_tool->lasty = coords->y;
502

503
  velocity = 10.0 * sqrt ((dist) / (gdouble) (thistime - lasttime));
504

505 506
  b = ink_pen_ellipse (options,
                       coords->x,
507 508 509 510 511
                       coords->y,
                       coords->pressure,
                       coords->xtilt,
		       coords->ytilt,
                       velocity);
512

513 514 515
  blob_union = blob_convex_union (ink_tool->last_blob, b);
  g_free (ink_tool->last_blob);
  ink_tool->last_blob = b;
516

517 518
  ink_paste (ink_tool, drawable, blob_union);  
  g_free (blob_union);
519

520 521
  gimp_display_flush_now (gdisp);
}
jtl's avatar
jtl committed
522

523 524 525 526 527 528
static void
gimp_ink_tool_cursor_update (GimpTool        *tool,
                             GimpCoords      *coords,
                             GdkModifierType  state,
                             GimpDisplay     *gdisp)
{
529 530
  GimpLayer     *layer;
  GdkCursorType  ctype = GDK_TOP_LEFT_ARROW;
531

532 533 534
  if ((layer = gimp_image_get_active_layer (gdisp->gimage))) 
    {
      gint off_x, off_y;
535

536
      gimp_drawable_offsets (GIMP_DRAWABLE (layer), &off_x, &off_y);
537

538 539 540 541 542 543 544 545
      if (coords->x >= off_x &&
          coords->y >= off_y &&
	  coords->x < (off_x + gimp_drawable_width (GIMP_DRAWABLE (layer))) &&
	  coords->y < (off_y + gimp_drawable_height (GIMP_DRAWABLE (layer))))
	{
	  /*  One more test--is there a selected region?
	   *  if so, is cursor inside?
	   */
546
	  if (gimp_image_mask_is_empty (gdisp->gimage))
547
	    ctype = GIMP_MOUSE_CURSOR;
548
	  else if (gimp_image_mask_value (gdisp->gimage, coords->x, coords->y))
549 550 551
	    ctype = GIMP_MOUSE_CURSOR;
	}
    }
552

553
  gimp_tool_control_set_cursor (tool->control, ctype);
554 555

  GIMP_TOOL_CLASS (parent_class)->cursor_update (tool, coords, state, gdisp);
556 557
}

558 559 560

/*  the brush widget functions  */

561
static void
562 563
brush_widget_active_rect (BrushWidget  *brush_widget,
			  GtkWidget    *widget,
564 565
			  GdkRectangle *rect)
{
566 567
  gint x,y;
  gint r;
568

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

571 572 573 574
  x = widget->allocation.width / 2 + 0.85 * r * brush_widget->ink_options->aspect / 10.0 *
    cos (brush_widget->ink_options->angle);
  y = widget->allocation.height / 2 + 0.85 * r * brush_widget->ink_options->aspect / 10.0 *
    sin (brush_widget->ink_options->angle);
575

576 577
  rect->x = x - 5;
  rect->y = y - 5;
578 579 580 581 582
  rect->width = 10;
  rect->height = 10;
}

static void
583
brush_widget_realize (GtkWidget *widget)
584
{
585
  gtk_style_set_background (widget->style, widget->window, GTK_STATE_ACTIVE);
586 587 588
}

static void
589 590 591 592 593
brush_widget_draw_brush (BrushWidget *brush_widget,
			 GtkWidget   *widget,
			 gdouble      xc,
			 gdouble      yc,
			 gdouble      radius)
594
{
595 596
  Blob *blob;

597 598 599 600 601 602 603
  blob = brush_widget->ink_options->function (xc, yc,
                                              radius * cos (brush_widget->ink_options->angle),
                                              radius * sin (brush_widget->ink_options->angle),
                                              (- (radius / brush_widget->ink_options->aspect) *
                                               sin (brush_widget->ink_options->angle)),
                                              ((radius / brush_widget->ink_options->aspect) *
                                               cos (brush_widget->ink_options->angle)));
604 605 606

  paint_blob (widget->window, widget->style->fg_gc[widget->state], blob);
  g_free (blob);
607 608
}

609
static gboolean
610 611 612
brush_widget_expose (GtkWidget      *widget,
		     GdkEventExpose *event,
		     BrushWidget    *brush_widget)
613 614
{
  GdkRectangle rect;
615
  gint         r0;
616 617

  r0 = MIN (widget->allocation.width, widget->allocation.height) / 2;
618 619

  if (r0 < 2)
620
    return TRUE;
621

622
  brush_widget_draw_brush (brush_widget, widget,
623
			   widget->allocation.width  / 2,
624 625
			   widget->allocation.height / 2,
			   0.9 * r0);
626

627 628
  brush_widget_active_rect (brush_widget, widget, &rect);
  gdk_draw_rectangle (widget->window, widget->style->bg_gc[GTK_STATE_NORMAL],
629 630 631
		      TRUE,	/* filled */
		      rect.x, rect.y, 
		      rect.width, rect.height);
632 633 634 635 636
  gtk_paint_shadow (widget->style, widget->window, widget->state,
                    GTK_SHADOW_OUT,
                    NULL, widget, NULL,
                    rect.x, rect.y,
                    rect.width, rect.height);
637 638

  return TRUE;
639 640
}

641
static gboolean
642 643 644
brush_widget_button_press (GtkWidget      *widget,
			   GdkEventButton *event,
			   BrushWidget    *brush_widget)
645 646 647
{
  GdkRectangle rect;

648
  brush_widget_active_rect (brush_widget, widget, &rect);
649

650 651 652 653
  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;
654

655
      gtk_grab_add (brush_widget->widget);
656
    }
657 658

  return TRUE;
659 660
}

661
static gboolean
662 663 664
brush_widget_button_release (GtkWidget      *widget,
			     GdkEventButton *event,
			     BrushWidget    *brush_widget)
665 666
{
  brush_widget->state = FALSE;
667

668
  gtk_grab_remove (brush_widget->widget);
669 670

  return TRUE;
671 672
}

673
static gboolean
674 675 676
brush_widget_motion_notify (GtkWidget      *widget,
			    GdkEventMotion *event,
			    BrushWidget    *brush_widget)
677 678 679 680 681 682 683 684
{
  int x;
  int y;
  int r0;
  int rsquare;

  if (brush_widget->state)
    {
685 686
      x = event->x - widget->allocation.width / 2;
      y = event->y - widget->allocation.height / 2;
687 688 689 690
      rsquare = x*x + y*y;

      if (rsquare != 0)
	{
691
	  brush_widget->ink_options->angle = atan2 (y, x);
692

693
	  r0 = MIN (widget->allocation.width, widget->allocation.height) / 2;
694 695
	  brush_widget->ink_options->aspect =
	    10.0 * sqrt ((gdouble) rsquare / (r0 * r0)) / 0.85;
696

697 698 699 700
	  if (brush_widget->ink_options->aspect < 1.0)
	    brush_widget->ink_options->aspect = 1.0;
	  if (brush_widget->ink_options->aspect > 10.0)
	    brush_widget->ink_options->aspect = 10.0;
701

702
	  gtk_widget_queue_draw (widget);
703 704
	}
    }
705 706

  return TRUE;
707 708
}

709 710 711 712
static gboolean
blob_button_expose (GtkWidget      *widget,
                    GdkEventExpose *event,
                    BlobFunc        function)
713
{
714 715 716
  Blob *blob = (*function) (widget->allocation.width  / 2,
                            widget->allocation.height / 2, 8, 0, 0, 8);
  paint_blob (widget->window, widget->style->fg_gc[widget->state], blob);
Sven Neumann's avatar
Sven Neumann committed
717
  g_free (blob);
718

719
  return TRUE;
720 721 722 723 724 725
}

/*
 * Draw a blob onto a drawable with the specified graphics context
 */
static void
726 727 728
paint_blob (GdkDrawable *drawable,
	    GdkGC       *gc,
	    Blob        *blob)
729
{
730
  gint i;
731

732 733 734 735 736
  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);
737 738
}

739

740
static Blob *
741 742 743 744 745 746 747
ink_pen_ellipse (InkOptions *options,
                 gdouble     x_center,
		 gdouble     y_center,
		 gdouble     pressure,
		 gdouble     xtilt,
		 gdouble     ytilt,
		 gdouble     velocity)
748
{
749 750 751 752 753 754 755 756
  gdouble size;
  gdouble tsin, tcos;
  gdouble aspect, radmin;
  gdouble x,y;
  gdouble tscale;
  gdouble tscale_c;
  gdouble tscale_s;

757 758
  /* Adjust the size depending on pressure. */

759 760
  size = options->size * (1.0 + options->sensitivity *
                          (2.0 * pressure - 1.0) );
761 762 763 764 765 766 767 768 769

  /* 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
770
  g_print ("%f (%f) -> ", (float)size, (float)velocity);
771 772
#endif  

773 774 775
  size = options->vel_sensitivity *
    ((4.5 * size) / (1.0 + options->vel_sensitivity * (2.0*(velocity))))
    + (1.0 - options->vel_sensitivity) * size;
776 777

#ifdef VERBOSE
778
  g_print ("%f\n", (float)size);
779 780 781 782
#endif

  /* Clamp resulting size to sane limits */

783 784
  if (size > options->size * (1.0 + options->sensitivity))
    size = options->size * (1.0 + options->sensitivity);
785

786 787 788
  if (size*SUBSAMPLE < 1.0) size = 1.0/SUBSAMPLE;

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

Raph Levien's avatar
Raph Levien committed
790 791 792 793
  /* 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 */

794 795 796 797 798 799 800 801 802
  tscale = options->tilt_sensitivity * 10.0;
  tscale_c = tscale * cos (gimp_deg_to_rad (options->tilt_angle));
  tscale_s = tscale * sin (gimp_deg_to_rad (options->tilt_angle));

  x = (options->aspect * cos (options->angle) +
       xtilt * tscale_c - ytilt * tscale_s);
  y = (options->aspect * sin (options->angle) +
       ytilt * tscale_c + xtilt * tscale_s);

Raph Levien's avatar
Raph Levien committed
803 804
#ifdef VERBOSE
  g_print ("angle %g aspect %g; %g %g; %g %g\n",
805
	   options->angle, options->aspect, tscale_c, tscale_s, x, y);
Raph Levien's avatar
Raph Levien committed
806
#endif
807 808 809 810 811 812 813 814 815
  aspect = sqrt(x*x+y*y);

  if (aspect != 0)
    {
      tcos = x/aspect;
      tsin = y/aspect;
    }
  else
    {
816 817
      tsin = sin (options->angle);
      tcos = cos (options->angle);
818
    }
819

820 821 822 823 824 825 826 827
  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;
  
828 829 830 831 832 833
  return options->function (x_center * SUBSAMPLE,
                            y_center * SUBSAMPLE,
                            radmin * aspect * tcos,
                            radmin * aspect * tsin,  
                            -radmin * tsin,
                            radmin * tcos);
834 835 836
}

static void
837 838
dist_smoother_init (GimpInkTool *ink_tool,
		    gdouble      initval)
839
{
840
  gint i;
841

842
  ink_tool->dt_index = 0;
843

844
  for (i=0; i<DIST_SMOOTHER_BUFFER; i++)
845
    {
846 847 848
      ink_tool->dt_buffer[i] = initval;
    }
}
849

850 851 852 853 854
static gdouble
dist_smoother_result (GimpInkTool *ink_tool)
{
  gint    i;
  gdouble result = 0.0;
855

856 857 858
  for (i = 0; i < DIST_SMOOTHER_BUFFER; i++)
    {
      result += ink_tool->dt_buffer[i];
859
    }
860 861

  return (result / (gdouble) DIST_SMOOTHER_BUFFER);
862 863 864
}

static void
865 866
dist_smoother_add (GimpInkTool *ink_tool,
		   gdouble      value)
867
{
868
  ink_tool->dt_buffer[ink_tool->dt_index] = value;
Michael Natterer's avatar
Michael Natterer committed
869

870 871 872
  if ((++ink_tool->dt_index) == DIST_SMOOTHER_BUFFER)
    ink_tool->dt_index = 0;
}
873 874


875 876 877 878 879
static void
time_smoother_init (GimpInkTool *ink_tool,
		    guint32      initval)
{
  gint i;
880

881
  ink_tool->ts_index = 0;
882

883
  for (i = 0; i < TIME_SMOOTHER_BUFFER; i++)
884
    {
885
      ink_tool->ts_buffer[i] = initval;
886
    }
887 888 889 890 891 892 893 894 895
}

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

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

900 901 902 903 904 905
#ifdef _MSC_VER
  return (gdouble) (gint64) (result / TIME_SMOOTHER_BUFFER);
#else
  return (result / TIME_SMOOTHER_BUFFER);
#endif
}
906 907

static void
908 909
time_smoother_add (GimpInkTool *ink_tool,
		   guint32      value)
910 911 912 913 914 915 916
{
  ink_tool->ts_buffer[ink_tool->ts_index] = value;

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

917
static void
918
ink_init (GimpInkTool  *ink_tool,
919 920 921
	  GimpDrawable *drawable, 
	  gdouble       x,
	  gdouble       y)
922 923 924 925 926 927 928 929
{
  /*  free the block structures  */
  if (undo_tiles)
    tile_manager_destroy (undo_tiles);
  if (canvas_tiles)
    tile_manager_destroy (canvas_tiles);

  /*  Allocate the undo structure  */
930 931 932
  undo_tiles = tile_manager_new (gimp_drawable_width (drawable),
				 gimp_drawable_height (drawable),
				 gimp_drawable_bytes (drawable));
933 934

  /*  Allocate the canvas blocks structure  */
935 936
  canvas_tiles = tile_manager_new (gimp_drawable_width (drawable),
				   gimp_drawable_height (drawable), 1);
937 938 939 940 941 942 943

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

static void
944 945
ink_finish (GimpInkTool  *ink_tool,
	    GimpDrawable *drawable)
946
{
947 948 949 950
  gimp_drawable_push_undo (drawable,
                           ink_tool->x1, ink_tool->y1,
                           ink_tool->x2, ink_tool->y2,
                           undo_tiles, TRUE);
951 952 953 954 955
  undo_tiles = NULL;

  /*  invalidate the drawable--have to do it here, because
   *  it is not done during the actual painting.
   */
956
  gimp_viewable_invalidate_preview (GIMP_VIEWABLE (drawable));
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
}

static void
ink_cleanup (void)
{
  /*  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)
978