utils.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. Copyright (c) 2006 soho vfx inc.
  3. Copyright (c) 2006 The 3Delight Team.
  4. */
  5. #ifndef _utils_h
  6. #define _utils_h
  7. #define LOG05 -0.693147180559945 /* log(0.5) */
  8. #ifndef UNDEFINED_UV
  9. #define UNDEFINED_UV -10000000
  10. #endif
  11. #define ISUVDEFINED(U, V) ( abs(U) < 100000 && abs(V) < 100000 )
  12. #define WRAPMAX (1.000001)
  13. #define EPSILON 1e-6
  14. float
  15. luminance( color i_color )
  16. {
  17. return
  18. 0.3 * comp(i_color, 0) +
  19. 0.59 * comp(i_color, 1) +
  20. 0.11 * comp(i_color, 2);
  21. }
  22. // Compute the CIE luminance (Rec. 709) of a given color.
  23. float CIEluminance(color c)
  24. {
  25. return
  26. comp(c, 0) * 0.212671
  27. + comp(c, 1) * 0.715160
  28. + comp(c, 2) * 0.072169;
  29. }
  30. // Compute the square of a given value.
  31. float sq(float x)
  32. {
  33. return x * x;
  34. }
  35. color
  36. cabs( color i_color )
  37. {
  38. return color(
  39. abs(comp(i_color, 0)), abs(comp(i_color,1)), abs(comp(i_color,2)) );
  40. }
  41. /* Taken from ARMAN */
  42. float filteredpulse (float edge0, edge1, x, dx)
  43. {
  44. float x0 = x - dx/2;
  45. float x1 = x0 + dx;
  46. return max (0, (min(x1,edge1)-max(x0,edge0)) / dx);
  47. }
  48. /* Taken from ARMAN and improved. */
  49. float filteredpulsetrain (float edge, period, x, dx)
  50. {
  51. /* First, normalize so period == 1 and our domain of interest is > 0 */
  52. float w = dx/period;
  53. float x0 = x/period - w/2;
  54. float x1 = x0+w;
  55. float nedge = edge / period;
  56. /* Definite integral of normalized pulsetrain from 0 to t */
  57. float integral (float t) {
  58. extern float nedge;
  59. return ((1-nedge)*floor(t) + max(0,t-floor(t)-nedge));
  60. }
  61. float result;
  62. if( x0 == x1 )
  63. {
  64. /* Trap the unfiltered case so it doesn't return 0 (when dx << x). */
  65. result = (x0 - floor(x0) < nedge) ? 0 : 1;
  66. }
  67. else
  68. {
  69. result = (integral(x1) - integral(x0)) / w;
  70. /*
  71. The above integral is subject to its own aliasing as we go beyond
  72. where the pattern should be extinct. We try to avoid that by
  73. switching to a constant value.
  74. */
  75. float extinct = smoothstep( 0.4, 0.5, w );
  76. result = result + extinct * (1 - nedge - result);
  77. }
  78. return result;
  79. }
  80. /*
  81. Perlin's bias function
  82. */
  83. float bias(float b, x)
  84. {
  85. return pow(x, log(b)/LOG05);
  86. }
  87. void
  88. colorBalance(
  89. output color io_outColor;
  90. output float io_outAlpha;
  91. uniform float i_alphaIsLuminance;
  92. float i_alphaGain;
  93. float i_alphaOffset;
  94. color i_colorGain;
  95. color i_colorOffset;
  96. uniform float i_invert; )
  97. {
  98. if(i_invert != 0.0)
  99. {
  100. io_outColor = 1 - io_outColor;
  101. io_outAlpha = 1 - io_outAlpha;
  102. }
  103. if(i_alphaIsLuminance != 0.0)
  104. {
  105. io_outAlpha = luminance( io_outColor );
  106. }
  107. io_outColor *= i_colorGain;
  108. io_outColor += i_colorOffset;
  109. io_outAlpha *= i_alphaGain;
  110. io_outAlpha += i_alphaOffset;
  111. }
  112. float srgbToLinear(float i_value)
  113. {
  114. float linear;
  115. if(i_value <= 0.04045)
  116. {
  117. linear = (1.0/12.92) * i_value;
  118. }
  119. else
  120. {
  121. linear = pow((i_value + 0.055) * (1.0 / 1.055), 2.4);
  122. }
  123. return linear;
  124. }
  125. float rec709ToLinear(float i_value)
  126. {
  127. float linear;
  128. if(i_value <= 0.081)
  129. {
  130. linear = (1.0 / 4.5) * i_value;
  131. }
  132. else
  133. {
  134. linear = pow((i_value + 0.099) * (1.0 / 1.099), (1.0 / 0.45));
  135. }
  136. return linear;
  137. }
  138. color getLinearizedColor(color i_color)
  139. {
  140. color result = i_color;
  141. float profile;
  142. if( option( "user:defaultinputcolorprofile", profile ) != 0 )
  143. {
  144. if( profile == 3 )
  145. {
  146. result =
  147. color(
  148. srgbToLinear(result[0]),
  149. srgbToLinear(result[1]),
  150. srgbToLinear(result[2]) );
  151. }
  152. else if( profile == 5 )
  153. {
  154. result =
  155. color(
  156. rec709ToLinear(result[0]),
  157. rec709ToLinear(result[1]),
  158. rec709ToLinear(result[2]) );
  159. }
  160. }
  161. return result;
  162. }
  163. #endif