Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

fcolor.c 2.8 KiB

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