]> git.imager.perl.org - imager.git/blob - draw.c
4b6ba042c5ff475acca6d1f757f531b4c491c742
[imager.git] / draw.c
1 #include "imager.h"
2 #include "draw.h"
3 #include "log.h"
4 #include "imageri.h"
5
6 #include <limits.h>
7
8 void
9 i_mmarray_cr(i_mmarray *ar,int l) {
10   int i;
11   int alloc_size;
12
13   ar->lines=l;
14   alloc_size = sizeof(minmax) * l;
15   /* check for overflow */
16   if (alloc_size / l != sizeof(minmax)) {
17     fprintf(stderr, "overflow calculating memory allocation");
18     exit(3);
19   }
20   ar->data=mymalloc(alloc_size); /* checked 5jul05 tonyc */
21   for(i=0;i<l;i++) { ar->data[i].max=-1; ar->data[i].min=MAXINT; }
22 }
23
24 void
25 i_mmarray_dst(i_mmarray *ar) {
26   ar->lines=0;
27   if (ar->data != NULL) { myfree(ar->data); ar->data=NULL; }
28 }
29
30 void
31 i_mmarray_add(i_mmarray *ar,int x,int y) {
32   if (y>-1 && y<ar->lines)
33     {
34       if (x<ar->data[y].min) ar->data[y].min=x;
35       if (x>ar->data[y].max) ar->data[y].max=x;
36     }
37 }
38
39 int
40 i_mmarray_gmin(i_mmarray *ar,int y) {
41   if (y>-1 && y<ar->lines) return ar->data[y].min;
42   else return -1;
43 }
44
45 int
46 i_mmarray_getm(i_mmarray *ar,int y) {
47   if (y>-1 && y<ar->lines) return ar->data[y].max;
48   else return MAXINT;
49 }
50
51 void
52 i_mmarray_render(i_img *im,i_mmarray *ar,i_color *val) {
53   int i,x;
54   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);
55 }
56
57 void
58 i_mmarray_render_fill(i_img *im,i_mmarray *ar,i_fill_t *fill) {
59   int x, w, y;
60   if (im->bits == i_8_bits && fill->fill_with_color) {
61     i_color *line = mymalloc(sizeof(i_color) * im->xsize); /* checked 5jul05 tonyc */
62     i_color *work = NULL;
63     if (fill->combine)
64       work = mymalloc(sizeof(i_color) * im->xsize); /* checked 5jul05 tonyc */
65     for(y=0;y<ar->lines;y++) {
66       if (ar->data[y].max!=-1) {
67         x = ar->data[y].min;
68         w = ar->data[y].max-ar->data[y].min;
69
70         if (fill->combine) {
71           i_glin(im, x, x+w, y, line);
72           (fill->fill_with_color)(fill, x, y, w, im->channels, work);
73           (fill->combine)(line, work, im->channels, w);
74         }
75         else {
76           (fill->fill_with_color)(fill, x, y, w, im->channels, line);
77         }
78         i_plin(im, x, x+w, y, line);
79       }
80     }
81   
82     myfree(line);
83     if (work)
84       myfree(work);
85   }
86   else {
87     i_fcolor *line = mymalloc(sizeof(i_fcolor) * im->xsize); /* checked 5jul05 tonyc */
88     i_fcolor *work = NULL;
89     if (fill->combinef)
90       work = mymalloc(sizeof(i_fcolor) * im->xsize); /* checked 5jul05 tonyc */
91     for(y=0;y<ar->lines;y++) {
92       if (ar->data[y].max!=-1) {
93         x = ar->data[y].min;
94         w = ar->data[y].max-ar->data[y].min;
95
96         if (fill->combinef) {
97           i_glinf(im, x, x+w, y, line);
98           (fill->fill_with_fcolor)(fill, x, y, w, im->channels, work);
99           (fill->combinef)(line, work, im->channels, w);
100         }
101         else {
102           (fill->fill_with_fcolor)(fill, x, y, w, im->channels, line);
103         }
104         i_plinf(im, x, x+w, y, line);
105       }
106     }
107   
108     myfree(line);
109     if (work)
110       myfree(work);
111   }
112 }
113
114
115 static
116 void
117 i_arcdraw(int x1, int y1, int x2, int y2, i_mmarray *ar) {
118   double alpha;
119   double dsec;
120   int temp;
121   alpha=(double)(y2-y1)/(double)(x2-x1);
122   if (fabs(alpha) <= 1) 
123     {
124       if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
125       dsec=y1;
126       while(x1<=x2)
127         {
128           i_mmarray_add(ar,x1,(int)(dsec+0.5));
129           dsec+=alpha;
130           x1++;
131         }
132     }
133   else
134     {
135       alpha=1/alpha;
136       if (y2<y1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
137       dsec=x1;
138       while(y1<=y2)
139         {
140           i_mmarray_add(ar,(int)(dsec+0.5),y1);
141           dsec+=alpha;
142           y1++;
143         }
144     }
145 }
146
147 void
148 i_mmarray_info(i_mmarray *ar) {
149   int i;
150   for(i=0;i<ar->lines;i++)
151   if (ar->data[i].max!=-1) printf("line %d: min=%d, max=%d.\n",i,ar->data[i].min,ar->data[i].max);
152 }
153
154 static void
155 i_arc_minmax(i_int_hlines *hlines,int x,int y,float rad,float d1,float d2) {
156   i_mmarray dot;
157   float f,fx,fy;
158   int x1,y1;
159
160   /*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));*/
161
162   i_mmarray_cr(&dot, hlines->limit_y);
163
164   x1=(int)(x+0.5+rad*cos(d1*PI/180.0));
165   y1=(int)(y+0.5+rad*sin(d1*PI/180.0));
166   fx=(float)x1; fy=(float)y1;
167
168   /*  printf("x1: %d.\ny1: %d.\n",x1,y1); */
169   i_arcdraw(x, y, x1, y1, &dot);
170
171   x1=(int)(x+0.5+rad*cos(d2*PI/180.0));
172   y1=(int)(y+0.5+rad*sin(d2*PI/180.0));
173
174   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)));
175   
176   /*  printf("x1: %d.\ny1: %d.\n",x1,y1); */
177   i_arcdraw(x, y, x1, y1, &dot);
178
179   /* render the minmax values onto the hlines */
180   for (y = 0; y < dot.lines; y++) {
181     if (dot.data[y].max!=-1) {
182       int minx, width;
183       minx = dot.data[y].min;
184       width = dot.data[y].max - dot.data[y].min + 1;
185       i_int_hlines_add(hlines, y, minx, width);
186     }
187   }
188
189   /*  dot.info(); */
190   i_mmarray_dst(&dot);
191 }
192
193 static void
194 i_arc_hlines(i_int_hlines *hlines,int x,int y,float rad,float d1,float d2) {
195   if (d1 <= d2) {
196     i_arc_minmax(hlines, x, y, rad, d1, d2);
197   }
198   else {
199     i_arc_minmax(hlines, x, y, rad, d1, 360);
200     i_arc_minmax(hlines, x, y, rad, 0, d2);
201   }
202 }
203
204 /*
205 =item i_arc(im, x, y, rad, d1, d2, color)
206
207 =category Drawing
208 =synopsis i_arc(im, 50, 50, 20, 45, 135, &color);
209
210 Fills an arc centered at (x,y) with radius I<rad> covering the range
211 of angles in degrees from d1 to d2, with the color.
212
213 =cut
214 */
215
216 void
217 i_arc(i_img *im,int x,int y,float rad,float d1,float d2,const i_color *val) {
218   i_int_hlines hlines;
219
220   i_int_init_hlines_img(&hlines, im);
221
222   i_arc_hlines(&hlines, x, y, rad, d1, d2);
223
224   i_int_hlines_fill_color(im, &hlines, val);
225
226   i_int_hlines_destroy(&hlines);
227 }
228
229 /*
230 =item i_arc_cfill(im, x, y, rad, d1, d2, fill)
231
232 =category Drawing
233 =synopsis i_arc_cfill(im, 50, 50, 35, 90, 135, fill);
234
235 Fills an arc centered at (x,y) with radius I<rad> covering the range
236 of angles in degrees from d1 to d2, with the fill object.
237
238 =cut
239 */
240
241 #define MIN_CIRCLE_STEPS 8
242 #define MAX_CIRCLE_STEPS 360
243
244 void
245 i_arc_cfill(i_img *im,int x,int y,float rad,float d1,float d2,i_fill_t *fill) {
246   i_int_hlines hlines;
247
248   i_int_init_hlines_img(&hlines, im);
249
250   i_arc_hlines(&hlines, x, y, rad, d1, d2);
251
252   i_int_hlines_fill_fill(im, &hlines, fill);
253
254   i_int_hlines_destroy(&hlines);
255 }
256
257 static void
258 arc_poly(int *count, double **xvals, double **yvals,
259          double x, double y, double rad, double d1, double d2) {
260   double d1_rad, d2_rad;
261   double circum;
262   int steps, point_count;
263   double angle_inc;
264
265   /* normalize the angles */
266   d1 = fmod(d1, 360);
267   if (d1 == 0) {
268     if (d2 >= 360) { /* default is 361 */
269       d2 = 360;
270     }
271     else {
272       d2 = fmod(d2, 360);
273       if (d2 < d1)
274         d2 += 360;
275     }
276   }
277   else {
278     d2 = fmod(d2, 360);
279     if (d2 < d1)
280       d2 += 360;
281   }
282   d1_rad = d1 * PI / 180;
283   d2_rad = d2 * PI / 180;
284
285   /* how many segments for the curved part? 
286      we do a maximum of one per degree, with a minimum of 8/circle
287      we try to aim at having about one segment per 2 pixels
288      Work it out per circle to get a step size.
289
290      I was originally making steps = circum/2 but that looked horrible.
291
292      I think there might be an issue in the polygon filler.
293   */
294   circum = 2 * PI * rad;
295   steps = circum;
296   if (steps > MAX_CIRCLE_STEPS)
297     steps = MAX_CIRCLE_STEPS;
298   else if (steps < MIN_CIRCLE_STEPS)
299     steps = MIN_CIRCLE_STEPS;
300
301   angle_inc = 2 * PI / steps;
302
303   point_count = steps + 5; /* rough */
304   *xvals = mymalloc(point_count * sizeof(double));
305   *yvals = mymalloc(point_count * sizeof(double));
306
307   /* from centre to edge at d1 */
308   (*xvals)[0] = x;
309   (*yvals)[0] = y;
310   (*xvals)[1] = x + rad * cos(d1_rad);
311   (*yvals)[1] = y + rad * sin(d1_rad);
312   *count = 2;
313
314   /* step around the curve */
315   while (d1_rad < d2_rad) {
316     (*xvals)[*count] = x + rad * cos(d1_rad);
317     (*yvals)[*count] = y + rad * sin(d1_rad);
318     ++*count;
319     d1_rad += angle_inc;
320   }
321
322   /* finish off the curve */
323   (*xvals)[*count] = x + rad * cos(d2_rad);
324   (*yvals)[*count] = y + rad * sin(d2_rad);
325   ++*count;
326 }
327
328 /*
329 =item i_arc_aa(im, x, y, rad, d1, d2, color)
330
331 =category Drawing
332 =synopsis i_arc_aa(im, 50, 50, 35, 90, 135, &color);
333
334 Antialias fills an arc centered at (x,y) with radius I<rad> covering
335 the range of angles in degrees from d1 to d2, with the color.
336
337 =cut
338 */
339
340 void
341 i_arc_aa(i_img *im, double x, double y, double rad, double d1, double d2,
342          const i_color *val) {
343   double *xvals, *yvals;
344   int count;
345
346   arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
347
348   i_poly_aa(im, count, xvals, yvals, val);
349
350   myfree(xvals);
351   myfree(yvals);
352 }
353
354 /*
355 =item i_arc_aa_cfill(im, x, y, rad, d1, d2, fill)
356
357 =category Drawing
358 =synopsis i_arc_aa_cfill(im, 50, 50, 35, 90, 135, fill);
359
360 Antialias fills an arc centered at (x,y) with radius I<rad> covering
361 the range of angles in degrees from d1 to d2, with the fill object.
362
363 =cut
364 */
365
366 void
367 i_arc_aa_cfill(i_img *im, double x, double y, double rad, double d1, double d2,
368                i_fill_t *fill) {
369   double *xvals, *yvals;
370   int count;
371
372   arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
373
374   i_poly_aa_cfill(im, count, xvals, yvals, fill);
375
376   myfree(xvals);
377   myfree(yvals);
378 }
379
380 /* Temporary AA HACK */
381
382
383 typedef int frac;
384 static  frac float_to_frac(float x) { return (frac)(0.5+x*16.0); }
385
386 static 
387 void
388 polar_to_plane(float cx, float cy, float angle, float radius, frac *x, frac *y) {
389   *x = float_to_frac(cx+radius*cos(angle));
390   *y = float_to_frac(cy+radius*sin(angle));
391 }
392
393 static
394 void
395 make_minmax_list(i_mmarray *dot, float x, float y, float radius) {
396   float angle = 0.0;
397   float astep = radius>0.1 ? .5/radius : 10;
398   frac cx, cy, lx, ly, sx, sy;
399
400   mm_log((1, "make_minmax_list(dot %p, x %.2f, y %.2f, radius %.2f)\n", dot, x, y, radius));
401
402   polar_to_plane(x, y, angle, radius, &sx, &sy);
403   
404   for(angle = 0.0; angle<361; angle +=astep) {
405     lx = sx; ly = sy;
406     polar_to_plane(x, y, angle, radius, &cx, &cy);
407     sx = cx; sy = cy;
408
409     if (fabs(cx-lx) > fabs(cy-ly)) {
410       int ccx, ccy;
411       if (lx>cx) { 
412         ccx = lx; lx = cx; cx = ccx; 
413         ccy = ly; ly = cy; cy = ccy; 
414       }
415
416       for(ccx=lx; ccx<=cx; ccx++) {
417         ccy = ly + ((cy-ly)*(ccx-lx))/(cx-lx);
418         i_mmarray_add(dot, ccx, ccy);
419       }
420     } else {
421       int ccx, ccy;
422
423       if (ly>cy) { 
424         ccy = ly; ly = cy; cy = ccy; 
425         ccx = lx; lx = cx; cx = ccx; 
426       }
427       
428       for(ccy=ly; ccy<=cy; ccy++) {
429         if (cy-ly) ccx = lx + ((cx-lx)*(ccy-ly))/(cy-ly); else ccx = lx;
430         i_mmarray_add(dot, ccx, ccy);
431       }
432     }
433   }
434 }
435
436 /* Get the number of subpixels covered */
437
438 static
439 int
440 i_pixel_coverage(i_mmarray *dot, int x, int y) {
441   frac minx = x*16;
442   frac maxx = minx+15;
443   frac cy;
444   int cnt = 0;
445   
446   for(cy=y*16; cy<(y+1)*16; cy++) {
447     frac tmin = dot->data[cy].min;
448     frac tmax = dot->data[cy].max;
449
450     if (tmax == -1 || tmin > maxx || tmax < minx) continue;
451     
452     if (tmin < minx) tmin = minx;
453     if (tmax > maxx) tmax = maxx;
454     
455     cnt+=1+tmax-tmin;
456   }
457   return cnt;
458 }
459
460 /*
461 =item i_circle_aa(im, x, y, rad, color)
462
463 =category Drawing
464 =synopsis i_circle_aa(im, 50, 50, 45, &color);
465
466 Antialias fills a circle centered at (x,y) for radius I<rad> with
467 color.
468
469 =cut
470 */
471 void
472 i_circle_aa(i_img *im, float x, float y, float rad, const i_color *val) {
473   i_mmarray dot;
474   i_color temp;
475   int ly;
476
477   mm_log((1, "i_circle_aa(im %p, x %d, y %d, rad %.2f, val %p)\n", im, x, y, rad, val));
478
479   i_mmarray_cr(&dot,16*im->ysize);
480   make_minmax_list(&dot, x, y, rad);
481
482   for(ly = 0; ly<im->ysize; ly++) {
483     int ix, cy, minx = INT_MAX, maxx = INT_MIN;
484
485     /* Find the left/rightmost set subpixels */
486     for(cy = 0; cy<16; cy++) {
487       frac tmin = dot.data[ly*16+cy].min;
488       frac tmax = dot.data[ly*16+cy].max;
489       if (tmax == -1) continue;
490
491       if (minx > tmin) minx = tmin;
492       if (maxx < tmax) maxx = tmax;
493     }
494
495     if (maxx == INT_MIN) continue; /* no work to be done for this row of pixels */
496
497     minx /= 16;
498     maxx /= 16;
499     for(ix=minx; ix<=maxx; ix++) {
500       int cnt = i_pixel_coverage(&dot, ix, ly);
501       if (cnt>255) cnt = 255;
502       if (cnt) { /* should never be true */
503         int ch;
504         float ratio = (float)cnt/255.0;
505         i_gpix(im, ix, ly, &temp);
506         for(ch=0;ch<im->channels; ch++) temp.channel[ch] = (unsigned char)((float)val->channel[ch]*ratio + (float)temp.channel[ch]*(1.0-ratio));
507         i_ppix(im, ix, ly, &temp);
508       }
509     }
510   }
511   i_mmarray_dst(&dot);
512 }
513
514 /*
515 =item i_box(im, x1, y1, x2, y2, color)
516
517 =category Drawing
518 =synopsis i_box(im, 0, 0, im->xsize-1, im->ysize-1, &color).
519
520 Outlines the box from (x1,y1) to (x2,y2) inclusive with I<color>.
521
522 =cut
523 */
524
525 void
526 i_box(i_img *im,int x1,int y1,int x2,int y2,const i_color *val) {
527   int x,y;
528   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));
529   for(x=x1;x<x2+1;x++) {
530     i_ppix(im,x,y1,val);
531     i_ppix(im,x,y2,val);
532   }
533   for(y=y1;y<y2+1;y++) {
534     i_ppix(im,x1,y,val);
535     i_ppix(im,x2,y,val);
536   }
537 }
538
539 /*
540 =item i_box_filled(im, x1, y1, x2, y2, color)
541
542 =category Drawing
543 =synopsis i_box_filled(im, 0, 0, im->xsize-1, im->ysize-1, &color);
544
545 Fills the box from (x1,y1) to (x2,y2) inclusive with color.
546
547 =cut
548 */
549
550 void
551 i_box_filled(i_img *im,int x1,int y1,int x2,int y2, const i_color *val) {
552   int x,y;
553   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));
554   for(x=x1;x<x2+1;x++) for (y=y1;y<y2+1;y++) i_ppix(im,x,y,val);
555 }
556
557 /*
558 =item i_box_cfill(im, x1, y1, x2, y2, fill)
559
560 =category Drawing
561 =synopsis i_box_cfill(im, 0, 0, im->xsize-1, im->ysize-1, fill);
562
563 Fills the box from (x1,y1) to (x2,y2) inclusive with fill.
564
565 =cut
566 */
567
568 void
569 i_box_cfill(i_img *im,int x1,int y1,int x2,int y2,i_fill_t *fill) {
570   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));
571
572   ++x2;
573   if (x1 < 0)
574     x1 = 0;
575   if (y1 < 0) 
576     y1 = 0;
577   if (x2 > im->xsize) 
578     x2 = im->xsize;
579   if (y2 >= im->ysize)
580     y2 = im->ysize-1;
581   if (x1 >= x2 || y1 > y2)
582     return;
583   if (im->bits == i_8_bits && fill->fill_with_color) {
584     i_color *line = mymalloc(sizeof(i_color) * (x2 - x1)); /* checked 5jul05 tonyc */
585     i_color *work = NULL;
586     if (fill->combine)
587       work = mymalloc(sizeof(i_color) * (x2-x1)); /* checked 5jul05 tonyc */
588     while (y1 <= y2) {
589       if (fill->combine) {
590         i_glin(im, x1, x2, y1, line);
591         (fill->fill_with_color)(fill, x1, y1, x2-x1, im->channels, work);
592         (fill->combine)(line, work, im->channels, x2-x1);
593       }
594       else {
595         (fill->fill_with_color)(fill, x1, y1, x2-x1, im->channels, line);
596       }
597       i_plin(im, x1, x2, y1, line);
598       ++y1;
599     }
600     myfree(line);
601     if (work)
602       myfree(work);
603   }
604   else {
605     i_fcolor *line = mymalloc(sizeof(i_fcolor) * (x2 - x1)); /* checked 5jul05 tonyc */
606     i_fcolor *work;
607     work = mymalloc(sizeof(i_fcolor) * (x2 - x1)); /* checked 5jul05 tonyc */
608
609     while (y1 <= y2) {
610       if (fill->combine) {
611         i_glinf(im, x1, x2, y1, line);
612         (fill->fill_with_fcolor)(fill, x1, y1, x2-x1, im->channels, work);
613         (fill->combinef)(line, work, im->channels, x2-x1);
614       }
615       else {
616         (fill->fill_with_fcolor)(fill, x1, y1, x2-x1, im->channels, line);
617       }
618       i_plinf(im, x1, x2, y1, line);
619       ++y1;
620     }
621     myfree(line);
622     if (work)
623       myfree(work);
624   }
625 }
626
627
628 /* 
629 =item i_line(im, x1, y1, x2, y2, val, endp)
630
631 =category Drawing
632
633 Draw a line to image using bresenhams linedrawing algorithm
634
635    im   - image to draw to
636    x1   - starting x coordinate
637    y1   - starting x coordinate
638    x2   - starting x coordinate
639    y2   - starting x coordinate
640    val  - color to write to image
641    endp - endpoint flag (boolean)
642
643 =cut
644 */
645
646 void
647 i_line(i_img *im, int x1, int y1, int x2, int y2, const i_color *val, int endp) {
648   int x, y;
649   int dx, dy;
650   int p;
651
652   dx = x2 - x1;
653   dy = y2 - y1;
654
655
656   /* choose variable to iterate on */
657   if (abs(dx)>abs(dy)) {
658     int dx2, dy2, cpy;
659
660     /* sort by x */
661     if (x1 > x2) {
662       int t;
663       t = x1; x1 = x2; x2 = t;
664       t = y1; y1 = y2; y2 = t;
665     }
666     
667     dx = abs(dx);
668     dx2 = dx*2;
669     dy = y2 - y1;
670
671     if (dy<0) {
672       dy = -dy;
673       cpy = -1;
674     } else {
675       cpy = 1;
676     }
677     dy2 = dy*2;
678     p = dy2 - dx;
679
680     
681     y = y1;
682     for(x=x1; x<x2-1; x++) {
683       if (p<0) {
684         p += dy2;
685       } else {
686         y += cpy;
687         p += dy2-dx2;
688       }
689       i_ppix(im, x+1, y, val);
690     }
691   } else {
692     int dy2, dx2, cpx;
693
694     /* sort bx y */
695     if (y1 > y2) {
696       int t;
697       t = x1; x1 = x2; x2 = t;
698       t = y1; y1 = y2; y2 = t;
699     }
700     
701     dy = abs(dy);
702     dx = x2 - x1;
703     dy2 = dy*2;
704
705     if (dx<0) {
706       dx = -dx;
707       cpx = -1;
708     } else {
709       cpx = 1;
710     }
711     dx2 = dx*2;
712     p = dx2 - dy;
713
714     x = x1;
715     
716     for(y=y1; y<y2-1; y++) {
717       if (p<0) {
718         p  += dx2;
719       } else {
720         x += cpx;
721         p += dx2-dy2;
722       }
723       i_ppix(im, x, y+1, val);
724     }
725   }
726   if (endp) {
727     i_ppix(im, x1, y1, val);
728     i_ppix(im, x2, y2, val);
729   } else {
730     if (x1 != x2 || y1 != y2) 
731       i_ppix(im, x1, y1, val);
732   }
733 }
734
735
736 void
737 i_line_dda(i_img *im, int x1, int y1, int x2, int y2, i_color *val) {
738
739   float dy;
740   int x;
741   
742   for(x=x1; x<=x2; x++) {
743     dy = y1+ (x-x1)/(float)(x2-x1)*(y2-y1);
744     i_ppix(im, x, (int)(dy+0.5), val);
745   }
746 }
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774 void
775 i_line_aa3(i_img *im,int x1,int y1,int x2,int y2,i_color *val) {
776   i_color tval;
777   float alpha;
778   float dsec,dfrac;
779   int temp,dx,dy,isec,ch;
780
781   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));
782
783   dy=y2-y1;
784   dx=x2-x1;
785
786   if (abs(dx)>abs(dy)) { /* alpha < 1 */
787     if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
788     alpha=(float)(y2-y1)/(float)(x2-x1);
789
790     dsec=y1;
791     while(x1<=x2) {
792       isec=(int)dsec;
793       dfrac=dsec-isec;
794       /*      dfrac=1-(1-dfrac)*(1-dfrac); */
795       /* This is something we can play with to try to get better looking lines */
796
797       i_gpix(im,x1,isec,&tval);
798       for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)(dfrac*(float)tval.channel[ch]+(1-dfrac)*(float)val->channel[ch]);
799       i_ppix(im,x1,isec,&tval);
800       
801       i_gpix(im,x1,isec+1,&tval);
802       for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)((1-dfrac)*(float)tval.channel[ch]+dfrac*(float)val->channel[ch]);
803       i_ppix(im,x1,isec+1,&tval);
804       
805       dsec+=alpha;
806       x1++;
807     }
808   } else {
809     if (y2<y1) { temp=y1; y1=y2; y2=temp; temp=x1; x1=x2; x2=temp; }
810     alpha=(float)(x2-x1)/(float)(y2-y1);
811     dsec=x1;
812     while(y1<=y2) {
813       isec=(int)dsec;
814       dfrac=dsec-isec;
815       /*      dfrac=sqrt(dfrac); */
816       /* This is something we can play with */
817       i_gpix(im,isec,y1,&tval);
818       for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)(dfrac*(float)tval.channel[ch]+(1-dfrac)*(float)val->channel[ch]);
819       i_ppix(im,isec,y1,&tval);
820
821       i_gpix(im,isec+1,y1,&tval);
822       for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)((1-dfrac)*(float)tval.channel[ch]+dfrac*(float)val->channel[ch]);
823       i_ppix(im,isec+1,y1,&tval);
824
825       dsec+=alpha;
826       y1++;
827     }
828   }
829 }
830
831
832 /*
833 =item i_line_aa(im, x1, x2, y1, y2, color, endp)
834
835 =category Drawing
836
837 Antialias draws a line from (x1,y1) to (x2, y2) in color.
838
839 The point (x2, y2) is drawn only if endp is set.
840
841 =cut
842 */
843
844 void
845 i_line_aa(i_img *im, int x1, int y1, int x2, int y2, const i_color *val, int endp) {
846   int x, y;
847   int dx, dy;
848   int p;
849
850   dx = x2 - x1;
851   dy = y2 - y1;
852
853   /* choose variable to iterate on */
854   if (abs(dx)>abs(dy)) {
855     int dx2, dy2, cpy;
856     
857     /* sort by x */
858     if (x1 > x2) {
859       int t;
860       t = x1; x1 = x2; x2 = t;
861       t = y1; y1 = y2; y2 = t;
862     }
863     
864     dx = abs(dx);
865     dx2 = dx*2;
866     dy = y2 - y1;
867
868     if (dy<0) {
869       dy = -dy;
870       cpy = -1;
871     } else {
872       cpy = 1;
873     }
874     dy2 = dy*2;
875     p = dy2 - dx2; /* this has to be like this for AA */
876     
877     y = y1;
878
879     for(x=x1; x<x2-1; x++) {
880       int ch;
881       i_color tval;
882       float t = (dy) ? -(float)(p)/(float)(dx2) : 1;
883       float t1, t2;
884
885       if (t<0) t = 0;
886       t1 = 1-t;
887       t2 = t;
888
889       i_gpix(im,x+1,y,&tval);
890       for(ch=0;ch<im->channels;ch++)
891         tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
892       i_ppix(im,x+1,y,&tval);
893
894       i_gpix(im,x+1,y+cpy,&tval);
895       for(ch=0;ch<im->channels;ch++)
896         tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
897       i_ppix(im,x+1,y+cpy,&tval);
898
899       if (p<0) {
900         p += dy2;
901       } else {
902         y += cpy;
903         p += dy2-dx2;
904       }
905     }
906   } else {
907     int dy2, dx2, cpx;
908
909     /* sort bx y */
910     if (y1 > y2) {
911       int t;
912       t = x1; x1 = x2; x2 = t;
913       t = y1; y1 = y2; y2 = t;
914     }
915     
916     dy = abs(dy);
917     dx = x2 - x1;
918     dy2 = dy*2;
919
920     if (dx<0) {
921       dx = -dx;
922       cpx = -1;
923     } else {
924       cpx = 1;
925     }
926     dx2 = dx*2;
927     p = dx2 - dy2; /* this has to be like this for AA */
928
929     x = x1;
930     
931     for(y=y1; y<y2-1; y++) {
932       int ch;
933       i_color tval;
934       float t = (dx) ? -(float)(p)/(float)(dy2) : 1;
935       float t1, t2;
936       
937       if (t<0) t = 0;
938       t1 = 1-t;
939       t2 = t;
940
941       i_gpix(im,x,y+1,&tval);
942       for(ch=0;ch<im->channels;ch++)
943         tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
944       i_ppix(im,x,y+1,&tval);
945
946       i_gpix(im,x+cpx,y+1,&tval);
947       for(ch=0;ch<im->channels;ch++)
948         tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
949       i_ppix(im,x+cpx,y+1,&tval);
950
951       if (p<0) {
952         p  += dx2;
953       } else {
954         x += cpx;
955         p += dx2-dy2;
956       }
957     }
958   }
959
960
961   if (endp) {
962     i_ppix(im, x1, y1, val);
963     i_ppix(im, x2, y2, val);
964   } else {
965     if (x1 != x2 || y1 != y2) 
966       i_ppix(im, x1, y1, val);
967   }
968 }
969
970
971
972 static double
973 perm(int n,int k) {
974   double r;
975   int i;
976   r=1;
977   for(i=k+1;i<=n;i++) r*=i;
978   for(i=1;i<=(n-k);i++) r/=i;
979   return r;
980 }
981
982
983 /* Note in calculating t^k*(1-t)^(n-k) 
984    we can start by using t^0=1 so this simplifies to
985    t^0*(1-t)^n - we want to multiply that with t/(1-t) each iteration
986    to get a new level - this may lead to errors who knows lets test it */
987
988 void
989 i_bezier_multi(i_img *im,int l,const double *x,const double *y, const i_color *val) {
990   double *bzcoef;
991   double t,cx,cy;
992   int k,i;
993   int lx = 0,ly = 0;
994   int n=l-1;
995   double itr,ccoef;
996
997   /* this is the same size as the x and y arrays, so shouldn't overflow */
998   bzcoef=mymalloc(sizeof(double)*l); /* checked 5jul05 tonyc */
999   for(k=0;k<l;k++) bzcoef[k]=perm(n,k);
1000   ICL_info(val);
1001
1002
1003   /*  for(k=0;k<l;k++) printf("bzcoef: %d -> %f\n",k,bzcoef[k]); */
1004   i=0;
1005   for(t=0;t<=1;t+=0.005) {
1006     cx=cy=0;
1007     itr=t/(1-t);
1008     ccoef=pow(1-t,n);
1009     for(k=0;k<l;k++) {
1010       /*      cx+=bzcoef[k]*x[k]*pow(t,k)*pow(1-t,n-k); 
1011               cy+=bzcoef[k]*y[k]*pow(t,k)*pow(1-t,n-k);*/
1012
1013       cx+=bzcoef[k]*x[k]*ccoef;
1014       cy+=bzcoef[k]*y[k]*ccoef;
1015       ccoef*=itr;
1016     }
1017     /*    printf("%f -> (%d,%d)\n",t,(int)(0.5+cx),(int)(0.5+cy)); */
1018     if (i++) { 
1019       i_line_aa(im,lx,ly,(int)(0.5+cx),(int)(0.5+cy),val, 1);
1020     }
1021       /*     i_ppix(im,(int)(0.5+cx),(int)(0.5+cy),val); */
1022     lx=(int)(0.5+cx);
1023     ly=(int)(0.5+cy);
1024   }
1025   ICL_info(val);
1026   myfree(bzcoef);
1027 }
1028
1029 /* Flood fill 
1030
1031    REF: Graphics Gems I. page 282+
1032
1033 */
1034
1035 /* This should be moved into a seperate file? */
1036
1037 /* This is the truncation used:
1038    
1039    a double is multiplied by 16 and then truncated.
1040    This means that 0 -> 0
1041    So a triangle of (0,0) (10,10) (10,0) Will look like it's
1042    not filling the (10,10) point nor the (10,0)-(10,10)  line segment
1043
1044 */
1045
1046
1047 /* Flood fill algorithm - based on the Ken Fishkins (pixar) gem in 
1048    graphics gems I */
1049
1050 /*
1051 struct stc {
1052   int mylx,myrx; 
1053   int dadlx,dadrx;
1054   int myy;
1055   int mydirection;
1056 };
1057
1058 Not used code???
1059 */
1060
1061
1062 struct stack_element {
1063   int myLx,myRx;
1064   int dadLx,dadRx;
1065   int myY;
1066   int myDirection;
1067 };
1068
1069
1070 /* create the link data to put push onto the stack */
1071
1072 static
1073 struct stack_element*
1074 crdata(int left,int right,int dadl,int dadr,int y, int dir) {
1075   struct stack_element *ste;
1076   ste              = mymalloc(sizeof(struct stack_element)); /* checked 5jul05 tonyc */
1077   ste->myLx        = left;
1078   ste->myRx        = right;
1079   ste->dadLx       = dadl;
1080   ste->dadRx       = dadr;
1081   ste->myY         = y;
1082   ste->myDirection = dir;
1083   return ste;
1084 }
1085
1086 /* i_ccomp compares two colors and gives true if they are the same */
1087
1088 static int
1089 i_ccomp(i_color *val1,i_color *val2,int ch) {
1090   int i;
1091   for(i=0;i<ch;i++) if (val1->channel[i] !=val2->channel[i]) return 0;
1092   return 1;
1093 }
1094
1095
1096 static int
1097 i_lspan(i_img *im, int seedx, int seedy, i_color *val) {
1098   i_color cval;
1099   while(1) {
1100     if (seedx-1 < 0) break;
1101     i_gpix(im,seedx-1,seedy,&cval);
1102     if (!i_ccomp(val,&cval,im->channels)) break;
1103     seedx--;
1104   }
1105   return seedx;
1106 }
1107
1108 static int
1109 i_rspan(i_img *im, int seedx, int seedy, i_color *val) {
1110   i_color cval;
1111   while(1) {
1112     if (seedx+1 > im->xsize-1) break;
1113     i_gpix(im,seedx+1,seedy,&cval);
1114     if (!i_ccomp(val,&cval,im->channels)) break;
1115     seedx++;
1116   }
1117   return seedx;
1118 }
1119
1120 /* Macro to create a link and push on to the list */
1121
1122 #define ST_PUSH(left,right,dadl,dadr,y,dir) do {                 \
1123   struct stack_element *s = crdata(left,right,dadl,dadr,y,dir);  \
1124   llist_push(st,&s);                                             \
1125 } while (0)
1126
1127 /* pops the shadow on TOS into local variables lx,rx,y,direction,dadLx and dadRx */
1128 /* No overflow check! */
1129  
1130 #define ST_POP() do {         \
1131   struct stack_element *s;    \
1132   llist_pop(st,&s);           \
1133   lx        = s->myLx;        \
1134   rx        = s->myRx;        \
1135   dadLx     = s->dadLx;       \
1136   dadRx     = s->dadRx;       \
1137   y         = s->myY;         \
1138   direction = s->myDirection; \
1139   myfree(s);                  \
1140 } while (0)
1141
1142 #define ST_STACK(dir,dadLx,dadRx,lx,rx,y) do {                    \
1143   int pushrx = rx+1;                                              \
1144   int pushlx = lx-1;                                              \
1145   ST_PUSH(lx,rx,pushlx,pushrx,y+dir,dir);                         \
1146   if (rx > dadRx)                                                 \
1147     ST_PUSH(dadRx+1,rx,pushlx,pushrx,y-dir,-dir);                 \
1148   if (lx < dadLx) ST_PUSH(lx,dadLx-1,pushlx,pushrx,y-dir,-dir);   \
1149 } while (0)
1150
1151 #define SET(x,y) btm_set(btm,x,y)
1152
1153 /* INSIDE returns true if pixel is correct color and we haven't set it before. */
1154 #define INSIDE(x,y) ((!btm_test(btm,x,y) && ( i_gpix(im,x,y,&cval),i_ccomp(&val,&cval,channels)  ) ))
1155
1156
1157
1158 /* The function that does all the real work */
1159
1160 static struct i_bitmap *
1161 i_flood_fill_low(i_img *im,int seedx,int seedy,
1162                  int *bxminp, int *bxmaxp, int *byminp, int *bymaxp) {
1163
1164   /*
1165     int lx,rx;
1166     int y;
1167     int direction;
1168     int dadLx,dadRx;
1169     int wasIn=0;
1170   */
1171   int ltx, rtx;
1172   int tx = 0;
1173
1174   int bxmin = seedx;
1175   int bxmax = seedx;
1176   int bymin = seedy;
1177   int bymax = seedy;
1178
1179   struct llist *st;
1180   struct i_bitmap *btm;
1181
1182   int channels,xsize,ysize;
1183   i_color cval,val;
1184
1185   channels = im->channels;
1186   xsize    = im->xsize;
1187   ysize    = im->ysize;
1188
1189   btm = btm_new(xsize, ysize);
1190   st  = llist_new(100, sizeof(struct stack_element*));
1191
1192   /* Get the reference color */
1193   i_gpix(im, seedx, seedy, &val);
1194
1195   /* Find the starting span and fill it */
1196   ltx = i_lspan(im, seedx, seedy, &val);
1197   rtx = i_rspan(im, seedx, seedy, &val);
1198   for(tx=ltx; tx<=rtx; tx++) SET(tx, seedy);
1199
1200   ST_PUSH(ltx, rtx, ltx, rtx, seedy+1,  1);
1201   ST_PUSH(ltx, rtx, ltx, rtx, seedy-1, -1);
1202
1203   while(st->count) {
1204     /* Stack variables */
1205     int lx,rx;
1206     int dadLx,dadRx;
1207     int y;
1208     int direction;
1209
1210     int x;
1211     int wasIn=0;
1212
1213     ST_POP(); /* sets lx, rx, dadLx, dadRx, y, direction */
1214
1215
1216     if (y<0 || y>ysize-1) continue;
1217     if (bymin > y) bymin=y; /* in the worst case an extra line */
1218     if (bymax < y) bymax=y; 
1219
1220
1221     x = lx+1;
1222     if ( lx >= 0 && (wasIn = INSIDE(lx, y)) ) {
1223       SET(lx, y);
1224       lx--;
1225       while(INSIDE(lx, y) && lx > 0) {
1226         SET(lx,y);
1227         lx--;
1228       }
1229     }
1230
1231     if (bxmin > lx) bxmin = lx;
1232     while(x <= xsize-1) {
1233       /*  printf("x=%d\n",x); */
1234       if (wasIn) {
1235         
1236         if (INSIDE(x, y)) {
1237           /* case 1: was inside, am still inside */
1238           SET(x,y);
1239         } else {
1240           /* case 2: was inside, am no longer inside: just found the
1241              right edge of a span */
1242           ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1243
1244           if (bxmax < x) bxmax = x;
1245           wasIn=0;
1246         }
1247       } else {
1248         if (x > rx) goto EXT;
1249         if (INSIDE(x, y)) {
1250           SET(x, y);
1251           /* case 3: Wasn't inside, am now: just found the start of a new run */
1252           wasIn = 1;
1253             lx = x;
1254         } else {
1255           /* case 4: Wasn't inside, still isn't */
1256         }
1257       }
1258       x++;
1259     }
1260   EXT: /* out of loop */
1261     if (wasIn) {
1262       /* hit an edge of the frame buffer while inside a run */
1263       ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1264       if (bxmax < x) bxmax = x;
1265     }
1266   }
1267
1268   llist_destroy(st);
1269
1270   *bxminp = bxmin;
1271   *bxmaxp = bxmax;
1272   *byminp = bymin;
1273   *bymaxp = bymax;
1274
1275   return btm;
1276 }
1277
1278 /*
1279 =item i_flood_fill(im, seedx, seedy, color)
1280
1281 =category Drawing
1282 =synopsis i_flood_fill(im, 50, 50, &color);
1283
1284 Flood fills the 4-connected region starting from the point (seedx,
1285 seedy) with I<color>.
1286
1287 Returns false if (seedx, seedy) are outside the image.
1288
1289 =cut
1290 */
1291
1292 undef_int
1293 i_flood_fill(i_img *im, int seedx, int seedy, const i_color *dcol) {
1294   int bxmin, bxmax, bymin, bymax;
1295   struct i_bitmap *btm;
1296   int x, y;
1297
1298   i_clear_error();
1299   if (seedx < 0 || seedx >= im->xsize ||
1300       seedy < 0 || seedy >= im->ysize) {
1301     i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
1302     return 0;
1303   }
1304
1305   btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax);
1306
1307   for(y=bymin;y<=bymax;y++)
1308     for(x=bxmin;x<=bxmax;x++)
1309       if (btm_test(btm,x,y)) 
1310         i_ppix(im,x,y,dcol);
1311   btm_destroy(btm);
1312   return 1;
1313 }
1314
1315 /*
1316 =item i_flood_cfill(im, seedx, seedy, fill)
1317
1318 =category Drawing
1319 =synopsis i_flood_cfill(im, 50, 50, fill);
1320
1321 Flood fills the 4-connected region starting from the point (seedx,
1322 seedy) with I<fill>.
1323
1324 Returns false if (seedx, seedy) are outside the image.
1325
1326 =cut
1327 */
1328
1329 undef_int
1330 i_flood_cfill(i_img *im, int seedx, int seedy, i_fill_t *fill) {
1331   int bxmin, bxmax, bymin, bymax;
1332   struct i_bitmap *btm;
1333   int x, y;
1334   int start;
1335
1336   i_clear_error();
1337   
1338   if (seedx < 0 || seedx >= im->xsize ||
1339       seedy < 0 || seedy >= im->ysize) {
1340     i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
1341     return 0;
1342   }
1343
1344   btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax);
1345
1346   if (im->bits == i_8_bits && fill->fill_with_color) {
1347     /* bxmax/bxmin are inside the image, hence this won't overflow */
1348     i_color *line = mymalloc(sizeof(i_color) * (bxmax - bxmin)); /* checked 5jul05 tonyc */
1349     i_color *work = NULL;
1350     if (fill->combine)
1351       work = mymalloc(sizeof(i_color) * (bxmax - bxmin)); /* checked 5jul05 tonyc */
1352
1353     for(y=bymin; y<=bymax; y++) {
1354       x = bxmin;
1355       while (x < bxmax) {
1356         while (x < bxmax && !btm_test(btm, x, y)) {
1357           ++x;
1358         }
1359         if (btm_test(btm, x, y)) {
1360           start = x;
1361           while (x < bxmax && btm_test(btm, x, y)) {
1362             ++x;
1363           }
1364           if (fill->combine) {
1365             i_glin(im, start, x, y, line);
1366             (fill->fill_with_color)(fill, start, y, x-start, im->channels, 
1367                                     work);
1368             (fill->combine)(line, work, im->channels, x-start);
1369           }
1370           else {
1371             (fill->fill_with_color)(fill, start, y, x-start, im->channels, 
1372                                     line);
1373           }
1374           i_plin(im, start, x, y, line);
1375         }
1376       }
1377     }
1378     myfree(line);
1379     if (work)
1380       myfree(work);
1381   }
1382   else {
1383     /* bxmax/bxmin are inside the image, hence this won't overflow */
1384     i_fcolor *line = mymalloc(sizeof(i_fcolor) * (bxmax - bxmin)); /* checked 5jul05 tonyc */
1385     i_fcolor *work = NULL;
1386     if (fill->combinef)
1387       work = mymalloc(sizeof(i_fcolor) * (bxmax - bxmin)); /* checked 5jul05 tonyc */
1388     
1389     for(y=bymin;y<=bymax;y++) {
1390       x = bxmin;
1391       while (x < bxmax) {
1392         while (x < bxmax && !btm_test(btm, x, y)) {
1393           ++x;
1394         }
1395         if (btm_test(btm, x, y)) {
1396           start = x;
1397           while (x < bxmax && btm_test(btm, x, y)) {
1398             ++x;
1399           }
1400           if (fill->combinef) {
1401             i_glinf(im, start, x, y, line);
1402             (fill->fill_with_fcolor)(fill, start, y, x-start, im->channels, 
1403                                     work);
1404             (fill->combinef)(line, work, im->channels, x-start);
1405           }
1406           else {
1407             (fill->fill_with_fcolor)(fill, start, y, x-start, im->channels, 
1408                                     line);
1409           }
1410           i_plinf(im, start, x, y, line);
1411         }
1412       }
1413     }
1414     myfree(line);
1415     if (work)
1416       myfree(work);
1417   }
1418
1419   btm_destroy(btm);
1420   return 1;
1421 }