-
Notifications
You must be signed in to change notification settings - Fork 39
/
CFA.cpp
169 lines (162 loc) · 5.95 KB
/
CFA.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
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
#include "MyISP.h"
#include <math.h>
#include <string.h>
PixelRGB malvar(const char* is_color, float center, int y, int x, ImageRaw* img, uint8_t cfa_clip)
{
float r = 0, g = 0, b = 0;
if (strcmp(is_color, "r") == 0)
{
r = center;
g = 4 * img->at(y, x) - img->at(y - 2, x) - img->at(y, x - 2) - img->at(y + 2, x) - img->at(y, x + 2) +
2 * (img->at(y + 1, x) + img->at(y, x + 1) + img->at(y - 1, x) + img->at(y, x - 1));
b = 6 * img->at(y, x) - 3 * (img->at(y - 2, x) + img->at(y, x - 2) + img->at(y + 2, x) + img->at(y, x + 2)) / 2 +
2 * (img->at(y - 1, x - 1) + img->at(y - 1, x + 1) + img->at(y + 1, x - 1) + img->at(y + 1, x + 1));
g = g / 8;
b = b / 8;
}
if (strcmp(is_color, "gr") == 0)
{
r = 5 * img->at(y, x) - img->at(y, x - 2) - img->at(y - 1, x - 1) - img->at(y + 1, x - 1) - img->at(y - 1, x + 1) - img->at(y + 1, x + 1) - img->at(y, x + 2)
+ (img->at(y - 2, x) + img->at(y + 2, x)) / 2 + 4 * (img->at(y, x - 1) + img->at(y, x + 1));
g = center;
b = 5 * img->at(y, x) - img->at(y - 2, x) - img->at(y - 1, x - 1) - img->at(y - 1, x + 1) - img->at(y + 2, x) - img->at(y + 1, x - 1) - img->at(y + 1, x + 1)
+ (img->at(y, x - 2) + img->at(y, x + 2)) / 2 + 4 * (img->at(y - 1, x) + img->at(y + 1, x));
r = r / 8;
b = b / 8;
}
if (strcmp(is_color, "gb") == 0)
{
r = 5 * img->at(y, x) - img->at(y - 2, x) - img->at(y - 1, x - 1) - img->at(y - 1, x + 1) - img->at(y + 2, x) - img->at(y + 1, x - 1) - img->at(y + 1, x + 1)
+ (img->at(y, x - 2) + img->at(y, x + 2)) / 2 + 4 * (img->at(y - 1, x) + img->at(y + 1, x));
g = center;
b = 5 * img->at(y, x) - img->at(y, x - 2) - img->at(y - 1, x - 1) - img->at(y + 1, x - 1) - img->at(y - 1, x + 1) - img->at(y + 1, x + 1) - img->at(y, x + 2)
+ (img->at(y - 2, x) + img->at(y + 2, x)) / 2 + 4 * (img->at(y, x - 1) + img->at(y, x + 1));
r = r / 8;
b = b / 8;
}
if (strcmp(is_color, "b") == 0)
{
r = 6 * img->at(y, x) - 3 * (img->at(y - 2, x) + img->at(y, x - 2) + img->at(y + 2, x) + img->at(y, x + 2)) / 2
+ 2 * (img->at(y - 1, x - 1) + img->at(y - 1, x + 1) + img->at(y + 1, x - 1) + img->at(y + 1, x + 1));
g = 4 * img->at(y, x) - img->at(y - 2, x) - img->at(y, x - 2) - img->at(y + 2, x) - img->at(y, x + 2)
+ 2 * (img->at(y + 1, x) + img->at(y, x + 1) + img->at(y - 1, x) + img->at(y, x - 1));
b = center;
r = r / 8;
g = g / 8;
}
PixelRGB pixel;
if (r < 0)r = 0;
if (g < 0)g = 0;
if (b < 0)b = 0;
if (r > cfa_clip)r = cfa_clip;
if (g > cfa_clip)g = cfa_clip;
if (b > cfa_clip)b = cfa_clip;
pixel.R = r;
pixel.G = g;
pixel.B = b;
return pixel;
}
ImageRGB CFA(ImageRaw& img, DEMOSAICING_MODE cfa_mode, BAYER_PATTERN bayer_pattern, uint8_t cfa_clip)
{
ImageRaw* img_pad = new ImageRaw(img);
img_pad->padding(2, PADDING_MODE_REFLECT);
int raw_h = img.getHeight(), raw_w = img.getWidth();
ImageRGB cfa_img(raw_h, raw_w);
float r, gr, gb, b;
for (int y = 0; y < img_pad->getHeight() - 4 - 1; y += 2)
{
for (int x = 0; x < img_pad->getWidth() - 4 - 1; x += 2)
{
switch (bayer_pattern)
{
case BAYER_PATTERN_RGGB:
r = img_pad->at(y + 2, x + 2);
gr = img_pad->at(y + 2, x + 3);
gb = img_pad->at(y + 3, x + 2);
b = img_pad->at(y + 3, x + 3);
switch (cfa_mode)
{
case DEMOSAIC_MALVAR:
cfa_img.at(y, x) = malvar("r", r, y + 2, x + 2, img_pad, cfa_clip);
cfa_img.at(y, x + 1) = malvar("gr", gr, y + 2, x + 3, img_pad, cfa_clip);
cfa_img.at(y + 1, x) = malvar("gb", gb, y + 3, x + 2, img_pad, cfa_clip);
cfa_img.at(y + 1, x + 1) = malvar("b", b, y + 3, x + 3, img_pad, cfa_clip);
break;
case DEMOSAIC_UNKNOWN:
default:
TRACE_DEBUG_LOG_ERROR("Unknown Pattern:%s\n", cfa_mode);
throw "Unimplemented mode";
break;
}
break;
case BAYER_PATTERN_BGGR:
b = img_pad->at(y + 2, x + 2);
gb = img_pad->at(y + 2, x + 3);
gr = img_pad->at(y + 3, x + 2);
r = img_pad->at(y + 3, x + 3);
switch (cfa_mode)
{
case DEMOSAIC_MALVAR:
cfa_img.at(y, x) = malvar("b", b, y + 2, x + 2, img_pad, cfa_clip);
cfa_img.at(y, x + 1) = malvar("gb", gb, y + 2, x + 3, img_pad, cfa_clip);
cfa_img.at(y + 1, x) = malvar("gr", gr, y + 3, x + 2, img_pad, cfa_clip);
cfa_img.at(y + 1, x + 1) = malvar("r", r, y + 3, x + 3, img_pad, cfa_clip);
break;
case DEMOSAIC_UNKNOWN:
default:
TRACE_DEBUG_LOG_ERROR("Unknown Pattern:%s\n", cfa_mode);
throw "Unimplemented mode";
break;
}
break;
case BAYER_PATTERN_GBRG:
gb = img_pad->at(y + 2, x + 2);
b = img_pad->at(y + 2, x + 3);
r = img_pad->at(y + 3, x + 2);
gr = img_pad->at(y + 3, x + 3);
switch (cfa_mode)
{
case DEMOSAIC_MALVAR:
cfa_img.at(y, x) = malvar("gb", gb, y + 2, x + 2, img_pad, cfa_clip);
cfa_img.at(y, x + 1) = malvar("b", b, y + 2, x + 3, img_pad, cfa_clip);
cfa_img.at(y + 1, x) = malvar("r", r, y + 3, x + 2, img_pad, cfa_clip);
cfa_img.at(y + 1, x + 1) = malvar("gr", gr, y + 3, x + 3, img_pad, cfa_clip);
break;
case DEMOSAIC_UNKNOWN:
default:
TRACE_DEBUG_LOG_ERROR("Unknown Pattern:%s\n", cfa_mode);
throw "Unimplemented mode";
break;
}
break;
case BAYER_PATTERN_GRBG:
gr = img_pad->at(y + 2, x + 2);
r = img_pad->at(y + 2, x + 3);
b = img_pad->at(y + 3, x + 2);
gb = img_pad->at(y + 3, x + 3);
switch (cfa_mode)
{
case DEMOSAIC_MALVAR:
cfa_img.at(y, x) = malvar("gr", gr, y + 2, x + 2, img_pad, cfa_clip);
cfa_img.at(y, x + 1) = malvar("r", r, y + 2, x + 3, img_pad, cfa_clip);
cfa_img.at(y + 1, x) = malvar("b", b, y + 3, x + 2, img_pad, cfa_clip);
cfa_img.at(y + 1, x + 1) = malvar("gb", gb, y + 3, x + 3, img_pad, cfa_clip);
break;
case DEMOSAIC_UNKNOWN:
default:
TRACE_DEBUG_LOG_ERROR("Unknown Pattern:%s\n", cfa_mode);
throw "Unimplemented mode";
break;
}
break;
case BAYER_PATTERN_UNKNOWN:
default:
TRACE_DEBUG_LOG_ERROR("Unknown Pattern:%s\n", bayer_pattern);
throw "Unimplemented mode";
break;
}
}
}
delete img_pad;
return cfa_img;
}