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