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 dest_alpha = work.channel[1] + remains * src.channel[1] / 255;
33 if (work.channel[1] == 255) {
34 return i_ppix(im, x, y, &work);
37 src.channel[0] = (work.channel[1] * work.channel[0]
38 + remains * src.channel[0] * src.channel[1] / 255) / dest_alpha;
39 src.channel[1] = dest_alpha;
40 return i_ppix(im, x, y, &src);
45 i_gpix(im, x, y, &src);
46 remains = 255 - work.channel[3];
47 src.channel[0] = (src.channel[0] * remains
48 + work.channel[0] * work.channel[3]) / 255;
49 src.channel[1] = (src.channel[1] * remains
50 + work.channel[1] * work.channel[3]) / 255;
51 src.channel[2] = (src.channel[2] * remains
52 + work.channel[2] * work.channel[3]) / 255;
53 return i_ppix(im, x, y, &src);
57 i_gpix(im, x, y, &src);
58 dest_alpha = work.channel[3] + remains * src.channel[3] / 255;
59 if (work.channel[3] == 255) {
60 return i_ppix(im, x, y, &work);
63 src.channel[0] = (work.channel[3] * work.channel[0]
64 + remains * src.channel[0] * src.channel[3] / 255) / dest_alpha;
65 src.channel[1] = (work.channel[3] * work.channel[1]
66 + remains * src.channel[1] * src.channel[3] / 255) / dest_alpha;
67 src.channel[2] = (work.channel[3] * work.channel[2]
68 + remains * src.channel[2] * src.channel[3] / 255) / dest_alpha;
69 src.channel[3] = dest_alpha;
70 return i_ppix(im, x, y, &src);
77 cfill_from_btm(i_img *im, i_fill_t *fill, struct i_bitmap *btm,
78 int bxmin, int bxmax, int bymin, int bymax);
81 i_mmarray_cr(i_mmarray *ar,int l) {
86 alloc_size = sizeof(minmax) * l;
87 /* check for overflow */
88 if (alloc_size / l != sizeof(minmax)) {
89 fprintf(stderr, "overflow calculating memory allocation");
92 ar->data=mymalloc(alloc_size); /* checked 5jul05 tonyc */
93 for(i=0;i<l;i++) { ar->data[i].max=-1; ar->data[i].min=MAXINT; }
97 i_mmarray_dst(i_mmarray *ar) {
99 if (ar->data != NULL) { myfree(ar->data); ar->data=NULL; }
103 i_mmarray_add(i_mmarray *ar,int x,int y) {
104 if (y>-1 && y<ar->lines)
106 if (x<ar->data[y].min) ar->data[y].min=x;
107 if (x>ar->data[y].max) ar->data[y].max=x;
112 i_mmarray_gmin(i_mmarray *ar,int y) {
113 if (y>-1 && y<ar->lines) return ar->data[y].min;
118 i_mmarray_getm(i_mmarray *ar,int y) {
119 if (y>-1 && y<ar->lines) return ar->data[y].max;
124 i_mmarray_render(i_img *im,i_mmarray *ar,i_color *val) {
126 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 i_arcdraw(int x1, int y1, int x2, int y2, i_mmarray *ar) {
135 alpha=(double)(y2-y1)/(double)(x2-x1);
136 if (fabs(alpha) <= 1)
138 if (x2<x1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
142 i_mmarray_add(ar,x1,(int)(dsec+0.5));
150 if (y2<y1) { temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }
154 i_mmarray_add(ar,(int)(dsec+0.5),y1);
162 i_mmarray_info(i_mmarray *ar) {
164 for(i=0;i<ar->lines;i++)
165 if (ar->data[i].max!=-1) printf("line %d: min=%d, max=%d.\n",i,ar->data[i].min,ar->data[i].max);
169 i_arc_minmax(i_int_hlines *hlines,int x,int y,float rad,float d1,float d2) {
174 /*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));*/
176 i_mmarray_cr(&dot, hlines->limit_y);
178 x1=(int)(x+0.5+rad*cos(d1*PI/180.0));
179 y1=(int)(y+0.5+rad*sin(d1*PI/180.0));
180 fx=(float)x1; fy=(float)y1;
182 /* printf("x1: %d.\ny1: %d.\n",x1,y1); */
183 i_arcdraw(x, y, x1, y1, &dot);
185 x1=(int)(x+0.5+rad*cos(d2*PI/180.0));
186 y1=(int)(y+0.5+rad*sin(d2*PI/180.0));
188 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)));
190 /* printf("x1: %d.\ny1: %d.\n",x1,y1); */
191 i_arcdraw(x, y, x1, y1, &dot);
193 /* render the minmax values onto the hlines */
194 for (y = 0; y < dot.lines; y++) {
195 if (dot.data[y].max!=-1) {
197 minx = dot.data[y].min;
198 width = dot.data[y].max - dot.data[y].min + 1;
199 i_int_hlines_add(hlines, y, minx, width);
208 i_arc_hlines(i_int_hlines *hlines,int x,int y,float rad,float d1,float d2) {
210 i_arc_minmax(hlines, x, y, rad, d1, d2);
213 i_arc_minmax(hlines, x, y, rad, d1, 360);
214 i_arc_minmax(hlines, x, y, rad, 0, d2);
219 =item i_arc(im, x, y, rad, d1, d2, color)
222 =synopsis i_arc(im, 50, 50, 20, 45, 135, &color);
224 Fills an arc centered at (x,y) with radius I<rad> covering the range
225 of angles in degrees from d1 to d2, with the color.
231 i_arc(i_img *im,int x,int y,float rad,float d1,float d2,const i_color *val) {
234 i_int_init_hlines_img(&hlines, im);
236 i_arc_hlines(&hlines, x, y, rad, d1, d2);
238 i_int_hlines_fill_color(im, &hlines, val);
240 i_int_hlines_destroy(&hlines);
244 =item i_arc_cfill(im, x, y, rad, d1, d2, fill)
247 =synopsis i_arc_cfill(im, 50, 50, 35, 90, 135, fill);
249 Fills an arc centered at (x,y) with radius I<rad> covering the range
250 of angles in degrees from d1 to d2, with the fill object.
255 #define MIN_CIRCLE_STEPS 8
256 #define MAX_CIRCLE_STEPS 360
259 i_arc_cfill(i_img *im,int x,int y,float rad,float d1,float d2,i_fill_t *fill) {
262 i_int_init_hlines_img(&hlines, im);
264 i_arc_hlines(&hlines, x, y, rad, d1, d2);
266 i_int_hlines_fill_fill(im, &hlines, fill);
268 i_int_hlines_destroy(&hlines);
272 arc_poly(int *count, double **xvals, double **yvals,
273 double x, double y, double rad, double d1, double d2) {
274 double d1_rad, d2_rad;
276 int steps, point_count;
279 /* normalize the angles */
282 if (d2 >= 360) { /* default is 361 */
296 d1_rad = d1 * PI / 180;
297 d2_rad = d2 * PI / 180;
299 /* how many segments for the curved part?
300 we do a maximum of one per degree, with a minimum of 8/circle
301 we try to aim at having about one segment per 2 pixels
302 Work it out per circle to get a step size.
304 I was originally making steps = circum/2 but that looked horrible.
306 I think there might be an issue in the polygon filler.
308 circum = 2 * PI * rad;
310 if (steps > MAX_CIRCLE_STEPS)
311 steps = MAX_CIRCLE_STEPS;
312 else if (steps < MIN_CIRCLE_STEPS)
313 steps = MIN_CIRCLE_STEPS;
315 angle_inc = 2 * PI / steps;
317 point_count = steps + 5; /* rough */
318 /* point_count is always relatively small, so allocation won't overflow */
319 *xvals = mymalloc(point_count * sizeof(double)); /* checked 17feb2005 tonyc */
320 *yvals = mymalloc(point_count * sizeof(double)); /* checked 17feb2005 tonyc */
322 /* from centre to edge at d1 */
325 (*xvals)[1] = x + rad * cos(d1_rad);
326 (*yvals)[1] = y + rad * sin(d1_rad);
329 /* step around the curve */
330 while (d1_rad < d2_rad) {
331 (*xvals)[*count] = x + rad * cos(d1_rad);
332 (*yvals)[*count] = y + rad * sin(d1_rad);
337 /* finish off the curve */
338 (*xvals)[*count] = x + rad * cos(d2_rad);
339 (*yvals)[*count] = y + rad * sin(d2_rad);
344 =item i_arc_aa(im, x, y, rad, d1, d2, color)
347 =synopsis i_arc_aa(im, 50, 50, 35, 90, 135, &color);
349 Anti-alias fills an arc centered at (x,y) with radius I<rad> covering
350 the range of angles in degrees from d1 to d2, with the color.
356 i_arc_aa(i_img *im, double x, double y, double rad, double d1, double d2,
357 const i_color *val) {
358 double *xvals, *yvals;
361 arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
363 i_poly_aa(im, count, xvals, yvals, val);
370 =item i_arc_aa_cfill(im, x, y, rad, d1, d2, fill)
373 =synopsis i_arc_aa_cfill(im, 50, 50, 35, 90, 135, fill);
375 Anti-alias fills an arc centered at (x,y) with radius I<rad> covering
376 the range of angles in degrees from d1 to d2, with the fill object.
382 i_arc_aa_cfill(i_img *im, double x, double y, double rad, double d1, double d2,
384 double *xvals, *yvals;
387 arc_poly(&count, &xvals, &yvals, x, y, rad, d1, d2);
389 i_poly_aa_cfill(im, count, xvals, yvals, fill);
395 /* Temporary AA HACK */
399 static frac float_to_frac(float x) { return (frac)(0.5+x*16.0); }
403 polar_to_plane(float cx, float cy, float angle, float radius, frac *x, frac *y) {
404 *x = float_to_frac(cx+radius*cos(angle));
405 *y = float_to_frac(cy+radius*sin(angle));
410 make_minmax_list(i_mmarray *dot, float x, float y, float radius) {
412 float astep = radius>0.1 ? .5/radius : 10;
413 frac cx, cy, lx, ly, sx, sy;
415 mm_log((1, "make_minmax_list(dot %p, x %.2f, y %.2f, radius %.2f)\n", dot, x, y, radius));
417 polar_to_plane(x, y, angle, radius, &sx, &sy);
419 for(angle = 0.0; angle<361; angle +=astep) {
421 polar_to_plane(x, y, angle, radius, &cx, &cy);
424 if (fabs(cx-lx) > fabs(cy-ly)) {
427 ccx = lx; lx = cx; cx = ccx;
428 ccy = ly; ly = cy; cy = ccy;
431 for(ccx=lx; ccx<=cx; ccx++) {
432 ccy = ly + ((cy-ly)*(ccx-lx))/(cx-lx);
433 i_mmarray_add(dot, ccx, ccy);
439 ccy = ly; ly = cy; cy = ccy;
440 ccx = lx; lx = cx; cx = ccx;
443 for(ccy=ly; ccy<=cy; ccy++) {
444 if (cy-ly) ccx = lx + ((cx-lx)*(ccy-ly))/(cy-ly); else ccx = lx;
445 i_mmarray_add(dot, ccx, ccy);
451 /* Get the number of subpixels covered */
455 i_pixel_coverage(i_mmarray *dot, int x, int y) {
461 for(cy=y*16; cy<(y+1)*16; cy++) {
462 frac tmin = dot->data[cy].min;
463 frac tmax = dot->data[cy].max;
465 if (tmax == -1 || tmin > maxx || tmax < minx) continue;
467 if (tmin < minx) tmin = minx;
468 if (tmax > maxx) tmax = maxx;
476 =item i_circle_aa(im, x, y, rad, color)
479 =synopsis i_circle_aa(im, 50, 50, 45, &color);
481 Anti-alias fills a circle centered at (x,y) for radius I<rad> with
487 i_circle_aa(i_img *im, float x, float y, float rad, const i_color *val) {
492 mm_log((1, "i_circle_aa(im %p, x %d, y %d, rad %.2f, val %p)\n", im, x, y, rad, val));
494 i_mmarray_cr(&dot,16*im->ysize);
495 make_minmax_list(&dot, x, y, rad);
497 for(ly = 0; ly<im->ysize; ly++) {
498 int ix, cy, minx = INT_MAX, maxx = INT_MIN;
500 /* Find the left/rightmost set subpixels */
501 for(cy = 0; cy<16; cy++) {
502 frac tmin = dot.data[ly*16+cy].min;
503 frac tmax = dot.data[ly*16+cy].max;
504 if (tmax == -1) continue;
506 if (minx > tmin) minx = tmin;
507 if (maxx < tmax) maxx = tmax;
510 if (maxx == INT_MIN) continue; /* no work to be done for this row of pixels */
514 for(ix=minx; ix<=maxx; ix++) {
515 int cnt = i_pixel_coverage(&dot, ix, ly);
516 if (cnt>255) cnt = 255;
517 if (cnt) { /* should never be true */
519 float ratio = (float)cnt/255.0;
520 i_gpix(im, ix, ly, &temp);
521 for(ch=0;ch<im->channels; ch++) temp.channel[ch] = (unsigned char)((float)val->channel[ch]*ratio + (float)temp.channel[ch]*(1.0-ratio));
522 i_ppix(im, ix, ly, &temp);
530 =item i_circle_out(im, x, y, r, col)
533 =synopsis i_circle_out(im, 50, 50, 45, &color);
535 Draw a circle outline centered at (x,y) with radius r,
544 (x, y) - the center of the circle
548 r - the radius of the circle in pixels, must be non-negative
552 Returns non-zero on success.
560 i_circle_out(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r,
561 const i_color *col) {
569 i_push_error(0, "circle: radius must be non-negative");
573 i_ppix(im, xc+r, yc, col);
574 i_ppix(im, xc-r, yc, col);
575 i_ppix(im, xc, yc+r, col);
576 i_ppix(im, xc, yc-r, col);
593 i_ppix(im, xc + x, yc + y, col);
594 i_ppix(im, xc + x, yc - y, col);
595 i_ppix(im, xc - x, yc + y, col);
596 i_ppix(im, xc - x, yc - y, col);
598 i_ppix(im, xc + y, yc + x, col);
599 i_ppix(im, xc + y, yc - x, col);
600 i_ppix(im, xc - y, yc + x, col);
601 i_ppix(im, xc - y, yc - x, col);
611 Convert an angle in degrees into an angle measure we can generate
612 simply from the numbers we have when drawing the circle.
618 arc_seg(double angle, int scale) {
619 i_img_dim seg = (angle + 45) / 90;
620 double remains = angle - seg * 90; /* should be in the range [-45,45] */
621 int sign = remains < 0 ? -1 : remains ? 1 : 0;
625 if (seg == 4 && remains > 0)
628 return scale * (seg * 2 + sin(remains * PI/180));
632 =item i_arc_out(im, x, y, r, d1, d2, col)
635 =synopsis i_arc_out(im, 50, 50, 45, 45, 135, &color);
637 Draw an arc outline centered at (x,y) with radius r, non-anti-aliased
638 over the angle range d1 through d2 degrees.
646 (x, y) - the center of the circle
650 r - the radius of the circle in pixels, must be non-negative
654 d1, d2 - the range of angles to draw the arc over, in degrees.
658 Returns non-zero on success.
666 i_arc_out(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r,
667 float d1, float d2, const i_color *col) {
671 i_img_dim segs[2][2];
674 i_img_dim seg_d1, seg_d2;
677 i_img_dim scale = r + 1;
678 i_img_dim seg1 = scale * 2;
679 i_img_dim seg2 = scale * 4;
680 i_img_dim seg3 = scale * 6;
681 i_img_dim seg4 = scale * 8;
686 i_push_error(0, "arc: radius must be non-negative");
690 return i_circle_out(im, xc, yc, r, col);
693 d1 += 360 * floor((-d1 + 359) / 360);
695 d2 += 360 * floor((-d2 + 359) / 360);
698 seg_d1 = arc_seg(d1, scale);
699 seg_d2 = arc_seg(d2, scale);
700 if (seg_d2 < seg_d1) {
701 /* split into two segments */
714 for (seg_num = 0; seg_num < seg_count; ++seg_num) {
715 i_img_dim seg_start = segs[seg_num][0];
716 i_img_dim seg_end = segs[seg_num][1];
718 i_ppix(im, xc+r, yc, col);
719 if (seg_start <= seg1 && seg_end >= seg1)
720 i_ppix(im, xc, yc+r, col);
721 if (seg_start <= seg2 && seg_end >= seg2)
722 i_ppix(im, xc-r, yc, col);
723 if (seg_start <= seg3 && seg_end >= seg3)
724 i_ppix(im, xc, yc-r, col);
742 if (seg_start <= sin_th && seg_end >= sin_th)
743 i_ppix(im, xc + x, yc + y, col);
744 if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
745 i_ppix(im, xc + y, yc + x, col);
747 if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
748 i_ppix(im, xc - y, yc + x, col);
749 if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
750 i_ppix(im, xc - x, yc + y, col);
752 if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
753 i_ppix(im, xc - x, yc - y, col);
754 if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
755 i_ppix(im, xc - y, yc - x, col);
757 if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
758 i_ppix(im, xc + y, yc - x, col);
759 if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
760 i_ppix(im, xc + x, yc - y, col);
768 cover(i_img_dim r, i_img_dim j) {
769 float rjsqrt = sqrt(r*r - j*j);
771 return ceil(rjsqrt) - rjsqrt;
775 =item i_circle_out_aa(im, xc, yc, r, col)
777 =synopsis i_circle_out_aa(im, 50, 50, 45, &color);
779 Draw a circle outline centered at (x,y) with radius r, anti-aliased.
787 (xc, yc) - the center of the circle
791 r - the radius of the circle in pixels, must be non-negative
795 col - an i_color for the color to draw in.
799 Returns non-zero on success.
803 Based on "Fast Anti-Aliased Circle Generation", Xiaolin Wu, Graphics
806 I use floating point for I<D> since for large circles the precision of
807 a [0,255] value isn't sufficient when approaching the end of the
813 i_circle_out_aa(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r, const i_color *col) {
816 i_color workc = *col;
817 int orig_alpha = col->channel[3];
821 i_push_error(0, "arc: radius must be non-negative");
827 i_ppix_norm(im, xc+i, yc+j, col);
828 i_ppix_norm(im, xc-i, yc+j, col);
829 i_ppix_norm(im, xc+j, yc+i, col);
830 i_ppix_norm(im, xc+j, yc-i, col);
839 cv = (int)(d * 255 + 0.5);
845 workc.channel[3] = orig_alpha * inv_cv / 255;
846 i_ppix_norm(im, xc+i, yc+j, &workc);
847 i_ppix_norm(im, xc-i, yc+j, &workc);
848 i_ppix_norm(im, xc+i, yc-j, &workc);
849 i_ppix_norm(im, xc-i, yc-j, &workc);
852 i_ppix_norm(im, xc+j, yc+i, &workc);
853 i_ppix_norm(im, xc-j, yc+i, &workc);
854 i_ppix_norm(im, xc+j, yc-i, &workc);
855 i_ppix_norm(im, xc-j, yc-i, &workc);
859 workc.channel[3] = orig_alpha * cv / 255;
860 i_ppix_norm(im, xc+i-1, yc+j, &workc);
861 i_ppix_norm(im, xc-i+1, yc+j, &workc);
862 i_ppix_norm(im, xc+i-1, yc-j, &workc);
863 i_ppix_norm(im, xc-i+1, yc-j, &workc);
866 i_ppix_norm(im, xc+j, yc+i-1, &workc);
867 i_ppix_norm(im, xc-j, yc+i-1, &workc);
868 i_ppix_norm(im, xc+j, yc-i+1, &workc);
869 i_ppix_norm(im, xc-j, yc-i+1, &workc);
879 =item i_arc_out_aa(im, xc, yc, r, d1, d2, col)
881 =synopsis i_arc_out_aa(im, 50, 50, 45, 45, 125, &color);
883 Draw a circle arc outline centered at (x,y) with radius r, from angle
884 d1 degrees through angle d2 degrees, anti-aliased.
892 (xc, yc) - the center of the circle
896 r - the radius of the circle in pixels, must be non-negative
900 d1, d2 - the range of angle in degrees to draw the arc through. If
901 d2-d1 >= 360 a full circle is drawn.
905 Returns non-zero on success.
909 Based on "Fast Anti-Aliased Circle Generation", Xiaolin Wu, Graphics
915 i_arc_out_aa(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r, float d1, float d2, const i_color *col) {
918 i_color workc = *col;
919 i_img_dim segs[2][2];
922 i_img_dim seg_d1, seg_d2;
924 int orig_alpha = col->channel[3];
925 i_img_dim scale = r + 1;
926 i_img_dim seg1 = scale * 2;
927 i_img_dim seg2 = scale * 4;
928 i_img_dim seg3 = scale * 6;
929 i_img_dim seg4 = scale * 8;
933 i_push_error(0, "arc: radius must be non-negative");
937 return i_circle_out_aa(im, xc, yc, r, col);
940 d1 += 360 * floor((-d1 + 359) / 360);
942 d2 += 360 * floor((-d2 + 359) / 360);
945 seg_d1 = arc_seg(d1, scale);
946 seg_d2 = arc_seg(d2, scale);
947 if (seg_d2 < seg_d1) {
948 /* split into two segments */
961 for (seg_num = 0; seg_num < seg_count; ++seg_num) {
962 i_img_dim seg_start = segs[seg_num][0];
963 i_img_dim seg_end = segs[seg_num][1];
970 i_ppix_norm(im, xc+i, yc+j, col);
971 if (seg_start <= seg1 && seg_end >= seg1)
972 i_ppix_norm(im, xc+j, yc+i, col);
973 if (seg_start <= seg2 && seg_end >= seg2)
974 i_ppix_norm(im, xc-i, yc+j, col);
975 if (seg_start <= seg3 && seg_end >= seg3)
976 i_ppix_norm(im, xc+j, yc-i, col);
985 cv = (int)(d * 255 + 0.5);
992 workc.channel[3] = orig_alpha * inv_cv / 255;
994 if (seg_start <= sin_th && seg_end >= sin_th)
995 i_ppix_norm(im, xc+i, yc+j, &workc);
996 if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
997 i_ppix_norm(im, xc-i, yc+j, &workc);
998 if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
999 i_ppix_norm(im, xc+i, yc-j, &workc);
1000 if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
1001 i_ppix_norm(im, xc-i, yc-j, &workc);
1004 if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
1005 i_ppix_norm(im, xc+j, yc+i, &workc);
1006 if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
1007 i_ppix_norm(im, xc-j, yc+i, &workc);
1008 if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
1009 i_ppix_norm(im, xc+j, yc-i, &workc);
1010 if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
1011 i_ppix_norm(im, xc-j, yc-i, &workc);
1015 workc.channel[3] = orig_alpha * cv / 255;
1016 if (seg_start <= sin_th && seg_end >= sin_th)
1017 i_ppix_norm(im, xc+i-1, yc+j, &workc);
1018 if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
1019 i_ppix_norm(im, xc-i+1, yc+j, &workc);
1020 if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
1021 i_ppix_norm(im, xc+i-1, yc-j, &workc);
1022 if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
1023 i_ppix_norm(im, xc-i+1, yc-j, &workc);
1025 if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
1026 i_ppix_norm(im, xc+j, yc+i-1, &workc);
1027 if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
1028 i_ppix_norm(im, xc-j, yc+i-1, &workc);
1029 if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
1030 i_ppix_norm(im, xc+j, yc-i+1, &workc);
1031 if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
1032 i_ppix_norm(im, xc-j, yc-i+1, &workc);
1042 =item i_box(im, x1, y1, x2, y2, color)
1045 =synopsis i_box(im, 0, 0, im->xsize-1, im->ysize-1, &color).
1047 Outlines the box from (x1,y1) to (x2,y2) inclusive with I<color>.
1053 i_box(i_img *im,int x1,int y1,int x2,int y2,const i_color *val) {
1055 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));
1056 for(x=x1;x<x2+1;x++) {
1057 i_ppix(im,x,y1,val);
1058 i_ppix(im,x,y2,val);
1060 for(y=y1;y<y2+1;y++) {
1061 i_ppix(im,x1,y,val);
1062 i_ppix(im,x2,y,val);
1067 =item i_box_filled(im, x1, y1, x2, y2, color)
1070 =synopsis i_box_filled(im, 0, 0, im->xsize-1, im->ysize-1, &color);
1072 Fills the box from (x1,y1) to (x2,y2) inclusive with color.
1078 i_box_filled(i_img *im,int x1,int y1,int x2,int y2, const i_color *val) {
1079 i_img_dim x, y, width;
1082 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));
1084 if (x1 > x2 || y1 > y2
1086 || x1 >= im->xsize || y1 > im->ysize)
1091 if (x2 >= im->xsize)
1095 if (y2 >= im->ysize)
1098 width = x2 - x1 + 1;
1100 if (im->type == i_palette_type
1101 && i_findcolor(im, val, &index)) {
1102 i_palidx *line = mymalloc(sizeof(i_palidx) * width);
1104 for (x = 0; x < width; ++x)
1107 for (y = y1; y <= y2; ++y)
1108 i_ppal(im, x1, x2+1, y, line);
1113 i_color *line = mymalloc(sizeof(i_color) * width);
1115 for (x = 0; x < width; ++x)
1118 for (y = y1; y <= y2; ++y)
1119 i_plin(im, x1, x2+1, y, line);
1126 =item i_box_cfill(im, x1, y1, x2, y2, fill)
1129 =synopsis i_box_cfill(im, 0, 0, im->xsize-1, im->ysize-1, fill);
1131 Fills the box from (x1,y1) to (x2,y2) inclusive with fill.
1137 i_box_cfill(i_img *im,int x1,int y1,int x2,int y2,i_fill_t *fill) {
1139 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));
1148 if (y2 >= im->ysize)
1150 if (x1 >= x2 || y1 > y2)
1153 i_render_init(&r, im, x2-x1);
1155 i_render_fill(&r, x1, y1, x2-x1, NULL, fill);
1162 =item i_line(C<im>, C<x1>, C<y1>, C<x2>, C<y2>, C<color>, C<endp>)
1166 =for stopwords Bresenham's
1168 Draw a line to image using Bresenham's line drawing algorithm
1170 im - image to draw to
1171 x1 - starting x coordinate
1172 y1 - starting x coordinate
1173 x2 - starting x coordinate
1174 y2 - starting x coordinate
1175 color - color to write to image
1176 endp - endpoint flag (boolean)
1182 i_line(i_img *im, int x1, int y1, int x2, int y2, const i_color *val, int endp) {
1191 /* choose variable to iterate on */
1192 if (abs(dx)>abs(dy)) {
1198 t = x1; x1 = x2; x2 = t;
1199 t = y1; y1 = y2; y2 = t;
1217 for(x=x1; x<x2-1; x++) {
1224 i_ppix(im, x+1, y, val);
1232 t = x1; x1 = x2; x2 = t;
1233 t = y1; y1 = y2; y2 = t;
1251 for(y=y1; y<y2-1; y++) {
1258 i_ppix(im, x, y+1, val);
1262 i_ppix(im, x1, y1, val);
1263 i_ppix(im, x2, y2, val);
1265 if (x1 != x2 || y1 != y2)
1266 i_ppix(im, x1, y1, val);
1272 i_line_dda(i_img *im, int x1, int y1, int x2, int y2, i_color *val) {
1277 for(x=x1; x<=x2; x++) {
1278 dy = y1+ (x-x1)/(float)(x2-x1)*(y2-y1);
1279 i_ppix(im, x, (int)(dy+0.5), val);
1284 =item i_line_aa(C<im>, C<x1>, C<x2>, C<y1>, C<y2>, C<color>, C<endp>)
1288 Anti-alias draws a line from (x1,y1) to (x2, y2) in color.
1290 The point (x2, y2) is drawn only if C<endp> is set.
1296 i_line_aa(i_img *im, int x1, int y1, int x2, int y2, const i_color *val, int endp) {
1304 /* choose variable to iterate on */
1305 if (abs(dx)>abs(dy)) {
1311 t = x1; x1 = x2; x2 = t;
1312 t = y1; y1 = y2; y2 = t;
1326 p = dy2 - dx2; /* this has to be like this for AA */
1330 for(x=x1; x<x2-1; x++) {
1333 float t = (dy) ? -(float)(p)/(float)(dx2) : 1;
1340 i_gpix(im,x+1,y,&tval);
1341 for(ch=0;ch<im->channels;ch++)
1342 tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
1343 i_ppix(im,x+1,y,&tval);
1345 i_gpix(im,x+1,y+cpy,&tval);
1346 for(ch=0;ch<im->channels;ch++)
1347 tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
1348 i_ppix(im,x+1,y+cpy,&tval);
1363 t = x1; x1 = x2; x2 = t;
1364 t = y1; y1 = y2; y2 = t;
1378 p = dx2 - dy2; /* this has to be like this for AA */
1382 for(y=y1; y<y2-1; y++) {
1385 float t = (dx) ? -(float)(p)/(float)(dy2) : 1;
1392 i_gpix(im,x,y+1,&tval);
1393 for(ch=0;ch<im->channels;ch++)
1394 tval.channel[ch]=(unsigned char)(t1*(float)tval.channel[ch]+t2*(float)val->channel[ch]);
1395 i_ppix(im,x,y+1,&tval);
1397 i_gpix(im,x+cpx,y+1,&tval);
1398 for(ch=0;ch<im->channels;ch++)
1399 tval.channel[ch]=(unsigned char)(t2*(float)tval.channel[ch]+t1*(float)val->channel[ch]);
1400 i_ppix(im,x+cpx,y+1,&tval);
1413 i_ppix(im, x1, y1, val);
1414 i_ppix(im, x2, y2, val);
1416 if (x1 != x2 || y1 != y2)
1417 i_ppix(im, x1, y1, val);
1428 for(i=k+1;i<=n;i++) r*=i;
1429 for(i=1;i<=(n-k);i++) r/=i;
1434 /* Note in calculating t^k*(1-t)^(n-k)
1435 we can start by using t^0=1 so this simplifies to
1436 t^0*(1-t)^n - we want to multiply that with t/(1-t) each iteration
1437 to get a new level - this may lead to errors who knows lets test it */
1440 i_bezier_multi(i_img *im,int l,const double *x,const double *y, const i_color *val) {
1448 /* this is the same size as the x and y arrays, so shouldn't overflow */
1449 bzcoef=mymalloc(sizeof(double)*l); /* checked 5jul05 tonyc */
1450 for(k=0;k<l;k++) bzcoef[k]=perm(n,k);
1454 /* for(k=0;k<l;k++) printf("bzcoef: %d -> %f\n",k,bzcoef[k]); */
1456 for(t=0;t<=1;t+=0.005) {
1461 /* cx+=bzcoef[k]*x[k]*pow(t,k)*pow(1-t,n-k);
1462 cy+=bzcoef[k]*y[k]*pow(t,k)*pow(1-t,n-k);*/
1464 cx+=bzcoef[k]*x[k]*ccoef;
1465 cy+=bzcoef[k]*y[k]*ccoef;
1468 /* printf("%f -> (%d,%d)\n",t,(int)(0.5+cx),(int)(0.5+cy)); */
1470 i_line_aa(im,lx,ly,(int)(0.5+cx),(int)(0.5+cy),val, 1);
1472 /* i_ppix(im,(int)(0.5+cx),(int)(0.5+cy),val); */
1482 REF: Graphics Gems I. page 282+
1486 /* This should be moved into a seperate file? */
1488 /* This is the truncation used:
1490 a double is multiplied by 16 and then truncated.
1491 This means that 0 -> 0
1492 So a triangle of (0,0) (10,10) (10,0) Will look like it's
1493 not filling the (10,10) point nor the (10,0)-(10,10) line segment
1498 /* Flood fill algorithm - based on the Ken Fishkins (pixar) gem in
1513 struct stack_element {
1521 /* create the link data to put push onto the stack */
1524 struct stack_element*
1525 crdata(int left,int right,int dadl,int dadr,int y, int dir) {
1526 struct stack_element *ste;
1527 ste = mymalloc(sizeof(struct stack_element)); /* checked 5jul05 tonyc */
1533 ste->myDirection = dir;
1537 /* i_ccomp compares two colors and gives true if they are the same */
1539 typedef int (*ff_cmpfunc)(i_color const *c1, i_color const *c2, int channels);
1542 i_ccomp_normal(i_color const *val1, i_color const *val2, int ch) {
1544 for(i = 0; i < ch; i++)
1545 if (val1->channel[i] !=val2->channel[i])
1551 i_ccomp_border(i_color const *val1, i_color const *val2, int ch) {
1553 for(i = 0; i < ch; i++)
1554 if (val1->channel[i] !=val2->channel[i])
1560 i_lspan(i_img *im, int seedx, int seedy, i_color const *val, ff_cmpfunc cmpfunc) {
1563 if (seedx-1 < 0) break;
1564 i_gpix(im,seedx-1,seedy,&cval);
1565 if (!cmpfunc(val,&cval,im->channels))
1573 i_rspan(i_img *im, int seedx, int seedy, i_color const *val, ff_cmpfunc cmpfunc) {
1576 if (seedx+1 > im->xsize-1) break;
1577 i_gpix(im,seedx+1,seedy,&cval);
1578 if (!cmpfunc(val,&cval,im->channels)) break;
1584 /* Macro to create a link and push on to the list */
1586 #define ST_PUSH(left,right,dadl,dadr,y,dir) do { \
1587 struct stack_element *s = crdata(left,right,dadl,dadr,y,dir); \
1588 llist_push(st,&s); \
1591 /* pops the shadow on TOS into local variables lx,rx,y,direction,dadLx and dadRx */
1592 /* No overflow check! */
1594 #define ST_POP() do { \
1595 struct stack_element *s; \
1602 direction = s->myDirection; \
1606 #define ST_STACK(dir,dadLx,dadRx,lx,rx,y) do { \
1607 int pushrx = rx+1; \
1608 int pushlx = lx-1; \
1609 ST_PUSH(lx,rx,pushlx,pushrx,y+dir,dir); \
1611 ST_PUSH(dadRx+1,rx,pushlx,pushrx,y-dir,-dir); \
1612 if (lx < dadLx) ST_PUSH(lx,dadLx-1,pushlx,pushrx,y-dir,-dir); \
1615 #define SET(x,y) btm_set(btm,x,y)
1617 /* INSIDE returns true if pixel is correct color and we haven't set it before. */
1618 #define INSIDE(x,y, seed) ((!btm_test(btm,x,y) && ( i_gpix(im,x,y,&cval),cmpfunc(seed,&cval,channels) ) ))
1622 /* The function that does all the real work */
1624 static struct i_bitmap *
1625 i_flood_fill_low(i_img *im,int seedx,int seedy,
1626 int *bxminp, int *bxmaxp, int *byminp, int *bymaxp,
1627 i_color const *seed, ff_cmpfunc cmpfunc) {
1637 struct i_bitmap *btm;
1639 int channels,xsize,ysize;
1642 channels = im->channels;
1646 btm = btm_new(xsize, ysize);
1647 st = llist_new(100, sizeof(struct stack_element*));
1649 /* Find the starting span and fill it */
1650 ltx = i_lspan(im, seedx, seedy, seed, cmpfunc);
1651 rtx = i_rspan(im, seedx, seedy, seed, cmpfunc);
1652 for(tx=ltx; tx<=rtx; tx++) SET(tx, seedy);
1656 ST_PUSH(ltx, rtx, ltx, rtx, seedy+1, 1);
1657 ST_PUSH(ltx, rtx, ltx, rtx, seedy-1, -1);
1660 /* Stack variables */
1669 ST_POP(); /* sets lx, rx, dadLx, dadRx, y, direction */
1672 if (y<0 || y>ysize-1) continue;
1673 if (bymin > y) bymin=y; /* in the worst case an extra line */
1674 if (bymax < y) bymax=y;
1678 if ( lx >= 0 && (wasIn = INSIDE(lx, y, seed)) ) {
1681 while(lx >= 0 && INSIDE(lx, y, seed)) {
1687 if (bxmin > lx) bxmin = lx;
1688 while(x <= xsize-1) {
1689 /* printf("x=%d\n",x); */
1692 if (INSIDE(x, y, seed)) {
1693 /* case 1: was inside, am still inside */
1696 /* case 2: was inside, am no longer inside: just found the
1697 right edge of a span */
1698 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1700 if (bxmax < x) bxmax = x;
1704 if (x > rx) goto EXT;
1705 if (INSIDE(x, y, seed)) {
1707 /* case 3: Wasn't inside, am now: just found the start of a new run */
1711 /* case 4: Wasn't inside, still isn't */
1716 EXT: /* out of loop */
1718 /* hit an edge of the frame buffer while inside a run */
1719 ST_STACK(direction, dadLx, dadRx, lx, (x-1), y);
1720 if (bxmax < x) bxmax = x;
1735 =item i_flood_fill(C<im>, C<seedx>, C<seedy>, C<color>)
1738 =synopsis i_flood_fill(im, 50, 50, &color);
1740 Flood fills the 4-connected region starting from the point (C<seedx>,
1741 C<seedy>) with I<color>.
1743 Returns false if (C<seedx>, C<seedy>) are outside the image.
1749 i_flood_fill(i_img *im, int seedx, int seedy, const i_color *dcol) {
1750 int bxmin, bxmax, bymin, bymax;
1751 struct i_bitmap *btm;
1756 if (seedx < 0 || seedx >= im->xsize ||
1757 seedy < 0 || seedy >= im->ysize) {
1758 i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
1762 /* Get the reference color */
1763 i_gpix(im, seedx, seedy, &val);
1765 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1766 &val, i_ccomp_normal);
1768 for(y=bymin;y<=bymax;y++)
1769 for(x=bxmin;x<=bxmax;x++)
1770 if (btm_test(btm,x,y))
1771 i_ppix(im,x,y,dcol);
1777 =item i_flood_cfill(C<im>, C<seedx>, C<seedy>, C<fill>)
1780 =synopsis i_flood_cfill(im, 50, 50, fill);
1782 Flood fills the 4-connected region starting from the point (C<seedx>,
1783 C<seedy>) with C<fill>.
1785 Returns false if (C<seedx>, C<seedy>) are outside the image.
1791 i_flood_cfill(i_img *im, int seedx, int seedy, i_fill_t *fill) {
1792 int bxmin, bxmax, bymin, bymax;
1793 struct i_bitmap *btm;
1798 if (seedx < 0 || seedx >= im->xsize ||
1799 seedy < 0 || seedy >= im->ysize) {
1800 i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
1804 /* Get the reference color */
1805 i_gpix(im, seedx, seedy, &val);
1807 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1808 &val, i_ccomp_normal);
1810 cfill_from_btm(im, fill, btm, bxmin, bxmax, bymin, bymax);
1817 =item i_flood_fill_border(C<im>, C<seedx>, C<seedy>, C<color>, C<border>)
1820 =synopsis i_flood_fill_border(im, 50, 50, &color, &border);
1822 Flood fills the 4-connected region starting from the point (C<seedx>,
1823 C<seedy>) with C<color>, fill stops when the fill reaches a pixels
1824 with color C<border>.
1826 Returns false if (C<seedx>, C<seedy>) are outside the image.
1832 i_flood_fill_border(i_img *im, int seedx, int seedy, const i_color *dcol,
1833 const i_color *border) {
1834 int bxmin, bxmax, bymin, bymax;
1835 struct i_bitmap *btm;
1839 if (seedx < 0 || seedx >= im->xsize ||
1840 seedy < 0 || seedy >= im->ysize) {
1841 i_push_error(0, "i_flood_cfill: Seed pixel outside of image");
1845 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1846 border, i_ccomp_border);
1848 for(y=bymin;y<=bymax;y++)
1849 for(x=bxmin;x<=bxmax;x++)
1850 if (btm_test(btm,x,y))
1851 i_ppix(im,x,y,dcol);
1857 =item i_flood_cfill_border(C<im>, C<seedx>, C<seedy>, C<fill>, C<border>)
1860 =synopsis i_flood_cfill_border(im, 50, 50, fill, border);
1862 Flood fills the 4-connected region starting from the point (C<seedx>,
1863 C<seedy>) with C<fill>, the fill stops when it reaches pixels of color
1866 Returns false if (C<seedx>, C<seedy>) are outside the image.
1872 i_flood_cfill_border(i_img *im, int seedx, int seedy, i_fill_t *fill,
1873 const i_color *border) {
1874 int bxmin, bxmax, bymin, bymax;
1875 struct i_bitmap *btm;
1879 if (seedx < 0 || seedx >= im->xsize ||
1880 seedy < 0 || seedy >= im->ysize) {
1881 i_push_error(0, "i_flood_cfill_border: Seed pixel outside of image");
1885 btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax,
1886 border, i_ccomp_border);
1888 cfill_from_btm(im, fill, btm, bxmin, bxmax, bymin, bymax);
1896 cfill_from_btm(i_img *im, i_fill_t *fill, struct i_bitmap *btm,
1897 int bxmin, int bxmax, int bymin, int bymax) {
1903 i_render_init(&r, im, bxmax - bxmin + 1);
1905 for(y=bymin; y<=bymax; y++) {
1907 while (x <= bxmax) {
1908 while (x <= bxmax && !btm_test(btm, x, y)) {
1911 if (btm_test(btm, x, y)) {
1913 while (x <= bxmax && btm_test(btm, x, y)) {
1916 i_render_fill(&r, start, y, x-start, NULL, fill);