agg_gamma_functions.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //----------------------------------------------------------------------------
  2. // Anti-Grain Geometry - Version 2.4
  3. // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
  4. //
  5. // Permission to copy, use, modify, sell and distribute this software
  6. // is granted provided this copyright notice appears in all copies.
  7. // This software is provided "as is" without express or implied
  8. // warranty, and with no claim as to its suitability for any purpose.
  9. //
  10. //----------------------------------------------------------------------------
  11. // Contact: mcseem@antigrain.com
  12. // mcseemagg@yahoo.com
  13. // http://www.antigrain.com
  14. //----------------------------------------------------------------------------
  15. #ifndef AGG_GAMMA_FUNCTIONS_INCLUDED
  16. #define AGG_GAMMA_FUNCTIONS_INCLUDED
  17. #include <math.h>
  18. #include "agg_basics.h"
  19. namespace agg
  20. {
  21. //===============================================================gamma_none
  22. struct gamma_none
  23. {
  24. double operator()(double x) const { return x; }
  25. };
  26. //==============================================================gamma_power
  27. class gamma_power
  28. {
  29. public:
  30. gamma_power() : m_gamma(1.0) {}
  31. gamma_power(double g) : m_gamma(g) {}
  32. void gamma(double g) { m_gamma = g; }
  33. double gamma() const { return m_gamma; }
  34. double operator() (double x) const
  35. {
  36. return pow(x, m_gamma);
  37. }
  38. private:
  39. double m_gamma;
  40. };
  41. //==========================================================gamma_threshold
  42. class gamma_threshold
  43. {
  44. public:
  45. gamma_threshold() : m_threshold(0.5) {}
  46. gamma_threshold(double t) : m_threshold(t) {}
  47. void threshold(double t) { m_threshold = t; }
  48. double threshold() const { return m_threshold; }
  49. double operator() (double x) const
  50. {
  51. return (x < m_threshold) ? 0.0 : 1.0;
  52. }
  53. private:
  54. double m_threshold;
  55. };
  56. //============================================================gamma_linear
  57. class gamma_linear
  58. {
  59. public:
  60. gamma_linear() : m_start(0.0), m_end(1.0) {}
  61. gamma_linear(double s, double e) : m_start(s), m_end(e) {}
  62. void set(double s, double e) { m_start = s; m_end = e; }
  63. void start(double s) { m_start = s; }
  64. void end(double e) { m_end = e; }
  65. double start() const { return m_start; }
  66. double end() const { return m_end; }
  67. double operator() (double x) const
  68. {
  69. if(x < m_start) return 0.0;
  70. if(x > m_end) return 1.0;
  71. return (x - m_start) / (m_end - m_start);
  72. }
  73. private:
  74. double m_start;
  75. double m_end;
  76. };
  77. //==========================================================gamma_multiply
  78. class gamma_multiply
  79. {
  80. public:
  81. gamma_multiply() : m_mul(1.0) {}
  82. gamma_multiply(double v) : m_mul(v) {}
  83. void value(double v) { m_mul = v; }
  84. double value() const { return m_mul; }
  85. double operator() (double x) const
  86. {
  87. double y = x * m_mul;
  88. if(y > 1.0) y = 1.0;
  89. return y;
  90. }
  91. private:
  92. double m_mul;
  93. };
  94. inline double sRGB_to_linear(double x)
  95. {
  96. return (x <= 0.04045) ? (x / 12.92) : pow((x + 0.055) / (1.055), 2.4);
  97. }
  98. inline double linear_to_sRGB(double x)
  99. {
  100. return (x <= 0.0031308) ? (x * 12.92) : (1.055 * pow(x, 1 / 2.4) - 0.055);
  101. }
  102. }
  103. #endif