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