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 08af03380c
No known key found for this signature in database
GPG Key ID: 582B3C35EAFE46F8
5 changed files with 111 additions and 8 deletions

View File

@ -4,4 +4,5 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [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.

53
src/dimensions.rs Normal file
View File

@ -0,0 +1,53 @@
#[derive(Debug, Clone, Copy)]
pub struct Dimensions {
pub width: u32,
pub height: u32,
}
impl Dimensions {
pub fn new(width: u32, height: u32) -> Self {
Self { width, height }
}
pub fn aspect_ratio(&self) -> f32 {
self.width as f32 / self.height as f32
}
}
/// Calculates the dimensions to resize an image to fill the screen while preserving aspect ratio.
/// The image will be scaled to fit the screen without cropping.
///
/// # Arguments
///
/// * `screen` - The dimensions of the screen
/// * `image` - The original dimensions of the image
///
/// # Returns
///
/// * `Dimensions` - The new dimensions that will fit the screen
///
/// # Example
///
/// ```rust
/// let screen = Dimensions::new(1920, 1080);
/// let image = Dimensions::new(3000, 2000);
/// let new_size = calculate_resize_dimensions(screen, image);
/// ```
pub fn calculate_resize_dimensions(screen: Dimensions, image: Dimensions) -> Dimensions {
let screen_ratio = screen.aspect_ratio();
let image_ratio = image.aspect_ratio();
if image_ratio > screen_ratio {
// Image is wider relative to its height than the screen
// Scale based on width
let new_width = screen.width;
let new_height = (screen.width as f32 / image_ratio).round() as u32;
Dimensions::new(new_width, new_height)
} else {
// Image is taller relative to its width than the screen
// Scale based on height
let new_height = screen.height;
let new_width = (screen.height as f32 * image_ratio).round() as u32;
Dimensions::new(new_width, new_height)
}
}

View File

@ -1,12 +1,61 @@
use dimensions::{calculate_resize_dimensions, Dimensions};
use image::ImageReader;
use macroquad::prelude::*; 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() { 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 { loop {
clear_background(LIGHTGRAY); clear_background(BLACK);
draw_texture(&texture, 0., 0., WHITE); if is_quit_requested() || is_key_released(KeyCode::Escape) {
break;
}
display_image(&texture);
next_frame().await next_frame().await
} }
} }