You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fcolor.c 2.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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; *g = t; *b = p;
  24. break;
  25. case 1:
  26. *r = q; *g = pix.v; *b = p;
  27. break;
  28. case 2:
  29. *r = p; *g = pix.v; *b = t;
  30. break;
  31. case 3:
  32. *r = p; *g = q; *b = pix.v;
  33. break;
  34. case 4:
  35. *r = t; *g = p; *b = pix.v;
  36. break;
  37. default: // case 5:
  38. *r = pix.v; *g = p; *b = q;
  39. break;
  40. }
  41. }
  42. static struct {
  43. float Vthresold;
  44. float Tthresold;
  45. hsvpix_t CloudTop;
  46. hsvpix_t CloudBot;
  47. hsvpix_t SeaTop;
  48. hsvpix_t SeaBot;
  49. hsvpix_t GroundTop;
  50. hsvpix_t GroundBot;
  51. } fcinfo= {
  52. 30.0,
  53. 170.0,
  54. {0,0.0,0.3},{0,0.0,1.0},
  55. {240.0,0.9,0.5},{220.0,0.8,0.8},
  56. {90.0,0.8,0.3},{50.0,0.8,1.0}
  57. };
  58. void readfconf(char *file)
  59. {
  60. FILE * fin;
  61. fin=fopen(file,"r");
  62. if(fin==NULL) return;
  63. fscanf(fin,"%g\n",&fcinfo.Vthresold);
  64. fscanf(fin,"%g\n",&fcinfo.Tthresold);
  65. fscanf(fin,"%g %g %g\n",&fcinfo.CloudTop.h,&fcinfo.CloudTop.s,&fcinfo.CloudTop.v);
  66. fscanf(fin,"%g %g %g\n",&fcinfo.CloudBot.h,&fcinfo.CloudBot.s,&fcinfo.CloudBot.v);
  67. fscanf(fin,"%g %g %g\n",&fcinfo.SeaTop.h,&fcinfo.SeaTop.s,&fcinfo.SeaTop.v);
  68. fscanf(fin,"%g %g %g\n",&fcinfo.SeaBot.h,&fcinfo.SeaBot.s,&fcinfo.SeaBot.v);
  69. fscanf(fin,"%g %g %g\n",&fcinfo.GroundTop.h,&fcinfo.GroundTop.s,&fcinfo.GroundTop.v);
  70. fscanf(fin,"%g %g %g\n",&fcinfo.GroundBot.h,&fcinfo.GroundBot.s,&fcinfo.GroundBot.v);
  71. fclose(fin);
  72. };
  73. void falsecolor(double v, double t, float *r, float *g, float *b)
  74. {
  75. hsvpix_t top,bot,c;
  76. double scv,sct;
  77. if(v <= fcinfo.Vthresold) {
  78. /* sea */
  79. top=fcinfo.SeaTop,bot=fcinfo.SeaBot;
  80. scv=v/fcinfo.Vthresold;
  81. sct=t/255.0;
  82. } else {
  83. if(t<=fcinfo.Tthresold) {
  84. /* clouds */
  85. top=fcinfo.CloudTop,bot=fcinfo.CloudBot;
  86. scv=(v-fcinfo.Vthresold)/(255.0-fcinfo.Vthresold);
  87. sct=t/fcinfo.Tthresold;
  88. } else {
  89. /* ground */
  90. top=fcinfo.GroundTop,bot=fcinfo.GroundBot;
  91. scv=(v-fcinfo.Vthresold)/(255.0-fcinfo.Vthresold);
  92. sct=(t-fcinfo.Tthresold)/(255.0-fcinfo.Tthresold);
  93. }
  94. };
  95. c.h=top.h+sct*(bot.h-top.h);
  96. c.v=top.v+scv*(bot.v-top.v);
  97. c.s=top.s+scv*sct*(bot.s-top.s);
  98. HSVtoRGB( r, g, b, c );
  99. };