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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
/* SPDX-License-Identifier: BSD-2-Clause */
/*
* Based on the code from http://jgt.akpeters.com/papers/McGuire08/
*
* Efficient, High-Quality Bayer Demosaic Filtering on GPUs
*
* Morgan McGuire
*
* This paper appears in issue Volume 13, Number 4.
* ---------------------------------------------------------
* Copyright (c) 2008, Morgan McGuire. All rights reserved.
*
*
* Modified by Linaro Ltd for 10/12-bit packed vs 8-bit raw Bayer format,
* and for simpler demosaic algorithm.
* Copyright (C) 2020, Linaro
*
* bayer_1x_packed.frag - Fragment shader code for raw Bayer 10-bit and 12-bit
* packed formats
*/
#ifdef GL_ES
precision mediump float;
#endif
/*
* These constants are used to select the bytes containing the HS part of
* the pixel value:
* BPP - bytes per pixel,
* THRESHOLD_L = fract(BPP) * 0.5 + 0.02
* THRESHOLD_H = 1.0 - fract(BPP) * 1.5 + 0.02
* Let X is the x coordinate in the texture measured in bytes (so that the
* range is from 0 to (stride_-1)) aligned on the nearest pixel.
* E.g. for RAW10P:
* -------------+-------------------+-------------------+--
* pixel No | 0 1 2 3 | 4 5 6 7 | ...
* -------------+-------------------+-------------------+--
* byte offset | 0 1 2 3 4 | 5 6 7 8 9 | ...
* -------------+-------------------+-------------------+--
* X | 0.0 1.25 2.5 3.75 | 5.0 6.25 7.5 8.75 | ...
* -------------+-------------------+-------------------+--
* If fract(X) < THRESHOLD_L then the previous byte contains the LS
* bits of the pixel values and needs to be skipped.
* If fract(X) > THRESHOLD_H then the next byte contains the LS bits
* of the pixel values and needs to be skipped.
*/
#if defined(RAW10P)
#define BPP 1.25
#define THRESHOLD_L 0.14
#define THRESHOLD_H 0.64
#elif defined(RAW12P)
#define BPP 1.5
#define THRESHOLD_L 0.27
#define THRESHOLD_H 0.27
#else
#error Invalid raw format
#endif
varying vec2 textureOut;
/* the texture size in pixels */
uniform vec2 tex_size;
uniform vec2 tex_step;
uniform vec2 tex_bayer_first_red;
uniform sampler2D tex_y;
void main(void)
{
vec3 rgb;
/*
* center_bytes holds the coordinates of the MS byte of the pixel
* being sampled on the [0, stride-1/height-1] range.
* center_pixel holds the coordinates of the pixel being sampled
* on the [0, width/height-1] range.
*/
vec2 center_bytes;
vec2 center_pixel;
/*
* x- and y-positions of the adjacent pixels on the [0, 1] range.
*/
vec2 xcoords;
vec2 ycoords;
/*
* The coordinates passed to the shader in textureOut may point
* to a place in between the pixels if the texture format doesn't
* match the image format. In particular, MIPI packed raw Bayer
* formats don't have a matching texture format.
* In this case align the coordinates to the left nearest pixel
* by hand.
*/
center_pixel = floor(textureOut * tex_size);
center_bytes.y = center_pixel.y;
/*
* Add a small number (a few mantissa's LSBs) to avoid float
* representation issues. Maybe paranoic.
*/
center_bytes.x = BPP * center_pixel.x + 0.02;
float fract_x = fract(center_bytes.x);
/*
* The below floor() call ensures that center_bytes.x points
* at one of the bytes representing the 8 higher bits of
* the pixel value, not at the byte containing the LS bits
* of the group of the pixels.
*/
center_bytes.x = floor(center_bytes.x);
center_bytes *= tex_step;
xcoords = center_bytes.x + vec2(-tex_step.x, tex_step.x);
ycoords = center_bytes.y + vec2(-tex_step.y, tex_step.y);
/*
* If xcoords[0] points at the byte containing the LS bits
* of the previous group of the pixels, move xcoords[0] one
* byte back.
*/
xcoords[0] += (fract_x < THRESHOLD_L) ? -tex_step.x : 0.0;
/*
* If xcoords[1] points at the byte containing the LS bits
|