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
mod cubic;
mod linear;

#[cfg(test)]
mod test;

pub use self::cubic::{lift_cubic, unlift_cubic};
pub use self::linear::{lift_linear, unlift_linear};

use crate::processing::process_maybe_parallel_for_each;

fn lift(
    image: &mut [&mut [i16]],
    config_factor: usize,
    horizontal_lift: unsafe fn(&mut [i16], usize),
    vertical_lift: unsafe fn(&mut [&mut [i16]], usize),
) {
    let mut step = 1;
    let hint_do_parallel = get_hint_do_parallel(&image, config_factor);

    while step < image.len() || step < image[0].len() {
        if step < image[0].len() {
            process_maybe_parallel_for_each(
                image.iter_mut().step_by(step),
                |col| unsafe { horizontal_lift(col, step) },
                hint_do_parallel,
            );
        }

        if step < image.len() {
            unsafe { vertical_lift(image, step) };
        }

        step *= 2;
    }
}

fn unlift(
    image: &mut [&mut [i16]],
    config_factor: usize,
    horizontal_unlift: unsafe fn(&mut [i16], usize),
    vertical_unlift: unsafe fn(&mut [&mut [i16]], usize),
) {
    let hint_do_parallel = get_hint_do_parallel(&image, config_factor);

    let mut step = 1;
    while 2 * step < image.len() || 2 * step < image[0].len() {
        step *= 2;
    }

    while step > 0 {
        if step < image.len() {
            unsafe { vertical_unlift(image, step) };
        }

        if step < image[0].len() {
            process_maybe_parallel_for_each(
                image.iter_mut().step_by(step),
                |mut col| unsafe { horizontal_unlift(&mut col, step) },
                hint_do_parallel,
            );
        }

        step /= 2;
    }
}

fn get_hint_do_parallel(image: &[&mut [i16]], config_factor: usize) -> bool {
    image.len() * image[0].len() > config_factor
}