-
Notifications
You must be signed in to change notification settings - Fork 1
/
demosaic.cpp
126 lines (110 loc) · 2.41 KB
/
demosaic.cpp
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
#include "demosaic.h"
// http://jp.mathworks.com/help/images/ref/demosaic.html
// ------
// grgrgr
// bgbgbg
// grgrgr
// bgbgbg
// ------
// +r+r+r
// ++++++
// +r+r+r
// ++++++
// ------
// g+g+g+
// +g+g+g
// g+g+g+
// +g+g+g
// ------
// ++++++
// b+b+b+
// ++++++
// b+b+b+
void demosaic_grbg(
const uint16_t* pSrc,
size_t width,
size_t height,
uint32_t* pDst
)
{
size_t offset0 = 0;
size_t offset1 = width;
size_t offset2 = width * 2;
const uint16_t* pUp = pSrc;
const uint16_t* pMi = pSrc + width;
const uint16_t* pLo = pMi + width;
uint32_t* pColor = pDst + width;
uint16_t r, g, b;
uint32_t r0, g0, b0;
uint16_t
ul, uc, ur, ur2,
ml, mc, mr, mr2,
ll, lc, lr, lr2
;
const size_t nShifts = 8;
// g r g r g r
// b g b g b g
// g r g r g r
// b g b g b g
for (size_t y=1; y<height-1; y+=2) {
ul = pUp[0]; uc = pUp[1]; ur = pUp[2];
ml = pMi[0]; mc = pMi[1]; mr = pMi[2];
ll = pLo[0]; lc = pLo[1]; lr = pLo[2];
for (size_t x=1; x<width-1; x+=2) {
r0 = uc + lc + 1;
r = r0 >> (1 + nShifts);
g = mc >> nShifts;
b = (ml + mr + 1) >> (1 + nShifts);
pColor[x] = r | (g << 8) | (b << 16);
ur2 = pUp[x + 2];
mr2 = pMi[x + 2];
lr2 = pLo[x + 2];
r = (r0 + ur2 + lr2 + 1) >> (2 + nShifts);
g = (mc + mr2 + 1) >> (1 + nShifts);
b = mr >> nShifts;
pColor[x + 1] = r | (g << 8) | (b << 16);
ul = ur;
ml = mr;
ll = lr;
uc = ur2;
mc = mr2;
lc = lr2;
ur = pUp[x + 3];
mr = pMi[x + 3];
lr = pLo[x + 3];
}
pUp += width;
pMi += width;
pLo += width;
pColor += width;
ul = pUp[0]; uc = pUp[1]; ur = pUp[2];
ml = pMi[0]; mc = pMi[1]; mr = pMi[2];
ll = pLo[0]; lc = pLo[1]; lr = pLo[2];
for (size_t x=1; x<width-1; x+=2) {
r = mc >> nShifts;
g = (uc + ml + mr + lc + 2) >> (2 + nShifts);
b = (ul + ur + ll + lr + 2) >> (2 + nShifts);
pColor[x] = r | (g << 8) | (b << 16);
ur2 = pUp[x + 2];
mr2 = pMi[x + 2];
lr2 = pLo[x + 2];
r = (mc + mr2 + 1) >> (1 + nShifts);
g = mr >> nShifts;
b = (ur + lr + 1) >> (1 + nShifts);
pColor[x + 1] = r | (g << 8) | (b << 16);
ul = ur;
ml = mr;
ll = lr;
uc = ur2;
mc = mr2;
lc = lr2;
ur = pUp[x + 3];
mr = pMi[x + 3];
lr = pLo[x + 3];
}
pUp += width;
pMi += width;
pLo += width;
pColor += width;
}
}