]> git.imager.perl.org - imager.git/blob - draw.c
fix TIFF test count to include the new ifd loop tests
[imager.git] / draw.c
1 #include "imager.h"
2 #include "draw.h"
3 #include "log.h"
4 #include "imageri.h"
5 #include "imrender.h"
6 #include <limits.h>
7
8 int
9 i_ppix_norm(i_img *im, i_img_dim x, i_img_dim y, i_color const *col) {
10   i_color src;
11   i_color work;
12   int dest_alpha;
13   int remains;
14
15   if (!col->channel[3])
16     return 0;
17
18   switch (im->channels) {
19   case 1:
20     work = *col;
21     i_adapt_colors(2, 4, &work, 1);
22     i_gpix(im, x, y, &src);
23     remains = 255 - work.channel[1];
24     src.channel[0] = (src.channel[0] * remains
25                       + work.channel[0] * work.channel[1]) / 255;
26     return i_ppix(im, x, y, &src);
27
28   case 2:
29     work = *col;
30     i_adapt_colors(2, 4, &work, 1);
31     i_gpix(im, x, y, &src);
32     remains = 255 - work.channel[1];
33     dest_alpha = work.channel[1] + remains * src.channel[1] / 255;
34     if (work.channel[1] == 255) {
35       return i_ppix(im, x, y, &work);
36     }
37     else {
38       src.channel[0] = (work.channel[1] * work.channel[0]
39                         + remains * src.channel[0] * src.channel[1] / 255) / dest_alpha;
40       src.channel[1] = dest_alpha;
41       return i_ppix(im, x, y, &src);
42     }
43
44   case 3:
45     work = *col;
46     i_gpix(im, x, y, &src);
47     remains = 255 - work.channel[3];
48     src.channel[0] = (src.channel[0] * remains
49                       + work.channel[0] * work.channel[3]) / 255;
50     src.channel[1] = (src.channel[1] * remains
51                       + work.channel[1] * work.channel[3]) / 255;
52     src.channel[2] = (src.channel[2] * remains
53                       + work.channel[2] * work.channel[3]) / 255;
54     return i_ppix(im, x, y, &src);
55
56   case 4:
57     work = *col;
58     i_gpix(im, x, y, &src);
59     remains = 255 - work.channel[3];
60     dest_alpha = work.channel[3] + remains * src.channel[3] / 255;
61     if (work.channel[3] == 255) {
62       return i_ppix(im, x, y, &work);
63     }
64     else {
65       src.channel[0] = (work.channel[3] * work.channel[0]
66                         + remains * src.channel[0] * src.channel[3] / 255) / dest_alpha;
67       src.channel[1] = (work.channel[3] * work.channel[1]
68                         + remains * src.channel[1] * src.channel[3] / 255) / dest_alpha;
69       src.channel[2] = (work.channel[3] * work.channel[2]
70                         + remains * src.channel[2] * src.channel[3] / 255) / dest_alpha;
71       src.channel[3] = dest_alpha;
72       return i_ppix(im, x, y, &src);
73     }
74   }
75   return 0;
76 }
77
78 static void
79 cfill_from_btm(i_img *im, i_fill_t *fill, struct i_bitmap *btm, 
80                i_img_dim bxmin, i_img_dim bxmax, i_img_dim bymin, i_img_dim bymax);
81
82 void
83 i_mmarray_cr(i_mmarray *ar,i_img_dim l) {
84   i_img_dim i;
85   size_t alloc_size;
86
87   ar->lines=l;
88   alloc_size = sizeof(minmax) * l;
89   /* check for overflow */
90   if (alloc_size / l != sizeof(minmax)) {
91     fprintf(stderr, "overflow calculating memory allocation");
92     exit(3);
93   }
94   ar->data=mymalloc(alloc_size); /* checked 5jul05 tonyc */
95   for(i=0;i<l;i++) { ar->data[i].max=-1; ar->data[i].min=MAXINT; }
96 }
97
98 void
99 i_mmarray_dst(i_mmarray *ar) {
100   ar->lines=0;
101   if (ar->data != NULL) { myfree(ar->data); ar->data=NULL; }
102 }
103
104 void
105 i_mmarray_add(i_mmarray *ar,i_img_dim x,i_img_dim y) {
106   if (y>-1 && y<ar->lines)
107     {
108       if (x<ar->data[y].min) ar->data[y].min=x;
109       if (x>ar->data[y].max) ar->data[y].max=x;
110     }
111 }
112
113 int
114 i_mmarray_gmin(i_mmarray *ar,i_img_dim y) {
115   if (y>-1 && y<ar->lines) return ar->data[y].min;
116   else return -1;
117 }
118
119 int
120 i_mmarray_getm(i_mmarray *ar,i_img_dim y) {
121   if (y>-1 && y<ar->lines) return ar->data[y].max;
122   else return MAXINT;
123 }
124
125 #if 0
126 /* unused? */
127 void
128 i_mmarray_render(i_img *im,i_mmarray *ar,i_color *val) {
129   i_img_dim i,x;
130   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);
131 }
132 #endif
133
134 static
135 void
136 i_arcdraw(i_img_dim x1, i_img_dim y1, i_img_dim x2, i_img_dim y2, i_mmarray *ar) {
137   double alpha;
138   double dsec;
139   i_img_dim temp;
140   alpha=(double)(y2-y1)/(double)(x2-x1);
141   if (fabs(alpha) <= 1) 
142     {
143       if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
144       dsec=y1;
145       while(x1<=x2)
146         {
147           i_mmarray_add(ar,x1,(i_img_dim)(dsec+0.5));
148           dsec+=alpha;
149           x1++;
150         }
151     }
152   else
153     {
154       alpha=1/alpha;
155       if (y2<y1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
156       dsec=x1;
157       while(y1<=y2)
158         {
159           i_mmarray_add(ar,(i_img_dim)(dsec+0.5),y1);
160           dsec+=alpha;
161           y1++;
162         }
163     }
164 }
165
166 void
167 i_mmarray_info(i_mmarray *ar) {
168   i_img_dim i;
169   for(i=0;i<ar->lines;i++)
170   if (ar->data[i].max!=-1)
171     printf("line %"i_DF ": min=%" i_DF ", max=%" i_DF ".\n",
172            i_DFc(i), i_DFc(ar->data[i].min), i_DFc(ar->data[i].max));
173 }
174
175 static void
176 i_arc_minmax(i_int_hlines *hlines,i_img_dim x,i_img_dim y, double rad,float d1,float d2) {
177   i_mmarray dot;
178   double f,fx,fy;
179   i_img_dim x1,y1;
180
181   i_mmarray_cr(&dot, hlines->limit_y);
182
183   x1=(i_img_dim)(x+0.5+rad*cos(d1*PI/180.0));
184   y1=(i_img_dim)(y+0.5+rad*sin(d1*PI/180.0));
185   fx=(float)x1; fy=(float)y1;
186
187   /*  printf("x1: %d.\ny1: %d.\n",x1,y1); */
188   i_arcdraw(x, y, x1, y1, &dot);
189
190   x1=(i_img_dim)(x+0.5+rad*cos(d2*PI/180.0));
191   y1=(i_img_dim)(y+0.5+rad*sin(d2*PI/180.0));
192
193   for(f=d1;f<=d2;f+=0.01)
194     i_mmarray_add(&dot,(i_img_dim)(x+0.5+rad*cos(f*PI/180.0)),(i_img_dim)(y+0.5+rad*sin(f*PI/180.0)));
195   
196   /*  printf("x1: %d.\ny1: %d.\n",x1,y1); */
197   i_arcdraw(x, y, x1, y1, &dot);
198
199   /* render the minmax values onto the hlines */
200   for (y = 0; y < dot.lines; y++) {
201     if (dot.data[y].max!=-1) {
202       i_img_dim minx, width;
203       minx = dot.data[y].min;
204       width = dot.data[y].max - dot.data[y].min + 1;
205       i_int_hlines_add(hlines, y, minx, width);
206     }
207   }
208
209   /*  dot.info(); */
210   i_mmarray_dst(&dot);
211 }
212
213 static void
214 i_arc_hlines(i_int_hlines *hlines,i_img_dim x,i_img_dim y,double rad,float d1,float d2) {
215   if (d1 <= d2) {
216     i_arc_minmax(hlines, x, y, rad, d1, d2);
217   }
218   else {
219     i_arc_minmax(hlines, x, y, rad, d1, 360);
220     i_arc_minmax(hlines, x, y, rad, 0, d2);
221   }
222 }
223
224 /*
225 =item i_arc(im, x, y, rad, d1, d2, color)
226
227 =category Drawing
228 =synopsis i_arc(im, 50, 50, 20, 45, 135, &color);
229
230 Fills an arc centered at (x,y) with radius I<rad> covering the range
231 of angles in degrees from d1 to d2, with the color.
232
233 =cut
234 */
235
236 void
237 i_arc(i_img *im, i_img_dim x, i_img_dim y,double rad,double d1,double d2,const i_color *val) {
238   i_int_hlines hlines;
239
240   i_int_init_hlines_img(&hlines, im);
241
242   i_arc_hlines(&hlines, x, y, rad, d1, d2);
243
244   i_int_hlines_fill_color(im, &hlines, val);
245
246   i_int_hlines_destroy(&hlines);
247 }
248
249 /*
250 =item i_arc_cfill(im, x, y, rad, d1, d2, fill)
251
252 =category Drawing
253 =synopsis i_arc_cfill(im, 50, 50, 35, 90, 135, fill);
254
255 Fills an arc centered at (x,y) with radius I<rad> covering the range
256 of angles in degrees from d1 to d2, with the fill object.
257
258 =cut
259 */
260
261 #define MIN_CIRCLE_STEPS 8
262 #define MAX_CIRCLE_STEPS 360
263
264 void
265 i_arc_cfill(i_img *im, i_img_dim x, i_img_dim y,double rad,double d1,double d2,i_fill_t *fill) {
266   i_int_hlines hlines;
267
268   i_int_init_hlines_img(&hlines, im);
269
270   i_arc_hlines(&hlines, x, y, rad, d1, d2);
271
272   i_int_hlines_fill_fill(im, &hlines, fill);
273
274   i_int_hlines_destroy(&hlines);
275 }
276
277 static void
278 arc_poly(int *count, double **xvals, double **yvals,
279          double x, double y, double rad, double d1, double d2) {
280   double d1_rad, d2_rad;
281   double circum;
282   i_img_dim steps, point_count;
283   double angle_inc;
284
285   /* normalize the angles */
286   d1 = fmod(d1, 360);
287   if (d1 == 0) {
288     if (d2 >= 360) { /* default is 361 */
289       d2 = 360;
290     }
291     else {
292       d2 = fmod(d2, 360);
293       if (d2 < d1)
294         d2 += 360;
295     }
296   }
297   else {
298     d2 = fmod(d2, 360);
299     if (d2 < d1)
300       d2 += 360;
301   }
302   d1_rad = d1 * PI / 180;
303   d2_rad = d2 * PI / 180;
304
305   /* how many segments for the curved part? 
306      we do a maximum of one per degree, with a minimum of 8/circle
307      we try to aim at having about one segment per 2 pixels
308      Work it out per circle to get a step size.
309
310      I was originally making steps = circum/2 but that looked horrible.
311
312      I think there might be an issue in the polygon filler.
313   */
314   circum = 2 * PI * rad;
315   steps = circum;
316   if (steps > MAX_CIRCLE_STEPS)
317     steps = MAX_CIRCLE_STEPS;
318   else if (steps < MIN_CIRCLE_STEPS)
319     steps = MIN_CIRCLE_STEPS;
320
321   angle_inc = 2 * PI / steps;
322
323   point_count = steps + 5; /* rough */
324   /* point_count is always relatively small, so allocation won't overflow */
325   *xvals = mymalloc(point_count * sizeof(double)); /* checked 17feb2005 tonyc */
326   *yvals = mymalloc(point_count * sizeof(double)); /* checked 17feb2005 tonyc */
327
328   /* from centre to edge at d1 */
329   (*xvals)[0] = x;
330   (*yvals)[0] = y;
331   (*xvals)[1] = x + rad * cos(d1_rad);
332   (*yvals)[1] = y + rad * sin(d1_rad);
333   *count = 2;
334
335   /* step around the curve */
336   while (d1_rad < d2_rad) {
337     (*xvals)[*count] = x + rad * cos(d1_rad);
338     (*yvals)[*count] = y + rad * sin(d1_rad);
339     ++*count;
340     d1_rad += angle_inc;
341   }
342
343   /* finish off the curve */
344   (*xvals)[*count] = x + rad * cos(d2_rad);
345   (*yvals)[*count] = y + rad * sin(d2_rad);
346   ++*count;
347 }
348
349 /*
350 =item i_arc_aa(im, x, y, rad, d1, d2, color)
351
352 =category Drawing
353 =synopsis i_arc_aa(im, 50, 50, 35, 90, 135, &color);
354
355 Anti-alias fills an arc centered at (x,y) with radius I<rad> covering
356 the range of angles in degrees from d1 to d2, with the color.
357
358 =cut
359 */
360
361 void
362 i_arc_aa(i_img *im, double x, double y, double rad, double d1, double d2,
363          const i_color *val) {
364   double *xvals, *yvals;
365   int count;
366
367   arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
368
369   i_poly_aa(im, count, xvals, yvals, val);
370
371   myfree(xvals);
372   myfree(yvals);
373 }
374
375 /*
376 =item i_arc_aa_cfill(im, x, y, rad, d1, d2, fill)
377
378 =category Drawing
379 =synopsis i_arc_aa_cfill(im, 50, 50, 35, 90, 135, fill);
380
381 Anti-alias fills an arc centered at (x,y) with radius I<rad> covering
382 the range of angles in degrees from d1 to d2, with the fill object.
383
384 =cut
385 */
386
387 void
388 i_arc_aa_cfill(i_img *im, double x, double y, double rad, double d1, double d2,
389                i_fill_t *fill) {
390   double *xvals, *yvals;
391   int count;
392
393   arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
394
395   i_poly_aa_cfill(im, count, xvals, yvals, fill);
396
397   myfree(xvals);
398   myfree(yvals);
399 }
400
401 /* Temporary AA HACK */
402
403
404 typedef i_img_dim frac;
405 static  frac float_to_frac(double x) { return (frac)(0.5+x*16.0); }
406
407 static 
408 void
409 polar_to_plane(double cx, double cy, float angle, double radius, frac *x, frac *y) {
410   *x = float_to_frac(cx+radius*cos(angle));
411   *y = float_to_frac(cy+radius*sin(angle));
412 }
413
414 static
415 void
416 make_minmax_list(i_mmarray *dot, double x, double y, double radius) {
417   float angle = 0.0;
418   float astep = radius>0.1 ? .5/radius : 10;
419   frac cx, cy, lx, ly, sx, sy;
420
421   mm_log((1, "make_minmax_list(dot %p, x %.2f, y %.2f, radius %.2f)\n", dot, x, y, radius));
422
423   polar_to_plane(x, y, angle, radius, &sx, &sy);
424   
425   for(angle = 0.0; angle<361; angle +=astep) {
426     lx = sx; ly = sy;
427     polar_to_plane(x, y, angle, radius, &cx, &cy);
428     sx = cx; sy = cy;
429
430     if (fabs(cx-lx) > fabs(cy-ly)) {
431       int ccx, ccy;
432       if (lx>cx) { 
433         ccx = lx; lx = cx; cx = ccx; 
434         ccy = ly; ly = cy; cy = ccy; 
435       }
436
437       for(ccx=lx; ccx<=cx; ccx++) {
438         ccy = ly + ((cy-ly)*(ccx-lx))/(cx-lx);
439         i_mmarray_add(dot, ccx, ccy);
440       }
441     } else {
442       int ccx, ccy;
443
444       if (ly>cy) { 
445         ccy = ly; ly = cy; cy = ccy; 
446         ccx = lx; lx = cx; cx = ccx; 
447       }
448       
449       for(ccy=ly; ccy<=cy; ccy++) {
450         if (cy-ly) ccx = lx + ((cx-lx)*(ccy-ly))/(cy-ly); else ccx = lx;
451         i_mmarray_add(dot, ccx, ccy);
452       }
453     }
454   }
455 }
456
457 /* Get the number of subpixels covered */
458
459 static
460 int
461 i_pixel_coverage(i_mmarray *dot, i_img_dim x, i_img_dim y) {
462   frac minx = x*16;
463   frac maxx = minx+15;
464   frac cy;
465   int cnt = 0;
466   
467   for(cy=y*16; cy<(y+1)*16; cy++) {
468     frac tmin = dot->data[cy].min;
469     frac tmax = dot->data[cy].max;
470
471     if (tmax == -1 || tmin > maxx || tmax < minx) continue;
472     
473     if (tmin < minx) tmin = minx;
474     if (tmax > maxx) tmax = maxx;
475     
476     cnt+=1+tmax-tmin;
477   }
478   return cnt;
479 }
480
481 /*
482 =item i_circle_aa(im, x, y, rad, color)
483
484 =category Drawing
485 =synopsis i_circle_aa(im, 50, 50, 45, &color);
486
487 Anti-alias fills a circle centered at (x,y) for radius I<rad> with
488 color.
489
490 =cut
491 */
492 void
493 i_circle_aa(i_img *im, double x, double y, double rad, const i_color *val) {
494   i_mmarray dot;
495   i_color temp;
496   i_img_dim ly;
497
498   mm_log((1, "i_circle_aa(im %p, centre(" i_DFp "), rad %.2f, val %p)\n",
499           im, i_DFcp(x, y), rad, val));
500
501   i_mmarray_cr(&dot,16*im->ysize);
502   make_minmax_list(&dot, x, y, rad);
503
504   for(ly = 0; ly<im->ysize; ly++) {
505     int ix, cy, minx = INT_MAX, maxx = INT_MIN;
506
507     /* Find the left/rightmost set subpixels */
508     for(cy = 0; cy<16; cy++) {
509       frac tmin = dot.data[ly*16+cy].min;
510       frac tmax = dot.data[ly*16+cy].max;
511       if (tmax == -1) continue;
512
513       if (minx > tmin) minx = tmin;
514       if (maxx < tmax) maxx = tmax;
515     }
516
517     if (maxx == INT_MIN) continue; /* no work to be done for this row of pixels */
518
519     minx /= 16;
520     maxx /= 16;
521     for(ix=minx; ix<=maxx; ix++) {
522       int cnt = i_pixel_coverage(&dot, ix, ly);
523       if (cnt>255) cnt = 255;
524       if (cnt) { /* should never be true */
525         int ch;
526         float ratio = (float)cnt/255.0;
527         i_gpix(im, ix, ly, &temp);
528         for(ch=0;ch<im->channels; ch++) temp.channel[ch] = (unsigned char)((float)val->channel[ch]*ratio + (float)temp.channel[ch]*(1.0-ratio));
529         i_ppix(im, ix, ly, &temp);
530       }
531     }
532   }
533   i_mmarray_dst(&dot);
534 }
535
536 /*
537 =item i_circle_out(im, x, y, r, col)
538
539 =category Drawing
540 =synopsis i_circle_out(im, 50, 50, 45, &color);
541
542 Draw a circle outline centered at (x,y) with radius r,
543 non-anti-aliased.
544
545 Parameters:
546
547 =over
548
549 =item *
550
551 (x, y) - the center of the circle
552
553 =item *
554
555 r - the radius of the circle in pixels, must be non-negative
556
557 =back
558
559 Returns non-zero on success.
560
561 Implementation:
562
563 =cut
564 */
565
566 int
567 i_circle_out(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r,
568              const i_color *col) {
569   i_img_dim x, y;
570   i_img_dim dx, dy;
571   int error;
572
573   i_clear_error();
574
575   if (r < 0) {
576     i_push_error(0, "circle: radius must be non-negative");
577     return 0;
578   }
579
580   i_ppix(im, xc+r, yc, col);
581   i_ppix(im, xc-r, yc, col);
582   i_ppix(im, xc, yc+r, col);
583   i_ppix(im, xc, yc-r, col);
584
585   x = 0;
586   y = r;
587   dx = 1;
588   dy = -2 * r;
589   error = 1 - r;
590   while (x < y) {
591     if (error >= 0) {
592       --y;
593       dy += 2;
594       error += dy;
595     }
596     ++x;
597     dx += 2;
598     error += dx;
599
600     i_ppix(im, xc + x, yc + y, col);
601     i_ppix(im, xc + x, yc - y, col);
602     i_ppix(im, xc - x, yc + y, col);
603     i_ppix(im, xc - x, yc - y, col);
604     if (x != y) {
605       i_ppix(im, xc + y, yc + x, col);
606       i_ppix(im, xc + y, yc - x, col);
607       i_ppix(im, xc - y, yc + x, col);
608       i_ppix(im, xc - y, yc - x, col);
609     }
610   }
611
612   return 1;
613 }
614
615 /*
616 =item arc_seg(angle)
617
618 Convert an angle in degrees into an angle measure we can generate
619 simply from the numbers we have when drawing the circle.
620
621 =back
622 */
623
624 static i_img_dim
625 arc_seg(double angle, int scale) {
626   i_img_dim seg = (angle + 45) / 90;
627   double remains = angle - seg * 90; /* should be in the range [-45,45] */
628
629   while (seg > 4)
630     seg -= 4;
631   if (seg == 4 && remains > 0)
632     seg = 0;
633
634   return scale * (seg * 2 + sin(remains * PI/180));
635 }
636
637 /*
638 =item i_arc_out(im, x, y, r, d1, d2, col)
639
640 =category Drawing
641 =synopsis i_arc_out(im, 50, 50, 45, 45, 135, &color);
642
643 Draw an arc outline centered at (x,y) with radius r, non-anti-aliased
644 over the angle range d1 through d2 degrees.
645
646 Parameters:
647
648 =over
649
650 =item *
651
652 (x, y) - the center of the circle
653
654 =item *
655
656 r - the radius of the circle in pixels, must be non-negative
657
658 =item *
659
660 d1, d2 - the range of angles to draw the arc over, in degrees.
661
662 =back
663
664 Returns non-zero on success.
665
666 Implementation:
667
668 =cut
669 */
670
671 int
672 i_arc_out(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r,
673           double d1, double d2, const i_color *col) {
674   i_img_dim x, y;
675   i_img_dim dx, dy;
676   int error;
677   i_img_dim segs[2][2];
678   int seg_count;
679   i_img_dim sin_th;
680   i_img_dim seg_d1, seg_d2;
681   int seg_num;
682   i_img_dim scale = r + 1;
683   i_img_dim seg1 = scale * 2;
684   i_img_dim seg2 = scale * 4;
685   i_img_dim seg3 = scale * 6;
686   i_img_dim seg4 = scale * 8;
687
688   i_clear_error();
689
690   if (r <= 0) {
691     i_push_error(0, "arc: radius must be non-negative");
692     return 0;
693   }
694   if (d1 + 360 <= d2)
695     return i_circle_out(im, xc, yc, r, col);
696
697   if (d1 < 0)
698     d1 += 360 * floor((-d1 + 359) / 360);
699   if (d2 < 0)
700     d2 += 360 * floor((-d2 + 359) / 360);
701   d1 = fmod(d1, 360);
702   d2 = fmod(d2, 360);
703   seg_d1 = arc_seg(d1, scale);
704   seg_d2 = arc_seg(d2, scale);
705   if (seg_d2 < seg_d1) {
706     /* split into two segments */
707     segs[0][0] = 0;
708     segs[0][1] = seg_d2;
709     segs[1][0] = seg_d1;
710     segs[1][1] = seg4;
711     seg_count = 2;
712   }
713   else {
714     segs[0][0] = seg_d1;
715     segs[0][1] = seg_d2;
716     seg_count = 1;
717   }
718
719   for (seg_num = 0; seg_num < seg_count; ++seg_num) {
720     i_img_dim seg_start = segs[seg_num][0];
721     i_img_dim seg_end = segs[seg_num][1];
722     if (seg_start == 0)
723       i_ppix(im, xc+r, yc, col);
724     if (seg_start <= seg1 && seg_end >= seg1)
725       i_ppix(im, xc, yc+r, col);
726     if (seg_start <= seg2 && seg_end >= seg2)
727       i_ppix(im, xc-r, yc, col);
728     if (seg_start <= seg3 && seg_end >= seg3)
729       i_ppix(im, xc, yc-r, col);
730
731     y = 0;
732     x = r;
733     dy = 1;
734     dx = -2 * r;
735     error = 1 - r;
736     while (y < x) {
737       if (error >= 0) {
738         --x;
739         dx += 2;
740         error += dx;
741       }
742       ++y;
743       dy += 2;
744       error += dy;
745       
746       sin_th = y;
747       if (seg_start <= sin_th && seg_end >= sin_th)
748         i_ppix(im, xc + x, yc + y, col);
749       if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
750         i_ppix(im, xc + y, yc + x, col);
751
752       if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
753         i_ppix(im, xc - y, yc + x, col);
754       if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
755         i_ppix(im, xc - x, yc + y, col);
756       
757       if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
758         i_ppix(im, xc - x, yc - y, col);
759       if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
760         i_ppix(im, xc - y, yc - x, col);
761
762       if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
763         i_ppix(im, xc + y, yc - x, col);
764       if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
765         i_ppix(im, xc + x, yc - y, col);
766     }
767   }
768
769   return 1;
770 }
771
772 static double
773 cover(i_img_dim r, i_img_dim j) {
774   double rjsqrt = sqrt(r*r - j*j);
775
776   return ceil(rjsqrt) - rjsqrt;
777 }
778
779 /*
780 =item i_circle_out_aa(im, xc, yc, r, col)
781
782 =synopsis i_circle_out_aa(im, 50, 50, 45, &color);
783
784 Draw a circle outline centered at (x,y) with radius r, anti-aliased.
785
786 Parameters:
787
788 =over
789
790 =item *
791
792 (xc, yc) - the center of the circle
793
794 =item *
795
796 r - the radius of the circle in pixels, must be non-negative
797
798 =item *
799
800 col - an i_color for the color to draw in.
801
802 =back
803
804 Returns non-zero on success.
805
806 =cut
807
808 Based on "Fast Anti-Aliased Circle Generation", Xiaolin Wu, Graphics
809 Gems.
810
811 I use floating point for I<D> since for large circles the precision of
812 a [0,255] value isn't sufficient when approaching the end of the
813 octant.
814
815 */
816
817 int
818 i_circle_out_aa(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r, const i_color *col) {
819   i_img_dim i, j;
820   double t;
821   i_color workc = *col;
822   int orig_alpha = col->channel[3];
823
824   i_clear_error();
825   if (r <= 0) {
826     i_push_error(0, "arc: radius must be non-negative");
827     return 0;
828   }
829   i = r;
830   j = 0;
831   t = 0;
832   i_ppix_norm(im, xc+i, yc+j, col);
833   i_ppix_norm(im, xc-i, yc+j, col);
834   i_ppix_norm(im, xc+j, yc+i, col);
835   i_ppix_norm(im, xc+j, yc-i, col);
836
837   while (i > j+1) {
838     double d;
839     int cv, inv_cv;
840     j++;
841     d = cover(r, j);
842     cv = (int)(d * 255 + 0.5);
843     inv_cv = 255-cv;
844     if (d < t) {
845       --i;
846     }
847     if (inv_cv) {
848       workc.channel[3] = orig_alpha * inv_cv / 255;
849       i_ppix_norm(im, xc+i, yc+j, &workc);
850       i_ppix_norm(im, xc-i, yc+j, &workc);
851       i_ppix_norm(im, xc+i, yc-j, &workc);
852       i_ppix_norm(im, xc-i, yc-j, &workc);
853
854       if (i != j) {
855         i_ppix_norm(im, xc+j, yc+i, &workc);
856         i_ppix_norm(im, xc-j, yc+i, &workc);
857         i_ppix_norm(im, xc+j, yc-i, &workc);
858         i_ppix_norm(im, xc-j, yc-i, &workc);
859       }
860     }
861     if (cv && i > j) {
862       workc.channel[3] = orig_alpha * cv / 255;
863       i_ppix_norm(im, xc+i-1, yc+j, &workc);
864       i_ppix_norm(im, xc-i+1, yc+j, &workc);
865       i_ppix_norm(im, xc+i-1, yc-j, &workc);
866       i_ppix_norm(im, xc-i+1, yc-j, &workc);
867
868       if (j != i-1) {
869         i_ppix_norm(im, xc+j, yc+i-1, &workc);
870         i_ppix_norm(im, xc-j, yc+i-1, &workc);
871         i_ppix_norm(im, xc+j, yc-i+1, &workc);
872         i_ppix_norm(im, xc-j, yc-i+1, &workc);
873       }
874     }
875     t = d;
876   }
877
878   return 1;
879 }
880
881 /*
882 =item i_arc_out_aa(im, xc, yc, r, d1, d2, col)
883
884 =synopsis i_arc_out_aa(im, 50, 50, 45, 45, 125, &color);
885
886 Draw a circle arc outline centered at (x,y) with radius r, from angle
887 d1 degrees through angle d2 degrees, anti-aliased.
888
889 Parameters:
890
891 =over
892
893 =item *
894
895 (xc, yc) - the center of the circle
896
897 =item *
898
899 r - the radius of the circle in pixels, must be non-negative
900
901 =item *
902
903 d1, d2 - the range of angle in degrees to draw the arc through.  If
904 d2-d1 >= 360 a full circle is drawn.
905
906 =back
907
908 Returns non-zero on success.
909
910 =cut
911
912 Based on "Fast Anti-Aliased Circle Generation", Xiaolin Wu, Graphics
913 Gems.
914
915 */
916
917 int
918 i_arc_out_aa(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r, double d1, double d2, const i_color *col) {
919   i_img_dim i, j;
920   double t;
921   i_color workc = *col;
922   i_img_dim segs[2][2];
923   int seg_count;
924   i_img_dim sin_th;
925   i_img_dim seg_d1, seg_d2;
926   int seg_num;
927   int orig_alpha = col->channel[3];
928   i_img_dim scale = r + 1;
929   i_img_dim seg1 = scale * 2;
930   i_img_dim seg2 = scale * 4;
931   i_img_dim seg3 = scale * 6;
932   i_img_dim seg4 = scale * 8;
933
934   i_clear_error();
935   if (r <= 0) {
936     i_push_error(0, "arc: radius must be non-negative");
937     return 0;
938   }
939   if (d1 + 360 <= d2)
940     return i_circle_out_aa(im, xc, yc, r, col);
941
942   if (d1 < 0)
943     d1 += 360 * floor((-d1 + 359) / 360);
944   if (d2 < 0)
945     d2 += 360 * floor((-d2 + 359) / 360);
946   d1 = fmod(d1, 360);
947   d2 = fmod(d2, 360);
948   seg_d1 = arc_seg(d1, scale);
949   seg_d2 = arc_seg(d2, scale);
950   if (seg_d2 < seg_d1) {
951     /* split into two segments */
952     segs[0][0] = 0;
953     segs[0][1] = seg_d2;
954     segs[1][0] = seg_d1;
955     segs[1][1] = seg4;
956     seg_count = 2;
957   }
958   else {
959     segs[0][0] = seg_d1;
960     segs[0][1] = seg_d2;
961     seg_count = 1;
962   }
963
964   for (seg_num = 0; seg_num < seg_count; ++seg_num) {
965     i_img_dim seg_start = segs[seg_num][0];
966     i_img_dim seg_end = segs[seg_num][1];
967
968     i = r;
969     j = 0;
970     t = 0;
971
972     if (seg_start == 0)
973       i_ppix_norm(im, xc+i, yc+j, col);
974     if (seg_start <= seg1 && seg_end >= seg1)
975       i_ppix_norm(im, xc+j, yc+i, col);
976     if (seg_start <= seg2 && seg_end >= seg2)
977       i_ppix_norm(im, xc-i, yc+j, col);
978     if (seg_start <= seg3 && seg_end >= seg3)
979       i_ppix_norm(im, xc+j, yc-i, col);
980     
981     while (i > j+1) {
982       int cv, inv_cv;
983       double d;
984       j++;
985       d = cover(r, j);
986       cv = (int)(d * 255 + 0.5);
987       inv_cv = 255-cv;
988       if (d < t) {
989         --i;
990       }
991       sin_th = j;
992       if (inv_cv) {
993         workc.channel[3] = orig_alpha * inv_cv / 255;
994
995         if (seg_start <= sin_th && seg_end >= sin_th)
996           i_ppix_norm(im, xc+i, yc+j, &workc);
997         if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
998           i_ppix_norm(im, xc-i, yc+j, &workc);
999         if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
1000           i_ppix_norm(im, xc+i, yc-j, &workc);
1001         if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
1002           i_ppix_norm(im, xc-i, yc-j, &workc);
1003         
1004         if (i != j) {
1005           if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
1006             i_ppix_norm(im, xc+j, yc+i, &workc);
1007           if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
1008             i_ppix_norm(im, xc-j, yc+i, &workc);
1009           if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
1010             i_ppix_norm(im, xc+j, yc-i, &workc);
1011           if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
1012             i_ppix_norm(im, xc-j, yc-i, &workc);
1013         }
1014       }
1015       if (cv && i > j) {
1016         workc.channel[3] = orig_alpha * cv / 255;
1017         if (seg_start <= sin_th && seg_end >= sin_th)
1018           i_ppix_norm(im, xc+i-1, yc+j, &workc);
1019         if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
1020           i_ppix_norm(im, xc-i+1, yc+j, &workc);
1021         if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
1022           i_ppix_norm(im, xc+i-1, yc-j, &workc);
1023         if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
1024           i_ppix_norm(im, xc-i+1, yc-j, &workc);
1025         
1026         if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
1027           i_ppix_norm(im, xc+j, yc+i-1, &workc);
1028         if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
1029           i_ppix_norm(im, xc-j, yc+i-1, &workc);
1030         if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
1031           i_ppix_norm(im, xc+j, yc-i+1, &workc);
1032         if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
1033           i_ppix_norm(im, xc-j, yc-i+1, &workc);
1034       }
1035       t = d;
1036     }
1037   }
1038
1039   return 1;
1040 }
1041
1042 /*
1043 =item i_box(im, x1, y1, x2, y2, color)
1044
1045 =category Drawing
1046 =synopsis i_box(im, 0, 0, im->xsize-1, im->ysize-1, &color).
1047
1048 Outlines the box from (x1,y1) to (x2,y2) inclusive with I<color>.
1049
1050 =cut
1051 */
1052
1053 void
1054 i_box(i_img *im,i_img_dim x1,i_img_dim y1,i_img_dim x2,i_img_dim y2,const i_color *val) {
1055   i_img_dim x,y;
1056   mm_log((1,"i_box(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
1057           im, i_DFcp(x1,y1), i_DFcp(x2,y2), val));
1058   for(x=x1;x<x2+1;x++) {
1059     i_ppix(im,x,y1,val);
1060     i_ppix(im,x,y2,val);
1061   }
1062   for(y=y1;y<y2+1;y++) {
1063     i_ppix(im,x1,y,val);
1064     i_ppix(im,x2,y,val);
1065   }
1066 }
1067
1068 /*
1069 =item i_box_filled(im, x1, y1, x2, y2, color)
1070
1071 =category Drawing
1072 =synopsis i_box_filled(im, 0, 0, im->xsize-1, im->ysize-1, &color);
1073
1074 Fills the box from (x1,y1) to (x2,y2) inclusive with color.
1075
1076 =cut
1077 */
1078
1079 void
1080 i_box_filled(i_img *im,i_img_dim x1,i_img_dim y1,i_img_dim x2,i_img_dim y2, const i_color *val) {
1081   i_img_dim x, y, width;
1082   i_palidx index;
1083
1084   mm_log((1,"i_box_filled(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
1085           im, i_DFcp(x1, y1), i_DFcp(x2,y2) ,val));
1086
1087   if (x1 > x2 || y1 > y2
1088       || x2 < 0 || y2 < 0
1089       || x1 >= im->xsize || y1 > im->ysize)
1090     return;
1091
1092   if (x1 < 0)
1093     x1 = 0;
1094   if (x2 >= im->xsize)
1095     x2 = im->xsize - 1;
1096   if (y1 < 0)
1097     y1 = 0;
1098   if (y2 >= im->ysize)
1099     y2 = im->ysize - 1;
1100
1101   width = x2 - x1 + 1;
1102
1103   if (im->type == i_palette_type
1104       && i_findcolor(im, val, &index)) {
1105     i_palidx *line = mymalloc(sizeof(i_palidx) * width);
1106
1107     for (x = 0; x < width; ++x)
1108       line[x] = index;
1109
1110     for (y = y1; y <= y2; ++y)
1111       i_ppal(im, x1, x2+1, y, line);
1112
1113     myfree(line);
1114   }
1115   else {
1116     i_color *line = mymalloc(sizeof(i_color) * width);
1117
1118     for (x = 0; x < width; ++x)
1119       line[x] = *val;
1120
1121     for (y = y1; y <= y2; ++y)
1122       i_plin(im, x1, x2+1, y, line);
1123
1124     myfree(line);
1125   }
1126 }
1127
1128 /*
1129 =item i_box_filledf(im, x1, y1, x2, y2, color)
1130
1131 =category Drawing
1132 =synopsis i_box_filledf(im, 0, 0, im->xsize-1, im->ysize-1, &fcolor);
1133
1134 Fills the box from (x1,y1) to (x2,y2) inclusive with a floating point
1135 color.
1136
1137 =cut
1138 */
1139
1140 int
1141 i_box_filledf(i_img *im,i_img_dim x1,i_img_dim y1,i_img_dim x2,i_img_dim y2, const i_fcolor *val) {
1142   i_img_dim x, y, width;
1143
1144   mm_log((1,"i_box_filledf(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
1145           im, i_DFcp(x1, y1), i_DFcp(x2, y2), val));
1146
1147   if (x1 > x2 || y1 > y2
1148       || x2 < 0 || y2 < 0
1149       || x1 >= im->xsize || y1 > im->ysize)
1150     return 0;
1151
1152   if (x1 < 0)
1153     x1 = 0;
1154   if (x2 >= im->xsize)
1155     x2 = im->xsize - 1;
1156   if (y1 < 0)
1157     y1 = 0;
1158   if (y2 >= im->ysize)
1159     y2 = im->ysize - 1;
1160
1161   width = x2 - x1 + 1;
1162
1163   if (im->bits <= 8) {
1164     i_color c;
1165     c.rgba.r = SampleFTo8(val->rgba.r);
1166     c.rgba.g = SampleFTo8(val->rgba.g);
1167     c.rgba.b = SampleFTo8(val->rgba.b);
1168     c.rgba.a = SampleFTo8(val->rgba.a);
1169
1170     i_box_filled(im, x1, y1, x2, y2, &c);
1171   }
1172   else {
1173     i_fcolor *line = mymalloc(sizeof(i_fcolor) * width);
1174     
1175     for (x = 0; x < width; ++x)
1176       line[x] = *val;
1177     
1178     for (y = y1; y <= y2; ++y)
1179       i_plinf(im, x1, x2+1, y, line);
1180     
1181     myfree(line);
1182   }
1183   
1184   return 1;
1185 }
1186
1187 /*
1188 =item i_box_cfill(im, x1, y1, x2, y2, fill)
1189
1190 =category Drawing
1191 =synopsis i_box_cfill(im, 0, 0, im->xsize-1, im->ysize-1, fill);
1192
1193 Fills the box from (x1,y1) to (x2,y2) inclusive with fill.
1194
1195 =cut
1196 */
1197
1198 void
1199 i_box_cfill(i_img *im,i_img_dim x1,i_img_dim y1,i_img_dim x2,i_img_dim y2,i_fill_t *fill) {
1200   i_render r;
1201
1202   mm_log((1,"i_box_cfill(im* %p, p1(" i_DFp "), p2(" i_DFp "), fill %p)\n",
1203           im, i_DFcp(x1, y1), i_DFcp(x2,y2), fill));
1204
1205   ++x2;
1206   if (x1 < 0)
1207     x1 = 0;
1208   if (y1 < 0) 
1209     y1 = 0;
1210   if (x2 > im->xsize) 
1211     x2 = im->xsize;
1212   if (y2 >= im->ysize)
1213     y2 = im->ysize-1;
1214   if (x1 >= x2 || y1 > y2)
1215     return;
1216
1217   i_render_init(&r, im, x2-x1);
1218   while (y1 <= y2) {
1219     i_render_fill(&r, x1, y1, x2-x1, NULL, fill);
1220     ++y1;
1221   }
1222   i_render_done(&r);
1223 }
1224
1225 /* 
1226 =item i_line(C<im>, C<x1>, C<y1>, C<x2>, C<y2>, C<color>, C<endp>)
1227
1228 =category Drawing
1229
1230 =for stopwords Bresenham's
1231
1232 Draw a line to image using Bresenham's line drawing algorithm
1233
1234    im    - image to draw to
1235    x1    - starting x coordinate
1236    y1    - starting x coordinate
1237    x2    - starting x coordinate
1238    y2    - starting x coordinate
1239    color - color to write to image
1240    endp  - endpoint flag (boolean)
1241
1242 =cut
1243 */
1244
1245 void
1246 i_line(i_img *im, i_img_dim x1, i_img_dim y1, i_img_dim x2, i_img_dim y2, const i_color *val, int endp) {
1247   i_img_dim x, y;
1248   i_img_dim dx, dy;
1249   i_img_dim p;
1250
1251   dx = x2 - x1;
1252   dy = y2 - y1;
1253
1254
1255   /* choose variable to iterate on */
1256   if (i_abs(dx) > i_abs(dy)) {
1257     i_img_dim dx2, dy2, cpy;
1258
1259     /* sort by x */
1260     if (x1 > x2) {
1261       i_img_dim t;
1262       t = x1; x1 = x2; x2 = t;
1263       t = y1; y1 = y2; y2 = t;
1264     }
1265     
1266     dx = i_abs(dx);
1267     dx2 = dx*2;
1268     dy = y2 - y1;
1269
1270     if (dy<0) {
1271       dy = -dy;
1272       cpy = -1;
1273     } else {
1274       cpy = 1;
1275     }
1276     dy2 = dy*2;
1277     p = dy2 - dx;
1278
1279     
1280     y = y1;
1281     for(x=x1; x<x2-1; x++) {
1282       if (p<0) {
1283         p += dy2;
1284       } else {
1285         y += cpy;
1286         p += dy2-dx2;
1287       }
1288       i_ppix(im, x+1, y, val);
1289     }
1290   } else {
1291     i_img_dim dy2, dx2, cpx;
1292
1293     /* sort bx y */
1294     if (y1 > y2) {
1295       i_img_dim t;
1296       t = x1; x1 = x2; x2 = t;
1297       t = y1; y1 = y2; y2 = t;
1298     }
1299     
1300     dy = i_abs(dy);
1301     dx = x2 - x1;
1302     dy2 = dy*2;
1303
1304     if (dx<0) {
1305       dx = -dx;
1306       cpx = -1;
1307     } else {
1308       cpx = 1;
1309     }
1310     dx2 = dx*2;
1311     p = dx2 - dy;
1312
1313     x = x1;
1314     
1315     for(y=y1; y<y2-1; y++) {
1316       if (p<0) {
1317         p  += dx2;
1318       } else {
1319         x += cpx;
1320         p += dx2-dy2;
1321       }
1322       i_ppix(im, x, y+1, val);
1323     }
1324   }
1325   if (endp) {
1326     i_ppix(im, x1, y1, val);
1327     i_ppix(im, x2, y2, val);
1328   } else {
1329     if (x1 != x2 || y1 != y2) 
1330       i_ppix(im, x1, y1, val);
1331   }
1332 }
1333
1334
1335 void
1336 i_line_dda(i_img *im, i_img_dim x1, i_img_dim y1, i_img_dim x2, i_img_dim y2, i_color *val) {
1337
1338   double dy;
1339   i_img_dim x;
1340   
1341   for(x=x1; x<=x2; x++) {
1342     dy = y1+ (x-x1)/(double)(x2-x1)*(y2-y1);
1343     i_ppix(im, x, (i_img_dim)(dy+0.5), val);
1344   }
1345 }
1346
1347 /*
1348 =item i_line_aa(C<im>, C<x1>, C<x2>, C<y1>, C<y2>, C<color>, C<endp>)
1349
1350 =category Drawing
1351
1352 Anti-alias draws a line from (x1,y1) to (x2, y2) in color.
1353
1354 The point (x2, y2) is drawn only if C<endp> is set.
1355
1356 =cut
1357 */
1358
1359 void
1360 i_line_aa(i_img *im, i_img_dim x1, i_img_dim y1, i_img_dim x2, i_img_dim y2, const i_color *val, int endp) {
1361   i_img_dim x, y;
1362   i_img_dim dx, dy;
1363   i_img_dim p;
1364
1365   dx = x2 - x1;
1366   dy = y2 - y1;
1367
1368   /* choose variable to iterate on */
1369   if (i_abs(dx) > i_abs(dy)) {
1370     i_img_dim dx2, dy2, cpy;
1371     
1372     /* sort by x */
1373     if (x1 > x2) {
1374       i_img_dim t;
1375       t = x1; x1 = x2; x2 = t;
1376       t = y1; y1 = y2; y2 = t;
1377     }
1378     
1379     dx = i_abs(dx);
1380     dx2 = dx*2;
1381     dy = y2 - y1;
1382
1383     if (dy<0) {
1384       dy = -dy;
1385       cpy = -1;
1386     } else {
1387       cpy = 1;
1388     }
1389     dy2 = dy*2;
1390     p = dy2 - dx2; /* this has to be like this for AA */
1391     
1392     y = y1;
1393
1394     for(x=x1; x<x2-1; x++) {
1395       int ch;
1396       i_color tval;
1397       double t = (dy) ? -(float)(p)/(float)(dx2) : 1;
1398       double t1, t2;
1399
1400       if (t<0) t = 0;
1401       t1 = 1-t;
1402       t2 = t;
1403
1404       i_gpix(im,x+1,y,&tval);
1405       for(ch=0;ch<im->channels;ch++)
1406         tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
1407       i_ppix(im,x+1,y,&tval);
1408
1409       i_gpix(im,x+1,y+cpy,&tval);
1410       for(ch=0;ch<im->channels;ch++)
1411         tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
1412       i_ppix(im,x+1,y+cpy,&tval);
1413
1414       if (p<0) {
1415         p += dy2;
1416       } else {
1417         y += cpy;
1418         p += dy2-dx2;
1419       }
1420     }
1421   } else {
1422     i_img_dim dy2, dx2, cpx;
1423
1424     /* sort bx y */
1425     if (y1 > y2) {
1426       i_img_dim t;
1427       t = x1; x1 = x2; x2 = t;
1428       t = y1; y1 = y2; y2 = t;
1429     }
1430     
1431     dy = i_abs(dy);
1432     dx = x2 - x1;
1433     dy2 = dy*2;
1434
1435     if (dx<0) {
1436       dx = -dx;
1437       cpx = -1;
1438     } else {
1439       cpx = 1;
1440     }
1441     dx2 = dx*2;
1442     p = dx2 - dy2; /* this has to be like this for AA */
1443
1444     x = x1;
1445     
1446     for(y=y1; y<y2-1; y++) {
1447       int ch;
1448       i_color tval;
1449       double t = (dx) ? -(double)(p)/(double)(dy2) : 1;
1450       double t1, t2;
1451       
1452       if (t<0) t = 0;
1453       t1 = 1-t;
1454       t2 = t;
1455
1456       i_gpix(im,x,y+1,&tval);
1457       for(ch=0;ch<im->channels;ch++)
1458         tval.channel[ch]=(unsigned char)(t1*(double)tval.channel[ch]+t2*(double)val->channel[ch]);
1459       i_ppix(im,x,y+1,&tval);
1460
1461       i_gpix(im,x+cpx,y+1,&tval);
1462       for(ch=0;ch<im->channels;ch++)
1463         tval.channel[ch]=(unsigned char)(t2*(double)tval.channel[ch]+t1*(double)val->channel[ch]);
1464       i_ppix(im,x+cpx,y+1,&tval);
1465
1466       if (p<0) {
1467         p  += dx2;
1468       } else {
1469         x += cpx;
1470         p += dx2-dy2;
1471       }
1472     }
1473   }
1474
1475
1476   if (endp) {
1477     i_ppix(im, x1, y1, val);
1478     i_ppix(im, x2, y2, val);
1479   } else {
1480     if (x1 != x2 || y1 != y2) 
1481       i_ppix(im, x1, y1, val);
1482   }
1483 }
1484
1485
1486
1487 static double
1488 perm(i_img_dim n,i_img_dim k) {
1489   double r;
1490   i_img_dim i;
1491   r=1;
1492   for(i=k+1;i<=n;i++) r*=i;
1493   for(i=1;i<=(n-k);i++) r/=i;
1494   return r;
1495 }
1496
1497
1498 /* Note in calculating t^k*(1-t)^(n-k) 
1499    we can start by using t^0=1 so this simplifies to
1500    t^0*(1-t)^n - we want to multiply that with t/(1-t) each iteration
1501    to get a new level - this may lead to errors who knows lets test it */
1502
1503 void
1504 i_bezier_multi(i_img *im,int l,const double *x,const double *y, const i_color *val) {
1505   double *bzcoef;
1506   double t,cx,cy;
1507   int k,i;
1508   i_img_dim lx = 0,ly = 0;
1509   int n=l-1;
1510   double itr,ccoef;
1511
1512   /* this is the same size as the x and y arrays, so shouldn't overflow */
1513   bzcoef=mymalloc(sizeof(double)*l); /* checked 5jul05 tonyc */
1514   for(k=0;k<l;k++) bzcoef[k]=perm(n,k);
1515   ICL_info(val);
1516
1517
1518   /*  for(k=0;k<l;k++) printf("bzcoef: %d -> %f\n",k,bzcoef[k]); */
1519   i=0;
1520   for(t=0;t<=1;t+=0.005) {
1521     cx=cy=0;
1522     itr=t/(1-t);
1523     ccoef=pow(1-t,n);
1524     for(k=0;k<l;k++) {
1525       /*      cx+=bzcoef[k]*x[k]*pow(t,k)*pow(1-t,n-k); 
1526               cy+=bzcoef[k]*y[k]*pow(t,k)*pow(1-t,n-k);*/
1527
1528       cx+=bzcoef[k]*x[k]*ccoef;
1529       cy+=bzcoef[k]*y[k]*ccoef;
1530       ccoef*=itr;
1531     }
1532     /*    printf("%f -> (%d,%d)\n",t,(int)(0.5+cx),(int)(0.5+cy)); */
1533     if (i++) { 
1534       i_line_aa(im,lx,ly,(i_img_dim)(0.5+cx),(i_img_dim)(0.5+cy),val, 1);
1535     }
1536       /*     i_ppix(im,(i_img_dim)(0.5+cx),(i_img_dim)(0.5+cy),val); */
1537     lx=(i_img_dim)(0.5+cx);
1538     ly=(i_img_dim)(0.5+cy);
1539   }
1540   ICL_info(val);
1541   myfree(bzcoef);
1542 }
1543
1544 /* Flood fill 
1545
1546    REF: Graphics Gems I. page 282+
1547
1548 */
1549
1550 /* This should be moved into a seperate file? */
1551
1552 /* This is the truncation used:
1553    
1554    a double is multiplied by 16 and then truncated.
1555    This means that 0 -> 0
1556    So a triangle of (0,0) (10,10) (10,0) Will look like it's
1557    not filling the (10,10) point nor the (10,0)-(10,10)  line segment
1558
1559 */
1560
1561
1562 /* Flood fill algorithm - based on the Ken Fishkins (pixar) gem in 
1563    graphics gems I */
1564
1565 /*
1566 struct stc {
1567   i_img_dim mylx,myrx; 
1568   i_img_dim dadlx,dadrx;
1569   i_img_dim myy;
1570   int mydirection;
1571 };
1572
1573 Not used code???
1574 */
1575
1576
1577 struct stack_element {
1578   i_img_dim myLx,myRx;
1579   i_img_dim dadLx,dadRx;
1580   i_img_dim myY;
1581   int myDirection;
1582 };
1583
1584
1585 /* create the link data to put push onto the stack */
1586
1587 static
1588 struct stack_element*
1589 crdata(i_img_dim left,i_img_dim right,i_img_dim dadl,i_img_dim dadr,i_img_dim y, int dir) {
1590   struct stack_element *ste;
1591   ste              = mymalloc(sizeof(struct stack_element)); /* checked 5jul05 tonyc */
1592   ste->myLx        = left;
1593   ste->myRx        = right;
1594   ste->dadLx       = dadl;
1595   ste->dadRx       = dadr;
1596   ste->myY         = y;
1597   ste->myDirection = dir;
1598   return ste;
1599 }
1600
1601 /* i_ccomp compares two colors and gives true if they are the same */
1602
1603 typedef int (*ff_cmpfunc)(i_color const *c1, i_color const *c2, int channels);
1604
1605 static int
1606 i_ccomp_normal(i_color const *val1, i_color const *val2, int ch) {
1607   int i;
1608   for(i = 0; i < ch; i++) 
1609     if (val1->channel[i] !=val2->channel[i])
1610       return 0;
1611   return 1;
1612 }
1613
1614 static int
1615 i_ccomp_border(i_color const *val1, i_color const *val2, int ch) {
1616   int i;
1617   for(i = 0; i < ch; i++) 
1618     if (val1->channel[i] !=val2->channel[i])
1619       return 1;
1620   return 0;
1621 }
1622
1623 static int
1624 i_lspan(i_img *im, i_img_dim seedx, i_img_dim seedy, i_color const *val, ff_cmpfunc cmpfunc) {
1625   i_color cval;
1626   while(1) {
1627     if (seedx-1 < 0) break;
1628     i_gpix(im,seedx-1,seedy,&cval);
1629     if (!cmpfunc(val,&cval,im->channels)) 
1630       break;
1631     seedx--;
1632   }
1633   return seedx;
1634 }
1635
1636 static int
1637 i_rspan(i_img *im, i_img_dim seedx, i_img_dim seedy, i_color const *val, ff_cmpfunc cmpfunc) {
1638   i_color cval;
1639   while(1) {
1640     if (seedx+1 > im->xsize-1) break;
1641     i_gpix(im,seedx+1,seedy,&cval);
1642     if (!cmpfunc(val,&cval,im->channels)) break;
1643     seedx++;
1644   }
1645   return seedx;
1646 }
1647
1648 /* Macro to create a link and push on to the list */
1649
1650 #define ST_PUSH(left,right,dadl,dadr,y,dir) do {                 \
1651   struct stack_element *s = crdata(left,right,dadl,dadr,y,dir);  \
1652   llist_push(st,&s);                                             \
1653 } while (0)
1654
1655 /* pops the shadow on TOS into local variables lx,rx,y,direction,dadLx and dadRx */
1656 /* No overflow check! */
1657  
1658 #define ST_POP() do {         \
1659   struct stack_element *s;    \
1660   llist_pop(st,&s);           \
1661   lx        = s->myLx;        \
1662   rx        = s->myRx;        \
1663   dadLx     = s->dadLx;       \
1664   dadRx     = s->dadRx;       \
1665   y         = s->myY;         \
1666   direction = s->myDirection; \
1667   myfree(s);                  \
1668 } while (0)
1669
1670 #define ST_STACK(dir,dadLx,dadRx,lx,rx,y) do {                    \
1671   i_img_dim pushrx = rx+1;                                              \
1672   i_img_dim pushlx = lx-1;                                              \
1673   ST_PUSH(lx,rx,pushlx,pushrx,y+dir,dir);                         \
1674   if (rx > dadRx)                                                 \
1675     ST_PUSH(dadRx+1,rx,pushlx,pushrx,y-dir,-dir);                 \
1676   if (lx < dadLx) ST_PUSH(lx,dadLx-1,pushlx,pushrx,y-dir,-dir);   \
1677 } while (0)
1678
1679 #define SET(x,y) btm_set(btm,x,y)
1680
1681 /* INSIDE returns true if pixel is correct color and we haven't set it before. */
1682 #define INSIDE(x,y, seed) ((!btm_test(btm,x,y) && ( i_gpix(im,x,y,&cval),cmpfunc(seed,&cval,channels)  ) ))
1683
1684
1685
1686 /* The function that does all the real work */
1687
1688 static struct i_bitmap *
1689 i_flood_fill_low(i_img *im,i_img_dim seedx,i_img_dim seedy,
1690                  i_img_dim *bxminp, i_img_dim *bxmaxp, i_img_dim *byminp, i_img_dim *bymaxp,
1691                  i_color const *seed, ff_cmpfunc cmpfunc) {
1692   i_img_dim ltx, rtx;
1693   i_img_dim tx = 0;
1694
1695   i_img_dim bxmin = seedx;
1696   i_img_dim bxmax = seedx;
1697   i_img_dim bymin = seedy;
1698   i_img_dim bymax = seedy;
1699
1700   struct llist *st;
1701   struct i_bitmap *btm;
1702
1703   int channels;
1704   i_img_dim xsize,ysize;
1705   i_color cval;
1706
1707   channels = im->channels;
1708   xsize    = im->xsize;
1709   ysize    = im->ysize;
1710
1711   btm = btm_new(xsize, ysize);
1712   st  = llist_new(100, sizeof(struct stack_element*));
1713
1714   /* Find the starting span and fill it */
1715   ltx = i_lspan(im, seedx, seedy, seed, cmpfunc);
1716   rtx = i_rspan(im, seedx, seedy, seed, cmpfunc);
1717   for(tx=ltx; tx<=rtx; tx++) SET(tx, seedy);
1718   bxmin = ltx;
1719   bxmax = rtx;
1720
1721   ST_PUSH(ltx, rtx, ltx, rtx, seedy+1,  1);
1722   ST_PUSH(ltx, rtx, ltx, rtx, seedy-1, -1);
1723
1724   while(st->count) {
1725     /* Stack variables */
1726     i_img_dim lx,rx;
1727     i_img_dim dadLx,dadRx;
1728     i_img_dim y;
1729     int direction;
1730
1731     i_img_dim x;
1732     int wasIn=0;
1733
1734     ST_POP(); /* sets lx, rx, dadLx, dadRx, y, direction */
1735
1736
1737     if (y<0 || y>ysize-1) continue;
1738     if (bymin > y) bymin=y; /* in the worst case an extra line */
1739     if (bymax < y) bymax=y; 
1740
1741
1742     x = lx+1;
1743     if ( lx >= 0 && (wasIn = INSIDE(lx, y, seed)) ) {
1744       SET(lx, y);
1745       lx--;
1746       while(lx >= 0 && INSIDE(lx, y, seed)) {
1747         SET(lx,y);
1748         lx--;
1749       }
1750     }
1751
1752     if (bxmin > lx) bxmin = lx;
1753     while(x <= xsize-1) {
1754       /*  printf("x=%d\n",x); */
1755       if (wasIn) {
1756         
1757         if (INSIDE(x, y, seed)) {
1758           /* case 1: was inside, am still inside */
1759           SET(x,y);
1760         } else {
1761           /* case 2: was inside, am no longer inside: just found the
1762              right edge of a span */
1763           ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1764
1765           if (bxmax < x) bxmax = x;
1766           wasIn=0;
1767         }
1768       } else {
1769         if (x > rx) goto EXT;
1770         if (INSIDE(x, y, seed)) {
1771           SET(x, y);
1772           /* case 3: Wasn't inside, am now: just found the start of a new run */
1773           wasIn = 1;
1774             lx = x;
1775         } else {
1776           /* case 4: Wasn't inside, still isn't */
1777         }
1778       }
1779       x++;
1780     }
1781   EXT: /* out of loop */
1782     if (wasIn) {
1783       /* hit an edge of the frame buffer while inside a run */
1784       ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1785       if (bxmax < x) bxmax = x;
1786     }
1787   }
1788
1789   llist_destroy(st);
1790
1791   *bxminp = bxmin;
1792   *bxmaxp = bxmax;
1793   *byminp = bymin;
1794   *bymaxp = bymax;
1795
1796   return btm;
1797 }
1798
1799 /*
1800 =item i_flood_fill(C<im>, C<seedx>, C<seedy>, C<color>)
1801
1802 =category Drawing
1803 =synopsis i_flood_fill(im, 50, 50, &color);
1804
1805 Flood fills the 4-connected region starting from the point (C<seedx>,
1806 C<seedy>) with I<color>.
1807
1808 Returns false if (C<seedx>, C<seedy>) are outside the image.
1809
1810 =cut
1811 */
1812
1813 undef_int
1814 i_flood_fill(i_img *im, i_img_dim seedx, i_img_dim seedy, const i_color *dcol) {
1815   i_img_dim bxmin, bxmax, bymin, bymax;
1816   struct i_bitmap *btm;
1817   i_img_dim x, y;
1818   i_color val;
1819
1820   i_clear_error();
1821   if (seedx < 0 || seedx >= im->xsize ||
1822       seedy < 0 || seedy >= im->ysize) {
1823     i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
1824     return 0;
1825   }
1826
1827   /* Get the reference color */
1828   i_gpix(im, seedx, seedy, &val);
1829
1830   btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1831                          &val, i_ccomp_normal);
1832
1833   for(y=bymin;y<=bymax;y++)
1834     for(x=bxmin;x<=bxmax;x++)
1835       if (btm_test(btm,x,y)) 
1836         i_ppix(im,x,y,dcol);
1837   btm_destroy(btm);
1838   return 1;
1839 }
1840
1841 /*
1842 =item i_flood_cfill(C<im>, C<seedx>, C<seedy>, C<fill>)
1843
1844 =category Drawing
1845 =synopsis i_flood_cfill(im, 50, 50, fill);
1846
1847 Flood fills the 4-connected region starting from the point (C<seedx>,
1848 C<seedy>) with C<fill>.
1849
1850 Returns false if (C<seedx>, C<seedy>) are outside the image.
1851
1852 =cut
1853 */
1854
1855 undef_int
1856 i_flood_cfill(i_img *im, i_img_dim seedx, i_img_dim seedy, i_fill_t *fill) {
1857   i_img_dim bxmin, bxmax, bymin, bymax;
1858   struct i_bitmap *btm;
1859   i_color val;
1860
1861   i_clear_error();
1862   
1863   if (seedx < 0 || seedx >= im->xsize ||
1864       seedy < 0 || seedy >= im->ysize) {
1865     i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
1866     return 0;
1867   }
1868
1869   /* Get the reference color */
1870   i_gpix(im, seedx, seedy, &val);
1871
1872   btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1873                          &val, i_ccomp_normal);
1874
1875   cfill_from_btm(im, fill, btm, bxmin, bxmax, bymin, bymax);
1876
1877   btm_destroy(btm);
1878   return 1;
1879 }
1880
1881 /*
1882 =item i_flood_fill_border(C<im>, C<seedx>, C<seedy>, C<color>, C<border>)
1883
1884 =category Drawing
1885 =synopsis i_flood_fill_border(im, 50, 50, &color, &border);
1886
1887 Flood fills the 4-connected region starting from the point (C<seedx>,
1888 C<seedy>) with C<color>, fill stops when the fill reaches a pixels
1889 with color C<border>.
1890
1891 Returns false if (C<seedx>, C<seedy>) are outside the image.
1892
1893 =cut
1894 */
1895
1896 undef_int
1897 i_flood_fill_border(i_img *im, i_img_dim seedx, i_img_dim seedy, const i_color *dcol,
1898                     const i_color *border) {
1899   i_img_dim bxmin, bxmax, bymin, bymax;
1900   struct i_bitmap *btm;
1901   i_img_dim x, y;
1902
1903   i_clear_error();
1904   if (seedx < 0 || seedx >= im->xsize ||
1905       seedy < 0 || seedy >= im->ysize) {
1906     i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
1907     return 0;
1908   }
1909
1910   btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1911                          border, i_ccomp_border);
1912
1913   for(y=bymin;y<=bymax;y++)
1914     for(x=bxmin;x<=bxmax;x++)
1915       if (btm_test(btm,x,y)) 
1916         i_ppix(im,x,y,dcol);
1917   btm_destroy(btm);
1918   return 1;
1919 }
1920
1921 /*
1922 =item i_flood_cfill_border(C<im>, C<seedx>, C<seedy>, C<fill>, C<border>)
1923
1924 =category Drawing
1925 =synopsis i_flood_cfill_border(im, 50, 50, fill, border);
1926
1927 Flood fills the 4-connected region starting from the point (C<seedx>,
1928 C<seedy>) with C<fill>, the fill stops when it reaches pixels of color
1929 C<border>.
1930
1931 Returns false if (C<seedx>, C<seedy>) are outside the image.
1932
1933 =cut
1934 */
1935
1936 undef_int
1937 i_flood_cfill_border(i_img *im, i_img_dim seedx, i_img_dim seedy, i_fill_t *fill,
1938                      const i_color *border) {
1939   i_img_dim bxmin, bxmax, bymin, bymax;
1940   struct i_bitmap *btm;
1941
1942   i_clear_error();
1943   
1944   if (seedx < 0 || seedx >= im->xsize ||
1945       seedy < 0 || seedy >= im->ysize) {
1946     i_push_error(0, "i_flood_cfill_border: Seed pixel outside of image");
1947     return 0;
1948   }
1949
1950   btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1951                          border, i_ccomp_border);
1952
1953   cfill_from_btm(im, fill, btm, bxmin, bxmax, bymin, bymax);
1954
1955   btm_destroy(btm);
1956
1957   return 1;
1958 }
1959
1960 static void
1961 cfill_from_btm(i_img *im, i_fill_t *fill, struct i_bitmap *btm, 
1962                i_img_dim bxmin, i_img_dim bxmax, i_img_dim bymin, i_img_dim bymax) {
1963   i_img_dim x, y;
1964   i_img_dim start;
1965
1966   i_render r;
1967
1968   i_render_init(&r, im, bxmax - bxmin + 1);
1969
1970   for(y=bymin; y<=bymax; y++) {
1971     x = bxmin;
1972     while (x <= bxmax) {
1973       while (x <= bxmax && !btm_test(btm, x, y)) {
1974         ++x;
1975       }
1976       if (btm_test(btm, x, y)) {
1977         start = x;
1978         while (x <= bxmax && btm_test(btm, x, y)) {
1979           ++x;
1980         }
1981         i_render_fill(&r, start, y, x-start, NULL, fill);
1982       }
1983     }
1984   }
1985   i_render_done(&r);
1986 }