]> git.imager.perl.org - imager.git/blame - draw.c
[rt #69170] update the bundled Devel::CheckLib with original dist changes
[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"
9b1ec2b8 5#include "imrender.h"
6af18d2b
AMH
6#include <limits.h>
7
40068b33
TC
8int
9i_ppix_norm(i_img *im, i_img_dim x, i_img_dim y, i_color const *col) {
10 i_color src;
11 i_color work;
12 int dest_alpha;
13 int remains;
14
15 if (!col->channel[3])
16 return 0;
17
18 switch (im->channels) {
19 case 1:
20 work = *col;
21 i_adapt_colors(2, 4, &work, 1);
22 i_gpix(im, x, y, &src);
23 remains = 255 - work.channel[1];
24 src.channel[0] = (src.channel[0] * remains
25 + work.channel[0] * work.channel[1]) / 255;
26 return i_ppix(im, x, y, &src);
27
28 case 2:
29 work = *col;
30 i_adapt_colors(2, 4, &work, 1);
31 i_gpix(im, x, y, &src);
8d14daab 32 remains = 255 - work.channel[1];
40068b33
TC
33 dest_alpha = work.channel[1] + remains * src.channel[1] / 255;
34 if (work.channel[1] == 255) {
35 return i_ppix(im, x, y, &work);
36 }
37 else {
38 src.channel[0] = (work.channel[1] * work.channel[0]
39 + remains * src.channel[0] * src.channel[1] / 255) / dest_alpha;
40 src.channel[1] = dest_alpha;
41 return i_ppix(im, x, y, &src);
42 }
43
44 case 3:
45 work = *col;
46 i_gpix(im, x, y, &src);
47 remains = 255 - work.channel[3];
48 src.channel[0] = (src.channel[0] * remains
49 + work.channel[0] * work.channel[3]) / 255;
50 src.channel[1] = (src.channel[1] * remains
51 + work.channel[1] * work.channel[3]) / 255;
52 src.channel[2] = (src.channel[2] * remains
53 + work.channel[2] * work.channel[3]) / 255;
54 return i_ppix(im, x, y, &src);
55
56 case 4:
57 work = *col;
58 i_gpix(im, x, y, &src);
8d14daab 59 remains = 255 - work.channel[3];
40068b33
TC
60 dest_alpha = work.channel[3] + remains * src.channel[3] / 255;
61 if (work.channel[3] == 255) {
62 return i_ppix(im, x, y, &work);
63 }
64 else {
65 src.channel[0] = (work.channel[3] * work.channel[0]
66 + remains * src.channel[0] * src.channel[3] / 255) / dest_alpha;
67 src.channel[1] = (work.channel[3] * work.channel[1]
68 + remains * src.channel[1] * src.channel[3] / 255) / dest_alpha;
69 src.channel[2] = (work.channel[3] * work.channel[2]
70 + remains * src.channel[2] * src.channel[3] / 255) / dest_alpha;
71 src.channel[3] = dest_alpha;
72 return i_ppix(im, x, y, &src);
73 }
74 }
75 return 0;
76}
77
3efb0915
TC
78static void
79cfill_from_btm(i_img *im, i_fill_t *fill, struct i_bitmap *btm,
8d14daab 80 i_img_dim bxmin, i_img_dim bxmax, i_img_dim bymin, i_img_dim bymax);
3efb0915 81
02d1d628 82void
8d14daab
TC
83i_mmarray_cr(i_mmarray *ar,i_img_dim l) {
84 i_img_dim i;
85 size_t alloc_size;
02d1d628
AMH
86
87 ar->lines=l;
f0960b14
TC
88 alloc_size = sizeof(minmax) * l;
89 /* check for overflow */
90 if (alloc_size / l != sizeof(minmax)) {
91 fprintf(stderr, "overflow calculating memory allocation");
92 exit(3);
93 }
94 ar->data=mymalloc(alloc_size); /* checked 5jul05 tonyc */
02d1d628
AMH
95 for(i=0;i<l;i++) { ar->data[i].max=-1; ar->data[i].min=MAXINT; }
96}
97
98void
99i_mmarray_dst(i_mmarray *ar) {
100 ar->lines=0;
101 if (ar->data != NULL) { myfree(ar->data); ar->data=NULL; }
102}
103
104void
8d14daab 105i_mmarray_add(i_mmarray *ar,i_img_dim x,i_img_dim y) {
02d1d628
AMH
106 if (y>-1 && y<ar->lines)
107 {
108 if (x<ar->data[y].min) ar->data[y].min=x;
109 if (x>ar->data[y].max) ar->data[y].max=x;
110 }
111}
112
113int
8d14daab 114i_mmarray_gmin(i_mmarray *ar,i_img_dim y) {
02d1d628
AMH
115 if (y>-1 && y<ar->lines) return ar->data[y].min;
116 else return -1;
117}
118
119int
8d14daab 120i_mmarray_getm(i_mmarray *ar,i_img_dim y) {
02d1d628
AMH
121 if (y>-1 && y<ar->lines) return ar->data[y].max;
122 else return MAXINT;
123}
124
8d14daab
TC
125#if 0
126/* unused? */
02d1d628
AMH
127void
128i_mmarray_render(i_img *im,i_mmarray *ar,i_color *val) {
8d14daab 129 i_img_dim i,x;
02d1d628
AMH
130 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);
131}
8d14daab 132#endif
02d1d628 133
02d1d628
AMH
134static
135void
8d14daab 136i_arcdraw(i_img_dim x1, i_img_dim y1, i_img_dim x2, i_img_dim y2, i_mmarray *ar) {
02d1d628
AMH
137 double alpha;
138 double dsec;
8d14daab 139 i_img_dim temp;
02d1d628 140 alpha=(double)(y2-y1)/(double)(x2-x1);
b254292b 141 if (fabs(alpha) <= 1)
02d1d628
AMH
142 {
143 if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
144 dsec=y1;
b254292b 145 while(x1<=x2)
02d1d628 146 {
8d14daab 147 i_mmarray_add(ar,x1,(i_img_dim)(dsec+0.5));
b254292b 148 dsec+=alpha;
02d1d628
AMH
149 x1++;
150 }
151 }
152 else
153 {
154 alpha=1/alpha;
155 if (y2<y1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
156 dsec=x1;
b254292b 157 while(y1<=y2)
02d1d628 158 {
8d14daab 159 i_mmarray_add(ar,(i_img_dim)(dsec+0.5),y1);
b254292b 160 dsec+=alpha;
02d1d628
AMH
161 y1++;
162 }
163 }
164}
165
166void
167i_mmarray_info(i_mmarray *ar) {
8d14daab 168 i_img_dim i;
02d1d628 169 for(i=0;i<ar->lines;i++)
8d14daab
TC
170 if (ar->data[i].max!=-1)
171 printf("line %"i_DF ": min=%" i_DF ", max=%" i_DF ".\n",
172 i_DFc(i), i_DFc(ar->data[i].min), i_DFc(ar->data[i].max));
02d1d628
AMH
173}
174
a8652edf 175static void
8d14daab 176i_arc_minmax(i_int_hlines *hlines,i_img_dim x,i_img_dim y, double rad,float d1,float d2) {
02d1d628 177 i_mmarray dot;
8d14daab
TC
178 double f,fx,fy;
179 i_img_dim x1,y1;
02d1d628 180
a8652edf 181 i_mmarray_cr(&dot, hlines->limit_y);
02d1d628 182
8d14daab
TC
183 x1=(i_img_dim)(x+0.5+rad*cos(d1*PI/180.0));
184 y1=(i_img_dim)(y+0.5+rad*sin(d1*PI/180.0));
02d1d628
AMH
185 fx=(float)x1; fy=(float)y1;
186
187 /* printf("x1: %d.\ny1: %d.\n",x1,y1); */
188 i_arcdraw(x, y, x1, y1, &dot);
189
8d14daab
TC
190 x1=(i_img_dim)(x+0.5+rad*cos(d2*PI/180.0));
191 y1=(i_img_dim)(y+0.5+rad*sin(d2*PI/180.0));
02d1d628 192
8d14daab
TC
193 for(f=d1;f<=d2;f+=0.01)
194 i_mmarray_add(&dot,(i_img_dim)(x+0.5+rad*cos(f*PI/180.0)),(i_img_dim)(y+0.5+rad*sin(f*PI/180.0)));
6af18d2b 195
02d1d628
AMH
196 /* printf("x1: %d.\ny1: %d.\n",x1,y1); */
197 i_arcdraw(x, y, x1, y1, &dot);
198
a8652edf
TC
199 /* render the minmax values onto the hlines */
200 for (y = 0; y < dot.lines; y++) {
201 if (dot.data[y].max!=-1) {
8d14daab 202 i_img_dim minx, width;
a8652edf
TC
203 minx = dot.data[y].min;
204 width = dot.data[y].max - dot.data[y].min + 1;
205 i_int_hlines_add(hlines, y, minx, width);
206 }
207 }
208
02d1d628 209 /* dot.info(); */
7f882a01 210 i_mmarray_dst(&dot);
02d1d628
AMH
211}
212
a8652edf 213static void
8d14daab 214i_arc_hlines(i_int_hlines *hlines,i_img_dim x,i_img_dim y,double rad,float d1,float d2) {
a8652edf
TC
215 if (d1 <= d2) {
216 i_arc_minmax(hlines, x, y, rad, d1, d2);
217 }
218 else {
219 i_arc_minmax(hlines, x, y, rad, d1, 360);
220 i_arc_minmax(hlines, x, y, rad, 0, d2);
221 }
222}
223
92bda632
TC
224/*
225=item i_arc(im, x, y, rad, d1, d2, color)
226
227=category Drawing
228=synopsis i_arc(im, 50, 50, 20, 45, 135, &color);
229
230Fills an arc centered at (x,y) with radius I<rad> covering the range
231of angles in degrees from d1 to d2, with the color.
232
233=cut
234*/
235
a8652edf 236void
8d14daab 237i_arc(i_img *im, i_img_dim x, i_img_dim y,double rad,double d1,double d2,const i_color *val) {
a8652edf
TC
238 i_int_hlines hlines;
239
240 i_int_init_hlines_img(&hlines, im);
241
242 i_arc_hlines(&hlines, x, y, rad, d1, d2);
243
244 i_int_hlines_fill_color(im, &hlines, val);
245
246 i_int_hlines_destroy(&hlines);
247}
248
92bda632
TC
249/*
250=item i_arc_cfill(im, x, y, rad, d1, d2, fill)
251
252=category Drawing
253=synopsis i_arc_cfill(im, 50, 50, 35, 90, 135, fill);
254
255Fills an arc centered at (x,y) with radius I<rad> covering the range
256of angles in degrees from d1 to d2, with the fill object.
257
258=cut
259*/
260
a8652edf
TC
261#define MIN_CIRCLE_STEPS 8
262#define MAX_CIRCLE_STEPS 360
263
f1ac5027 264void
8d14daab 265i_arc_cfill(i_img *im, i_img_dim x, i_img_dim y,double rad,double d1,double d2,i_fill_t *fill) {
a8652edf 266 i_int_hlines hlines;
f1ac5027 267
a8652edf 268 i_int_init_hlines_img(&hlines, im);
f1ac5027 269
a8652edf 270 i_arc_hlines(&hlines, x, y, rad, d1, d2);
f1ac5027 271
a8652edf 272 i_int_hlines_fill_fill(im, &hlines, fill);
f1ac5027 273
a8652edf
TC
274 i_int_hlines_destroy(&hlines);
275}
f1ac5027 276
a8652edf
TC
277static void
278arc_poly(int *count, double **xvals, double **yvals,
279 double x, double y, double rad, double d1, double d2) {
280 double d1_rad, d2_rad;
281 double circum;
8d14daab 282 i_img_dim steps, point_count;
a8652edf
TC
283 double angle_inc;
284
285 /* normalize the angles */
286 d1 = fmod(d1, 360);
287 if (d1 == 0) {
288 if (d2 >= 360) { /* default is 361 */
289 d2 = 360;
290 }
291 else {
292 d2 = fmod(d2, 360);
293 if (d2 < d1)
294 d2 += 360;
295 }
296 }
297 else {
298 d2 = fmod(d2, 360);
299 if (d2 < d1)
300 d2 += 360;
301 }
302 d1_rad = d1 * PI / 180;
303 d2_rad = d2 * PI / 180;
304
305 /* how many segments for the curved part?
306 we do a maximum of one per degree, with a minimum of 8/circle
307 we try to aim at having about one segment per 2 pixels
308 Work it out per circle to get a step size.
309
310 I was originally making steps = circum/2 but that looked horrible.
311
312 I think there might be an issue in the polygon filler.
313 */
314 circum = 2 * PI * rad;
315 steps = circum;
316 if (steps > MAX_CIRCLE_STEPS)
317 steps = MAX_CIRCLE_STEPS;
318 else if (steps < MIN_CIRCLE_STEPS)
319 steps = MIN_CIRCLE_STEPS;
320
321 angle_inc = 2 * PI / steps;
322
323 point_count = steps + 5; /* rough */
e310e5f9
TC
324 /* point_count is always relatively small, so allocation won't overflow */
325 *xvals = mymalloc(point_count * sizeof(double)); /* checked 17feb2005 tonyc */
326 *yvals = mymalloc(point_count * sizeof(double)); /* checked 17feb2005 tonyc */
a8652edf
TC
327
328 /* from centre to edge at d1 */
329 (*xvals)[0] = x;
330 (*yvals)[0] = y;
331 (*xvals)[1] = x + rad * cos(d1_rad);
332 (*yvals)[1] = y + rad * sin(d1_rad);
333 *count = 2;
334
335 /* step around the curve */
336 while (d1_rad < d2_rad) {
337 (*xvals)[*count] = x + rad * cos(d1_rad);
338 (*yvals)[*count] = y + rad * sin(d1_rad);
339 ++*count;
340 d1_rad += angle_inc;
341 }
f1ac5027 342
a8652edf
TC
343 /* finish off the curve */
344 (*xvals)[*count] = x + rad * cos(d2_rad);
345 (*yvals)[*count] = y + rad * sin(d2_rad);
346 ++*count;
347}
f1ac5027 348
92bda632
TC
349/*
350=item i_arc_aa(im, x, y, rad, d1, d2, color)
351
352=category Drawing
353=synopsis i_arc_aa(im, 50, 50, 35, 90, 135, &color);
354
5715f7c3 355Anti-alias fills an arc centered at (x,y) with radius I<rad> covering
92bda632
TC
356the range of angles in degrees from d1 to d2, with the color.
357
358=cut
359*/
360
a8652edf
TC
361void
362i_arc_aa(i_img *im, double x, double y, double rad, double d1, double d2,
97ac0a96 363 const i_color *val) {
a8652edf
TC
364 double *xvals, *yvals;
365 int count;
366
6b8fe08b 367 arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
a8652edf
TC
368
369 i_poly_aa(im, count, xvals, yvals, val);
370
371 myfree(xvals);
372 myfree(yvals);
f1ac5027
TC
373}
374
92bda632
TC
375/*
376=item i_arc_aa_cfill(im, x, y, rad, d1, d2, fill)
377
378=category Drawing
379=synopsis i_arc_aa_cfill(im, 50, 50, 35, 90, 135, fill);
380
5715f7c3 381Anti-alias fills an arc centered at (x,y) with radius I<rad> covering
92bda632
TC
382the range of angles in degrees from d1 to d2, with the fill object.
383
384=cut
385*/
386
a8652edf
TC
387void
388i_arc_aa_cfill(i_img *im, double x, double y, double rad, double d1, double d2,
389 i_fill_t *fill) {
390 double *xvals, *yvals;
391 int count;
392
393 arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
394
395 i_poly_aa_cfill(im, count, xvals, yvals, fill);
6af18d2b 396
a8652edf
TC
397 myfree(xvals);
398 myfree(yvals);
399}
6af18d2b
AMH
400
401/* Temporary AA HACK */
402
403
8d14daab
TC
404typedef i_img_dim frac;
405static frac float_to_frac(double x) { return (frac)(0.5+x*16.0); }
6af18d2b
AMH
406
407static
408void
8d14daab 409polar_to_plane(double cx, double cy, float angle, double radius, frac *x, frac *y) {
6af18d2b
AMH
410 *x = float_to_frac(cx+radius*cos(angle));
411 *y = float_to_frac(cy+radius*sin(angle));
412}
413
6af18d2b
AMH
414static
415void
8d14daab 416make_minmax_list(i_mmarray *dot, double x, double y, double radius) {
6af18d2b
AMH
417 float angle = 0.0;
418 float astep = radius>0.1 ? .5/radius : 10;
419 frac cx, cy, lx, ly, sx, sy;
420
421 mm_log((1, "make_minmax_list(dot %p, x %.2f, y %.2f, radius %.2f)\n", dot, x, y, radius));
422
423 polar_to_plane(x, y, angle, radius, &sx, &sy);
424
425 for(angle = 0.0; angle<361; angle +=astep) {
6af18d2b
AMH
426 lx = sx; ly = sy;
427 polar_to_plane(x, y, angle, radius, &cx, &cy);
428 sx = cx; sy = cy;
429
430 if (fabs(cx-lx) > fabs(cy-ly)) {
431 int ccx, ccy;
432 if (lx>cx) {
433 ccx = lx; lx = cx; cx = ccx;
434 ccy = ly; ly = cy; cy = ccy;
435 }
436
437 for(ccx=lx; ccx<=cx; ccx++) {
438 ccy = ly + ((cy-ly)*(ccx-lx))/(cx-lx);
439 i_mmarray_add(dot, ccx, ccy);
440 }
441 } else {
442 int ccx, ccy;
443
444 if (ly>cy) {
445 ccy = ly; ly = cy; cy = ccy;
446 ccx = lx; lx = cx; cx = ccx;
447 }
448
449 for(ccy=ly; ccy<=cy; ccy++) {
450 if (cy-ly) ccx = lx + ((cx-lx)*(ccy-ly))/(cy-ly); else ccx = lx;
451 i_mmarray_add(dot, ccx, ccy);
452 }
453 }
454 }
455}
456
457/* Get the number of subpixels covered */
458
459static
460int
8d14daab 461i_pixel_coverage(i_mmarray *dot, i_img_dim x, i_img_dim y) {
6af18d2b
AMH
462 frac minx = x*16;
463 frac maxx = minx+15;
464 frac cy;
465 int cnt = 0;
466
467 for(cy=y*16; cy<(y+1)*16; cy++) {
468 frac tmin = dot->data[cy].min;
469 frac tmax = dot->data[cy].max;
470
471 if (tmax == -1 || tmin > maxx || tmax < minx) continue;
472
473 if (tmin < minx) tmin = minx;
474 if (tmax > maxx) tmax = maxx;
475
476 cnt+=1+tmax-tmin;
477 }
478 return cnt;
479}
480
92bda632
TC
481/*
482=item i_circle_aa(im, x, y, rad, color)
483
484=category Drawing
485=synopsis i_circle_aa(im, 50, 50, 45, &color);
486
5715f7c3 487Anti-alias fills a circle centered at (x,y) for radius I<rad> with
92bda632
TC
488color.
489
490=cut
491*/
6af18d2b 492void
8d14daab 493i_circle_aa(i_img *im, double x, double y, double rad, const i_color *val) {
6af18d2b
AMH
494 i_mmarray dot;
495 i_color temp;
8d14daab 496 i_img_dim ly;
6af18d2b 497
8d14daab
TC
498 mm_log((1, "i_circle_aa(im %p, centre(" i_DFp "), rad %.2f, val %p)\n",
499 im, i_DFcp(x, y), rad, val));
6af18d2b
AMH
500
501 i_mmarray_cr(&dot,16*im->ysize);
502 make_minmax_list(&dot, x, y, rad);
503
504 for(ly = 0; ly<im->ysize; ly++) {
a659442a 505 int ix, cy, minx = INT_MAX, maxx = INT_MIN;
6af18d2b
AMH
506
507 /* Find the left/rightmost set subpixels */
508 for(cy = 0; cy<16; cy++) {
509 frac tmin = dot.data[ly*16+cy].min;
510 frac tmax = dot.data[ly*16+cy].max;
511 if (tmax == -1) continue;
512
513 if (minx > tmin) minx = tmin;
514 if (maxx < tmax) maxx = tmax;
515 }
516
517 if (maxx == INT_MIN) continue; /* no work to be done for this row of pixels */
518
519 minx /= 16;
520 maxx /= 16;
521 for(ix=minx; ix<=maxx; ix++) {
522 int cnt = i_pixel_coverage(&dot, ix, ly);
523 if (cnt>255) cnt = 255;
524 if (cnt) { /* should never be true */
525 int ch;
526 float ratio = (float)cnt/255.0;
527 i_gpix(im, ix, ly, &temp);
528 for(ch=0;ch<im->channels; ch++) temp.channel[ch] = (unsigned char)((float)val->channel[ch]*ratio + (float)temp.channel[ch]*(1.0-ratio));
529 i_ppix(im, ix, ly, &temp);
530 }
531 }
532 }
4b19f77a 533 i_mmarray_dst(&dot);
6af18d2b
AMH
534}
535
40068b33
TC
536/*
537=item i_circle_out(im, x, y, r, col)
538
539=category Drawing
540=synopsis i_circle_out(im, 50, 50, 45, &color);
541
542Draw a circle outline centered at (x,y) with radius r,
543non-anti-aliased.
544
545Parameters:
546
547=over
548
549=item *
550
551(x, y) - the center of the circle
552
553=item *
554
555r - the radius of the circle in pixels, must be non-negative
556
557=back
558
559Returns non-zero on success.
560
561Implementation:
562
563=cut
564*/
565
566int
567i_circle_out(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r,
568 const i_color *col) {
569 i_img_dim x, y;
570 i_img_dim dx, dy;
571 int error;
572
573 i_clear_error();
574
575 if (r < 0) {
576 i_push_error(0, "circle: radius must be non-negative");
577 return 0;
578 }
579
580 i_ppix(im, xc+r, yc, col);
581 i_ppix(im, xc-r, yc, col);
582 i_ppix(im, xc, yc+r, col);
583 i_ppix(im, xc, yc-r, col);
584
585 x = 0;
586 y = r;
587 dx = 1;
588 dy = -2 * r;
589 error = 1 - r;
590 while (x < y) {
591 if (error >= 0) {
592 --y;
593 dy += 2;
594 error += dy;
595 }
596 ++x;
597 dx += 2;
598 error += dx;
599
600 i_ppix(im, xc + x, yc + y, col);
601 i_ppix(im, xc + x, yc - y, col);
602 i_ppix(im, xc - x, yc + y, col);
603 i_ppix(im, xc - x, yc - y, col);
604 if (x != y) {
605 i_ppix(im, xc + y, yc + x, col);
606 i_ppix(im, xc + y, yc - x, col);
607 i_ppix(im, xc - y, yc + x, col);
608 i_ppix(im, xc - y, yc - x, col);
609 }
610 }
611
612 return 1;
613}
614
615/*
616=item arc_seg(angle)
617
618Convert an angle in degrees into an angle measure we can generate
619simply from the numbers we have when drawing the circle.
620
621=back
622*/
623
624static i_img_dim
625arc_seg(double angle, int scale) {
626 i_img_dim seg = (angle + 45) / 90;
627 double remains = angle - seg * 90; /* should be in the range [-45,45] */
40068b33
TC
628
629 while (seg > 4)
630 seg -= 4;
631 if (seg == 4 && remains > 0)
632 seg = 0;
633
634 return scale * (seg * 2 + sin(remains * PI/180));
635}
636
637/*
638=item i_arc_out(im, x, y, r, d1, d2, col)
639
640=category Drawing
641=synopsis i_arc_out(im, 50, 50, 45, 45, 135, &color);
642
643Draw an arc outline centered at (x,y) with radius r, non-anti-aliased
644over the angle range d1 through d2 degrees.
645
646Parameters:
647
648=over
649
650=item *
651
652(x, y) - the center of the circle
653
654=item *
655
656r - the radius of the circle in pixels, must be non-negative
657
658=item *
659
660d1, d2 - the range of angles to draw the arc over, in degrees.
661
662=back
663
664Returns non-zero on success.
665
666Implementation:
667
668=cut
669*/
670
671int
672i_arc_out(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r,
8d14daab 673 double d1, double d2, const i_color *col) {
40068b33
TC
674 i_img_dim x, y;
675 i_img_dim dx, dy;
676 int error;
677 i_img_dim segs[2][2];
678 int seg_count;
679 i_img_dim sin_th;
680 i_img_dim seg_d1, seg_d2;
681 int seg_num;
40068b33
TC
682 i_img_dim scale = r + 1;
683 i_img_dim seg1 = scale * 2;
684 i_img_dim seg2 = scale * 4;
685 i_img_dim seg3 = scale * 6;
686 i_img_dim seg4 = scale * 8;
687
688 i_clear_error();
689
690 if (r <= 0) {
691 i_push_error(0, "arc: radius must be non-negative");
692 return 0;
693 }
694 if (d1 + 360 <= d2)
695 return i_circle_out(im, xc, yc, r, col);
696
697 if (d1 < 0)
698 d1 += 360 * floor((-d1 + 359) / 360);
699 if (d2 < 0)
700 d2 += 360 * floor((-d2 + 359) / 360);
701 d1 = fmod(d1, 360);
702 d2 = fmod(d2, 360);
703 seg_d1 = arc_seg(d1, scale);
704 seg_d2 = arc_seg(d2, scale);
705 if (seg_d2 < seg_d1) {
706 /* split into two segments */
707 segs[0][0] = 0;
708 segs[0][1] = seg_d2;
709 segs[1][0] = seg_d1;
710 segs[1][1] = seg4;
711 seg_count = 2;
712 }
713 else {
714 segs[0][0] = seg_d1;
715 segs[0][1] = seg_d2;
716 seg_count = 1;
717 }
718
719 for (seg_num = 0; seg_num < seg_count; ++seg_num) {
720 i_img_dim seg_start = segs[seg_num][0];
721 i_img_dim seg_end = segs[seg_num][1];
722 if (seg_start == 0)
723 i_ppix(im, xc+r, yc, col);
724 if (seg_start <= seg1 && seg_end >= seg1)
725 i_ppix(im, xc, yc+r, col);
726 if (seg_start <= seg2 && seg_end >= seg2)
727 i_ppix(im, xc-r, yc, col);
728 if (seg_start <= seg3 && seg_end >= seg3)
729 i_ppix(im, xc, yc-r, col);
730
731 y = 0;
732 x = r;
733 dy = 1;
734 dx = -2 * r;
735 error = 1 - r;
736 while (y < x) {
737 if (error >= 0) {
738 --x;
739 dx += 2;
740 error += dx;
741 }
742 ++y;
743 dy += 2;
744 error += dy;
745
746 sin_th = y;
747 if (seg_start <= sin_th && seg_end >= sin_th)
748 i_ppix(im, xc + x, yc + y, col);
749 if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
750 i_ppix(im, xc + y, yc + x, col);
751
752 if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
753 i_ppix(im, xc - y, yc + x, col);
754 if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
755 i_ppix(im, xc - x, yc + y, col);
756
757 if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
758 i_ppix(im, xc - x, yc - y, col);
759 if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
760 i_ppix(im, xc - y, yc - x, col);
761
762 if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
763 i_ppix(im, xc + y, yc - x, col);
764 if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
765 i_ppix(im, xc + x, yc - y, col);
766 }
767 }
768
769 return 1;
770}
771
772static double
773cover(i_img_dim r, i_img_dim j) {
8d14daab 774 double rjsqrt = sqrt(r*r - j*j);
40068b33
TC
775
776 return ceil(rjsqrt) - rjsqrt;
777}
778
779/*
780=item i_circle_out_aa(im, xc, yc, r, col)
781
782=synopsis i_circle_out_aa(im, 50, 50, 45, &color);
783
784Draw a circle outline centered at (x,y) with radius r, anti-aliased.
785
786Parameters:
787
788=over
789
790=item *
791
792(xc, yc) - the center of the circle
793
794=item *
795
796r - the radius of the circle in pixels, must be non-negative
797
798=item *
799
800col - an i_color for the color to draw in.
801
802=back
803
804Returns non-zero on success.
805
806=cut
807
808Based on "Fast Anti-Aliased Circle Generation", Xiaolin Wu, Graphics
809Gems.
810
811I use floating point for I<D> since for large circles the precision of
812a [0,255] value isn't sufficient when approaching the end of the
813octant.
814
815*/
816
817int
818i_circle_out_aa(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r, const i_color *col) {
819 i_img_dim i, j;
820 double t;
821 i_color workc = *col;
822 int orig_alpha = col->channel[3];
823
824 i_clear_error();
825 if (r <= 0) {
826 i_push_error(0, "arc: radius must be non-negative");
827 return 0;
828 }
829 i = r;
830 j = 0;
831 t = 0;
832 i_ppix_norm(im, xc+i, yc+j, col);
833 i_ppix_norm(im, xc-i, yc+j, col);
834 i_ppix_norm(im, xc+j, yc+i, col);
835 i_ppix_norm(im, xc+j, yc-i, col);
836
837 while (i > j+1) {
838 double d;
839 int cv, inv_cv;
40068b33
TC
840 j++;
841 d = cover(r, j);
842 cv = (int)(d * 255 + 0.5);
843 inv_cv = 255-cv;
844 if (d < t) {
845 --i;
846 }
847 if (inv_cv) {
848 workc.channel[3] = orig_alpha * inv_cv / 255;
849 i_ppix_norm(im, xc+i, yc+j, &workc);
850 i_ppix_norm(im, xc-i, yc+j, &workc);
851 i_ppix_norm(im, xc+i, yc-j, &workc);
852 i_ppix_norm(im, xc-i, yc-j, &workc);
853
854 if (i != j) {
855 i_ppix_norm(im, xc+j, yc+i, &workc);
856 i_ppix_norm(im, xc-j, yc+i, &workc);
857 i_ppix_norm(im, xc+j, yc-i, &workc);
858 i_ppix_norm(im, xc-j, yc-i, &workc);
859 }
860 }
861 if (cv && i > j) {
862 workc.channel[3] = orig_alpha * cv / 255;
863 i_ppix_norm(im, xc+i-1, yc+j, &workc);
864 i_ppix_norm(im, xc-i+1, yc+j, &workc);
865 i_ppix_norm(im, xc+i-1, yc-j, &workc);
866 i_ppix_norm(im, xc-i+1, yc-j, &workc);
867
868 if (j != i-1) {
869 i_ppix_norm(im, xc+j, yc+i-1, &workc);
870 i_ppix_norm(im, xc-j, yc+i-1, &workc);
871 i_ppix_norm(im, xc+j, yc-i+1, &workc);
872 i_ppix_norm(im, xc-j, yc-i+1, &workc);
873 }
874 }
875 t = d;
876 }
877
878 return 1;
879}
880
881/*
882=item i_arc_out_aa(im, xc, yc, r, d1, d2, col)
883
884=synopsis i_arc_out_aa(im, 50, 50, 45, 45, 125, &color);
885
886Draw a circle arc outline centered at (x,y) with radius r, from angle
887d1 degrees through angle d2 degrees, anti-aliased.
888
889Parameters:
890
891=over
892
893=item *
894
895(xc, yc) - the center of the circle
896
897=item *
898
899r - the radius of the circle in pixels, must be non-negative
900
901=item *
902
903d1, d2 - the range of angle in degrees to draw the arc through. If
904d2-d1 >= 360 a full circle is drawn.
905
906=back
907
908Returns non-zero on success.
909
910=cut
911
912Based on "Fast Anti-Aliased Circle Generation", Xiaolin Wu, Graphics
913Gems.
914
915*/
916
917int
8d14daab 918i_arc_out_aa(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r, double d1, double d2, const i_color *col) {
40068b33
TC
919 i_img_dim i, j;
920 double t;
921 i_color workc = *col;
922 i_img_dim segs[2][2];
923 int seg_count;
924 i_img_dim sin_th;
925 i_img_dim seg_d1, seg_d2;
926 int seg_num;
927 int orig_alpha = col->channel[3];
928 i_img_dim scale = r + 1;
929 i_img_dim seg1 = scale * 2;
930 i_img_dim seg2 = scale * 4;
931 i_img_dim seg3 = scale * 6;
932 i_img_dim seg4 = scale * 8;
933
934 i_clear_error();
935 if (r <= 0) {
936 i_push_error(0, "arc: radius must be non-negative");
937 return 0;
938 }
939 if (d1 + 360 <= d2)
940 return i_circle_out_aa(im, xc, yc, r, col);
941
942 if (d1 < 0)
943 d1 += 360 * floor((-d1 + 359) / 360);
944 if (d2 < 0)
945 d2 += 360 * floor((-d2 + 359) / 360);
946 d1 = fmod(d1, 360);
947 d2 = fmod(d2, 360);
948 seg_d1 = arc_seg(d1, scale);
949 seg_d2 = arc_seg(d2, scale);
950 if (seg_d2 < seg_d1) {
951 /* split into two segments */
952 segs[0][0] = 0;
953 segs[0][1] = seg_d2;
954 segs[1][0] = seg_d1;
955 segs[1][1] = seg4;
956 seg_count = 2;
957 }
958 else {
959 segs[0][0] = seg_d1;
960 segs[0][1] = seg_d2;
961 seg_count = 1;
962 }
963
964 for (seg_num = 0; seg_num < seg_count; ++seg_num) {
965 i_img_dim seg_start = segs[seg_num][0];
966 i_img_dim seg_end = segs[seg_num][1];
967
968 i = r;
969 j = 0;
970 t = 0;
971
972 if (seg_start == 0)
973 i_ppix_norm(im, xc+i, yc+j, col);
974 if (seg_start <= seg1 && seg_end >= seg1)
975 i_ppix_norm(im, xc+j, yc+i, col);
976 if (seg_start <= seg2 && seg_end >= seg2)
977 i_ppix_norm(im, xc-i, yc+j, col);
978 if (seg_start <= seg3 && seg_end >= seg3)
979 i_ppix_norm(im, xc+j, yc-i, col);
980
981 while (i > j+1) {
982 int cv, inv_cv;
40068b33
TC
983 double d;
984 j++;
985 d = cover(r, j);
986 cv = (int)(d * 255 + 0.5);
987 inv_cv = 255-cv;
988 if (d < t) {
989 --i;
990 }
991 sin_th = j;
992 if (inv_cv) {
993 workc.channel[3] = orig_alpha * inv_cv / 255;
994
995 if (seg_start <= sin_th && seg_end >= sin_th)
996 i_ppix_norm(im, xc+i, yc+j, &workc);
997 if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
998 i_ppix_norm(im, xc-i, yc+j, &workc);
999 if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
1000 i_ppix_norm(im, xc+i, yc-j, &workc);
1001 if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
1002 i_ppix_norm(im, xc-i, yc-j, &workc);
1003
1004 if (i != j) {
1005 if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
1006 i_ppix_norm(im, xc+j, yc+i, &workc);
1007 if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
1008 i_ppix_norm(im, xc-j, yc+i, &workc);
1009 if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
1010 i_ppix_norm(im, xc+j, yc-i, &workc);
1011 if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
1012 i_ppix_norm(im, xc-j, yc-i, &workc);
1013 }
1014 }
1015 if (cv && i > j) {
1016 workc.channel[3] = orig_alpha * cv / 255;
1017 if (seg_start <= sin_th && seg_end >= sin_th)
1018 i_ppix_norm(im, xc+i-1, yc+j, &workc);
1019 if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
1020 i_ppix_norm(im, xc-i+1, yc+j, &workc);
1021 if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
1022 i_ppix_norm(im, xc+i-1, yc-j, &workc);
1023 if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
1024 i_ppix_norm(im, xc-i+1, yc-j, &workc);
1025
1026 if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
1027 i_ppix_norm(im, xc+j, yc+i-1, &workc);
1028 if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
1029 i_ppix_norm(im, xc-j, yc+i-1, &workc);
1030 if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
1031 i_ppix_norm(im, xc+j, yc-i+1, &workc);
1032 if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
1033 i_ppix_norm(im, xc-j, yc-i+1, &workc);
1034 }
1035 t = d;
1036 }
1037 }
1038
1039 return 1;
1040}
1041
92bda632
TC
1042/*
1043=item i_box(im, x1, y1, x2, y2, color)
6af18d2b 1044
92bda632
TC
1045=category Drawing
1046=synopsis i_box(im, 0, 0, im->xsize-1, im->ysize-1, &color).
6af18d2b 1047
92bda632 1048Outlines the box from (x1,y1) to (x2,y2) inclusive with I<color>.
6af18d2b 1049
92bda632
TC
1050=cut
1051*/
6af18d2b 1052
02d1d628 1053void
8d14daab
TC
1054i_box(i_img *im,i_img_dim x1,i_img_dim y1,i_img_dim x2,i_img_dim y2,const i_color *val) {
1055 i_img_dim x,y;
1056 mm_log((1,"i_box(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
1057 im, i_DFcp(x1,y1), i_DFcp(x2,y2), val));
02d1d628
AMH
1058 for(x=x1;x<x2+1;x++) {
1059 i_ppix(im,x,y1,val);
1060 i_ppix(im,x,y2,val);
1061 }
1062 for(y=y1;y<y2+1;y++) {
1063 i_ppix(im,x1,y,val);
1064 i_ppix(im,x2,y,val);
1065 }
1066}
1067
92bda632
TC
1068/*
1069=item i_box_filled(im, x1, y1, x2, y2, color)
1070
1071=category Drawing
1072=synopsis i_box_filled(im, 0, 0, im->xsize-1, im->ysize-1, &color);
1073
1074Fills the box from (x1,y1) to (x2,y2) inclusive with color.
1075
1076=cut
1077*/
1078
02d1d628 1079void
8d14daab 1080i_box_filled(i_img *im,i_img_dim x1,i_img_dim y1,i_img_dim x2,i_img_dim y2, const i_color *val) {
3b000586
TC
1081 i_img_dim x, y, width;
1082 i_palidx index;
1083
8d14daab
TC
1084 mm_log((1,"i_box_filled(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
1085 im, i_DFcp(x1, y1), i_DFcp(x2,y2) ,val));
3b000586
TC
1086
1087 if (x1 > x2 || y1 > y2
1088 || x2 < 0 || y2 < 0
1089 || x1 >= im->xsize || y1 > im->ysize)
1090 return;
1091
1092 if (x1 < 0)
1093 x1 = 0;
1094 if (x2 >= im->xsize)
1095 x2 = im->xsize - 1;
1096 if (y1 < 0)
1097 y1 = 0;
1098 if (y2 >= im->ysize)
1099 y2 = im->ysize - 1;
1100
1101 width = x2 - x1 + 1;
1102
1103 if (im->type == i_palette_type
1104 && i_findcolor(im, val, &index)) {
1105 i_palidx *line = mymalloc(sizeof(i_palidx) * width);
1106
1107 for (x = 0; x < width; ++x)
1108 line[x] = index;
1109
1110 for (y = y1; y <= y2; ++y)
1111 i_ppal(im, x1, x2+1, y, line);
1112
1113 myfree(line);
1114 }
1115 else {
1116 i_color *line = mymalloc(sizeof(i_color) * width);
1117
1118 for (x = 0; x < width; ++x)
1119 line[x] = *val;
1120
1121 for (y = y1; y <= y2; ++y)
1122 i_plin(im, x1, x2+1, y, line);
1123
1124 myfree(line);
1125 }
02d1d628
AMH
1126}
1127
7477ff14
TC
1128/*
1129=item i_box_filledf(im, x1, y1, x2, y2, color)
1130
1131=category Drawing
1132=synopsis i_box_filledf(im, 0, 0, im->xsize-1, im->ysize-1, &fcolor);
1133
1134Fills the box from (x1,y1) to (x2,y2) inclusive with a floating point
1135color.
1136
1137=cut
1138*/
1139
1140int
8d14daab 1141i_box_filledf(i_img *im,i_img_dim x1,i_img_dim y1,i_img_dim x2,i_img_dim y2, const i_fcolor *val) {
7477ff14 1142 i_img_dim x, y, width;
7477ff14 1143
8d14daab
TC
1144 mm_log((1,"i_box_filledf(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
1145 im, i_DFcp(x1, y1), i_DFcp(x2, y2), val));
7477ff14
TC
1146
1147 if (x1 > x2 || y1 > y2
1148 || x2 < 0 || y2 < 0
1149 || x1 >= im->xsize || y1 > im->ysize)
1150 return 0;
1151
1152 if (x1 < 0)
1153 x1 = 0;
1154 if (x2 >= im->xsize)
1155 x2 = im->xsize - 1;
1156 if (y1 < 0)
1157 y1 = 0;
1158 if (y2 >= im->ysize)
1159 y2 = im->ysize - 1;
1160
1161 width = x2 - x1 + 1;
1162
1163 if (im->bits <= 8) {
1164 i_color c;
1165 c.rgba.r = SampleFTo8(val->rgba.r);
1166 c.rgba.g = SampleFTo8(val->rgba.g);
1167 c.rgba.b = SampleFTo8(val->rgba.b);
1168 c.rgba.a = SampleFTo8(val->rgba.a);
1169
1170 i_box_filled(im, x1, y1, x2, y2, &c);
1171 }
1172 else {
1173 i_fcolor *line = mymalloc(sizeof(i_fcolor) * width);
1174
1175 for (x = 0; x < width; ++x)
1176 line[x] = *val;
1177
1178 for (y = y1; y <= y2; ++y)
1179 i_plinf(im, x1, x2+1, y, line);
1180
1181 myfree(line);
1182 }
1183
1184 return 1;
1185}
1186
92bda632
TC
1187/*
1188=item i_box_cfill(im, x1, y1, x2, y2, fill)
1189
1190=category Drawing
1191=synopsis i_box_cfill(im, 0, 0, im->xsize-1, im->ysize-1, fill);
1192
1193Fills the box from (x1,y1) to (x2,y2) inclusive with fill.
1194
1195=cut
1196*/
1197
f1ac5027 1198void
8d14daab 1199i_box_cfill(i_img *im,i_img_dim x1,i_img_dim y1,i_img_dim x2,i_img_dim y2,i_fill_t *fill) {
9b1ec2b8 1200 i_render r;
8d14daab
TC
1201
1202 mm_log((1,"i_box_cfill(im* %p, p1(" i_DFp "), p2(" i_DFp "), fill %p)\n",
1203 im, i_DFcp(x1, y1), i_DFcp(x2,y2), fill));
f1ac5027
TC
1204
1205 ++x2;
f0960b14
TC
1206 if (x1 < 0)
1207 x1 = 0;
1208 if (y1 < 0)
1209 y1 = 0;
1210 if (x2 > im->xsize)
1211 x2 = im->xsize;
1212 if (y2 >= im->ysize)
1213 y2 = im->ysize-1;
1214 if (x1 >= x2 || y1 > y2)
1215 return;
9b1ec2b8
TC
1216
1217 i_render_init(&r, im, x2-x1);
1218 while (y1 <= y2) {
1219 i_render_fill(&r, x1, y1, x2-x1, NULL, fill);
1220 ++y1;
f1ac5027 1221 }
9b1ec2b8 1222 i_render_done(&r);
f1ac5027 1223}
02d1d628 1224
aa833c97 1225/*
5715f7c3 1226=item i_line(C<im>, C<x1>, C<y1>, C<x2>, C<y2>, C<color>, C<endp>)
aa833c97 1227
92bda632
TC
1228=category Drawing
1229
5715f7c3 1230=for stopwords Bresenham's
aa833c97 1231
5715f7c3
TC
1232Draw a line to image using Bresenham's line drawing algorithm
1233
1234 im - image to draw to
1235 x1 - starting x coordinate
1236 y1 - starting x coordinate
1237 x2 - starting x coordinate
1238 y2 - starting x coordinate
1239 color - color to write to image
1240 endp - endpoint flag (boolean)
aa833c97
AMH
1241
1242=cut
1243*/
1244
02d1d628 1245void
8d14daab
TC
1246i_line(i_img *im, i_img_dim x1, i_img_dim y1, i_img_dim x2, i_img_dim y2, const i_color *val, int endp) {
1247 i_img_dim x, y;
1248 i_img_dim dx, dy;
1249 i_img_dim p;
02d1d628 1250
aa833c97
AMH
1251 dx = x2 - x1;
1252 dy = y2 - y1;
02d1d628 1253
aa833c97
AMH
1254
1255 /* choose variable to iterate on */
8d14daab
TC
1256 if (i_abs(dx) > i_abs(dy)) {
1257 i_img_dim dx2, dy2, cpy;
aa833c97
AMH
1258
1259 /* sort by x */
1260 if (x1 > x2) {
8d14daab 1261 i_img_dim t;
aa833c97
AMH
1262 t = x1; x1 = x2; x2 = t;
1263 t = y1; y1 = y2; y2 = t;
02d1d628 1264 }
aa833c97 1265
8d14daab 1266 dx = i_abs(dx);
aa833c97
AMH
1267 dx2 = dx*2;
1268 dy = y2 - y1;
1269
1270 if (dy<0) {
1271 dy = -dy;
1272 cpy = -1;
1273 } else {
1274 cpy = 1;
1275 }
1276 dy2 = dy*2;
1277 p = dy2 - dx;
1278
1279
1280 y = y1;
1281 for(x=x1; x<x2-1; x++) {
1282 if (p<0) {
1283 p += dy2;
1284 } else {
1285 y += cpy;
1286 p += dy2-dx2;
1287 }
1288 i_ppix(im, x+1, y, val);
02d1d628 1289 }
aa833c97 1290 } else {
8d14daab 1291 i_img_dim dy2, dx2, cpx;
aa833c97
AMH
1292
1293 /* sort bx y */
1294 if (y1 > y2) {
8d14daab 1295 i_img_dim t;
aa833c97
AMH
1296 t = x1; x1 = x2; x2 = t;
1297 t = y1; y1 = y2; y2 = t;
1298 }
1299
8d14daab 1300 dy = i_abs(dy);
aa833c97
AMH
1301 dx = x2 - x1;
1302 dy2 = dy*2;
1303
1304 if (dx<0) {
1305 dx = -dx;
1306 cpx = -1;
1307 } else {
1308 cpx = 1;
1309 }
1310 dx2 = dx*2;
1311 p = dx2 - dy;
1312
1313 x = x1;
1314
1315 for(y=y1; y<y2-1; y++) {
1316 if (p<0) {
1317 p += dx2;
1318 } else {
1319 x += cpx;
1320 p += dx2-dy2;
1321 }
1322 i_ppix(im, x, y+1, val);
1323 }
1324 }
1325 if (endp) {
1326 i_ppix(im, x1, y1, val);
1327 i_ppix(im, x2, y2, val);
1328 } else {
1329 if (x1 != x2 || y1 != y2)
1330 i_ppix(im, x1, y1, val);
1331 }
02d1d628
AMH
1332}
1333
aa833c97 1334
02d1d628 1335void
8d14daab 1336i_line_dda(i_img *im, i_img_dim x1, i_img_dim y1, i_img_dim x2, i_img_dim y2, i_color *val) {
b437ce0a 1337
8d14daab
TC
1338 double dy;
1339 i_img_dim x;
b437ce0a
AMH
1340
1341 for(x=x1; x<=x2; x++) {
8d14daab
TC
1342 dy = y1+ (x-x1)/(double)(x2-x1)*(y2-y1);
1343 i_ppix(im, x, (i_img_dim)(dy+0.5), val);
b437ce0a
AMH
1344 }
1345}
1346
92bda632 1347/*
5715f7c3 1348=item i_line_aa(C<im>, C<x1>, C<x2>, C<y1>, C<y2>, C<color>, C<endp>)
92bda632
TC
1349
1350=category Drawing
1351
5715f7c3 1352Anti-alias draws a line from (x1,y1) to (x2, y2) in color.
b437ce0a 1353
5715f7c3 1354The point (x2, y2) is drawn only if C<endp> is set.
92bda632
TC
1355
1356=cut
1357*/
b437ce0a
AMH
1358
1359void
8d14daab
TC
1360i_line_aa(i_img *im, i_img_dim x1, i_img_dim y1, i_img_dim x2, i_img_dim y2, const i_color *val, int endp) {
1361 i_img_dim x, y;
1362 i_img_dim dx, dy;
1363 i_img_dim p;
b437ce0a
AMH
1364
1365 dx = x2 - x1;
1366 dy = y2 - y1;
1367
1368 /* choose variable to iterate on */
8d14daab
TC
1369 if (i_abs(dx) > i_abs(dy)) {
1370 i_img_dim dx2, dy2, cpy;
b437ce0a
AMH
1371
1372 /* sort by x */
1373 if (x1 > x2) {
8d14daab 1374 i_img_dim t;
b437ce0a
AMH
1375 t = x1; x1 = x2; x2 = t;
1376 t = y1; y1 = y2; y2 = t;
1377 }
1378
8d14daab 1379 dx = i_abs(dx);
b437ce0a
AMH
1380 dx2 = dx*2;
1381 dy = y2 - y1;
1382
1383 if (dy<0) {
1384 dy = -dy;
1385 cpy = -1;
1386 } else {
1387 cpy = 1;
1388 }
1389 dy2 = dy*2;
1390 p = dy2 - dx2; /* this has to be like this for AA */
1391
1392 y = y1;
1393
1394 for(x=x1; x<x2-1; x++) {
1395 int ch;
1396 i_color tval;
8d14daab
TC
1397 double t = (dy) ? -(float)(p)/(float)(dx2) : 1;
1398 double t1, t2;
b437ce0a
AMH
1399
1400 if (t<0) t = 0;
1401 t1 = 1-t;
1402 t2 = t;
1403
1404 i_gpix(im,x+1,y,&tval);
1405 for(ch=0;ch<im->channels;ch++)
1406 tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
1407 i_ppix(im,x+1,y,&tval);
1408
1409 i_gpix(im,x+1,y+cpy,&tval);
1410 for(ch=0;ch<im->channels;ch++)
1411 tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
1412 i_ppix(im,x+1,y+cpy,&tval);
1413
1414 if (p<0) {
1415 p += dy2;
1416 } else {
1417 y += cpy;
1418 p += dy2-dx2;
1419 }
1420 }
1421 } else {
8d14daab 1422 i_img_dim dy2, dx2, cpx;
b437ce0a
AMH
1423
1424 /* sort bx y */
1425 if (y1 > y2) {
8d14daab 1426 i_img_dim t;
b437ce0a
AMH
1427 t = x1; x1 = x2; x2 = t;
1428 t = y1; y1 = y2; y2 = t;
1429 }
1430
8d14daab 1431 dy = i_abs(dy);
b437ce0a
AMH
1432 dx = x2 - x1;
1433 dy2 = dy*2;
1434
1435 if (dx<0) {
1436 dx = -dx;
1437 cpx = -1;
1438 } else {
1439 cpx = 1;
1440 }
1441 dx2 = dx*2;
1442 p = dx2 - dy2; /* this has to be like this for AA */
1443
1444 x = x1;
1445
1446 for(y=y1; y<y2-1; y++) {
1447 int ch;
1448 i_color tval;
8d14daab
TC
1449 double t = (dx) ? -(double)(p)/(double)(dy2) : 1;
1450 double t1, t2;
b437ce0a
AMH
1451
1452 if (t<0) t = 0;
1453 t1 = 1-t;
1454 t2 = t;
1455
1456 i_gpix(im,x,y+1,&tval);
1457 for(ch=0;ch<im->channels;ch++)
8d14daab 1458 tval.channel[ch]=(unsigned char)(t1*(double)tval.channel[ch]+t2*(double)val->channel[ch]);
b437ce0a
AMH
1459 i_ppix(im,x,y+1,&tval);
1460
1461 i_gpix(im,x+cpx,y+1,&tval);
1462 for(ch=0;ch<im->channels;ch++)
8d14daab 1463 tval.channel[ch]=(unsigned char)(t2*(double)tval.channel[ch]+t1*(double)val->channel[ch]);
b437ce0a
AMH
1464 i_ppix(im,x+cpx,y+1,&tval);
1465
1466 if (p<0) {
1467 p += dx2;
1468 } else {
1469 x += cpx;
1470 p += dx2-dy2;
1471 }
1472 }
1473 }
1474
1475
1476 if (endp) {
1477 i_ppix(im, x1, y1, val);
1478 i_ppix(im, x2, y2, val);
1479 } else {
1480 if (x1 != x2 || y1 != y2)
1481 i_ppix(im, x1, y1, val);
1482 }
1483}
1484
1485
1486
b33c08f8 1487static double
8d14daab 1488perm(i_img_dim n,i_img_dim k) {
02d1d628 1489 double r;
8d14daab 1490 i_img_dim i;
02d1d628
AMH
1491 r=1;
1492 for(i=k+1;i<=n;i++) r*=i;
1493 for(i=1;i<=(n-k);i++) r/=i;
1494 return r;
1495}
1496
1497
1498/* Note in calculating t^k*(1-t)^(n-k)
1499 we can start by using t^0=1 so this simplifies to
1500 t^0*(1-t)^n - we want to multiply that with t/(1-t) each iteration
1501 to get a new level - this may lead to errors who knows lets test it */
1502
1503void
97ac0a96 1504i_bezier_multi(i_img *im,int l,const double *x,const double *y, const i_color *val) {
02d1d628
AMH
1505 double *bzcoef;
1506 double t,cx,cy;
1507 int k,i;
8d14daab 1508 i_img_dim lx = 0,ly = 0;
02d1d628
AMH
1509 int n=l-1;
1510 double itr,ccoef;
1511
f0960b14
TC
1512 /* this is the same size as the x and y arrays, so shouldn't overflow */
1513 bzcoef=mymalloc(sizeof(double)*l); /* checked 5jul05 tonyc */
02d1d628
AMH
1514 for(k=0;k<l;k++) bzcoef[k]=perm(n,k);
1515 ICL_info(val);
1516
1517
1518 /* for(k=0;k<l;k++) printf("bzcoef: %d -> %f\n",k,bzcoef[k]); */
1519 i=0;
1520 for(t=0;t<=1;t+=0.005) {
1521 cx=cy=0;
1522 itr=t/(1-t);
1523 ccoef=pow(1-t,n);
1524 for(k=0;k<l;k++) {
1525 /* cx+=bzcoef[k]*x[k]*pow(t,k)*pow(1-t,n-k);
1526 cy+=bzcoef[k]*y[k]*pow(t,k)*pow(1-t,n-k);*/
1527
1528 cx+=bzcoef[k]*x[k]*ccoef;
1529 cy+=bzcoef[k]*y[k]*ccoef;
1530 ccoef*=itr;
1531 }
1532 /* printf("%f -> (%d,%d)\n",t,(int)(0.5+cx),(int)(0.5+cy)); */
1533 if (i++) {
8d14daab 1534 i_line_aa(im,lx,ly,(i_img_dim)(0.5+cx),(i_img_dim)(0.5+cy),val, 1);
02d1d628 1535 }
8d14daab
TC
1536 /* i_ppix(im,(i_img_dim)(0.5+cx),(i_img_dim)(0.5+cy),val); */
1537 lx=(i_img_dim)(0.5+cx);
1538 ly=(i_img_dim)(0.5+cy);
02d1d628
AMH
1539 }
1540 ICL_info(val);
1541 myfree(bzcoef);
1542}
1543
02d1d628
AMH
1544/* Flood fill
1545
1546 REF: Graphics Gems I. page 282+
1547
1548*/
1549
02d1d628
AMH
1550/* This should be moved into a seperate file? */
1551
1552/* This is the truncation used:
1553
1554 a double is multiplied by 16 and then truncated.
1555 This means that 0 -> 0
1556 So a triangle of (0,0) (10,10) (10,0) Will look like it's
1557 not filling the (10,10) point nor the (10,0)-(10,10) line segment
1558
1559*/
1560
1561
02d1d628
AMH
1562/* Flood fill algorithm - based on the Ken Fishkins (pixar) gem in
1563 graphics gems I */
1564
1565/*
1566struct stc {
8d14daab
TC
1567 i_img_dim mylx,myrx;
1568 i_img_dim dadlx,dadrx;
1569 i_img_dim myy;
02d1d628
AMH
1570 int mydirection;
1571};
1572
1573Not used code???
1574*/
1575
1576
1577struct stack_element {
8d14daab
TC
1578 i_img_dim myLx,myRx;
1579 i_img_dim dadLx,dadRx;
1580 i_img_dim myY;
02d1d628
AMH
1581 int myDirection;
1582};
1583
1584
1585/* create the link data to put push onto the stack */
1586
1587static
1588struct stack_element*
8d14daab 1589crdata(i_img_dim left,i_img_dim right,i_img_dim dadl,i_img_dim dadr,i_img_dim y, int dir) {
02d1d628 1590 struct stack_element *ste;
f0960b14 1591 ste = mymalloc(sizeof(struct stack_element)); /* checked 5jul05 tonyc */
a73aeb5f
AMH
1592 ste->myLx = left;
1593 ste->myRx = right;
1594 ste->dadLx = dadl;
1595 ste->dadRx = dadr;
1596 ste->myY = y;
1597 ste->myDirection = dir;
02d1d628
AMH
1598 return ste;
1599}
1600
1601/* i_ccomp compares two colors and gives true if they are the same */
1602
3efb0915
TC
1603typedef int (*ff_cmpfunc)(i_color const *c1, i_color const *c2, int channels);
1604
02d1d628 1605static int
3efb0915 1606i_ccomp_normal(i_color const *val1, i_color const *val2, int ch) {
02d1d628 1607 int i;
3efb0915
TC
1608 for(i = 0; i < ch; i++)
1609 if (val1->channel[i] !=val2->channel[i])
1610 return 0;
02d1d628
AMH
1611 return 1;
1612}
1613
3efb0915
TC
1614static int
1615i_ccomp_border(i_color const *val1, i_color const *val2, int ch) {
1616 int i;
1617 for(i = 0; i < ch; i++)
1618 if (val1->channel[i] !=val2->channel[i])
1619 return 1;
1620 return 0;
1621}
02d1d628
AMH
1622
1623static int
8d14daab 1624i_lspan(i_img *im, i_img_dim seedx, i_img_dim seedy, i_color const *val, ff_cmpfunc cmpfunc) {
02d1d628
AMH
1625 i_color cval;
1626 while(1) {
1627 if (seedx-1 < 0) break;
1628 i_gpix(im,seedx-1,seedy,&cval);
3efb0915
TC
1629 if (!cmpfunc(val,&cval,im->channels))
1630 break;
02d1d628
AMH
1631 seedx--;
1632 }
1633 return seedx;
1634}
1635
1636static int
8d14daab 1637i_rspan(i_img *im, i_img_dim seedx, i_img_dim seedy, i_color const *val, ff_cmpfunc cmpfunc) {
02d1d628
AMH
1638 i_color cval;
1639 while(1) {
1640 if (seedx+1 > im->xsize-1) break;
1641 i_gpix(im,seedx+1,seedy,&cval);
3efb0915 1642 if (!cmpfunc(val,&cval,im->channels)) break;
02d1d628
AMH
1643 seedx++;
1644 }
1645 return seedx;
1646}
1647
1648/* Macro to create a link and push on to the list */
1649
e25e59b1
AMH
1650#define ST_PUSH(left,right,dadl,dadr,y,dir) do { \
1651 struct stack_element *s = crdata(left,right,dadl,dadr,y,dir); \
1652 llist_push(st,&s); \
1653} while (0)
02d1d628
AMH
1654
1655/* pops the shadow on TOS into local variables lx,rx,y,direction,dadLx and dadRx */
1656/* No overflow check! */
1657
e25e59b1
AMH
1658#define ST_POP() do { \
1659 struct stack_element *s; \
1660 llist_pop(st,&s); \
1661 lx = s->myLx; \
1662 rx = s->myRx; \
1663 dadLx = s->dadLx; \
1664 dadRx = s->dadRx; \
1665 y = s->myY; \
1666 direction = s->myDirection; \
1667 myfree(s); \
1668} while (0)
1669
1670#define ST_STACK(dir,dadLx,dadRx,lx,rx,y) do { \
8d14daab
TC
1671 i_img_dim pushrx = rx+1; \
1672 i_img_dim pushlx = lx-1; \
e25e59b1
AMH
1673 ST_PUSH(lx,rx,pushlx,pushrx,y+dir,dir); \
1674 if (rx > dadRx) \
1675 ST_PUSH(dadRx+1,rx,pushlx,pushrx,y-dir,-dir); \
1676 if (lx < dadLx) ST_PUSH(lx,dadLx-1,pushlx,pushrx,y-dir,-dir); \
1677} while (0)
1678
1679#define SET(x,y) btm_set(btm,x,y)
02d1d628 1680
86d20cb9 1681/* INSIDE returns true if pixel is correct color and we haven't set it before. */
3efb0915 1682#define INSIDE(x,y, seed) ((!btm_test(btm,x,y) && ( i_gpix(im,x,y,&cval),cmpfunc(seed,&cval,channels) ) ))
02d1d628 1683
02d1d628 1684
02d1d628 1685
aa833c97
AMH
1686/* The function that does all the real work */
1687
1688static struct i_bitmap *
8d14daab
TC
1689i_flood_fill_low(i_img *im,i_img_dim seedx,i_img_dim seedy,
1690 i_img_dim *bxminp, i_img_dim *bxmaxp, i_img_dim *byminp, i_img_dim *bymaxp,
3efb0915 1691 i_color const *seed, ff_cmpfunc cmpfunc) {
8d14daab
TC
1692 i_img_dim ltx, rtx;
1693 i_img_dim tx = 0;
02d1d628 1694
8d14daab
TC
1695 i_img_dim bxmin = seedx;
1696 i_img_dim bxmax = seedx;
1697 i_img_dim bymin = seedy;
1698 i_img_dim bymax = seedy;
02d1d628
AMH
1699
1700 struct llist *st;
1701 struct i_bitmap *btm;
1702
8d14daab
TC
1703 int channels;
1704 i_img_dim xsize,ysize;
3efb0915 1705 i_color cval;
02d1d628 1706
a73aeb5f
AMH
1707 channels = im->channels;
1708 xsize = im->xsize;
1709 ysize = im->ysize;
02d1d628 1710
86d20cb9
AMH
1711 btm = btm_new(xsize, ysize);
1712 st = llist_new(100, sizeof(struct stack_element*));
02d1d628 1713
02d1d628 1714 /* Find the starting span and fill it */
3efb0915
TC
1715 ltx = i_lspan(im, seedx, seedy, seed, cmpfunc);
1716 rtx = i_rspan(im, seedx, seedy, seed, cmpfunc);
aa833c97 1717 for(tx=ltx; tx<=rtx; tx++) SET(tx, seedy);
353eb6e7
TC
1718 bxmin = ltx;
1719 bxmax = rtx;
02d1d628 1720
aa833c97
AMH
1721 ST_PUSH(ltx, rtx, ltx, rtx, seedy+1, 1);
1722 ST_PUSH(ltx, rtx, ltx, rtx, seedy-1, -1);
02d1d628
AMH
1723
1724 while(st->count) {
aa833c97 1725 /* Stack variables */
8d14daab
TC
1726 i_img_dim lx,rx;
1727 i_img_dim dadLx,dadRx;
1728 i_img_dim y;
aa833c97 1729 int direction;
e25e59b1 1730
8d14daab 1731 i_img_dim x;
aa833c97 1732 int wasIn=0;
02d1d628 1733
aa833c97
AMH
1734 ST_POP(); /* sets lx, rx, dadLx, dadRx, y, direction */
1735
1736
1737 if (y<0 || y>ysize-1) continue;
02d1d628
AMH
1738 if (bymin > y) bymin=y; /* in the worst case an extra line */
1739 if (bymax < y) bymax=y;
1740
e25e59b1
AMH
1741
1742 x = lx+1;
3efb0915 1743 if ( lx >= 0 && (wasIn = INSIDE(lx, y, seed)) ) {
aa833c97 1744 SET(lx, y);
02d1d628 1745 lx--;
95b9922f 1746 while(lx >= 0 && INSIDE(lx, y, seed)) {
02d1d628
AMH
1747 SET(lx,y);
1748 lx--;
1749 }
1750 }
1751
86d20cb9 1752 if (bxmin > lx) bxmin = lx;
02d1d628
AMH
1753 while(x <= xsize-1) {
1754 /* printf("x=%d\n",x); */
1755 if (wasIn) {
1756
3efb0915 1757 if (INSIDE(x, y, seed)) {
02d1d628
AMH
1758 /* case 1: was inside, am still inside */
1759 SET(x,y);
1760 } else {
1761 /* case 2: was inside, am no longer inside: just found the
1762 right edge of a span */
aa833c97 1763 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
02d1d628 1764
aa833c97 1765 if (bxmax < x) bxmax = x;
02d1d628
AMH
1766 wasIn=0;
1767 }
1768 } else {
aa833c97 1769 if (x > rx) goto EXT;
3efb0915 1770 if (INSIDE(x, y, seed)) {
aa833c97 1771 SET(x, y);
02d1d628 1772 /* case 3: Wasn't inside, am now: just found the start of a new run */
aa833c97
AMH
1773 wasIn = 1;
1774 lx = x;
02d1d628
AMH
1775 } else {
1776 /* case 4: Wasn't inside, still isn't */
1777 }
1778 }
1779 x++;
1780 }
1781 EXT: /* out of loop */
1782 if (wasIn) {
1783 /* hit an edge of the frame buffer while inside a run */
aa833c97
AMH
1784 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1785 if (bxmax < x) bxmax = x;
02d1d628
AMH
1786 }
1787 }
02d1d628 1788
02d1d628 1789 llist_destroy(st);
cc6483e0 1790
aa833c97
AMH
1791 *bxminp = bxmin;
1792 *bxmaxp = bxmax;
1793 *byminp = bymin;
1794 *bymaxp = bymax;
cc6483e0 1795
aa833c97
AMH
1796 return btm;
1797}
cc6483e0 1798
92bda632 1799/*
5715f7c3 1800=item i_flood_fill(C<im>, C<seedx>, C<seedy>, C<color>)
92bda632
TC
1801
1802=category Drawing
1803=synopsis i_flood_fill(im, 50, 50, &color);
cc6483e0 1804
5715f7c3
TC
1805Flood fills the 4-connected region starting from the point (C<seedx>,
1806C<seedy>) with I<color>.
cc6483e0 1807
5715f7c3 1808Returns false if (C<seedx>, C<seedy>) are outside the image.
92bda632
TC
1809
1810=cut
1811*/
cc6483e0 1812
aa833c97 1813undef_int
8d14daab
TC
1814i_flood_fill(i_img *im, i_img_dim seedx, i_img_dim seedy, const i_color *dcol) {
1815 i_img_dim bxmin, bxmax, bymin, bymax;
aa833c97 1816 struct i_bitmap *btm;
8d14daab 1817 i_img_dim x, y;
3efb0915 1818 i_color val;
cc6483e0 1819
aa833c97
AMH
1820 i_clear_error();
1821 if (seedx < 0 || seedx >= im->xsize ||
1822 seedy < 0 || seedy >= im->ysize) {
1823 i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
1824 return 0;
cc6483e0 1825 }
cc6483e0 1826
3efb0915
TC
1827 /* Get the reference color */
1828 i_gpix(im, seedx, seedy, &val);
1829
1830 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1831 &val, i_ccomp_normal);
cc6483e0 1832
aa833c97
AMH
1833 for(y=bymin;y<=bymax;y++)
1834 for(x=bxmin;x<=bxmax;x++)
1835 if (btm_test(btm,x,y))
1836 i_ppix(im,x,y,dcol);
1837 btm_destroy(btm);
1838 return 1;
cc6483e0
TC
1839}
1840
92bda632 1841/*
5715f7c3 1842=item i_flood_cfill(C<im>, C<seedx>, C<seedy>, C<fill>)
92bda632
TC
1843
1844=category Drawing
1845=synopsis i_flood_cfill(im, 50, 50, fill);
aa833c97 1846
5715f7c3
TC
1847Flood fills the 4-connected region starting from the point (C<seedx>,
1848C<seedy>) with C<fill>.
92bda632 1849
5715f7c3 1850Returns false if (C<seedx>, C<seedy>) are outside the image.
92bda632
TC
1851
1852=cut
1853*/
aa833c97 1854
a321d497 1855undef_int
8d14daab
TC
1856i_flood_cfill(i_img *im, i_img_dim seedx, i_img_dim seedy, i_fill_t *fill) {
1857 i_img_dim bxmin, bxmax, bymin, bymax;
cc6483e0 1858 struct i_bitmap *btm;
3efb0915 1859 i_color val;
cc6483e0 1860
aa833c97 1861 i_clear_error();
a321d497
AMH
1862
1863 if (seedx < 0 || seedx >= im->xsize ||
1864 seedy < 0 || seedy >= im->ysize) {
3e1e2ed3 1865 i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
a321d497
AMH
1866 return 0;
1867 }
1868
3efb0915
TC
1869 /* Get the reference color */
1870 i_gpix(im, seedx, seedy, &val);
1871
1872 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1873 &val, i_ccomp_normal);
1874
1875 cfill_from_btm(im, fill, btm, bxmin, bxmax, bymin, bymax);
1876
1877 btm_destroy(btm);
1878 return 1;
1879}
1880
1881/*
5715f7c3 1882=item i_flood_fill_border(C<im>, C<seedx>, C<seedy>, C<color>, C<border>)
3efb0915
TC
1883
1884=category Drawing
1885=synopsis i_flood_fill_border(im, 50, 50, &color, &border);
1886
5715f7c3
TC
1887Flood fills the 4-connected region starting from the point (C<seedx>,
1888C<seedy>) with C<color>, fill stops when the fill reaches a pixels
1889with color C<border>.
3efb0915 1890
5715f7c3 1891Returns false if (C<seedx>, C<seedy>) are outside the image.
3efb0915
TC
1892
1893=cut
1894*/
1895
1896undef_int
8d14daab 1897i_flood_fill_border(i_img *im, i_img_dim seedx, i_img_dim seedy, const i_color *dcol,
3efb0915 1898 const i_color *border) {
8d14daab 1899 i_img_dim bxmin, bxmax, bymin, bymax;
3efb0915 1900 struct i_bitmap *btm;
8d14daab 1901 i_img_dim x, y;
3efb0915
TC
1902
1903 i_clear_error();
1904 if (seedx < 0 || seedx >= im->xsize ||
1905 seedy < 0 || seedy >= im->ysize) {
1906 i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
1907 return 0;
1908 }
1909
1910 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1911 border, i_ccomp_border);
1912
1913 for(y=bymin;y<=bymax;y++)
1914 for(x=bxmin;x<=bxmax;x++)
1915 if (btm_test(btm,x,y))
1916 i_ppix(im,x,y,dcol);
1917 btm_destroy(btm);
1918 return 1;
1919}
1920
1921/*
5715f7c3 1922=item i_flood_cfill_border(C<im>, C<seedx>, C<seedy>, C<fill>, C<border>)
3efb0915
TC
1923
1924=category Drawing
1925=synopsis i_flood_cfill_border(im, 50, 50, fill, border);
1926
5715f7c3
TC
1927Flood fills the 4-connected region starting from the point (C<seedx>,
1928C<seedy>) with C<fill>, the fill stops when it reaches pixels of color
1929C<border>.
3efb0915 1930
5715f7c3 1931Returns false if (C<seedx>, C<seedy>) are outside the image.
3efb0915
TC
1932
1933=cut
1934*/
1935
1936undef_int
8d14daab 1937i_flood_cfill_border(i_img *im, i_img_dim seedx, i_img_dim seedy, i_fill_t *fill,
3efb0915 1938 const i_color *border) {
8d14daab 1939 i_img_dim bxmin, bxmax, bymin, bymax;
3efb0915
TC
1940 struct i_bitmap *btm;
1941
1942 i_clear_error();
1943
1944 if (seedx < 0 || seedx >= im->xsize ||
1945 seedy < 0 || seedy >= im->ysize) {
1946 i_push_error(0, "i_flood_cfill_border: Seed pixel outside of image");
1947 return 0;
1948 }
1949
1950 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1951 border, i_ccomp_border);
1952
1953 cfill_from_btm(im, fill, btm, bxmin, bxmax, bymin, bymax);
1954
1955 btm_destroy(btm);
1956
1957 return 1;
1958}
1959
1960static void
1961cfill_from_btm(i_img *im, i_fill_t *fill, struct i_bitmap *btm,
8d14daab
TC
1962 i_img_dim bxmin, i_img_dim bxmax, i_img_dim bymin, i_img_dim bymax) {
1963 i_img_dim x, y;
1964 i_img_dim start;
cc6483e0 1965
9b1ec2b8
TC
1966 i_render r;
1967
1968 i_render_init(&r, im, bxmax - bxmin + 1);
1969
1970 for(y=bymin; y<=bymax; y++) {
1971 x = bxmin;
1972 while (x <= bxmax) {
1973 while (x <= bxmax && !btm_test(btm, x, y)) {
1974 ++x;
cc6483e0 1975 }
9b1ec2b8
TC
1976 if (btm_test(btm, x, y)) {
1977 start = x;
1978 while (x <= bxmax && btm_test(btm, x, y)) {
1979 ++x;
1980 }
1981 i_render_fill(&r, start, y, x-start, NULL, fill);
cc6483e0
TC
1982 }
1983 }
cc6483e0 1984 }
9b1ec2b8 1985 i_render_done(&r);
cc6483e0 1986}