]> git.imager.perl.org - imager.git/blob - draw.c
- update ppport.h and remove the duplicate definitions from
[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       ++y1;
417     }
418     myfree(line);
419     if (work)
420       myfree(work);
421   }
422 }
423
424
425 /* 
426 =item i_line(im, x1, y1, x2, y2, val, endp)
427
428 Draw a line to image using bresenhams linedrawing algorithm
429
430    im   - image to draw to
431    x1   - starting x coordinate
432    y1   - starting x coordinate
433    x2   - starting x coordinate
434    y2   - starting x coordinate
435    val  - color to write to image
436    endp - endpoint flag (boolean)
437
438 =cut
439 */
440
441 void
442 i_line(i_img *im, int x1, int y1, int x2, int y2, i_color *val, int endp) {
443   int x, y;
444   int dx, dy;
445   int p;
446   unsigned char *cp;
447
448   dx = x2 - x1;
449   dy = y2 - y1;
450
451
452   /* choose variable to iterate on */
453   if (abs(dx)>abs(dy)) {
454     int dx2, dy2, cpy;
455
456     /* sort by x */
457     if (x1 > x2) {
458       int t;
459       t = x1; x1 = x2; x2 = t;
460       t = y1; y1 = y2; y2 = t;
461     }
462     
463     dx = abs(dx);
464     dx2 = dx*2;
465     dy = y2 - y1;
466
467     if (dy<0) {
468       dy = -dy;
469       cpy = -1;
470     } else {
471       cpy = 1;
472     }
473     dy2 = dy*2;
474     p = dy2 - dx;
475
476     
477     y = y1;
478     for(x=x1; x<x2-1; x++) {
479       if (p<0) {
480         p += dy2;
481       } else {
482         y += cpy;
483         p += dy2-dx2;
484       }
485       i_ppix(im, x+1, y, val);
486     }
487   } else {
488     int dy2, dx2, cpx;
489
490     /* sort bx y */
491     if (y1 > y2) {
492       int t;
493       t = x1; x1 = x2; x2 = t;
494       t = y1; y1 = y2; y2 = t;
495     }
496     
497     dy = abs(dy);
498     dx = x2 - x1;
499     dy2 = dy*2;
500
501     if (dx<0) {
502       dx = -dx;
503       cpx = -1;
504     } else {
505       cpx = 1;
506     }
507     dx2 = dx*2;
508     p = dx2 - dy;
509
510     x = x1;
511     
512     for(y=y1; y<y2-1; y++) {
513       if (p<0) {
514         p  += dx2;
515       } else {
516         x += cpx;
517         p += dx2-dy2;
518       }
519       i_ppix(im, x, y+1, val);
520     }
521   }
522   if (endp) {
523     i_ppix(im, x1, y1, val);
524     i_ppix(im, x2, y2, val);
525   } else {
526     if (x1 != x2 || y1 != y2) 
527       i_ppix(im, x1, y1, val);
528   }
529 }
530
531
532 void
533 i_line_dda(i_img *im, int x1, int y1, int x2, int y2, i_color *val) {
534
535   float dy;
536   int x;
537   
538   for(x=x1; x<=x2; x++) {
539     dy = y1+ (x-x1)/(float)(x2-x1)*(y2-y1);
540     i_ppix(im, x, (int)(dy+0.5), val);
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 void
571 i_line_aa3(i_img *im,int x1,int y1,int x2,int y2,i_color *val) {
572   i_color tval;
573   float alpha;
574   float dsec,dfrac;
575   int temp,dx,dy,isec,ch;
576
577   mm_log((1,"i_line_aa(im* 0x%x,x1 %d,y1 %d,x2 %d,y2 %d,val 0x%x)\n",im,x1,y1,x2,y2,val));
578
579   dy=y2-y1;
580   dx=x2-x1;
581
582   if (abs(dx)>abs(dy)) { /* alpha < 1 */
583     if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
584     alpha=(float)(y2-y1)/(float)(x2-x1);
585
586     dsec=y1;
587     while(x1<=x2) {
588       isec=(int)dsec;
589       dfrac=dsec-isec;
590       /*      dfrac=1-(1-dfrac)*(1-dfrac); */
591       /* This is something we can play with to try to get better looking lines */
592
593       i_gpix(im,x1,isec,&tval);
594       for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)(dfrac*(float)tval.channel[ch]+(1-dfrac)*(float)val->channel[ch]);
595       i_ppix(im,x1,isec,&tval);
596       
597       i_gpix(im,x1,isec+1,&tval);
598       for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)((1-dfrac)*(float)tval.channel[ch]+dfrac*(float)val->channel[ch]);
599       i_ppix(im,x1,isec+1,&tval);
600       
601       dsec+=alpha;
602       x1++;
603     }
604   } else {
605     if (y2<y1) { temp=y1; y1=y2; y2=temp; temp=x1; x1=x2; x2=temp; }
606     alpha=(float)(x2-x1)/(float)(y2-y1);
607     dsec=x1;
608     while(y1<=y2) {
609       isec=(int)dsec;
610       dfrac=dsec-isec;
611       /*      dfrac=sqrt(dfrac); */
612       /* This is something we can play with */
613       i_gpix(im,isec,y1,&tval);
614       for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)(dfrac*(float)tval.channel[ch]+(1-dfrac)*(float)val->channel[ch]);
615       i_ppix(im,isec,y1,&tval);
616
617       i_gpix(im,isec+1,y1,&tval);
618       for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)((1-dfrac)*(float)tval.channel[ch]+dfrac*(float)val->channel[ch]);
619       i_ppix(im,isec+1,y1,&tval);
620
621       dsec+=alpha;
622       y1++;
623     }
624   }
625 }
626
627
628
629
630 void
631 i_line_aa(i_img *im, int x1, int y1, int x2, int y2, i_color *val, int endp) {
632   int x, y;
633   int dx, dy;
634   int p;
635   unsigned char *cp;
636
637   dx = x2 - x1;
638   dy = y2 - y1;
639
640   /* choose variable to iterate on */
641   if (abs(dx)>abs(dy)) {
642     int dx2, dy2, cpy;
643     
644     /* sort by x */
645     if (x1 > x2) {
646       int t;
647       t = x1; x1 = x2; x2 = t;
648       t = y1; y1 = y2; y2 = t;
649     }
650     
651     dx = abs(dx);
652     dx2 = dx*2;
653     dy = y2 - y1;
654
655     if (dy<0) {
656       dy = -dy;
657       cpy = -1;
658     } else {
659       cpy = 1;
660     }
661     dy2 = dy*2;
662     p = dy2 - dx2; /* this has to be like this for AA */
663     
664     y = y1;
665
666     for(x=x1; x<x2-1; x++) {
667       int ch;
668       i_color tval;
669       float t = (dy) ? -(float)(p)/(float)(dx2) : 1;
670       float t1, t2;
671
672       if (t<0) t = 0;
673       t1 = 1-t;
674       t2 = t;
675
676       i_gpix(im,x+1,y,&tval);
677       for(ch=0;ch<im->channels;ch++)
678         tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
679       i_ppix(im,x+1,y,&tval);
680
681       i_gpix(im,x+1,y+cpy,&tval);
682       for(ch=0;ch<im->channels;ch++)
683         tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
684       i_ppix(im,x+1,y+cpy,&tval);
685
686       if (p<0) {
687         p += dy2;
688       } else {
689         y += cpy;
690         p += dy2-dx2;
691       }
692     }
693   } else {
694     int dy2, dx2, cpx;
695
696     /* sort bx y */
697     if (y1 > y2) {
698       int t;
699       t = x1; x1 = x2; x2 = t;
700       t = y1; y1 = y2; y2 = t;
701     }
702     
703     dy = abs(dy);
704     dx = x2 - x1;
705     dy2 = dy*2;
706
707     if (dx<0) {
708       dx = -dx;
709       cpx = -1;
710     } else {
711       cpx = 1;
712     }
713     dx2 = dx*2;
714     p = dx2 - dy2; /* this has to be like this for AA */
715
716     x = x1;
717     
718     for(y=y1; y<y2-1; y++) {
719       int ch;
720       i_color tval;
721       float t = (dx) ? -(float)(p)/(float)(dy2) : 1;
722       float t1, t2;
723       
724       if (t<0) t = 0;
725       t1 = 1-t;
726       t2 = t;
727
728       i_gpix(im,x,y+1,&tval);
729       for(ch=0;ch<im->channels;ch++)
730         tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
731       i_ppix(im,x,y+1,&tval);
732
733       i_gpix(im,x+cpx,y+1,&tval);
734       for(ch=0;ch<im->channels;ch++)
735         tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
736       i_ppix(im,x+cpx,y+1,&tval);
737
738       if (p<0) {
739         p  += dx2;
740       } else {
741         x += cpx;
742         p += dx2-dy2;
743       }
744     }
745   }
746
747
748   if (endp) {
749     i_ppix(im, x1, y1, val);
750     i_ppix(im, x2, y2, val);
751   } else {
752     if (x1 != x2 || y1 != y2) 
753       i_ppix(im, x1, y1, val);
754   }
755 }
756
757
758
759 static double
760 perm(int n,int k) {
761   double r;
762   int i;
763   r=1;
764   for(i=k+1;i<=n;i++) r*=i;
765   for(i=1;i<=(n-k);i++) r/=i;
766   return r;
767 }
768
769
770 /* Note in calculating t^k*(1-t)^(n-k) 
771    we can start by using t^0=1 so this simplifies to
772    t^0*(1-t)^n - we want to multiply that with t/(1-t) each iteration
773    to get a new level - this may lead to errors who knows lets test it */
774
775 void
776 i_bezier_multi(i_img *im,int l,double *x,double *y,i_color *val) {
777   double *bzcoef;
778   double t,cx,cy;
779   int k,i;
780   int lx = 0,ly = 0;
781   int n=l-1;
782   double itr,ccoef;
783
784
785   bzcoef=mymalloc(sizeof(double)*l);
786   for(k=0;k<l;k++) bzcoef[k]=perm(n,k);
787   ICL_info(val);
788
789
790   /*  for(k=0;k<l;k++) printf("bzcoef: %d -> %f\n",k,bzcoef[k]); */
791   i=0;
792   for(t=0;t<=1;t+=0.005) {
793     cx=cy=0;
794     itr=t/(1-t);
795     ccoef=pow(1-t,n);
796     for(k=0;k<l;k++) {
797       /*      cx+=bzcoef[k]*x[k]*pow(t,k)*pow(1-t,n-k); 
798               cy+=bzcoef[k]*y[k]*pow(t,k)*pow(1-t,n-k);*/
799
800       cx+=bzcoef[k]*x[k]*ccoef;
801       cy+=bzcoef[k]*y[k]*ccoef;
802       ccoef*=itr;
803     }
804     /*    printf("%f -> (%d,%d)\n",t,(int)(0.5+cx),(int)(0.5+cy)); */
805     if (i++) { 
806       i_line_aa(im,lx,ly,(int)(0.5+cx),(int)(0.5+cy),val, 1);
807     }
808       /*     i_ppix(im,(int)(0.5+cx),(int)(0.5+cy),val); */
809     lx=(int)(0.5+cx);
810     ly=(int)(0.5+cy);
811   }
812   ICL_info(val);
813   myfree(bzcoef);
814 }
815
816
817
818
819
820
821
822
823
824
825
826 /* Flood fill 
827
828    REF: Graphics Gems I. page 282+
829
830 */
831
832 /* This should be moved into a seperate file? */
833
834 /* This is the truncation used:
835    
836    a double is multiplied by 16 and then truncated.
837    This means that 0 -> 0
838    So a triangle of (0,0) (10,10) (10,0) Will look like it's
839    not filling the (10,10) point nor the (10,0)-(10,10)  line segment
840
841 */
842
843
844 /* Flood fill algorithm - based on the Ken Fishkins (pixar) gem in 
845    graphics gems I */
846
847 /*
848 struct stc {
849   int mylx,myrx; 
850   int dadlx,dadrx;
851   int myy;
852   int mydirection;
853 };
854
855 Not used code???
856 */
857
858
859 struct stack_element {
860   int myLx,myRx;
861   int dadLx,dadRx;
862   int myY;
863   int myDirection;
864 };
865
866
867 /* create the link data to put push onto the stack */
868
869 static
870 struct stack_element*
871 crdata(int left,int right,int dadl,int dadr,int y, int dir) {
872   struct stack_element *ste;
873   ste              = mymalloc(sizeof(struct stack_element));
874   ste->myLx        = left;
875   ste->myRx        = right;
876   ste->dadLx       = dadl;
877   ste->dadRx       = dadr;
878   ste->myY         = y;
879   ste->myDirection = dir;
880   return ste;
881 }
882
883 /* i_ccomp compares two colors and gives true if they are the same */
884
885 static int
886 i_ccomp(i_color *val1,i_color *val2,int ch) {
887   int i;
888   for(i=0;i<ch;i++) if (val1->channel[i] !=val2->channel[i]) return 0;
889   return 1;
890 }
891
892
893 static int
894 i_lspan(i_img *im, int seedx, int seedy, i_color *val) {
895   i_color cval;
896   while(1) {
897     if (seedx-1 < 0) break;
898     i_gpix(im,seedx-1,seedy,&cval);
899     if (!i_ccomp(val,&cval,im->channels)) break;
900     seedx--;
901   }
902   return seedx;
903 }
904
905 static int
906 i_rspan(i_img *im, int seedx, int seedy, i_color *val) {
907   i_color cval;
908   while(1) {
909     if (seedx+1 > im->xsize-1) break;
910     i_gpix(im,seedx+1,seedy,&cval);
911     if (!i_ccomp(val,&cval,im->channels)) break;
912     seedx++;
913   }
914   return seedx;
915 }
916
917 /* Macro to create a link and push on to the list */
918
919 #define ST_PUSH(left,right,dadl,dadr,y,dir) do {                 \
920   struct stack_element *s = crdata(left,right,dadl,dadr,y,dir);  \
921   llist_push(st,&s);                                             \
922 } while (0)
923
924 /* pops the shadow on TOS into local variables lx,rx,y,direction,dadLx and dadRx */
925 /* No overflow check! */
926  
927 #define ST_POP() do {         \
928   struct stack_element *s;    \
929   llist_pop(st,&s);           \
930   lx        = s->myLx;        \
931   rx        = s->myRx;        \
932   dadLx     = s->dadLx;       \
933   dadRx     = s->dadRx;       \
934   y         = s->myY;         \
935   direction = s->myDirection; \
936   myfree(s);                  \
937 } while (0)
938
939 #define ST_STACK(dir,dadLx,dadRx,lx,rx,y) do {                    \
940   int pushrx = rx+1;                                              \
941   int pushlx = lx-1;                                              \
942   ST_PUSH(lx,rx,pushlx,pushrx,y+dir,dir);                         \
943   if (rx > dadRx)                                                 \
944     ST_PUSH(dadRx+1,rx,pushlx,pushrx,y-dir,-dir);                 \
945   if (lx < dadLx) ST_PUSH(lx,dadLx-1,pushlx,pushrx,y-dir,-dir);   \
946 } while (0)
947
948 #define SET(x,y) btm_set(btm,x,y)
949
950 /* INSIDE returns true if pixel is correct color and we haven't set it before. */
951 #define INSIDE(x,y) ((!btm_test(btm,x,y) && ( i_gpix(im,x,y,&cval),i_ccomp(&val,&cval,channels)  ) ))
952
953
954
955 /* The function that does all the real work */
956
957 static struct i_bitmap *
958 i_flood_fill_low(i_img *im,int seedx,int seedy,
959                  int *bxminp, int *bxmaxp, int *byminp, int *bymaxp) {
960
961   /*
962     int lx,rx;
963     int y;
964     int direction;
965     int dadLx,dadRx;
966     int wasIn=0;
967   */
968   int ltx, rtx;
969   int tx = 0;
970
971   int bxmin = seedx;
972   int bxmax = seedx;
973   int bymin = seedy;
974   int bymax = seedy;
975
976   struct llist *st;
977   struct i_bitmap *btm;
978
979   int channels,xsize,ysize;
980   i_color cval,val;
981
982   channels = im->channels;
983   xsize    = im->xsize;
984   ysize    = im->ysize;
985
986   btm = btm_new(xsize, ysize);
987   st  = llist_new(100, sizeof(struct stack_element*));
988
989   /* Get the reference color */
990   i_gpix(im, seedx, seedy, &val);
991
992   /* Find the starting span and fill it */
993   ltx = i_lspan(im, seedx, seedy, &val);
994   rtx = i_rspan(im, seedx, seedy, &val);
995   for(tx=ltx; tx<=rtx; tx++) SET(tx, seedy);
996
997   ST_PUSH(ltx, rtx, ltx, rtx, seedy+1,  1);
998   ST_PUSH(ltx, rtx, ltx, rtx, seedy-1, -1);
999
1000   while(st->count) {
1001     /* Stack variables */
1002     int lx,rx;
1003     int dadLx,dadRx;
1004     int y;
1005     int direction;
1006
1007     int x;
1008     int wasIn=0;
1009
1010     ST_POP(); /* sets lx, rx, dadLx, dadRx, y, direction */
1011
1012
1013     if (y<0 || y>ysize-1) continue;
1014     if (bymin > y) bymin=y; /* in the worst case an extra line */
1015     if (bymax < y) bymax=y; 
1016
1017
1018     x = lx+1;
1019     if ( lx >= 0 && (wasIn = INSIDE(lx, y)) ) {
1020       SET(lx, y);
1021       lx--;
1022       while(INSIDE(lx, y) && lx > 0) {
1023         SET(lx,y);
1024         lx--;
1025       }
1026     }
1027
1028     if (bxmin > lx) bxmin = lx;
1029     while(x <= xsize-1) {
1030       /*  printf("x=%d\n",x); */
1031       if (wasIn) {
1032         
1033         if (INSIDE(x, y)) {
1034           /* case 1: was inside, am still inside */
1035           SET(x,y);
1036         } else {
1037           /* case 2: was inside, am no longer inside: just found the
1038              right edge of a span */
1039           ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1040
1041           if (bxmax < x) bxmax = x;
1042           wasIn=0;
1043         }
1044       } else {
1045         if (x > rx) goto EXT;
1046         if (INSIDE(x, y)) {
1047           SET(x, y);
1048           /* case 3: Wasn't inside, am now: just found the start of a new run */
1049           wasIn = 1;
1050             lx = x;
1051         } else {
1052           /* case 4: Wasn't inside, still isn't */
1053         }
1054       }
1055       x++;
1056     }
1057   EXT: /* out of loop */
1058     if (wasIn) {
1059       /* hit an edge of the frame buffer while inside a run */
1060       ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1061       if (bxmax < x) bxmax = x;
1062     }
1063   }
1064
1065   llist_destroy(st);
1066
1067   *bxminp = bxmin;
1068   *bxmaxp = bxmax;
1069   *byminp = bymin;
1070   *bymaxp = bymax;
1071
1072   return btm;
1073 }
1074
1075
1076
1077
1078 undef_int
1079 i_flood_fill(i_img *im, int seedx, int seedy, i_color *dcol) {
1080   int bxmin, bxmax, bymin, bymax;
1081   struct i_bitmap *btm;
1082   int x, y;
1083
1084   i_clear_error();
1085   if (seedx < 0 || seedx >= im->xsize ||
1086       seedy < 0 || seedy >= im->ysize) {
1087     i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
1088     return 0;
1089   }
1090
1091   btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax);
1092
1093   for(y=bymin;y<=bymax;y++)
1094     for(x=bxmin;x<=bxmax;x++)
1095       if (btm_test(btm,x,y)) 
1096         i_ppix(im,x,y,dcol);
1097   btm_destroy(btm);
1098   return 1;
1099 }
1100
1101
1102
1103 undef_int
1104 i_flood_cfill(i_img *im, int seedx, int seedy, i_fill_t *fill) {
1105   int bxmin, bxmax, bymin, bymax;
1106   struct i_bitmap *btm;
1107   int x, y;
1108   int start;
1109
1110   i_clear_error();
1111   
1112   if (seedx < 0 || seedx >= im->xsize ||
1113       seedy < 0 || seedy >= im->ysize) {
1114     i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
1115     return 0;
1116   }
1117
1118   btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax);
1119
1120   if (im->bits == i_8_bits && fill->fill_with_color) {
1121     i_color *line = mymalloc(sizeof(i_color) * (bxmax - bxmin));
1122     i_color *work = NULL;
1123     if (fill->combine)
1124       work = mymalloc(sizeof(i_color) * (bxmax - bxmin));
1125
1126     for(y=bymin; y<=bymax; y++) {
1127       x = bxmin;
1128       while (x < bxmax) {
1129         while (x < bxmax && !btm_test(btm, x, y)) {
1130           ++x;
1131         }
1132         if (btm_test(btm, x, y)) {
1133           start = x;
1134           while (x < bxmax && btm_test(btm, x, y)) {
1135             ++x;
1136           }
1137           if (fill->combine) {
1138             i_glin(im, start, x, y, line);
1139             (fill->fill_with_color)(fill, start, y, x-start, im->channels, 
1140                                     work);
1141             (fill->combine)(line, work, im->channels, x-start);
1142           }
1143           else {
1144             (fill->fill_with_color)(fill, start, y, x-start, im->channels, 
1145                                     line);
1146           }
1147           i_plin(im, start, x, y, line);
1148         }
1149       }
1150     }
1151     myfree(line);
1152     if (work)
1153       myfree(work);
1154   }
1155   else {
1156     i_fcolor *line = mymalloc(sizeof(i_fcolor) * (bxmax - bxmin));
1157     i_fcolor *work = NULL;
1158     if (fill->combinef)
1159       work = mymalloc(sizeof(i_fcolor) * (bxmax - bxmin));
1160     
1161     for(y=bymin;y<=bymax;y++) {
1162       x = bxmin;
1163       while (x < bxmax) {
1164         while (x < bxmax && !btm_test(btm, x, y)) {
1165           ++x;
1166         }
1167         if (btm_test(btm, x, y)) {
1168           start = x;
1169           while (x < bxmax && btm_test(btm, x, y)) {
1170             ++x;
1171           }
1172           if (fill->combinef) {
1173             i_glinf(im, start, x, y, line);
1174             (fill->fill_with_fcolor)(fill, start, y, x-start, im->channels, 
1175                                     work);
1176             (fill->combinef)(line, work, im->channels, x-start);
1177           }
1178           else {
1179             (fill->fill_with_fcolor)(fill, start, y, x-start, im->channels, 
1180                                     line);
1181           }
1182           i_plinf(im, start, x, y, line);
1183         }
1184       }
1185     }
1186     myfree(line);
1187     if (work)
1188       myfree(work);
1189   }
1190
1191   btm_destroy(btm);
1192   return 1;
1193 }