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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
use decoders::*;
use decoders::cfa::*;
use imageops;
#[derive(Debug, Clone)]
pub struct RawImage {
pub make: String,
pub model: String,
pub clean_make: String,
pub clean_model: String,
pub width: usize,
pub height: usize,
pub cpp: usize,
pub wb_coeffs: [f32;4],
pub whitelevels: [u16;4],
pub blacklevels: [u16;4],
pub xyz_to_cam: [[f32;3];4],
pub cfa: CFA,
pub crops: [usize;4],
pub data: Vec<u16>,
}
#[derive(Debug, Clone)]
pub struct RGBImage {
pub width: usize,
pub height: usize,
pub data: Vec<f32>,
}
#[derive(Debug, Clone)]
pub struct SRGBImage {
pub width: usize,
pub height: usize,
pub data: Vec<u8>,
}
impl RawImage {
#[doc(hidden)] pub fn new(camera: &Camera, width: usize, height: usize, wb_coeffs: [f32;4], image: Vec<u16>) -> RawImage {
let blacks = if camera.blackareah.1 != 0 || camera.blackareav.1 != 0 {
let mut avg = [0 as f32; 4];
let mut count = [0 as f32; 4];
for row in camera.blackareah.0 .. camera.blackareah.0+camera.blackareah.1 {
for col in 0..width {
let color = camera.cfa.color_at(row,col);
avg[color] += image[row*width+col] as f32;
count[color] += 1.0;
}
}
for row in 0..height {
for col in camera.blackareav.0 .. camera.blackareav.0+camera.blackareav.1 {
let color = camera.cfa.color_at(row,col);
avg[color] += image[row*width+col] as f32;
count[color] += 1.0;
}
}
[(avg[0]/count[0]) as u16,
(avg[1]/count[1]) as u16,
(avg[2]/count[2]) as u16,
(avg[3]/count[3]) as u16]
} else {
camera.blacklevels
};
RawImage {
make: camera.make.clone(),
model: camera.model.clone(),
clean_make: camera.clean_make.clone(),
clean_model: camera.clean_model.clone(),
width: width,
height: height,
cpp: 1,
wb_coeffs: wb_coeffs,
data: image,
blacklevels: blacks,
whitelevels: camera.whitelevels,
xyz_to_cam: camera.xyz_to_cam,
cfa: camera.cfa.clone(),
crops: camera.crops,
}
}
pub fn cam_to_xyz(&self) -> [[f32;4];3] {
let (cam_to_xyz, _) = self.xyz_matrix_and_neutralwb();
cam_to_xyz
}
pub fn neutralwb(&self) -> [f32;4] {
let (_, neutralwb) = self.xyz_matrix_and_neutralwb();
[neutralwb[0]/neutralwb[1],
neutralwb[1]/neutralwb[1],
neutralwb[2]/neutralwb[1],
neutralwb[3]/neutralwb[1]]
}
fn xyz_matrix_and_neutralwb(&self) -> ([[f32;4];3],[f32;4]) {
let d65_white = [0.9547,1.0,1.08883];
let rgb_to_xyz = [
[ 0.412453, 0.357580, 0.180423 ],
[ 0.212671, 0.715160, 0.072169 ],
[ 0.019334, 0.119193, 0.950227 ],
];
let mut rgb_to_cam = [[0.0;3];4];
for i in 0..4 {
for j in 0..3 {
rgb_to_cam[i][j] = 0.0;
for k in 0..3 {
rgb_to_cam[i][j] += self.xyz_to_cam[i][k] * rgb_to_xyz[k][j];
}
}
}
let mut neutralwb = [0 as f32; 4];
for i in 0..4 {
let mut num = 0.0;
for j in 0..3 {
num += rgb_to_cam[i][j];
}
for j in 0..3 {
rgb_to_cam[i][j] = if num == 0.0 {
0.0
} else {
rgb_to_cam[i][j] / num
};
}
neutralwb[i] = 1.0 / num;
}
let cam_to_rgb = self.pseudoinverse(rgb_to_cam);
let mut cam_to_xyz = [[0.0;4];3];
for i in 0..3 {
for j in 0..4 {
cam_to_xyz[i][j] = 0.0;
for k in 0..3 {
cam_to_xyz[i][j] += cam_to_rgb[k][j] * rgb_to_xyz[i][k] / d65_white[i];
}
}
}
(cam_to_xyz, neutralwb)
}
fn pseudoinverse(&self, inm: [[f32;3];4]) -> [[f32;4];3] {
let mut temp: [[f32;6];3] = [[0.0; 6];3];
for i in 0..3 {
for j in 0..6 {
temp[i][j] = if j == i+3 { 1.0 } else { 0.0 };
}
for j in 0..3 {
for k in 0..4 {
temp[i][j] += inm[k][i] * inm[k][j];
}
}
}
for i in 0..3 {
let mut num = temp[i][i];
for j in 0..6 {
temp[i][j] /= num;
}
for k in 0..3 {
if k == i { continue }
num = temp[k][i];
for j in 0..6 {
temp[k][j] -= temp[i][j] * num;
}
}
}
let mut out: [[f32;4];3] = [[0.0; 4];3];
for i in 0..4 {
for j in 0..3 {
out[j][i] = 0.0;
for k in 0..3 {
out[j][i] += temp[j][k+3] * inm[i][k];
}
}
}
out
}
pub fn to_rgb(&self, maxwidth: usize, maxheight: usize) -> Result<RGBImage,String> {
let buffer = imageops::simple_decode(self, maxwidth, maxheight);
Ok(RGBImage{
width: buffer.width,
height: buffer.height,
data: buffer.data,
})
}
pub fn to_srgb(&self, maxwidth: usize, maxheight: usize) -> Result<SRGBImage,String> {
let buffer = imageops::simple_decode(self, maxwidth, maxheight);
let mut image = vec![0 as u8; buffer.width*buffer.height*3];
for (o, i) in image.chunks_mut(1).zip(buffer.data.iter()) {
o[0] = (i*255.0).max(0.0).min(255.0) as u8;
}
Ok(SRGBImage{
width: buffer.width,
height: buffer.height,
data: image,
})
}
pub fn to_linear_rgb(&self, maxwidth: usize, maxheight: usize) -> Result<RGBImage,String> {
let buffer = imageops::simple_decode_linear(self, maxwidth, maxheight);
Ok(RGBImage{
width: buffer.width,
height: buffer.height,
data: buffer.data,
})
}
}