chan.c 1.1 KB

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