]> git.imager.perl.org - imager.git/blob - draw.c
- reword and provide an example for non-proportionally scaling an
[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 static   int frac_sub     (frac x)  { return (x%16); }
386 static   int frac_int     (frac x)  { return (x/16); }
387 static float frac_to_float(float x) { return (float)x/16.0; }
388
389 static 
390 void
391 polar_to_plane(float cx, float cy, float angle, float radius, frac *x, frac *y) {
392   *x = float_to_frac(cx+radius*cos(angle));
393   *y = float_to_frac(cy+radius*sin(angle));
394 }
395
396 static
397 void
398 order_pair(frac *x, frac *y) {
399   frac t = *x;
400   if (t>*y) {
401     *x = *y;
402     *y = t;
403   }
404 }
405
406
407
408
409 static
410 void
411 make_minmax_list(i_mmarray *dot, float x, float y, float radius) {
412   float angle = 0.0;
413   float astep = radius>0.1 ? .5/radius : 10;
414   frac cx, cy, lx, ly, sx, sy;
415
416   mm_log((1, "make_minmax_list(dot %p, x %.2f, y %.2f, radius %.2f)\n", dot, x, y, radius));
417
418   polar_to_plane(x, y, angle, radius, &sx, &sy);
419   
420   for(angle = 0.0; angle<361; angle +=astep) {
421     lx = sx; ly = sy;
422     polar_to_plane(x, y, angle, radius, &cx, &cy);
423     sx = cx; sy = cy;
424
425     if (fabs(cx-lx) > fabs(cy-ly)) {
426       int ccx, ccy;
427       if (lx>cx) { 
428         ccx = lx; lx = cx; cx = ccx; 
429         ccy = ly; ly = cy; cy = ccy; 
430       }
431
432       for(ccx=lx; ccx<=cx; ccx++) {
433         ccy = ly + ((cy-ly)*(ccx-lx))/(cx-lx);
434         i_mmarray_add(dot, ccx, ccy);
435       }
436     } else {
437       int ccx, ccy;
438
439       if (ly>cy) { 
440         ccy = ly; ly = cy; cy = ccy; 
441         ccx = lx; lx = cx; cx = ccx; 
442       }
443       
444       for(ccy=ly; ccy<=cy; ccy++) {
445         if (cy-ly) ccx = lx + ((cx-lx)*(ccy-ly))/(cy-ly); else ccx = lx;
446         i_mmarray_add(dot, ccx, ccy);
447       }
448     }
449   }
450 }
451
452 /* Get the number of subpixels covered */
453
454 static
455 int
456 i_pixel_coverage(i_mmarray *dot, int x, int y) {
457   frac minx = x*16;
458   frac maxx = minx+15;
459   frac cy;
460   int cnt = 0;
461   
462   for(cy=y*16; cy<(y+1)*16; cy++) {
463     frac tmin = dot->data[cy].min;
464     frac tmax = dot->data[cy].max;
465
466     if (tmax == -1 || tmin > maxx || tmax < minx) continue;
467     
468     if (tmin < minx) tmin = minx;
469     if (tmax > maxx) tmax = maxx;
470     
471     cnt+=1+tmax-tmin;
472   }
473   return cnt;
474 }
475
476 /*
477 =item i_circle_aa(im, x, y, rad, color)
478
479 =category Drawing
480 =synopsis i_circle_aa(im, 50, 50, 45, &color);
481
482 Antialias fills a circle centered at (x,y) for radius I<rad> with
483 color.
484
485 =cut
486 */
487 void
488 i_circle_aa(i_img *im, float x, float y, float rad, const i_color *val) {
489   i_mmarray dot;
490   i_color temp;
491   int ly;
492
493   mm_log((1, "i_circle_aa(im %p, x %d, y %d, rad %.2f, val %p)\n", im, x, y, rad, val));
494
495   i_mmarray_cr(&dot,16*im->ysize);
496   make_minmax_list(&dot, x, y, rad);
497
498   for(ly = 0; ly<im->ysize; ly++) {
499     int ix, cy, minx = INT_MAX, maxx = INT_MIN;
500
501     /* Find the left/rightmost set subpixels */
502     for(cy = 0; cy<16; cy++) {
503       frac tmin = dot.data[ly*16+cy].min;
504       frac tmax = dot.data[ly*16+cy].max;
505       if (tmax == -1) continue;
506
507       if (minx > tmin) minx = tmin;
508       if (maxx < tmax) maxx = tmax;
509     }
510
511     if (maxx == INT_MIN) continue; /* no work to be done for this row of pixels */
512
513     minx /= 16;
514     maxx /= 16;
515     for(ix=minx; ix<=maxx; ix++) {
516       int cnt = i_pixel_coverage(&dot, ix, ly);
517       if (cnt>255) cnt = 255;
518       if (cnt) { /* should never be true */
519         int ch;
520         float ratio = (float)cnt/255.0;
521         i_gpix(im, ix, ly, &temp);
522         for(ch=0;ch<im->channels; ch++) temp.channel[ch] = (unsigned char)((float)val->channel[ch]*ratio + (float)temp.channel[ch]*(1.0-ratio));
523         i_ppix(im, ix, ly, &temp);
524       }
525     }
526   }
527   i_mmarray_dst(&dot);
528 }
529
530 /*
531 =item i_box(im, x1, y1, x2, y2, color)
532
533 =category Drawing
534 =synopsis i_box(im, 0, 0, im->xsize-1, im->ysize-1, &color).
535
536 Outlines the box from (x1,y1) to (x2,y2) inclusive with I<color>.
537
538 =cut
539 */
540
541 void
542 i_box(i_img *im,int x1,int y1,int x2,int y2,const i_color *val) {
543   int x,y;
544   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));
545   for(x=x1;x<x2+1;x++) {
546     i_ppix(im,x,y1,val);
547     i_ppix(im,x,y2,val);
548   }
549   for(y=y1;y<y2+1;y++) {
550     i_ppix(im,x1,y,val);
551     i_ppix(im,x2,y,val);
552   }
553 }
554
555 /*
556 =item i_box_filled(im, x1, y1, x2, y2, color)
557
558 =category Drawing
559 =synopsis i_box_filled(im, 0, 0, im->xsize-1, im->ysize-1, &color);
560
561 Fills the box from (x1,y1) to (x2,y2) inclusive with color.
562
563 =cut
564 */
565
566 void
567 i_box_filled(i_img *im,int x1,int y1,int x2,int y2, const i_color *val) {
568   int x,y;
569   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));
570   for(x=x1;x<x2+1;x++) for (y=y1;y<y2+1;y++) i_ppix(im,x,y,val);
571 }
572
573 /*
574 =item i_box_cfill(im, x1, y1, x2, y2, fill)
575
576 =category Drawing
577 =synopsis i_box_cfill(im, 0, 0, im->xsize-1, im->ysize-1, fill);
578
579 Fills the box from (x1,y1) to (x2,y2) inclusive with fill.
580
581 =cut
582 */
583
584 void
585 i_box_cfill(i_img *im,int x1,int y1,int x2,int y2,i_fill_t *fill) {
586   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));
587
588   ++x2;
589   if (x1 < 0)
590     x1 = 0;
591   if (y1 < 0) 
592     y1 = 0;
593   if (x2 > im->xsize) 
594     x2 = im->xsize;
595   if (y2 >= im->ysize)
596     y2 = im->ysize-1;
597   if (x1 >= x2 || y1 > y2)
598     return;
599   if (im->bits == i_8_bits && fill->fill_with_color) {
600     i_color *line = mymalloc(sizeof(i_color) * (x2 - x1)); /* checked 5jul05 tonyc */
601     i_color *work = NULL;
602     if (fill->combine)
603       work = mymalloc(sizeof(i_color) * (x2-x1)); /* checked 5jul05 tonyc */
604     while (y1 <= y2) {
605       if (fill->combine) {
606         i_glin(im, x1, x2, y1, line);
607         (fill->fill_with_color)(fill, x1, y1, x2-x1, im->channels, work);
608         (fill->combine)(line, work, im->channels, x2-x1);
609       }
610       else {
611         (fill->fill_with_color)(fill, x1, y1, x2-x1, im->channels, line);
612       }
613       i_plin(im, x1, x2, y1, line);
614       ++y1;
615     }
616     myfree(line);
617     if (work)
618       myfree(work);
619   }
620   else {
621     i_fcolor *line = mymalloc(sizeof(i_fcolor) * (x2 - x1)); /* checked 5jul05 tonyc */
622     i_fcolor *work;
623     work = mymalloc(sizeof(i_fcolor) * (x2 - x1)); /* checked 5jul05 tonyc */
624
625     while (y1 <= y2) {
626       if (fill->combine) {
627         i_glinf(im, x1, x2, y1, line);
628         (fill->fill_with_fcolor)(fill, x1, y1, x2-x1, im->channels, work);
629         (fill->combinef)(line, work, im->channels, x2-x1);
630       }
631       else {
632         (fill->fill_with_fcolor)(fill, x1, y1, x2-x1, im->channels, line);
633       }
634       i_plinf(im, x1, x2, y1, line);
635       ++y1;
636     }
637     myfree(line);
638     if (work)
639       myfree(work);
640   }
641 }
642
643
644 /* 
645 =item i_line(im, x1, y1, x2, y2, val, endp)
646
647 =category Drawing
648
649 Draw a line to image using bresenhams linedrawing algorithm
650
651    im   - image to draw to
652    x1   - starting x coordinate
653    y1   - starting x coordinate
654    x2   - starting x coordinate
655    y2   - starting x coordinate
656    val  - color to write to image
657    endp - endpoint flag (boolean)
658
659 =cut
660 */
661
662 void
663 i_line(i_img *im, int x1, int y1, int x2, int y2, const i_color *val, int endp) {
664   int x, y;
665   int dx, dy;
666   int p;
667
668   dx = x2 - x1;
669   dy = y2 - y1;
670
671
672   /* choose variable to iterate on */
673   if (abs(dx)>abs(dy)) {
674     int dx2, dy2, cpy;
675
676     /* sort by x */
677     if (x1 > x2) {
678       int t;
679       t = x1; x1 = x2; x2 = t;
680       t = y1; y1 = y2; y2 = t;
681     }
682     
683     dx = abs(dx);
684     dx2 = dx*2;
685     dy = y2 - y1;
686
687     if (dy<0) {
688       dy = -dy;
689       cpy = -1;
690     } else {
691       cpy = 1;
692     }
693     dy2 = dy*2;
694     p = dy2 - dx;
695
696     
697     y = y1;
698     for(x=x1; x<x2-1; x++) {
699       if (p<0) {
700         p += dy2;
701       } else {
702         y += cpy;
703         p += dy2-dx2;
704       }
705       i_ppix(im, x+1, y, val);
706     }
707   } else {
708     int dy2, dx2, cpx;
709
710     /* sort bx y */
711     if (y1 > y2) {
712       int t;
713       t = x1; x1 = x2; x2 = t;
714       t = y1; y1 = y2; y2 = t;
715     }
716     
717     dy = abs(dy);
718     dx = x2 - x1;
719     dy2 = dy*2;
720
721     if (dx<0) {
722       dx = -dx;
723       cpx = -1;
724     } else {
725       cpx = 1;
726     }
727     dx2 = dx*2;
728     p = dx2 - dy;
729
730     x = x1;
731     
732     for(y=y1; y<y2-1; y++) {
733       if (p<0) {
734         p  += dx2;
735       } else {
736         x += cpx;
737         p += dx2-dy2;
738       }
739       i_ppix(im, x, y+1, val);
740     }
741   }
742   if (endp) {
743     i_ppix(im, x1, y1, val);
744     i_ppix(im, x2, y2, val);
745   } else {
746     if (x1 != x2 || y1 != y2) 
747       i_ppix(im, x1, y1, val);
748   }
749 }
750
751
752 void
753 i_line_dda(i_img *im, int x1, int y1, int x2, int y2, i_color *val) {
754
755   float dy;
756   int x;
757   
758   for(x=x1; x<=x2; x++) {
759     dy = y1+ (x-x1)/(float)(x2-x1)*(y2-y1);
760     i_ppix(im, x, (int)(dy+0.5), val);
761   }
762 }
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790 void
791 i_line_aa3(i_img *im,int x1,int y1,int x2,int y2,i_color *val) {
792   i_color tval;
793   float alpha;
794   float dsec,dfrac;
795   int temp,dx,dy,isec,ch;
796
797   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));
798
799   dy=y2-y1;
800   dx=x2-x1;
801
802   if (abs(dx)>abs(dy)) { /* alpha < 1 */
803     if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
804     alpha=(float)(y2-y1)/(float)(x2-x1);
805
806     dsec=y1;
807     while(x1<=x2) {
808       isec=(int)dsec;
809       dfrac=dsec-isec;
810       /*      dfrac=1-(1-dfrac)*(1-dfrac); */
811       /* This is something we can play with to try to get better looking lines */
812
813       i_gpix(im,x1,isec,&tval);
814       for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)(dfrac*(float)tval.channel[ch]+(1-dfrac)*(float)val->channel[ch]);
815       i_ppix(im,x1,isec,&tval);
816       
817       i_gpix(im,x1,isec+1,&tval);
818       for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)((1-dfrac)*(float)tval.channel[ch]+dfrac*(float)val->channel[ch]);
819       i_ppix(im,x1,isec+1,&tval);
820       
821       dsec+=alpha;
822       x1++;
823     }
824   } else {
825     if (y2<y1) { temp=y1; y1=y2; y2=temp; temp=x1; x1=x2; x2=temp; }
826     alpha=(float)(x2-x1)/(float)(y2-y1);
827     dsec=x1;
828     while(y1<=y2) {
829       isec=(int)dsec;
830       dfrac=dsec-isec;
831       /*      dfrac=sqrt(dfrac); */
832       /* This is something we can play with */
833       i_gpix(im,isec,y1,&tval);
834       for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)(dfrac*(float)tval.channel[ch]+(1-dfrac)*(float)val->channel[ch]);
835       i_ppix(im,isec,y1,&tval);
836
837       i_gpix(im,isec+1,y1,&tval);
838       for(ch=0;ch<im->channels;ch++) tval.channel[ch]=(unsigned char)((1-dfrac)*(float)tval.channel[ch]+dfrac*(float)val->channel[ch]);
839       i_ppix(im,isec+1,y1,&tval);
840
841       dsec+=alpha;
842       y1++;
843     }
844   }
845 }
846
847
848 /*
849 =item i_line_aa(im, x1, x2, y1, y2, color, endp)
850
851 =category Drawing
852
853 Antialias draws a line from (x1,y1) to (x2, y2) in color.
854
855 The point (x2, y2) is drawn only if endp is set.
856
857 =cut
858 */
859
860 void
861 i_line_aa(i_img *im, int x1, int y1, int x2, int y2, const i_color *val, int endp) {
862   int x, y;
863   int dx, dy;
864   int p;
865
866   dx = x2 - x1;
867   dy = y2 - y1;
868
869   /* choose variable to iterate on */
870   if (abs(dx)>abs(dy)) {
871     int dx2, dy2, cpy;
872     
873     /* sort by x */
874     if (x1 > x2) {
875       int t;
876       t = x1; x1 = x2; x2 = t;
877       t = y1; y1 = y2; y2 = t;
878     }
879     
880     dx = abs(dx);
881     dx2 = dx*2;
882     dy = y2 - y1;
883
884     if (dy<0) {
885       dy = -dy;
886       cpy = -1;
887     } else {
888       cpy = 1;
889     }
890     dy2 = dy*2;
891     p = dy2 - dx2; /* this has to be like this for AA */
892     
893     y = y1;
894
895     for(x=x1; x<x2-1; x++) {
896       int ch;
897       i_color tval;
898       float t = (dy) ? -(float)(p)/(float)(dx2) : 1;
899       float t1, t2;
900
901       if (t<0) t = 0;
902       t1 = 1-t;
903       t2 = t;
904
905       i_gpix(im,x+1,y,&tval);
906       for(ch=0;ch<im->channels;ch++)
907         tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
908       i_ppix(im,x+1,y,&tval);
909
910       i_gpix(im,x+1,y+cpy,&tval);
911       for(ch=0;ch<im->channels;ch++)
912         tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
913       i_ppix(im,x+1,y+cpy,&tval);
914
915       if (p<0) {
916         p += dy2;
917       } else {
918         y += cpy;
919         p += dy2-dx2;
920       }
921     }
922   } else {
923     int dy2, dx2, cpx;
924
925     /* sort bx y */
926     if (y1 > y2) {
927       int t;
928       t = x1; x1 = x2; x2 = t;
929       t = y1; y1 = y2; y2 = t;
930     }
931     
932     dy = abs(dy);
933     dx = x2 - x1;
934     dy2 = dy*2;
935
936     if (dx<0) {
937       dx = -dx;
938       cpx = -1;
939     } else {
940       cpx = 1;
941     }
942     dx2 = dx*2;
943     p = dx2 - dy2; /* this has to be like this for AA */
944
945     x = x1;
946     
947     for(y=y1; y<y2-1; y++) {
948       int ch;
949       i_color tval;
950       float t = (dx) ? -(float)(p)/(float)(dy2) : 1;
951       float t1, t2;
952       
953       if (t<0) t = 0;
954       t1 = 1-t;
955       t2 = t;
956
957       i_gpix(im,x,y+1,&tval);
958       for(ch=0;ch<im->channels;ch++)
959         tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
960       i_ppix(im,x,y+1,&tval);
961
962       i_gpix(im,x+cpx,y+1,&tval);
963       for(ch=0;ch<im->channels;ch++)
964         tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
965       i_ppix(im,x+cpx,y+1,&tval);
966
967       if (p<0) {
968         p  += dx2;
969       } else {
970         x += cpx;
971         p += dx2-dy2;
972       }
973     }
974   }
975
976
977   if (endp) {
978     i_ppix(im, x1, y1, val);
979     i_ppix(im, x2, y2, val);
980   } else {
981     if (x1 != x2 || y1 != y2) 
982       i_ppix(im, x1, y1, val);
983   }
984 }
985
986
987
988 static double
989 perm(int n,int k) {
990   double r;
991   int i;
992   r=1;
993   for(i=k+1;i<=n;i++) r*=i;
994   for(i=1;i<=(n-k);i++) r/=i;
995   return r;
996 }
997
998
999 /* Note in calculating t^k*(1-t)^(n-k) 
1000    we can start by using t^0=1 so this simplifies to
1001    t^0*(1-t)^n - we want to multiply that with t/(1-t) each iteration
1002    to get a new level - this may lead to errors who knows lets test it */
1003
1004 void
1005 i_bezier_multi(i_img *im,int l,const double *x,const double *y, const i_color *val) {
1006   double *bzcoef;
1007   double t,cx,cy;
1008   int k,i;
1009   int lx = 0,ly = 0;
1010   int n=l-1;
1011   double itr,ccoef;
1012
1013   /* this is the same size as the x and y arrays, so shouldn't overflow */
1014   bzcoef=mymalloc(sizeof(double)*l); /* checked 5jul05 tonyc */
1015   for(k=0;k<l;k++) bzcoef[k]=perm(n,k);
1016   ICL_info(val);
1017
1018
1019   /*  for(k=0;k<l;k++) printf("bzcoef: %d -> %f\n",k,bzcoef[k]); */
1020   i=0;
1021   for(t=0;t<=1;t+=0.005) {
1022     cx=cy=0;
1023     itr=t/(1-t);
1024     ccoef=pow(1-t,n);
1025     for(k=0;k<l;k++) {
1026       /*      cx+=bzcoef[k]*x[k]*pow(t,k)*pow(1-t,n-k); 
1027               cy+=bzcoef[k]*y[k]*pow(t,k)*pow(1-t,n-k);*/
1028
1029       cx+=bzcoef[k]*x[k]*ccoef;
1030       cy+=bzcoef[k]*y[k]*ccoef;
1031       ccoef*=itr;
1032     }
1033     /*    printf("%f -> (%d,%d)\n",t,(int)(0.5+cx),(int)(0.5+cy)); */
1034     if (i++) { 
1035       i_line_aa(im,lx,ly,(int)(0.5+cx),(int)(0.5+cy),val, 1);
1036     }
1037       /*     i_ppix(im,(int)(0.5+cx),(int)(0.5+cy),val); */
1038     lx=(int)(0.5+cx);
1039     ly=(int)(0.5+cy);
1040   }
1041   ICL_info(val);
1042   myfree(bzcoef);
1043 }
1044
1045 /* Flood fill 
1046
1047    REF: Graphics Gems I. page 282+
1048
1049 */
1050
1051 /* This should be moved into a seperate file? */
1052
1053 /* This is the truncation used:
1054    
1055    a double is multiplied by 16 and then truncated.
1056    This means that 0 -> 0
1057    So a triangle of (0,0) (10,10) (10,0) Will look like it's
1058    not filling the (10,10) point nor the (10,0)-(10,10)  line segment
1059
1060 */
1061
1062
1063 /* Flood fill algorithm - based on the Ken Fishkins (pixar) gem in 
1064    graphics gems I */
1065
1066 /*
1067 struct stc {
1068   int mylx,myrx; 
1069   int dadlx,dadrx;
1070   int myy;
1071   int mydirection;
1072 };
1073
1074 Not used code???
1075 */
1076
1077
1078 struct stack_element {
1079   int myLx,myRx;
1080   int dadLx,dadRx;
1081   int myY;
1082   int myDirection;
1083 };
1084
1085
1086 /* create the link data to put push onto the stack */
1087
1088 static
1089 struct stack_element*
1090 crdata(int left,int right,int dadl,int dadr,int y, int dir) {
1091   struct stack_element *ste;
1092   ste              = mymalloc(sizeof(struct stack_element)); /* checked 5jul05 tonyc */
1093   ste->myLx        = left;
1094   ste->myRx        = right;
1095   ste->dadLx       = dadl;
1096   ste->dadRx       = dadr;
1097   ste->myY         = y;
1098   ste->myDirection = dir;
1099   return ste;
1100 }
1101
1102 /* i_ccomp compares two colors and gives true if they are the same */
1103
1104 static int
1105 i_ccomp(i_color *val1,i_color *val2,int ch) {
1106   int i;
1107   for(i=0;i<ch;i++) if (val1->channel[i] !=val2->channel[i]) return 0;
1108   return 1;
1109 }
1110
1111
1112 static int
1113 i_lspan(i_img *im, int seedx, int seedy, i_color *val) {
1114   i_color cval;
1115   while(1) {
1116     if (seedx-1 < 0) break;
1117     i_gpix(im,seedx-1,seedy,&cval);
1118     if (!i_ccomp(val,&cval,im->channels)) break;
1119     seedx--;
1120   }
1121   return seedx;
1122 }
1123
1124 static int
1125 i_rspan(i_img *im, int seedx, int seedy, i_color *val) {
1126   i_color cval;
1127   while(1) {
1128     if (seedx+1 > im->xsize-1) break;
1129     i_gpix(im,seedx+1,seedy,&cval);
1130     if (!i_ccomp(val,&cval,im->channels)) break;
1131     seedx++;
1132   }
1133   return seedx;
1134 }
1135
1136 /* Macro to create a link and push on to the list */
1137
1138 #define ST_PUSH(left,right,dadl,dadr,y,dir) do {                 \
1139   struct stack_element *s = crdata(left,right,dadl,dadr,y,dir);  \
1140   llist_push(st,&s);                                             \
1141 } while (0)
1142
1143 /* pops the shadow on TOS into local variables lx,rx,y,direction,dadLx and dadRx */
1144 /* No overflow check! */
1145  
1146 #define ST_POP() do {         \
1147   struct stack_element *s;    \
1148   llist_pop(st,&s);           \
1149   lx        = s->myLx;        \
1150   rx        = s->myRx;        \
1151   dadLx     = s->dadLx;       \
1152   dadRx     = s->dadRx;       \
1153   y         = s->myY;         \
1154   direction = s->myDirection; \
1155   myfree(s);                  \
1156 } while (0)
1157
1158 #define ST_STACK(dir,dadLx,dadRx,lx,rx,y) do {                    \
1159   int pushrx = rx+1;                                              \
1160   int pushlx = lx-1;                                              \
1161   ST_PUSH(lx,rx,pushlx,pushrx,y+dir,dir);                         \
1162   if (rx > dadRx)                                                 \
1163     ST_PUSH(dadRx+1,rx,pushlx,pushrx,y-dir,-dir);                 \
1164   if (lx < dadLx) ST_PUSH(lx,dadLx-1,pushlx,pushrx,y-dir,-dir);   \
1165 } while (0)
1166
1167 #define SET(x,y) btm_set(btm,x,y)
1168
1169 /* INSIDE returns true if pixel is correct color and we haven't set it before. */
1170 #define INSIDE(x,y) ((!btm_test(btm,x,y) && ( i_gpix(im,x,y,&cval),i_ccomp(&val,&cval,channels)  ) ))
1171
1172
1173
1174 /* The function that does all the real work */
1175
1176 static struct i_bitmap *
1177 i_flood_fill_low(i_img *im,int seedx,int seedy,
1178                  int *bxminp, int *bxmaxp, int *byminp, int *bymaxp) {
1179
1180   /*
1181     int lx,rx;
1182     int y;
1183     int direction;
1184     int dadLx,dadRx;
1185     int wasIn=0;
1186   */
1187   int ltx, rtx;
1188   int tx = 0;
1189
1190   int bxmin = seedx;
1191   int bxmax = seedx;
1192   int bymin = seedy;
1193   int bymax = seedy;
1194
1195   struct llist *st;
1196   struct i_bitmap *btm;
1197
1198   int channels,xsize,ysize;
1199   i_color cval,val;
1200
1201   channels = im->channels;
1202   xsize    = im->xsize;
1203   ysize    = im->ysize;
1204
1205   btm = btm_new(xsize, ysize);
1206   st  = llist_new(100, sizeof(struct stack_element*));
1207
1208   /* Get the reference color */
1209   i_gpix(im, seedx, seedy, &val);
1210
1211   /* Find the starting span and fill it */
1212   ltx = i_lspan(im, seedx, seedy, &val);
1213   rtx = i_rspan(im, seedx, seedy, &val);
1214   for(tx=ltx; tx<=rtx; tx++) SET(tx, seedy);
1215
1216   ST_PUSH(ltx, rtx, ltx, rtx, seedy+1,  1);
1217   ST_PUSH(ltx, rtx, ltx, rtx, seedy-1, -1);
1218
1219   while(st->count) {
1220     /* Stack variables */
1221     int lx,rx;
1222     int dadLx,dadRx;
1223     int y;
1224     int direction;
1225
1226     int x;
1227     int wasIn=0;
1228
1229     ST_POP(); /* sets lx, rx, dadLx, dadRx, y, direction */
1230
1231
1232     if (y<0 || y>ysize-1) continue;
1233     if (bymin > y) bymin=y; /* in the worst case an extra line */
1234     if (bymax < y) bymax=y; 
1235
1236
1237     x = lx+1;
1238     if ( lx >= 0 && (wasIn = INSIDE(lx, y)) ) {
1239       SET(lx, y);
1240       lx--;
1241       while(INSIDE(lx, y) && lx > 0) {
1242         SET(lx,y);
1243         lx--;
1244       }
1245     }
1246
1247     if (bxmin > lx) bxmin = lx;
1248     while(x <= xsize-1) {
1249       /*  printf("x=%d\n",x); */
1250       if (wasIn) {
1251         
1252         if (INSIDE(x, y)) {
1253           /* case 1: was inside, am still inside */
1254           SET(x,y);
1255         } else {
1256           /* case 2: was inside, am no longer inside: just found the
1257              right edge of a span */
1258           ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1259
1260           if (bxmax < x) bxmax = x;
1261           wasIn=0;
1262         }
1263       } else {
1264         if (x > rx) goto EXT;
1265         if (INSIDE(x, y)) {
1266           SET(x, y);
1267           /* case 3: Wasn't inside, am now: just found the start of a new run */
1268           wasIn = 1;
1269             lx = x;
1270         } else {
1271           /* case 4: Wasn't inside, still isn't */
1272         }
1273       }
1274       x++;
1275     }
1276   EXT: /* out of loop */
1277     if (wasIn) {
1278       /* hit an edge of the frame buffer while inside a run */
1279       ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1280       if (bxmax < x) bxmax = x;
1281     }
1282   }
1283
1284   llist_destroy(st);
1285
1286   *bxminp = bxmin;
1287   *bxmaxp = bxmax;
1288   *byminp = bymin;
1289   *bymaxp = bymax;
1290
1291   return btm;
1292 }
1293
1294 /*
1295 =item i_flood_fill(im, seedx, seedy, color)
1296
1297 =category Drawing
1298 =synopsis i_flood_fill(im, 50, 50, &color);
1299
1300 Flood fills the 4-connected region starting from the point (seedx,
1301 seedy) with I<color>.
1302
1303 Returns false if (seedx, seedy) are outside the image.
1304
1305 =cut
1306 */
1307
1308 undef_int
1309 i_flood_fill(i_img *im, int seedx, int seedy, const i_color *dcol) {
1310   int bxmin, bxmax, bymin, bymax;
1311   struct i_bitmap *btm;
1312   int x, y;
1313
1314   i_clear_error();
1315   if (seedx < 0 || seedx >= im->xsize ||
1316       seedy < 0 || seedy >= im->ysize) {
1317     i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
1318     return 0;
1319   }
1320
1321   btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax);
1322
1323   for(y=bymin;y<=bymax;y++)
1324     for(x=bxmin;x<=bxmax;x++)
1325       if (btm_test(btm,x,y)) 
1326         i_ppix(im,x,y,dcol);
1327   btm_destroy(btm);
1328   return 1;
1329 }
1330
1331 /*
1332 =item i_flood_cfill(im, seedx, seedy, fill)
1333
1334 =category Drawing
1335 =synopsis i_flood_cfill(im, 50, 50, fill);
1336
1337 Flood fills the 4-connected region starting from the point (seedx,
1338 seedy) with I<fill>.
1339
1340 Returns false if (seedx, seedy) are outside the image.
1341
1342 =cut
1343 */
1344
1345 undef_int
1346 i_flood_cfill(i_img *im, int seedx, int seedy, i_fill_t *fill) {
1347   int bxmin, bxmax, bymin, bymax;
1348   struct i_bitmap *btm;
1349   int x, y;
1350   int start;
1351
1352   i_clear_error();
1353   
1354   if (seedx < 0 || seedx >= im->xsize ||
1355       seedy < 0 || seedy >= im->ysize) {
1356     i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
1357     return 0;
1358   }
1359
1360   btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax);
1361
1362   if (im->bits == i_8_bits && fill->fill_with_color) {
1363     /* bxmax/bxmin are inside the image, hence this won't overflow */
1364     i_color *line = mymalloc(sizeof(i_color) * (bxmax - bxmin)); /* checked 5jul05 tonyc */
1365     i_color *work = NULL;
1366     if (fill->combine)
1367       work = mymalloc(sizeof(i_color) * (bxmax - bxmin)); /* checked 5jul05 tonyc */
1368
1369     for(y=bymin; y<=bymax; y++) {
1370       x = bxmin;
1371       while (x < bxmax) {
1372         while (x < bxmax && !btm_test(btm, x, y)) {
1373           ++x;
1374         }
1375         if (btm_test(btm, x, y)) {
1376           start = x;
1377           while (x < bxmax && btm_test(btm, x, y)) {
1378             ++x;
1379           }
1380           if (fill->combine) {
1381             i_glin(im, start, x, y, line);
1382             (fill->fill_with_color)(fill, start, y, x-start, im->channels, 
1383                                     work);
1384             (fill->combine)(line, work, im->channels, x-start);
1385           }
1386           else {
1387             (fill->fill_with_color)(fill, start, y, x-start, im->channels, 
1388                                     line);
1389           }
1390           i_plin(im, start, x, y, line);
1391         }
1392       }
1393     }
1394     myfree(line);
1395     if (work)
1396       myfree(work);
1397   }
1398   else {
1399     /* bxmax/bxmin are inside the image, hence this won't overflow */
1400     i_fcolor *line = mymalloc(sizeof(i_fcolor) * (bxmax - bxmin)); /* checked 5jul05 tonyc */
1401     i_fcolor *work = NULL;
1402     if (fill->combinef)
1403       work = mymalloc(sizeof(i_fcolor) * (bxmax - bxmin)); /* checked 5jul05 tonyc */
1404     
1405     for(y=bymin;y<=bymax;y++) {
1406       x = bxmin;
1407       while (x < bxmax) {
1408         while (x < bxmax && !btm_test(btm, x, y)) {
1409           ++x;
1410         }
1411         if (btm_test(btm, x, y)) {
1412           start = x;
1413           while (x < bxmax && btm_test(btm, x, y)) {
1414             ++x;
1415           }
1416           if (fill->combinef) {
1417             i_glinf(im, start, x, y, line);
1418             (fill->fill_with_fcolor)(fill, start, y, x-start, im->channels, 
1419                                     work);
1420             (fill->combinef)(line, work, im->channels, x-start);
1421           }
1422           else {
1423             (fill->fill_with_fcolor)(fill, start, y, x-start, im->channels, 
1424                                     line);
1425           }
1426           i_plinf(im, start, x, y, line);
1427         }
1428       }
1429     }
1430     myfree(line);
1431     if (work)
1432       myfree(work);
1433   }
1434
1435   btm_destroy(btm);
1436   return 1;
1437 }