gimpink.c 28.8 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
  gdouble size;
  gdouble tsin, tcos;
  gdouble aspect, radmin;
  gdouble x,y;
  gdouble tscale;
  gdouble tscale_c;
  gdouble tscale_s;

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

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

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

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

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

  /* Clamp resulting size to sane limits */

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

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

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

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

525 526 527 528 529 530 531 532 533
  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
534 535
#ifdef VERBOSE
  g_print ("angle %g aspect %g; %g %g; %g %g\n",
536
	   options->angle, options->aspect, tscale_c, tscale_s, x, y);
Raph Levien's avatar
Raph Levien committed
537
#endif
538 539 540 541 542 543 544 545 546
  aspect = sqrt(x*x+y*y);

  if (aspect != 0)
    {
      tcos = x/aspect;
      tsin = y/aspect;
    }
  else
    {
547 548
      tsin = sin (options->angle);
      tcos = cos (options->angle);
549
    }
550

551 552 553 554 555 556 557 558
  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;
  
559 560 561 562 563 564
  return options->function (x_center * SUBSAMPLE,
                            y_center * SUBSAMPLE,
                            radmin * aspect * tcos,
                            radmin * aspect * tsin,  
                            -radmin * tsin,
                            radmin * tcos);
565 566 567
}

static void
568 569
dist_smoother_init (GimpInkTool *ink_tool,
		    gdouble      initval)
570
{
571
  gint i;
572

573
  ink_tool->dt_index = 0;
574

575
  for (i=0; i<DIST_SMOOTHER_BUFFER; i++)
576
    {
577 578 579
      ink_tool->dt_buffer[i] = initval;
    }
}
580

581 582 583 584 585
static gdouble
dist_smoother_result (GimpInkTool *ink_tool)
{
  gint    i;
  gdouble result = 0.0;
586

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

  return (result / (gdouble) DIST_SMOOTHER_BUFFER);
593 594 595
}

static void
596 597
dist_smoother_add (GimpInkTool *ink_tool,
		   gdouble      value)
598
{
599
  ink_tool->dt_buffer[ink_tool->dt_index] = value;
Michael Natterer's avatar
Michael Natterer committed
600

601 602 603
  if ((++ink_tool->dt_index) == DIST_SMOOTHER_BUFFER)
    ink_tool->dt_index = 0;
}
604 605


606 607 608 609 610
static void
time_smoother_init (GimpInkTool *ink_tool,
		    guint32      initval)
{
  gint i;
611

612
  ink_tool->ts_index = 0;
613

614
  for (i = 0; i < TIME_SMOOTHER_BUFFER; i++)
615
    {
616
      ink_tool->ts_buffer[i] = initval;
617
    }
618 619 620 621 622 623 624 625 626
}

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

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

631 632 633 634 635 636
#ifdef _MSC_VER
  return (gdouble) (gint64) (result / TIME_SMOOTHER_BUFFER);
#else
  return (result / TIME_SMOOTHER_BUFFER);
#endif
}
637 638

static void
639 640
time_smoother_add (GimpInkTool *ink_tool,
		   guint32      value)
641 642 643 644 645 646 647
{
  ink_tool->ts_buffer[ink_tool->ts_index] = value;

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

648
static void
649
ink_init (GimpInkTool  *ink_tool,
650 651 652
	  GimpDrawable *drawable, 
	  gdouble       x,
	  gdouble       y)
653 654 655 656 657 658 659 660
{
  /*  free the block structures  */
  if (undo_tiles)
    tile_manager_destroy (undo_tiles);
  if (canvas_tiles)
    tile_manager_destroy (canvas_tiles);

  /*  Allocate the undo structure  */
661 662 663
  undo_tiles = tile_manager_new (gimp_drawable_width (drawable),
				 gimp_drawable_height (drawable),
				 gimp_drawable_bytes (drawable));
664 665

  /*  Allocate the canvas blocks structure  */
666 667
  canvas_tiles = tile_manager_new (gimp_drawable_width (drawable),
				   gimp_drawable_height (drawable), 1);
668 669 670 671 672 673 674

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

static void
675 676
ink_finish (GimpInkTool  *ink_tool,
	    GimpDrawable *drawable)
677
{
678 679 680 681
  gimp_drawable_push_undo (drawable,
                           ink_tool->x1, ink_tool->y1,
                           ink_tool->x2, ink_tool->y2,
                           undo_tiles, TRUE);
682 683 684 685 686
  undo_tiles = NULL;

  /*  invalidate the drawable--have to do it here, because
   *  it is not done during the actual painting.
   */
687
  gimp_viewable_invalidate_preview (GIMP_VIEWABLE (drawable));
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
}

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)
709 710 711 712
    {
      temp_buf_free (canvas_buf);
      canvas_buf = NULL;
    }
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
}

/*********************************
 *  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
729
ink_set_paint_area (GimpInkTool  *ink_tool, 
730 731 732
		    GimpDrawable *drawable, 
		    Blob         *blob)
{
733 734 735
  gint x, y, width, height;
  gint x1, y1, x2, y2;
  gint bytes;
736

737 738
  blob_bounds (blob, &x, &y, &width, &height);

739 740
  bytes = gimp_drawable_has_alpha (drawable) ?
    gimp_drawable_bytes (drawable) : gimp_drawable_bytes (drawable) + 1;
741

742 743 744 745
  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));
746 747 748 749 750 751 752 753 754 755 756 757

  /*  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
 */
758 759 760
static void
insert_sort (gint *data,
	     gint  n)
761
{
762 763
  gint i, j, k;
  gint tmp1, tmp2;
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786

  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,
787
	  gint    w)
788 789 790 791 792 793 794 795 796
{
  if (alpha == 255)
    {
      memset (dest, 255, w);
    }
  else
    {
      while (w--)
	{
797
	  *dest = MAX(*dest, alpha);
798 799 800 801 802 803
	  dest++;
	}
    }
}

static void
804 805 806 807 808
render_blob_line (Blob   *blob,
		  guchar *dest,
		  gint    x,
		  gint    y,
		  gint    width)
809
{
810 811 812
  gint  buf[4 * SUBSAMPLE];
  gint *data    = buf;
  gint  n       = 0;
813 814 815 816 817
  gint  i, j;
  gint  current = 0;  /* number of filled rows at this point
		       * in the scan line
		       */
  gint last_x;
818 819 820 821

  /* Sort start and ends for all lines */
  
  j = y * SUBSAMPLE - blob->y;
822
  for (i = 0; i < SUBSAMPLE; i++)
823 824 825
    {
      if (j >= blob->height)
	break;
826

827 828
      if ((j > 0) && (blob->data[j].left <= blob->data[j].right))
	{
829 830 831 832
	  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;
833 834 835 836 837 838 839 840
	  n++;
	}
      j++;
    }

  /*   If we have less than SUBSAMPLE rows, compress */
  if (n < SUBSAMPLE)
    {
841 842
      for (i = 0; i < 2 * n; i++)
	data[2 * n + i] = data[2 * SUBSAMPLE + i];
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
    }

  /*   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;
868
  for (i = 0; i < n;)
869
    {
870
      gint cur_x = data[2 * i] / SUBSAMPLE - x;
871
      gint pixel;
872 873 874

      /* Fill in portion leading up to this pixel */
      if (current && cur_x != last_x)
875
	fill_run (dest + last_x, (255 * current) / SUBSAMPLE, cur_x - last_x);
876 877 878 879 880 881

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

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

884 885 886
	  if (tmp_x - x != cur_x)
	    break;

887
	  if (data[2 * i + 1] == ROW_START)
888 889
	    {
	      current++;
890
	      pixel += ((tmp_x + 1) * SUBSAMPLE) - data[2 * i];
891 892 893 894
	    }
	  else
	    {
	      current--;
895
	      pixel -= ((tmp_x + 1) * SUBSAMPLE) - data[2 * i];
896 897 898 899 900
	    }
	  
	  i++;
	}

901
      dest[cur_x] = MAX (dest[cur_x], (pixel * 255) / (SUBSAMPLE * SUBSAMPLE));
902 903 904 905 906

      last_x = cur_x + 1;
    }

  if (current != 0)
907
    fill_run (dest + last_x, (255 * current)/ SUBSAMPLE, width - last_x);
908 909 910
}

static void
911 912
render_blob (PixelRegion *dest,
	     Blob        *blob)
913
{
914 915 916 917
  gint      i;
  gint      h;
  guchar   *s;
  gpointer  pr;
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935

  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
936
ink_paste (GimpInkTool  *ink_tool, 
937 938 939
	   GimpDrawable *drawable,
	   Blob         *blob)
{
940
  GimpImage   *gimage;
Michael Natterer's avatar
Michael Natterer committed
941
  GimpContext *context;
942 943 944
  PixelRegion  srcPR;
  gint         offx, offy;
  gchar        col[MAX_CHANNELS];
945

946
  if (! (gimage = gimp_item_get_image (GIMP_ITEM (drawable))))
947
    return;
948

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

951 952
  /* Get the the buffer */
  ink_set_paint_area (ink_tool, drawable, blob);
953 954 955 956
 
  /* check to make sure there is actually a canvas to draw on */
  if (!canvas_buf)
    return;
957

958
  gimp_image_get_foreground (gimage, drawable, col);
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986

  /*  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  */
987 988
  gimp_image_apply_image (gimage, drawable, &srcPR,
			  FALSE, 
989
			  gimp_context_get_opacity (context),
Michael Natterer's avatar
Michael Natterer committed
990
			  gimp_context_get_paint_mode (context),
991 992
			  undo_tiles,  /*  specify an alternative src1  */
			  canvas_buf->x, canvas_buf->y);
993 994 995 996 997 998 999

  /*  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
1000
  /*  Update the gimage--it is important to call gimp_image_update
1001 1002 1003
   *  instead of drawable_update because we don't want the drawable
   *  preview to be constantly invalidated
   */
1004
  gimp_drawable_offsets (drawable, &offx, &offy);
Michael Natterer's avatar
Michael Natterer committed
1005 1006 1007 1008 1009
  gimp_image_update (gimage,
                     canvas_buf->x + offx,
                     canvas_buf->y + offy,
                     canvas_buf->width,
                     canvas_buf->height);
1010 1011
}

1012 1013 1014 1015
/* This routine a) updates the representation of the stroke
 * in the canvas tiles, then renders the dirty bit of it
 * into canvas_buf.
 */
1016
static void
1017 1018 1019
ink_to_canvas_tiles (GimpInkTool *ink_tool,
		     Blob        *blob,
		     guchar      *color)
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
{
  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
1047
ink_set_undo_tiles (GimpDrawable *drawable,
1048 1049 1050 1051
		    gint          x,
		    gint          y,
		    gint          w,
		    gint          h)
1052
{
1053
  gint  i, j;
1054 1055 1056 1057 1058 1059 1060
  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 += (TILE_WIDTH - (j % TILE_WIDTH)))
	{
1061
	  dest_tile = tile_manager_get_tile (undo_tiles, j, i, FALSE, FALSE);
scott's avatar
scott committed
1062
	  if (tile_is_valid (dest_tile) == FALSE)
1063
	    {
1064 1065
	      src_tile = tile_manager_get_tile (gimp_drawable_data (drawable),
						j, i, TRUE, FALSE);
1066
	      tile_manager_map_tile (undo_tiles, j, i, src_tile);
1067
	      tile_release (src_tile, FALSE);
1068 1069 1070 1071