Display test photo with automatic resize

This commit is contained in:
Jean-Loup Beaussart 2024-11-04 00:24:20 +01:00
parent 4c38a6550f
commit 7db242e770
No known key found for this signature in database
GPG Key ID: 582B3C35EAFE46F8
4 changed files with 58 additions and 8 deletions

View File

@ -4,4 +4,5 @@ version = "0.1.0"
edition = "2021"
[dependencies]
macroquad = "0.4.13"
image = "0.25.4"
macroquad = { version = "0.4.13" }

BIN
examples/ferris.png (Stored with Git LFS)

Binary file not shown.

BIN
examples/test.jpg (Stored with Git LFS) Executable file

Binary file not shown.

View File

@ -1,12 +1,61 @@
use dimensions::{calculate_resize_dimensions, Dimensions};
use image::ImageReader;
use macroquad::prelude::*;
#[macroquad::main("Texture")]
mod dimensions;
fn window_conf() -> Conf {
Conf {
window_title: String::from("Photo"),
window_width: 2560,
window_height: 1440,
fullscreen: true,
..Default::default()
}
}
fn display_image(texture: &Texture2D) {
let screen_width = screen_width();
let screen_height = screen_height();
draw_texture(
&texture,
clamp(screen_width - texture.width(), 0.0f32, screen_width) / 2.0f32,
clamp(screen_height - texture.height(), 0.0f32, screen_height) / 2.0f32,
WHITE,
);
}
#[macroquad::main(window_conf)]
async fn main() {
let texture: Texture2D = load_texture("examples/ferris.png").await.unwrap();
show_mouse(false);
let image = ImageReader::open("examples/test.jpg")
.unwrap()
.decode()
.unwrap();
let screen_dim = Dimensions::new(screen_width() as u32, screen_height() as u32);
let image_dim = Dimensions::new(image.width(), image.height());
let new_dim = calculate_resize_dimensions(screen_dim, image_dim);
let image = image.resize(
new_dim.width,
new_dim.height,
image::imageops::FilterType::Lanczos3,
);
let texture = Texture2D::from_rgba8(
image.width().try_into().unwrap(),
image.height().try_into().unwrap(),
&image.into_rgba8().to_vec(),
);
loop {
clear_background(LIGHTGRAY);
draw_texture(&texture, 0., 0., WHITE);
clear_background(BLACK);
if is_quit_requested() || is_key_released(KeyCode::Escape) {
break;
}
display_image(&texture);
next_frame().await
}
}