levels.frag 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. ** Copyright (c) 2012, Romain Dura romain@shazbits.com
  3. **
  4. ** Permission to use, copy, modify, and/or distribute this software for any
  5. ** purpose with or without fee is hereby granted, provided that the above
  6. ** copyright notice and this permission notice appear in all copies.
  7. **
  8. ** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. ** WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. ** MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11. ** SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. ** WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. ** ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  14. ** IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /*
  17. ** Gamma correction
  18. ** Details: http://blog.mouaif.org/2009/01/22/photoshop-gamma-correction-shader/
  19. */
  20. /*
  21. ** Levels control (input (+gamma), output)
  22. ** Details: http://blog.mouaif.org/2009/01/28/levels-control-shader/
  23. */
  24. /*
  25. ** 2013 adapted for Extended View Toolkit by Marian Weger
  26. */
  27. #extension GL_ARB_texture_rectangle : enable
  28. uniform sampler2DRect tex0;
  29. uniform float gamma;
  30. uniform vec3 minInput;
  31. uniform vec3 maxInput;
  32. uniform vec3 minOutput;
  33. uniform vec3 maxOutput;
  34. void main(void)
  35. {
  36. vec2 pos = (gl_TextureMatrix[0] * gl_TexCoord[0]).st;
  37. vec4 color_old = texture2DRect(tex0, vec2(pos.x, pos.y));
  38. vec3 LevelsControlInputRange = min(max(color_old.rgb - minInput, vec3(0.0)) / (maxInput - minInput), vec3(1.0));
  39. vec3 LevelsControlInput = pow(LevelsControlInputRange, vec3(1.0 / gamma));
  40. vec3 LevelsControl = mix(minOutput, maxOutput, LevelsControlInput);
  41. vec4 color_new = vec4(LevelsControl, color_old.a);
  42. gl_FragColor = color_new;
  43. }