glance_core/
lib.rs

1pub mod drawing;
2mod error;
3pub mod img;
4pub(crate) mod utils;
5
6pub use self::error::{CoreError, Result};
7
8#[cfg(test)]
9mod tests {
10    use super::*;
11    use crate::{
12        drawing::shapes::{AABB, Circle, Line},
13        img::Image,
14    };
15    use std::path::PathBuf;
16
17    // Test with a real image file
18    #[test]
19    fn open_valid_image() -> Result<()> {
20        let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
21        path.push("../media/test_imgs/eye.png");
22
23        let img = Image::open(&path)?;
24
25        if std::env::var("NO_DISPLAY").is_err() {
26            img.display("open_valid_image")?;
27        }
28
29        assert!(!img.is_empty());
30        Ok(())
31    }
32
33    // Test error case for missing file
34    #[test]
35    fn open_invalid_path() {
36        let result = Image::open("non_existent_file.jpg");
37        assert!(result.is_err());
38    }
39
40    // Draw a point in the center of an image
41    #[test]
42    fn draw_shapes() -> Result<()> {
43        let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
44        path.push("../media/test_imgs/eye_orange.png");
45
46        let mut img = Image::open(&path)?;
47        let dims = img.dimensions();
48
49        let center = [dims[0] / 2, dims[1] / 2];
50        let green = [0, 255, 0, 150];
51        let blue = [0, 0, 255, 155];
52
53        img.draw(Circle {
54            position: center,
55            color: green,
56            radius: 100,
57            filled: true,
58            thickness: 5,
59        })?;
60
61        img.draw(AABB {
62            position: [center[0] - 100, center[1] - 100],
63            color: blue,
64            size: [200, 200],
65            thickness: 2,
66            filled: false,
67        })?;
68
69        img.draw(Circle {
70            position: center,
71            color: blue,
72            radius: 150,
73            filled: false,
74            thickness: 8,
75        })?;
76
77        img.draw(Line {
78            start: [0, 0],
79            end: [256, 500],
80            color: [120, 120, 200, 255],
81            thickness: 2,
82        })?;
83
84        if std::env::var("NO_DISPLAY").is_err() {
85            img.display("draw_shapes")?;
86        }
87
88        assert!(img.get_pixel(center)? == green);
89        Ok(())
90    }
91
92    // Draw a point out of bounds, should still pass, as shape may be partially visible
93    #[test]
94    fn draw_partially_out_of_bounds_shape() -> Result<()> {
95        let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
96        path.push("../media/test_imgs/eye_orange.png");
97
98        let mut img = Image::open(&path)?;
99        let dims = img.dimensions();
100
101        let center = [dims[0], dims[1]];
102        let green = [0, 255, 0, 255];
103
104        img.draw(Circle {
105            position: center,
106            color: green,
107            radius: 100,
108            filled: false,
109            thickness: 5,
110        })?;
111
112        if std::env::var("NO_DISPLAY").is_err() {
113            img.display("draw_partially_out_of_bounds_shape")?;
114        }
115
116        assert!(img.get_pixel([dims[0] - 1, dims[1] - 1])? == [0, 0, 0, 0]);
117        Ok(())
118    }
119}