Debian Codegraph @

cvd_src/bayer.cxx

raw symbols SCIP
1 #include <cvd/colourspace_convert.h>
2 #include <cvd/colourspaces.h>
3 #include <string.h>
4 #if WIN32
5 #include <Winsock2.h>
6 #pragma comment(lib, "Ws2_32.lib")
7 #else
8 #include <arpa/inet.h>
9 #endif
10 //Written by Ethan
11 //Modified by Olaf :)
12
13 //It seems that the assumption is that cameras are big endian (network byte order).
14 //This is correct for firewire. (ER)
15
16 namespace CVD
17 {
18 namespace ColourSpace
19 {
20
21 template <class T>
22 struct read_host_byteorder
23 {
24 static const T& get(const T& from) { return from; }
25 };
26
27 template <class T>
28 struct read_net_byteorder;
29
30 template <>
31 struct read_net_byteorder<unsigned short>
32 {
33 static unsigned short get(const unsigned short& from)
34 {
35 return ntohs(from);
36 }
37 };
38
39 // RGRGRG
40 // GBGBGB
41 // RGRGRG
42 // GBGBGB
43 template <class T, class R>
44 struct bayer_sample_rggb
45 {
46 static inline void upper_left(T (*out)[3], const T* row, const T* next)
47 {
48 T row0 = R::get(row[0]);
49 T row1 = R::get(row[1]);
50 T row2 = R::get(row[2]);
51 T next0 = R::get(next[0]);
52 T next1 = R::get(next[1]);
53 out[0][0] = row0;
54 out[0][1] = static_cast<T>((row1 + next0) / 2);
55 out[0][2] = next1;
56 out[1][0] = static_cast<T>((row0 + row2) / 2);
57 out[1][1] = row1;
58 out[1][2] = next1;
59 }
60 static inline void upper_row(T (*out)[3], const T* row, const T* next)
61 {
62 T row_1 = R::get(row[-1]);
63 T row0 = R::get(row[0]);
64 T row1 = R::get(row[1]);
65 T row2 = R::get(row[2]);
66 T next_1 = R::get(next[-1]);
67 T next0 = R::get(next[0]);
68 T next1 = R::get(next[1]);
69 out[0][0] = row0;
70 out[0][1] = static_cast<T>((row_1 + row1 + next0) / 3);
71 out[0][2] = static_cast<T>((next_1 + next1) / 2);
72 out[1][0] = static_cast<T>((row0 + row2) / 2);
73 out[1][1] = row1;
74 out[1][2] = next1;
75 }
76 static inline void upper_right(T (*out)[3], const T* row, const T* next)
77 {
78 T row_1 = R::get(row[-1]);
79 T row0 = R::get(row[0]);
80 T row1 = R::get(row[1]);
81 T next_1 = R::get(next[-1]);
82 T next0 = R::get(next[0]);
83 T next1 = R::get(next[1]);
84 out[0][0] = row0;
85 out[0][1] = static_cast<T>((row_1 + row1 + next0) / 3);
86 out[0][2] = static_cast<T>((next_1 + next1) / 2);
87 out[1][0] = row0;
88 out[1][1] = row1;
89 out[1][2] = next1;
90 }
91
92 static inline void odd_left(T (*out)[3], const T* previous, const T* row, const T* next)
93 {
94 T prev0 = R::get(previous[0]);
95 T prev1 = R::get(previous[1]);
96 T prev2 = R::get(previous[2]);
97 T row0 = R::get(row[0]);
98 T row1 = R::get(row[1]);
99 T row2 = R::get(row[2]);
100 T next0 = R::get(next[0]);
101 T next1 = R::get(next[1]);
102 T next2 = R::get(next[2]);
103 out[0][0] = static_cast<T>((prev0 + next0) / 2);
104 out[0][1] = row0;
105 out[0][2] = row1;
106 out[1][0] = static_cast<T>((prev0 + next0 + prev2 + next2) / 4);
107 out[1][1] = static_cast<T>((row0 + row2 + prev1 + next1) / 4);
108 out[1][2] = row1;
109 }
110 static inline void odd_row(T (*out)[3], const T* previous, const T* row, const T* next)
111 {
112 T prev0 = R::get(previous[0]);
113 T prev1 = R::get(previous[1]);
114 T prev2 = R::get(previous[2]);
115 T row_1 = R::get(row[-1]);
116 T row0 = R::get(row[0]);
117 T row1 = R::get(row[1]);
118 T row2 = R::get(row[2]);
119 T next0 = R::get(next[0]);
120 T next1 = R::get(next[1]);
121 T next2 = R::get(next[2]);
122 out[0][0] = static_cast<T>((prev0 + next0) / 2);
123 out[0][1] = row0;
124 out[0][2] = static_cast<T>((row_1 + row1) / 2);
125 out[1][0] = static_cast<T>((prev0 + next0 + prev2 + next2) / 4);
126 out[1][1] = static_cast<T>((row0 + row2 + prev1 + next1) / 4);
127 out[1][2] = row1;
128 }
129 static inline void odd_right(T (*out)[3], const T* previous, const T* row, const T* next)
130 {
131 T prev0 = R::get(previous[0]);
132 T prev1 = R::get(previous[1]);
133 T row_1 = R::get(row[-1]);
134 T row0 = R::get(row[0]);
135 T row1 = R::get(row[1]);
136 T next0 = R::get(next[0]);
137 T next1 = R::get(next[1]);
138 out[0][0] = static_cast<T>((prev0 + next0) / 2);
139 out[0][1] = row0;
140 out[0][2] = static_cast<T>((row_1 + row1) / 2);
141 out[1][0] = static_cast<T>((prev0 + next0) / 2);
142 out[1][1] = static_cast<T>((row0 + prev1 + next1) / 3);
143 out[1][2] = row1;
144 }
145
146 static inline void even_left(T (*out)[3], const T* previous, const T* row, const T* next)
147 {
148 T prev0 = R::get(previous[0]);
149 T prev1 = R::get(previous[1]);
150 T row0 = R::get(row[0]);
151 T row1 = R::get(row[1]);
152 T row2 = R::get(row[2]);
153 T next0 = R::get(next[0]);
154 T next1 = R::get(next[1]);
155 out[0][0] = row0;
156 out[0][1] = static_cast<T>((row1 + prev0 + next0) / 3);
157 out[0][2] = static_cast<T>((prev1 + next1) / 2);
158 out[1][0] = static_cast<T>((row0 + row2) / 2);
159 out[1][1] = row1;
160 out[1][2] = out[0][2];
161 }
162 static inline void even_row(T (*out)[3], const T* previous, const T* row, const T* next)
163 {
164 T prev_1 = R::get(previous[-1]);
165 T prev0 = R::get(previous[0]);
166 T prev1 = R::get(previous[1]);
167 T row_1 = R::get(row[-1]);
168 T row0 = R::get(row[0]);
169 T row1 = R::get(row[1]);
170 T row2 = R::get(row[2]);
171 T next_1 = R::get(next[-1]);
172 T next0 = R::get(next[0]);
173 T next1 = R::get(next[1]);
174 out[0][0] = row0;
175 out[0][1] = static_cast<T>((row_1 + row1 + prev0 + next0) / 4);
176 out[0][2] = static_cast<T>((prev_1 + next_1 + prev1 + next1) / 4);
177 out[1][0] = static_cast<T>((row0 + row2) / 2);
178 out[1][1] = row1;
179 out[1][2] = static_cast<T>((prev1 + next1) / 2);
180 }
181 static inline void even_right(T (*out)[3], const T* previous, const T* row, const T* next)
182 {
183 T prev_1 = R::get(previous[-1]);
184 T prev0 = R::get(previous[0]);
185 T prev1 = R::get(previous[1]);
186 T row_1 = R::get(row[-1]);
187 T row0 = R::get(row[0]);
188 T row1 = R::get(row[1]);
189 T next_1 = R::get(next[-1]);
190 T next0 = R::get(next[0]);
191 T next1 = R::get(next[1]);
192 out[0][0] = row0;
193 out[0][1] = static_cast<T>((row_1 + row1 + prev0 + next0) / 4);
194 out[0][2] = static_cast<T>((prev_1 + next_1 + prev1 + next1) / 4);
195 out[1][0] = row0;
196 out[1][1] = row1;
197 out[1][2] = static_cast<T>((prev1 + next1) / 2);
198 }
199
200 static inline void lower_left(T (*out)[3], const T* previous, const T* row)
201 {
202 T prev0 = R::get(previous[0]);
203 T prev1 = R::get(previous[1]);
204 T prev2 = R::get(previous[2]);
205 T row0 = R::get(row[0]);
206 T row1 = R::get(row[1]);
207 T row2 = R::get(row[2]);
208 out[0][0] = prev0;
209 out[0][1] = row0;
210 out[0][2] = row1;
211 out[1][0] = static_cast<T>((prev0 + prev2) / 2);
212 out[1][1] = static_cast<T>((row0 + row2 + prev1) / 3);
213 out[1][2] = row1;
214 }
215 static inline void lower_row(T (*out)[3], const T* previous, const T* row)
216 {
217 T prev0 = R::get(previous[0]);
218 T prev1 = R::get(previous[1]);
219 T prev2 = R::get(previous[2]);
220 T row_1 = R::get(row[-1]);
221 T row0 = R::get(row[0]);
222 T row1 = R::get(row[1]);
223 T row2 = R::get(row[2]);
224 out[0][0] = prev0;
225 out[0][1] = row0;
226 out[0][2] = static_cast<T>((row_1 + row1) / 2);
227 out[1][0] = static_cast<T>((prev0 + prev2) / 2);
228 out[1][1] = static_cast<T>((row0 + row2 + prev1) / 3);
229 out[1][2] = row1;
230 }
231 static inline void lower_right(T (*out)[3], const T* previous, const T* row)
232 {
233 T prev0 = R::get(previous[0]);
234 T prev1 = R::get(previous[1]);
235 T row_1 = R::get(row[-1]);
236 T row0 = R::get(row[0]);
237 T row1 = R::get(row[1]);
238 out[0][0] = prev0;
239 out[0][1] = row0;
240 out[0][2] = static_cast<T>((row_1 + row1) / 2);
241 out[1][0] = prev0;
242 out[1][1] = static_cast<T>((row0 + prev1) / 2);
243 out[1][2] = row1;
244 }
245 };
246
247 // BGBGBG
248 // GRGRGR
249 // BGBGBG
250 // GRGRGR
251 // swap red and blue
252 template <class T, class R>
253 struct bayer_sample_bggr
254 {
255 static inline void upper_left(T (*out)[3], const T* row, const T* next)
256 {
257 T row0 = R::get(row[0]);
258 T row1 = R::get(row[1]);
259 T row2 = R::get(row[2]);
260 T next0 = R::get(next[0]);
261 T next1 = R::get(next[1]);
262 out[0][2] = row0; ////////////////////////////////////////////////
263 out[0][1] = static_cast<T>((row1 + next0) / 2);
264 out[0][0] = next1;
265 out[1][2] = static_cast<T>((row0 + row2) / 2);
266 out[1][1] = row1;
267 out[1][0] = next1;
268 }
269 static inline void upper_row(T (*out)[3], const T* row, const T* next)
270 {
271 T row_1 = R::get(row[-1]);
272 T row0 = R::get(row[0]);
273 T row1 = R::get(row[1]);
274 T row2 = R::get(row[2]);
275 T next_1 = R::get(next[-1]);
276 T next0 = R::get(next[0]);
277 T next1 = R::get(next[1]);
278 out[0][2] = row0;
279 out[0][1] = static_cast<T>((row_1 + row1 + next0) / 3);
280 out[0][0] = static_cast<T>((next_1 + next1) / 2);
281 out[1][2] = static_cast<T>((row0 + row2) / 2);
282 out[1][1] = row1;
283 out[1][0] = next1;
284 }
285 static inline void upper_right(T (*out)[3], const T* row, const T* next)
286 {
287 T row_1 = R::get(row[-1]);
288 T row0 = R::get(row[0]);
289 T row1 = R::get(row[1]);
290 T next_1 = R::get(next[-1]);
291 T next0 = R::get(next[0]);
292 T next1 = R::get(next[1]);
293 out[0][2] = row0;
294 out[0][1] = static_cast<T>((row_1 + row1 + next0) / 3);
295 out[0][0] = static_cast<T>((next_1 + next1) / 2);
296 out[1][2] = row0;
297 out[1][1] = row1;
298 out[1][0] = next1;
299 }
300
301 static inline void odd_left(T (*out)[3], const T* previous, const T* row, const T* next)
302 {
303 T prev0 = R::get(previous[0]);
304 T prev1 = R::get(previous[1]);
305 T prev2 = R::get(previous[2]);
306 T row0 = R::get(row[0]);
307 T row1 = R::get(row[1]);
308 T row2 = R::get(row[2]);
309 T next0 = R::get(next[0]);
310 T next1 = R::get(next[1]);
311 T next2 = R::get(next[2]);
312 out[0][2] = static_cast<T>((prev0 + next0) / 2);
313 out[0][1] = row0;
314 out[0][0] = row1;
315 out[1][2] = static_cast<T>((prev0 + next0 + prev2 + next2) / 4);
316 out[1][1] = static_cast<T>((row0 + row2 + prev1 + next1) / 4);
317 out[1][0] = row1;
318 }
319 static inline void odd_row(T (*out)[3], const T* previous, const T* row, const T* next)
320 {
321 T prev0 = R::get(previous[0]);
322 T prev1 = R::get(previous[1]);
323 T prev2 = R::get(previous[2]);
324 T row_1 = R::get(row[-1]);
325 T row0 = R::get(row[0]);
326 T row1 = R::get(row[1]);
327 T row2 = R::get(row[2]);
328 T next0 = R::get(next[0]);
329 T next1 = R::get(next[1]);
330 T next2 = R::get(next[2]);
331 out[0][2] = static_cast<T>((prev0 + next0) / 2);
332 out[0][1] = row0;
333 out[0][0] = static_cast<T>((row_1 + row1) / 2);
334 out[1][2] = static_cast<T>((prev0 + next0 + prev2 + next2) / 4);
335 out[1][1] = static_cast<T>((row0 + row2 + prev1 + next1) / 4);
336 out[1][0] = row1;
337 }
338 static inline void odd_right(T (*out)[3], const T* previous, const T* row, const T* next)
339 {
340 T prev0 = R::get(previous[0]);
341 T prev1 = R::get(previous[1]);
342 T row_1 = R::get(row[-1]);
343 T row0 = R::get(row[0]);
344 T row1 = R::get(row[1]);
345 T next0 = R::get(next[0]);
346 T next1 = R::get(next[1]);
347 out[0][2] = static_cast<T>((prev0 + next0) / 2);
348 out[0][1] = row0;
349 out[0][0] = static_cast<T>((row_1 + row1) / 2);
350 out[1][2] = static_cast<T>((prev0 + next0) / 2);
351 out[1][1] = static_cast<T>((row0 + prev1 + next1) / 3);
352 out[1][0] = row1;
353 }
354
355 static inline void even_left(T (*out)[3], const T* previous, const T* row, const T* next)
356 {
357 T prev0 = R::get(previous[0]);
358 T prev1 = R::get(previous[1]);
359 T row0 = R::get(row[0]);
360 T row1 = R::get(row[1]);
361 T row2 = R::get(row[2]);
362 T next0 = R::get(next[0]);
363 T next1 = R::get(next[1]);
364 out[0][2] = row0;
365 out[0][1] = static_cast<T>((row1 + prev0 + next0) / 3);
366 out[0][0] = static_cast<T>((prev1 + next1) / 2);
367 out[1][2] = static_cast<T>((row0 + row2) / 2);
368 out[1][1] = row1;
369 out[1][0] = out[0][0];
370 }
371 static inline void even_row(T (*out)[3], const T* previous, const T* row, const T* next)
372 {
373 T prev_1 = R::get(previous[-1]);
374 T prev0 = R::get(previous[0]);
375 T prev1 = R::get(previous[1]);
376 T row_1 = R::get(row[-1]);
377 T row0 = R::get(row[0]);
378 T row1 = R::get(row[1]);
379 T row2 = R::get(row[2]);
380 T next_1 = R::get(next[-1]);
381 T next0 = R::get(next[0]);
382 T next1 = R::get(next[1]);
383 out[0][2] = row0;
384 out[0][1] = static_cast<T>((row_1 + row1 + prev0 + next0) / 4);
385 out[0][0] = static_cast<T>((prev_1 + next_1 + prev1 + next1) / 4);
386 out[1][2] = static_cast<T>((row0 + row2) / 2);
387 out[1][1] = row1;
388 out[1][0] = static_cast<T>((prev1 + next1) / 2);
389 }
390 static inline void even_right(T (*out)[3], const T* previous, const T* row, const T* next)
391 {
392 T prev_1 = R::get(previous[-1]);
393 T prev0 = R::get(previous[0]);
394 T prev1 = R::get(previous[1]);
395 T row_1 = R::get(row[-1]);
396 T row0 = R::get(row[0]);
397 T row1 = R::get(row[1]);
398 T next_1 = R::get(next[-1]);
399 T next0 = R::get(next[0]);
400 T next1 = R::get(next[1]);
401 out[0][2] = row0;
402 out[0][1] = static_cast<T>((row_1 + row1 + prev0 + next0) / 4);
403 out[0][0] = static_cast<T>((prev_1 + next_1 + prev1 + next1) / 4);
404 out[1][2] = row0;
405 out[1][1] = row1;
406 out[1][0] = static_cast<T>((prev1 + next1) / 2);
407 }
408
409 static inline void lower_left(T (*out)[3], const T* previous, const T* row)
410 {
411 T prev0 = R::get(previous[0]);
412 T prev1 = R::get(previous[1]);
413 T prev2 = R::get(previous[2]);
414 T row0 = R::get(row[0]);
415 T row1 = R::get(row[1]);
416 T row2 = R::get(row[2]);
417 out[0][2] = prev0;
418 out[0][1] = row0;
419 out[0][0] = row1;
420 out[1][2] = static_cast<T>((prev0 + prev2) / 2);
421 out[1][1] = static_cast<T>((row0 + row2 + prev1) / 3);
422 out[1][0] = row1;
423 }
424 static inline void lower_row(T (*out)[3], const T* previous, const T* row)
425 {
426 T prev0 = R::get(previous[0]);
427 T prev1 = R::get(previous[1]);
428 T prev2 = R::get(previous[2]);
429 T row_1 = R::get(row[-1]);
430 T row0 = R::get(row[0]);
431 T row1 = R::get(row[1]);
432 T row2 = R::get(row[2]);
433 out[0][2] = prev0;
434 out[0][1] = row0;
435 out[0][0] = static_cast<T>((row_1 + row1) / 2);
436 out[1][2] = static_cast<T>((prev0 + prev2) / 2);
437 out[1][1] = static_cast<T>((row0 + row2 + prev1) / 3);
438 out[1][0] = row1;
439 }
440 static inline void lower_right(T (*out)[3], const T* previous, const T* row)
441 {
442 T prev0 = R::get(previous[0]);
443 T prev1 = R::get(previous[1]);
444 T row_1 = R::get(row[-1]);
445 T row0 = R::get(row[0]);
446 T row1 = R::get(row[1]);
447 out[0][2] = prev0;
448 out[0][1] = row0;
449 out[0][0] = static_cast<T>((row_1 + row1) / 2);
450 out[1][2] = prev0;
451 out[1][1] = static_cast<T>((row0 + prev1) / 2);
452 out[1][0] = row1;
453 }
454 };
455
456 // GBGBGB
457 // RGRGRG
458 // GBGBGB
459 // RGRGRG
460 template <class T, class R>
461 struct bayer_sample_gbrg
462 {
463 static inline void upper_left(T (*out)[3], const T* row, const T* next)
464 {
465 T row0 = R::get(row[0]);
466 T row1 = R::get(row[1]);
467 T row2 = R::get(row[2]);
468 T next0 = R::get(next[0]);
469 T next1 = R::get(next[1]);
470 T next2 = R::get(next[2]);
471 out[0][0] = next0;
472 out[0][1] = row0;
473 out[0][2] = row1;
474 out[1][0] = static_cast<T>((next0 + next2) / 2);
475 out[1][1] = static_cast<T>((row0 + row2 + next1) / 3);
476 out[1][2] = row1;
477 }
478 static inline void upper_row(T (*out)[3], const T* row, const T* next)
479 {
480 T row_1 = R::get(row[-1]);
481 T row0 = R::get(row[0]);
482 T row1 = R::get(row[1]);
483 T row2 = R::get(row[2]);
484 T next0 = R::get(next[0]);
485 T next1 = R::get(next[1]);
486 T next2 = R::get(next[2]);
487 out[0][0] = next0;
488 out[0][1] = row0;
489 out[0][2] = static_cast<T>((row_1 + row1) / 2);
490 out[1][0] = static_cast<T>((next0 + next2) / 2);
491 out[1][1] = static_cast<T>((row0 + row2 + next1) / 3);
492 out[1][2] = row1;
493 }
494 static inline void upper_right(T (*out)[3], const T* row, const T* next)
495 {
496 T row_1 = R::get(row[-1]);
497 T row0 = R::get(row[0]);
498 T row1 = R::get(row[1]);
499 T next0 = R::get(next[0]);
500 T next1 = R::get(next[1]);
501 out[0][0] = next0;
502 out[0][1] = row0;
503 out[0][2] = static_cast<T>((row_1 + row1) / 2);
504 out[1][0] = next0;
505 out[1][1] = static_cast<T>((row0 + next1) / 2);
506 out[1][2] = row1;
507 }
508
509 static inline void odd_left(T (*out)[3], const T* previous, const T* row, const T* next)
510 {
511 bayer_sample_rggb<T, R>::even_left(out, previous, row, next);
512 }
513 static inline void odd_row(T (*out)[3], const T* previous, const T* row, const T* next)
514 {
515 bayer_sample_rggb<T, R>::even_row(out, previous, row, next);
516 }
517 static inline void odd_right(T (*out)[3], const T* previous, const T* row, const T* next)
518 {
519 bayer_sample_rggb<T, R>::even_right(out, previous, row, next);
520 }
521
522 static inline void even_left(T (*out)[3], const T* previous, const T* row, const T* next)
523 {
524 bayer_sample_rggb<T, R>::odd_left(out, previous, row, next);
525 }
526 static inline void even_row(T (*out)[3], const T* previous, const T* row, const T* next)
527 {
528 bayer_sample_rggb<T, R>::odd_row(out, previous, row, next);
529 }
530 static inline void even_right(T (*out)[3], const T* previous, const T* row, const T* next)
531 {
532 bayer_sample_rggb<T, R>::odd_right(out, previous, row, next);
533 }
534
535 static inline void lower_left(T (*out)[3], const T* previous, const T* row)
536 {
537 T prev0 = R::get(previous[0]);
538 T prev1 = R::get(previous[1]);
539 T row0 = R::get(row[0]);
540 T row1 = R::get(row[1]);
541 T row2 = R::get(row[2]);
542 out[0][0] = row0;
543 out[0][1] = static_cast<T>((prev0 + row1) / 2);
544 out[0][2] = prev1;
545 out[1][0] = static_cast<T>((row0 + row2) / 2);
546 out[1][1] = row1;
547 out[1][2] = prev1;
548 }
549 static inline void lower_row(T (*out)[3], const T* previous, const T* row)
550 {
551 T prev_1 = R::get(previous[-1]);
552 T prev0 = R::get(previous[0]);
553 T prev1 = R::get(previous[1]);
554 T row_1 = R::get(row[-1]);
555 T row0 = R::get(row[0]);
556 T row1 = R::get(row[1]);
557 T row2 = R::get(row[2]);
558 out[0][0] = row0;
559 out[0][1] = static_cast<T>((row_1 + prev0 + row1) / 3);
560 out[0][2] = static_cast<T>((prev_1 + prev1) / 2);
561 out[1][0] = static_cast<T>((row0 + row2) / 2);
562 out[1][1] = row1;
563 out[1][2] = prev1;
564 }
565 static inline void lower_right(T (*out)[3], const T* previous, const T* row)
566 {
567 T prev_1 = R::get(previous[-1]);
568 T prev0 = R::get(previous[0]);
569 T prev1 = R::get(previous[1]);
570 T row_1 = R::get(row[-1]);
571 T row0 = R::get(row[0]);
572 T row1 = R::get(row[1]);
573 out[0][0] = row0;
574 out[0][1] = static_cast<T>((row_1 + prev0 + row1) / 3);
575 out[0][2] = static_cast<T>((prev_1 + prev1) / 2);
576 out[1][0] = row0;
577 out[1][1] = row1;
578 out[1][2] = prev1;
579 }
580 };
581
582 /*
583 T prev_1 = R::get(previous[-1]);
584 T prev0 = R::get(previous[0]);
585 T prev1 = R::get(previous[1]);
586 T prev2 = R::get(previous[2]);
587 T row_1 = R::get(row[-1]);
588 T row0 = R::get(row[0]);
589 T row1 = R::get(row[1]);
590 T row2 = R::get(row[2]);
591 T next_1 = R::get(next[-1]);
592 T next0 = R::get(next[0]);
593 T next1 = R::get(next[1]);
594 T next2 = R::get(next[2]);
595 */
596 // GRGRGR
597 // BGBGBG
598 // GRGRGR
599 // BGBGBG
600 template <class T, class R>
601 struct bayer_sample_grbg
602 {
603 static inline void upper_left(T (*out)[3], const T* row, const T* next)
604 {
605 T row0 = R::get(row[0]);
606 T row1 = R::get(row[1]);
607 T row2 = R::get(row[2]);
608 T next0 = R::get(next[0]);
609 T next1 = R::get(next[1]);
610 T next2 = R::get(next[2]);
611 out[0][2] = next0;
612 out[0][1] = row0;
613 out[0][0] = row1;
614 out[1][2] = static_cast<T>((next0 + next2) / 2);
615 out[1][1] = static_cast<T>((row0 + row2 + next1) / 3);
616 out[1][0] = row1;
617 }
618 static inline void upper_row(T (*out)[3], const T* row, const T* next)
619 {
620 T row_1 = R::get(row[-1]);
621 T row0 = R::get(row[0]);
622 T row1 = R::get(row[1]);
623 T row2 = R::get(row[2]);
624 T next0 = R::get(next[0]);
625 T next1 = R::get(next[1]);
626 T next2 = R::get(next[2]);
627 out[0][2] = next0;
628 out[0][1] = row0;
629 out[0][0] = static_cast<T>((row_1 + row1) / 2);
630 out[1][2] = static_cast<T>((next0 + next2) / 2);
631 out[1][1] = static_cast<T>((row0 + row2 + next1) / 3);
632 out[1][0] = row1;
633 }
634 static inline void upper_right(T (*out)[3], const T* row, const T* next)
635 {
636 T row_1 = R::get(row[-1]);
637 T row0 = R::get(row[0]);
638 T row1 = R::get(row[1]);
639 T next0 = R::get(next[0]);
640 T next1 = R::get(next[1]);
641 out[0][2] = next0;
642 out[0][1] = row0;
643 out[0][0] = static_cast<T>((row_1 + row1) / 2);
644 out[1][2] = next0;
645 out[1][1] = static_cast<T>((row0 + next1) / 2);
646 out[1][0] = row1;
647 }
648
649 static inline void odd_left(T (*out)[3], const T* previous, const T* row, const T* next)
650 {
651 bayer_sample_bggr<T, R>::even_left(out, previous, row, next);
652 }
653 static inline void odd_row(T (*out)[3], const T* previous, const T* row, const T* next)
654 {
655 bayer_sample_bggr<T, R>::even_row(out, previous, row, next);
656 }
657 static inline void odd_right(T (*out)[3], const T* previous, const T* row, const T* next)
658 {
659 bayer_sample_bggr<T, R>::even_right(out, previous, row, next);
660 }
661
662 static inline void even_left(T (*out)[3], const T* previous, const T* row, const T* next)
663 {
664 bayer_sample_bggr<T, R>::odd_left(out, previous, row, next);
665 }
666 static inline void even_row(T (*out)[3], const T* previous, const T* row, const T* next)
667 {
668 bayer_sample_bggr<T, R>::odd_row(out, previous, row, next);
669 }
670 static inline void even_right(T (*out)[3], const T* previous, const T* row, const T* next)
671 {
672 bayer_sample_bggr<T, R>::odd_right(out, previous, row, next);
673 }
674
675 static inline void lower_left(T (*out)[3], const T* previous, const T* row)
676 {
677 T prev0 = R::get(previous[0]);
678 T prev1 = R::get(previous[1]);
679 T row0 = R::get(row[0]);
680 T row1 = R::get(row[1]);
681 T row2 = R::get(row[2]);
682 out[0][2] = row0;
683 out[0][1] = static_cast<T>((prev0 + row1) / 2);
684 out[0][0] = prev1;
685 out[1][2] = static_cast<T>((row0 + row2) / 2);
686 out[1][1] = row1;
687 out[1][0] = prev1;
688 }
689 static inline void lower_row(T (*out)[3], const T* previous, const T* row)
690 {
691 T prev_1 = R::get(previous[-1]);
692 T prev0 = R::get(previous[0]);
693 T prev1 = R::get(previous[1]);
694 T row_1 = R::get(row[-1]);
695 T row0 = R::get(row[0]);
696 T row1 = R::get(row[1]);
697 T row2 = R::get(row[2]);
698 out[0][2] = row0;
699 out[0][1] = static_cast<T>((row_1 + prev0 + row1) / 3);
700 out[0][0] = static_cast<T>((prev_1 + prev1) / 2);
701 out[1][2] = static_cast<T>((row0 + row2) / 2);
702 out[1][1] = row1;
703 out[1][0] = prev1;
704 }
705 static inline void lower_right(T (*out)[3], const T* previous, const T* row)
706 {
707 T prev_1 = R::get(previous[-1]);
708 T prev0 = R::get(previous[0]);
709 T prev1 = R::get(previous[1]);
710 T row_1 = R::get(row[-1]);
711 T row0 = R::get(row[0]);
712 T row1 = R::get(row[1]);
713 out[0][2] = row0;
714 out[0][1] = static_cast<T>((row_1 + prev0 + row1) / 3);
715 out[0][0] = static_cast<T>((prev_1 + prev1) / 2);
716 out[1][2] = row0;
717 out[1][1] = row1;
718 out[1][0] = prev1;
719 }
720 };
721
722 // TYPE is 0, 1, 2, 3 corresponding to the layouts RGGB = 0, GBRG = 1, GRBG = 2, BGGR = 3
723 //template <typename T, class SAMPLER>
724 //void bayer_to_rgb(const T* bggr, T* rgb, unsigned int width, unsigned int height)
725 template <typename T, class SAMPLER>
726 void bayer_to_rgb(const T* bggr, int bggr_stride, T* rgb, int rgb_stride, unsigned int width, unsigned int height)
727 {
728 int rgb_inc = rgb_stride - width;
729 int bggr_inc = bggr_stride - width;
730
731 const T* row = bggr;
732 const T* next = bggr + width;
733 const T* prev = bggr;
734 const unsigned int midcount = (width - 4) / 2;
735
736 T(*out)
737 [3] = (T(*)[3])rgb;
738
739 SAMPLER::upper_left(out, row, next);
740 out += 2;
741 row += 2;
742 next += 2;
743 for(unsigned int j = 0; j < midcount; ++j)
744 {
745 SAMPLER::upper_row(out, row, next);
746 out += 2;
747 row += 2;
748 next += 2;
749 }
750 SAMPLER::upper_right(out, row, next);
751
752 out += 2 + rgb_inc;
753 row += 2 + bggr_inc;
754 next += 2 + bggr_inc;
755
756 // Middle rows
757 for(unsigned int i = 1; i < height - 1; i += 2)
758 {
759 // odd row
760 SAMPLER::odd_left(out, prev, row, next);
761 out += 2;
762 prev += 2;
763 row += 2;
764 next += 2;
765 for(unsigned int j = 0; j < midcount; ++j)
766 {
767 SAMPLER::odd_row(out, prev, row, next);
768 out += 2;
769 prev += 2;
770 row += 2;
771 next += 2;
772 }
773 SAMPLER::odd_right(out, prev, row, next);
774
775 out += 2 + rgb_inc;
776 row += 2 + bggr_inc;
777 next += 2 + bggr_inc;
778 prev += 2 + bggr_inc;
779
780 // even row
781 SAMPLER::even_left(out, prev, row, next);
782 out += 2;
783 prev += 2;
784 row += 2;
785 next += 2;
786 for(unsigned int j = 0; j < midcount; ++j)
787 {
788 SAMPLER::even_row(out, prev, row, next);
789 out += 2;
790 prev += 2;
791 row += 2;
792 next += 2;
793 }
794 SAMPLER::even_right(out, prev, row, next);
795
796 out += 2 + rgb_inc;
797 row += 2 + bggr_inc;
798 next += 2 + bggr_inc;
799 prev += 2 + bggr_inc;
800 }
801 // last row
802 SAMPLER::lower_left(out, prev, row);
803 out += 2;
804 prev += 2;
805 row += 2;
806 for(unsigned int j = 0; j < midcount; ++j)
807 {
808 SAMPLER::lower_row(out, prev, row);
809 out += 2;
810 prev += 2;
811 row += 2;
812 }
813 SAMPLER::lower_right(out, prev, row);
814 }
815
816 template <class T>
817 inline T cie(T* c) { return static_cast<T>((c[0] * 77 + c[1] * 150 + c[2] * 29) >> 8); }
818
819 // TYPE is 0, 1, 2, 3 corresponding to the layouts RGGB = 0, GBRG = 1, GRBG = 2, BGGR = 3
820 template <class T, class SAMPLER>
821 void bayer_to_grey(const T* bggr, int bggr_stride, T* grey, int grey_stride, unsigned int width, unsigned int height)
822 {
823 int grey_inc = grey_stride - width;
824 int bggr_inc = bggr_stride - width;
825
826 const T* row = bggr;
827 const T* next = bggr + width;
828 const T* prev = bggr;
829 const unsigned int midcount = (width - 4) / 2;
830
831 T out[2][3];
832
833 SAMPLER::upper_left(out, row, next);
834 grey[0] = cie(out[0]);
835 grey[1] = cie(out[1]);
836 grey += 2;
837 row += 2;
838 next += 2;
839 for(unsigned int j = 0; j < midcount; ++j)
840 {
841 SAMPLER::upper_row(out, row, next);
842 grey[0] = cie(out[0]);
843 grey[1] = cie(out[1]);
844 grey += 2;
845 row += 2;
846 next += 2;
847 }
848 SAMPLER::upper_right(out, row, next);
849 grey[0] = cie(out[0]);
850 grey[1] = cie(out[1]);
851
852 grey += 2 + grey_inc;
853 row += 2 + bggr_inc;
854 next += 2 + bggr_inc;
855
856 // Middle rows
857 for(unsigned int i = 1; i < height - 1; i += 2)
858 {
859
860 // odd row
861 SAMPLER::odd_left(out, prev, row, next);
862 grey[0] = cie(out[0]);
863 grey[1] = cie(out[1]);
864 grey += 2;
865 prev += 2;
866 row += 2;
867 next += 2;
868 for(unsigned int j = 0; j < midcount; ++j)
869 {
870 SAMPLER::odd_row(out, prev, row, next);
871 grey[0] = cie(out[0]);
872 grey[1] = cie(out[1]);
873 grey += 2;
874 prev += 2;
875 row += 2;
876 next += 2;
877 }
878
879 SAMPLER::odd_right(out, prev, row, next);
880 grey[0] = cie(out[0]);
881 grey[1] = cie(out[1]);
882
883 grey += 2 + grey_inc;
884 row += 2 + bggr_inc;
885 next += 2 + bggr_inc;
886 prev += 2 + bggr_inc;
887
888 // even row
889 SAMPLER::even_left(out, prev, row, next);
890 grey[0] = cie(out[0]);
891 grey[1] = cie(out[1]);
892 grey += 2;
893 prev += 2;
894 row += 2;
895 next += 2;
896 for(unsigned int j = 0; j < midcount; ++j)
897 {
898 SAMPLER::even_row(out, prev, row, next);
899 grey[0] = cie(out[0]);
900 grey[1] = cie(out[1]);
901 grey += 2;
902 prev += 2;
903 row += 2;
904 next += 2;
905 }
906
907 SAMPLER::even_right(out, prev, row, next);
908 grey[0] = cie(out[0]);
909 grey[1] = cie(out[1]);
910
911 grey += 2 + grey_inc;
912 row += 2 + bggr_inc;
913 next += 2 + bggr_inc;
914 prev += 2 + bggr_inc;
915 }
916
917 // last row
918 SAMPLER::lower_left(out, prev, row);
919 grey[0] = cie(out[0]);
920 grey[1] = cie(out[1]);
921 grey += 2;
922 row += 2;
923 prev += 2;
924 for(unsigned int j = 0; j < midcount; ++j)
925 {
926 grey[0] = cie(out[0]);
927 grey[1] = cie(out[1]);
928 grey += 2;
929 row += 2;
930 prev += 2;
931 }
932 SAMPLER::lower_right(out, prev, row);
933 }
934
935 template <class B, class P, template <class, class> class Sampler, template <class> class ByteOrder>
936 void convert_bayer_to_rgb(const BasicImage<B>& bayer, BasicImage<Rgb<P>>& out)
937 {
938 if(bayer.size() != out.size())
939 throw Exceptions::Image::IncompatibleImageSizes("convert_image");
940
941 bayer_to_rgb<P, Sampler<P, ByteOrder<P>>>(
942 reinterpret_cast<const P*>(bayer.data()), bayer.row_stride(),
943 reinterpret_cast<P*>(out.data()), out.row_stride(),
944 out.size().x, out.size().y);
945 }
946
947 template <class B, class P, template <class, class> class Sampler, template <class> class ByteOrder>
948 void convert_bayer_to_grey(const BasicImage<B>& bayer, BasicImage<P>& out)
949 {
950 if(bayer.size() != out.size())
951 throw Exceptions::Image::IncompatibleImageSizes("convert_image");
952
953 bayer_to_grey<P, Sampler<P, ByteOrder<P>>>(
954 reinterpret_cast<const P*>(bayer.data()), bayer.row_stride(),
955 out.data(), out.row_stride(),
956 out.size().x, out.size().y);
957 }
958
959 }
960
961 #define MAKE_CONVERT(Bayer, Suffix, Pixel, ByteOrder) \
962 template <> \
963 void convert_image(const BasicImage<bayer_##Bayer##Suffix>& from, BasicImage<Pixel>& to) \
964 { \
965 ColourSpace::convert_bayer_to_grey<bayer_##Bayer##Suffix, Pixel, ColourSpace::bayer_sample_##Bayer, ColourSpace::ByteOrder>(from, to); \
966 } \
967 template <> \
968 void convert_image(const BasicImage<bayer_##Bayer##Suffix>& from, BasicImage<Rgb<Pixel>>& to) \
969 { \
970 ColourSpace::convert_bayer_to_rgb<bayer_##Bayer##Suffix, Pixel, ColourSpace::bayer_sample_##Bayer, ColourSpace::ByteOrder>(from, to); \
971 }
972
973 #define MAKE_CONVERT4(X, Y, B) \
974 MAKE_CONVERT(bggr, X, Y, B) \
975 MAKE_CONVERT(gbrg, X, Y, B) \
976 MAKE_CONVERT(grbg, X, Y, B) \
977 MAKE_CONVERT(rggb, X, Y, B)
978
979 MAKE_CONVERT4(, byte, read_host_byteorder)
980 MAKE_CONVERT4(16, unsigned short, read_host_byteorder)
981 MAKE_CONVERT4(16be, unsigned short, read_net_byteorder)
982
983 }