nv_flip_sys/
lib.rs

1include!("bindings.rs");
2
3/// Default configuration for pixels per degree.
4///
5/// Corresponds to _roughly_ a 31.6" monitor at 3840x2160 resolution, viewed from 70cm away.
6///
7/// This value is how you adjust the sensitivity of the comparison.
8/// Use [`pixels_per_degree`] to compute a custom value for your situation.
9///
10/// ```rust
11/// # use float_eq::assert_float_eq;
12/// let computed = nv_flip::pixels_per_degree(0.7, 3840.0, 0.7);
13/// assert_float_eq!(computed, nv_flip::DEFAULT_PIXELS_PER_DEGREE, abs <= 0.05);
14/// ```
15pub const DEFAULT_PIXELS_PER_DEGREE: f32 = 67.0;
16
17/// Computes the pixels per degree of arc given a monitor configuration.
18///
19/// - `distance` - Distance from the monitor in meters.
20/// - `resolution_x` - Horizontal resolution of the monitor in pixels.
21/// - `monitor_width` - Width of the monitor in meters.
22///
23/// This value is how you adjust the sensitivity of the comparison.
24///
25/// If you don't care, use [`DEFAULT_PIXELS_PER_DEGREE`].
26pub fn pixels_per_degree(distance: f32, resolution_x: f32, monitor_width: f32) -> f32 {
27    distance * (resolution_x / monitor_width) * (std::f32::consts::PI / 180.0)
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    #[test]
35    fn creation_deletion() {
36        unsafe {
37            let image = flip_image_color3_new(10, 10, std::ptr::null_mut());
38            assert!(!image.is_null());
39            flip_image_color3_free(image);
40        }
41    }
42
43    #[test]
44    fn creation_with_data_and_deletion() {
45        let data = vec![0u8; 10 * 10 * 3];
46        unsafe {
47            let image = flip_image_color3_new(10, 10, data.as_ptr());
48            assert!(!image.is_null());
49            flip_image_color3_free(image);
50        }
51    }
52
53    #[test]
54    fn end_to_end() {
55        let ref_image = image::open("../etc/tree-ref.png").unwrap().into_rgb8();
56        let test_image = image::open("../etc/tree-test.png").unwrap().into_rgb8();
57
58        let ref_flip = unsafe {
59            flip_image_color3_new(ref_image.width(), ref_image.height(), ref_image.as_ptr())
60        };
61        let test_flip = unsafe {
62            flip_image_color3_new(test_image.width(), test_image.height(), test_image.as_ptr())
63        };
64
65        let error_map = unsafe {
66            flip_image_float_new(ref_image.width(), ref_image.height(), std::ptr::null())
67        };
68
69        unsafe {
70            flip_image_float_flip(error_map, ref_flip, test_flip, 67.0);
71        }
72
73        let histogram = unsafe { flip_image_pool_new(256) };
74        unsafe { flip_image_pool_update_image(histogram, error_map) }
75        println!("{:.6}", unsafe {
76            flip_image_pool_get_percentile(histogram, 0.75, true)
77        });
78
79        let output_flip = unsafe {
80            flip_image_color3_new(ref_image.width(), ref_image.height(), std::ptr::null())
81        };
82
83        let magma_flip = unsafe { flip_image_color3_magma_map() };
84
85        unsafe {
86            flip_image_float_copy_float_to_color3(error_map, output_flip);
87            flip_image_color3_color_map(output_flip, error_map, magma_flip);
88        };
89
90        let mut output_image = image::RgbImage::new(ref_image.width(), ref_image.height());
91
92        unsafe {
93            flip_image_color3_get_data(output_flip, output_image.as_mut_ptr());
94        }
95
96        unsafe {
97            flip_image_color3_free(ref_flip);
98            flip_image_color3_free(test_flip);
99            flip_image_color3_free(output_flip);
100            flip_image_color3_free(magma_flip);
101            flip_image_float_free(error_map);
102            flip_image_pool_free(histogram);
103        }
104
105        let sample = image::open("../etc/tree-comparison-cli.png")
106            .unwrap()
107            .into_rgb8();
108
109        for (a, b) in output_image.pixels().zip(sample.pixels()) {
110            assert!(a.0[0].abs_diff(b.0[0]) <= 3);
111            assert!(a.0[1].abs_diff(b.0[1]) <= 3);
112            assert!(a.0[2].abs_diff(b.0[2]) <= 3);
113        }
114    }
115}