]> git.imager.perl.org - imager.git/blob - draw.c
update skip count
[imager.git] / draw.c
1 #include "image.h"
2 #include "draw.h"
3 #include "log.h"
4
5 #include <limits.h>
6
7 void
8 i_mmarray_cr(i_mmarray *ar,int l) {
9   int i;
10
11   ar->lines=l;
12   ar->data=mymalloc(sizeof(minmax)*l);
13   for(i=0;i<l;i++) { ar->data[i].max=-1; ar->data[i].min=MAXINT; }
14 }
15
16 void
17 i_mmarray_dst(i_mmarray *ar) {
18   ar->lines=0;
19   if (ar->data != NULL) { myfree(ar->data); ar->data=NULL; }
20 }
21
22 void
23 i_mmarray_add(i_mmarray *ar,int x,int y) {
24   if (y>-1 && y<ar->lines)
25     {
26       if (x<ar->data[y].min) ar->data[y].min=x;
27       if (x>ar->data[y].max) ar->data[y].max=x;
28     }
29 }
30
31 int
32 i_mmarray_gmin(i_mmarray *ar,int y) {
33   if (y>-1 && y<ar->lines) return ar->data[y].min;
34   else return -1;
35 }
36
37 int
38 i_mmarray_getm(i_mmarray *ar,int y) {
39   if (y>-1 && y<ar->lines) return ar->data[y].max;
40   else return MAXINT;
41 }
42
43 void
44 i_mmarray_render(i_img *im,i_mmarray *ar,i_color *val) {
45   int i,x;
46   for(i=0;i<ar->lines;i++) if (ar->data[i].max!=-1) for(x=ar->data[i].min;x<ar->data[i].max;x++) i_ppix(im,x,i,val);
47 }
48
49 void
50 i_mmarray_render_fill(i_img *im,i_mmarray *ar,i_fill_t *fill) {
51   int x, w, y;
52   if (im->bits == i_8_bits && fill->fill_with_color) {
53     i_color *line = mymalloc(sizeof(i_color) * im->xsize);
54     i_color *work = NULL;
55     if (fill->combine)
56       work = mymalloc(sizeof(i_color) * im->xsize);
57     for(y=0;y<ar->lines;y++) {
58       if (ar->data[y].max!=-1) {
59         x = ar->data[y].min;
60         w = ar->data[y].max-ar->data[y].min;
61
62         if (fill->combine) {
63           i_glin(im, x, x+w, y, line);
64           (fill->fill_with_color)(fill, x, y, w, im->channels, work);
65           (fill->combine)(line, work, im->channels, w);
66         }
67         else {
68           (fill->fill_with_color)(fill, x, y, w, im->channels, line);
69         }
70         i_plin(im, x, x+w, y, line);
71       }
72     }
73   
74     myfree(line);
75     if (work)
76       myfree(work);
77   }
78   else {
79     i_fcolor *line = mymalloc(sizeof(i_fcolor) * im->xsize);
80     i_fcolor *work = NULL;
81     if (fill->combinef)
82       work = mymalloc(sizeof(i_fcolor) * im->xsize);
83     for(y=0;y<ar->lines;y++) {
84       if (ar->data[y].max!=-1) {
85         x = ar->data[y].min;
86         w = ar->data[y].max-ar->data[y].min;
87
88         if (fill->combinef) {
89           i_glinf(im, x, x+w, y, line);
90           (fill->fill_with_fcolor)(fill, x, y, w, im->channels, work);
91           (fill->combinef)(line, work, im->channels, w);
92         }
93         else {
94           (fill->fill_with_fcolor)(fill, x, y, w, im->channels, line);
95         }
96         i_plinf(im, x, x+w, y, line);
97       }
98     }
99   
100     myfree(line);
101     if (work)
102       myfree(work);
103   }
104 }
105
106
107 static
108 void
109 i_arcdraw(int x1, int y1, int x2, int y2, i_mmarray *ar) {
110   double alpha;
111   double dsec;
112   int temp;
113   alpha=(double)(y2-y1)/(double)(x2-x1);
114   if (fabs(alpha)<1) 
115     {
116       if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
117       dsec=y1;
118       while(x1<x2)
119         {
120           dsec+=alpha;
121           i_mmarray_add(ar,x1,(int)(dsec+0.5));
122           x1++;
123         }
124     }
125   else
126     {
127       alpha=1/alpha;
128       if (y2<y1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
129       dsec=x1;
130       while(y1<y2)
131         {
132           dsec+=alpha;
133           i_mmarray_add(ar,(int)(dsec+0.5),y1);
134           y1++;
135         }
136     }
137 }
138
139 void
140 i_mmarray_info(i_mmarray *ar) {
141   int i;
142   for(i=0;i<ar->lines;i++)
143   if (ar->data[i].max!=-1) printf("line %d: min=%d, max=%d.\n",i,ar->data[i].min,ar->data[i].max);
144 }
145
146
147 void
148 i_arc(i_img *im,int x,int y,float rad,float d1,float d2,i_color *val) {
149   i_mmarray dot;
150   float f,fx,fy;
151   int x1,y1;
152
153   mm_log((1,"i_arc(im* 0x%x,x %d,y %d,rad %.2f,d1 %.2f,d2 %.2f,val 0x%x)\n",im,x,y,rad,d1,d2,val));
154
155   i_mmarray_cr(&dot,im->ysize);
156
157   x1=(int)(x+0.5+rad*cos(d1*PI/180.0));
158   y1=(int)(y+0.5+rad*sin(d1*PI/180.0));
159   fx=(float)x1; fy=(float)y1;
160
161   /*  printf("x1: %d.\ny1: %d.\n",x1,y1); */
162   i_arcdraw(x, y, x1, y1, &dot);
163
164   x1=(int)(x+0.5+rad*cos(d2*PI/180.0));
165   y1=(int)(y+0.5+rad*sin(d2*PI/180.0));
166
167   for(f=d1;f<=d2;f+=0.01) i_mmarray_add(&dot,(int)(x+0.5+rad*cos(f*PI/180.0)),(int)(y+0.5+rad*sin(f*PI/180.0)));
168   
169   /*  printf("x1: %d.\ny1: %d.\n",x1,y1); */
170   i_arcdraw(x, y, x1, y1, &dot);
171
172   /*  dot.info(); */
173   i_mmarray_render(im,&dot,val);
174   i_mmarray_dst(&dot);
175 }
176
177 void
178 i_arc_cfill(i_img *im,int x,int y,float rad,float d1,float d2,i_fill_t *fill) {
179   i_mmarray dot;
180   float f,fx,fy;
181   int x1,y1;
182
183   mm_log((1,"i_arc_cfill(im* 0x%x,x %d,y %d,rad %.2f,d1 %.2f,d2 %.2f,fill 0x%x)\n",im,x,y,rad,d1,d2,fill));
184
185   i_mmarray_cr(&dot,im->ysize);
186
187   x1=(int)(x+0.5+rad*cos(d1*PI/180.0));
188   y1=(int)(y+0.5+rad*sin(d1*PI/180.0));
189   fx=(float)x1; fy=(float)y1;
190
191   /*  printf("x1: %d.\ny1: %d.\n",x1,y1); */
192   i_arcdraw(x, y, x1, y1, &dot);
193
194   x1=(int)(x+0.5+rad*cos(d2*PI/180.0));
195   y1=(int)(y+0.5+rad*sin(d2*PI/180.0));
196
197   for(f=d1;f<=d2;f+=0.01) i_mmarray_add(&dot,(int)(x+0.5+rad*cos(f*PI/180.0)),(int)(y+0.5+rad*sin(f*PI/180.0)));
198   
199   /*  printf("x1: %d.\ny1: %d.\n",x1,y1); */
200   i_arcdraw(x, y, x1, y1, &dot);
201
202   /*  dot.info(); */
203   i_mmarray_render_fill(im,&dot,fill);
204   i_mmarray_dst(&dot);
205 }
206
207
208
209 /* Temporary AA HACK */
210
211
212 typedef int frac;
213 static  frac float_to_frac(float x) { return (frac)(0.5+x*16.0); }
214 static   int frac_sub     (frac x)  { return (x%16); }
215 static   int frac_int     (frac x)  { return (x/16); }
216 static float frac_to_float(float x) { return (float)x/16.0; }
217
218 static 
219 void
220 polar_to_plane(float cx, float cy, float angle, float radius, frac *x, frac *y) {
221   *x = float_to_frac(cx+radius*cos(angle));
222   *y = float_to_frac(cy+radius*sin(angle));
223 }
224
225 static
226 void
227 order_pair(frac *x, frac *y) {
228   frac t = *x;
229   if (t>*y) {
230     *x = *y;
231     *y = t;
232   }
233 }
234
235
236
237
238 static
239 void
240 make_minmax_list(i_mmarray *dot, float x, float y, float radius) {
241   float angle = 0.0;
242   float astep = radius>0.1 ? .5/radius : 10;
243   frac cx, cy, lx, ly, sx, sy;
244
245   mm_log((1, "make_minmax_list(dot %p, x %.2f, y %.2f, radius %.2f)\n", dot, x, y, radius));
246
247   polar_to_plane(x, y, angle, radius, &sx, &sy);
248   
249   for(angle = 0.0; angle<361; angle +=astep) {
250     float alpha;
251     lx = sx; ly = sy;
252     polar_to_plane(x, y, angle, radius, &cx, &cy);
253     sx = cx; sy = cy;
254
255     if (fabs(cx-lx) > fabs(cy-ly)) {
256       int ccx, ccy;
257       if (lx>cx) { 
258         ccx = lx; lx = cx; cx = ccx; 
259         ccy = ly; ly = cy; cy = ccy; 
260       }
261
262       for(ccx=lx; ccx<=cx; ccx++) {
263         ccy = ly + ((cy-ly)*(ccx-lx))/(cx-lx);
264         i_mmarray_add(dot, ccx, ccy);
265       }
266     } else {
267       int ccx, ccy;
268
269       if (ly>cy) { 
270         ccy = ly; ly = cy; cy = ccy; 
271         ccx = lx; lx = cx; cx = ccx; 
272       }
273       
274       for(ccy=ly; ccy<=cy; ccy++) {
275         if (cy-ly) ccx = lx + ((cx-lx)*(ccy-ly))/(cy-ly); else ccx = lx;
276         i_mmarray_add(dot, ccx, ccy);
277       }
278     }
279   }
280 }
281
282 /* Get the number of subpixels covered */
283
284 static
285 int
286 i_pixel_coverage(i_mmarray *dot, int x, int y) {
287   frac minx = x*16;
288   frac maxx = minx+15;
289   frac cy;
290   int cnt = 0;
291   
292   for(cy=y*16; cy<(y+1)*16; cy++) {
293     frac tmin = dot->data[cy].min;
294     frac tmax = dot->data[cy].max;
295
296     if (tmax == -1 || tmin > maxx || tmax < minx) continue;
297     
298     if (tmin < minx) tmin = minx;
299     if (tmax > maxx) tmax = maxx;
300     
301     cnt+=1+tmax-tmin;
302   }
303   return cnt;
304 }
305
306 void
307 i_circle_aa(i_img *im, float x, float y, float rad, i_color *val) {
308   i_mmarray dot;
309   i_color temp;
310   int ly;
311
312   mm_log((1, "i_circle_aa(im %p, x %d, y %d, rad %.2f, val %p)\n", im, x, y, rad, val));
313
314   i_mmarray_cr(&dot,16*im->ysize);
315   make_minmax_list(&dot, x, y, rad);
316
317   for(ly = 0; ly<im->ysize; ly++) {
318     int ix, cy, cnt = 0, minx = INT_MAX, maxx = INT_MIN;
319
320     /* Find the left/rightmost set subpixels */
321     for(cy = 0; cy<16; cy++) {
322       frac tmin = dot.data[ly*16+cy].min;
323       frac tmax = dot.data[ly*16+cy].max;
324       if (tmax == -1) continue;
325
326       if (minx > tmin) minx = tmin;
327       if (maxx < tmax) maxx = tmax;
328     }
329
330     if (maxx == INT_MIN) continue; /* no work to be done for this row of pixels */
331
332     minx /= 16;
333     maxx /= 16;
334     for(ix=minx; ix<=maxx; ix++) {
335       int cnt = i_pixel_coverage(&dot, ix, ly);
336       if (cnt>255) cnt = 255;
337       if (cnt) { /* should never be true */
338         int ch;
339         float ratio = (float)cnt/255.0;
340         i_gpix(im, ix, ly, &temp);
341         for(ch=0;ch<im->channels; ch++) temp.channel[ch] = (unsigned char)((float)val->channel[ch]*ratio + (float)temp.channel[ch]*(1.0-ratio));
342         i_ppix(im, ix, ly, &temp);
343       }
344     }
345   }
346   i_mmarray_dst(&dot);
347 }
348
349
350
351
352
353
354 void
355 i_box(i_img *im,int x1,int y1,int x2,int y2,i_color *val) {
356   int x,y;
357   mm_log((1,"i_box(im* 0x%x,x1 %d,y1 %d,x2 %d,y2 %d,val 0x%x)\n",im,x1,y1,x2,y2,val));
358   for(x=x1;x<x2+1;x++) {
359     i_ppix(im,x,y1,val);
360     i_ppix(im,x,y2,val);
361   }
362   for(y=y1;y<y2+1;y++) {
363     i_ppix(im,x1,y,val);
364     i_ppix(im,x2,y,val);
365   }
366 }
367
368 void
369 i_box_filled(i_img *im,int x1,int y1,int x2,int y2,i_color *val) {
370   int x,y;
371   mm_log((1,"i_box_filled(im* 0x%x,x1 %d,y1 %d,x2 %d,y2 %d,val 0x%x)\n",im,x1,y1,x2,y2,val));
372   for(x=x1;x<x2+1;x++) for (y=y1;y<y2+1;y++) i_ppix(im,x,y,val);
373 }
374
375 void
376 i_box_cfill(i_img *im,int x1,int y1,int x2,int y2,i_fill_t *fill) {
377   mm_log((1,"i_box_cfill(im* 0x%x,x1 %d,y1 %d,x2 %d,y2 %d,fill 0x%x)\n",im,x1,y1,x2,y2,fill));
378
379   ++x2;
380   if (im->bits == i_8_bits && fill->fill_with_color) {
381     i_color *line = mymalloc(sizeof(i_color) * (x2 - x1));
382     i_color *work = NULL;
383     if (fill->combine)
384       work = mymalloc(sizeof(i_color) * (x2-x1));
385     while (y1 <= y2) {
386       if (fill->combine) {
387         i_glin(im, x1, x2, y1, line);
388         (fill->fill_with_color)(fill, x1, y1, x2-x1, im->channels, work);
389         (fill->combine)(line, work, im->channels, x2-x1);
390       }
391       else {
392         (fill->fill_with_color)(fill, x1, y1, x2-x1, im->channels, line);
393       }
394       i_plin(im, x1, x2, y1, line);
395       ++y1;
396     }
397     myfree(line);
398     if (work)
399       myfree(work);
400   }
401   else {
402     i_fcolor *line = mymalloc(sizeof(i_fcolor) * (x2 - x1));
403     i_fcolor *work;
404     work = mymalloc(sizeof(i_fcolor) * (x2 - x1));
405
406     while (y1 <= y2) {
407       if (fill->combine) {
408         i_glinf(im, x1, x2, y1, line);
409         (fill->fill_with_fcolor)(fill, x1, y1, x2-x1, im->channels, work);
410         (fill->combinef)(line, work, im->channels, x2-x1);
411       }
412       else {
413         (fill->fill_with_fcolor)(fill, x1, y1, x2-x1, im->channels, line);
414       }
415       i_plinf(im, x1, x2, y1, line);
416     }
417     myfree(line);
418     if (work)
419       myfree(work);
420   }
421 }
422
423 void
424 i_draw(i_img *im,int x1,int y1,int x2,int y2,i_color *val) {
425   double alpha;
426   double dsec;
427   int temp;
428
429   mm_log((1,"i_draw(im* 0x%x,x1 %d,y1 %d,x2 %d,y2 %d,val 0x%x)\n",im,x1,y1,x2,y2,val));
430
431   alpha=(double)(y2-y1)/(double)(x2-x1);
432   if (fabs(alpha)<1) 
433     {
434       if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
435       dsec=y1;
436       while(x1<x2)
437         {
438           dsec+=alpha;
439           i_ppix(im,x1,(int)(dsec+0.5),val);
440           x1++;
441         }
442     }
443   else
444     {
445       alpha=1/alpha;
446       if (y2<y1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
447       dsec=x1;
448       while(y1<y2)
449         {
450           dsec+=alpha;
451           i_ppix(im,(int)(dsec+0.5),y1,val);
452           y1++;
453         }
454     }
455   mm_log((1,"i_draw: alpha=%f.\n",alpha));
456 }
457
458 void
459 i_line_aa(i_img *im,int x1,int y1,int x2,int y2,i_color *val) {
460   i_color tval;
461   float alpha;
462   float dsec,dfrac;
463   int temp,dx,dy,isec,ch;
464
465   mm_log((1,"i_draw(im* 0x%x,x1 %d,y1 %d,x2 %d,y2 %d,val 0x%x)\n",im,x1,y1,x2,y2,val));
466
467   dy=y2-y1;
468   dx=x2-x1;
469
470   if (abs(dx)>abs(dy)) { /* alpha < 1 */
471     if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
472     alpha=(float)(y2-y1)/(float)(x2-x1);
473
474     dsec=y1;
475     while(x1<=x2) {
476       isec=(int)dsec;
477       dfrac=dsec-isec;
478       /*      dfrac=1-(1-dfrac)*(1-dfrac); */
479       /* This is something we can play with to try to get better looking lines */
480
481       i_gpix(im,x1,isec,&tval);
482       for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)(dfrac*(float)tval.channel[ch]+(1-dfrac)*(float)val->channel[ch]);
483       i_ppix(im,x1,isec,&tval);
484       
485       i_gpix(im,x1,isec+1,&tval);
486       for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)((1-dfrac)*(float)tval.channel[ch]+dfrac*(float)val->channel[ch]);
487       i_ppix(im,x1,isec+1,&tval);
488       
489       dsec+=alpha;
490       x1++;
491     }
492   } else {
493     if (y2<y1) { temp=y1; y1=y2; y2=temp; temp=x1; x1=x2; x2=temp; }
494     alpha=(float)(x2-x1)/(float)(y2-y1);
495     dsec=x1;
496     while(y1<=y2) {
497       isec=(int)dsec;
498       dfrac=dsec-isec;
499       /*      dfrac=sqrt(dfrac); */
500       /* This is something we can play with */
501       i_gpix(im,isec,y1,&tval);
502       for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)(dfrac*(float)tval.channel[ch]+(1-dfrac)*(float)val->channel[ch]);
503       i_ppix(im,isec,y1,&tval);
504
505       i_gpix(im,isec+1,y1,&tval);
506       for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)((1-dfrac)*(float)tval.channel[ch]+dfrac*(float)val->channel[ch]);
507       i_ppix(im,isec+1,y1,&tval);
508
509       dsec+=alpha;
510       y1++;
511     }
512   }
513 }
514
515 double
516 perm(int n,int k) {
517   double r;
518   int i;
519   r=1;
520   for(i=k+1;i<=n;i++) r*=i;
521   for(i=1;i<=(n-k);i++) r/=i;
522   return r;
523 }
524
525
526 /* Note in calculating t^k*(1-t)^(n-k) 
527    we can start by using t^0=1 so this simplifies to
528    t^0*(1-t)^n - we want to multiply that with t/(1-t) each iteration
529    to get a new level - this may lead to errors who knows lets test it */
530
531 void
532 i_bezier_multi(i_img *im,int l,double *x,double *y,i_color *val) {
533   double *bzcoef;
534   double t,cx,cy;
535   int k,i;
536   int lx = 0,ly = 0;
537   int n=l-1;
538   double itr,ccoef;
539
540
541   bzcoef=mymalloc(sizeof(double)*l);
542   for(k=0;k<l;k++) bzcoef[k]=perm(n,k);
543   ICL_info(val);
544
545
546   /*  for(k=0;k<l;k++) printf("bzcoef: %d -> %f\n",k,bzcoef[k]); */
547   i=0;
548   for(t=0;t<=1;t+=0.005) {
549     cx=cy=0;
550     itr=t/(1-t);
551     ccoef=pow(1-t,n);
552     for(k=0;k<l;k++) {
553       /*      cx+=bzcoef[k]*x[k]*pow(t,k)*pow(1-t,n-k); 
554               cy+=bzcoef[k]*y[k]*pow(t,k)*pow(1-t,n-k);*/
555
556       cx+=bzcoef[k]*x[k]*ccoef;
557       cy+=bzcoef[k]*y[k]*ccoef;
558       ccoef*=itr;
559     }
560     /*    printf("%f -> (%d,%d)\n",t,(int)(0.5+cx),(int)(0.5+cy)); */
561     if (i++) { 
562       i_line_aa(im,lx,ly,(int)(0.5+cx),(int)(0.5+cy),val);
563     }
564       /*     i_ppix(im,(int)(0.5+cx),(int)(0.5+cy),val); */
565     lx=(int)(0.5+cx);
566     ly=(int)(0.5+cy);
567   }
568   ICL_info(val);
569   myfree(bzcoef);
570 }
571
572
573
574
575
576
577
578
579
580
581
582 /* Flood fill 
583
584    REF: Graphics Gems I. page 282+
585
586 */
587
588 /* This should be moved into a seperate file? */
589
590 /* This is the truncation used:
591    
592    a double is multiplied by 16 and then truncated.
593    This means that 0 -> 0
594    So a triangle of (0,0) (10,10) (10,0) Will look like it's
595    not filling the (10,10) point nor the (10,0)-(10,10)  line segment
596
597 */
598
599
600 /* Flood fill algorithm - based on the Ken Fishkins (pixar) gem in 
601    graphics gems I */
602
603 /*
604 struct stc {
605   int mylx,myrx; 
606   int dadlx,dadrx;
607   int myy;
608   int mydirection;
609 };
610
611 Not used code???
612 */
613
614
615 struct stack_element {
616   int myLx,myRx;
617   int dadLx,dadRx;
618   int myY;
619   int myDirection;
620 };
621
622
623 /* create the link data to put push onto the stack */
624
625 static
626 struct stack_element*
627 crdata(int left,int right,int dadl,int dadr,int y, int dir) {
628   struct stack_element *ste;
629   ste              = mymalloc(sizeof(struct stack_element));
630   ste->myLx        = left;
631   ste->myRx        = right;
632   ste->dadLx       = dadl;
633   ste->dadRx       = dadr;
634   ste->myY         = y;
635   ste->myDirection = dir;
636   return ste;
637 }
638
639 /* i_ccomp compares two colors and gives true if they are the same */
640
641 static int
642 i_ccomp(i_color *val1,i_color *val2,int ch) {
643   int i;
644   for(i=0;i<ch;i++) if (val1->channel[i] !=val2->channel[i]) return 0;
645   return 1;
646 }
647
648
649 static int
650 i_lspan(i_img *im,int seedx,int seedy,i_color *val) {
651   i_color cval;
652   while(1) {
653     if (seedx-1 < 0) break;
654     i_gpix(im,seedx-1,seedy,&cval);
655     if (!i_ccomp(val,&cval,im->channels)) break;
656     seedx--;
657   }
658   return seedx;
659 }
660
661 static int
662 i_rspan(i_img *im,int seedx,int seedy,i_color *val) {
663   i_color cval;
664   while(1) {
665     if (seedx+1 > im->xsize-1) break;
666     i_gpix(im,seedx+1,seedy,&cval);
667     if (!i_ccomp(val,&cval,im->channels)) break;
668     seedx++;
669   }
670   return seedx;
671 }
672
673 /* Macro to create a link and push on to the list */
674
675 #define ST_PUSH(left,right,dadl,dadr,y,dir) { struct stack_element *s = crdata(left,right,dadl,dadr,y,dir); llist_push(st,&s); }
676
677 /* pops the shadow on TOS into local variables lx,rx,y,direction,dadLx and dadRx */
678 /* No overflow check! */
679  
680 #define ST_POP() { struct stack_element *s; llist_pop(st,&s); lx = s->myLx; rx = s->myRx; dadLx = s->dadLx; dadRx = s->dadRx; y = s->myY; direction= s->myDirection; myfree(s); }
681
682 #define ST_STACK(dir,dadLx,dadRx,lx,rx,y) { int pushrx = rx+1; int pushlx = lx-1; ST_PUSH(lx,rx,pushlx,pushrx,y+dir,dir); if (rx > dadRx)                                      ST_PUSH(dadRx+1,rx,pushlx,pushrx,y-dir,-dir); if (lx < dadLx) ST_PUSH(lx,dadLx-1,pushlx,pushrx,y-dir,-dir); }
683
684 #define SET(x,y) btm_set(btm,x,y);
685
686 #define INSIDE(x,y) ((!btm_test(btm,x,y) && ( i_gpix(im,x,y,&cval),i_ccomp(&val,&cval,channels)  ) ))
687
688 void
689 i_flood_fill(i_img *im,int seedx,int seedy,i_color *dcol) {
690
691   int lx,rx;
692   int y;
693   int direction;
694   int dadLx,dadRx;
695
696   int wasIn=0;
697   int x=0;
698
699   /*  int tx,ty; */
700
701   int bxmin=seedx,bxmax=seedx,bymin=seedy,bymax=seedy;
702
703   struct llist *st;
704   struct i_bitmap *btm;
705
706   int channels,xsize,ysize;
707   i_color cval,val;
708
709   channels = im->channels;
710   xsize    = im->xsize;
711   ysize    = im->ysize;
712
713   btm = btm_new(xsize,ysize);
714   st = llist_new(100,sizeof(struct stack_element*));
715
716   /* Get the reference color */
717   i_gpix(im,seedx,seedy,&val);
718
719   /* Find the starting span and fill it */
720   lx = i_lspan(im,seedx,seedy,&val);
721   rx = i_rspan(im,seedx,seedy,&val);
722   
723   /* printf("span: %d %d \n",lx,rx); */
724
725   for(x=lx; x<=rx; x++) SET(x,seedy);
726
727   ST_PUSH(lx, rx, lx, rx, seedy+1, 1);
728   ST_PUSH(lx, rx, lx, rx, seedy-1,-1);
729
730   while(st->count) {
731     ST_POP();
732     
733     if (y<0 || y>ysize-1) continue;
734
735     if (bymin > y) bymin=y; /* in the worst case an extra line */
736     if (bymax < y) bymax=y; 
737
738     /*     printf("start of scan - on stack : %d \n",st->count); */
739
740     
741     /*     printf("lx=%d rx=%d dadLx=%d dadRx=%d y=%d direction=%d\n",lx,rx,dadLx,dadRx,y,direction); */
742     
743     /*
744     printf(" ");
745     for(tx=0;tx<xsize;tx++) printf("%d",tx%10);
746     printf("\n");
747     for(ty=0;ty<ysize;ty++) {
748       printf("%d",ty%10);
749       for(tx=0;tx<xsize;tx++) printf("%d",!!btm_test(btm,tx,ty));
750       printf("\n");
751     }
752
753     printf("y=%d\n",y);
754     */
755
756
757     x=lx+1;
758     if ( (wasIn = INSIDE(lx,y)) ) {
759       SET(lx,y);
760       lx--;
761       while(INSIDE(lx,y) && lx > 0) {
762         SET(lx,y);
763         lx--;
764       }
765     }
766
767     if (bxmin > lx) bxmin=lx;
768     
769     while(x <= xsize-1) {
770       /*  printf("x=%d\n",x); */
771       if (wasIn) {
772         
773         if (INSIDE(x,y)) {
774           /* case 1: was inside, am still inside */
775           SET(x,y);
776         } else {
777           /* case 2: was inside, am no longer inside: just found the
778              right edge of a span */
779           ST_STACK(direction,dadLx,dadRx,lx,(x-1),y);
780
781           if (bxmax < x) bxmax=x;
782
783           wasIn=0;
784         }
785       } else {
786         if (x>rx) goto EXT;
787         if (INSIDE(x,y)) {
788           SET(x,y);
789           /* case 3: Wasn't inside, am now: just found the start of a new run */
790           wasIn=1;
791             lx=x;
792         } else {
793           /* case 4: Wasn't inside, still isn't */
794         }
795       }
796       x++;
797     }
798   EXT: /* out of loop */
799     if (wasIn) {
800       /* hit an edge of the frame buffer while inside a run */
801       ST_STACK(direction,dadLx,dadRx,lx,(x-1),y);
802       if (bxmax < x) bxmax=x;
803     }
804   }
805   
806   /*   printf("lx=%d rx=%d dadLx=%d dadRx=%d y=%d direction=%d\n",lx,rx,dadLx,dadRx,y,direction); 
807        printf("bounding box: [%d,%d] - [%d,%d]\n",bxmin,bymin,bxmax,bymax); */
808
809   for(y=bymin;y<=bymax;y++) for(x=bxmin;x<=bxmax;x++) if (btm_test(btm,x,y)) i_ppix(im,x,y,dcol);
810
811   btm_destroy(btm);
812   mm_log((1, "DESTROY\n"));
813   llist_destroy(st);
814 }
815
816 static struct i_bitmap *
817 i_flood_fill_low(i_img *im,int seedx,int seedy,
818                  int *bxminp, int *bxmaxp, int *byminp, int *bymaxp) {
819   int lx,rx;
820   int y;
821   int direction;
822   int dadLx,dadRx;
823
824   int wasIn=0;
825   int x=0;
826
827   /*  int tx,ty; */
828
829   int bxmin=seedx,bxmax=seedx,bymin=seedy,bymax=seedy;
830
831   struct llist *st;
832   struct i_bitmap *btm;
833
834   int channels,xsize,ysize;
835   i_color cval,val;
836
837   channels=im->channels;
838   xsize=im->xsize;
839   ysize=im->ysize;
840
841   btm=btm_new(xsize,ysize);
842   st=llist_new(100,sizeof(struct stack_element*));
843
844   /* Get the reference color */
845   i_gpix(im,seedx,seedy,&val);
846
847   /* Find the starting span and fill it */
848   lx=i_lspan(im,seedx,seedy,&val);
849   rx=i_rspan(im,seedx,seedy,&val);
850   
851   /* printf("span: %d %d \n",lx,rx); */
852
853   for(x=lx;x<=rx;x++) SET(x,seedy);
854
855   ST_PUSH(lx,rx,lx,rx,seedy+1,1);
856   ST_PUSH(lx,rx,lx,rx,seedy-1,-1);
857
858   while(st->count) {
859     ST_POP();
860     
861     if (y<0 || y>ysize-1) continue;
862
863     if (bymin > y) bymin=y; /* in the worst case an extra line */
864     if (bymax < y) bymax=y; 
865
866     /*     printf("start of scan - on stack : %d \n",st->count); */
867
868     
869     /*     printf("lx=%d rx=%d dadLx=%d dadRx=%d y=%d direction=%d\n",lx,rx,dadLx,dadRx,y,direction); */
870     
871     /*
872     printf(" ");
873     for(tx=0;tx<xsize;tx++) printf("%d",tx%10);
874     printf("\n");
875     for(ty=0;ty<ysize;ty++) {
876       printf("%d",ty%10);
877       for(tx=0;tx<xsize;tx++) printf("%d",!!btm_test(btm,tx,ty));
878       printf("\n");
879     }
880
881     printf("y=%d\n",y);
882     */
883
884
885     x=lx+1;
886     if ( (wasIn = INSIDE(lx,y)) ) {
887       SET(lx,y);
888       lx--;
889       while(INSIDE(lx,y) && lx > 0) {
890         SET(lx,y);
891         lx--;
892       }
893     }
894
895     if (bxmin > lx) bxmin=lx;
896     
897     while(x <= xsize-1) {
898       /*  printf("x=%d\n",x); */
899       if (wasIn) {
900         
901         if (INSIDE(x,y)) {
902           /* case 1: was inside, am still inside */
903           SET(x,y);
904         } else {
905           /* case 2: was inside, am no longer inside: just found the
906              right edge of a span */
907           ST_STACK(direction,dadLx,dadRx,lx,(x-1),y);
908
909           if (bxmax < x) bxmax=x;
910
911           wasIn=0;
912         }
913       } else {
914         if (x>rx) goto EXT;
915         if (INSIDE(x,y)) {
916           SET(x,y);
917           /* case 3: Wasn't inside, am now: just found the start of a new run */
918           wasIn=1;
919             lx=x;
920         } else {
921           /* case 4: Wasn't inside, still isn't */
922         }
923       }
924       x++;
925     }
926   EXT: /* out of loop */
927     if (wasIn) {
928       /* hit an edge of the frame buffer while inside a run */
929       ST_STACK(direction,dadLx,dadRx,lx,(x-1),y);
930       if (bxmax < x) bxmax=x;
931     }
932   }
933   
934   /*   printf("lx=%d rx=%d dadLx=%d dadRx=%d y=%d direction=%d\n",lx,rx,dadLx,dadRx,y,direction); 
935        printf("bounding box: [%d,%d] - [%d,%d]\n",bxmin,bymin,bxmax,bymax); */
936
937   llist_destroy(st);
938
939   *bxminp = bxmin;
940   *bxmaxp = bxmax;
941   *byminp = bymin;
942   *bymaxp = bymax;
943
944   return btm;
945 }
946
947 void
948 i_flood_cfill(i_img *im, int seedx, int seedy, i_fill_t *fill) {
949   int bxmin, bxmax, bymin, bymax;
950   struct i_bitmap *btm;
951   int x, y;
952   int start;
953
954   btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax);
955
956   if (im->bits == i_8_bits && fill->fill_with_color) {
957     i_color *line = mymalloc(sizeof(i_color) * (bxmax - bxmin));
958     i_color *work = NULL;
959     if (fill->combine)
960       work = mymalloc(sizeof(i_color) * (bxmax - bxmin));
961
962     for(y=bymin;y<=bymax;y++) {
963       x = bxmin;
964       while (x < bxmax) {
965         while (x < bxmax && !btm_test(btm, x, y)) {
966           ++x;
967         }
968         if (btm_test(btm, x, y)) {
969           start = x;
970           while (x < bxmax && btm_test(btm, x, y)) {
971             ++x;
972           }
973           if (fill->combine) {
974             i_glin(im, start, x, y, line);
975             (fill->fill_with_color)(fill, start, y, x-start, im->channels, 
976                                     work);
977             (fill->combine)(line, work, im->channels, x-start);
978           }
979           else {
980             (fill->fill_with_color)(fill, start, y, x-start, im->channels, 
981                                     line);
982           }
983           i_plin(im, start, x, y, line);
984         }
985       }
986     }
987     myfree(line);
988     if (work)
989       myfree(work);
990   }
991   else {
992     i_fcolor *line = mymalloc(sizeof(i_fcolor) * (bxmax - bxmin));
993     i_fcolor *work = NULL;
994     if (fill->combinef)
995       work = mymalloc(sizeof(i_fcolor) * (bxmax - bxmin));
996     
997     for(y=bymin;y<=bymax;y++) {
998       x = bxmin;
999       while (x < bxmax) {
1000         while (x < bxmax && !btm_test(btm, x, y)) {
1001           ++x;
1002         }
1003         if (btm_test(btm, x, y)) {
1004           start = x;
1005           while (x < bxmax && btm_test(btm, x, y)) {
1006             ++x;
1007           }
1008           if (fill->combinef) {
1009             i_glinf(im, start, x, y, line);
1010             (fill->fill_with_fcolor)(fill, start, y, x-start, im->channels, 
1011                                     work);
1012             (fill->combinef)(line, work, im->channels, x-start);
1013           }
1014           else {
1015             (fill->fill_with_fcolor)(fill, start, y, x-start, im->channels, 
1016                                     line);
1017           }
1018           i_plinf(im, start, x, y, line);
1019         }
1020       }
1021     }
1022     myfree(line);
1023     if (work)
1024       myfree(work);
1025   }
1026
1027   btm_destroy(btm);
1028 }