close.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. float c1 = 1.402;
  13. float c2 = 0.34414;
  14. float c3 = 0.71414;
  15. float c4 = 1.772;
  16. int
  17. closest(int Y, int Cb, int Cr)
  18. {
  19. double r, g, b;
  20. double diff, min;
  21. int rgb, R, G, B, v, i;
  22. int y1, cb1, cr1;
  23. Cb -= 128;
  24. Cr -= 128;
  25. r = Y+c1*Cr;
  26. g = Y-c2*Cb-c3*Cr;
  27. b = Y+c4*Cb;
  28. //print("YCbCr: %d %d %d, RGB: %g %g %g\n", Y, Cb, Cr, r, g, b);
  29. min = 1000000.;
  30. v = 1000;
  31. for(i=0; i<256; i++){
  32. rgb = cmap2rgb(i);
  33. R = (rgb >> 16) & 0xFF;
  34. G = (rgb >> 8) & 0xFF;
  35. B = (rgb >> 0) & 0xFF;
  36. diff = (R-r)*(R-r) + (G-g)*(G-g) + (B-b)*(B-b);
  37. // y1 = 0.5870*G + 0.114*B + 0.299*R;
  38. // cb1 = (B-y1)/1.772;
  39. // cr1 = (R-y1)/1.402;
  40. if(diff < min){
  41. // if(Y==0 && y1!=0)
  42. // continue;
  43. // if(Y==256-16 && y1<256-16)
  44. // continue;
  45. // if(Cb==0 && cb1!=0)
  46. // continue;
  47. // if(Cb==256-16 && cb1<256-16)
  48. // continue;
  49. // if(Cr==0 && cr1!=0)
  50. // continue;
  51. // if(Cr==256-16 && cr1<256-16)
  52. // continue;
  53. //print("%d %d %d\n", R, G, B);
  54. min = diff;
  55. v = i;
  56. }
  57. }
  58. if(v > 255)
  59. abort();
  60. return v;
  61. }
  62. #define SHIFT 5
  63. #define INC (1<<SHIFT)
  64. typedef struct Color Color;
  65. struct Color
  66. {
  67. int col;
  68. Color *next;
  69. };
  70. Color *col[INC*INC*INC];
  71. void
  72. add(int c, int y, int cb, int cr)
  73. {
  74. Color *cp;
  75. y >>= 8-SHIFT;
  76. cb >>= 8-SHIFT;
  77. cr >>= 8-SHIFT;
  78. cp = col[cr+INC*(cb+INC*y)];
  79. while(cp != nil){
  80. if(cp->col == c)
  81. return;
  82. cp = cp->next;
  83. }
  84. cp = malloc(sizeof(Color));
  85. cp->col = c;
  86. cp->next = col[cr+INC*(cb+INC*y)];
  87. col[cr+INC*(cb+INC*y)] = cp;
  88. }
  89. void
  90. main(void)
  91. {
  92. int y, cb, cr, n;
  93. Color *cp;
  94. for(y=0; y<256; y++){
  95. for(cb=0; cb<256; cb++)
  96. for(cr=0;cr<256;cr++)
  97. add(closest(y, cb, cr), y, cb, cr);
  98. fprint(2, "%d done\n", y);
  99. }
  100. for(y=0; y<INC*INC*INC; y++){
  101. n = 0;
  102. cp = col[y];
  103. while(cp != nil){
  104. n++;
  105. cp = cp->next;
  106. }
  107. cp = col[y];
  108. while(cp != nil){
  109. n++;
  110. print("%d ", cp->col);
  111. cp = cp->next;
  112. }
  113. print("\n");
  114. }
  115. }