Skip to content

Commit

Permalink
example: replace bmp with image/png
Browse files Browse the repository at this point in the history
bmp is a small crate, but it is not popular, and apparently
unmaintained. Use the ubiquitous image crate and the PNG format instead.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
  • Loading branch information
elmarco authored and CBenoit committed Nov 29, 2024
1 parent 0ee5bfc commit fe0d9e9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 27 deletions.
20 changes: 19 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/ironrdp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ ironrdp-blocking.workspace = true
ironrdp-cliprdr-native.workspace = true
anyhow = "1"
async-trait = "0.1"
bmp = "0.5"
image = { version = "0.25.5", default-features = false, features = ["png"] }
pico-args = "0.5"
x509-cert = { version = "0.2", default-features = false, features = ["std"] }
sspi = { workspace = true, features = ["network_client"] }
Expand Down
32 changes: 8 additions & 24 deletions crates/ironrdp/examples/screenshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
//!
//! In this basic client implementation, the client establishes a connection
//! with the destination server, decodes incoming graphics updates, and saves the
//! resulting output as a BMP image file on the disk.
//! resulting output as a PNG image file on the disk.
//!
//! # Usage example
//!
//! ```shell
//! cargo run --example=screenshot -- --host <HOSTNAME> -u <USERNAME> -p <PASSWORD> -o out.bmp
//! cargo run --example=screenshot -- --host <HOSTNAME> -u <USERNAME> -p <PASSWORD> -o out.png
//! ```
#![allow(unused_crate_dependencies)] // false positives because there is both a library and a binary
Expand Down Expand Up @@ -99,7 +99,7 @@ fn parse_args() -> anyhow::Result<Action> {
let password = args.value_from_str(["-p", "--password"])?;
let output = args
.opt_value_from_str(["-o", "--output"])?
.unwrap_or_else(|| PathBuf::from("out.bmp"));
.unwrap_or_else(|| PathBuf::from("out.png"));
let domain = args.opt_value_from_str(["-d", "--domain"])?;

Action::Run {
Expand Down Expand Up @@ -156,27 +156,11 @@ fn run(

active_stage(connection_result, framed, &mut image).context("active stage")?;

let mut bmp = bmp::Image::new(u32::from(image.width()), u32::from(image.height()));

image
.data()
.chunks_exact(usize::from(image.width()).checked_mul(4).expect("never overflow"))
.enumerate()
.for_each(|(y, row)| {
row.chunks_exact(4).enumerate().for_each(|(x, pixel)| {
let r = pixel[0];
let g = pixel[1];
let b = pixel[2];
let _a = pixel[3];
bmp.set_pixel(
u32::try_from(x).unwrap(),
u32::try_from(y).unwrap(),
bmp::Pixel::new(r, g, b),
);
})
});

bmp.save(output).context("save BMP image to disk")?;
let img: image::ImageBuffer<image::Rgba<u8>, _> =
image::ImageBuffer::from_raw(u32::from(image.width()), u32::from(image.height()), image.data())
.context("invalid image")?;

img.save(output).context("save image to disk")?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ironrdp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#[cfg(test)]
use {
anyhow as _, async_trait as _, bmp as _, ironrdp_blocking as _, ironrdp_cliprdr_native as _, pico_args as _,
anyhow as _, async_trait as _, image as _, ironrdp_blocking as _, ironrdp_cliprdr_native as _, pico_args as _,
rand as _, sspi as _, tokio_rustls as _, tracing as _, tracing_subscriber as _, x509_cert as _,
};

Expand Down

0 comments on commit fe0d9e9

Please sign in to comment.