File: blob.c

package info (click to toggle)
gsumi 0.8-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 344 kB
  • ctags: 421
  • sloc: ansic: 5,088; makefile: 58; sh: 17
file content (804 lines) | stat: -rw-r--r-- 17,221 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
/*
 * Routines for bitmap manipulation
 *
  gsumi version 0.5

  Copyright 1997 Owen Taylor <owt1@cornell.edu>

  Heavily based on

  xink version 0.02

  Copyright 1997 Raph Levien <raph@acm.org>

  This code is free for commercial and non-commercial use or
  redistribution, as long as the source code release, startup screen,
  or product packaging includes this copyright notice.
*/

#include "gsumi.h"
#include "rect.h"

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ROUND(A) floor(A+0.5)

static Blob *
blob_new (int y, int height)
{
  Blob *result;

  result = malloc (sizeof (Blob) +  sizeof(BlobSpan) * height);
  result->y = y;
  result->height = height;

  return result;
}

typedef enum {
  NONE = 0,
  LEFT = 1 << 0,
  RIGHT = 1 << 1,
} EdgeType;

Blob *
blob_convex_union (Blob *b1, Blob *b2)
{
  Blob *result;
  int y, x1, x2, y1, y2, i1, i2;
  int i, j;
  int start;
  EdgeType *present;

  /* Create the storage for the result */

  y = MIN(b1->y,b2->y);
  result = blob_new (y, MAX(b1->y+b1->height,b2->y+b2->height)-y);

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

  present = malloc (sizeof (EdgeType) * result->height);
  memset (present, 0, result->height * sizeof(EdgeType));

  /* 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)
	{
	  present[j] = LEFT | RIGHT;
	  result->data[j].left = b1->data[i].left;
	  result->data[j].right = b1->data[i].right;
	}
    }

  for (i=0, j=b2->y-y; i<b2->height; i++,j++)
    {
      if (b2->data[i].right >= b2->data[i].left)
	{
	  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] = LEFT | RIGHT;
	      result->data[j].left = b2->data[i].left;
	      result->data[j].right = b2->data[i].right;
	    }
	}
    }

  /* Now 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 = result->data[start].left - result->data[start].right;
  y1 = 0;
  
  for (i=start+1;i<result->height;i++)
    {
      if (!(present[i] & LEFT))
	continue;

      x2 = result->data[i].left - result->data[i2].left;
      y2 = i-i2;
      
      while (x2*y1 - x1*y2 < 0) /* clockwise rotation */
	{
	  present[i2] &= ~LEFT;
	  i2 = i1;
	  while (!(present[--i1] & LEFT) && i1>=start);

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

  /*     Right edge */

  i1 = start -1; 
  i2 = start;
  x1 = result->data[start].right - result->data[start].left;
  y1 = 0;
  
  for (i=start+1;i<result->height;i++)
    {
      if (!(present[i] & RIGHT))
	continue;

      x2 = result->data[i].right - result->data[i2].right;
      y2 = i-i2;
      
      while (x2*y1 - x1*y2 > 0) /* counter-clockwise rotation */
	{
	  present[i2] &= ~RIGHT;
	  i2 = i1;
	  while (!(present[--i1] & RIGHT) && i1>=start);

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

  /* Restore edges of spans that were deleted in last step or never present */

  /* 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<result->height-1; i1++)
    {
      /* Find empty gaps */
      if (!(present[i1+1] & LEFT))
	{
	  int increment;	/* fractional part */
	  int denom;		/* denominator of fraction */
	  int step;		/* integral step */
	  int frac;		/* fractional step */
	  int reverse;

	  /* find bottom of gap */
	  i2 = i1+2;
	  while (!(present[i2] & LEFT) && i2 < result->height) i2++;
	  
	  if (i2 < result->height)
	    {
	      denom = i2-i1;
	      x1 = result->data[i1].left;
	      x2 = result->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)
		    result->data[i].left = x1;
		  else
		    result->data[i].left = x1 + 1;
		}
	    }
	  i1 = i2-1;		/* advance to next possibility */
	}
    }

  /*     right edge */
  for (i1=start; i1<result->height-1; i1++)
    {
      /* Find empty gaps */
      if (!(present[i1+1] & RIGHT))
	{
	  int increment;	/* fractional part */
	  int denom;		/* denominator of fraction */
	  int step;		/* integral step */
	  int frac;		/* fractional step */
	  int reverse;

	  /* find bottom of gap */
	  i2 = i1+2;
	  while (!(present[i2] & RIGHT) && i2 < result->height) i2++;
	  
	  if (i2 < result->height)
	    {
	      denom = i2-i1;
	      x1 = result->data[i1].right;
	      x2 = result->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)
		    result->data[i].right = x1 - 1;
		  else
		    result->data[i].right = x1;
		}
	    }
	  i1 = i2-1;		/* advance to next possibility */
	}
    }
  
  /* Mark empty lines at top and bottom as unused */
  for (i=0;i<start;i++)
    {
      result->data[i].left = 0;
      result->data[i].right = -1;
    }
  for (i=result->height-1;!present[i];i--)
    {
      result->data[i].left = 0;
      result->data[i].right = -1;
    }

  free (present);
  return result;
}

/****************************************************************
 * Code to scan convert an arbitrary ellipse into a Blob. Based
 * on Van Aken's conic algorithm in Foley and Van Damn 
 ****************************************************************/

/* Return octant from gradient */
static int
blob_get_octant (int D, int E)
{
  if (D>=0)
    {
    if (E<0)
      return (D<-E) ? 1 : 2;
    else
      return (D>E) ? 3 : 4;
    }
  else
    if (E>0)
      return (-D<E) ? 5 : 6;
    else
      return (-D>-E) ? 7 : 8;
}

static void
blob_conic_add_pixel (Blob *b, EdgeType *present, int x, int y, int octant)
{
  /*  printf ("%d %d\n",x,y); */
  if (y<b->y || y>=b->y+b->height)
    {
      /*      g_warning("Out of bounds!\n"); */
    }
  else
    {
      if (octant <= 4)
	{
	  if (present[y-b->y] & RIGHT)
	    b->data[y-b->y].right = MAX(b->data[y-b->y].right,x);
	  else
	    {
	      b->data[y-b->y].right = x;
	      present[y-b->y] |= RIGHT;
	    }
	}
      else
	{
	  if (present[y-b->y] & LEFT)
	    b->data[y-b->y].left = MIN(b->data[y-b->y].left,x);
	  else
	    {
	      b->data[y-b->y].left = x;
	      present[y-b->y] |= LEFT;
	    }
	}
    }
}

static void
blob_conic (Blob *b, int xs, int ys,
	    int A, int B, int C, int D, int E, int F)
{
  int x,y;			/* current point */
  int octant;			/* current octant */
  int dxsquare, dysquare;	/* change in (x,y) for square moves */
  int dxdiag, dydiag;		/* change in (x,y) for diagonal moves */
  int d,u,v,k1,k2,k3;		/* decision variables and increments */
  int octantCount;		/* number of octants to be drawn */
  int count;			/* number of steps for last octant */
  int fx,dfx;			/* check x-boundaries */
  int fy,dfy;			/* check y-boundaries */
  int ddf;			/* second difference for boundary-checking */
  int tmp, i;

  EdgeType *present;
  
  present = malloc (sizeof (EdgeType) * b->height);
  memset (present, 0, b->height * sizeof(EdgeType));

  octant = blob_get_octant (D,E);

  switch (octant)
    {
    case 1:
      d = ROUND (A+B/2.+C/4.+D+E/2.+F);
      u = ROUND (A+B/2.+D);
      v = ROUND (A+B/2.+D+E);
      k1 = 2*A;
      k2 = 2*A + B;
      k3 = k2 + B + 2*C;
      dxsquare = 1;
      dysquare = 0;
      dxdiag = 1;
      dydiag = 1;
      break;
    case 2:
      d = ROUND (A/4.+B/2.+C+D/2.+E+F);
      u = ROUND (B/2.+C+E);
      v = ROUND (B/2.+C+D+E);
      k1 = 2*C;
      k2 = B + 2*C;
      k3 = 2*A + 2*B + 2*C;
      dxsquare = 0;
      dysquare = 1;
      dxdiag = 1;
      dydiag = 1;
      break;
    case 3:
      d = ROUND (A/4.-B/2.+C-D/2.+E+F);
      u = ROUND (-B/2.+C+E);
      v = ROUND (-B/2.+C-D+E);
      k1 = 2*C;
      k2 = 2*C - B;
      k3 = 2*A - 2*B + 2*C;
      dxsquare = 0;
      dysquare = 1;
      dxdiag = -1;
      dydiag = 1;
      break;
    case 4:
      d = ROUND (A-B/2.+C/4.-D+E/2.+F);
      u = ROUND (A-B/2.-D);
      v = ROUND (A-B/2.-D+E);
      k1 = 2*A;
      k2 = 2*A - B;
      k3 = k2 - B + 2*C;
      dxsquare = -1;
      dysquare = 0;
      dxdiag = -1;
      dydiag = 1;
      break;
    case 5:
      d = ROUND (A+B/2.+C/4.-D-E/2.+F);
      u = ROUND (A+B/2.-D);
      v = ROUND (A+B/2.-D-E);
      k1 = 2*A;
      k2 = 2*A + B;
      k3 = k2 + B + 2*C;
      dxsquare = -1;
      dysquare = 0;
      dxdiag = -1;
      dydiag = -1;
      break;
    case 6:
      d = ROUND (A/4.+B/2.+C-D/2.-E+F);
      u = ROUND (B/2.+C-E);
      v = ROUND (B/2.+C-D-E);
      k1 = 2*C;
      k2 = B + 2*C;
      k3 = 2*A + 2*B + 2*C;
      dxsquare = 0;
      dysquare = -1;
      dxdiag = -1;
      dydiag = -1;
      break;
    case 7:
      d = ROUND (A/4.-B/2.+C+D/2.-E+F);
      u = ROUND (-B/2.+C-E);
      v = ROUND (-B/2.+C+D-E);
      k1 = 2*C;
      k2 = 2*C - B;
      k3 = 2*A - 2*B + 2*C;
      dxsquare = 0;
      dysquare = -1;
      dxdiag = 1;
      dydiag = -1;
      break;
    default:			/* case 8: */
      d = ROUND (A-B/2.+C/4.+D-E/2.+F);
      u = ROUND (A-B/2.+D);
      v = ROUND (A-B/2.+D-E);
      k1 = 2*A;
      k2 = 2*A - B;
      k3 = k2 - B + 2*C;
      dxsquare = 1;
      dysquare = 0;
      dxdiag = 1;
      dydiag = -1;
      break;
    }

  octantCount = 8;
  x = xs;
  y = ys;
  count = 0;			/* ignore until last octant */

  /* Initialize boundary checking - we keep track of the discriminants
     for the conic as quadratics in x and y, and when they go negative
     we know we are beyond the boundaries of the conic. */

  ddf = 2*(B*B - 4*A*C);
  fx = E*E - 4*C*F;
  dfx = ddf/2 + dxdiag * (2*B*E - 4*C*D);
  fy = D*D - 4*A*F;
  dfy = ddf/2 + dydiag * (2*B*D - 4*A*E);

  while (1)
    {
      if (octantCount == 0)
	{
	  /* figure out remaining steps in square direction */
	  switch (octant)
	    {
	    case 1:
	    case 8:
	      count = xs - x;
	      break;
	    case 2:
	    case 3:
	      count = ys - y;
	      break;
	    case 4:
	    case 5:
	      count = x - xs;
	      break;
	    case 6:
	    case 7:
	      count = y - ys;
	      break;
	    }
	  /*	  if (count < 0)
	    g_warning("Negative count (%d) in octant %d\n",count,octant); */
	  if (count <= 0)
	    goto done;
	     
	}
      if (octant %2)	/* odd octants */
	{
	  while (v < k2/2)
	    {
	      blob_conic_add_pixel (b, present, x, y, octant);
	      if (d<0)
		{
		  x += dxsquare; y += dysquare;
		  u += k1;
		  v += k2;
		  d += u;
		  if (dxsquare)
		    {
		      fx += dfx;
		      dfx += ddf;
		    }
		  if (dysquare)
		    {
		      fy += dfy;
		      dfy += ddf;
		    }
		}
	      else
		{
		  x += dxdiag; y += dydiag;
		  u += k2;
		  v += k3;
		  d += v;
		  fx += dfx;
		  dfx += ddf;
		  fy += dfy;
		  dfy += ddf;
#ifdef NOHOP
		  /* Check for octant hopping */
		  if (u < v && fx >= 0 && fy >= 0)
		    {
		      /* hopped diag across ellipse, take square instead */
		      x += dxsquare - dxdiag;
		      y += dysquare - dydiag;
		      d -= v;
		      u += k1 - k2;
		      v += k2 - k3;
		      d += u;
		      dfx -= ddf;
		      fx -= dfx;
		      dfy -= ddf;
		      fy -= dfy;
		      if (dxsquare)
			{
			  fx += dfx;
			  dfx += ddf;
			}
		      if (dysquare)
			{
			  fy += dfy;
			  dfy += ddf;
			}
		    }
#endif
		}
	      if (count && --count == 0)
		goto done;
	    }
	  /* We now cross diagonal octant boundary */
	  d = ROUND (d - u + v/2. - k2/2. + 3*k3/8.);
	  u = ROUND (-u + v - k2/2. + k3/2.);
	  v = ROUND (v - k2 + k3/2.); /* could be v + A - C */
	  k1 = k1 - 2*k2 + k3;
	  k2 = k3 - k2;
	  tmp = dxsquare;
	  dxsquare = -dysquare;
	  dysquare = tmp;
	}
      else			/* Even octants */
	{
	  while (u < k2 /2)
	    {
	      blob_conic_add_pixel (b, present, x, y, octant);
	      if (d<0)
		{
		  x += dxdiag; y += dydiag;
		  u += k2;
		  v += k3;
		  d += v;
		  fx += dfx;
		  dfx += ddf;
		  fy += dfy;
		  dfy += ddf;
		}
	      else
		{
		  x += dxsquare; y += dysquare;
		  u += k1;
		  v += k2;
		  d += u;
		  if (dxsquare)
		    {
		      fx += dfx;
		      dfx += ddf;
		    }
		  if (dysquare)
		    {
		      fy += dfy;
		      dfy += ddf;
		    }
#ifdef NOHOP
		  /* Check for octant hopping */
		   if ((fx >= 0) && (fy >= 0) &&
		       ((((octant == 2) || (octant == 6)) &&
			 2*u > ROUND(v + B/2 + C)) ||
			(((octant == 4) || (octant == 8)) &&
			 2*u > ROUND(v - B/2 + A))))
		       {
			 /* hopped square across ellipse, take diag instead */
			 x += dxdiag - dxsquare;
			 y += dydiag - dysquare;
			 d -= u;
			 u += k2 - k1;
			 v += k3 - k2;
			 d += v;
			 if (dxsquare)
			   {
			     dfx -= ddf;
			     fx -= dfx;
			   }
			 if (dysquare)
			   {
			     dfy -= ddf;
			     fy -= dfy;
			   }
			 fx += dfx;
			 dfx += ddf;
			 fy += dfy;
			 dfy += ddf;
		       }
#endif
		}
	      if (count && --count <= 0)
		goto done;
	    }
	  /* We now cross square octant boundary */
	  d = d + u - v + k1 - k2;
	  v = 2*u - v + k1 - k2;
	  /* Do v first; it depends on u */
	  u = u + k1 - k2;
	  k3 = 4 * (k1 - k2) + k3;
	  k2 = 2 * k1 - k2;
	  tmp = dxdiag;
	  dxdiag = -dydiag;
	  dydiag = tmp;
	  /* Change appropriate boundary checking increment */
	  if (octant == 2 || octant == 6)
	    dfx = -dfx + ddf;
	  else
	    dfy = -dfy + ddf;
	}
      octant++;
      if (octant > 8) octant -= 8;
      octantCount--;
    }

done:				/* jump out of two levels */
  
  for (i=0; i<b->height; i++)
    if (present[i] != (LEFT | RIGHT))
      {
	b->data[i].left = 0;
	b->data[i].right = -1;
      }

  free (present);
}

/* Scan convert an ellipse specified by _offsets_ of major and
   minor axes, and by center into a blob */
Blob *
blob_ellipse (double xc, double yc, double xp, double yp, double xq, double yq)
{
  Blob *r;
  double A,B,C,D,E,F;		/* coefficients of conic */
  double xprod,tmp;
  double height;
  double dpx, dpy;
  int y;

  xprod = xp*yq - xq*yp;

  if (xprod == 0)		/* colinear points */
    {
      g_print("Colinear points!\n");
      g_debug("gsumi");
    }
  
  if (xprod < 0)
    {
      tmp = xp; xp = xq; xq = tmp;
      tmp = yp; yp = yq; yq = tmp;
      xprod = -xprod;
    }
  
  A = yp*yp + yq*yq;
  B = -2 * (xp*yp + xq*yq);
  C = xp*xp + xq*xq;
  D = 2*yq*xprod;
  E = -2*xq*xprod;
  F = 0;
  /* Now offset the ellipse so that the center is exact, but the
     starting point is no longer exactly on the ellipse */

  dpx = ROUND(xp+xc)-xp-xc;
  dpy = ROUND(yp+yc)-yp-yc;

  F += dpx*(A*dpx+D+B*dpy) + dpy*(C*dpy+E);
  D += 2*A*dpx + B*dpy;
  E += B*dpx + 2*C*dpy;

  height = sqrt(A);
  y = floor(yc-height-0.5);
  /* We allow an extra pixel of slop on top and bottom to deal with
     round-off error */
  r = blob_new (y-1,ceil(yc+height+0.5)-y+3);

  /* Although it seems that multiplying A-F by a constant would improve
     things, this seems not to be the case in practice. Several test
     cases showed about equal improvement and degradation */
  blob_conic (r,ROUND(xp+xc),ROUND(yp+yc),ROUND(16*A),ROUND(16*B),ROUND(16*C),
	      ROUND(16*D),ROUND(16*E),ROUND(16*F));

  return r;
}

void
blob_bounds(Blob *b, rect *r)
{
  int i;

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

  if (i<b->height)
    {
      r->y0 = b->y + i;
      r->x0 = b->data[i].left;
      r->x1 = b->data[i].right + 1;
      while (i<b->height && b->data[i].left <= b->data[i].right)
	{
	  r->x0 = MIN(b->data[i].left, r->x0);
	  r->x1 = MAX(b->data[i].right+1, r->x1);
	  i++;
	}
      r->y1 = b->y + i;
    }
  else
    {
      r->x0 = r->y0 = 0;
      r->x1 = r->y1 = 0;
    }
}

void
blob_dump(Blob *b) {
  int i,j;
  for (i=0; i<b->height; i++)
    {
      for (j=0;j<b->data[i].left;j++)
	putchar(' ');
      for (j=b->data[i].left;j<=b->data[i].right;j++)
	putchar('*');
      putchar('\n');
    }
}