gimpink.c 17.9 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * 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"

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

23
#include <gegl.h>
Sven Neumann's avatar
Sven Neumann committed
24

25 26
#include "libgimpmath/gimpmath.h"

27
#include "paint-types.h"
Sven Neumann's avatar
Sven Neumann committed
28

Michael Natterer's avatar
Michael Natterer committed
29 30 31
#include "base/pixel-region.h"
#include "base/temp-buf.h"

32 33
#include "paint-funcs/paint-funcs.h"

34
#include "core/gimpdrawable.h"
35
#include "core/gimpimage.h"
36
#include "core/gimpimage-undo.h"
37

38
#include "gimpinkoptions.h"
39 40
#include "gimpink.h"
#include "gimpink-blob.h"
41
#include "gimpinkundo.h"
42

43
#include "gimp-intl.h"
44

45

46
#define SUBSAMPLE 8
47

48

49
/*  local function prototypes  */
50

51 52 53 54 55 56 57 58 59 60
static void      gimp_ink_finalize       (GObject          *object);

static void      gimp_ink_paint          (GimpPaintCore    *paint_core,
                                          GimpDrawable     *drawable,
                                          GimpPaintOptions *paint_options,
                                          GimpPaintState    paint_state,
                                          guint32           time);
static TempBuf * gimp_ink_get_paint_area (GimpPaintCore    *paint_core,
                                          GimpDrawable     *drawable,
                                          GimpPaintOptions *paint_options);
61 62 63
static GimpUndo* gimp_ink_push_undo      (GimpPaintCore    *core,
                                          GimpImage        *image,
                                          const gchar      *undo_desc);
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

static void      gimp_ink_motion         (GimpPaintCore    *paint_core,
                                          GimpDrawable     *drawable,
                                          GimpPaintOptions *paint_options,
                                          guint32           time);

static Blob    * ink_pen_ellipse         (GimpInkOptions   *options,
                                          gdouble           x_center,
                                          gdouble           y_center,
                                          gdouble           pressure,
                                          gdouble           xtilt,
                                          gdouble           ytilt,
                                          gdouble           velocity);

static void      render_blob             (Blob             *blob,
                                          PixelRegion      *dest);
80

81

82
G_DEFINE_TYPE (GimpInk, gimp_ink, GIMP_TYPE_PAINT_CORE)
Michael Natterer's avatar
Michael Natterer committed
83 84

#define parent_class gimp_ink_parent_class
85

86

87
void
88 89
gimp_ink_register (Gimp                      *gimp,
                   GimpPaintRegisterCallback  callback)
90
{
91 92
  (* callback) (gimp,
                GIMP_TYPE_INK,
93
                GIMP_TYPE_INK_OPTIONS,
94 95 96
                "gimp-ink",
                _("Ink"),
                "gimp-tool-ink");
97 98 99
}

static void
100
gimp_ink_class_init (GimpInkClass *klass)
101
{
102 103
  GObjectClass       *object_class     = G_OBJECT_CLASS (klass);
  GimpPaintCoreClass *paint_core_class = GIMP_PAINT_CORE_CLASS (klass);
104

Michael Natterer's avatar
Michael Natterer committed
105
  object_class->finalize           = gimp_ink_finalize;
106

107 108
  paint_core_class->paint          = gimp_ink_paint;
  paint_core_class->get_paint_area = gimp_ink_get_paint_area;
Michael Natterer's avatar
Michael Natterer committed
109
  paint_core_class->push_undo      = gimp_ink_push_undo;
110 111 112
}

static void
113
gimp_ink_init (GimpInk *ink)
114 115 116 117
{
}

static void
118
gimp_ink_finalize (GObject *object)
119
{
120
  GimpInk *ink = GIMP_INK (object);
121

Michael Natterer's avatar
Michael Natterer committed
122 123 124 125 126 127
  if (ink->start_blob)
    {
      g_free (ink->start_blob);
      ink->start_blob = NULL;
    }

128
  if (ink->last_blob)
129
    {
130 131
      g_free (ink->last_blob);
      ink->last_blob = NULL;
132
    }
133

134
  G_OBJECT_CLASS (parent_class)->finalize (object);
135 136
}

137
static void
138 139 140 141 142
gimp_ink_paint (GimpPaintCore    *paint_core,
                GimpDrawable     *drawable,
                GimpPaintOptions *paint_options,
                GimpPaintState    paint_state,
                guint32           time)
143
{
144
  GimpInk *ink = GIMP_INK (paint_core);
145 146

  switch (paint_state)
147
    {
148
    case GIMP_PAINT_STATE_INIT:
Michael Natterer's avatar
Michael Natterer committed
149
      if (paint_core->cur_coords.x == paint_core->last_coords.x &&
150 151
          paint_core->cur_coords.y == paint_core->last_coords.y)
        {
Michael Natterer's avatar
Michael Natterer committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
          /*  start with new blobs if we're not interpolating  */

          if (ink->start_blob)
            {
              g_free (ink->start_blob);
              ink->start_blob = NULL;
            }

          if (ink->last_blob)
            {
              g_free (ink->last_blob);
              ink->last_blob = NULL;
            }
        }
      else if (ink->last_blob)
        {
          /*  save the start blob of the line for undo otherwise  */

          if (ink->start_blob)
            g_free (ink->start_blob);

          ink->start_blob = blob_duplicate (ink->last_blob);
174
        }
175
      break;
176

177
    case GIMP_PAINT_STATE_MOTION:
178
      gimp_ink_motion (paint_core, drawable, paint_options, time);
179
      break;
180

181
    case GIMP_PAINT_STATE_FINISH:
182 183
      break;
    }
184
}
jtl's avatar
jtl committed
185

186 187 188 189
static TempBuf *
gimp_ink_get_paint_area (GimpPaintCore    *paint_core,
                         GimpDrawable     *drawable,
                         GimpPaintOptions *paint_options)
190
{
Sven Neumann's avatar
Sven Neumann committed
191
  GimpInk *ink = GIMP_INK (paint_core);
192 193 194 195 196
  gint     x, y;
  gint     width, height;
  gint     dwidth, dheight;
  gint     x1, y1, x2, y2;
  gint     bytes;
jtl's avatar
jtl committed
197

198
  bytes = gimp_drawable_bytes_with_alpha (drawable);
199

Michael Natterer's avatar
Michael Natterer committed
200
  blob_bounds (ink->cur_blob, &x, &y, &width, &height);
201

202 203
  dwidth  = gimp_item_get_width  (GIMP_ITEM (drawable));
  dheight = gimp_item_get_height (GIMP_ITEM (drawable));
204

205 206 207 208
  x1 = CLAMP (x / SUBSAMPLE - 1,            0, dwidth);
  y1 = CLAMP (y / SUBSAMPLE - 1,            0, dheight);
  x2 = CLAMP ((x + width)  / SUBSAMPLE + 2, 0, dwidth);
  y2 = CLAMP ((y + height) / SUBSAMPLE + 2, 0, dheight);
209

210 211 212 213 214 215 216
  /*  configure the canvas buffer  */
  if ((x2 - x1) && (y2 - y1))
    paint_core->canvas_buf = temp_buf_resize (paint_core->canvas_buf, bytes,
                                              x1, y1,
                                              (x2 - x1), (y2 - y1));
  else
    return NULL;
Raph Levien's avatar
Raph Levien committed
217

218
  return paint_core->canvas_buf;
219
}
220

221 222 223 224 225 226 227 228 229 230 231 232
static GimpUndo *
gimp_ink_push_undo (GimpPaintCore *core,
                    GimpImage     *image,
                    const gchar   *undo_desc)
{
  return gimp_image_undo_push (image, GIMP_TYPE_INK_UNDO,
                               GIMP_UNDO_INK, undo_desc,
                               0,
                               "paint-core", core,
                               NULL);
}

233
static void
234 235 236 237
gimp_ink_motion (GimpPaintCore    *paint_core,
                 GimpDrawable     *drawable,
                 GimpPaintOptions *paint_options,
                 guint32           time)
238
{
239 240 241
  GimpInk        *ink     = GIMP_INK (paint_core);
  GimpInkOptions *options = GIMP_INK_OPTIONS (paint_options);
  GimpContext    *context = GIMP_CONTEXT (paint_options);
242
  GimpImage      *image;
243 244
  Blob           *blob_union = NULL;
  Blob           *blob_to_render;
245 246 247
  TempBuf        *area;
  guchar          col[MAX_CHANNELS];
  PixelRegion     blob_maskPR;
248

249
  image = gimp_item_get_image (GIMP_ITEM (drawable));
250

251 252 253 254 255 256 257 258
  if (! ink->last_blob)
    {
      ink->last_blob = ink_pen_ellipse (options,
                                        paint_core->cur_coords.x,
                                        paint_core->cur_coords.y,
                                        paint_core->cur_coords.pressure,
                                        paint_core->cur_coords.xtilt,
                                        paint_core->cur_coords.ytilt,
259
                                        100);
260

Michael Natterer's avatar
Michael Natterer committed
261 262 263 264 265
      if (ink->start_blob)
        g_free (ink->start_blob);

      ink->start_blob = blob_duplicate (ink->last_blob);

266
      blob_to_render = ink->last_blob;
267 268 269
    }
  else
    {
Sven Neumann's avatar
Sven Neumann committed
270 271 272 273 274 275 276
      Blob *blob = ink_pen_ellipse (options,
                                    paint_core->cur_coords.x,
                                    paint_core->cur_coords.y,
                                    paint_core->cur_coords.pressure,
                                    paint_core->cur_coords.xtilt,
                                    paint_core->cur_coords.ytilt,
                                    paint_core->cur_coords.velocity * 100);
277 278

      blob_union = blob_convex_union (ink->last_blob, blob);
Sven Neumann's avatar
Sven Neumann committed
279

280 281
      g_free (ink->last_blob);
      ink->last_blob = blob;
282

283 284
      blob_to_render = blob_union;
    }
285

286
  /* Get the buffer */
Michael Natterer's avatar
Michael Natterer committed
287
  ink->cur_blob = blob_to_render;
288
  area = gimp_paint_core_get_paint_area (paint_core, drawable, paint_options);
Michael Natterer's avatar
Michael Natterer committed
289 290
  ink->cur_blob = NULL;

291 292
  if (! area)
    return;
jtl's avatar
jtl committed
293

294 295
  gimp_image_get_foreground (image, context, gimp_drawable_type (drawable),
                             col);
296

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

300
  /*  color the pixels  */
301
  color_pixels (temp_buf_get_data (paint_core->canvas_buf), col,
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
                area->width * area->height, area->bytes);

  gimp_paint_core_validate_canvas_tiles (paint_core,
                                         paint_core->canvas_buf->x,
                                         paint_core->canvas_buf->y,
                                         paint_core->canvas_buf->width,
                                         paint_core->canvas_buf->height);

  /*  draw the blob directly to the canvas_tiles  */
  pixel_region_init (&blob_maskPR, paint_core->canvas_tiles,
                     paint_core->canvas_buf->x,
                     paint_core->canvas_buf->y,
                     paint_core->canvas_buf->width,
                     paint_core->canvas_buf->height,
                     TRUE);

318
  render_blob (blob_to_render, &blob_maskPR);
319 320 321 322 323 324 325 326 327 328 329 330 331 332

  /*  draw the canvas_buf using the just rendered canvas_tiles as mask */
  pixel_region_init (&blob_maskPR, paint_core->canvas_tiles,
                     paint_core->canvas_buf->x,
                     paint_core->canvas_buf->y,
                     paint_core->canvas_buf->width,
                     paint_core->canvas_buf->height,
                     FALSE);

  gimp_paint_core_paste (paint_core, &blob_maskPR, drawable,
                         GIMP_OPACITY_OPAQUE,
                         gimp_context_get_opacity (context),
                         gimp_context_get_paint_mode (context),
                         GIMP_PAINT_CONSTANT);
333

334 335
  if (blob_union)
    g_free (blob_union);
336 337 338
}

static Blob *
339 340
ink_pen_ellipse (GimpInkOptions *options,
                 gdouble         x_center,
341 342 343 344 345
                 gdouble         y_center,
                 gdouble         pressure,
                 gdouble         xtilt,
                 gdouble         ytilt,
                 gdouble         velocity)
346
{
347
  BlobFunc blob_function;
348 349 350 351 352 353 354
  gdouble  size;
  gdouble  tsin, tcos;
  gdouble  aspect, radmin;
  gdouble  x,y;
  gdouble  tscale;
  gdouble  tscale_c;
  gdouble  tscale_s;
355

356 357
  /* Adjust the size depending on pressure. */

358
  size = options->size * (1.0 + options->size_sensitivity *
359
                          (2.0 * pressure - 1.0));
360

361 362 363 364
  /* Adjust the size further depending on pointer velocity and
   * velocity-sensitivity.  These 'magic constants' are 'feels
   * natural' tigert-approved. --ADM
   */
365 366 367 368 369

  if (velocity < 3.0)
    velocity = 3.0;

#ifdef VERBOSE
370
  g_printerr ("%g (%g) -> ", size, velocity);
371
#endif
372

373 374 375
  size = (options->vel_sensitivity *
          ((4.5 * size) / (1.0 + options->vel_sensitivity * (2.0 * velocity)))
          + (1.0 - options->vel_sensitivity) * size);
376 377

#ifdef VERBOSE
378
  g_printerr ("%g\n", (gfloat) size);
379 380 381 382
#endif

  /* Clamp resulting size to sane limits */

383 384
  if (size > options->size * (1.0 + options->size_sensitivity))
    size = options->size * (1.0 + options->size_sensitivity);
385

386 387
  if (size * SUBSAMPLE < 1.0)
    size = 1.0 / SUBSAMPLE;
388 389

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

Raph Levien's avatar
Raph Levien committed
391
  /* I'm not happy with the way the brush widget info is combined with
392 393 394 395
   * tilt info from the brush. My personal feeling is that
   * representing both as affine transforms would make the most
   * sense. -RLL
   */
Raph Levien's avatar
Raph Levien committed
396

397
  tscale   = options->tilt_sensitivity * 10.0;
398 399 400
  tscale_c = tscale * cos (gimp_deg_to_rad (options->tilt_angle));
  tscale_s = tscale * sin (gimp_deg_to_rad (options->tilt_angle));

401
  x = (options->blob_aspect * cos (options->blob_angle) +
402
       xtilt * tscale_c - ytilt * tscale_s);
403
  y = (options->blob_aspect * sin (options->blob_angle) +
404 405
       ytilt * tscale_c + xtilt * tscale_s);

Raph Levien's avatar
Raph Levien committed
406
#ifdef VERBOSE
407 408 409
  g_printerr ("angle %g aspect %g; %g %g; %g %g\n",
              options->blob_angle, options->blob_aspect,
              tscale_c, tscale_s, x, y);
Raph Levien's avatar
Raph Levien committed
410
#endif
411

Sven Neumann's avatar
Sven Neumann committed
412
  aspect = sqrt (SQR (x) + SQR (y));
413 414 415

  if (aspect != 0)
    {
416 417
      tcos = x / aspect;
      tsin = y / aspect;
418 419 420
    }
  else
    {
421 422 423 424 425 426 427 428 429 430 431
      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:
432
      blob_function = blob_ellipse;
433 434 435
      break;

    case GIMP_INK_BLOB_TYPE_SQUARE:
436
      blob_function = blob_square;
437 438 439
      break;

    case GIMP_INK_BLOB_TYPE_DIAMOND:
440 441 442 443 444
      blob_function = blob_diamond;
      break;

    default:
      g_return_val_if_reached (NULL);
445
      break;
446
    }
447

448 449 450 451 452 453
  return (* blob_function) (x_center * SUBSAMPLE,
                            y_center * SUBSAMPLE,
                            radmin * aspect * tcos,
                            radmin * aspect * tsin,
                            -radmin * tsin,
                            radmin * tcos);
454 455
}

456

457 458 459
/*********************************/
/*  Rendering functions          */
/*********************************/
460

461
/* Some of this stuff should probably be combined with the
462 463 464 465 466 467 468 469
 * 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.
 */

Sven Neumann's avatar
Sven Neumann committed
470 471 472 473 474
enum
{
  ROW_START,
  ROW_STOP
};
475 476 477 478

/* The insertion sort here, for SUBSAMPLE = 8, tends to beat out
 * qsort() by 4x with CFLAGS=-O2, 2x with CFLAGS=-g
 */
479 480
static void
insert_sort (gint *data,
481
             gint  n)
482
{
483
  gint i, j, k;
484

485
  for (i = 2; i < 2 * n; i += 2)
486
    {
Sven Neumann's avatar
Sven Neumann committed
487 488 489
      gint tmp1 = data[i];
      gint tmp2 = data[i + 1];

490
      j = 0;
Sven Neumann's avatar
Sven Neumann committed
491

492
      while (data[j] < tmp1)
493
        j += 2;
494

495
      for (k = i; k > j; k -= 2)
496 497 498 499
        {
          data[k]     = data[k - 2];
          data[k + 1] = data[k - 1];
        }
500

501 502
      data[j]     = tmp1;
      data[j + 1] = tmp2;
503 504 505 506 507
    }
}

static void
fill_run (guchar *dest,
508 509
          guchar  alpha,
          gint    w)
510 511 512 513 514 515 516 517
{
  if (alpha == 255)
    {
      memset (dest, 255, w);
    }
  else
    {
      while (w--)
518 519 520 521
        {
          *dest = MAX (*dest, alpha);
          dest++;
        }
522 523 524 525
    }
}

static void
526
render_blob_line (Blob   *blob,
527 528 529 530
                  guchar *dest,
                  gint    x,
                  gint    y,
                  gint    width)
531
{
532 533 534
  gint  buf[4 * SUBSAMPLE];
  gint *data    = buf;
  gint  n       = 0;
535 536
  gint  i, j;
  gint  current = 0;  /* number of filled rows at this point
537 538
                       * in the scan line
                       */
539
  gint last_x;
540 541

  /* Sort start and ends for all lines */
542

543
  j = y * SUBSAMPLE - blob->y;
544
  for (i = 0; i < SUBSAMPLE; i++)
545 546
    {
      if (j >= blob->height)
547
        break;
548

549
      if ((j > 0) && (blob->data[j].left <= blob->data[j].right))
550 551 552 553 554 555 556
        {
          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;
          n++;
        }
557 558 559 560 561 562
      j++;
    }

  /*   If we have less than SUBSAMPLE rows, compress */
  if (n < SUBSAMPLE)
    {
563
      for (i = 0; i < 2 * n; i++)
564
        data[2 * n + i] = data[2 * SUBSAMPLE + i];
565 566 567 568 569 570 571 572 573 574 575 576
    }

  /*   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)
577
        current++;
578
      else
579
        current--;
580 581 582 583 584 585
      data += 2;
      n--;
    }

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

587 588 589
  /* Render the row */

  last_x = 0;
590
  for (i = 0; i < n;)
591
    {
592
      gint cur_x = data[2 * i] / SUBSAMPLE - x;
593
      gint pixel;
594 595 596

      /* Fill in portion leading up to this pixel */
      if (current && cur_x != last_x)
597
        fill_run (dest + last_x, (255 * current) / SUBSAMPLE, cur_x - last_x);
598 599

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

      while (i<n)
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
        {
          gint tmp_x = data[2 * i] / SUBSAMPLE;

          if (tmp_x - x != cur_x)
            break;

          if (data[2 * i + 1] == ROW_START)
            {
              current++;
              pixel += ((tmp_x + 1) * SUBSAMPLE) - data[2 * i];
            }
          else
            {
              current--;
              pixel -= ((tmp_x + 1) * SUBSAMPLE) - data[2 * i];
            }

          i++;
        }
622

623
      dest[cur_x] = MAX (dest[cur_x], (pixel * 255) / (SUBSAMPLE * SUBSAMPLE));
624 625 626 627 628

      last_x = cur_x + 1;
    }

  if (current != 0)
629
    fill_run (dest + last_x, (255 * current)/ SUBSAMPLE, width - last_x);
630 631 632
}

static void
633 634
render_blob (Blob        *blob,
             PixelRegion *dest)
635
{
636
  gpointer  pr;
637

638 639
  for (pr = pixel_regions_register (1, dest);
       pr != NULL;
640 641
       pr = pixel_regions_process (pr))
    {
Sven Neumann's avatar
Sven Neumann committed
642 643 644
      guchar *d = dest->data;
      gint    h = dest->h;
      gint    y;
645

Sven Neumann's avatar
Sven Neumann committed
646
      for (y = 0; y < h; y++, d += dest->rowstride)
647
        {
Sven Neumann's avatar
Sven Neumann committed
648
          render_blob_line (blob, d, dest->x, dest->y + y, dest->w);
649
        }
650 651
    }
}