1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <memdraw.h>
enum
{
Arrow1 = 8,
Arrow2 = 10,
Arrow3 = 3,
};
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(u32int);
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(u32int);
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;
deltax = p1.x - p0.x;
deltay = p1.y - p0.y;
sx = drawreplxy(src->r.min.x, src->r.max.x, p0.x+dsrc.x);
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)+deltax/2)/deltax;
if(clipr.min.y<=y && y<clipr.max.y){
d = byteaddr(dst, Pt(x, y));
sy = drawreplxy(src->r.min.y, src->r.max.y, y+dsrc.y);
s = byteaddr(src, Pt(sx, sy));
*d ^= (*d^*s) & m;
}
if(++sx >= src->r.max.x)
sx = src->r.min.x;
m >>= bpp;
if(m == 0)
m = m0;
}
}
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;
deltax = p1.x - p0.x;
deltay = p1.y - p0.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;
if(clipr.min.x<=x && x<clipr.max.x){
m = m0 >> (x&(7/dst->depth))*bpp;
d = byteaddr(dst, Pt(x, y));
s = byteaddr(src, addpt(dsrc, Pt(x, y)));
*d ^= (*d^*s) & m;
}
}
}
#endif /* NOTUSED */
static Memimage*
membrush(int radius)
{
static Memimage *brush;
static int brushradius;
if(brush==nil || brushradius!=radius){
freememimage(brush);
brush = allocmemimage(Rect(0, 0, 2*radius+1, 2*radius+1), memopaque->chan);
if(brush != nil){
memfillcolor(brush, DTransparent); /* zeros */
memellipse(brush, Pt(radius, radius), radius, radius, -1, memopaque, Pt(radius, radius), S);
}
brushradius = radius;
}
return brush;
}
static
void
discend(Point p, int radius, Memimage *dst, Memimage *src, Point dsrc, int op)
{
Memimage *disc;
Rectangle r;
disc = membrush(radius);
if(disc != nil){
r.min.x = p.x - radius;
r.min.y = p.y - radius;
r.max.x = p.x + radius+1;
r.max.y = p.y + radius+1;
memdraw(dst, r, src, addpt(r.min, dsrc), disc, Pt(0,0), op);
}
}
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 */
}
/* comments follow track of right-facing arrowhead */
pp->x = tip.x+((2*radius+1)*sin/2-x1*cos); /* upper side of shaft */
pp->y = tip.y-((2*radius+1)*cos/2+x1*sin);
pp++;
pp->x = tip.x+((2*radius+2*x3+1)*sin/2-x2*cos); /* upper barb */
pp->y = tip.y-((2*radius+2*x3+1)*cos/2+x2*sin);
pp++;
pp->x = tip.x;
pp->y = tip.y;
pp++;
pp->x = tip.x+(-(2*radius+2*x3+1)*sin/2-x2*cos); /* lower barb */
pp->y = tip.y-(-(2*radius+2*x3+1)*cos/2+x2*sin);
pp++;
pp->x = tip.x+(-(2*radius+1)*sin/2-x1*cos); /* lower side of shaft */
pp->y = tip.y+((2*radius+1)*cos/2-x1*sin);
}
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;
}
if((p0.x == p1.x || p0.y == p1.y) && (end0&0x1F) == Endsquare && (end1&0x1F) == Endsquare){
r.min = p0;
r.max = p1;
if(p0.x == p1.x){
r.min.x -= radius;
r.max.x += radius+1;
}
else{
r.min.y -= radius;
r.max.y += radius+1;
}
oclipr = dst->clipr;
dst->clipr = clipr;
memimagedraw(dst, r, src, sp, memopaque, sp, op);
dst->clipr = oclipr;
return;
}
/* Hard: */
/* draw thick line using polygon fill */
icossin2(p1.x-p0.x, p1.y-p0.y, &cos, &sin);
dx = (sin*(2*radius+1))/2;
dy = (cos*(2*radius+1))/2;
pp = pts;
oclipr = dst->clipr;
dst->clipr = clipr;
q.x = ICOSSCALE*p0.x+ICOSSCALE/2-cos/2;
q.y = ICOSSCALE*p0.y+ICOSSCALE/2-sin/2;
switch(end0 & 0x1F){
case Enddisc:
discend(p0, radius, dst, src, d, op);
/* fall through */
case Endsquare:
default:
pp->x = q.x-dx;
pp->y = q.y+dy;
pp++;
pp->x = q.x+dx;
pp->y = q.y-dy;
pp++;
break;
case Endarrow:
arrowend(q, pp, end0, -sin, -cos, radius);
_memfillpolysc(dst, pts, 5, ~0, src, addpt(pts[0], mulpt(d, ICOSSCALE)), 1, 10, 1, op);
pp[1] = pp[4];
pp += 2;
}
q.x = ICOSSCALE*p1.x+ICOSSCALE/2+cos/2;
q.y = ICOSSCALE*p1.y+ICOSSCALE/2+sin/2;
switch(end1 & 0x1F){
case Enddisc:
discend(p1, radius, dst, src, d, op);
/* fall through */
case Endsquare:
default:
pp->x = q.x+dx;
pp->y = q.y-dy;
pp++;
pp->x = q.x-dx;
pp->y = q.y+dy;
pp++;
break;
case Endarrow:
arrowend(q, pp, end1, sin, cos, radius);
_memfillpolysc(dst, pp, 5, ~0, src, addpt(pts[0], mulpt(d, ICOSSCALE)), 1, 10, 1, op);
pp[1] = pp[4];
pp += 2;
}
_memfillpolysc(dst, pts, pp-pts, ~0, src, addpt(pts[0], mulpt(d, ICOSSCALE)), 0, 10, 1, op);
dst->clipr = oclipr;
return;
}
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;
}
int
memlineendsize(int end)
{
int x3;
if((end&0x3F) != Endarrow)
return 0;
if(end == Endarrow)
x3 = Arrow3;
else
x3 = (end>>23) & 0x1FF;
return x3;
}
Rectangle
memlinebbox(Point p0, Point p1, int end0, int end1, int radius)
{
Rectangle r, r1;
int extra;
r.min.x = 10000000;
r.min.y = 10000000;
r.max.x = -10000000;
r.max.y = -10000000;
extra = lmax(memlineendsize(end0), memlineendsize(end1));
r1 = insetrect(canonrect(Rpt(p0, p1)), -(radius+extra));
addbbox(&r, r1.min);
addbbox(&r, r1.max);
return r;
}
|