static
int
lmin(int a, int b)
{
if(a < b)
return a;
return b;
}
static
int
lmax(int a, int b)
{
if(a > b)
return a;
return b;
}
#ifdef NOTUSED
/*
* Rather than line clip, we run the Bresenham loop over the full line,
* and clip on each pixel. This is more expensive but means that
* lines look the same regardless of how the windowing has tiled them.
* For speed, we check for clipping outside the loop and make the
* test easy when possible.
*/
static
void
horline1(Memimage *dst, Point p0, Point p1, int srcval, Rectangle clipr)
{
int x, y, dy, deltay, deltax, maxx;
int dd, easy, e, bpp, m, m0;
uchar *d;
deltax = p1.x - p0.x;
deltay = p1.y - p0.y;
dd = dst->width*sizeof(ulong);
dy = 1;
if(deltay < 0){
dd = -dd;
deltay = -deltay;
dy = -1;
}
maxx = lmin(p1.x, clipr.max.x-1);
bpp = dst->depth;
m0 = 0xFF^(0xFF>>bpp);
m = m0 >> (p0.x&(7/dst->depth))*bpp;
easy = ptinrect(p0, clipr) && ptinrect(p1, clipr);
e = 2*deltay - deltax;
y = p0.y;
d = byteaddr(dst, p0);
deltay *= 2;
deltax = deltay - 2*deltax;
for(x=p0.x; x<=maxx; x++){
if(easy || (clipr.min.x<=x && clipr.min.y<=y && y<clipr.max.y))
*d ^= (*d^srcval) & m;
if(e > 0){
y += dy;
d += dd;
e += deltax;
}else
e += deltay;
d++;
m >>= bpp;
if(m == 0)
m = m0;
}
}
static
void
verline1(Memimage *dst, Point p0, Point p1, int srcval, Rectangle clipr)
{
int x, y, deltay, deltax, maxy;
int easy, e, bpp, m, m0, dd;
uchar *d;
deltax = p1.x - p0.x;
deltay = p1.y - p0.y;
dd = 1;
if(deltax < 0){
dd = -1;
deltax = -deltax;
}
maxy = lmin(p1.y, clipr.max.y-1);
bpp = dst->depth;
m0 = 0xFF^(0xFF>>bpp);
m = m0 >> (p0.x&(7/dst->depth))*bpp;
easy = ptinrect(p0, clipr) && ptinrect(p1, clipr);
e = 2*deltax - deltay;
x = p0.x;
d = byteaddr(dst, p0);
deltax *= 2;
deltay = deltax - 2*deltay;
for(y=p0.y; y<=maxy; y++){
if(easy || (clipr.min.y<=y && clipr.min.x<=x && x<clipr.max.x))
*d ^= (*d^srcval) & m;
if(e > 0){
x += dd;
d += dd;
e += deltay;
}else
e += deltax;
d += dst->width*sizeof(ulong);
m >>= bpp;
if(m == 0)
m = m0;
}
}
static
void
horliner(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr)
{
int x, y, sx, sy, deltay, deltax, minx, maxx;
int bpp, m, m0;
uchar *d, *s;
static
void
verliner(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr)
{
int x, y, sx, sy, deltay, deltax, miny, maxy;
int bpp, m, m0;
uchar *d, *s;
deltax = p1.x - p0.x;
deltay = p1.y - p0.y;
sy = drawreplxy(src->r.min.y, src->r.max.y, p0.y+dsrc.y);
miny = lmax(p0.y, clipr.min.y);
maxy = lmin(p1.y, clipr.max.y-1);
bpp = dst->depth;
m0 = 0xFF^(0xFF>>bpp);
for(y=miny; y<=maxy; y++){
if(deltay == 0) /* degenerate line */
x = p0.x;
else
x = p0.x + (deltax*(y-p0.y)+deltay/2)/deltay;
if(clipr.min.x<=x && x<clipr.max.x){
m = m0 >> (x&(7/dst->depth))*bpp;
d = byteaddr(dst, Pt(x, y));
sx = drawreplxy(src->r.min.x, src->r.max.x, x+dsrc.x);
s = byteaddr(src, Pt(sx, sy));
*d ^= (*d^*s) & m;
}
if(++sy >= src->r.max.y)
sy = src->r.min.y;
}
}
static
void
horline(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr)
{
int x, y, deltay, deltax, minx, maxx;
int bpp, m, m0;
uchar *d, *s;
deltax = p1.x - p0.x;
deltay = p1.y - p0.y;
minx = lmax(p0.x, clipr.min.x);
maxx = lmin(p1.x, clipr.max.x-1);
bpp = dst->depth;
m0 = 0xFF^(0xFF>>bpp);
m = m0 >> (minx&(7/dst->depth))*bpp;
for(x=minx; x<=maxx; x++){
y = p0.y + (deltay*(x-p0.x)+deltay/2)/deltax;
if(clipr.min.y<=y && y<clipr.max.y){
d = byteaddr(dst, Pt(x, y));
s = byteaddr(src, addpt(dsrc, Pt(x, y)));
*d ^= (*d^*s) & m;
}
m >>= bpp;
if(m == 0)
m = m0;
}
}
static
void
verline(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr)
{
int x, y, deltay, deltax, miny, maxy;
int bpp, m, m0;
uchar *d, *s;
static
void
arrowend(Point tip, Point *pp, int end, int sin, int cos, int radius)
{
int x1, x2, x3;
/* before rotation */
if(end == Endarrow){
x1 = Arrow1;
x2 = Arrow2;
x3 = Arrow3;
}else{
x1 = (end>>5) & 0x1FF; /* distance along line from end of line to tip */
x2 = (end>>14) & 0x1FF; /* distance along line from barb to tip */
x3 = (end>>23) & 0x1FF; /* distance perpendicular from edge of line to barb */
}
void
_memimageline(Memimage *dst, Point p0, Point p1, int end0, int end1, int radius, Memimage *src, Point sp, Rectangle clipr, int op)
{
/*
* BUG: We should really really pick off purely horizontal and purely
* vertical lines and handle them separately with calls to memimagedraw
* on rectangles.
*/
int hor;
int sin, cos, dx, dy, t;
Rectangle oclipr, r;
Point q, pts[10], *pp, d;
if(radius < 0)
return;
if(rectclip(&clipr, dst->r) == 0)
return;
if(rectclip(&clipr, dst->clipr) == 0)
return;
d = subpt(sp, p0);
if(rectclip(&clipr, rectsubpt(src->clipr, d)) == 0)
return;
if((src->flags&Frepl)==0 && rectclip(&clipr, rectsubpt(src->r, d))==0)
return;
/* this means that only verline() handles degenerate lines (p0==p1) */
hor = (abs(p1.x-p0.x) > abs(p1.y-p0.y));
/*
* Clipping is a little peculiar. We can't use Sutherland-Cohen
* clipping because lines are wide. But this is probably just fine:
* we do all math with the original p0 and p1, but clip when deciding
* what pixels to draw. This means the layer code can call this routine,
* using clipr to define the region being written, and get the same set
* of pixels regardless of the dicing.
*/
if((hor && p0.x>p1.x) || (!hor && p0.y>p1.y)){
q = p0;
p0 = p1;
p1 = q;
t = end0;
end0 = end1;
end1 = t;
}
void
memimageline(Memimage *dst, Point p0, Point p1, int end0, int end1, int radius, Memimage *src, Point sp, int op)
{
_memimageline(dst, p0, p1, end0, end1, radius, src, sp, dst->clipr, op);
}
/*
* Simple-minded conservative code to compute bounding box of line.
* Result is probably a little larger than it needs to be.
*/
static
void
addbbox(Rectangle *r, Point p)
{
if(r->min.x > p.x)
r->min.x = p.x;
if(r->min.y > p.y)
r->min.y = p.y;
if(r->max.x < p.x+1)
r->max.x = p.x+1;
if(r->max.y < p.y+1)
r->max.y = p.y+1;
}