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 replace() and overlay() functions #666

Merged
merged 3 commits into from
Jun 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions src/compose.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Functions for composing one or more images.

use std::cmp::min;

use image::math::Rect;
use image::Pixel;

Expand Down Expand Up @@ -160,3 +162,117 @@ where
}
}
}

/// Replaces the pixels in the `bottom` image with the pixels from the top image starting from the
/// given `(x, y)` coordinates in the `bottom` image and starting from `(0, 0)` in the `top` image.
///
/// # Panics
///
/// - If `x >= bottom.width()`
/// - If `y >= bottom.height()`
///
/// # Examples
/// ```
/// use imageproc::compose::replace;
/// use imageproc::gray_image;
///
/// let bottom = gray_image!(
/// 0, 0, 0, 0, 0, 0;
/// 0, 0, 0, 0, 0, 0;
/// 0, 0, 0, 0, 0, 0;
/// 0, 0, 0, 0, 0, 0;
/// 0, 0, 0, 0, 0, 0;
/// 0, 0, 0, 0, 0, 0);
///
/// let top = gray_image!(
/// 1, 1;
/// 1, 1;
/// 1, 1);
///
/// let replaced = replace(&bottom, &top, 3, 1);
///
/// assert_eq!(replaced, gray_image!(
/// 0, 0, 0, 0, 0, 0;
/// 0, 0, 0, 1, 1, 0;
/// 0, 0, 0, 1, 1, 0;
/// 0, 0, 0, 1, 1, 0;
/// 0, 0, 0, 0, 0, 0;
/// 0, 0, 0, 0, 0, 0));
/// ```
pub fn replace<P>(bottom: &Image<P>, top: &Image<P>, x: u32, y: u32) -> Image<P>
where
P: Pixel,
{
let mut bottom = bottom.clone();
replace_mut(&mut bottom, top, x, y);
bottom
}
#[doc=generate_mut_doc_comment!("replace")]
pub fn replace_mut<P>(bottom: &mut Image<P>, top: &Image<P>, x: u32, y: u32)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the error type on the GenericImage function is a bit clunky.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't see copy_from(), I think the functions are similar but being based on GenericImage may also have performance issues as we've found previously.

Also technically it is an image processing operation so belongs here more than in image but I don't feel too strongly about that.

where
P: Pixel,
{
assert!(x < bottom.width());
assert!(y < bottom.height());

let x_end = min(bottom.width() - 1, x + top.width());
let y_end = min(bottom.height() - 1, y + top.height());

for y_bot in y..y_end {
for x_bot in x..x_end {
bottom.put_pixel(x_bot, y_bot, *top.get_pixel(x_bot - x, y_bot - y));
}
}
}

/// Blends the pixels in the `bottom` image with the pixels from the top image starting from the
/// given `(x, y)` coordinates in the `bottom` image and starting from `(0, 0)` in the `top` image.
///
/// # Panics
///
/// - If `x >= bottom.width()`
/// - If `y >= bottom.height()`
///
/// # Examples
/// ```
/// use imageproc::compose::overlay;
/// use imageproc::definitions::Image;
/// use image::LumaA;
///
/// let bottom = Image::from_pixel(4, 4, LumaA([0u8, 255]));
/// let top = Image::from_pixel(4, 4, LumaA([255u8, 0]));
///
/// let overlay = overlay(&bottom, &top, 0, 0);
///
/// assert_eq!(overlay, bottom);
/// ```
pub fn overlay<P>(bottom: &Image<P>, top: &Image<P>, x: u32, y: u32) -> Image<P>
where
P: Pixel,
{
let mut bottom = bottom.clone();
overlay_mut(&mut bottom, top, x, y);
bottom
}
#[doc=generate_mut_doc_comment!("overlay")]
pub fn overlay_mut<P>(bottom: &mut Image<P>, top: &Image<P>, x: u32, y: u32)
where
P: Pixel,
{
assert!(x < bottom.width());
assert!(y < bottom.height());

let x_end = min(bottom.width() - 1, x + top.width());
let y_end = min(bottom.height() - 1, y + top.height());

for y_bot in y..y_end {
for x_bot in x..x_end {
let mut bottom_pixel = *bottom.get_pixel(x_bot, y_bot);
let top_pixel = bottom.get_pixel(x_bot - x, y_bot - y);

bottom_pixel.blend(top_pixel);

bottom.put_pixel(x_bot, y_bot, bottom_pixel);
}
}
}
Loading