blob: 1677c5b527325b73f1f37f259e6919655eace27f [file] [log] [blame]
rsc76193d72003-09-30 17:47:42 +00001#include <u.h>
2#include <libc.h>
3#include <draw.h>
4
5static char channames[] = "rgbkamx";
6char*
7chantostr(char *buf, u32int cc)
8{
9 u32int 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
rsc76193d72003-09-30 17:47:42 +000032u32int
33strtochan(char *s)
34{
35 char *p, *q;
36 u32int c;
37 int t, n;
38
39 c = 0;
40 p=s;
41 while(*p && isspace(*p))
42 p++;
43
44 while(*p && !isspace(*p)){
45 if((q = strchr(channames, p[0])) == nil)
46 return 0;
47 t = q-channames;
48 if(p[1] < '0' || p[1] > '9')
49 return 0;
50 n = p[1]-'0';
51 c = (c<<8) | __DC(t, n);
52 p += 2;
53 }
54 return c;
55}
56
57int
58chantodepth(u32int c)
59{
60 int n;
61
62 for(n=0; c; c>>=8){
63 if(TYPE(c) >= NChan || NBITS(c) > 8 || NBITS(c) <= 0)
64 return 0;
65 n += NBITS(c);
66 }
67 if(n==0 || (n>8 && n%8) || (n<8 && 8%n))
68 return 0;
69 return n;
70}