chan.c 1.1 KB

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