summaryrefslogtreecommitdiff
path: root/debayer.fs.glsl
blob: e6eace4357309598556cf08d8a0304d081ab3aa0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
uniform sampler2D u_sampler;
uniform highp mat4 u_coef_r;
uniform highp mat4 u_coef_g;
uniform highp mat4 u_coef_b;
varying highp vec2 v_position;

/* Evaluate power serial polynomial of degree 16, with 0-th order term 0 */
highp float polyeval(highp mat4 c, highp float x){
	highp float y = c[3][3];
	for(int i=4, j=3; 0<i; --i, j=4){ for(; 0<j; --j){
		y = y*x + c[i-1][j-1];
	}}
	return y*x;
}

void main(){
	/* Even and odd rows each contain alternating primitives of the
	 * Bayer pattern;
	 * RGRGRGRGRG
	 * GBGBGBGBGB
	 * RGRGRGRGRG
	 * GBGBGBGBGB
	 * ...
	 *
	 * The pixel data was uploaded to the texture in
	 * a way, that the even rows are in 0<x<0.5 and the odd rows
	 * in 0.5<x<1, i.e. we end up with
	 * RGRGRGRGRG GBGBGBGBGB
	 * RGRGRGRGRG GBGBGBGBGB
	 * ...
	 *
	 * Also OpenGL assumes texture origin lower left, so flip y here */
	lowp vec2 v_pos_even = vec2(0.5*(   v_position.x), 1.-v_position.y);
	lowp vec2 v_pos_odd  = vec2(0.5*(1.+v_position.x), 1.-v_position.y);
	highp vec4 rggb = vec4(texture2D(u_sampler, v_pos_even).wx, texture2D(u_sampler, v_pos_odd ).wx);
	highp vec3 rgb  = vec3(
	       polyeval(u_coef_r, rggb[0]),
	       mix(polyeval(u_coef_g, rggb[1]), polyeval(u_coef_g, rggb[2]), 0.5),
	       polyeval(u_coef_b, rggb[3]) );
	gl_FragColor = vec4(rgb, 1.);
}