use std::cmp::min;
use image::Pixel;
use image::math::Rect;
use crate::definitions::Image;
pub fn crop<P>(image: &Image<P>, rect: Rect) -> Image<P>
where
P: Pixel,
{
assert!(rect.x + rect.width <= image.width());
assert!(rect.y + rect.height <= image.height());
Image::from_fn(rect.width, rect.height, |x, y| {
*image.get_pixel(rect.x + x, rect.y + y)
})
}
#[cfg(feature = "rayon")]
#[doc = generate_parallel_doc_comment!("crop")]
pub fn crop_parallel<P>(image: &Image<P>, rect: Rect) -> Image<P>
where
P: Pixel + Send + Sync,
P::Subpixel: Send + Sync,
{
assert!(rect.x + rect.width <= image.width());
assert!(rect.y + rect.height <= image.height());
Image::from_par_fn(rect.width, rect.height, |x, y| {
*image.get_pixel(rect.x + x, rect.y + y)
})
}
pub fn flip_horizontal<P>(image: &Image<P>) -> Image<P>
where
P: Pixel,
{
let mut out = image.clone();
flip_horizontal_mut(&mut out);
out
}
#[doc=generate_mut_doc_comment!("flip_horizontal")]
pub fn flip_horizontal_mut<P>(image: &mut Image<P>)
where
P: Pixel,
{
for y in 0..image.height() {
for x in 0..(image.width() / 2) {
let flipped_x = image.width() - x - 1;
let pixel = *image.get_pixel(x, y);
let flipped_pixel = *image.get_pixel(flipped_x, y);
image.put_pixel(x, y, flipped_pixel);
image.put_pixel(flipped_x, y, pixel);
}
}
}
pub fn flip_vertical<P>(image: &Image<P>) -> Image<P>
where
P: Pixel,
{
let mut out = image.clone();
flip_vertical_mut(&mut out);
out
}
#[doc=generate_mut_doc_comment!("flip_vertical")]
pub fn flip_vertical_mut<P>(image: &mut Image<P>)
where
P: Pixel,
{
for y in 0..(image.height() / 2) {
for x in 0..image.width() {
let flipped_y = image.height() - y - 1;
let pixel = *image.get_pixel(x, y);
let flipped_pixel = *image.get_pixel(x, flipped_y);
image.put_pixel(x, y, flipped_pixel);
image.put_pixel(x, flipped_y, pixel);
}
}
}
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)
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));
}
}
}
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);
}
}
}