debayer_packed.frag 1.7 KB

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