gimpink.c 21.4 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 <glib-object.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 32 33
#include "base/pixel-region.h"
#include "base/temp-buf.h"
#include "base/tile.h"
#include "base/tile-manager.h"

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

36
#include "core/gimpdrawable.h"
37
#include "core/gimpimage.h"
38
#include "core/gimpimage-undo.h"
39

40
#include "gimpinkoptions.h"
41 42
#include "gimpink.h"
#include "gimpink-blob.h"
43
#include "gimpinkundo.h"
44

45
#include "gimp-intl.h"
46

47

48
#define SUBSAMPLE 8
49

50

51
/*  local function prototypes  */
52

53 54 55 56 57 58 59 60 61 62
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);
63 64 65
static GimpUndo* gimp_ink_push_undo      (GimpPaintCore    *core,
                                          GimpImage        *image,
                                          const gchar      *undo_desc);
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

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      time_smoother_add       (GimpInk          *ink,
                                          guint32           value);
82
static guint32   time_smoother_result    (GimpInk          *ink);
83 84
static void      time_smoother_init      (GimpInk          *ink,
                                          guint32           initval);
85

86 87 88 89 90 91 92 93
static void      dist_smoother_add       (GimpInk          *ink,
                                          gdouble           value);
static gdouble   dist_smoother_result    (GimpInk          *ink);
static void      dist_smoother_init      (GimpInk          *ink,
                                          gdouble           initval);

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

95

96
G_DEFINE_TYPE (GimpInk, gimp_ink, GIMP_TYPE_PAINT_CORE)
Michael Natterer's avatar
Michael Natterer committed
97 98

#define parent_class gimp_ink_parent_class
99

100

101
void
102 103
gimp_ink_register (Gimp                      *gimp,
                   GimpPaintRegisterCallback  callback)
104
{
105 106
  (* callback) (gimp,
                GIMP_TYPE_INK,
107
                GIMP_TYPE_INK_OPTIONS,
108 109 110
                "gimp-ink",
                _("Ink"),
                "gimp-tool-ink");
111 112 113
}

static void
114
gimp_ink_class_init (GimpInkClass *klass)
115
{
116 117
  GObjectClass       *object_class     = G_OBJECT_CLASS (klass);
  GimpPaintCoreClass *paint_core_class = GIMP_PAINT_CORE_CLASS (klass);
118

Michael Natterer's avatar
Michael Natterer committed
119
  object_class->finalize           = gimp_ink_finalize;
120

121 122
  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
123
  paint_core_class->push_undo      = gimp_ink_push_undo;
124 125 126
}

static void
127
gimp_ink_init (GimpInk *ink)
128 129 130 131
{
}

static void
132
gimp_ink_finalize (GObject *object)
133
{
134
  GimpInk *ink = GIMP_INK (object);
135

Michael Natterer's avatar
Michael Natterer committed
136 137 138 139 140 141
  if (ink->start_blob)
    {
      g_free (ink->start_blob);
      ink->start_blob = NULL;
    }

142
  if (ink->last_blob)
143
    {
144 145
      g_free (ink->last_blob);
      ink->last_blob = NULL;
146
    }
147

148
  G_OBJECT_CLASS (parent_class)->finalize (object);
149 150
}

151
static void
152 153 154 155 156
gimp_ink_paint (GimpPaintCore    *paint_core,
                GimpDrawable     *drawable,
                GimpPaintOptions *paint_options,
                GimpPaintState    paint_state,
                guint32           time)
157
{
158
  GimpInk *ink = GIMP_INK (paint_core);
159 160

  switch (paint_state)
161
    {
162
    case GIMP_PAINT_STATE_INIT:
Michael Natterer's avatar
Michael Natterer committed
163
      if (paint_core->cur_coords.x == paint_core->last_coords.x &&
164 165
          paint_core->cur_coords.y == paint_core->last_coords.y)
        {
Michael Natterer's avatar
Michael Natterer committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
          /*  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);
188
        }
189
      break;
190

191
    case GIMP_PAINT_STATE_MOTION:
192
      gimp_ink_motion (paint_core, drawable, paint_options, time);
193
      break;
194

195
    case GIMP_PAINT_STATE_FINISH:
196 197
      break;
    }
198
}
jtl's avatar
jtl committed
199

200 201 202 203
static TempBuf *
gimp_ink_get_paint_area (GimpPaintCore    *paint_core,
                         GimpDrawable     *drawable,
                         GimpPaintOptions *paint_options)
204
{
205 206 207 208 209 210
  GimpInk *ink  = GIMP_INK (paint_core);
  gint     x, y;
  gint     width, height;
  gint     dwidth, dheight;
  gint     x1, y1, x2, y2;
  gint     bytes;
jtl's avatar
jtl committed
211

212
  bytes = gimp_drawable_bytes_with_alpha (drawable);
213

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

216 217
  dwidth  = gimp_item_width  (GIMP_ITEM (drawable));
  dheight = gimp_item_height (GIMP_ITEM (drawable));
218

219 220 221 222
  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);
223

224 225 226 227 228 229 230
  /*  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
231

232
  return paint_core->canvas_buf;
233
}
234

235 236 237 238 239 240 241 242 243 244 245 246
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);
}

247
static void
248 249 250 251
gimp_ink_motion (GimpPaintCore    *paint_core,
                 GimpDrawable     *drawable,
                 GimpPaintOptions *paint_options,
                 guint32           time)
252
{
253 254 255
  GimpInk        *ink     = GIMP_INK (paint_core);
  GimpInkOptions *options = GIMP_INK_OPTIONS (paint_options);
  GimpContext    *context = GIMP_CONTEXT (paint_options);
256
  GimpImage      *image;
257 258
  Blob           *blob_union = NULL;
  Blob           *blob_to_render;
259 260 261
  TempBuf        *area;
  guchar          col[MAX_CHANNELS];
  PixelRegion     blob_maskPR;
262

263
  image = gimp_item_get_image (GIMP_ITEM (drawable));
264

265 266 267 268 269 270 271 272 273
  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,
                                        10.0);
274

Michael Natterer's avatar
Michael Natterer committed
275 276 277 278 279
      if (ink->start_blob)
        g_free (ink->start_blob);

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

280 281
      time_smoother_init (ink, time);
      ink->last_time = time;
282

283 284
      dist_smoother_init (ink, 0.0);
      ink->init_velocity = TRUE;
285

286
      blob_to_render = ink->last_blob;
287 288 289
    }
  else
    {
290 291 292
      Blob    *blob;
      gdouble  dist;
      gdouble  velocity;
293 294
      guint32  lasttime = ink->last_time;
      guint32  thistime;
295 296 297 298 299 300 301 302 303 304 305 306 307 308

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

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

Michael Natterer's avatar
Michael Natterer committed
309 310 311 312
      dist = sqrt ((paint_core->last_coords.x - paint_core->cur_coords.x) *
                   (paint_core->last_coords.x - paint_core->cur_coords.x) +
                   (paint_core->last_coords.y - paint_core->cur_coords.y) *
                   (paint_core->last_coords.y - paint_core->cur_coords.y));
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337

      if (ink->init_velocity)
        {
          dist_smoother_init (ink, dist);
          ink->init_velocity = FALSE;
        }
      else
        {
          dist_smoother_add (ink, dist);
          dist = dist_smoother_result (ink);
        }

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

      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,
                              velocity);

      blob_union = blob_convex_union (ink->last_blob, blob);
      g_free (ink->last_blob);
      ink->last_blob = blob;
338

339 340
      blob_to_render = blob_union;
    }
341

342
  /* Get the buffer */
Michael Natterer's avatar
Michael Natterer committed
343
  ink->cur_blob = blob_to_render;
344
  area = gimp_paint_core_get_paint_area (paint_core, drawable, paint_options);
Michael Natterer's avatar
Michael Natterer committed
345 346
  ink->cur_blob = NULL;

347 348
  if (! area)
    return;
jtl's avatar
jtl committed
349

350 351
  gimp_image_get_foreground (image, context, gimp_drawable_type (drawable),
                             col);
352

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

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
  /*  color the pixels  */
  color_pixels (temp_buf_data (paint_core->canvas_buf), col,
                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);

374
  render_blob (blob_to_render, &blob_maskPR);
375 376 377 378 379 380 381 382 383 384 385 386 387 388

  /*  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);
389

390 391
  if (blob_union)
    g_free (blob_union);
392 393 394
}

static Blob *
395 396
ink_pen_ellipse (GimpInkOptions *options,
                 gdouble         x_center,
397 398 399 400 401
                 gdouble         y_center,
                 gdouble         pressure,
                 gdouble         xtilt,
                 gdouble         ytilt,
                 gdouble         velocity)
402
{
403
  BlobFunc blob_function;
404 405 406 407 408 409 410
  gdouble  size;
  gdouble  tsin, tcos;
  gdouble  aspect, radmin;
  gdouble  x,y;
  gdouble  tscale;
  gdouble  tscale_c;
  gdouble  tscale_s;
411

412 413
  /* Adjust the size depending on pressure. */

414
  size = options->size * (1.0 + options->size_sensitivity *
415
                          (2.0 * pressure - 1.0));
416

417 418 419 420
  /* Adjust the size further depending on pointer velocity and
   * velocity-sensitivity.  These 'magic constants' are 'feels
   * natural' tigert-approved. --ADM
   */
421 422 423 424 425

  if (velocity < 3.0)
    velocity = 3.0;

#ifdef VERBOSE
426
  g_printerr ("%g (%g) -> ", size, velocity);
427
#endif
428

429 430 431
  size = (options->vel_sensitivity *
          ((4.5 * size) / (1.0 + options->vel_sensitivity * (2.0 * velocity)))
          + (1.0 - options->vel_sensitivity) * size);
432 433

#ifdef VERBOSE
434
  g_printerr ("%g\n", (gfloat) size);
435 436 437 438
#endif

  /* Clamp resulting size to sane limits */

439 440
  if (size > options->size * (1.0 + options->size_sensitivity))
    size = options->size * (1.0 + options->size_sensitivity);
441

442 443
  if (size * SUBSAMPLE < 1.0)
    size = 1.0 / SUBSAMPLE;
444 445

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

Raph Levien's avatar
Raph Levien committed
447
  /* I'm not happy with the way the brush widget info is combined with
448 449 450 451
   * 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
452

453
  tscale   = options->tilt_sensitivity * 10.0;
454 455 456
  tscale_c = tscale * cos (gimp_deg_to_rad (options->tilt_angle));
  tscale_s = tscale * sin (gimp_deg_to_rad (options->tilt_angle));

457
  x = (options->blob_aspect * cos (options->blob_angle) +
458
       xtilt * tscale_c - ytilt * tscale_s);
459
  y = (options->blob_aspect * sin (options->blob_angle) +
460 461
       ytilt * tscale_c + xtilt * tscale_s);

Raph Levien's avatar
Raph Levien committed
462
#ifdef VERBOSE
463 464 465
  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
466
#endif
467

468
  aspect = sqrt (x * x + y * y);
469 470 471

  if (aspect != 0)
    {
472 473
      tcos = x / aspect;
      tsin = y / aspect;
474 475 476
    }
  else
    {
477 478 479 480 481 482 483 484 485 486 487
      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:
488
      blob_function = blob_ellipse;
489 490 491
      break;

    case GIMP_INK_BLOB_TYPE_SQUARE:
492
      blob_function = blob_square;
493 494 495
      break;

    case GIMP_INK_BLOB_TYPE_DIAMOND:
496 497 498 499 500
      blob_function = blob_diamond;
      break;

    default:
      g_return_val_if_reached (NULL);
501
      break;
502
    }
503

504 505 506 507 508 509
  return (* blob_function) (x_center * SUBSAMPLE,
                            y_center * SUBSAMPLE,
                            radmin * aspect * tcos,
                            radmin * aspect * tsin,
                            -radmin * tsin,
                            radmin * tcos);
510 511
}

512

513
static void
514 515
time_smoother_init (GimpInk *ink,
                    guint32  initval)
516
{
517
  gint i;
518

519
  ink->ts_index = 0;
520

521 522
  for (i = 0; i < TIME_SMOOTHER_BUFFER; i++)
    ink->ts_buffer[i] = initval;
523
}
524

525 526
static guint32
time_smoother_result (GimpInk *ink)
527 528
{
  gint    i;
529
  guint64 result = 0;
530

531 532
  for (i = 0; i < TIME_SMOOTHER_BUFFER; i++)
    result += ink->ts_buffer[i];
533

534
  return (result / (guint64) TIME_SMOOTHER_BUFFER);
535 536 537
}

static void
538 539
time_smoother_add (GimpInk *ink,
                   guint32  value)
540
{
541
  guint64 long_value = (guint64) value;
Michael Natterer's avatar
Michael Natterer committed
542

543 544 545 546 547 548 549 550 551 552
  /*  handle wrap-around of time values  */
  if (long_value < ink->ts_buffer[ink->ts_index])
    long_value += (guint64) + G_MAXUINT32;

  ink->ts_buffer[ink->ts_index++] = long_value;

  ink->ts_buffer[ink->ts_index++] = value;

  if (ink->ts_index == TIME_SMOOTHER_BUFFER)
    ink->ts_index = 0;
553
}
554

555

556
static void
557 558
dist_smoother_init (GimpInk *ink,
                    gdouble  initval)
559 560
{
  gint i;
561

562
  ink->dt_index = 0;
563

564 565
  for (i = 0; i < DIST_SMOOTHER_BUFFER; i++)
    ink->dt_buffer[i] = initval;
566 567 568
}

static gdouble
569
dist_smoother_result (GimpInk *ink)
570 571
{
  gint    i;
572
  gdouble result = 0.0;
573

574 575
  for (i = 0; i < DIST_SMOOTHER_BUFFER; i++)
    result += ink->dt_buffer[i];
576

577
  return (result / (gdouble) DIST_SMOOTHER_BUFFER);
578
}
579 580

static void
581 582
dist_smoother_add (GimpInk *ink,
                   gdouble  value)
583
{
584
  ink->dt_buffer[ink->dt_index++] = value;
585

586 587
  if (ink->dt_index == DIST_SMOOTHER_BUFFER)
    ink->dt_index = 0;
588 589 590
}


591 592 593
/*********************************/
/*  Rendering functions          */
/*********************************/
594

595
/* Some of this stuff should probably be combined with the
596 597 598 599 600 601 602 603 604 605 606 607 608
 * 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.
 */

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
 */
609 610
static void
insert_sort (gint *data,
611
             gint  n)
612
{
613 614
  gint i, j, k;
  gint tmp1, tmp2;
615

616
  for (i = 2; i < 2 * n; i += 2)
617 618
    {
      tmp1 = data[i];
619
      tmp2 = data[i + 1];
620 621
      j = 0;
      while (data[j] < tmp1)
622
        j += 2;
623

624
      for (k = i; k > j; k -= 2)
625 626 627 628
        {
          data[k]     = data[k - 2];
          data[k + 1] = data[k - 1];
        }
629

630 631
      data[j]     = tmp1;
      data[j + 1] = tmp2;
632 633 634 635 636
    }
}

static void
fill_run (guchar *dest,
637 638
          guchar  alpha,
          gint    w)
639 640 641 642 643 644 645 646
{
  if (alpha == 255)
    {
      memset (dest, 255, w);
    }
  else
    {
      while (w--)
647 648 649 650
        {
          *dest = MAX (*dest, alpha);
          dest++;
        }
651 652 653 654
    }
}

static void
655
render_blob_line (Blob   *blob,
656 657 658 659
                  guchar *dest,
                  gint    x,
                  gint    y,
                  gint    width)
660
{
661 662 663
  gint  buf[4 * SUBSAMPLE];
  gint *data    = buf;
  gint  n       = 0;
664 665
  gint  i, j;
  gint  current = 0;  /* number of filled rows at this point
666 667
                       * in the scan line
                       */
668
  gint last_x;
669 670

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

672
  j = y * SUBSAMPLE - blob->y;
673
  for (i = 0; i < SUBSAMPLE; i++)
674 675
    {
      if (j >= blob->height)
676
        break;
677

678
      if ((j > 0) && (blob->data[j].left <= blob->data[j].right))
679 680 681 682 683 684 685
        {
          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++;
        }
686 687 688 689 690 691
      j++;
    }

  /*   If we have less than SUBSAMPLE rows, compress */
  if (n < SUBSAMPLE)
    {
692
      for (i = 0; i < 2 * n; i++)
693
        data[2 * n + i] = data[2 * SUBSAMPLE + i];
694 695 696 697 698 699 700 701 702 703 704 705
    }

  /*   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)
706
        current++;
707
      else
708
        current--;
709 710 711 712 713 714
      data += 2;
      n--;
    }

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

716 717 718
  /* Render the row */

  last_x = 0;
719
  for (i = 0; i < n;)
720
    {
721
      gint cur_x = data[2 * i] / SUBSAMPLE - x;
722
      gint pixel;
723 724 725

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

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

      while (i<n)
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
        {
          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++;
        }
751

752
      dest[cur_x] = MAX (dest[cur_x], (pixel * 255) / (SUBSAMPLE * SUBSAMPLE));
753 754 755 756 757

      last_x = cur_x + 1;
    }

  if (current != 0)
758
    fill_run (dest + last_x, (255 * current)/ SUBSAMPLE, width - last_x);
759 760 761
}

static void
762 763
render_blob (Blob        *blob,
             PixelRegion *dest)
764
{
765 766 767 768
  gint      i;
  gint      h;
  guchar   *s;
  gpointer  pr;
769

770 771
  for (pr = pixel_regions_register (1, dest);
       pr != NULL;
772 773 774 775 776 777
       pr = pixel_regions_process (pr))
    {
      h = dest->h;
      s = dest->data;

      for (i=0; i<h; i++)
778 779 780 781 782
        {
          render_blob_line (blob, s,
                            dest->x, dest->y + i, dest->w);
          s += dest->rowstride;
        }
783 784
    }
}