Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

148 righe
2.8 KiB

  1. #include <stdio.h>
  2. #include <math.h>
  3. typedef struct
  4. {
  5. float h, s, v;
  6. } hsvpix_t;
  7. static void
  8. HSVtoRGB (float *r, float *g, float *b, hsvpix_t pix)
  9. {
  10. int i;
  11. double f, p, q, t, h;
  12. if (pix.s == 0) {
  13. // achromatic (grey)
  14. *r = *g = *b = pix.v;
  15. return;
  16. }
  17. h = pix.h / 60; // sector 0 to 5
  18. i = floor (h);
  19. f = h - i; // factorial part of h
  20. p = pix.v * (1 - pix.s);
  21. q = pix.v * (1 - pix.s * f);
  22. t = pix.v * (1 - pix.s * (1 - f));
  23. switch (i) {
  24. case 0:
  25. *r = pix.v;
  26. *g = t;
  27. *b = p;
  28. break;
  29. case 1:
  30. *r = q;
  31. *g = pix.v;
  32. *b = p;
  33. break;
  34. case 2:
  35. *r = p;
  36. *g = pix.v;
  37. *b = t;
  38. break;
  39. case 3:
  40. *r = p;
  41. *g = q;
  42. *b = pix.v;
  43. break;
  44. case 4:
  45. *r = t;
  46. *g = p;
  47. *b = pix.v;
  48. break;
  49. default: // case 5:
  50. *r = pix.v;
  51. *g = p;
  52. *b = q;
  53. break;
  54. }
  55. }
  56. static struct
  57. {
  58. float Seathresold;
  59. float Landthresold;
  60. float Tthresold;
  61. hsvpix_t CloudTop;
  62. hsvpix_t CloudBot;
  63. hsvpix_t SeaTop;
  64. hsvpix_t SeaBot;
  65. hsvpix_t GroundTop;
  66. hsvpix_t GroundBot;
  67. } fcinfo = {
  68. 30.0, 90.0, 100.0, {
  69. 230, 0.2, 0.3}, {
  70. 230, 0.0, 1.0}, {
  71. 200.0, 0.7, 0.6}, {
  72. 240.0, 0.6, 0.4}, {
  73. 60.0, 0.6, 0.2}, {
  74. 100.0, 0.0, 0.5}
  75. };
  76. void
  77. readfconf (char *file)
  78. {
  79. FILE *fin;
  80. fin = fopen (file, "r");
  81. if (fin == NULL)
  82. return;
  83. fscanf (fin, "%g\n", &fcinfo.Seathresold);
  84. fscanf (fin, "%g\n", &fcinfo.Landthresold);
  85. fscanf (fin, "%g\n", &fcinfo.Tthresold);
  86. fscanf (fin, "%g %g %g\n", &fcinfo.CloudTop.h, &fcinfo.CloudTop.s,
  87. &fcinfo.CloudTop.v);
  88. fscanf (fin, "%g %g %g\n", &fcinfo.CloudBot.h, &fcinfo.CloudBot.s,
  89. &fcinfo.CloudBot.v);
  90. fscanf (fin, "%g %g %g\n", &fcinfo.SeaTop.h, &fcinfo.SeaTop.s,
  91. &fcinfo.SeaTop.v);
  92. fscanf (fin, "%g %g %g\n", &fcinfo.SeaBot.h, &fcinfo.SeaBot.s,
  93. &fcinfo.SeaBot.v);
  94. fscanf (fin, "%g %g %g\n", &fcinfo.GroundTop.h, &fcinfo.GroundTop.s,
  95. &fcinfo.GroundTop.v);
  96. fscanf (fin, "%g %g %g\n", &fcinfo.GroundBot.h, &fcinfo.GroundBot.s,
  97. &fcinfo.GroundBot.v);
  98. fclose (fin);
  99. };
  100. void
  101. falsecolor (double v, double t, float *r, float *g, float *b)
  102. {
  103. hsvpix_t top, bot, c;
  104. double scv, sct;
  105. if (t < fcinfo.Tthresold) {
  106. if (v < fcinfo.Seathresold) {
  107. /* sea */
  108. top = fcinfo.SeaTop, bot = fcinfo.SeaBot;
  109. scv = v / fcinfo.Seathresold;
  110. sct = t / fcinfo.Tthresold;
  111. }
  112. else {
  113. /* ground */
  114. top = fcinfo.GroundTop, bot = fcinfo.GroundBot;
  115. scv =
  116. (v - fcinfo.Seathresold) / (fcinfo.Landthresold - fcinfo.Seathresold);
  117. sct = t / fcinfo.Tthresold;
  118. }
  119. }
  120. else {
  121. /* clouds */
  122. top = fcinfo.CloudTop, bot = fcinfo.CloudBot;
  123. scv = v / 255.0;
  124. sct = t / 255.0;
  125. }
  126. c.s = top.s + sct * (bot.s - top.s);
  127. c.v = top.v + scv * (bot.v - top.v);
  128. c.h = top.h + scv * sct * (bot.h - top.h);
  129. HSVtoRGB (r, g, b, c);
  130. };