use crate::{cell::ImageCell, core::Image, merger::Point};
use image::Pixel;
use rayon::{
prelude::{IndexedParallelIterator, ParallelIterator},
slice::ParallelSlice,
};
use std::{marker::Sync, ops::DerefMut};
pub fn paste<P, Container>(
bottom: &ImageCell<P, image::ImageBuffer<P, Vec<<P as Pixel>::Subpixel>>>,
top: &Image<P, image::ImageBuffer<P, Container>>,
loc: Point,
) where
P: Pixel + Sync,
<P as Pixel>::Subpixel: Sync,
Container: DerefMut<Target = [P::Subpixel]>,
{
let image_width = top.width();
top.par_chunks_exact(<P as Pixel>::CHANNEL_COUNT as usize)
.enumerate()
.for_each(|(index, chunk)| {
let x = index as u32 % image_width;
let y = index as u32 / image_width;
let canvas_x = loc.x + x;
let canvas_y = loc.y + y;
let pixel = <P as Pixel>::from_slice(chunk);
unsafe {
let mut handout = bottom.request_handout(canvas_x, canvas_y);
handout.unsafe_put_pixel(*pixel);
}
});
}