-
Notifications
You must be signed in to change notification settings - Fork 8
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
Replace show image crate #55
Merged
Merged
Changes from 5 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
3207970
Replace show-image crate
ashbeitz d276162
Replace show-image crate
ashbeitz 838d98f
Replace show-image crate
ashbeitz ca7be65
Replace show-image crate
ashbeitz d15420a
Replace show-image crate
ashbeitz d2143e7
Replace show-image crate
ashbeitz 1ed9b63
Replace show-image crate
ashbeitz d17ea8e
Replace show-image crate
ashbeitz c8eb1a0
Replace show-image crate
ashbeitz 8cce500
Replace show-image crate
ashbeitz cac45e4
Replace show-image crate
ashbeitz 8bc828a
Replace show-image crate
ashbeitz 6a2a990
Replace show-image crate
ashbeitz a914a0f
Replace show-image crate
ashbeitz ee43fc9
Replace show-image crate
ashbeitz 6a27737
Replace show-image crate
ashbeitz c82a4a3
Replace show-image crate
ashbeitz a81c555
Replace show-image crate
ashbeitz fd547bc
Replace show-image crate
ashbeitz 4aa37a6
Replace show-image crate
ashbeitz 6302790
Replace show-image crate
ashbeitz beee7e2
Replace show-image crate
ashbeitz 86f7193
Replace show-image crate
ashbeitz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
// SPDX-License-Identifier: MIT | ||
|
||
use image::{imageops::FilterType, DynamicImage}; | ||
use sdl2::pixels::{Color, PixelFormatEnum}; | ||
use sdl2::rect::Rect; | ||
use sdl2::render::WindowCanvas; | ||
use sdl2::surface::Surface; | ||
use sdl2::Sdl; | ||
|
||
// This module is based on SDL2 (Simple DirectMedia Layer). It complements the sdl2 crate by providing | ||
// methods that make it easier to render images. | ||
|
||
/// Create a canvas with an enclosing window. | ||
/// | ||
/// # Arguments | ||
/// * `sdl_context` - The SDL context. | ||
/// * `window_title` - The window's title. | ||
/// * `window_width` - The window's width. | ||
/// * `window_height` - The window's height. | ||
pub fn create_canvas( | ||
sdl_context: &mut Sdl, | ||
window_title: &str, | ||
window_width: u32, | ||
window_height: u32, | ||
) -> Result<WindowCanvas, String> { | ||
let video_subsystem = sdl_context.video()?; | ||
|
||
let window = video_subsystem | ||
.window(window_title, window_width, window_height) | ||
.position_centered() | ||
.allow_highdpi() | ||
.build() | ||
.map_err(|err| format!("{}", err))?; | ||
|
||
let mut canvas = window.into_canvas().build().map_err(|err| format!("{}", err))?; | ||
|
||
// Set the background color to black. | ||
canvas.set_draw_color(Color::RGB(0, 0, 0)); | ||
|
||
Ok(canvas) | ||
} | ||
|
||
/// Resize an image to fit inside a canvas. | ||
/// | ||
/// # Arguments | ||
/// * `image` - The image that needs to be resized. | ||
/// * `canvas` - The canvas that it needs to fit in. | ||
pub fn resize_image_to_fit_in_canvas( | ||
image: DynamicImage, | ||
canvas: &WindowCanvas, | ||
) -> Result<DynamicImage, String> { | ||
let (window_width, window_height): (u32, u32) = canvas.output_size()?; | ||
|
||
let width_scale = window_width as f32 / image.width() as f32; | ||
let height_scale = window_height as f32 / image.height() as f32; | ||
let scale: f32 = height_scale.min(width_scale); | ||
|
||
let resized_image_width = (scale * image.width() as f32) as u32; | ||
let resized_image_height = (scale * image.height() as f32) as u32; | ||
|
||
Ok(image.resize(resized_image_width, resized_image_height, FilterType::Triangle)) | ||
} | ||
|
||
/// Render an image to a canvas. | ||
/// | ||
/// # Arguments | ||
/// * `image` - The image that we want to render. | ||
/// * `canvas` - The canvas that will render the image. | ||
pub fn render_image_to_canvas( | ||
image: &DynamicImage, | ||
canvas: &mut WindowCanvas, | ||
) -> Result<(), String> { | ||
// Prepare the image for copying it to a surface. | ||
let rgb_image = image.to_rgb8(); | ||
let mut image_buffer = rgb_image.into_raw(); | ||
|
||
let image_width = image.width(); | ||
let image_height = image.height(); | ||
// The pitch is the width of the texture times the size of a single pixel in bytes. | ||
// Since we are using 24 bit pixels (RGB24), we need to mutiple the width by 3. | ||
let image_pitch: u32 = image_width * 3; | ||
|
||
let surface = Surface::from_data( | ||
&mut image_buffer, | ||
image_width, | ||
image_height, | ||
image_pitch, | ||
PixelFormatEnum::RGB24, | ||
) | ||
.map_err(|err| format!("{}", err))?; | ||
|
||
let texture_creator = canvas.texture_creator(); | ||
let texture = texture_creator.create_texture_from_surface(surface).unwrap(); | ||
jorchiu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Render the image. | ||
canvas.clear(); | ||
canvas.copy(&texture, None, Rect::new(0, 0, image_width, image_height))?; | ||
canvas.present(); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ | |
|
||
pub mod constants; | ||
pub mod consumer_config; | ||
pub mod image_rendering; | ||
pub mod provider_config; | ||
pub mod utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for rust libraries it's generally not recommended to use strings for the error type. this makes it hard to do anything but log the error. I think other places in Ibeji are using
Status
(e.g. here) so ideally we should use that here tooThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used String to be consistent with the sdl2 crate's methods, which use String.
Status comes from the tonic crate and is currently only used in conjunction with gRPC-related calls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Converting errors can be done with
map_err
. It's also worth mentioning that the library owner would prefer to use a proper error type but hasn't made the change due to concerns with breaking changes: Rust-SDL2/rust-sdl2#1053Then
Status
wouldn't be the right thing to use, we should find something else. Consider defining your own error type similar to what Freyja does, or you could useanyhow
(not recommended for libraries but easy to use and better thanString
) orthiserror
, or even reusestd::io::Error