Extra ways of calling translate(), shear()
[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
3efb0915
TC
8static void
9cfill_from_btm(i_img *im, i_fill_t *fill, struct i_bitmap *btm,
10 int bxmin, int bxmax, int bymin, int bymax);
11
02d1d628
AMH
12void
13i_mmarray_cr(i_mmarray *ar,int l) {
14 int i;
f0960b14 15 int alloc_size;
02d1d628
AMH
16
17 ar->lines=l;
f0960b14
TC
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 */
02d1d628
AMH
25 for(i=0;i<l;i++) { ar->data[i].max=-1; ar->data[i].min=MAXINT; }
26}
27
28void
29i_mmarray_dst(i_mmarray *ar) {
30 ar->lines=0;
31 if (ar->data != NULL) { myfree(ar->data); ar->data=NULL; }
32}
33
34void
35i_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
43int
44i_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
49int
50i_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
55void
56i_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
f1ac5027
TC
61void
62i_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) {
f0960b14 65 i_color *line = mymalloc(sizeof(i_color) * im->xsize); /* checked 5jul05 tonyc */
efdc2568
TC
66 i_color *work = NULL;
67 if (fill->combine)
f0960b14 68 work = mymalloc(sizeof(i_color) * im->xsize); /* checked 5jul05 tonyc */
f1ac5027
TC
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
43c5dacb 74 if (fill->combine) {
f1ac5027 75 i_glin(im, x, x+w, y, line);
43c5dacb
TC
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 }
f1ac5027
TC
82 i_plin(im, x, x+w, y, line);
83 }
84 }
85
86 myfree(line);
efdc2568
TC
87 if (work)
88 myfree(work);
f1ac5027
TC
89 }
90 else {
f0960b14 91 i_fcolor *line = mymalloc(sizeof(i_fcolor) * im->xsize); /* checked 5jul05 tonyc */
efdc2568
TC
92 i_fcolor *work = NULL;
93 if (fill->combinef)
f0960b14 94 work = mymalloc(sizeof(i_fcolor) * im->xsize); /* checked 5jul05 tonyc */
f1ac5027
TC
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
43c5dacb 100 if (fill->combinef) {
f1ac5027 101 i_glinf(im, x, x+w, y, line);
43c5dacb
TC
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 }
f1ac5027
TC
108 i_plinf(im, x, x+w, y, line);
109 }
110 }
111
112 myfree(line);
efdc2568
TC
113 if (work)
114 myfree(work);
f1ac5027
TC
115 }
116}
117
02d1d628
AMH
118
119static
120void
121i_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);
b254292b 126 if (fabs(alpha) <= 1)
02d1d628
AMH
127 {
128 if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
129 dsec=y1;
b254292b 130 while(x1<=x2)
02d1d628 131 {
02d1d628 132 i_mmarray_add(ar,x1,(int)(dsec+0.5));
b254292b 133 dsec+=alpha;
02d1d628
AMH
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;
b254292b 142 while(y1<=y2)
02d1d628 143 {
02d1d628 144 i_mmarray_add(ar,(int)(dsec+0.5),y1);
b254292b 145 dsec+=alpha;
02d1d628
AMH
146 y1++;
147 }
148 }
149}
150
151void
152i_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
a8652edf
TC
158static void
159i_arc_minmax(i_int_hlines *hlines,int x,int y,float rad,float d1,float d2) {
02d1d628
AMH
160 i_mmarray dot;
161 float f,fx,fy;
162 int x1,y1;
163
a8652edf 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));*/
02d1d628 165
a8652edf 166 i_mmarray_cr(&dot, hlines->limit_y);
02d1d628
AMH
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)));
6af18d2b 179
02d1d628
AMH
180 /* printf("x1: %d.\ny1: %d.\n",x1,y1); */
181 i_arcdraw(x, y, x1, y1, &dot);
182
a8652edf
TC
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
02d1d628 193 /* dot.info(); */
7f882a01 194 i_mmarray_dst(&dot);
02d1d628
AMH
195}
196
a8652edf
TC
197static void
198i_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
92bda632
TC
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
214Fills an arc centered at (x,y) with radius I<rad> covering the range
215of angles in degrees from d1 to d2, with the color.
216
217=cut
218*/
219
a8652edf 220void
97ac0a96 221i_arc(i_img *im,int x,int y,float rad,float d1,float d2,const i_color *val) {
a8652edf
TC
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
92bda632
TC
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
239Fills an arc centered at (x,y) with radius I<rad> covering the range
240of angles in degrees from d1 to d2, with the fill object.
241
242=cut
243*/
244
a8652edf
TC
245#define MIN_CIRCLE_STEPS 8
246#define MAX_CIRCLE_STEPS 360
247
f1ac5027
TC
248void
249i_arc_cfill(i_img *im,int x,int y,float rad,float d1,float d2,i_fill_t *fill) {
a8652edf 250 i_int_hlines hlines;
f1ac5027 251
a8652edf 252 i_int_init_hlines_img(&hlines, im);
f1ac5027 253
a8652edf 254 i_arc_hlines(&hlines, x, y, rad, d1, d2);
f1ac5027 255
a8652edf 256 i_int_hlines_fill_fill(im, &hlines, fill);
f1ac5027 257
a8652edf
TC
258 i_int_hlines_destroy(&hlines);
259}
f1ac5027 260
a8652edf
TC
261static void
262arc_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 */
e310e5f9
TC
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 */
a8652edf
TC
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 }
f1ac5027 326
a8652edf
TC
327 /* finish off the curve */
328 (*xvals)[*count] = x + rad * cos(d2_rad);
329 (*yvals)[*count] = y + rad * sin(d2_rad);
330 ++*count;
331}
f1ac5027 332
92bda632
TC
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
339Antialias fills an arc centered at (x,y) with radius I<rad> covering
340the range of angles in degrees from d1 to d2, with the color.
341
342=cut
343*/
344
a8652edf
TC
345void
346i_arc_aa(i_img *im, double x, double y, double rad, double d1, double d2,
97ac0a96 347 const i_color *val) {
a8652edf
TC
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);
f1ac5027
TC
357}
358
92bda632
TC
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
365Antialias fills an arc centered at (x,y) with radius I<rad> covering
366the range of angles in degrees from d1 to d2, with the fill object.
367
368=cut
369*/
370
a8652edf
TC
371void
372i_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);
6af18d2b 380
a8652edf
TC
381 myfree(xvals);
382 myfree(yvals);
383}
6af18d2b
AMH
384
385/* Temporary AA HACK */
386
387
388typedef int frac;
389static frac float_to_frac(float x) { return (frac)(0.5+x*16.0); }
6af18d2b
AMH
390
391static
392void
393polar_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
6af18d2b
AMH
398static
399void
400make_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) {
6af18d2b
AMH
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
443static
444int
445i_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
92bda632
TC
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
471Antialias fills a circle centered at (x,y) for radius I<rad> with
472color.
473
474=cut
475*/
6af18d2b 476void
97ac0a96 477i_circle_aa(i_img *im, float x, float y, float rad, const i_color *val) {
6af18d2b
AMH
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++) {
a659442a 488 int ix, cy, minx = INT_MAX, maxx = INT_MIN;
6af18d2b
AMH
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 }
4b19f77a 516 i_mmarray_dst(&dot);
6af18d2b
AMH
517}
518
92bda632
TC
519/*
520=item i_box(im, x1, y1, x2, y2, color)
6af18d2b 521
92bda632
TC
522=category Drawing
523=synopsis i_box(im, 0, 0, im->xsize-1, im->ysize-1, &color).
6af18d2b 524
92bda632 525Outlines the box from (x1,y1) to (x2,y2) inclusive with I<color>.
6af18d2b 526
92bda632
TC
527=cut
528*/
6af18d2b 529
02d1d628 530void
97ac0a96 531i_box(i_img *im,int x1,int y1,int x2,int y2,const i_color *val) {
02d1d628
AMH
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
92bda632
TC
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
550Fills the box from (x1,y1) to (x2,y2) inclusive with color.
551
552=cut
553*/
554
02d1d628 555void
97ac0a96 556i_box_filled(i_img *im,int x1,int y1,int x2,int y2, const i_color *val) {
02d1d628
AMH
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
92bda632
TC
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
568Fills the box from (x1,y1) to (x2,y2) inclusive with fill.
569
570=cut
571*/
572
f1ac5027
TC
573void
574i_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;
f0960b14
TC
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;
f1ac5027 588 if (im->bits == i_8_bits && fill->fill_with_color) {
f0960b14 589 i_color *line = mymalloc(sizeof(i_color) * (x2 - x1)); /* checked 5jul05 tonyc */
efdc2568
TC
590 i_color *work = NULL;
591 if (fill->combine)
f0960b14 592 work = mymalloc(sizeof(i_color) * (x2-x1)); /* checked 5jul05 tonyc */
f1ac5027 593 while (y1 <= y2) {
43c5dacb 594 if (fill->combine) {
f1ac5027 595 i_glin(im, x1, x2, y1, line);
43c5dacb
TC
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 }
f1ac5027
TC
602 i_plin(im, x1, x2, y1, line);
603 ++y1;
604 }
605 myfree(line);
efdc2568
TC
606 if (work)
607 myfree(work);
f1ac5027
TC
608 }
609 else {
f0960b14 610 i_fcolor *line = mymalloc(sizeof(i_fcolor) * (x2 - x1)); /* checked 5jul05 tonyc */
efdc2568 611 i_fcolor *work;
f0960b14 612 work = mymalloc(sizeof(i_fcolor) * (x2 - x1)); /* checked 5jul05 tonyc */
efdc2568 613
f1ac5027 614 while (y1 <= y2) {
43c5dacb 615 if (fill->combine) {
f1ac5027 616 i_glinf(im, x1, x2, y1, line);
43c5dacb
TC
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 }
f1ac5027 623 i_plinf(im, x1, x2, y1, line);
2de568dc 624 ++y1;
f1ac5027
TC
625 }
626 myfree(line);
efdc2568
TC
627 if (work)
628 myfree(work);
f1ac5027
TC
629 }
630}
02d1d628 631
aa833c97
AMH
632
633/*
634=item i_line(im, x1, y1, x2, y2, val, endp)
635
92bda632
TC
636=category Drawing
637
aa833c97
AMH
638Draw 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
02d1d628 651void
97ac0a96 652i_line(i_img *im, int x1, int y1, int x2, int y2, const i_color *val, int endp) {
aa833c97
AMH
653 int x, y;
654 int dx, dy;
655 int p;
02d1d628 656
aa833c97
AMH
657 dx = x2 - x1;
658 dy = y2 - y1;
02d1d628 659
aa833c97
AMH
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;
02d1d628 670 }
aa833c97
AMH
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);
02d1d628 695 }
aa833c97
AMH
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 }
02d1d628
AMH
738}
739
aa833c97 740
02d1d628 741void
b437ce0a
AMH
742i_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);
135fb460 749 i_ppix(im, x, (int)(dy+0.5), val);
b437ce0a
AMH
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
779void
780i_line_aa3(i_img *im,int x1,int y1,int x2,int y2,i_color *val) {
02d1d628
AMH
781 i_color tval;
782 float alpha;
783 float dsec,dfrac;
784 int temp,dx,dy,isec,ch;
785
b437ce0a 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));
02d1d628
AMH
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
b437ce0a 836
92bda632
TC
837/*
838=item i_line_aa(im, x1, x2, y1, y2, color, endp)
839
840=category Drawing
841
842Antialias draws a line from (x1,y1) to (x2, y2) in color.
b437ce0a 843
92bda632
TC
844The point (x2, y2) is drawn only if endp is set.
845
846=cut
847*/
b437ce0a
AMH
848
849void
97ac0a96 850i_line_aa(i_img *im, int x1, int y1, int x2, int y2, const i_color *val, int endp) {
b437ce0a
AMH
851 int x, y;
852 int dx, dy;
853 int p;
b437ce0a
AMH
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
b33c08f8 977static double
02d1d628
AMH
978perm(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
993void
97ac0a96 994i_bezier_multi(i_img *im,int l,const double *x,const double *y, const i_color *val) {
02d1d628
AMH
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
f0960b14
TC
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 */
02d1d628
AMH
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++) {
b437ce0a 1024 i_line_aa(im,lx,ly,(int)(0.5+cx),(int)(0.5+cy),val, 1);
02d1d628
AMH
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
02d1d628
AMH
1034/* Flood fill
1035
1036 REF: Graphics Gems I. page 282+
1037
1038*/
1039
02d1d628
AMH
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
02d1d628
AMH
1052/* Flood fill algorithm - based on the Ken Fishkins (pixar) gem in
1053 graphics gems I */
1054
1055/*
1056struct stc {
1057 int mylx,myrx;
1058 int dadlx,dadrx;
1059 int myy;
1060 int mydirection;
1061};
1062
1063Not used code???
1064*/
1065
1066
1067struct 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
1077static
1078struct stack_element*
1079crdata(int left,int right,int dadl,int dadr,int y, int dir) {
1080 struct stack_element *ste;
f0960b14 1081 ste = mymalloc(sizeof(struct stack_element)); /* checked 5jul05 tonyc */
a73aeb5f
AMH
1082 ste->myLx = left;
1083 ste->myRx = right;
1084 ste->dadLx = dadl;
1085 ste->dadRx = dadr;
1086 ste->myY = y;
1087 ste->myDirection = dir;
02d1d628
AMH
1088 return ste;
1089}
1090
1091/* i_ccomp compares two colors and gives true if they are the same */
1092
3efb0915
TC
1093typedef int (*ff_cmpfunc)(i_color const *c1, i_color const *c2, int channels);
1094
02d1d628 1095static int
3efb0915 1096i_ccomp_normal(i_color const *val1, i_color const *val2, int ch) {
02d1d628 1097 int i;
3efb0915
TC
1098 for(i = 0; i < ch; i++)
1099 if (val1->channel[i] !=val2->channel[i])
1100 return 0;
02d1d628
AMH
1101 return 1;
1102}
1103
3efb0915
TC
1104static int
1105i_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}
02d1d628
AMH
1112
1113static int
3efb0915 1114i_lspan(i_img *im, int seedx, int seedy, i_color const *val, ff_cmpfunc cmpfunc) {
02d1d628
AMH
1115 i_color cval;
1116 while(1) {
1117 if (seedx-1 < 0) break;
1118 i_gpix(im,seedx-1,seedy,&cval);
3efb0915
TC
1119 if (!cmpfunc(val,&cval,im->channels))
1120 break;
02d1d628
AMH
1121 seedx--;
1122 }
1123 return seedx;
1124}
1125
1126static int
3efb0915 1127i_rspan(i_img *im, int seedx, int seedy, i_color const *val, ff_cmpfunc cmpfunc) {
02d1d628
AMH
1128 i_color cval;
1129 while(1) {
1130 if (seedx+1 > im->xsize-1) break;
1131 i_gpix(im,seedx+1,seedy,&cval);
3efb0915 1132 if (!cmpfunc(val,&cval,im->channels)) break;
02d1d628
AMH
1133 seedx++;
1134 }
1135 return seedx;
1136}
1137
1138/* Macro to create a link and push on to the list */
1139
e25e59b1
AMH
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)
02d1d628
AMH
1144
1145/* pops the shadow on TOS into local variables lx,rx,y,direction,dadLx and dadRx */
1146/* No overflow check! */
1147
e25e59b1
AMH
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)
02d1d628 1170
86d20cb9 1171/* INSIDE returns true if pixel is correct color and we haven't set it before. */
3efb0915 1172#define INSIDE(x,y, seed) ((!btm_test(btm,x,y) && ( i_gpix(im,x,y,&cval),cmpfunc(seed,&cval,channels) ) ))
02d1d628 1173
02d1d628 1174
02d1d628 1175
aa833c97
AMH
1176/* The function that does all the real work */
1177
1178static struct i_bitmap *
1179i_flood_fill_low(i_img *im,int seedx,int seedy,
3efb0915
TC
1180 int *bxminp, int *bxmaxp, int *byminp, int *bymaxp,
1181 i_color const *seed, ff_cmpfunc cmpfunc) {
aa833c97
AMH
1182 int ltx, rtx;
1183 int tx = 0;
02d1d628 1184
e25e59b1
AMH
1185 int bxmin = seedx;
1186 int bxmax = seedx;
1187 int bymin = seedy;
1188 int bymax = seedy;
02d1d628
AMH
1189
1190 struct llist *st;
1191 struct i_bitmap *btm;
1192
1193 int channels,xsize,ysize;
3efb0915 1194 i_color cval;
02d1d628 1195
a73aeb5f
AMH
1196 channels = im->channels;
1197 xsize = im->xsize;
1198 ysize = im->ysize;
02d1d628 1199
86d20cb9
AMH
1200 btm = btm_new(xsize, ysize);
1201 st = llist_new(100, sizeof(struct stack_element*));
02d1d628 1202
02d1d628 1203 /* Find the starting span and fill it */
3efb0915
TC
1204 ltx = i_lspan(im, seedx, seedy, seed, cmpfunc);
1205 rtx = i_rspan(im, seedx, seedy, seed, cmpfunc);
aa833c97 1206 for(tx=ltx; tx<=rtx; tx++) SET(tx, seedy);
02d1d628 1207
aa833c97
AMH
1208 ST_PUSH(ltx, rtx, ltx, rtx, seedy+1, 1);
1209 ST_PUSH(ltx, rtx, ltx, rtx, seedy-1, -1);
02d1d628
AMH
1210
1211 while(st->count) {
aa833c97
AMH
1212 /* Stack variables */
1213 int lx,rx;
1214 int dadLx,dadRx;
1215 int y;
1216 int direction;
e25e59b1 1217
aa833c97
AMH
1218 int x;
1219 int wasIn=0;
02d1d628 1220
aa833c97
AMH
1221 ST_POP(); /* sets lx, rx, dadLx, dadRx, y, direction */
1222
1223
1224 if (y<0 || y>ysize-1) continue;
02d1d628
AMH
1225 if (bymin > y) bymin=y; /* in the worst case an extra line */
1226 if (bymax < y) bymax=y;
1227
e25e59b1
AMH
1228
1229 x = lx+1;
3efb0915 1230 if ( lx >= 0 && (wasIn = INSIDE(lx, y, seed)) ) {
aa833c97 1231 SET(lx, y);
02d1d628 1232 lx--;
3efb0915 1233 while(INSIDE(lx, y, seed) && lx > 0) {
02d1d628
AMH
1234 SET(lx,y);
1235 lx--;
1236 }
1237 }
1238
86d20cb9 1239 if (bxmin > lx) bxmin = lx;
02d1d628
AMH
1240 while(x <= xsize-1) {
1241 /* printf("x=%d\n",x); */
1242 if (wasIn) {
1243
3efb0915 1244 if (INSIDE(x, y, seed)) {
02d1d628
AMH
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 */
aa833c97 1250 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
02d1d628 1251
aa833c97 1252 if (bxmax < x) bxmax = x;
02d1d628
AMH
1253 wasIn=0;
1254 }
1255 } else {
aa833c97 1256 if (x > rx) goto EXT;
3efb0915 1257 if (INSIDE(x, y, seed)) {
aa833c97 1258 SET(x, y);
02d1d628 1259 /* case 3: Wasn't inside, am now: just found the start of a new run */
aa833c97
AMH
1260 wasIn = 1;
1261 lx = x;
02d1d628
AMH
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 */
aa833c97
AMH
1271 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1272 if (bxmax < x) bxmax = x;
02d1d628
AMH
1273 }
1274 }
02d1d628 1275
02d1d628 1276 llist_destroy(st);
cc6483e0 1277
aa833c97
AMH
1278 *bxminp = bxmin;
1279 *bxmaxp = bxmax;
1280 *byminp = bymin;
1281 *bymaxp = bymax;
cc6483e0 1282
aa833c97
AMH
1283 return btm;
1284}
cc6483e0 1285
92bda632
TC
1286/*
1287=item i_flood_fill(im, seedx, seedy, color)
1288
1289=category Drawing
1290=synopsis i_flood_fill(im, 50, 50, &color);
cc6483e0 1291
92bda632
TC
1292Flood fills the 4-connected region starting from the point (seedx,
1293seedy) with I<color>.
cc6483e0 1294
92bda632
TC
1295Returns false if (seedx, seedy) are outside the image.
1296
1297=cut
1298*/
cc6483e0 1299
aa833c97 1300undef_int
97ac0a96 1301i_flood_fill(i_img *im, int seedx, int seedy, const i_color *dcol) {
aa833c97
AMH
1302 int bxmin, bxmax, bymin, bymax;
1303 struct i_bitmap *btm;
1304 int x, y;
3efb0915 1305 i_color val;
cc6483e0 1306
aa833c97
AMH
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;
cc6483e0 1312 }
cc6483e0 1313
3efb0915
TC
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);
cc6483e0 1319
aa833c97
AMH
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;
cc6483e0
TC
1326}
1327
92bda632
TC
1328/*
1329=item i_flood_cfill(im, seedx, seedy, fill)
1330
1331=category Drawing
1332=synopsis i_flood_cfill(im, 50, 50, fill);
aa833c97 1333
92bda632
TC
1334Flood fills the 4-connected region starting from the point (seedx,
1335seedy) with I<fill>.
1336
1337Returns false if (seedx, seedy) are outside the image.
1338
1339=cut
1340*/
aa833c97 1341
a321d497 1342undef_int
cc6483e0
TC
1343i_flood_cfill(i_img *im, int seedx, int seedy, i_fill_t *fill) {
1344 int bxmin, bxmax, bymin, bymax;
1345 struct i_bitmap *btm;
3efb0915 1346 i_color val;
cc6483e0 1347
aa833c97 1348 i_clear_error();
a321d497
AMH
1349
1350 if (seedx < 0 || seedx >= im->xsize ||
1351 seedy < 0 || seedy >= im->ysize) {
3e1e2ed3 1352 i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
a321d497
AMH
1353 return 0;
1354 }
1355
3efb0915
TC
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
1374Flood fills the 4-connected region starting from the point (seedx,
1375seedy) with I<color>, fill stops when the fill reaches a pixels with
1376color I<border>.
1377
1378Returns false if (seedx, seedy) are outside the image.
1379
1380=cut
1381*/
1382
1383undef_int
1384i_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
1414Flood fills the 4-connected region starting from the point (seedx,
1415seedy) with I<fill>, the fill stops when it reaches pixels of color
1416I<border>.
1417
1418Returns false if (seedx, seedy) are outside the image.
1419
1420=cut
1421*/
1422
1423undef_int
1424i_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
1447static void
1448cfill_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;
cc6483e0
TC
1452
1453 if (im->bits == i_8_bits && fill->fill_with_color) {
f0960b14
TC
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 */
efdc2568
TC
1456 i_color *work = NULL;
1457 if (fill->combine)
f0960b14 1458 work = mymalloc(sizeof(i_color) * (bxmax - bxmin)); /* checked 5jul05 tonyc */
cc6483e0 1459
aa833c97 1460 for(y=bymin; y<=bymax; y++) {
cc6483e0
TC
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 }
43c5dacb 1471 if (fill->combine) {
cc6483e0 1472 i_glin(im, start, x, y, line);
43c5dacb
TC
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 }
cc6483e0
TC
1481 i_plin(im, start, x, y, line);
1482 }
1483 }
1484 }
1485 myfree(line);
efdc2568
TC
1486 if (work)
1487 myfree(work);
cc6483e0
TC
1488 }
1489 else {
f0960b14
TC
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 */
efdc2568
TC
1492 i_fcolor *work = NULL;
1493 if (fill->combinef)
f0960b14 1494 work = mymalloc(sizeof(i_fcolor) * (bxmax - bxmin)); /* checked 5jul05 tonyc */
cc6483e0
TC
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 }
43c5dacb 1507 if (fill->combinef) {
cc6483e0 1508 i_glinf(im, start, x, y, line);
43c5dacb
TC
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 }
cc6483e0
TC
1517 i_plinf(im, start, x, y, line);
1518 }
1519 }
1520 }
1521 myfree(line);
efdc2568
TC
1522 if (work)
1523 myfree(work);
cc6483e0 1524 }
cc6483e0 1525}