1 #include "../lib9.h" 2 3 #include "../libdraw/draw.h" 4 #include "../libmemdraw/memdraw.h" 5 #include "../libmemlayer/memlayer.h" 6 7 enum 8 { 9 Arrow1 = 8, 10 Arrow2 = 10, 11 Arrow3 = 3 12 }; 13 14 /* 15 * not used 16 static 17 int 18 lmin(int a, int b) 19 { 20 if(a < b) 21 return a; 22 return b; 23 } 24 */ 25 26 static 27 int 28 lmax(int a, int b) 29 { 30 if(a > b) 31 return a; 32 return b; 33 } 34 35 /* 36 * Rather than line clip, we run the Bresenham loop over the full line, 37 * and clip on each pixel. This is more expensive but means that 38 * lines look the same regardless of how the windowing has tiled them. 39 * For speed, we check for clipping outside the loop and make the 40 * test easy when possible. 41 */ 42 43 #ifdef NOTUSED 44 static 45 void 46 horline1(Memimage *dst, Point p0, Point p1, int srcval, Rectangle clipr) 47 { 48 int x, y, dy, deltay, deltax, maxx; 49 int dd, easy, e, bpp, m, m0; 50 uchar *d; 51 52 deltax = p1.x - p0.x; 53 deltay = p1.y - p0.y; 54 dd = dst->width*sizeof(ulong); 55 dy = 1; 56 if(deltay < 0){ 57 dd = -dd; 58 deltay = -deltay; 59 dy = -1; 60 } 61 maxx = lmin(p1.x, clipr.max.x-1); 62 bpp = dst->depth; 63 m0 = 0xFF^(0xFF>>bpp); 64 m = m0 >> (p0.x&(7/dst->depth))*bpp; 65 easy = ptinrect(p0, clipr) && ptinrect(p1, clipr); 66 e = 2*deltay - deltax; 67 y = p0.y; 68 d = byteaddr(dst, p0); 69 deltay *= 2; 70 deltax = deltay - 2*deltax; 71 for(x=p0.x; x<=maxx; x++){ 72 if(easy || (clipr.min.x<=x && clipr.min.y<=y && y<clipr.max.y)) 73 *d ^= (*d^srcval) & m; 74 if(e > 0){ 75 y += dy; 76 d += dd; 77 e += deltax; 78 }else 79 e += deltay; 80 d++; 81 m >>= bpp; 82 if(m == 0) 83 m = m0; 84 } 85 } 86 87 static 88 void 89 verline1(Memimage *dst, Point p0, Point p1, int srcval, Rectangle clipr) 90 { 91 int x, y, deltay, deltax, maxy; 92 int easy, e, bpp, m, m0, dd; 93 uchar *d; 94 95 deltax = p1.x - p0.x; 96 deltay = p1.y - p0.y; 97 dd = 1; 98 if(deltax < 0){ 99 dd = -1; 100 deltax = -deltax; 101 } 102 maxy = lmin(p1.y, clipr.max.y-1); 103 bpp = dst->depth; 104 m0 = 0xFF^(0xFF>>bpp); 105 m = m0 >> (p0.x&(7/dst->depth))*bpp; 106 easy = ptinrect(p0, clipr) && ptinrect(p1, clipr); 107 e = 2*deltax - deltay; 108 x = p0.x; 109 d = byteaddr(dst, p0); 110 deltax *= 2; 111 deltay = deltax - 2*deltay; 112 for(y=p0.y; y<=maxy; y++){ 113 if(easy || (clipr.min.y<=y && clipr.min.x<=x && x<clipr.max.x)) 114 *d ^= (*d^srcval) & m; 115 if(e > 0){ 116 x += dd; 117 d += dd; 118 e += deltay; 119 }else 120 e += deltax; 121 d += dst->width*sizeof(ulong); 122 m >>= bpp; 123 if(m == 0) 124 m = m0; 125 } 126 } 127 128 static 129 void 130 horliner(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr) 131 { 132 int x, y, sx, sy, deltay, deltax, minx, maxx; 133 int bpp, m, m0; 134 uchar *d, *s; 135 136 deltax = p1.x - p0.x; 137 deltay = p1.y - p0.y; 138 sx = drawreplxy(src->r.min.x, src->r.max.x, p0.x+dsrc.x); 139 minx = lmax(p0.x, clipr.min.x); 140 maxx = lmin(p1.x, clipr.max.x-1); 141 bpp = dst->depth; 142 m0 = 0xFF^(0xFF>>bpp); 143 m = m0 >> (minx&(7/dst->depth))*bpp; 144 for(x=minx; x<=maxx; x++){ 145 y = p0.y + (deltay*(x-p0.x)+deltax/2)/deltax; 146 if(clipr.min.y<=y && y<clipr.max.y){ 147 d = byteaddr(dst, Pt(x, y)); 148 sy = drawreplxy(src->r.min.y, src->r.max.y, y+dsrc.y); 149 s = byteaddr(src, Pt(sx, sy)); 150 *d ^= (*d^*s) & m; 151 } 152 if(++sx >= src->r.max.x) 153 sx = src->r.min.x; 154 m >>= bpp; 155 if(m == 0) 156 m = m0; 157 } 158 } 159 160 static 161 void 162 verliner(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr) 163 { 164 int x, y, sx, sy, deltay, deltax, miny, maxy; 165 int bpp, m, m0; 166 uchar *d, *s; 167 168 deltax = p1.x - p0.x; 169 deltay = p1.y - p0.y; 170 sy = drawreplxy(src->r.min.y, src->r.max.y, p0.y+dsrc.y); 171 miny = lmax(p0.y, clipr.min.y); 172 maxy = lmin(p1.y, clipr.max.y-1); 173 bpp = dst->depth; 174 m0 = 0xFF^(0xFF>>bpp); 175 for(y=miny; y<=maxy; y++){ 176 if(deltay == 0) /* degenerate line */ 177 x = p0.x; 178 else 179 x = p0.x + (deltax*(y-p0.y)+deltay/2)/deltay; 180 if(clipr.min.x<=x && x<clipr.max.x){ 181 m = m0 >> (x&(7/dst->depth))*bpp; 182 d = byteaddr(dst, Pt(x, y)); 183 sx = drawreplxy(src->r.min.x, src->r.max.x, x+dsrc.x); 184 s = byteaddr(src, Pt(sx, sy)); 185 *d ^= (*d^*s) & m; 186 } 187 if(++sy >= src->r.max.y) 188 sy = src->r.min.y; 189 } 190 } 191 192 static 193 void 194 horline(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr) 195 { 196 int x, y, deltay, deltax, minx, maxx; 197 int bpp, m, m0; 198 uchar *d, *s; 199 200 deltax = p1.x - p0.x; 201 deltay = p1.y - p0.y; 202 minx = lmax(p0.x, clipr.min.x); 203 maxx = lmin(p1.x, clipr.max.x-1); 204 bpp = dst->depth; 205 m0 = 0xFF^(0xFF>>bpp); 206 m = m0 >> (minx&(7/dst->depth))*bpp; 207 for(x=minx; x<=maxx; x++){ 208 y = p0.y + (deltay*(x-p0.x)+deltay/2)/deltax; 209 if(clipr.min.y<=y && y<clipr.max.y){ 210 d = byteaddr(dst, Pt(x, y)); 211 s = byteaddr(src, addpt(dsrc, Pt(x, y))); 212 *d ^= (*d^*s) & m; 213 } 214 m >>= bpp; 215 if(m == 0) 216 m = m0; 217 } 218 } 219 220 static 221 void 222 verline(Memimage *dst, Point p0, Point p1, Memimage *src, Point dsrc, Rectangle clipr) 223 { 224 int x, y, deltay, deltax, miny, maxy; 225 int bpp, m, m0; 226 uchar *d, *s; 227 228 deltax = p1.x - p0.x; 229 deltay = p1.y - p0.y; 230 miny = lmax(p0.y, clipr.min.y); 231 maxy = lmin(p1.y, clipr.max.y-1); 232 bpp = dst->depth; 233 m0 = 0xFF^(0xFF>>bpp); 234 for(y=miny; y<=maxy; y++){ 235 if(deltay == 0) /* degenerate line */ 236 x = p0.x; 237 else 238 x = p0.x + deltax*(y-p0.y)/deltay; 239 if(clipr.min.x<=x && x<clipr.max.x){ 240 m = m0 >> (x&(7/dst->depth))*bpp; 241 d = byteaddr(dst, Pt(x, y)); 242 s = byteaddr(src, addpt(dsrc, Pt(x, y))); 243 *d ^= (*d^*s) & m; 244 } 245 } 246 } 247 #endif /* NOTUSED */ 248 249 Memimage* 250 membrush(int radius) 251 { 252 static Memimage *brush; 253 static int brushradius; 254 255 if(brush==nil || brushradius!=radius){ 256 freememimage(brush); 257 brush = allocmemimage(Rect(0, 0, 2*radius+1, 2*radius+1), memopaque->chan); 258 if(brush != nil){ 259 memfillcolor(brush, DTransparent); /* zeros */ 260 memellipse(brush, Pt(radius, radius), radius, radius, -1, memopaque, Pt(radius, radius)); 261 } 262 brushradius = radius; 263 } 264 return brush; 265 } 266 267 static 268 void 269 discend(Point p, int radius, Memimage *dst, Memimage *src, Point dsrc) 270 { 271 Memimage *disc; 272 Rectangle r; 273 274 disc = membrush(radius); 275 if(disc != nil){ 276 r.min.x = p.x - radius; 277 r.min.y = p.y - radius; 278 r.max.x = p.x + radius+1; 279 r.max.y = p.y + radius+1; 280 memdraw(dst, r, src, addpt(r.min, dsrc), disc, Pt(0,0)); 281 } 282 } 283 284 static 285 void 286 arrowend(Point tip, Point *pp, int end, int sin, int cos, int radius) 287 { 288 int x1, x2, x3; 289 290 /* before rotation */ 291 if(end == Endarrow){ 292 x1 = Arrow1; 293 x2 = Arrow2; 294 x3 = Arrow3; 295 }else{ 296 x1 = (end>>5) & 0x1FF; /* distance along line from end of line to tip */ 297 x2 = (end>>14) & 0x1FF; /* distance along line from barb to tip */ 298 x3 = (end>>23) & 0x1FF; /* distance perpendicular from edge of line to barb */ 299 } 300 301 /* comments follow track of right-facing arrowhead */ 302 pp->x = tip.x+((2*radius+1)*sin/2-x1*cos); /* upper side of shaft */ 303 pp->y = tip.y-((2*radius+1)*cos/2+x1*sin); 304 pp++; 305 pp->x = tip.x+((2*radius+2*x3+1)*sin/2-x2*cos); /* upper barb */ 306 pp->y = tip.y-((2*radius+2*x3+1)*cos/2+x2*sin); 307 pp++; 308 pp->x = tip.x; 309 pp->y = tip.y; 310 pp++; 311 pp->x = tip.x+(-(2*radius+2*x3+1)*sin/2-x2*cos); /* lower barb */ 312 pp->y = tip.y-(-(2*radius+2*x3+1)*cos/2+x2*sin); 313 pp++; 314 pp->x = tip.x+(-(2*radius+1)*sin/2-x1*cos); /* lower side of shaft */ 315 pp->y = tip.y+((2*radius+1)*cos/2-x1*sin); 316 } 317 318 void 319 _memimageline(Memimage *dst, Point p0, Point p1, int end0, int end1, int radius, Memimage *src, Point sp, Rectangle clipr) 320 { 321 /* 322 * BUG: We should really really pick off purely horizontal and purely 323 * vertical lines and handle them separately with calls to memimagedraw 324 * on rectangles. 325 */ 326 327 int hor; 328 int sin, cos, dx, dy, t; 329 Rectangle oclipr; 330 Point q, pts[10], *pp, d; 331 332 if(radius < 0) 333 return; 334 if(rectclip(&clipr, dst->r) == 0) 335 return; 336 if(rectclip(&clipr, dst->clipr) == 0) 337 return; 338 d = subpt(sp, p0); 339 if(rectclip(&clipr, rectsubpt(src->clipr, d)) == 0) 340 return; 341 if((src->flags&Frepl)==0 && rectclip(&clipr, rectsubpt(src->r, d))==0) 342 return; 343 /* this means that only verline() handles degenerate lines (p0==p1) */ 344 hor = (abs(p1.x-p0.x) > abs(p1.y-p0.y)); 345 /* 346 * Clipping is a little peculiar. We can't use Sutherland-Cohen 347 * clipping because lines are wide. But this is probably just fine: 348 * we do all math with the original p0 and p1, but clip when deciding 349 * what pixels to draw. This means the layer code can call this routine, 350 * using clipr to define the region being written, and get the same set 351 * of pixels regardless of the dicing. 352 */ 353 if((hor && p0.x>p1.x) || (!hor && p0.y>p1.y)){ 354 q = p0; 355 p0 = p1; 356 p1 = q; 357 t = end0; 358 end0 = end1; 359 end1 = t; 360 } 361 362 /* Hard: */ 363 /* draw thick line using polygon fill */ 364 icossin2(p1.x-p0.x, p1.y-p0.y, &cos, &sin); 365 dx = (sin*(2*radius+1))/2; 366 dy = (cos*(2*radius+1))/2; 367 pp = pts; 368 oclipr = dst->clipr; 369 dst->clipr = clipr; 370 q.x = ICOSSCALE*p0.x+ICOSSCALE/2-cos/2; 371 q.y = ICOSSCALE*p0.y+ICOSSCALE/2-sin/2; 372 switch(end0 & 0x1F){ 373 case Enddisc: 374 discend(p0, radius, dst, src, d); 375 /* fall through */ 376 case Endsquare: 377 default: 378 pp->x = q.x-dx; 379 pp->y = q.y+dy; 380 pp++; 381 pp->x = q.x+dx; 382 pp->y = q.y-dy; 383 pp++; 384 break; 385 case Endarrow: 386 arrowend(q, pp, end0, -sin, -cos, radius); 387 memfillpolysc(dst, pts, 5, ~0, src, addpt(pts[0], mulpt(d, ICOSSCALE)), 1, 10, 1); 388 pp[1] = pp[4]; 389 pp += 2; 390 } 391 q.x = ICOSSCALE*p1.x+ICOSSCALE/2+cos/2; 392 q.y = ICOSSCALE*p1.y+ICOSSCALE/2+sin/2; 393 switch(end1 & 0x1F){ 394 case Enddisc: 395 discend(p1, radius, dst, src, d); 396 /* fall through */ 397 case Endsquare: 398 default: 399 pp->x = q.x+dx; 400 pp->y = q.y-dy; 401 pp++; 402 pp->x = q.x-dx; 403 pp->y = q.y+dy; 404 pp++; 405 break; 406 case Endarrow: 407 arrowend(q, pp, end1, sin, cos, radius); 408 memfillpolysc(dst, pp, 5, ~0, src, addpt(pts[0], mulpt(d, ICOSSCALE)), 1, 10, 1); 409 pp[1] = pp[4]; 410 pp += 2; 411 } 412 memfillpolysc(dst, pts, pp-pts, ~0, src, addpt(pts[0], mulpt(d, ICOSSCALE)), 0, 10, 1); 413 dst->clipr = oclipr; 414 return; 415 } 416 417 void 418 memimageline(Memimage *dst, Point p0, Point p1, int end0, int end1, int radius, Memimage *src, Point sp) 419 { 420 _memimageline(dst, p0, p1, end0, end1, radius, src, sp, dst->clipr); 421 } 422 423 /* 424 * Simple-minded conservative code to compute bounding box of line. 425 * Result is probably a little larger than it needs to be. 426 */ 427 static 428 void 429 addbbox(Rectangle *r, Point p) 430 { 431 if(r->min.x > p.x) 432 r->min.x = p.x; 433 if(r->min.y > p.y) 434 r->min.y = p.y; 435 if(r->max.x < p.x+1) 436 r->max.x = p.x+1; 437 if(r->max.y < p.y+1) 438 r->max.y = p.y+1; 439 } 440 441 int 442 memlineendsize(int end) 443 { 444 int x3; 445 446 if((end&0x3F) != Endarrow) 447 return 0; 448 if(end == Endarrow) 449 x3 = Arrow3; 450 else 451 x3 = (end>>23) & 0x1FF; 452 return x3; 453 } 454 455 Rectangle 456 memlinebbox(Point p0, Point p1, int end0, int end1, int radius) 457 { 458 Rectangle r, r1; 459 int extra; 460 461 r.min.x = 10000000; 462 r.min.y = 10000000; 463 r.max.x = -10000000; 464 r.max.y = -10000000; 465 extra = lmax(memlineendsize(end0), memlineendsize(end1)); 466 r1 = insetrect(canonrect(Rpt(p0, p1)), -(radius+extra)); 467 addbbox(&r, r1.min); 468 addbbox(&r, r1.max); 469 return r; 470 } 471