index : gmss.git

ascending towards madness

author holly sparkles <sparkles@holly.sh> 2023-12-19 18:51:06.0 +00:00:00
committer holly sparkles <sparkles@holly.sh> 2023-12-19 21:19:00.0 +00:00:00
commit
23847da33baa0ed61eadce25a90199c751ddb0a3 [patch]
tree
963851e30c92301b729e589b60c031cfb57b4e7b
parent
fbb667d02e55765a5928a087bb347cf57eab2976
download
23847da33baa0ed61eadce25a90199c751ddb0a3.tar.gz

feat: add first iteration of screen capture



Diff

 Cargo.toml  |  1 +
 src/main.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/Cargo.toml b/Cargo.toml
index de7072b..40ad268 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
screenshots = "0.8.6"
diff --git a/src/main.rs b/src/main.rs
index e7a11a9..3479e0a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,59 @@
use screenshots::{image::ImageBuffer, Screen};

fn main() {
    println!("Hello, world!");
    capture_all();
}

fn capture_all() {
    let mut screens = Screen::all().unwrap_or_else(|err| {
        eprintln!("there was an error retrieving screens: {:?}", err);
        vec![]
    });

    screens.sort_by(|first, second| first.display_info.x.cmp(&second.display_info.x));

    let captures: Vec<_> = screens
        .iter()
        .filter_map(|screen| screen.capture().ok())
        .collect();

    let dimensions = captures
        .iter()
        .fold(ImageDimensions::default(), |t, buf| ImageDimensions {
            x: t.x + buf.width(),
            y: t.y.max(buf.height()),
        });

    let mut img = ImageBuffer::new(dimensions.x, dimensions.y);

    captures
        .iter()
        .fold(ImageDimensions::default(), |offset, buf| {
            for (x, y, pixel) in buf.enumerate_pixels() {
                img.put_pixel(offset.x + x, y, *pixel);
            }
            ImageDimensions {
                x: offset.x + buf.width(),
                y: offset.y,
            }
        });

    if !img.is_empty() {
        match img.save("target/capture.png") {
            Ok(_) => println!("screenshot saved successfully!"),
            Err(err) => println!("there was an error saving the image: {:?}", err),
        }
    }
}

#[derive(Default)]
struct ImageDimensions {
    pub x: u32,
    pub y: u32,
}

impl std::fmt::Display for ImageDimensions {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!("({}, {})", self.x, self.y))
    }
}