luminance_av.glslf 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #version GLSL_VERSION
  2. #include <precision_statement.glslf>
  3. #include <std.glsl>
  4. uniform sampler2D u_input;
  5. /*==============================================================================
  6. SHADER INTERFACE
  7. ==============================================================================*/
  8. //------------------------------------------------------------------------------
  9. GLSL_OUT vec4 GLSL_OUT_FRAG_COLOR;
  10. /*==============================================================================
  11. MAIN
  12. ==============================================================================*/
  13. void main(void) {
  14. float luminance;
  15. float sum_lum = 0.0;
  16. float w = 0.025;
  17. float h = 0.025;
  18. //iterate over 400 pixels in the image
  19. // NOTE: Mac doesn't support iterations with float variables
  20. for (int i = 0; i < 20; i += 1) {
  21. h = 0.025;
  22. for (int j = 0; j < 20; j += 1) {
  23. // Avoid zero values as log will be -infinity
  24. luminance = max(GLSL_TEXTURE( u_input, vec2(w, h) ).r, 0.01);
  25. sum_lum += log(luminance);
  26. h += 0.05;
  27. }
  28. w += 0.05;
  29. }
  30. float average_lum = exp(sum_lum / 400.0);
  31. GLSL_OUT_FRAG_COLOR = vec4(vec3(average_lum), 1.0);
  32. }