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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
use ;
use ;
/*#[inline(always)]
fn zip_rows_buffer_mut<'a, N : Scalar>(
buf : &'a mut [u8],
dst : &'a DMatrix<N>,
c_stride : usize,
skip : (usize, usize)
) -> impl Iterator<Item = (&'a mut [u8], MatrixSlice<'a, N, U1, Dynamic, U1, Dynamic>)> {
let buf_rows = buf.chunks_mut(c_stride)
.skip(skip.0)
.take(dst.nrows())
.map(move |r| &mut r[(skip.1)..(skip.1+dst.ncols())]);
let dst_rows = dst.row_iter();
buf_rows.zip(dst_rows)
}*/
/*#[inline(always)]
fn zip_rows_dst_mut<'a, N : Scalar>(
buf : &'a [u8],
dst : &'a mut DMatrix<N>,
c_stride : usize,
skip : (usize, usize)
) -> impl Iterator<Item = (&'a [u8], MatrixSliceMut<'a, N, U1, Dynamic, U1, Dynamic>)> {
let win_cols = dst.ncols();
let buf_rows = buf.chunks(c_stride)
.skip(skip.0)
.take(dst.nrows())
.map(move |r| &r[(skip.1)..(skip.1+win_cols)]);
let dst_rows = dst.row_iter_mut();
buf_rows.zip(dst_rows)
}*/
/*fn read_from<N>(
src : &mut DMatrix<N>,
buf : &[u8],
start : (usize, usize),
buf_dims : (usize, usize),
offset : Option<N>,
scale : Option<N>
) -> ()
where
N : Scalar + From<f32> + From<u8> + Add<Output=N> + Mul<Output=N> + Copy
{
let offset = offset.unwrap_or(N::from(0.0));
let scale = scale.unwrap_or(N::from(1.0));
if check_bounds(start, src.shape(), buf_dims) {
for (buf_row, mut dst_row) in zip_rows_dst_mut(buf, src, buf_dims.1, start) {
buf_row.iter()
.zip(dst_row.iter_mut())
.for_each(|(b, d)|{
*d = (N::from(*b)+offset)*scale;
})
}
} else {
println!("[update_from] Out of buffer bounds. Skipping.");
}
}*/
/*#[inline(always)]
fn write_into<N>(
src : &DMatrix<N>,
buf : &mut [u8],
start : (usize, usize),
buf_dims : (usize, usize),
offset : Option<N>,
scale : Option<N>
) ->()
where
N : Scalar + Into<u8> + Add<Output=N> + Mul<Output=N> + From<f32> + Copy,
u8 : From<N>
{
let offset = offset.unwrap_or(N::from(0.0));
let scale = scale.unwrap_or(N::from(1.0));
if check_bounds(start, src.shape(), buf_dims) {
for (buf_row, dst_row) in zip_rows_buffer_mut(buf, &src, buf_dims.1, start) {
buf_row.iter_mut()
.zip(dst_row.iter())
.for_each(|(b, d)|{
*b = u8::from((*d + offset) * scale)
})
}
} else {
println!("[write_into] Out of buffer bounds. Skipping.");
}
}*/
// From trait cannot be used for f32/f64->u8 casting, which is why
// we need to do cast to a f64 first then cast to a u8 (this lossy cast
// can only be done with concerete types). The alternative would be to
// cast into f32, but then we could not implement the Signal<T> trait in
// a generic way for floating-point containers.
/*#[inline(always)]
pub fn convert_f32_slice(src : &[u8], dst : &mut [f32]) {
for (s, d) in src.iter().zip(dst.iter_mut()) {
*d = *s as f32;
}
}
#[inline(always)]
pub fn convert_f64_slice(src : &[u8], dst : &mut [f64]) {
for (s, d) in src.iter().zip(dst.iter_mut()) {
*d = *s as f64;
}
}*/
/*#[inline(always)]
pub fn convert_f32_slice_strided(src : &[u8], dst : &mut [f32], cstride : usize) {
for i in 0..dst.len() {
dst[i] = src[i*cstride] as f32
}
}
#[inline(always)]
pub fn convert_f64_slice_strided(src : &[u8], dst : &mut [f64], cstride : usize) {
for i in 0..dst.len() {
dst[i] = src[i*cstride] as f64
}
}*/
/*#[inline(always)]
pub fn copy_from_slices_f32(d : &mut DMatrix<f32>, data : &[&[u8]], step : usize) {
assert!(data.len() > 1);
let sz = data[0].len();
assert!(sz / step == d.ncols());
assert!(data.len() == d.nrows());
for d in data.iter().skip(1) {
assert!(d.len() == sz);
}
for (i, mut row) in d.row_iter_mut().enumerate() {
for (j, e) in row.iter_mut().enumerate() {
*e = data[i][j*step] as f32;
}
}
}
#[inline(always)]
pub fn copy_from_slices_f64(d : &mut DMatrix<f64>, data : &[&[u8]], step : usize) {
assert!(data.len() > 1);
let sz = data[0].len();
assert!(sz / step == d.ncols());
assert!(data.len() == d.nrows());
for d in data.iter().skip(1) {
assert!(d.len() == sz);
}
for (i, mut row) in d.row_iter_mut().enumerate() {
for (j, e) in row.iter_mut().enumerate() {
*e = data[i][j*step] as f64;
}
}
}*/
/// Converts a slice, without any subsampling.
/// Convert a pair of slices, assuming that walking one element in src means walking cstride
/// elements in dst. Can be used as a component for window/offset conversions.
/// Converts a src slice of type T, assumed to be an image with given ncols into a destination slice of type U.
/// If transpose is true, rows becomes columns in the resulting image. step gives the subsampling step required.
/// Same as subsample_convert, but now assuming an offset.
/*#[inline(always)]
pub fn subsample_convert_f32(content : &[u8], dst : &mut [f32], ncols : usize, sample_n : usize, transpose : bool) {
assert!(ncols < content.len(), "ncols smaller than content length");
assert!(content.len() % ncols == 0);
let nrows = content.len() / ncols;
let sparse_ncols = if ncols > 1 { ncols / sample_n } else { 1 };
let sparse_nrows = nrows / sample_n;
if dst.len() != sparse_nrows * sparse_ncols {
panic!("Dimension mismatch");
}
for r in 0..sparse_nrows {
for c in 0..sparse_ncols {
let dst_ix = if transpose { r + c*sparse_nrows } else { r*sparse_ncols + c };
dst[dst_ix] = content[r*sample_n*ncols + c*sample_n] as f32;
}
}
}
#[inline(always)]
pub fn subsample_convert_f64(content : &[u8], dst : &mut [f64], ncols : usize, sample_n : usize, transpose : bool) {
assert!(ncols < content.len(), "ncols smaller than content length");
assert!(content.len() % ncols == 0);
let nrows = content.len() / ncols;
let sparse_ncols = if ncols > 1 { ncols / sample_n } else { 1 };
let sparse_nrows = nrows / sample_n;
if dst.len() != sparse_nrows * sparse_ncols {
panic!("Dimension mismatch");
}
for r in 0..sparse_nrows {
for c in 0..sparse_ncols {
let dst_ix = if transpose { r + c*sparse_nrows } else { r*sparse_ncols + c };
dst[dst_ix] = content[r*sample_n*ncols + c*sample_n] as f64;
}
}
}*/
/* pub fn decode(&self, dec : &[u8]) -> Result<DMatrix<f32>, &'static str> {
let mut content = Vec::<f32>::new();
for w in dec.windows(4) {
let buffer : Result<[u8; 4], _> = w.try_into();
if let Ok(b) = buffer {
let u = u32::from_ne_bytes(b);
content.push(f32::from_bits(u))
} else {
return Err("Could not parse buffer as array");
}
}
Ok(DMatrix::from_vec(self.data.nrows(), self.data.ncols(), content))
}
*/