Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a screenshot utility #82

Merged
merged 4 commits into from
Dec 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 49 additions & 0 deletions examples/screenshot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use image::{DynamicImage, ImageOutputFormat};
use libremarkable::device::CURRENT_DEVICE;
use libremarkable::framebuffer::common::*;
use libremarkable::framebuffer::core::*;
use libremarkable::framebuffer::*;
use libremarkable::image::RgbImage;
use std::fs::OpenOptions;

fn main() {
let fb = Framebuffer::from_path(CURRENT_DEVICE.get_framebuffer_path());
let width = DISPLAYWIDTH as u32;
let height = DISPLAYHEIGHT as u32;
let contents = fb
.dump_region(mxcfb_rect {
top: 0,
left: 0,
width,
height,
})
.expect("dumping image buffer with known dimensions should succeed")
.chunks_exact(2)
.flat_map(|c| color::NATIVE_COMPONENTS(c[0], c[1]).to_rgb8())
.collect::<Vec<_>>();

let image =
RgbImage::from_raw(width, height, contents).expect("unable to construct the rgb image");

let args = std::env::args().collect::<Vec<_>>();

match args.get(1) {
Some(path) => {
let mut output_file = OpenOptions::new()
.write(true)
.create(true)
.truncate(false)
.open(path)
.expect("Invalid path provided as argument!");
DynamicImage::ImageRgb8(image)
.write_to(&mut output_file, ImageOutputFormat::Png)
.expect("failed while writing to output file");
}
None => {
let mut stdout = std::io::stdout();
DynamicImage::ImageRgb8(image)
.write_to(&mut stdout, ImageOutputFormat::Png)
.expect("failed while writing to stdout");
}
}
}