+ if (minx > tmin) minx = tmin;
+ if (maxx < tmax) maxx = tmax;
+ }
+
+ if (maxx == INT_MIN) continue; /* no work to be done for this row of pixels */
+
+ minx /= 16;
+ maxx /= 16;
+ for(ix=minx; ix<=maxx; ix++) {
+ int cnt = i_pixel_coverage(&dot, ix, ly);
+ if (cnt>255) cnt = 255;
+ if (cnt) { /* should never be true */
+ int ch;
+ float ratio = (float)cnt/255.0;
+ i_gpix(im, ix, ly, &temp);
+ for(ch=0;ch<im->channels; ch++) temp.channel[ch] = (unsigned char)((float)val->channel[ch]*ratio + (float)temp.channel[ch]*(1.0-ratio));
+ i_ppix(im, ix, ly, &temp);
+ }
+ }
+ }
+ i_mmarray_dst(&dot);
+}
+
+/*
+=item i_circle_out(im, x, y, r, col)
+
+=category Drawing
+=synopsis i_circle_out(im, 50, 50, 45, &color);
+
+Draw a circle outline centered at (x,y) with radius r,
+non-anti-aliased.
+
+Parameters:
+
+=over
+
+=item *
+
+(x, y) - the center of the circle
+
+=item *
+
+r - the radius of the circle in pixels, must be non-negative
+
+=back
+
+Returns non-zero on success.
+
+Implementation:
+
+=cut
+*/
+
+int
+i_circle_out(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r,
+ const i_color *col) {
+ i_img_dim x, y;
+ i_img_dim dx, dy;
+ int error;
+ dIMCTXim(im);
+
+ im_log((aIMCTX, 1, "i_circle_out(im %p, centre(" i_DFp "), rad %" i_DF ", col %p)\n",
+ im, i_DFcp(xc, yc), i_DFc(r), col));
+
+ im_clear_error(aIMCTX);
+
+ if (r < 0) {
+ im_push_error(aIMCTX, 0, "circle: radius must be non-negative");
+ return 0;
+ }
+
+ i_ppix(im, xc+r, yc, col);
+ i_ppix(im, xc-r, yc, col);
+ i_ppix(im, xc, yc+r, col);
+ i_ppix(im, xc, yc-r, col);
+
+ x = 0;
+ y = r;
+ dx = 1;
+ dy = -2 * r;
+ error = 1 - r;
+ while (x < y) {
+ if (error >= 0) {
+ --y;
+ dy += 2;
+ error += dy;
+ }
+ ++x;
+ dx += 2;
+ error += dx;
+
+ i_ppix(im, xc + x, yc + y, col);
+ i_ppix(im, xc + x, yc - y, col);
+ i_ppix(im, xc - x, yc + y, col);
+ i_ppix(im, xc - x, yc - y, col);
+ if (x != y) {
+ i_ppix(im, xc + y, yc + x, col);
+ i_ppix(im, xc + y, yc - x, col);
+ i_ppix(im, xc - y, yc + x, col);
+ i_ppix(im, xc - y, yc - x, col);
+ }
+ }
+
+ return 1;
+}
+
+/*
+=item arc_seg(angle)
+
+Convert an angle in degrees into an angle measure we can generate
+simply from the numbers we have when drawing the circle.
+
+=cut
+*/
+
+static i_img_dim
+arc_seg(double angle, int scale) {
+ i_img_dim seg = (angle + 45) / 90;
+ double remains = angle - seg * 90; /* should be in the range [-45,45] */
+
+ while (seg > 4)
+ seg -= 4;
+ if (seg == 4 && remains > 0)
+ seg = 0;
+
+ return scale * (seg * 2 + sin(remains * PI/180));
+}
+
+/*
+=item i_arc_out(im, x, y, r, d1, d2, col)
+
+=category Drawing
+=synopsis i_arc_out(im, 50, 50, 45, 45, 135, &color);
+
+Draw an arc outline centered at (x,y) with radius r, non-anti-aliased
+over the angle range d1 through d2 degrees.
+
+Parameters:
+
+=over
+
+=item *
+
+(x, y) - the center of the circle
+
+=item *
+
+r - the radius of the circle in pixels, must be non-negative
+
+=item *
+
+d1, d2 - the range of angles to draw the arc over, in degrees.
+
+=back
+
+Returns non-zero on success.
+
+Implementation:
+
+=cut
+*/
+
+int
+i_arc_out(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r,
+ double d1, double d2, const i_color *col) {
+ i_img_dim x, y;
+ i_img_dim dx, dy;
+ int error;
+ i_img_dim segs[2][2];
+ int seg_count;
+ i_img_dim sin_th;
+ i_img_dim seg_d1, seg_d2;
+ int seg_num;
+ i_img_dim scale = r + 1;
+ i_img_dim seg1 = scale * 2;
+ i_img_dim seg2 = scale * 4;
+ i_img_dim seg3 = scale * 6;
+ i_img_dim seg4 = scale * 8;
+ dIMCTXim(im);
+
+ im_log((aIMCTX,1,"i_arc_out(im %p,centre(" i_DFp "), rad %" i_DF ", d1 %f, d2 %f, col %p)",
+ im, i_DFcp(xc, yc), i_DFc(r), d1, d2, col));
+
+ im_clear_error(aIMCTX);
+
+ if (r <= 0) {
+ im_push_error(aIMCTX, 0, "arc: radius must be non-negative");
+ return 0;
+ }
+ if (d1 + 360 <= d2)
+ return i_circle_out(im, xc, yc, r, col);
+
+ if (d1 < 0)
+ d1 += 360 * floor((-d1 + 359) / 360);
+ if (d2 < 0)
+ d2 += 360 * floor((-d2 + 359) / 360);
+ d1 = fmod(d1, 360);
+ d2 = fmod(d2, 360);
+ seg_d1 = arc_seg(d1, scale);
+ seg_d2 = arc_seg(d2, scale);
+ if (seg_d2 < seg_d1) {
+ /* split into two segments */
+ segs[0][0] = 0;
+ segs[0][1] = seg_d2;
+ segs[1][0] = seg_d1;
+ segs[1][1] = seg4;
+ seg_count = 2;
+ }
+ else {
+ segs[0][0] = seg_d1;
+ segs[0][1] = seg_d2;
+ seg_count = 1;
+ }
+
+ for (seg_num = 0; seg_num < seg_count; ++seg_num) {
+ i_img_dim seg_start = segs[seg_num][0];
+ i_img_dim seg_end = segs[seg_num][1];
+ if (seg_start == 0)
+ i_ppix(im, xc+r, yc, col);
+ if (seg_start <= seg1 && seg_end >= seg1)
+ i_ppix(im, xc, yc+r, col);
+ if (seg_start <= seg2 && seg_end >= seg2)
+ i_ppix(im, xc-r, yc, col);
+ if (seg_start <= seg3 && seg_end >= seg3)
+ i_ppix(im, xc, yc-r, col);
+
+ y = 0;
+ x = r;
+ dy = 1;
+ dx = -2 * r;
+ error = 1 - r;
+ while (y < x) {
+ if (error >= 0) {
+ --x;
+ dx += 2;
+ error += dx;
+ }
+ ++y;
+ dy += 2;
+ error += dy;
+
+ sin_th = y;
+ if (seg_start <= sin_th && seg_end >= sin_th)
+ i_ppix(im, xc + x, yc + y, col);
+ if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
+ i_ppix(im, xc + y, yc + x, col);
+
+ if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
+ i_ppix(im, xc - y, yc + x, col);
+ if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
+ i_ppix(im, xc - x, yc + y, col);
+
+ if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
+ i_ppix(im, xc - x, yc - y, col);
+ if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
+ i_ppix(im, xc - y, yc - x, col);
+
+ if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
+ i_ppix(im, xc + y, yc - x, col);
+ if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
+ i_ppix(im, xc + x, yc - y, col);
+ }
+ }
+
+ return 1;
+}
+
+static double
+cover(i_img_dim r, i_img_dim j) {
+ double rjsqrt = sqrt(r*r - j*j);
+
+ return ceil(rjsqrt) - rjsqrt;
+}
+
+/*
+=item i_circle_out_aa(im, xc, yc, r, col)
+
+=synopsis i_circle_out_aa(im, 50, 50, 45, &color);
+
+Draw a circle outline centered at (x,y) with radius r, anti-aliased.
+
+Parameters:
+
+=over
+
+=item *
+
+(xc, yc) - the center of the circle
+
+=item *
+
+r - the radius of the circle in pixels, must be non-negative
+
+=item *
+
+col - an i_color for the color to draw in.
+
+=back
+
+Returns non-zero on success.
+
+=cut
+
+Based on "Fast Anti-Aliased Circle Generation", Xiaolin Wu, Graphics
+Gems.
+
+I use floating point for I<D> since for large circles the precision of
+a [0,255] value isn't sufficient when approaching the end of the
+octant.
+
+*/
+
+int
+i_circle_out_aa(i_img *im, i_img_dim xc, i_img_dim yc, i_img_dim r, const i_color *col) {
+ i_img_dim i, j;
+ double t;
+ i_color workc = *col;
+ int orig_alpha = col->channel[3];
+ dIMCTXim(im);
+
+ im_log((aIMCTX,1,"i_circle_out_aa(im %p,centre(" i_DFp "), rad %" i_DF ", col %p)",
+ im, i_DFcp(xc, yc), i_DFc(r), col));
+
+ im_clear_error(aIMCTX);
+ if (r <= 0) {
+ im_push_error(aIMCTX, 0, "arc: radius must be non-negative");
+ return 0;
+ }
+ i = r;
+ j = 0;
+ t = 0;
+ i_ppix_norm(im, xc+i, yc+j, col);
+ i_ppix_norm(im, xc-i, yc+j, col);
+ i_ppix_norm(im, xc+j, yc+i, col);
+ i_ppix_norm(im, xc+j, yc-i, col);
+
+ while (i > j+1) {
+ double d;
+ int cv, inv_cv;
+ j++;
+ d = cover(r, j);
+ cv = (int)(d * 255 + 0.5);
+ inv_cv = 255-cv;
+ if (d < t) {
+ --i;
+ }
+ if (inv_cv) {
+ workc.channel[3] = orig_alpha * inv_cv / 255;
+ i_ppix_norm(im, xc+i, yc+j, &workc);
+ i_ppix_norm(im, xc-i, yc+j, &workc);
+ i_ppix_norm(im, xc+i, yc-j, &workc);
+ i_ppix_norm(im, xc-i, yc-j, &workc);
+
+ if (i != j) {
+ i_ppix_norm(im, xc+j, yc+i, &workc);
+ i_ppix_norm(im, xc-j, yc+i, &workc);
+ i_ppix_norm(im, xc+j, yc-i, &workc);
+ i_ppix_norm(im, xc-j, yc-i, &workc);
+ }
+ }
+ if (cv && i > j) {
+ workc.channel[3] = orig_alpha * cv / 255;
+ i_ppix_norm(im, xc+i-1, yc+j, &workc);
+ i_ppix_norm(im, xc-i+1, yc+j, &workc);
+ i_ppix_norm(im, xc+i-1, yc-j, &workc);
+ i_ppix_norm(im, xc-i+1, yc-j, &workc);
+
+ if (j != i-1) {
+ i_ppix_norm(im, xc+j, yc+i-1, &workc);
+ i_ppix_norm(im, xc-j, yc+i-1, &workc);
+ i_ppix_norm(im, xc+j, yc-i+1, &workc);
+ i_ppix_norm(im, xc-j, yc-i+1, &workc);
+ }
+ }
+ t = d;
+ }
+
+ return 1;
+}
+
+/*
+=item i_arc_out_aa(im, xc, yc, r, d1, d2, col)
+
+=synopsis i_arc_out_aa(im, 50, 50, 45, 45, 125, &color);
+
+Draw a circle arc outline centered at (x,y) with radius r, from angle
+d1 degrees through angle d2 degrees, anti-aliased.
+
+Parameters:
+
+=over
+
+=item *
+
+(xc, yc) - the center of the circle
+
+=item *
+
+r - the radius of the circle in pixels, must be non-negative
+
+=item *
+
+d1, d2 - the range of angle in degrees to draw the arc through. If
+d2-d1 >= 360 a full circle is drawn.
+
+=back
+
+Returns non-zero on success.
+
+=cut
+
+Based on "Fast Anti-Aliased Circle Generation", Xiaolin Wu, Graphics
+Gems.
+
+*/
+
+int
+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) {
+ i_img_dim i, j;
+ double t;
+ i_color workc = *col;
+ i_img_dim segs[2][2];
+ int seg_count;
+ i_img_dim sin_th;
+ i_img_dim seg_d1, seg_d2;
+ int seg_num;
+ int orig_alpha = col->channel[3];
+ i_img_dim scale = r + 1;
+ i_img_dim seg1 = scale * 2;
+ i_img_dim seg2 = scale * 4;
+ i_img_dim seg3 = scale * 6;
+ i_img_dim seg4 = scale * 8;
+ dIMCTXim(im);
+
+ im_log((aIMCTX,1,"i_arc_out_aa(im %p,centre(" i_DFp "), rad %" i_DF ", d1 %f, d2 %f, col %p)",
+ im, i_DFcp(xc, yc), i_DFc(r), d1, d2, col));
+
+ im_clear_error(aIMCTX);
+ if (r <= 0) {
+ im_push_error(aIMCTX, 0, "arc: radius must be non-negative");
+ return 0;
+ }
+ if (d1 + 360 <= d2)
+ return i_circle_out_aa(im, xc, yc, r, col);
+
+ if (d1 < 0)
+ d1 += 360 * floor((-d1 + 359) / 360);
+ if (d2 < 0)
+ d2 += 360 * floor((-d2 + 359) / 360);
+ d1 = fmod(d1, 360);
+ d2 = fmod(d2, 360);
+ seg_d1 = arc_seg(d1, scale);
+ seg_d2 = arc_seg(d2, scale);
+ if (seg_d2 < seg_d1) {
+ /* split into two segments */
+ segs[0][0] = 0;
+ segs[0][1] = seg_d2;
+ segs[1][0] = seg_d1;
+ segs[1][1] = seg4;
+ seg_count = 2;
+ }
+ else {
+ segs[0][0] = seg_d1;
+ segs[0][1] = seg_d2;
+ seg_count = 1;
+ }
+
+ for (seg_num = 0; seg_num < seg_count; ++seg_num) {
+ i_img_dim seg_start = segs[seg_num][0];
+ i_img_dim seg_end = segs[seg_num][1];
+
+ i = r;
+ j = 0;
+ t = 0;
+
+ if (seg_start == 0)
+ i_ppix_norm(im, xc+i, yc+j, col);
+ if (seg_start <= seg1 && seg_end >= seg1)
+ i_ppix_norm(im, xc+j, yc+i, col);
+ if (seg_start <= seg2 && seg_end >= seg2)
+ i_ppix_norm(im, xc-i, yc+j, col);
+ if (seg_start <= seg3 && seg_end >= seg3)
+ i_ppix_norm(im, xc+j, yc-i, col);
+
+ while (i > j+1) {
+ int cv, inv_cv;
+ double d;
+ j++;
+ d = cover(r, j);
+ cv = (int)(d * 255 + 0.5);
+ inv_cv = 255-cv;
+ if (d < t) {
+ --i;
+ }
+ sin_th = j;
+ if (inv_cv) {
+ workc.channel[3] = orig_alpha * inv_cv / 255;
+
+ if (seg_start <= sin_th && seg_end >= sin_th)
+ i_ppix_norm(im, xc+i, yc+j, &workc);
+ if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
+ i_ppix_norm(im, xc-i, yc+j, &workc);
+ if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
+ i_ppix_norm(im, xc+i, yc-j, &workc);
+ if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
+ i_ppix_norm(im, xc-i, yc-j, &workc);
+
+ if (i != j) {
+ if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
+ i_ppix_norm(im, xc+j, yc+i, &workc);
+ if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
+ i_ppix_norm(im, xc-j, yc+i, &workc);
+ if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
+ i_ppix_norm(im, xc+j, yc-i, &workc);
+ if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
+ i_ppix_norm(im, xc-j, yc-i, &workc);
+ }
+ }
+ if (cv && i > j) {
+ workc.channel[3] = orig_alpha * cv / 255;
+ if (seg_start <= sin_th && seg_end >= sin_th)
+ i_ppix_norm(im, xc+i-1, yc+j, &workc);
+ if (seg_start <= seg2 - sin_th && seg_end >= seg2 - sin_th)
+ i_ppix_norm(im, xc-i+1, yc+j, &workc);
+ if (seg_start <= seg4 - sin_th && seg_end >= seg4 - sin_th)
+ i_ppix_norm(im, xc+i-1, yc-j, &workc);
+ if (seg_start <= seg2 + sin_th && seg_end >= seg2 + sin_th)
+ i_ppix_norm(im, xc-i+1, yc-j, &workc);
+
+ if (seg_start <= seg1 - sin_th && seg_end >= seg1 - sin_th)
+ i_ppix_norm(im, xc+j, yc+i-1, &workc);
+ if (seg_start <= seg1 + sin_th && seg_end >= seg1 + sin_th)
+ i_ppix_norm(im, xc-j, yc+i-1, &workc);
+ if (seg_start <= seg3 + sin_th && seg_end >= seg3 + sin_th)
+ i_ppix_norm(im, xc+j, yc-i+1, &workc);
+ if (seg_start <= seg3 - sin_th && seg_end >= seg3 - sin_th)
+ i_ppix_norm(im, xc-j, yc-i+1, &workc);
+ }
+ t = d;
+ }
+ }
+
+ return 1;
+}
+
+/*
+=item i_box(im, x1, y1, x2, y2, color)
+
+=category Drawing
+=synopsis i_box(im, 0, 0, im->xsize-1, im->ysize-1, &color).
+
+Outlines the box from (x1,y1) to (x2,y2) inclusive with I<color>.
+
+=cut
+*/
+
+void
+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) {
+ i_img_dim x,y;
+ dIMCTXim(im);
+
+ im_log((aIMCTX, 1,"i_box(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
+ im, i_DFcp(x1,y1), i_DFcp(x2,y2), val));
+ for(x=x1;x<x2+1;x++) {
+ i_ppix(im,x,y1,val);
+ i_ppix(im,x,y2,val);
+ }
+ for(y=y1;y<y2+1;y++) {
+ i_ppix(im,x1,y,val);
+ i_ppix(im,x2,y,val);
+ }
+}
+
+/*
+=item i_box_filled(im, x1, y1, x2, y2, color)
+
+=category Drawing
+=synopsis i_box_filled(im, 0, 0, im->xsize-1, im->ysize-1, &color);
+
+Fills the box from (x1,y1) to (x2,y2) inclusive with color.
+
+=cut
+*/
+
+void
+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) {
+ i_img_dim x, y, width;
+ i_palidx index;
+ dIMCTXim(im);
+
+ im_log((aIMCTX,1,"i_box_filled(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
+ im, i_DFcp(x1, y1), i_DFcp(x2,y2) ,val));
+
+ if (x1 > x2 || y1 > y2
+ || x2 < 0 || y2 < 0
+ || x1 >= im->xsize || y1 > im->ysize)
+ return;
+
+ if (x1 < 0)
+ x1 = 0;
+ if (x2 >= im->xsize)
+ x2 = im->xsize - 1;
+ if (y1 < 0)
+ y1 = 0;
+ if (y2 >= im->ysize)
+ y2 = im->ysize - 1;
+
+ width = x2 - x1 + 1;
+
+ if (im->type == i_palette_type
+ && i_findcolor(im, val, &index)) {
+ i_palidx *line = mymalloc(sizeof(i_palidx) * width);
+
+ for (x = 0; x < width; ++x)
+ line[x] = index;
+
+ for (y = y1; y <= y2; ++y)
+ i_ppal(im, x1, x2+1, y, line);
+
+ myfree(line);
+ }
+ else {
+ i_color *line = mymalloc(sizeof(i_color) * width);
+
+ for (x = 0; x < width; ++x)
+ line[x] = *val;
+
+ for (y = y1; y <= y2; ++y)
+ i_plin(im, x1, x2+1, y, line);
+
+ myfree(line);
+ }
+}
+
+/*
+=item i_box_filledf(im, x1, y1, x2, y2, color)
+
+=category Drawing
+=synopsis i_box_filledf(im, 0, 0, im->xsize-1, im->ysize-1, &fcolor);
+
+Fills the box from (x1,y1) to (x2,y2) inclusive with a floating point
+color.
+
+=cut
+*/
+
+int
+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) {
+ i_img_dim x, y, width;
+ dIMCTXim(im);
+
+ im_log((aIMCTX, 1,"i_box_filledf(im* %p, p1(" i_DFp "), p2(" i_DFp "),val %p)\n",
+ im, i_DFcp(x1, y1), i_DFcp(x2, y2), val));
+
+ if (x1 > x2 || y1 > y2
+ || x2 < 0 || y2 < 0
+ || x1 >= im->xsize || y1 > im->ysize)
+ return 0;
+
+ if (x1 < 0)
+ x1 = 0;
+ if (x2 >= im->xsize)
+ x2 = im->xsize - 1;
+ if (y1 < 0)
+ y1 = 0;
+ if (y2 >= im->ysize)
+ y2 = im->ysize - 1;
+
+ width = x2 - x1 + 1;
+
+ if (im->bits <= 8) {
+ i_color c;
+ c.rgba.r = SampleFTo8(val->rgba.r);
+ c.rgba.g = SampleFTo8(val->rgba.g);
+ c.rgba.b = SampleFTo8(val->rgba.b);
+ c.rgba.a = SampleFTo8(val->rgba.a);
+
+ i_box_filled(im, x1, y1, x2, y2, &c);
+ }
+ else {
+ i_fcolor *line = mymalloc(sizeof(i_fcolor) * width);
+
+ for (x = 0; x < width; ++x)
+ line[x] = *val;
+
+ for (y = y1; y <= y2; ++y)
+ i_plinf(im, x1, x2+1, y, line);
+
+ myfree(line);
+ }
+
+ return 1;
+}
+
+/*
+=item i_box_cfill(im, x1, y1, x2, y2, fill)
+
+=category Drawing
+=synopsis i_box_cfill(im, 0, 0, im->xsize-1, im->ysize-1, fill);
+
+Fills the box from (x1,y1) to (x2,y2) inclusive with fill.
+
+=cut
+*/
+
+void
+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) {
+ i_render r;
+ dIMCTXim(im);
+
+ im_log((aIMCTX,1,"i_box_cfill(im* %p, p1(" i_DFp "), p2(" i_DFp "), fill %p)\n",
+ im, i_DFcp(x1, y1), i_DFcp(x2,y2), fill));
+
+ ++x2;
+ if (x1 < 0)
+ x1 = 0;
+ if (y1 < 0)
+ y1 = 0;
+ if (x2 > im->xsize)
+ x2 = im->xsize;
+ if (y2 >= im->ysize)
+ y2 = im->ysize-1;
+ if (x1 >= x2 || y1 > y2)
+ return;
+
+ i_render_init(&r, im, x2-x1);
+ while (y1 <= y2) {
+ i_render_fill(&r, x1, y1, x2-x1, NULL, fill);
+ ++y1;
+ }
+ i_render_done(&r);
+}
+
+/*
+=item i_line(C<im>, C<x1>, C<y1>, C<x2>, C<y2>, C<color>, C<endp>)
+
+=category Drawing
+
+=for stopwords Bresenham's
+
+Draw a line to image using Bresenham's line drawing algorithm
+
+ im - image to draw to
+ x1 - starting x coordinate
+ y1 - starting x coordinate
+ x2 - starting x coordinate
+ y2 - starting x coordinate
+ color - color to write to image
+ endp - endpoint flag (boolean)
+
+=cut
+*/
+
+void
+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) {
+ i_img_dim x, y;
+ i_img_dim dx, dy;
+ i_img_dim p;
+
+ dx = x2 - x1;
+ dy = y2 - y1;
+
+
+ /* choose variable to iterate on */
+ if (i_abs(dx) > i_abs(dy)) {
+ i_img_dim dx2, dy2, cpy;
+
+ /* sort by x */
+ if (x1 > x2) {
+ i_img_dim t;
+ t = x1; x1 = x2; x2 = t;
+ t = y1; y1 = y2; y2 = t;
+ }
+
+ dx = i_abs(dx);
+ dx2 = dx*2;
+ dy = y2 - y1;
+
+ if (dy<0) {
+ dy = -dy;
+ cpy = -1;
+ } else {
+ cpy = 1;
+ }
+ dy2 = dy*2;
+ p = dy2 - dx;
+
+
+ y = y1;
+ for(x=x1; x<x2-1; x++) {
+ if (p<0) {
+ p += dy2;
+ } else {
+ y += cpy;
+ p += dy2-dx2;
+ }
+ i_ppix(im, x+1, y, val);
+ }
+ } else {
+ i_img_dim dy2, dx2, cpx;
+
+ /* sort bx y */
+ if (y1 > y2) {
+ i_img_dim t;
+ t = x1; x1 = x2; x2 = t;
+ t = y1; y1 = y2; y2 = t;
+ }
+
+ dy = i_abs(dy);
+ dx = x2 - x1;
+ dy2 = dy*2;
+
+ if (dx<0) {
+ dx = -dx;
+ cpx = -1;
+ } else {
+ cpx = 1;
+ }
+ dx2 = dx*2;
+ p = dx2 - dy;
+
+ x = x1;
+
+ for(y=y1; y<y2-1; y++) {
+ if (p<0) {
+ p += dx2;
+ } else {
+ x += cpx;
+ p += dx2-dy2;
+ }
+ i_ppix(im, x, y+1, val);
+ }
+ }
+ if (endp) {
+ i_ppix(im, x1, y1, val);
+ i_ppix(im, x2, y2, val);
+ } else {
+ if (x1 != x2 || y1 != y2)
+ i_ppix(im, x1, y1, val);
+ }
+}
+