ripple 0.2.1

General-purpose DSP data structures and algorithms
Documentation
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
use nalgebra::*;
// use iter::*;
use nalgebra::storage::*;
// use super::gsl::*;
// use crate::series::*;
// use crate::image::*;
// use super::dwt1d::gsl::*;
use std::ops::{Mul, MulAssign, Index, };
use crate::dwt::*;
use std::iter::FromIterator;
use std::cell::RefCell;
use std::fmt::Debug;

pub mod ipp;

// pub mod iter;

// use iter::DWTIteratorBase;

/// Two-dimensional wavelet decomposition
pub struct Wavelet2D {
    states : Vec<ipp::IppDWT2D>,
    // coefs : Vec<([f32; 4], [f32; 4])>,
    basis : Basis,
    img_side : usize,
    bwd_pyr : RefCell<Pyramid<f32>>
}

impl Wavelet2D {

    pub fn new(basis : Basis, img_side : usize, n_levels : usize) -> Self {
        assert!(n_levels >= 1 && n_levels <= basis.len());
        let (low, high) = basis.coefficients();
        let mut states = Vec::new();
        for i in 0..n_levels {
            let state = unsafe {
                ipp::build_dwt2d_state(&low[..], &high[..])
            };
            states.push(state);
        }
        let bwd_pyr = RefCell::new(Pyramid::new(basis, img_side, Some(states.len())));
        Self { states, /*coefs,*/ basis, img_side, bwd_pyr }
    }

    pub fn empty_pyramid(&self) -> Pyramid<f32> {
        Pyramid::new(self.basis, self.img_side, Some(self.states.len()))
    }

    pub fn forward_mut(&self, src : &impl AsRef<[f32]>, dst : &mut Pyramid<f32>) {
        assert!(self.states.len() == dst.levels.len());
        unsafe {
            for i in 0..self.states.len() {
                if i == 0 {
                    let curr_lvl = &mut dst.levels[i];
                    super::verify_dwt2d_dimensions(
                        src.as_ref(),
                        &curr_lvl.coarse[..],
                        &curr_lvl.detail_x[..],
                        &curr_lvl.detail_y[..],
                        &curr_lvl.detail_xy[..]
                    );
                    ipp::apply_forward(
                        self.states[i].spec_fwd,
                        self.states[i].buf_fwd,
                        src.as_ref(),
                        self.img_side,
                        self.basis.len(),
                        &mut curr_lvl.coarse,
                        &mut curr_lvl.detail_x,
                        &mut curr_lvl.detail_y,
                        &mut curr_lvl.detail_xy
                    );
                } else {
                    let (prev_lvl, curr_lvl) = dst.level_pair_mut(i);
                    super::verify_dwt2d_dimensions(
                        &prev_lvl.coarse[..],
                        &curr_lvl.coarse[..],
                        &curr_lvl.detail_x[..],
                        &curr_lvl.detail_y[..],
                        &curr_lvl.detail_xy[..]
                    );
                    ipp::apply_forward(
                        self.states[i].spec_fwd,
                        self.states[i].buf_fwd,
                        &prev_lvl.coarse[..],
                        self.img_side,
                        self.basis.len(),
                        &mut curr_lvl.coarse,
                        &mut curr_lvl.detail_x,
                        &mut curr_lvl.detail_y,
                        &mut curr_lvl.detail_xy
                    );
                }
            }
        }
    }

    pub fn backward_mut(&self, src : &Pyramid<f32>, dst : &mut AsMut<[f32]>) {
        assert!(self.states.len() == src.levels.len());
        let mut bwd_pyr = self.bwd_pyr.borrow_mut();
        unsafe {
            for i in (0..self.states.len()).rev().skip(1) {
                if i == 0 {
                    let curr_lvl = &bwd_pyr.levels[0];
                    ipp::apply_backward(
                        self.states[i].spec_bwd,
                        self.states[i].buf_bwd,
                        dst.as_mut(),
                        self.img_side,
                        self.basis.len(),
                        &curr_lvl.coarse[..],
                        &curr_lvl.detail_x[..],
                        &curr_lvl.detail_y[..],
                        &curr_lvl.detail_xy[..]
                    );
                } else {
                    let curr_lvl = &mut bwd_pyr.levels[i-1];
                    let prev_lvl = &src.levels[i];
                    ipp::apply_backward(
                        self.states[i].spec_bwd,
                        self.states[i].buf_bwd,
                        curr_lvl.coarse.as_mut(),
                        self.img_side,
                        self.basis.len(),
                        &prev_lvl.coarse[..],
                        &prev_lvl.detail_x[..],
                        &prev_lvl.detail_y[..],
                        &prev_lvl.detail_xy[..]
                    );
                }
            }
        }
    }

}

pub struct PyramidLevel<N>
where
    N : Scalar + Debug
{
    detail_x : Vec<N>,
    detail_y : Vec<N>,
    detail_xy : Vec<N>,
    coarse : Vec<N>,
    side_len : usize
}

impl<N> PyramidLevel<N>
where
    N : From<f32> + Scalar + Clone
{

    pub fn new(side_len : usize, filt_len : usize) -> Self {
        // let half_len = side_len / 2;
        // let dst_len = (half_len - filt_len / 2) as usize;
        let mut coarse = Vec::from_iter((0..side_len.pow(2u32)).map(|_| N::from(0.0) ));
        let mut detail_x = coarse.clone();
        let mut detail_y = coarse.clone();
        let mut detail_xy = coarse.clone();
        Self {
            detail_x,
            detail_y,
            detail_xy,
            coarse,
            side_len
        }
    }

    pub fn coarse(&self) -> &[N] {
        &self.coarse[..]
    }

    pub fn horizontal_detail(&self) -> &[N] {
        &self.detail_x[..]
    }

    pub fn vertical_detail(&self) -> &[N] {
        &self.detail_y[..]
    }

    pub fn diagonal_detail(&self) -> &[N] {
        &self.detail_xy[..]
    }

}

pub struct Pyramid<N>
where
    N : From<f32> + Scalar + Clone
{
    levels : Vec<PyramidLevel<N>>
}

impl<N> Pyramid<N>
where
    N : From<f32> + Scalar + Clone
{

    pub fn new(basis : Basis, side : usize, n_levels : Option<usize>) -> Self {
        let n_levels = n_levels.unwrap_or(dwt_max_levels(side));
        let filt_len = basis.len();
        let levels = (0..n_levels)
            .map(|lvl| PyramidLevel::new(side / (2usize).pow(lvl as u32 + 1), filt_len) )
            .collect();
        Self { levels }
    }

    /// Returns a level and its previous level
    pub fn level_pair_mut(&mut self, last : usize) -> (&mut PyramidLevel<N>, &mut PyramidLevel<N>) {
        index_and_prev_mut(&mut self.levels[..], last)
    }

    pub fn horizontal(&self, level : usize) -> &[N] {
        &self.levels[level].detail_x[..]
    }

    pub fn vertical(&self, level : usize) -> &[N] {
        &self.levels[level].detail_y[..]
    }

    pub fn diagonal(&self, level : usize) -> &[N] {
        &self.levels[level].detail_xy[..]
    }

    pub fn coarse(&self, level : usize) -> &[N] {
        &self.levels[level].coarse[..]
    }

    pub fn horizontal_mut(&mut self, level : usize) -> &mut [N] {
        &mut self.levels[level].detail_x[..]
    }

    pub fn vertical_mut(&mut self, level : usize) -> &mut [N] {
        &mut self.levels[level].detail_y[..]
    }

    pub fn diagonal_mut(&mut self, level : usize) -> &mut [N] {
        &mut self.levels[level].detail_xy[..]
    }

    pub fn coarse_mut(&mut self, level : usize) -> &mut [N] {
        &mut self.levels[level].coarse[..]
    }

    fn full_level_mut(&mut self, level : usize) -> (&mut [N], &mut [N], &mut [N], &mut [N]) {
        let level = &mut self.levels[level];
        (
            &mut level.coarse[..],
            &mut level.detail_x[..],
            &mut level.detail_y[..],
            &mut level.detail_xy[..]
        )
    }

    fn full_level(&self, level : usize) -> (&[N], &[N], &[N], &[N]) {
        let level = &self.levels[level];
        (
            &level.coarse[..],
            &level.detail_x[..],
            &level.detail_y[..],
            &level.detail_xy[..]
        )
    }

}

// cargo test --all-features image_dwt -- --nocapture
#[test]
fn image_dwt() {

    use crate::dwt::bank;
    use crate::signal::gen;
    use std::iter::FromIterator;
    use super::*;
    use crate::display_as_mat;

    // For anchor = 0, A 4x4 image should be padded to a 7x7 image (filter len-1), which has 49 entries.
    // If we assume anchor=0, the padding is in the first 3 rows and columns.
    //                  | Effective image starts here
    //                  V
    /*let img : [f32; 49] = [
        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, // <-- Effective image starts here
        0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0,
        0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0,
        0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0,
    ];*/

    let img_len = 64;
    let filt_len = 4;
    let half_len = img_len / 2;
    // Create step image
    let mut img = gen::step2d(img_len);
    let dst_len = (half_len - filt_len / 2) as usize;

    /*let mut approx = Vec::from_iter((0..dst_len.pow(2u32)).map(|_| 0.0 ));
    let mut detail_x = approx.clone();
    let mut detail_y = approx.clone();
    let mut detail_xy = approx.clone();
    println!("Building filters");*/

    /*unsafe {
        let dwt2d = build_dwt2d_state(&bank::DAUBECHIES[..], &bank::advance_level(&bank::DAUBECHIES)[..]);
        apply_forward(
            dwt2d.spec_fwd,
            dwt2d.buf_fwd,
            img.as_ref(),
            img_len,
            filt_len,
            &mut approx[..],
            &mut detail_x[..],
            &mut detail_y[..],
            &mut detail_xy[..]
        );
    }*/

    let wav = Wavelet::new(Basis::Daubechies(4, false), img_len, 1);
    let mut pyr = wav.generate_pyramid();
    wav.forward(&img, &mut pyr);

    let dst_offset = dst_len as usize;
    let dst_ncols = dst_len as usize;

    // let approx_mat = mat_from_slice(&approx[..], dst_len);
    // println!("{:?}", approx_mat.shape());

    println!("Approx = {}", display_as_mat(pyr.coarse(0), dst_len));
    println!("Detail X = {}", display_as_mat(pyr.horizontal(0), dst_len));
    println!("Detail Y = {}", display_as_mat(pyr.vertical(0), dst_len));
    println!("Detail XY = {}", display_as_mat(pyr.diagonal(0), dst_len));

    /* Approx = [0.0, 0.0, 0.40400636, 1.291266]
    Detail X = [0.0, 0.0, -0.10825317, -0.34599364]
    Detail Y = [0.0, 0.0, -0.10825317, 0.40400636]
    Detail XY = [0.0, 0.0, 0.02900635, -0.10825318] */
}

/// Output of a wavelet decomposition. Imgage pyramids are indexed by a (scale, x, y) triplet.
/*#[derive(Clone, Debug)]
pub struct ImagePyramid<N>
where
    N : Scalar + Copy
{
    pyr : Image<N>
}

impl<N> ImagePyramid<N>
where
    N : Scalar + Copy
{

    pub fn len(&self) -> usize {
        self.pyr.len()
    }
}

impl<N> ImagePyramid<N>
where
    N : Scalar + Copy
{

    pub fn new_constant(n : usize, value : N) -> Self {
        Self{ pyr : Image::new_constant(n, n, value) }
    }

    pub fn levels<'a>(&'a self) -> impl Iterator<Item=ImageLevel<'a, N>> {
        DWTIteratorBase::<&'a ImagePyramid<N>>::new_ref(&self)
    }

    /*pub fn levels_mut<'a>(&'a mut self) -> impl Iterator<Item=DMatrixSliceMut<'a, f64>> {
        DWTIteratorBase::<&'a mut DMatrix<f64>>::new_mut(&mut self.pyr)
    }*/
}

impl<N> AsRef<[N]> for ImagePyramid<N>
where
    N : Scalar + Copy
{
    fn as_ref(&self) -> &[N] {
        self.pyr.as_slice()
    }
}

impl<N> AsMut<[N]> for ImagePyramid<N>
where
    N : Scalar + Copy
{
    fn as_mut(&mut self) -> &mut [N] {
        self.pyr.as_mut_slice()
    }
}

pub struct ImageLevel<'a, N>
where
    N : Scalar + Copy
{
    win : Window<'a, N>
}

impl<'a, N> ImageLevel<'a, N>
where
    N : Scalar + Copy + Mul<Output=N> + MulAssign
{
    pub fn windows(&self, sz : (usize, usize)) -> impl Iterator<Item=Window<'a, N>> {
        self.win.clone().windows(sz)
    }

}

impl<'a, N> Index<(usize, usize)> for ImageLevel<'a, N>
where
    N : Scalar + Copy
{

    type Output = N;

    fn index(&self, index: (usize, usize)) -> &Self::Output {
        &self.win[index]
    }
}

impl<'a, N> From<Window<'a, N>> for ImageLevel<'a, N>
where
    N : Scalar + Copy
{

    fn from(win : Window<'a, N>) -> Self {
        Self{ win }
    }
}

pub struct ImageLevelMut<'a, N>
where
    N : Scalar + Copy
{
    win : WindowMut<'a, N>
}

impl<'a, N> From<WindowMut<'a, N>> for ImageLevelMut<'a, N>
where
    N : Scalar + Copy
{

    fn from(win : WindowMut<'a, N>) -> Self {
        Self{ win }
    }
}

impl<N> AsRef<Image<N>> for ImagePyramid<N>
where
    N : Scalar + Copy
{
    fn as_ref(&self) -> &Image<N> {
        &self.pyr
    }
}

impl<N> AsMut<Image<N>> for ImagePyramid<N>
where
    N : Scalar + Copy
{
    fn as_mut(&mut self) -> &mut Image<N> {
        &mut self.pyr
    }
}

/*impl<N> From<DMatrix<N>> for ImagePyramid<N>
where
    N : Scalar
{
    fn from(s : DMatrix<N>) -> Self {
        Self{ pyr : s }
    }
}

impl<N> AsRef<DMatrix<N>> for ImagePyramid<N>
where
    N : Scalar
{
    fn as_ref(&self) -> &DMatrix<N> {
        &self.pyr
    }
}*/

impl<N> From<Vec<N>> for Pyramid<N>
where
    N : Scalar
{
    fn from(s : Vec<N>) -> Self {
        Self{ buf : DVector::from_vec(s) }
    }
}*/

impl Wavelet2D {

    /*pub fn new(basis : Basis, sz : usize) -> Result<Self, &'static str> {
        Ok(Self { plan : DWTPlan::new(basis, (sz, sz) )? })
    }

    pub fn forward_mut(&self, src : &Image<f64>, dst : &mut ImagePyramid<f64>) {
        self.plan.apply_forward(src.as_ref(), dst.as_mut())
            .map_err(|e| panic!("{}", e) );
    }

    pub fn forward(&self, src : &Image<f64>) -> ImagePyramid<f64> {
        let (nrows, ncols) = self.plan.shape();
        let mut dst = ImagePyramid::new_constant(nrows, 0.0);
        self.forward_mut(src, &mut dst);
        dst
    }

    pub fn forward_inplace(&self, mut buffer : Image<f64>) -> ImagePyramid<f64> {
        if let Err(e) = self.plan.forward_inplace(buffer.as_mut()) {
            panic!("{}", e);
        }
        ImagePyramid { pyr : buffer }
    }

    pub fn backward_mut(&self, src : &ImagePyramid<f64>, dst : &mut Image<f64>) {
        self.plan.apply_backward(src.as_ref(), dst.as_mut())
            .map_err(|e| panic!("{}", e) );
    }

    pub fn backward_inplace(&self, mut buffer : ImagePyramid<f64>) -> Image<f64> {
        if let Err(e) = self.plan.backward_inplace(buffer.as_mut()) {
            panic!("{}", e);
        }
        buffer.pyr
    }

    pub fn backward(&self, src : &ImagePyramid<f64>) -> Image<f64> {
        let (nrows, ncols) = self.plan.shape();
        let mut dst = Image::new_constant(nrows, ncols, 0.0);
        self.backward_mut(src, &mut dst);
        dst
    }*/
}

/*impl Forward<Image<f64>> for Wavelet2D {

    type Output = Image<f64>;

    fn forward_mut(&self, src : &Image<f64>, dst : &mut Self::Output) {
        self.plan.apply_forward(src.as_ref(), dst.as_mut())
            .map_err(|e| panic!("{}", e) );
    }

    fn forward(&self, src : &Image<f64>) -> Self::Output {
        let (nrows, ncols) = self.plan.shape();
        let mut dst = Image::new_constant(nrows, ncols, 0.0);
        self.forward_mut(src, &mut dst);
        dst
    }
}

impl Backward<Image<f64>> for Wavelet2D {

    type Output = Image<f64>;

    fn backward_mut(&self, src : &Image<f64>, dst : &mut Self::Output) {
        self.plan.apply_backward(src.as_ref(), dst.as_mut())
            .map_err(|e| panic!("{}", e) );
    }

    fn backward(&self, src : &Image<f64>) -> Self::Output {
        let (nrows, ncols) = self.plan.shape();
        let mut dst = Image::new_constant(nrows, ncols, 0.0);
        self.backward_mut(src, &mut dst);
        dst
    }
}*/

/*
If the interest is exclusively on image reconstruction from low spatial frequencies,
see opencv::imgproc::{pyr_down, pyr_up} as an alternative. If the interest is on the coefficients
themselves (e.g. keypoint extraction) then the DWT is the way to go.

*/