gimpink.c 29 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
#include "core/gimpdrawable.h"
#include "core/gimpimage.h"
#include "core/gimpimage-mask.h"
41
#include "core/gimptoolinfo.h"
42

43 44
#include "paint/gimppaintoptions.h"

45 46
#include "display/gimpdisplay.h"

47
#include "gimpinkoptions.h"
48 49
#include "gimpinktool.h"
#include "gimpinktool-blob.h"
50

Michael Natterer's avatar
Michael Natterer committed
51 52
#include "undo.h"

53 54
#include "libgimp/gimpintl.h"

55

56
#define SUBSAMPLE 8
57

58

59
/*  local function prototypes  */
60

61 62 63
static void        gimp_ink_tool_class_init      (GimpInkToolClass *klass);
static void        gimp_ink_tool_init            (GimpInkTool      *tool);

64
static void        gimp_ink_tool_finalize        (GObject          *object);
65

66
static void        gimp_ink_tool_control         (GimpTool         *tool,
67
                                                  GimpToolAction    action,
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
                                                  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);

89
static Blob *      ink_pen_ellipse      (GimpInkOptions  *options,
90
                                         gdouble          x_center,
91 92 93 94 95
                                         gdouble          y_center,
                                         gdouble          pressure,
                                         gdouble          xtilt,
                                         gdouble          ytilt,
                                         gdouble          velocity);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

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);
114 115
static void        ink_cleanup          (void);

Michael Natterer's avatar
Michael Natterer committed
116
/*  Rendering functions  */
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
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);
137 138


139 140 141 142 143 144 145 146 147 148 149 150 151
/* 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;

152
static GimpToolClass *parent_class = NULL;
153

154

155
/*  public functions  */
156

157
void
Nate Summers's avatar
Nate Summers committed
158
gimp_ink_tool_register (GimpToolRegisterCallback  callback,
159
                        gpointer                  data)
160
{
Nate Summers's avatar
Nate Summers committed
161
  (* callback) (GIMP_TYPE_INK_TOOL,
162 163
                GIMP_TYPE_INK_OPTIONS,
                gimp_ink_options_gui,
164
                TRUE,
165
                "gimp-ink-tool",
166
                _("Ink"),
167 168 169
                _("Draw in ink"),
                N_("/Tools/Paint Tools/Ink"), "K",
                NULL, "tools/ink.html",
Nate Summers's avatar
Nate Summers committed
170
                GIMP_STOCK_TOOL_INK,
171
                data);
172 173
}

174
GType
175 176
gimp_ink_tool_get_type (void)
{
177
  static GType tool_type = 0;
178 179 180

  if (! tool_type)
    {
181
      static const GTypeInfo tool_info =
182
      {
183 184 185 186 187 188
        sizeof (GimpInkToolClass),
	(GBaseInitFunc) NULL,
	(GBaseFinalizeFunc) NULL,
	(GClassInitFunc) gimp_ink_tool_class_init,
	NULL,           /* class_finalize */
	NULL,           /* class_data     */
189
	sizeof (GimpInkTool),
190 191
	0,              /* n_preallocs    */
	(GInstanceInitFunc) gimp_ink_tool_init,
192 193
      };

194 195 196
      tool_type = g_type_register_static (GIMP_TYPE_TOOL,
					  "GimpInkTool", 
                                          &tool_info, 0);
197 198 199 200 201
    }

  return tool_type;
}

202 203 204

/*  private functions  */

205 206 207
static void
gimp_ink_tool_class_init (GimpInkToolClass *klass)
{
208
  GObjectClass   *object_class;
209 210
  GimpToolClass  *tool_class;

211 212
  object_class = G_OBJECT_CLASS (klass);
  tool_class   = GIMP_TOOL_CLASS (klass);
213

214
  parent_class = g_type_class_peek_parent (klass);
215

216
  object_class->finalize     = gimp_ink_tool_finalize;
217

218 219 220 221 222
  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;
223 224 225 226 227 228 229 230
}

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

  tool = GIMP_TOOL (ink_tool);
231

232 233
  gimp_tool_control_set_motion_mode (tool->control, GIMP_MOTION_MODE_EXACT);
  gimp_tool_control_set_tool_cursor (tool->control, GIMP_INK_TOOL_CURSOR);
234 235 236
}

static void
237
gimp_ink_tool_finalize (GObject *object)
238 239 240 241 242 243
{
  GimpInkTool *ink_tool;

  ink_tool = GIMP_INK_TOOL (object);

  if (ink_tool->last_blob)
244 245 246 247
    {
      g_free (ink_tool->last_blob);
      ink_tool->last_blob = NULL;
    }
248 249 250

  ink_cleanup ();

251
  G_OBJECT_CLASS (parent_class)->finalize (object);
252 253
}

254
static void
255 256 257
gimp_ink_tool_control (GimpTool       *tool,
                       GimpToolAction  action,
                       GimpDisplay    *gdisp)
258
{
259
  GimpInkTool *ink_tool;
260

261
  ink_tool = GIMP_INK_TOOL (tool);
262

263 264 265 266
  switch (action)
    {
    case PAUSE:
      break;
267

268 269
    case RESUME:
      break;
270

271 272 273
    case HALT:
      ink_cleanup ();
      break;
274

275 276 277
    default:
      break;
    }
278 279

  GIMP_TOOL_CLASS (parent_class)->control (tool, action, gdisp);
280 281
}

282 283 284 285 286 287
static void
gimp_ink_tool_button_press (GimpTool        *tool,
                            GimpCoords      *coords,
                            guint32          time,
                            GdkModifierType  state,
                            GimpDisplay     *gdisp)
288
{
289 290 291 292
  GimpInkTool    *ink_tool;
  GimpInkOptions *options;
  GimpDrawable   *drawable;
  Blob           *b;
293

294
  ink_tool = GIMP_INK_TOOL (tool);
295
  options  = GIMP_INK_OPTIONS (tool->tool_info->tool_options);
296

297
  drawable = gimp_image_active_drawable (gdisp->gimage);
jtl's avatar
jtl committed
298

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

301
  gimp_tool_control_activate (tool->control);
302
  tool->gdisp = gdisp; 
303

304
  /*  pause the current selection  */
305
  gimp_image_selection_control (gdisp->gimage, GIMP_SELECTION_PAUSE);
306

307 308
  b = ink_pen_ellipse (options,
                       coords->x,
309 310 311 312 313
                       coords->y,
		       coords->pressure,
                       coords->xtilt,
                       coords->ytilt,
		       10.0);
jtl's avatar
jtl committed
314

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

318 319 320 321 322 323
  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
324

325 326
  gimp_display_flush_now (gdisp);
}
jtl's avatar
jtl committed
327

328 329 330 331 332 333 334 335 336
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
337

338
  ink_tool = GIMP_INK_TOOL (tool);
339

340
  gimage = gdisp->gimage;
341

342
  /*  resume the current selection  */
343
  gimp_image_selection_control (gdisp->gimage, GIMP_SELECTION_RESUME);
344

345
  /*  Set tool state to inactive -- no longer painting */
346
  gimp_tool_control_halt (tool->control);
347

348 349 350
  /*  free the last blob  */
  g_free (ink_tool->last_blob);
  ink_tool->last_blob = NULL;
Raph Levien's avatar
Raph Levien committed
351

352
  ink_finish (ink_tool, gimp_image_active_drawable (gdisp->gimage));
353
  gimp_image_flush (gdisp->gimage);
354
}
355

356 357 358 359 360 361 362
static void
gimp_ink_tool_motion (GimpTool        *tool,
                      GimpCoords      *coords,
                      guint32          time,
                      GdkModifierType  state,
                      GimpDisplay     *gdisp)
{
363 364 365 366
  GimpInkTool    *ink_tool;
  GimpInkOptions *options;
  GimpDrawable   *drawable;
  Blob           *b, *blob_union;
367 368 369 370

  gdouble velocity;
  gdouble dist;
  gdouble lasttime, thistime;
371

372
  ink_tool = GIMP_INK_TOOL (tool);
373
  options  = GIMP_INK_OPTIONS (tool->tool_info->tool_options);
374

375
  drawable = gimp_image_active_drawable (gdisp->gimage);
376

377
  lasttime = ink_tool->last_time;
378

379 380
  time_smoother_add (ink_tool, time);
  thistime = ink_tool->last_time = time_smoother_result (ink_tool);
381

382 383 384 385 386 387
  /* 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 */
388

389 390
  if (thistime == lasttime)
    thistime = lasttime + 1;
391

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
  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)));
408

409 410
      dist = dist_smoother_result (ink_tool);
    }
411

412 413
  ink_tool->lastx = coords->x;
  ink_tool->lasty = coords->y;
414

415
  velocity = 10.0 * sqrt ((dist) / (gdouble) (thistime - lasttime));
416

417 418
  b = ink_pen_ellipse (options,
                       coords->x,
419 420 421 422 423
                       coords->y,
                       coords->pressure,
                       coords->xtilt,
		       coords->ytilt,
                       velocity);
424

425 426 427
  blob_union = blob_convex_union (ink_tool->last_blob, b);
  g_free (ink_tool->last_blob);
  ink_tool->last_blob = b;
428

429 430
  ink_paste (ink_tool, drawable, blob_union);  
  g_free (blob_union);
431

432 433
  gimp_display_flush_now (gdisp);
}
jtl's avatar
jtl committed
434

435 436 437 438 439 440
static void
gimp_ink_tool_cursor_update (GimpTool        *tool,
                             GimpCoords      *coords,
                             GdkModifierType  state,
                             GimpDisplay     *gdisp)
{
441 442
  GimpLayer     *layer;
  GdkCursorType  ctype = GDK_TOP_LEFT_ARROW;
443

444 445 446
  if ((layer = gimp_image_get_active_layer (gdisp->gimage))) 
    {
      gint off_x, off_y;
447

448
      gimp_drawable_offsets (GIMP_DRAWABLE (layer), &off_x, &off_y);
449

450 451 452 453 454 455 456 457
      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?
	   */
458
	  if (gimp_image_mask_is_empty (gdisp->gimage))
459
	    ctype = GIMP_MOUSE_CURSOR;
460
	  else if (gimp_image_mask_value (gdisp->gimage, coords->x, coords->y))
461 462 463
	    ctype = GIMP_MOUSE_CURSOR;
	}
    }
464

465
  gimp_tool_control_set_cursor (tool->control, ctype);
466 467

  GIMP_TOOL_CLASS (parent_class)->cursor_update (tool, coords, state, gdisp);
468 469
}

470

471
static Blob *
472 473 474 475 476 477 478
ink_pen_ellipse (GimpInkOptions *options,
                 gdouble         x_center,
		 gdouble         y_center,
		 gdouble         pressure,
		 gdouble         xtilt,
		 gdouble         ytilt,
		 gdouble         velocity)
479
{
480 481 482 483 484 485 486 487
  BlobFunc function = blob_ellipse;
  gdouble  size;
  gdouble  tsin, tcos;
  gdouble  aspect, radmin;
  gdouble  x,y;
  gdouble  tscale;
  gdouble  tscale_c;
  gdouble  tscale_s;
488

489 490
  /* Adjust the size depending on pressure. */

491 492
  size = options->size * (1.0 + options->sensitivity *
                          (2.0 * pressure - 1.0) );
493 494 495 496 497 498 499 500 501

  /* 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
502
  g_print ("%f (%f) -> ", (float)size, (float)velocity);
503 504
#endif  

505 506 507
  size = options->vel_sensitivity *
    ((4.5 * size) / (1.0 + options->vel_sensitivity * (2.0*(velocity))))
    + (1.0 - options->vel_sensitivity) * size;
508 509

#ifdef VERBOSE
510
  g_print ("%f\n", (float)size);
511 512 513 514
#endif

  /* Clamp resulting size to sane limits */

515 516
  if (size > options->size * (1.0 + options->sensitivity))
    size = options->size * (1.0 + options->sensitivity);
517

518 519 520
  if (size*SUBSAMPLE < 1.0) size = 1.0/SUBSAMPLE;

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

Raph Levien's avatar
Raph Levien committed
522 523 524 525
  /* 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 */

526 527 528 529
  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));

530
  x = (options->blob_aspect * cos (options->blob_angle) +
531
       xtilt * tscale_c - ytilt * tscale_s);
532
  y = (options->blob_aspect * sin (options->blob_angle) +
533 534
       ytilt * tscale_c + xtilt * tscale_s);

Raph Levien's avatar
Raph Levien committed
535 536
#ifdef VERBOSE
  g_print ("angle %g aspect %g; %g %g; %g %g\n",
537
	   options->blob_angle, options->blob_aspect, tscale_c, tscale_s, x, y);
Raph Levien's avatar
Raph Levien committed
538
#endif
539 540

  aspect = sqrt (x*x + y*y);
541 542 543

  if (aspect != 0)
    {
544 545
      tcos = x / aspect;
      tsin = y / aspect;
546 547 548
    }
  else
    {
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
      tsin = sin (options->blob_angle);
      tcos = cos (options->blob_angle);
    }

  aspect = CLAMP (aspect, 1.0, 10.0);

  radmin = MAX (1.0, SUBSAMPLE * size / aspect);

  switch (options->blob_type)
    {
    case GIMP_INK_BLOB_TYPE_ELLIPSE:
      function = blob_ellipse;
      break;

    case GIMP_INK_BLOB_TYPE_SQUARE:
      function = blob_square;
      break;

    case GIMP_INK_BLOB_TYPE_DIAMOND:
      function = blob_diamond;
      break;
570
    }
571

572 573 574 575 576 577
  return (* function) (x_center * SUBSAMPLE,
                       y_center * SUBSAMPLE,
                       radmin * aspect * tcos,
                       radmin * aspect * tsin,  
                       -radmin * tsin,
                       radmin * tcos);
578 579 580
}

static void
581 582
dist_smoother_init (GimpInkTool *ink_tool,
		    gdouble      initval)
583
{
584
  gint i;
585

586
  ink_tool->dt_index = 0;
587

588
  for (i = 0; i < DIST_SMOOTHER_BUFFER; i++)
589
    {
590 591 592
      ink_tool->dt_buffer[i] = initval;
    }
}
593

594 595 596 597 598
static gdouble
dist_smoother_result (GimpInkTool *ink_tool)
{
  gint    i;
  gdouble result = 0.0;
599

600 601 602
  for (i = 0; i < DIST_SMOOTHER_BUFFER; i++)
    {
      result += ink_tool->dt_buffer[i];
603
    }
604 605

  return (result / (gdouble) DIST_SMOOTHER_BUFFER);
606 607 608
}

static void
609 610
dist_smoother_add (GimpInkTool *ink_tool,
		   gdouble      value)
611
{
612
  ink_tool->dt_buffer[ink_tool->dt_index] = value;
Michael Natterer's avatar
Michael Natterer committed
613

614 615 616
  if ((++ink_tool->dt_index) == DIST_SMOOTHER_BUFFER)
    ink_tool->dt_index = 0;
}
617 618


619 620 621 622 623
static void
time_smoother_init (GimpInkTool *ink_tool,
		    guint32      initval)
{
  gint i;
624

625
  ink_tool->ts_index = 0;
626

627
  for (i = 0; i < TIME_SMOOTHER_BUFFER; i++)
628
    {
629
      ink_tool->ts_buffer[i] = initval;
630
    }
631 632 633 634 635 636 637 638 639
}

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

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

644 645 646 647 648 649
#ifdef _MSC_VER
  return (gdouble) (gint64) (result / TIME_SMOOTHER_BUFFER);
#else
  return (result / TIME_SMOOTHER_BUFFER);
#endif
}
650 651

static void
652 653
time_smoother_add (GimpInkTool *ink_tool,
		   guint32      value)
654 655 656 657 658 659 660
{
  ink_tool->ts_buffer[ink_tool->ts_index] = value;

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

661
static void
662
ink_init (GimpInkTool  *ink_tool,
663 664 665
	  GimpDrawable *drawable, 
	  gdouble       x,
	  gdouble       y)
666 667 668 669 670 671 672 673
{
  /*  free the block structures  */
  if (undo_tiles)
    tile_manager_destroy (undo_tiles);
  if (canvas_tiles)
    tile_manager_destroy (canvas_tiles);

  /*  Allocate the undo structure  */
674 675 676
  undo_tiles = tile_manager_new (gimp_drawable_width (drawable),
				 gimp_drawable_height (drawable),
				 gimp_drawable_bytes (drawable));
677 678

  /*  Allocate the canvas blocks structure  */
679 680
  canvas_tiles = tile_manager_new (gimp_drawable_width (drawable),
				   gimp_drawable_height (drawable), 1);
681 682 683 684 685 686 687

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

static void
688 689
ink_finish (GimpInkTool  *ink_tool,
	    GimpDrawable *drawable)
690
{
691 692 693 694
  gimp_drawable_push_undo (drawable,
                           ink_tool->x1, ink_tool->y1,
                           ink_tool->x2, ink_tool->y2,
                           undo_tiles, TRUE);
695 696 697 698 699
  undo_tiles = NULL;

  /*  invalidate the drawable--have to do it here, because
   *  it is not done during the actual painting.
   */
700
  gimp_viewable_invalidate_preview (GIMP_VIEWABLE (drawable));
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
}

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)
722 723 724 725
    {
      temp_buf_free (canvas_buf);
      canvas_buf = NULL;
    }
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
}

/*********************************
 *  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
742
ink_set_paint_area (GimpInkTool  *ink_tool, 
743 744 745
		    GimpDrawable *drawable, 
		    Blob         *blob)
{
746 747 748
  gint x, y, width, height;
  gint x1, y1, x2, y2;
  gint bytes;
749

750 751
  blob_bounds (blob, &x, &y, &width, &height);

752 753
  bytes = gimp_drawable_has_alpha (drawable) ?
    gimp_drawable_bytes (drawable) : gimp_drawable_bytes (drawable) + 1;
754

755 756 757 758
  x1 = CLAMP (x/SUBSAMPLE - 1,            0, gimp_drawable_width (drawable));
  y1 = CLAMP (y/SUBSAMPLE - 1,            0, gimp_drawable_height (drawable));
  x2 = CLAMP ((x + width)/SUBSAMPLE + 2,  0, gimp_drawable_width (drawable));
  y2 = CLAMP ((y + height)/SUBSAMPLE + 2, 0, gimp_drawable_height (drawable));
759 760 761 762 763 764 765 766 767 768 769 770

  /*  configure the canvas buffer  */
  if ((x2 - x1) && (y2 - y1))
    canvas_buf = temp_buf_resize (canvas_buf, bytes, x1, y1,
				  (x2 - x1), (y2 - y1));
}

enum { ROW_START, ROW_STOP };

/* The insertion sort here, for SUBSAMPLE = 8, tends to beat out
 * qsort() by 4x with CFLAGS=-O2, 2x with CFLAGS=-g
 */
771 772 773
static void
insert_sort (gint *data,
	     gint  n)
774
{
775 776
  gint i, j, k;
  gint tmp1, tmp2;
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799

  for (i=2; i<2*n; i+=2)
    {
      tmp1 = data[i];
      tmp2 = data[i+1];
      j = 0;
      while (data[j] < tmp1)
	j += 2;

      for (k=i; k>j; k-=2)
	{
	  data[k] = data[k-2];
	  data[k+1] = data[k-1];
	}

      data[j] = tmp1;
      data[j+1] = tmp2;
    }
}

static void
fill_run (guchar *dest,
	  guchar  alpha,
800
	  gint    w)
801 802 803 804 805 806 807 808 809
{
  if (alpha == 255)
    {
      memset (dest, 255, w);
    }
  else
    {
      while (w--)
	{
810
	  *dest = MAX(*dest, alpha);
811 812 813 814 815 816
	  dest++;
	}
    }
}

static void
817 818 819 820 821
render_blob_line (Blob   *blob,
		  guchar *dest,
		  gint    x,
		  gint    y,
		  gint    width)
822
{
823 824 825
  gint  buf[4 * SUBSAMPLE];
  gint *data    = buf;
  gint  n       = 0;
826 827 828 829 830
  gint  i, j;
  gint  current = 0;  /* number of filled rows at this point
		       * in the scan line
		       */
  gint last_x;
831 832 833 834

  /* Sort start and ends for all lines */
  
  j = y * SUBSAMPLE - blob->y;
835
  for (i = 0; i < SUBSAMPLE; i++)
836 837 838
    {
      if (j >= blob->height)
	break;
839

840 841
      if ((j > 0) && (blob->data[j].left <= blob->data[j].right))
	{
842 843 844 845
	  data[2 * n]                     = blob->data[j].left;
	  data[2 * n + 1]                 = ROW_START;
	  data[2 * SUBSAMPLE + 2 * n]     = blob->data[j].right;
	  data[2 * SUBSAMPLE + 2 * n + 1] = ROW_STOP;
846 847 848 849 850 851 852 853
	  n++;
	}
      j++;
    }

  /*   If we have less than SUBSAMPLE rows, compress */
  if (n < SUBSAMPLE)
    {
854 855
      for (i = 0; i < 2 * n; i++)
	data[2 * n + i] = data[2 * SUBSAMPLE + i];
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
    }

  /*   Now count start and end separately */
  n *= 2;

  insert_sort (data, n);

  /* Discard portions outside of tile */

  while ((n > 0) && (data[0] < SUBSAMPLE*x))
    {
      if (data[1] == ROW_START)
	current++;
      else
	current--;
      data += 2;
      n--;
    }

  while ((n > 0) && (data[2*(n-1)] >= SUBSAMPLE*(x+width)))
    n--;
  
  /* Render the row */

  last_x = 0;
881
  for (i = 0; i < n;)
882
    {
883
      gint cur_x = data[2 * i] / SUBSAMPLE - x;
884
      gint pixel;
885 886 887

      /* Fill in portion leading up to this pixel */
      if (current && cur_x != last_x)
888
	fill_run (dest + last_x, (255 * current) / SUBSAMPLE, cur_x - last_x);
889 890 891 892 893 894

      /* Compute the value for this pixel */
      pixel = current * SUBSAMPLE; 

      while (i<n)
	{
895 896
	  gint tmp_x = data[2 * i] / SUBSAMPLE;

897 898 899
	  if (tmp_x - x != cur_x)
	    break;

900
	  if (data[2 * i + 1] == ROW_START)
901 902
	    {
	      current++;
903
	      pixel += ((tmp_x + 1) * SUBSAMPLE) - data[2 * i];
904 905 906 907
	    }
	  else
	    {
	      current--;
908
	      pixel -= ((tmp_x + 1) * SUBSAMPLE) - data[2 * i];
909 910 911 912 913
	    }
	  
	  i++;
	}

914
      dest[cur_x] = MAX (dest[cur_x], (pixel * 255) / (SUBSAMPLE * SUBSAMPLE));
915 916 917 918 919

      last_x = cur_x + 1;
    }

  if (current != 0)
920
    fill_run (dest + last_x, (255 * current)/ SUBSAMPLE, width - last_x);
921 922 923
}

static void
924 925
render_blob (PixelRegion *dest,
	     Blob        *blob)
926
{
927 928 929 930
  gint      i;
  gint      h;
  guchar   *s;
  gpointer  pr;
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948

  for (pr = pixel_regions_register (1, dest); 
       pr != NULL; 
       pr = pixel_regions_process (pr))
    {
      h = dest->h;
      s = dest->data;

      for (i=0; i<h; i++)
	{
	  render_blob_line (blob, s,
			    dest->x, dest->y + i, dest->w);
	  s += dest->rowstride;
	}
    }
}

static void
949
ink_paste (GimpInkTool  *ink_tool, 
950 951 952
	   GimpDrawable *drawable,
	   Blob         *blob)
{
953
  GimpImage   *gimage;
Michael Natterer's avatar
Michael Natterer committed
954
  GimpContext *context;
955 956 957
  PixelRegion  srcPR;
  gint         offx, offy;
  gchar        col[MAX_CHANNELS];
958

959
  if (! (gimage = gimp_item_get_image (GIMP_ITEM (drawable))))
960
    return;
961

962
  context = GIMP_CONTEXT (GIMP_TOOL (ink_tool)->tool_info->tool_options);
Michael Natterer's avatar
Michael Natterer committed
963

964 965
  /* Get the the buffer */
  ink_set_paint_area (ink_tool, drawable, blob);
966 967 968 969
 
  /* check to make sure there is actually a canvas to draw on */
  if (!canvas_buf)
    return;
970

971
  gimp_image_get_foreground (gimage, drawable, col);
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999

  /*  set the alpha channel  */
  col[canvas_buf->bytes - 1] = OPAQUE_OPACITY;

  /*  color the pixels  */
  color_pixels (temp_buf_data (canvas_buf), col,
		canvas_buf->width * canvas_buf->height, canvas_buf->bytes);

  /*  set undo blocks  */
  ink_set_undo_tiles (drawable,
		      canvas_buf->x, canvas_buf->y,
		      canvas_buf->width, canvas_buf->height);

  /*  initialize any invalid canvas tiles  */
  ink_set_canvas_tiles (canvas_buf->x, canvas_buf->y,
			canvas_buf->width, canvas_buf->height);

  ink_to_canvas_tiles (ink_tool, blob, col);

  /*  initialize canvas buf source pixel regions  */
  srcPR.bytes = canvas_buf->bytes;
  srcPR.x = 0; srcPR.y = 0;
  srcPR.w = canvas_buf->width;
  srcPR.h = canvas_buf->height;
  srcPR.rowstride = canvas_buf->width * canvas_buf->bytes;
  srcPR.data = temp_buf_data (canvas_buf);

  /*  apply the paint area to the gimage  */
1000 1001
  gimp_image_apply_image (gimage, drawable, &srcPR,
			  FALSE, 
1002
			  gimp_context_get_opacity (context),
Michael Natterer's avatar
Michael Natterer committed
1003
			  gimp_context_get_paint_mode (context),
1004 1005
			  undo_tiles,  /*  specify an alternative src1  */
			  canvas_buf->x, canvas_buf->y);
1006 1007 1008 1009 1010 1011 1012

  /*  Update the undo extents  */
  ink_tool->x1 = MIN (ink_tool->x1, canvas_buf->x);
  ink_tool->y1 = MIN (ink_tool->y1, canvas_buf->y);
  ink_tool->x2 = MAX (ink_tool->x2, (canvas_buf->x + canvas_buf->width));
  ink_tool->y2 = MAX (ink_tool->y2, (canvas_buf->y + canvas_buf->height));

Michael Natterer's avatar
Michael Natterer committed
1013
  /*  Update the gimage--it is important to call gimp_image_update
1014 1015 1016
   *  instead of drawable_update because we don't want the drawable
   *  preview to be constantly invalidated
   */
1017
  gimp_drawable_offsets (drawable, &offx, &offy);
Michael Natterer's avatar
Michael Natterer committed
1018 1019 1020 1021 1022
  gimp_image_update (gimage,
                     canvas_buf->x + offx,
                     canvas_buf->y + offy,
                     canvas_buf->width,
                     canvas_buf->height);
1023 1024
}

1025 1026 1027 1028
/* This routine a) updates the representation of the stroke
 * in the canvas tiles, then renders the dirty bit of it
 * into canvas_buf.
 */
1029
static void
1030 1031 1032
ink_to_canvas_tiles (GimpInkTool *ink_tool,
		     Blob        *blob,
		     guchar      *color)
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
{
  PixelRegion srcPR, maskPR;

  /*  draw the blob on the canvas tiles  */
  pixel_region_init (&srcPR, canvas_tiles,
		     canvas_buf->x, canvas_buf->y,
		     canvas_buf->width, canvas_buf->height, TRUE);

  render_blob (&srcPR, blob);

  /*  combine the canvas tiles and the canvas buf  */
  srcPR.bytes = canvas_buf->bytes;
  srcPR.x = 0; srcPR.y = 0;
  srcPR.w = canvas_buf->width;
  srcPR.h = canvas_buf->height;
  srcPR.rowstride = canvas_buf->width * canvas_buf->bytes;
  srcPR.data = temp_buf_data (canvas_buf);

  pixel_region_init (&maskPR, canvas_tiles,
		     canvas_buf->x, canvas_buf->y,
		     canvas_buf->width, canvas_buf->height, FALSE);

  /*  apply the canvas tiles to the canvas buf  */
  apply_mask_to_region (&srcPR, &maskPR, OPAQUE_OPACITY);
}

static void
1060
ink_set_undo_tiles (GimpDrawable *drawable,
1061 1062 1063 1064
		    gint          x,
		    gint          y,
		    gint          w,
		    gint          h)
1065
{
1066
  gint  i, j;
1067 1068 1069 1070 1071 1072 1073
  Tile *src_tile;
  Tile *dest_tile;

  for (i = y; i < (y + h); i += (TILE_HEIGHT - (i % TILE_HEIGHT)))
    {
      for (j = x; j < (x + w); j += (