9 i_ppix_norm(i_img *im, i_img_dim x, i_img_dim y, i_color const *col) {
18 switch (im->channels) {
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);
30 i_adapt_colors(2, 4, &work, 1);
31 i_gpix(im, x, y, &src);
32 remains = 255 - work.channel[1];
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);
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);
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);
58 i_gpix(im, x, y, &src);
59 remains = 255 - work.channel[3];
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);
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);
79 cfill_from_btm(i_img *im, i_fill_t *fill, struct i_bitmap *btm,
80 i_img_dim bxmin, i_img_dim bxmax, i_img_dim bymin, i_img_dim bymax);
83 i_mmarray_cr(i_mmarray *ar,i_img_dim l) {
88 alloc_size = sizeof(minmax) * l;
89 /* check for overflow */
90 if (alloc_size / l != sizeof(minmax)) {
91 fprintf(stderr, "overflow calculating memory allocation");
94 ar->data=mymalloc(alloc_size); /* checked 5jul05 tonyc */
95 for(i=0;i<l;i++) { ar->data[i].max=-1; ar->data[i].min=MAXINT; }
99 i_mmarray_dst(i_mmarray *ar) {
101 if (ar->data != NULL) { myfree(ar->data); ar->data=NULL; }
105 i_mmarray_add(i_mmarray *ar,i_img_dim x,i_img_dim y) {
106 if (y>-1 && y<ar->lines)
108 if (x<ar->data[y].min) ar->data[y].min=x;
109 if (x>ar->data[y].max) ar->data[y].max=x;
114 i_mmarray_gmin(i_mmarray *ar,i_img_dim y) {
115 if (y>-1 && y<ar->lines) return ar->data[y].min;
120 i_mmarray_getm(i_mmarray *ar,i_img_dim y) {
121 if (y>-1 && y<ar->lines) return ar->data[y].max;
128 i_mmarray_render(i_img *im,i_mmarray *ar,i_color *val) {
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);
136 i_arcdraw(i_img_dim x1, i_img_dim y1, i_img_dim x2, i_img_dim y2, i_mmarray *ar) {
140 alpha=(double)(y2-y1)/(double)(x2-x1);
141 if (fabs(alpha) <= 1)
143 if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
147 i_mmarray_add(ar,x1,(i_img_dim)(dsec+0.5));
155 if (y2<y1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
159 i_mmarray_add(ar,(i_img_dim)(dsec+0.5),y1);
167 i_mmarray_info(i_mmarray *ar) {
169 for(i=0;i<ar->lines;i++)
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));
176 i_arc_minmax(i_int_hlines *hlines,i_img_dim x,i_img_dim y, double rad,float d1,float d2) {
181 i_mmarray_cr(&dot, hlines->limit_y);
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));
185 fx=(float)x1; fy=(float)y1;
187 /* printf("x1: %d.\ny1: %d.\n",x1,y1); */
188 i_arcdraw(x, y, x1, y1, &dot);
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));
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)));
196 /* printf("x1: %d.\ny1: %d.\n",x1,y1); */
197 i_arcdraw(x, y, x1, y1, &dot);
199 /* render the minmax values onto the hlines */
200 for (y = 0; y < dot.lines; y++) {
201 if (dot.data[y].max!=-1) {
202 i_img_dim minx, width;
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);
214 i_arc_hlines(i_int_hlines *hlines,i_img_dim x,i_img_dim y,double rad,float d1,float d2) {
216 i_arc_minmax(hlines, x, y, rad, d1, d2);
219 i_arc_minmax(hlines, x, y, rad, d1, 360);
220 i_arc_minmax(hlines, x, y, rad, 0, d2);
225 =item i_arc(im, x, y, rad, d1, d2, color)
228 =synopsis i_arc(im, 50, 50, 20, 45, 135, &color);
230 Fills an arc centered at (x,y) with radius I<rad> covering the range
231 of angles in degrees from d1 to d2, with the color.
237 i_arc(i_img *im, i_img_dim x, i_img_dim y,double rad,double d1,double d2,const i_color *val) {
240 i_int_init_hlines_img(&hlines, im);
242 i_arc_hlines(&hlines, x, y, rad, d1, d2);
244 i_int_hlines_fill_color(im, &hlines, val);
246 i_int_hlines_destroy(&hlines);
250 =item i_arc_cfill(im, x, y, rad, d1, d2, fill)
253 =synopsis i_arc_cfill(im, 50, 50, 35, 90, 135, fill);
255 Fills an arc centered at (x,y) with radius I<rad> covering the range
256 of angles in degrees from d1 to d2, with the fill object.
261 #define MIN_CIRCLE_STEPS 8
262 #define MAX_CIRCLE_STEPS 360
265 i_arc_cfill(i_img *im, i_img_dim x, i_img_dim y,double rad,double d1,double d2,i_fill_t *fill) {
268 i_int_init_hlines_img(&hlines, im);
270 i_arc_hlines(&hlines, x, y, rad, d1, d2);
272 i_int_hlines_fill_fill(im, &hlines, fill);
274 i_int_hlines_destroy(&hlines);
278 arc_poly(int *count, double **xvals, double **yvals,
279 double x, double y, double rad, double d1, double d2) {
280 double d1_rad, d2_rad;
282 i_img_dim steps, point_count;
285 /* normalize the angles */
288 if (d2 >= 360) { /* default is 361 */
302 d1_rad = d1 * PI / 180;
303 d2_rad = d2 * PI / 180;
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.
310 I was originally making steps = circum/2 but that looked horrible.
312 I think there might be an issue in the polygon filler.
314 circum = 2 * PI * rad;
316 if (steps > MAX_CIRCLE_STEPS)
317 steps = MAX_CIRCLE_STEPS;
318 else if (steps < MIN_CIRCLE_STEPS)
319 steps = MIN_CIRCLE_STEPS;
321 angle_inc = 2 * PI / steps;
323 point_count = steps + 5; /* rough */
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 */
328 /* from centre to edge at d1 */
331 (*xvals)[1] = x + rad * cos(d1_rad);
332 (*yvals)[1] = y + rad * sin(d1_rad);
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);
343 /* finish off the curve */
344 (*xvals)[*count] = x + rad * cos(d2_rad);
345 (*yvals)[*count] = y + rad * sin(d2_rad);
350 =item i_arc_aa(im, x, y, rad, d1, d2, color)
353 =synopsis i_arc_aa(im, 50, 50, 35, 90, 135, &color);
355 Anti-alias fills an arc centered at (x,y) with radius I<rad> covering
356 the range of angles in degrees from d1 to d2, with the color.
362 i_arc_aa(i_img *im, double x, double y, double rad, double d1, double d2,
363 const i_color *val) {
364 double *xvals, *yvals;
367 arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
369 i_poly_aa(im, count, xvals, yvals, val);
376 =item i_arc_aa_cfill(im, x, y, rad, d1, d2, fill)
379 =synopsis i_arc_aa_cfill(im, 50, 50, 35, 90, 135, fill);
381 Anti-alias fills an arc centered at (x,y) with radius I<rad> covering
382 the range of angles in degrees from d1 to d2, with the fill object.
388 i_arc_aa_cfill(i_img *im, double x, double y, double rad, double d1, double d2,
390 double *xvals, *yvals;
393 arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
395 i_poly_aa_cfill(im, count, xvals, yvals, fill);
401 /* Temporary AA HACK */
404 typedef i_img_dim frac;
405 static frac float_to_frac(double x) { return (frac)(0.5+x*16.0); }
409 polar_to_plane(double cx, double cy, float angle, double radius, frac *x, frac *y) {
410 *x = float_to_frac(cx+radius*cos(angle));
411 *y = float_to_frac(cy+radius*sin(angle));
416 make_minmax_list(i_mmarray *dot, double x, double y, double radius) {
418 float astep = radius>0.1 ? .5/radius : 10;
419 frac cx, cy, lx, ly, sx, sy;
421 mm_log((1, "make_minmax_list(dot %p, x %.2f, y %.2f, radius %.2f)\n", dot, x, y, radius));
423 polar_to_plane(x, y, angle, radius, &sx, &sy);
425 for(angle = 0.0; angle<361; angle +=astep) {
427 polar_to_plane(x, y, angle, radius, &cx, &cy);
430 if (fabs(cx-lx) > fabs(cy-ly)) {
433 ccx = lx; lx = cx; cx = ccx;
434 ccy = ly; ly = cy; cy = ccy;
437 for(ccx=lx; ccx<=cx; ccx++) {
438 ccy = ly + ((cy-ly)*(ccx-lx))/(cx-lx);
439 i_mmarray_add(dot, ccx, ccy);
445 ccy = ly; ly = cy; cy = ccy;
446 ccx = lx; lx = cx; cx = ccx;
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);
457 /* Get the number of subpixels covered */
461 i_pixel_coverage(i_mmarray *dot, i_img_dim x, i_img_dim y) {
467 for(cy=y*16; cy<(y+1)*16; cy++) {
468 frac tmin = dot->data[cy].min;
469 frac tmax = dot->data[cy].max;
471 if (tmax == -1 || tmin > maxx || tmax < minx) continue;
473 if (tmin < minx) tmin = minx;
474 if (tmax > maxx) tmax = maxx;
482 =item i_circle_aa(im, x, y, rad, color)
485 =synopsis i_circle_aa(im, 50, 50, 45, &color);
487 Anti-alias fills a circle centered at (x,y) for radius I<rad> with
493 i_circle_aa(i_img *im, double x, double y, double rad, const i_color *val) {
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));
501 i_mmarray_cr(&dot,16*im->ysize);
502 make_minmax_list(&dot, x, y, rad);
504 for(ly = 0; ly<im->ysize; ly++) {
505 int ix, cy, minx = INT_MAX, maxx = INT_MIN;
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;
513 if (minx > tmin) minx = tmin;
514 if (maxx < tmax) maxx = tmax;
517 if (maxx == INT_MIN) continue; /* no work to be done for this row of pixels */
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 */
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);
537 =item i_circle_out(im, x, y, r, col)
540 =synopsis i_circle_out(im, 50, 50, 45, &color);
542 Draw a circle outline centered at (x,y) with radius r,
551 (x, y) - the center of the circle
555 r - the radius of the circle in pixels, must be non-negative
559 Returns non-zero on success.
567 i_circle_out(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r,
568 const i_color *col) {
576 i_push_error(0, "circle: radius must be non-negative");
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);
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);
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);
618 Convert an angle in degrees into an angle measure we can generate
619 simply from the numbers we have when drawing the circle.
625 arc_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] */
631 if (seg == 4 && remains > 0)
634 return scale * (seg * 2 + sin(remains * PI/180));
638 =item i_arc_out(im, x, y, r, d1, d2, col)
641 =synopsis i_arc_out(im, 50, 50, 45, 45, 135, &color);
643 Draw an arc outline centered at (x,y) with radius r, non-anti-aliased
644 over the angle range d1 through d2 degrees.
652 (x, y) - the center of the circle
656 r - the radius of the circle in pixels, must be non-negative
660 d1, d2 - the range of angles to draw the arc over, in degrees.
664 Returns non-zero on success.
672 i_arc_out(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r,
673 double d1, double d2, const i_color *col) {
677 i_img_dim segs[2][2];
680 i_img_dim seg_d1, seg_d2;
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;
691 i_push_error(0, "arc: radius must be non-negative");
695 return i_circle_out(im, xc, yc, r, col);
698 d1 += 360 * floor((-d1 + 359) / 360);
700 d2 += 360 * floor((-d2 + 359) / 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 */
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];
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);
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);
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);
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);
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);
773 cover(i_img_dim r, i_img_dim j) {
774 double rjsqrt = sqrt(r*r - j*j);
776 return ceil(rjsqrt) - rjsqrt;
780 =item i_circle_out_aa(im, xc, yc, r, col)
782 =synopsis i_circle_out_aa(im, 50, 50, 45, &color);
784 Draw a circle outline centered at (x,y) with radius r, anti-aliased.
792 (xc, yc) - the center of the circle
796 r - the radius of the circle in pixels, must be non-negative
800 col - an i_color for the color to draw in.
804 Returns non-zero on success.
808 Based on "Fast Anti-Aliased Circle Generation", Xiaolin Wu, Graphics
811 I use floating point for I<D> since for large circles the precision of
812 a [0,255] value isn't sufficient when approaching the end of the
818 i_circle_out_aa(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r, const i_color *col) {
821 i_color workc = *col;
822 int orig_alpha = col->channel[3];
826 i_push_error(0, "arc: radius must be non-negative");
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);
842 cv = (int)(d * 255 + 0.5);
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);
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);
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);
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);
882 =item i_arc_out_aa(im, xc, yc, r, d1, d2, col)
884 =synopsis i_arc_out_aa(im, 50, 50, 45, 45, 125, &color);
886 Draw a circle arc outline centered at (x,y) with radius r, from angle
887 d1 degrees through angle d2 degrees, anti-aliased.
895 (xc, yc) - the center of the circle
899 r - the radius of the circle in pixels, must be non-negative
903 d1, d2 - the range of angle in degrees to draw the arc through. If
904 d2-d1 >= 360 a full circle is drawn.
908 Returns non-zero on success.
912 Based on "Fast Anti-Aliased Circle Generation", Xiaolin Wu, Graphics
918 i_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) {
921 i_color workc = *col;
922 i_img_dim segs[2][2];
925 i_img_dim seg_d1, seg_d2;
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;
936 i_push_error(0, "arc: radius must be non-negative");
940 return i_circle_out_aa(im, xc, yc, r, col);
943 d1 += 360 * floor((-d1 + 359) / 360);
945 d2 += 360 * floor((-d2 + 359) / 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 */
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];
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);
986 cv = (int)(d * 255 + 0.5);
993 workc.channel[3] = orig_alpha * inv_cv / 255;
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);
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);
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);
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);
1043 =item i_box(im, x1, y1, x2, y2, color)
1046 =synopsis i_box(im, 0, 0, im->xsize-1, im->ysize-1, &color).
1048 Outlines the box from (x1,y1) to (x2,y2) inclusive with I<color>.
1054 i_box(i_img *im,i_img_dim x1,i_img_dim y1,i_img_dim x2,i_img_dim y2,const i_color *val) {
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));
1058 for(x=x1;x<x2+1;x++) {
1059 i_ppix(im,x,y1,val);
1060 i_ppix(im,x,y2,val);
1062 for(y=y1;y<y2+1;y++) {
1063 i_ppix(im,x1,y,val);
1064 i_ppix(im,x2,y,val);
1069 =item i_box_filled(im, x1, y1, x2, y2, color)
1072 =synopsis i_box_filled(im, 0, 0, im->xsize-1, im->ysize-1, &color);
1074 Fills the box from (x1,y1) to (x2,y2) inclusive with color.
1080 i_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) {
1081 i_img_dim x, y, width;
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));
1087 if (x1 > x2 || y1 > y2
1089 || x1 >= im->xsize || y1 > im->ysize)
1094 if (x2 >= im->xsize)
1098 if (y2 >= im->ysize)
1101 width = x2 - x1 + 1;
1103 if (im->type == i_palette_type
1104 && i_findcolor(im, val, &index)) {
1105 i_palidx *line = mymalloc(sizeof(i_palidx) * width);
1107 for (x = 0; x < width; ++x)
1110 for (y = y1; y <= y2; ++y)
1111 i_ppal(im, x1, x2+1, y, line);
1116 i_color *line = mymalloc(sizeof(i_color) * width);
1118 for (x = 0; x < width; ++x)
1121 for (y = y1; y <= y2; ++y)
1122 i_plin(im, x1, x2+1, y, line);
1129 =item i_box_filledf(im, x1, y1, x2, y2, color)
1132 =synopsis i_box_filledf(im, 0, 0, im->xsize-1, im->ysize-1, &fcolor);
1134 Fills the box from (x1,y1) to (x2,y2) inclusive with a floating point
1141 i_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) {
1142 i_img_dim x, y, width;
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));
1147 if (x1 > x2 || y1 > y2
1149 || x1 >= im->xsize || y1 > im->ysize)
1154 if (x2 >= im->xsize)
1158 if (y2 >= im->ysize)
1161 width = x2 - x1 + 1;
1163 if (im->bits <= 8) {
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);
1170 i_box_filled(im, x1, y1, x2, y2, &c);
1173 i_fcolor *line = mymalloc(sizeof(i_fcolor) * width);
1175 for (x = 0; x < width; ++x)
1178 for (y = y1; y <= y2; ++y)
1179 i_plinf(im, x1, x2+1, y, line);
1188 =item i_box_cfill(im, x1, y1, x2, y2, fill)
1191 =synopsis i_box_cfill(im, 0, 0, im->xsize-1, im->ysize-1, fill);
1193 Fills the box from (x1,y1) to (x2,y2) inclusive with fill.
1199 i_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) {
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));
1212 if (y2 >= im->ysize)
1214 if (x1 >= x2 || y1 > y2)
1217 i_render_init(&r, im, x2-x1);
1219 i_render_fill(&r, x1, y1, x2-x1, NULL, fill);
1226 =item i_line(C<im>, C<x1>, C<y1>, C<x2>, C<y2>, C<color>, C<endp>)
1230 =for stopwords Bresenham's
1232 Draw a line to image using Bresenham's line drawing algorithm
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)
1246 i_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) {
1255 /* choose variable to iterate on */
1256 if (i_abs(dx) > i_abs(dy)) {
1257 i_img_dim dx2, dy2, cpy;
1262 t = x1; x1 = x2; x2 = t;
1263 t = y1; y1 = y2; y2 = t;
1281 for(x=x1; x<x2-1; x++) {
1288 i_ppix(im, x+1, y, val);
1291 i_img_dim dy2, dx2, cpx;
1296 t = x1; x1 = x2; x2 = t;
1297 t = y1; y1 = y2; y2 = t;
1315 for(y=y1; y<y2-1; y++) {
1322 i_ppix(im, x, y+1, val);
1326 i_ppix(im, x1, y1, val);
1327 i_ppix(im, x2, y2, val);
1329 if (x1 != x2 || y1 != y2)
1330 i_ppix(im, x1, y1, val);
1336 i_line_dda(i_img *im, i_img_dim x1, i_img_dim y1, i_img_dim x2, i_img_dim y2, i_color *val) {
1341 for(x=x1; x<=x2; x++) {
1342 dy = y1+ (x-x1)/(double)(x2-x1)*(y2-y1);
1343 i_ppix(im, x, (i_img_dim)(dy+0.5), val);
1348 =item i_line_aa(C<im>, C<x1>, C<x2>, C<y1>, C<y2>, C<color>, C<endp>)
1352 Anti-alias draws a line from (x1,y1) to (x2, y2) in color.
1354 The point (x2, y2) is drawn only if C<endp> is set.
1360 i_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) {
1368 /* choose variable to iterate on */
1369 if (i_abs(dx) > i_abs(dy)) {
1370 i_img_dim dx2, dy2, cpy;
1375 t = x1; x1 = x2; x2 = t;
1376 t = y1; y1 = y2; y2 = t;
1390 p = dy2 - dx2; /* this has to be like this for AA */
1394 for(x=x1; x<x2-1; x++) {
1397 double t = (dy) ? -(float)(p)/(float)(dx2) : 1;
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);
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);
1422 i_img_dim dy2, dx2, cpx;
1427 t = x1; x1 = x2; x2 = t;
1428 t = y1; y1 = y2; y2 = t;
1442 p = dx2 - dy2; /* this has to be like this for AA */
1446 for(y=y1; y<y2-1; y++) {
1449 double t = (dx) ? -(double)(p)/(double)(dy2) : 1;
1456 i_gpix(im,x,y+1,&tval);
1457 for(ch=0;ch<im->channels;ch++)
1458 tval.channel[ch]=(unsigned char)(t1*(double)tval.channel[ch]+t2*(double)val->channel[ch]);
1459 i_ppix(im,x,y+1,&tval);
1461 i_gpix(im,x+cpx,y+1,&tval);
1462 for(ch=0;ch<im->channels;ch++)
1463 tval.channel[ch]=(unsigned char)(t2*(double)tval.channel[ch]+t1*(double)val->channel[ch]);
1464 i_ppix(im,x+cpx,y+1,&tval);
1477 i_ppix(im, x1, y1, val);
1478 i_ppix(im, x2, y2, val);
1480 if (x1 != x2 || y1 != y2)
1481 i_ppix(im, x1, y1, val);
1488 perm(i_img_dim n,i_img_dim k) {
1492 for(i=k+1;i<=n;i++) r*=i;
1493 for(i=1;i<=(n-k);i++) r/=i;
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 */
1504 i_bezier_multi(i_img *im,int l,const double *x,const double *y, const i_color *val) {
1508 i_img_dim lx = 0,ly = 0;
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 */
1514 for(k=0;k<l;k++) bzcoef[k]=perm(n,k);
1518 /* for(k=0;k<l;k++) printf("bzcoef: %d -> %f\n",k,bzcoef[k]); */
1520 for(t=0;t<=1;t+=0.005) {
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);*/
1528 cx+=bzcoef[k]*x[k]*ccoef;
1529 cy+=bzcoef[k]*y[k]*ccoef;
1532 /* printf("%f -> (%d,%d)\n",t,(int)(0.5+cx),(int)(0.5+cy)); */
1534 i_line_aa(im,lx,ly,(i_img_dim)(0.5+cx),(i_img_dim)(0.5+cy),val, 1);
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);
1546 REF: Graphics Gems I. page 282+
1550 /* This should be moved into a seperate file? */
1552 /* This is the truncation used:
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
1562 /* Flood fill algorithm - based on the Ken Fishkins (pixar) gem in
1567 i_img_dim mylx,myrx;
1568 i_img_dim dadlx,dadrx;
1577 struct stack_element {
1578 i_img_dim myLx,myRx;
1579 i_img_dim dadLx,dadRx;
1585 /* create the link data to put push onto the stack */
1588 struct stack_element*
1589 crdata(i_img_dim left,i_img_dim right,i_img_dim dadl,i_img_dim dadr,i_img_dim y, int dir) {
1590 struct stack_element *ste;
1591 ste = mymalloc(sizeof(struct stack_element)); /* checked 5jul05 tonyc */
1597 ste->myDirection = dir;
1601 /* i_ccomp compares two colors and gives true if they are the same */
1603 typedef int (*ff_cmpfunc)(i_color const *c1, i_color const *c2, int channels);
1606 i_ccomp_normal(i_color const *val1, i_color const *val2, int ch) {
1608 for(i = 0; i < ch; i++)
1609 if (val1->channel[i] !=val2->channel[i])
1615 i_ccomp_border(i_color const *val1, i_color const *val2, int ch) {
1617 for(i = 0; i < ch; i++)
1618 if (val1->channel[i] !=val2->channel[i])
1624 i_lspan(i_img *im, i_img_dim seedx, i_img_dim seedy, i_color const *val, ff_cmpfunc cmpfunc) {
1627 if (seedx-1 < 0) break;
1628 i_gpix(im,seedx-1,seedy,&cval);
1629 if (!cmpfunc(val,&cval,im->channels))
1637 i_rspan(i_img *im, i_img_dim seedx, i_img_dim seedy, i_color const *val, ff_cmpfunc cmpfunc) {
1640 if (seedx+1 > im->xsize-1) break;
1641 i_gpix(im,seedx+1,seedy,&cval);
1642 if (!cmpfunc(val,&cval,im->channels)) break;
1648 /* Macro to create a link and push on to the list */
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); \
1655 /* pops the shadow on TOS into local variables lx,rx,y,direction,dadLx and dadRx */
1656 /* No overflow check! */
1658 #define ST_POP() do { \
1659 struct stack_element *s; \
1666 direction = s->myDirection; \
1670 #define ST_STACK(dir,dadLx,dadRx,lx,rx,y) do { \
1671 i_img_dim pushrx = rx+1; \
1672 i_img_dim pushlx = lx-1; \
1673 ST_PUSH(lx,rx,pushlx,pushrx,y+dir,dir); \
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); \
1679 #define SET(x,y) btm_set(btm,x,y)
1681 /* INSIDE returns true if pixel is correct color and we haven't set it before. */
1682 #define INSIDE(x,y, seed) ((!btm_test(btm,x,y) && ( i_gpix(im,x,y,&cval),cmpfunc(seed,&cval,channels) ) ))
1686 /* The function that does all the real work */
1688 static struct i_bitmap *
1689 i_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,
1691 i_color const *seed, ff_cmpfunc cmpfunc) {
1695 i_img_dim bxmin = seedx;
1696 i_img_dim bxmax = seedx;
1697 i_img_dim bymin = seedy;
1698 i_img_dim bymax = seedy;
1701 struct i_bitmap *btm;
1704 i_img_dim xsize,ysize;
1707 channels = im->channels;
1711 btm = btm_new(xsize, ysize);
1712 st = llist_new(100, sizeof(struct stack_element*));
1714 /* Find the starting span and fill it */
1715 ltx = i_lspan(im, seedx, seedy, seed, cmpfunc);
1716 rtx = i_rspan(im, seedx, seedy, seed, cmpfunc);
1717 for(tx=ltx; tx<=rtx; tx++) SET(tx, seedy);
1721 ST_PUSH(ltx, rtx, ltx, rtx, seedy+1, 1);
1722 ST_PUSH(ltx, rtx, ltx, rtx, seedy-1, -1);
1725 /* Stack variables */
1727 i_img_dim dadLx,dadRx;
1734 ST_POP(); /* sets lx, rx, dadLx, dadRx, y, direction */
1737 if (y<0 || y>ysize-1) continue;
1738 if (bymin > y) bymin=y; /* in the worst case an extra line */
1739 if (bymax < y) bymax=y;
1743 if ( lx >= 0 && (wasIn = INSIDE(lx, y, seed)) ) {
1746 while(lx >= 0 && INSIDE(lx, y, seed)) {
1752 if (bxmin > lx) bxmin = lx;
1753 while(x <= xsize-1) {
1754 /* printf("x=%d\n",x); */
1757 if (INSIDE(x, y, seed)) {
1758 /* case 1: was inside, am still inside */
1761 /* case 2: was inside, am no longer inside: just found the
1762 right edge of a span */
1763 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1765 if (bxmax < x) bxmax = x;
1769 if (x > rx) goto EXT;
1770 if (INSIDE(x, y, seed)) {
1772 /* case 3: Wasn't inside, am now: just found the start of a new run */
1776 /* case 4: Wasn't inside, still isn't */
1781 EXT: /* out of loop */
1783 /* hit an edge of the frame buffer while inside a run */
1784 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1785 if (bxmax < x) bxmax = x;
1800 =item i_flood_fill(C<im>, C<seedx>, C<seedy>, C<color>)
1803 =synopsis i_flood_fill(im, 50, 50, &color);
1805 Flood fills the 4-connected region starting from the point (C<seedx>,
1806 C<seedy>) with I<color>.
1808 Returns false if (C<seedx>, C<seedy>) are outside the image.
1814 i_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;
1816 struct i_bitmap *btm;
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");
1827 /* Get the reference color */
1828 i_gpix(im, seedx, seedy, &val);
1830 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1831 &val, i_ccomp_normal);
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);
1842 =item i_flood_cfill(C<im>, C<seedx>, C<seedy>, C<fill>)
1845 =synopsis i_flood_cfill(im, 50, 50, fill);
1847 Flood fills the 4-connected region starting from the point (C<seedx>,
1848 C<seedy>) with C<fill>.
1850 Returns false if (C<seedx>, C<seedy>) are outside the image.
1856 i_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;
1858 struct i_bitmap *btm;
1863 if (seedx < 0 || seedx >= im->xsize ||
1864 seedy < 0 || seedy >= im->ysize) {
1865 i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
1869 /* Get the reference color */
1870 i_gpix(im, seedx, seedy, &val);
1872 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1873 &val, i_ccomp_normal);
1875 cfill_from_btm(im, fill, btm, bxmin, bxmax, bymin, bymax);
1882 =item i_flood_fill_border(C<im>, C<seedx>, C<seedy>, C<color>, C<border>)
1885 =synopsis i_flood_fill_border(im, 50, 50, &color, &border);
1887 Flood fills the 4-connected region starting from the point (C<seedx>,
1888 C<seedy>) with C<color>, fill stops when the fill reaches a pixels
1889 with color C<border>.
1891 Returns false if (C<seedx>, C<seedy>) are outside the image.
1897 i_flood_fill_border(i_img *im, i_img_dim seedx, i_img_dim seedy, const i_color *dcol,
1898 const i_color *border) {
1899 i_img_dim bxmin, bxmax, bymin, bymax;
1900 struct i_bitmap *btm;
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");
1910 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1911 border, i_ccomp_border);
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);
1922 =item i_flood_cfill_border(C<im>, C<seedx>, C<seedy>, C<fill>, C<border>)
1925 =synopsis i_flood_cfill_border(im, 50, 50, fill, border);
1927 Flood fills the 4-connected region starting from the point (C<seedx>,
1928 C<seedy>) with C<fill>, the fill stops when it reaches pixels of color
1931 Returns false if (C<seedx>, C<seedy>) are outside the image.
1937 i_flood_cfill_border(i_img *im, i_img_dim seedx, i_img_dim seedy, i_fill_t *fill,
1938 const i_color *border) {
1939 i_img_dim bxmin, bxmax, bymin, bymax;
1940 struct i_bitmap *btm;
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");
1950 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1951 border, i_ccomp_border);
1953 cfill_from_btm(im, fill, btm, bxmin, bxmax, bymin, bymax);
1961 cfill_from_btm(i_img *im, i_fill_t *fill, struct i_bitmap *btm,
1962 i_img_dim bxmin, i_img_dim bxmax, i_img_dim bymin, i_img_dim bymax) {
1968 i_render_init(&r, im, bxmax - bxmin + 1);
1970 for(y=bymin; y<=bymax; y++) {
1972 while (x <= bxmax) {
1973 while (x <= bxmax && !btm_test(btm, x, y)) {
1976 if (btm_test(btm, x, y)) {
1978 while (x <= bxmax && btm_test(btm, x, y)) {
1981 i_render_fill(&r, start, y, x-start, NULL, fill);