1use super::{point::Point, rect::Rect};
2use rayon::prelude::{
3 IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator, ParallelSlice,
4 ParallelSliceMut,
5};
6use serde::{Deserialize, Serialize};
7
8#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash)]
9pub struct Chunk {
10 point: Point,
11 rect: Rect,
12}
13
14impl Chunk {
15 pub fn new(point: Point, rect: Rect) -> Self {
16 Self { point, rect }
17 }
18
19 pub fn point(&self) -> &Point {
20 &self.point
21 }
22
23 pub fn rect(&self) -> &Rect {
24 &self.rect
25 }
26
27 pub fn extract<'a>(&self, data: &'a [u8], full: &Rect) -> Vec<Vec<u8>> {
28 data.par_chunks(full.width())
29 .skip(self.point.y())
30 .take(self.rect.height())
31 .map(|slice| slice[self.point.x()..self.point.x() + self.rect.width()].to_vec())
32 .collect::<Vec<_>>()
33 }
34
35 pub fn apply(&self, data: &mut [u8], slices: &Vec<Vec<u8>>, full: &Rect) {
36 data.par_chunks_mut(full.width())
37 .skip(self.point.y())
38 .take(self.rect.height())
39 .map(|slice| &mut slice[self.point.x()..self.point.x() + self.rect.width()])
40 .zip(slices.par_iter())
41 .map(|(dst, src)| dst.copy_from_slice(src))
42 .collect::<Vec<_>>();
43 }
44}