xref: /plan9-contrib/sys/src/libdraw/chan.c (revision 815ca0085e46c6719a71345e32c5520a42b6f9b9)
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 
5 static char channames[] = "rgbkamx";
6 char*
chantostr(char * buf,ulong cc)7 chantostr(char *buf, ulong cc)
8 {
9 	ulong c, rc;
10 	char *p;
11 
12 	if(chantodepth(cc) == 0)
13 		return nil;
14 
15 	/* reverse the channel descriptor so we can easily generate the string in the right order */
16 	rc = 0;
17 	for(c=cc; c; c>>=8){
18 		rc <<= 8;
19 		rc |= c&0xFF;
20 	}
21 
22 	p = buf;
23 	for(c=rc; c; c>>=8) {
24 		*p++ = channames[TYPE(c)];
25 		*p++ = '0'+NBITS(c);
26 	}
27 	*p = 0;
28 
29 	return buf;
30 }
31 
32 /* avoid pulling in ctype when using with drawterm etc. */
33 static int
isspace(char c)34 isspace(char c)
35 {
36 	return c==' ' || c== '\t' || c=='\r' || c=='\n';
37 }
38 
39 ulong
strtochan(char * s)40 strtochan(char *s)
41 {
42 	char *p, *q;
43 	ulong c;
44 	int t, n, d;
45 
46 	c = 0;
47 	d = 0;
48 	p=s;
49 	while(*p && isspace(*p))
50 		p++;
51 
52 	while(*p && !isspace(*p)){
53 		if((q = strchr(channames, p[0])) == nil)
54 			return 0;
55 		t = q-channames;
56 		if(p[1] < '0' || p[1] > '9')
57 			return 0;
58 		n = p[1]-'0';
59 		d += n;
60 		c = (c<<8) | __DC(t, n);
61 		p += 2;
62 	}
63 	if(d==0 || (d>8 && d%8) || (d<8 && 8%d))
64 		return 0;
65 	return c;
66 }
67 
68 int
chantodepth(ulong c)69 chantodepth(ulong c)
70 {
71 	int n;
72 
73 	for(n=0; c; c>>=8){
74 		if(TYPE(c) >= NChan || NBITS(c) > 8 || NBITS(c) <= 0)
75 			return 0;
76 		n += NBITS(c);
77 	}
78 	if(n==0 || (n>8 && n%8) || (n<8 && 8%n))
79 		return 0;
80 	return n;
81 }
82