gimpink-blob.c 20.3 KB
Newer Older
1 2
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995-1999 Spencer Kimball and Peter Mattis
3
 *
4
 * gimpink-blob.c: routines for manipulating scan converted convex polygons.
5
 * Copyright 1998-1999, Owen Taylor <otaylor@gtk.org>
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10 11 12 13 14 15 16 17
 * (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
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 20
*/

Sven Neumann's avatar
Sven Neumann committed
21
#include "config.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

29
#include "gimpink-blob.h"
Sven Neumann's avatar
Sven Neumann committed
30 31


32 33 34 35 36 37 38 39
typedef enum
{
  EDGE_NONE  = 0,
  EDGE_LEFT  = 1 << 0,
  EDGE_RIGHT = 1 << 1
} EdgeType;


40
/*  local function prototypes  */
41

42 43 44 45 46 47
static GimpBlob * gimp_blob_new            (gint      y,
                                            gint      height);
static void       gimp_blob_fill           (GimpBlob *b,
                                            EdgeType *present);
static void       gimp_blob_make_convex    (GimpBlob *b,
                                            EdgeType *present);
48 49

#if 0
50 51 52 53 54 55 56 57
static void       gimp_blob_line_add_pixel (GimpBlob *b,
                                            gint      x,
                                            gint      y);
static void       gimp_blob_line           (GimpBlob *b,
                                            gint      x0,
                                            gint      y0,
                                            gint      x1,
                                            gint      y1);
58
#endif
59

60

61
/*  public functions  */
62

63 64
/* Return blob for the given (convex) polygon
 */
65 66 67
GimpBlob *
gimp_blob_polygon (GimpBlobPoint *points,
                   gint           n_points)
68
{
69
  GimpBlob *result;
70 71 72 73 74
  EdgeType *present;
  gint      i;
  gint      im1;
  gint      ip1;
  gint      ymin, ymax;
75

76 77
  ymax = points[0].y;
  ymin = points[0].y;
78

79
  for (i = 1; i < n_points; i++)
80
    {
81
      if (points[i].y > ymax)
82
        ymax = points[i].y;
83
      if (points[i].y < ymin)
84
        ymin = points[i].y;
85 86
    }

87
  result = gimp_blob_new (ymin, ymax - ymin + 1);
88
  present = g_new0 (EdgeType, result->height);
89

90
  im1 = n_points - 1;
91 92
  i = 0;
  ip1 = 1;
93

94
  for (; i < n_points ; i++)
95
    {
96 97
      gint sides = 0;
      gint j     = points[i].y - ymin;
98

99
      if (points[i].y < points[im1].y)
100
        sides |= EDGE_RIGHT;
101
      else if (points[i].y > points[im1].y)
102
        sides |= EDGE_LEFT;
103

104
      if (points[ip1].y < points[i].y)
105
        sides |= EDGE_RIGHT;
106
      else if (points[ip1].y > points[i].y)
107
        sides |= EDGE_LEFT;
108

109
      if (sides & EDGE_RIGHT)
110 111 112 113 114 115 116 117 118 119 120
        {
          if (present[j] & EDGE_RIGHT)
            {
              result->data[j].right = MAX (result->data[j].right, points[i].x);
            }
          else
            {
              present[j] |= EDGE_RIGHT;
              result->data[j].right = points[i].x;
            }
        }
121

122
      if (sides & EDGE_LEFT)
123 124 125 126 127 128 129 130 131 132 133
        {
          if (present[j] & EDGE_LEFT)
            {
              result->data[j].left = MIN (result->data[j].left, points[i].x);
            }
          else
            {
              present[j] |= EDGE_LEFT;
              result->data[j].left = points[i].x;
            }
        }
134 135 136

      im1 = i;
      ip1++;
137
      if (ip1 == n_points)
138
        ip1 = 0;
139
    }
140

141
  gimp_blob_fill (result, present);
142 143 144
  g_free (present);

  return result;
145 146
}

147 148 149
/* Scan convert a square specified by _offsets_ of major and minor
 * axes, and by center into a blob
 */
150 151 152 153 154 155 156
GimpBlob *
gimp_blob_square (gdouble xc,
                  gdouble yc,
                  gdouble xp,
                  gdouble yp,
                  gdouble xq,
                  gdouble yq)
157
{
158
  GimpBlobPoint points[4];
159

160
  /* Make sure we order points ccw */
161

162
  if (xp * yq - xq * yp < 0)
163 164 165 166
    {
      xq = -xq;
      yq = -yq;
    }
167

168 169 170 171 172 173 174 175
  points[0].x = xc + xp + xq;
  points[0].y = yc + yp + yq;
  points[1].x = xc + xp - xq;
  points[1].y = yc + yp - yq;
  points[2].x = xc - xp - xq;
  points[2].y = yc - yp - yq;
  points[3].x = xc - xp + xq;
  points[3].y = yc - yp + yq;
176

177
  return gimp_blob_polygon (points, 4);
178
}
179

180 181 182
/* Scan convert a diamond specified by _offsets_ of major and minor
 * axes, and by center into a blob
 */
183 184 185 186 187 188 189
GimpBlob *
gimp_blob_diamond (gdouble xc,
                   gdouble yc,
                   gdouble xp,
                   gdouble yp,
                   gdouble xq,
                   gdouble yq)
190
{
191
  GimpBlobPoint points[4];
192 193

  /* Make sure we order points ccw */
194

195
  if (xp * yq - xq * yp < 0)
196
    {
197 198
      xq = -xq;
      yq = -yq;
199 200
    }

201 202 203 204 205 206 207 208
  points[0].x = xc + xp;
  points[0].y = yc + yp;
  points[1].x = xc - xq;
  points[1].y = yc - yq;
  points[2].x = xc - xp;
  points[2].y = yc - yp;
  points[3].x = xc + xq;
  points[3].y = yc + yq;
209

210
  return gimp_blob_polygon (points, 4);
211 212 213
}


214
#define TABLE_SIZE 256
215

216
#define ELLIPSE_SHIFT 2
217 218 219 220
#define TABLE_SHIFT   12
#define TOTAL_SHIFT   (ELLIPSE_SHIFT + TABLE_SHIFT)

/*
221 222
 * The choose of this values limits the maximal image_size to
 * 16384 x 16384 pixels. The values will overflow as soon as
223
 * x or y > INT_MAX / (1 << (ELLIPSE_SHIFT + TABLE_SHIFT)) / SUBSAMPLE
224
 *
225 226 227 228
 * Alternatively the code could be change the code as follows:
 *
 *   xc_base = floor (xc)
 *   xc_shift = 0.5 + (xc - xc_base) * (1 << TOTAL_SHIFT);
229
 *
230 231 232 233
 *    gint x = xc_base + (xc_shift + c * xp_shift + s * xq_shift +
 *             (1 << (TOTAL_SHIFT - 1))) >> TOTAL_SHIFT;
 *
 * which would change the limit from the image to the ellipse size
234 235 236 237 238
 *
 * Update: this change was done, and now there apparently is a limit
 * on the ellipse size. I'm too lazy to fully understand what's going
 * on here and simply leave this comment here for
 * documentation. --Mitch
239
 */
240

241 242
static gboolean trig_initialized = FALSE;
static gint     trig_table[TABLE_SIZE];
243

244 245
/* Scan convert an ellipse specified by _offsets_ of major and
 * minor axes, and by center into a blob
246
 */
247 248 249 250 251 252 253
GimpBlob *
gimp_blob_ellipse (gdouble xc,
                   gdouble yc,
                   gdouble xp,
                   gdouble yp,
                   gdouble xq,
                   gdouble yq)
254
{
255
  GimpBlob *result;
256
  EdgeType *present;
257 258 259 260 261
  gint      i;
  gdouble   r1, r2;
  gint      maxy, miny;
  gint      step;
  gdouble   max_radius;
262

263 264 265
  gint      xc_shift, yc_shift;
  gint      xp_shift, yp_shift;
  gint      xq_shift, yq_shift;
266
  gint      xc_base, yc_base;
267

268
  if (! trig_initialized)
269
    {
270
      trig_initialized = TRUE;
271

272
      for (i = 0; i < 256; i++)
273
        trig_table[i] = 0.5 + sin (i * (G_PI / 128.0)) * (1 << TABLE_SHIFT);
274
    }
275

276
  /* Make sure we traverse ellipse in ccw direction */
277

278
  if (xp * yq - xq * yp < 0)
279
    {
280 281
      xq = -xq;
      yq = -yq;
282 283
    }

284 285
  /* Compute bounds as if we were drawing a rectangle */

286 287
  maxy = ceil  (yc + fabs (yp) + fabs (yq));
  miny = floor (yc - fabs (yp) - fabs (yq));
288

289
  result = gimp_blob_new (miny, maxy - miny + 1);
290
  present = g_new0 (EdgeType, result->height);
291

292 293 294
  xc_base = floor (xc);
  yc_base = floor (yc);

295 296 297
  /* Figure out a step that will draw most of the points */

  r1 = sqrt (xp * xp + yp * yp);
298
  r2 = sqrt (xq * xq + yq * yq);
299 300 301
  max_radius = MAX (r1, r2);
  step = TABLE_SIZE;

302
  while (step > 1 && (TABLE_SIZE / step < 4 * max_radius))
303 304 305
    step >>= 1;

  /* Fill in the edge points */
306

307 308
  xc_shift = 0.5 + (xc - xc_base) * (1 << TOTAL_SHIFT);
  yc_shift = 0.5 + (yc - yc_base) * (1 << TOTAL_SHIFT);
309 310 311 312
  xp_shift = 0.5 + xp * (1 << ELLIPSE_SHIFT);
  yp_shift = 0.5 + yp * (1 << ELLIPSE_SHIFT);
  xq_shift = 0.5 + xq * (1 << ELLIPSE_SHIFT);
  yq_shift = 0.5 + yq * (1 << ELLIPSE_SHIFT);
313

314
  for (i = 0 ; i < TABLE_SIZE ; i += step)
315
    {
316
      gint s = trig_table[i];
317 318
      gint c = trig_table[(TABLE_SIZE + TABLE_SIZE / 4 - i) % TABLE_SIZE];

319 320 321 322 323
      gint x = ((xc_shift + c * xp_shift + s * xq_shift +
                 (1 << (TOTAL_SHIFT - 1))) >> TOTAL_SHIFT) + xc_base;
      gint y = (((yc_shift + c * yp_shift + s * yq_shift +
                 (1 << (TOTAL_SHIFT - 1))) >> TOTAL_SHIFT)) + yc_base
                  - result->y;
324 325 326 327

      gint dydi = c * yq_shift  - s * yp_shift;

      if (dydi <= 0) /* left edge */
328 329 330 331 332 333 334 335 336 337 338
        {
          if (present[y] & EDGE_LEFT)
            {
              result->data[y].left = MIN (result->data[y].left, x);
            }
          else
            {
              present[y] |= EDGE_LEFT;
              result->data[y].left = x;
            }
        }
339

340
      if (dydi >= 0) /* right edge */
341 342 343 344 345 346 347 348 349 350 351
        {
          if (present[y] & EDGE_RIGHT)
            {
              result->data[y].right = MAX (result->data[y].right, x);
            }
          else
            {
              present[y] |= EDGE_RIGHT;
              result->data[y].right = x;
            }
        }
352 353
    }

354
  /* Now fill in missing points */
355

356
  gimp_blob_fill (result, present);
357
  g_free (present);
358

359
  return result;
360 361 362
}

void
363 364 365 366 367
gimp_blob_bounds (GimpBlob *b,
                  gint     *x,
                  gint     *y,
                  gint     *width,
                  gint     *height)
368
{
369 370
  gint i;
  gint x0, x1, y0, y1;
371 372

  i = 0;
373
  while (i < b->height && b->data[i].left > b->data[i].right)
374 375
    i++;

376
  if (i < b->height)
377 378 379 380
    {
      y0 = b->y + i;
      x0 = b->data[i].left;
      x1 = b->data[i].right + 1;
381 382

      while (i < b->height && b->data[i].left <= b->data[i].right)
383 384 385 386 387
        {
          x0 = MIN (b->data[i].left,      x0);
          x1 = MAX (b->data[i].right + 1, x1);
          i++;
        }
388

389 390 391 392 393 394 395 396 397 398 399 400 401 402
      y1 = b->y + i;
    }
  else
    {
      x0 = y0 = 0;
      x1 = y1 = 0;
    }

  *x = x0;
  *y = y0;
  *width = x1 - x0;
  *height = y1 - y0;
}

403 404 405
GimpBlob *
gimp_blob_convex_union (GimpBlob *b1,
                        GimpBlob *b2)
406
{
407
  GimpBlob *result;
408 409 410 411 412 413 414
  gint      y;
  gint      i, j;
  EdgeType *present;

  /* Create the storage for the result */

  y = MIN (b1->y, b2->y);
415
  result = gimp_blob_new (y, MAX (b1->y + b1->height, b2->y + b2->height)-y);
416 417 418 419 420 421 422 423 424 425 426

  if (result->height == 0)
    return result;

  present = g_new0 (EdgeType, result->height);

  /* Initialize spans from original objects */

  for (i = 0, j = b1->y-y; i < b1->height; i++, j++)
    {
      if (b1->data[i].right >= b1->data[i].left)
427 428 429 430 431
        {
          present[j] = EDGE_LEFT | EDGE_RIGHT;
          result->data[j].left  = b1->data[i].left;
          result->data[j].right = b1->data[i].right;
        }
432 433 434
    }

  for (i = 0, j = b2->y - y; i < b2->height; i++, j++)
435
    {
436
      if (b2->data[i].right >= b2->data[i].left)
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
        {
          if (present[j])
            {
              if (result->data[j].left > b2->data[i].left)
                result->data[j].left = b2->data[i].left;
              if (result->data[j].right < b2->data[i].right)
                result->data[j].right = b2->data[i].right;
            }
          else
            {
              present[j] = EDGE_LEFT | EDGE_RIGHT;
              result->data[j].left = b2->data[i].left;
              result->data[j].right = b2->data[i].right;
            }
        }
452 453
    }

454
  gimp_blob_make_convex (result, present);
455 456 457 458 459 460

  g_free (present);

  return result;
}

461 462
GimpBlob *
gimp_blob_duplicate (GimpBlob *b)
Michael Natterer's avatar
Michael Natterer committed
463 464 465
{
  g_return_val_if_fail (b != NULL, NULL);

466
  return g_memdup (b, sizeof (GimpBlob) +  sizeof (GimpBlobSpan) * (b->height - 1));
Michael Natterer's avatar
Michael Natterer committed
467 468
}

469 470
#if 0
void
471
gimp_blob_dump (GimpBlob *b)
472 473 474 475 476 477
{
  gint i,j;

  for (i = 0; i < b->height; i++)
    {
      for (j = 0; j < b->data[i].left; j++)
478
        putchar (' ');
479 480

      for (j = b->data[i].left; j <= b->data[i].right; j++)
481
        putchar ('*');
482 483 484 485 486 487 488 489 490

      putchar ('\n');
    }
}
#endif


/*  private functions  */

491 492 493
static GimpBlob *
gimp_blob_new (gint y,
               gint height)
494
{
495
  GimpBlob *result;
496

497
  result = g_malloc (sizeof (GimpBlob) +  sizeof (GimpBlobSpan) * (height - 1));
498 499 500 501 502 503 504 505

  result->y      = y;
  result->height = height;

  return result;
}

static void
506 507
gimp_blob_fill (GimpBlob *b,
                EdgeType *present)
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
{
  gint start;
  gint x1, x2, i1, i2;
  gint i;

  /* Mark empty lines at top and bottom as unused */

  start = 0;
  while (! present[start])
    {
      b->data[start].left  = 0;
      b->data[start].right = -1;
      start++;
    }

  if (present[start] != (EDGE_RIGHT | EDGE_LEFT))
    {
      if (present[start] == EDGE_RIGHT)
526
        b->data[start].left = b->data[start].right;
527
      else
528
        b->data[start].right = b->data[start].left;
529 530 531 532 533 534 535 536 537 538 539 540 541

      present[start] = EDGE_RIGHT | EDGE_LEFT;
    }

  for (i = b->height - 1; ! present[i]; i--)
    {
      b->data[i].left  = 0;
      b->data[i].right = -1;
    }

  if (present[i] != (EDGE_RIGHT | EDGE_LEFT))
    {
      if (present[i] == EDGE_RIGHT)
542
        b->data[i].left = b->data[i].right;
543
      else
544
        b->data[i].right = b->data[i].left;
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562

      present[i] = EDGE_RIGHT | EDGE_LEFT;
    }


  /* Restore missing edges  */

  /* We fill only interior regions of convex hull, as if we were
   * filling polygons. But since we draw ellipses with nearest points,
   * not interior points, maybe it would look better if we did the
   * same here. Probably not a big deal either way after anti-aliasing
   */

  /* left edge */
  for (i1 = start; i1 < b->height - 2; i1++)
    {
      /* Find empty gaps */
      if (! (present[i1 + 1] & EDGE_LEFT))
563 564 565 566 567 568 569 570 571 572
        {
          gint increment; /* fractional part */
          gint denom;     /* denominator of fraction */
          gint step;      /* integral step */
          gint frac;      /* fractional step */
          gint reverse;

          /* find bottom of gap */
          i2 = i1 + 2;
          while (i2 < b->height && ! (present[i2] & EDGE_LEFT))
573 574
            i2++;

575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
          if (i2 < b->height)
            {
              denom = i2 - i1;
              x1 = b->data[i1].left;
              x2 = b->data[i2].left;
              step = (x2 - x1) / denom;
              frac = x2 - x1 - step * denom;
              if (frac < 0)
                {
                  frac = -frac;
                  reverse = 1;
                }
              else
                reverse = 0;

              increment = 0;
              for (i = i1 + 1; i < i2; i++)
                {
                  x1 += step;
                  increment += frac;
                  if (increment >= denom)
                    {
                      increment -= denom;
                      x1 += reverse ? -1 : 1;
                    }
                  if (increment == 0 || reverse)
                    b->data[i].left = x1;
                  else
                    b->data[i].left = x1 + 1;
                }
            }

          i1 = i2 - 1;        /* advance to next possibility */
        }
609 610 611 612 613 614 615
    }

  /* right edge */
  for (i1 = start; i1 < b->height - 2; i1++)
    {
      /* Find empty gaps */
      if (! (present[i1 + 1] & EDGE_RIGHT))
616 617 618 619 620 621 622 623 624 625
        {
          gint increment; /* fractional part */
          gint denom;     /* denominator of fraction */
          gint step;      /* integral step */
          gint frac;      /* fractional step */
          gint reverse;

          /* find bottom of gap */
          i2 = i1 + 2;
          while (i2 < b->height && ! (present[i2] & EDGE_RIGHT))
626 627
            i2++;

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
          if (i2 < b->height)
            {
              denom = i2 - i1;
              x1 = b->data[i1].right;
              x2 = b->data[i2].right;
              step = (x2 - x1) / denom;
              frac = x2 - x1 - step * denom;
              if (frac < 0)
                {
                  frac = -frac;
                  reverse = 1;
                }
              else
                reverse = 0;

              increment = 0;
              for (i = i1 + 1; i<i2; i++)
                {
                  x1 += step;
                  increment += frac;
                  if (increment >= denom)
                    {
                      increment -= denom;
                      x1 += reverse ? -1 : 1;
                    }
                  if (reverse && increment != 0)
                    b->data[i].right = x1 - 1;
                  else
                    b->data[i].right = x1;
                }
            }

          i1 = i2 - 1;        /* advance to next possibility */
        }
662 663 664 665 666
    }

}

static void
667 668
gimp_blob_make_convex (GimpBlob *b,
                       EdgeType *present)
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
{
  gint x1, x2, y1, y2, i1, i2;
  gint i;
  gint start;

  /* Walk through edges, deleting points that aren't on convex hull */

  start = 0;
  while (! present[start])
    start++;

  /*    left edge */

  i1 = start - 1;
  i2 = start;
  x1 = b->data[start].left - b->data[start].right;
  y1 = 0;

  for (i = start + 1; i < b->height; i++)
    {
      if (! (present[i] & EDGE_LEFT))
690
        continue;
691 692 693 694 695

      x2 = b->data[i].left - b->data[i2].left;
      y2 = i - i2;

      while (x2 * y1 - x1 * y2 < 0) /* clockwise rotation */
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
        {
          present[i2] &= ~EDGE_LEFT;
          i2 = i1;
          while ((--i1) >= start && (! (present[i1] & EDGE_LEFT)));

          if (i1 < start)
            {
              x1 = b->data[start].left - b->data[start].right;
              y1 = 0;
            }
          else
            {
              x1 = b->data[i2].left - b->data[i1].left;
              y1 = i2 - i1;
            }
          x2 = b->data[i].left - b->data[i2].left;
          y2 = i - i2;
        }
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730

      x1 = x2;
      y1 = y2;
      i1 = i2;
      i2 = i;
    }

  /*     Right edge */

  i1 = start -1;
  i2 = start;
  x1 = b->data[start].right - b->data[start].left;
  y1 = 0;

  for (i = start + 1; i < b->height; i++)
    {
      if (! (present[i] & EDGE_RIGHT))
731
        continue;
732 733 734 735 736

      x2 = b->data[i].right - b->data[i2].right;
      y2 = i - i2;

      while (x2 * y1 - x1 * y2 > 0) /* counter-clockwise rotation */
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
        {
          present[i2] &= ~EDGE_RIGHT;
          i2 = i1;
          while ((--i1) >= start && (! (present[i1] & EDGE_RIGHT)));

          if (i1 < start)
            {
              x1 = b->data[start].right - b->data[start].left;
              y1 = 0;
            }
          else
            {
              x1 = b->data[i2].right - b->data[i1].right;
              y1 = i2 - i1;
            }

          x2 = b->data[i].right - b->data[i2].right;
          y2 = i - i2;
        }
756 757 758 759 760 761 762

      x1 = x2;
      y1 = y2;
      i1 = i2;
      i2 = i;
    }

763
  gimp_blob_fill (b, present);
764 765
}

766 767 768

#if 0

769
static void
770 771 772
gimp_blob_line_add_pixel (GimpBlob *b,
                          gint      x,
                          gint      y)
773 774 775 776 777 778 779 780 781 782 783 784 785
{
  if (b->data[y - b->y].left > b->data[y - b->y].right)
    {
      b->data[y - b->y].left = b->data[y - b->y].right = x;
    }
  else
    {
      b->data[y - b->y].left  = MIN (b->data[y - b->y].left,  x);
      b->data[y - b->y].right = MAX (b->data[y - b->y].right, x);
    }
}

static void
786 787 788 789 790
gimp_blob_line (GimpBlob *b,
                gint      x0,
                gint      y0,
                gint      x1,
                gint      y1)
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
{
  gint dx, dy, d;
  gint incrE, incrNE;
  gint x, y;

  gint xstep = 1;
  gint ystep = 1;

  dx = x1 - x0;
  dy = y1 - y0;

  if (dx < 0)
    {
      dx = -dx;
      xstep = -1;
    }

  if (dy < 0)
    {
      dy = -dy;
      ystep = -1;
    }

  /*  for (y = y0; y != y1 + ystep ; y += ystep)
    {
      b->data[y-b->y].left = 0;
      b->data[y-b->y].right = -1;
      }*/

  x = x0;
  y = y0;

  if (dy < dx)
    {
      d = 2 * dy - dx;        /* initial value of d */
      incrE  = 2 * dy;        /* increment used for move to E */
      incrNE = 2 * (dy - dx); /* increment used for move to NE */

829
      gimp_blob_line_add_pixel (b, x, y);
830 831

      while (x != x1)
832 833 834 835 836 837 838 839 840 841 842 843 844
        {
          if (d <= 0)
            {
              d += incrE;
              x += xstep;
            }
          else
            {
              d += incrNE;
              x += xstep;
              y += ystep;
            }

845
          gimp_blob_line_add_pixel (b, x, y);
846
        }
847 848 849 850 851 852 853
    }
  else
    {
      d = 2 * dx - dy;        /* initial value of d */
      incrE  = 2 * dx;        /* increment used for move to E */
      incrNE = 2 * (dx - dy); /* increment used for move to NE */

854
      gimp_blob_line_add_pixel (b, x, y);
855 856

      while (y != y1)
857 858 859 860 861 862 863 864 865 866 867 868 869
        {
          if (d <= 0)
            {
              d += incrE;
              y += ystep;
            }
          else
            {
              d += incrNE;
              x += xstep;
              y += ystep;
            }

870
          gimp_blob_line_add_pixel (b, x, y);
871
        }
872 873
    }
}
874 875

#endif