debayer.frag 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #ifdef GL_ES
  2. precision highp float;
  3. #endif
  4. uniform sampler2D texture;
  5. uniform mat3 color_matrix;
  6. uniform float inv_gamma;
  7. uniform float blacklevel;
  8. #ifdef BITS_10
  9. uniform float row_length;
  10. uniform float padding_ratio;
  11. #endif
  12. varying vec2 top_left_uv;
  13. varying vec2 top_right_uv;
  14. varying vec2 bottom_left_uv;
  15. varying vec2 bottom_right_uv;
  16. #ifdef BITS_10
  17. vec2
  18. skip_5th_pixel(vec2 uv)
  19. {
  20. vec2 new_uv = uv;
  21. new_uv.x *= 0.8;
  22. new_uv.x += floor(uv.x * row_length / 5.0) / row_length;
  23. // Crop out padding
  24. new_uv.x *= padding_ratio;
  25. return new_uv;
  26. }
  27. #endif
  28. void
  29. main()
  30. {
  31. // Note the coordinates for texture samples need to be a varying, as the
  32. // Mali-400 has this as a fast path allowing 32-bit floats. Otherwise
  33. // they end up as 16-bit floats and that's not accurate enough.
  34. #ifdef BITS_10
  35. vec4 samples = vec4(texture2D(texture, skip_5th_pixel(top_left_uv)).r,
  36. texture2D(texture, skip_5th_pixel(top_right_uv)).r,
  37. texture2D(texture, skip_5th_pixel(bottom_left_uv)).r,
  38. texture2D(texture, skip_5th_pixel(bottom_right_uv)).r);
  39. #else
  40. vec4 samples = vec4(texture2D(texture, top_left_uv).r,
  41. texture2D(texture, top_right_uv).r,
  42. texture2D(texture, bottom_left_uv).r,
  43. texture2D(texture, bottom_right_uv).r);
  44. #endif
  45. #if defined(CFA_BGGR)
  46. vec3 color = vec3(samples.w, (samples.y + samples.z) / 2.0, samples.x);
  47. #elif defined(CFA_GBRG)
  48. vec3 color = vec3(samples.z, (samples.x + samples.w) / 2.0, samples.y);
  49. #elif defined(CFA_GRBG)
  50. vec3 color = vec3(samples.y, (samples.x + samples.w) / 2.0, samples.z);
  51. #else
  52. vec3 color = vec3(samples.x, (samples.y + samples.z) / 2.0, samples.w);
  53. #endif
  54. color -= blacklevel;
  55. color *= color_matrix;
  56. vec3 gamma_color = pow(color, vec3(inv_gamma));
  57. gl_FragColor = vec4(gamma_color, 1);
  58. }