xref: /inferno-os/libdraw/poly.c (revision 37da2899f40661e3e9631e497da8dc59b971cbd0)
1 #include "lib9.h"
2 #include "draw.h"
3 
4 static
5 uchar*
addcoord(uchar * p,int oldx,int newx)6 addcoord(uchar *p, int oldx, int newx)
7 {
8 	int dx;
9 
10 	dx = newx-oldx;
11 	/* does dx fit in 7 signed bits? */
12 	if((unsigned)(dx - -0x40) <= 0x7F)
13 		*p++ = dx&0x7F;
14 	else{
15 		*p++ = 0x80 | (newx&0x7F);
16 		*p++ = newx>>7;
17 		*p++ = newx>>15;
18 	}
19 	return p;
20 }
21 
22 static
23 void
dopoly(int cmd,Image * dst,Point * pp,int np,int end0,int end1,int radius,Image * src,Point * sp,Drawop op)24 dopoly(int cmd, Image *dst, Point *pp, int np, int end0, int end1, int radius, Image *src, Point *sp, Drawop op)
25 {
26 	uchar *a, *t, *u;
27 	int i, ox, oy;
28 
29 	if(np == 0)
30 		return;
31 	t = malloc(np*2*3);
32 	if(t == nil)
33 		return;
34 	u = t;
35 	ox = oy = 0;
36 	for(i=0; i<np; i++){
37 		u = addcoord(u, ox, pp[i].x);
38 		ox = pp[i].x;
39 		u = addcoord(u, oy, pp[i].y);
40 		oy = pp[i].y;
41 	}
42 
43 	_setdrawop(dst->display, op);
44 
45 	a = bufimage(dst->display, 1+4+2+4+4+4+4+2*4+(u-t));
46 	if(a == 0){
47 		free(t);
48 		_drawprint(2, "image poly: %r\n");
49 		return;
50 	}
51 	a[0] = cmd;
52 	BPLONG(a+1, dst->id);
53 	BPSHORT(a+5, np-1);
54 	BPLONG(a+7, end0);
55 	BPLONG(a+11, end1);
56 	BPLONG(a+15, radius);
57 	BPLONG(a+19, src->id);
58 	BPLONG(a+23, sp->x);
59 	BPLONG(a+27, sp->y);
60 	memmove(a+31, t, u-t);
61 	free(t);
62 }
63 
64 void
poly(Image * dst,Point * p,int np,int end0,int end1,int radius,Image * src,Point sp)65 poly(Image *dst, Point *p, int np, int end0, int end1, int radius, Image *src, Point sp)
66 {
67 	dopoly('p', dst, p, np, end0, end1, radius, src, &sp, SoverD);
68 }
69 
70 void
polyop(Image * dst,Point * p,int np,int end0,int end1,int radius,Image * src,Point sp,Drawop op)71 polyop(Image *dst, Point *p, int np, int end0, int end1, int radius, Image *src, Point sp, Drawop op)
72 {
73 	dopoly('p', dst, p, np, end0, end1, radius, src, &sp, op);
74 }
75 
76 void
fillpoly(Image * dst,Point * p,int np,int wind,Image * src,Point sp)77 fillpoly(Image *dst, Point *p, int np, int wind, Image *src, Point sp)
78 {
79 	dopoly('P', dst, p, np, wind, 0, 0, src, &sp, SoverD);
80 }
81 
82 void
fillpolyop(Image * dst,Point * p,int np,int wind,Image * src,Point sp,Drawop op)83 fillpolyop(Image *dst, Point *p, int np, int wind, Image *src, Point sp, Drawop op)
84 {
85 	dopoly('P', dst, p, np, wind, 0, 0, src, &sp, op);
86 }
87