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
use crate::decoders::*;
use crate::decoders::cfa::*;
#[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 blackareas: Vec<(u64,u64,u64,u64)>,
pub orientation: Orientation,
pub data: RawImageData,
}
#[derive(Debug, Clone)]
pub enum RawImageData {
Integer(Vec<u16>),
Float(Vec<f32>),
}
impl RawImage {
#[doc(hidden)] pub fn new(camera: Camera, width: usize, height: usize, wb_coeffs: [f32;4], image: Vec<u16>, dummy: bool) -> RawImage {
let blacks = if !dummy && (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
};
let mut blackareas: Vec<(u64,u64,u64,u64)> = Vec::new();
if camera.blackareah.1 != 0 {
blackareas.push((camera.blackareah.0 as u64, width as u64,
(camera.blackareah.0 + camera.blackareah.1) as u64, 0));
}
if camera.blackareav.1 != 0 {
blackareas.push((0, (camera.blackareav.0 + camera.blackareav.1) as u64,
height as u64, camera.blackareav.0 as u64))
}
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: RawImageData::Integer(image),
blacklevels: blacks,
whitelevels: camera.whitelevels,
xyz_to_cam: camera.xyz_to_cam,
cfa: camera.cfa.clone(),
crops: camera.crops,
blackareas: blackareas,
orientation: camera.orientation,
}
}
pub fn cam_to_xyz(&self) -> [[f32;4];3] {
Self::pseudoinverse(self.xyz_to_cam)
}
pub fn cam_to_xyz_normalized(&self) -> [[f32;4];3] {
Self::normalized_pseudoinverse(self.xyz_to_cam)
}
pub fn neutralwb(&self) -> [f32;4] {
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];
}
neutralwb[i] = 1.0 / num;
}
[neutralwb[0]/neutralwb[1],
neutralwb[1]/neutralwb[1],
neutralwb[2]/neutralwb[1],
neutralwb[3]/neutralwb[1]]
}
pub fn normalized_pseudoinverse(inm: [[f32;3];4]) -> [[f32;4];3] {
let mut xyz_to_cam = inm;
for i in 0..4 {
let mut num = 0.0;
for j in 0..3 {
num += xyz_to_cam[i][j];
}
for j in 0..3 {
xyz_to_cam[i][j] = if num == 0.0 {
0.0
} else {
xyz_to_cam[i][j] / num
};
}
}
Self::pseudoinverse(xyz_to_cam)
}
pub fn pseudoinverse(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 cropped_cfa(&self) -> CFA {
self.cfa.shift(self.crops[3], self.crops[0])
}
pub fn is_monochrome(&self) -> bool {
self.cpp == 1 && !self.cfa.is_valid()
}
}