Curves.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*-----------------------------------------------------------.
  2. / Curves /
  3. '-----------------------------------------------------------*/
  4. /*
  5. by Christian Cann Schuldt Jensen ~ CeeJay.dk
  6. Curves, uses S-curves to increase contrast, without clipping highlights and shadows.
  7. */
  8. float4 CurvesPass( float4 colorInput )
  9. {
  10. float3 lumCoeff = float3(0.2126, 0.7152, 0.0722); //Values to calculate luma with
  11. float Curves_contrast_blend = Curves_contrast;
  12. float PI = acos(-1); //3.1415926589
  13. /*-----------------------------------------------------------.
  14. / Separation of Luma and Chroma /
  15. '-----------------------------------------------------------*/
  16. // -- Calculate Luma and Chroma if needed --
  17. #if Curves_mode != 2
  18. //calculate luma (grey)
  19. float luma = dot(lumCoeff, colorInput.rgb);
  20. //calculate chroma
  21. float3 chroma = colorInput.rgb - luma;
  22. #endif
  23. // -- Which value to put through the contrast formula? --
  24. // I name it x because makes it easier to copy-paste to Graphtoy or Wolfram Alpha or another graphing program
  25. #if Curves_mode == 2
  26. float3 x = colorInput.rgb; //if the curve should be applied to both Luma and Chroma
  27. #elif Curves_mode == 1
  28. float3 x = chroma; //if the curve should be applied to Chroma
  29. x = x * 0.5 + 0.5; //adjust range of Chroma from -1 -> 1 to 0 -> 1
  30. #else // Curves_mode == 0
  31. float x = luma; //if the curve should be applied to Luma
  32. #endif
  33. /*-----------------------------------------------------------.
  34. / Contrast formulas /
  35. '-----------------------------------------------------------*/
  36. // -- Curve 1 --
  37. #if Curves_formula == 1
  38. x = sin(PI * 0.5 * x); // Sin - 721 amd fps, +vign 536 nv
  39. x *= x;
  40. //x = 0.5 - 0.5*cos(PI*x);
  41. //x = 0.5 * -sin(PI * -x + (PI*0.5)) + 0.5;
  42. #endif
  43. // -- Curve 2 --
  44. #if Curves_formula == 2
  45. x = x - 0.5;
  46. x = ( x / (0.5 + abs(x)) ) + 0.5;
  47. //x = ( (x - 0.5) / (0.5 + abs(x-0.5)) ) + 0.5;
  48. #endif
  49. // -- Curve 3 --
  50. #if Curves_formula == 3
  51. //x = smoothstep(0.0,1.0,x); //smoothstep
  52. x = x*x*(3.0-2.0*x); //faster smoothstep alternative - 776 amd fps, +vign 536 nv
  53. //x = x - 2.0 * (x - 1.0) * x* (x- 0.5); //2.0 is contrast. Range is 0.0 to 2.0
  54. #endif
  55. // -- Curve 4 --
  56. #if Curves_formula == 4
  57. x = (1.0524 * exp(6.0 * x) - 1.05248) / (20.0855 + exp(6.0 * x)); //exp formula
  58. #endif
  59. // -- Curve 5 --
  60. #if Curves_formula == 5
  61. //x = 0.5 * (x + 3.0 * x * x - 2.0 * x * x * x); //a simplified catmull-rom (0,0,1,1) - btw smoothstep can also be expressed as a simplified catmull-rom using (1,0,1,0)
  62. //x = (0.5 * x) + (1.5 -x) * x*x; //estrin form - faster version
  63. x = x * (x * (1.5-x) + 0.5); //horner form - fastest version
  64. Curves_contrast_blend = Curves_contrast * 2.0; //I multiply by two to give it a strength closer to the other curves.
  65. #endif
  66. // -- Curve 6 --
  67. #if Curves_formula == 6
  68. x = x*x*x*(x*(x*6.0 - 15.0) + 10.0); //Perlins smootherstep
  69. #endif
  70. // -- Curve 7 --
  71. #if Curves_formula == 7
  72. //x = ((x-0.5) / ((0.5/(4.0/3.0)) + abs((x-0.5)*1.25))) + 0.5;
  73. x = x - 0.5;
  74. x = x / ((abs(x)*1.25) + 0.375 ) + 0.5;
  75. //x = ( (x-0.5) / ((abs(x-0.5)*1.25) + (0.5/(4.0/3.0))) ) + 0.5;
  76. #endif
  77. // -- Curve 8 --
  78. #if Curves_formula == 8
  79. x = (x * (x * (x * (x * (x * (x * (1.6 * x - 7.2) + 10.8) - 4.2) - 3.6) + 2.7) - 1.8) + 2.7) * x * x; //Techicolor Cinestyle - almost identical to curve 1
  80. #endif
  81. // -- Curve 9 --
  82. #if Curves_formula == 9
  83. x = -0.5 * (x*2.0-1.0) * (abs(x*2.0-1.0)-2.0) + 0.5; //parabola
  84. #endif
  85. // -- Curve 10 --
  86. #if Curves_formula == 10 //Half-circles
  87. #if Curves_mode == 0
  88. float xstep = step(x,0.5);
  89. float xstep_shift = (xstep - 0.5);
  90. float shifted_x = x + xstep_shift;
  91. #else
  92. float3 xstep = step(x,0.5);
  93. float3 xstep_shift = (xstep - 0.5);
  94. float3 shifted_x = x + xstep_shift;
  95. #endif
  96. x = abs(xstep - sqrt(-shifted_x * shifted_x + shifted_x) ) - xstep_shift;
  97. //x = abs(step(x,0.5)-sqrt(-(x+step(x,0.5)-0.5)*(x+step(x,0.5)-0.5)+(x+step(x,0.5)-0.5)))-(step(x,0.5)-0.5); //single line version of the above
  98. //x = 0.5 + (sign(x-0.5)) * sqrt(0.25-(x-trunc(x*2))*(x-trunc(x*2))); //worse
  99. /* // if/else - even worse
  100. if (x-0.5)
  101. x = 0.5-sqrt(0.25-x*x);
  102. else
  103. x = 0.5+sqrt(0.25-(x-1)*(x-1));
  104. */
  105. //x = (abs(step(0.5,x)-clamp( 1-sqrt(1-abs(step(0.5,x)- frac(x*2%1)) * abs(step(0.5,x)- frac(x*2%1))),0 ,1))+ step(0.5,x) )*0.5; //worst so far
  106. //TODO: Check if I could use an abs split instead of step. It might be more efficient
  107. Curves_contrast_blend = Curves_contrast * 0.5; //I divide by two to give it a strength closer to the other curves.
  108. #endif
  109. // -- Curve 11 --
  110. #if Curves_formula == 11 //Cubic catmull
  111. float a = 1.00; //control point 1
  112. float b = 0.00; //start point
  113. float c = 1.00; //endpoint
  114. float d = 0.20; //control point 2
  115. x = 0.5 * ((-a + 3*b -3*c + d)*x*x*x + (2*a -5*b + 4*c - d)*x*x + (-a+c)*x + 2*b); //A customizable cubic catmull-rom spline
  116. #endif
  117. // -- Curve 12 --
  118. #if Curves_formula == 12 //Cubic Bezier spline
  119. float a = 0.00; //start point
  120. float b = 0.00; //control point 1
  121. float c = 1.00; //control point 2
  122. float d = 1.00; //endpoint
  123. float r = (1-x);
  124. float r2 = r*r;
  125. float r3 = r2 * r;
  126. float x2 = x*x;
  127. float x3 = x2*x;
  128. //x = dot(float4(a,b,c,d),float4(r3,3*r2*x,3*r*x2,x3));
  129. //x = a * r*r*r + r * (3 * b * r * x + 3 * c * x*x) + d * x*x*x;
  130. //x = a*(1-x)*(1-x)*(1-x) +(1-x) * (3*b * (1-x) * x + 3 * c * x*x) + d * x*x*x;
  131. x = a*(1-x)*(1-x)*(1-x) + 3*b*(1-x)*(1-x)*x + 3*c*(1-x)*x*x + d*x*x*x;
  132. #endif
  133. // -- Curve 13 --
  134. #if Curves_formula == 13 //Cubic Bezier spline - alternative implementation.
  135. float3 a = float3(0.00,0.00,0.00); //start point
  136. float3 b = float3(0.25,0.15,0.85); //control point 1
  137. float3 c = float3(0.75,0.85,0.15); //control point 2
  138. float3 d = float3(1.00,1.00,1.00); //endpoint
  139. float3 ab = lerp(a,b,x); // point between a and b
  140. float3 bc = lerp(b,c,x); // point between b and c
  141. float3 cd = lerp(c,d,x); // point between c and d
  142. float3 abbc = lerp(ab,bc,x); // point between ab and bc
  143. float3 bccd = lerp(bc,cd,x); // point between bc and cd
  144. float3 dest = lerp(abbc,bccd,x); // point on the bezier-curve
  145. x = dest;
  146. #endif
  147. // -- Curve 14 --
  148. #if Curves_formula == 14
  149. x = 1.0 / (1.0 + exp(-(x * 10.0 - 5.0))); //alternative exp formula
  150. #endif
  151. /*-----------------------------------------------------------.
  152. / Joining of Luma and Chroma /
  153. '-----------------------------------------------------------*/
  154. #if Curves_mode == 2 //Both Luma and Chroma
  155. float3 color = x; //if the curve should be applied to both Luma and Chroma
  156. colorInput.rgb = lerp(colorInput.rgb, color, Curves_contrast_blend); //Blend by Curves_contrast
  157. #elif Curves_mode == 1 //Only Chroma
  158. x = x * 2.0 - 1.0; //adjust the Chroma range back to -1 -> 1
  159. float3 color = luma + x; //Luma + Chroma
  160. colorInput.rgb = lerp(colorInput.rgb, color, Curves_contrast_blend); //Blend by Curves_contrast
  161. #else // Curves_mode == 0 //Only Luma
  162. x = lerp(luma, x, Curves_contrast_blend); //Blend by Curves_contrast
  163. colorInput.rgb = x + chroma; //Luma + Chroma
  164. #endif
  165. //Return the result
  166. return colorInput;
  167. }
  168. /*
  169. //Working on a proper catmull-rom spline
  170. float catmullrom(float t, float p0, float p1, float p2, float p3)
  171. {
  172. 0.5 * ( (2 * p1) + (-p0 + p2) * x + (2 * p0 - 5 * p1 + 4 * p2 - p3) * t * t + (-p0 + 3 * p1 - 3 * p2 + p3) * x * x * x );
  173. }
  174. //0.5 *( (2 * b) + (-a + c) * x + (2*a - 5*b + 4*c - d) * x*x +(-a + 3*b- 3*c + d) * x*x*x)
  175. //y = 1/2 *(x (-a *(x-1)^2+c *(-3 * x^2+4 * x+1)+d * x *(x-1))+b (3 * x^3-5 * x^2+2)) //reduce
  176. //a * x * (x * (1 - 0.5 * x) - 0.5) + b * ((1.5 * x - 2.5) * x*x + 1) + x * (x * (x * (0.5 * d - 1.5 * c) + 2 * c - 0.5 * d) + 0.5 * c) //horner
  177. */