blob: 83386b1cfb12fe4445f48f45895eba8461c9c896 [file] [log] [blame]
Lun Dong32847242022-02-10 19:08:52 +00001#include "samples/risp4ml/common/utils.h"
2
3// int GetBayerIndex(int x, int y) {
4// // The Bayer pattern code defines which color is top left in the quad:
5// // +---+---+
6// // | R | Gr|
7// // +---+---+
8// // | Gb| B |
9// // +---+---+
10// return ((x & 1) + 2 * (y & 1));
11// }
12
13BayerIndex GetBayerIndex(BayerPattern bayerType, uint16_t x, uint16_t y) {
14 // The Bayer pattern code defines which color is top left in the quad:
15 // 0: +---+---+ 1: +---+---+ 2: +---+---+ 3: +---+---+
16 // | R | Gr| | Gr| R | | Gb| B | | B | Gb|
17 // +---+---+ +---+---+ +---+---+ +---+---+
18 // | Gb| B | | B | Gb| | R | Gr| | Gr| R |
19 // +---+---+ +---+---+ +---+---+ +---+---+
20 // pattern 0 is base pattern and other patterns are shifted versions of the
21 // base
22
23 // Patterns 1 and 3 shift in the x
24 uint16_t x_shift = (uint16_t)(bayerType == kGrbg || bayerType == kBggr);
25 // Patterns 2 and 3 shift in the y
26 uint16_t y_shift = (uint16_t)(bayerType == kGbrg || bayerType == kBggr);
27 return (BayerIndex)(((x + x_shift) & 1) + 2 * ((y + y_shift) & 1));
28}
29
30int BayerMirrorBoundary(int x, int size) {
31 if (x < 0)
32 return (-x + 2 * (-x & 0x1) - 2);
33 else if (x < size)
34 return x;
35 else
36 return 2 * size - x - 2 * ((x - size + 1) & 0x1);
37}