| 18 | |
| 19 | impl<'pixels> PixelsSource<'pixels, '_> { |
| 20 | pub(crate) fn for_pixels(pixels: SeaCow<'pixels, RGBA>, width: u32, height: u32, stride: u32) -> Result<Self, Error> { |
| 21 | if stride < width || height == 0 || width == 0 { |
| 22 | return Err(Error::ValueOutOfRange); |
| 23 | } |
| 24 | let stride = stride as usize; |
| 25 | let width = width as usize; |
| 26 | let height = height as usize; |
| 27 | |
| 28 | let slice = pixels.as_slice(); |
| 29 | let min_area = stride.checked_mul(height).and_then(|a| a.checked_add(width)).ok_or(Error::ValueOutOfRange)? - stride; |
| 30 | if slice.len() < min_area { |
| 31 | return Err(Error::BufferTooSmall); |
| 32 | } |
| 33 | |
| 34 | let rows = SeaCow::boxed(slice.chunks(stride).map(|row| Pointer(row.as_ptr())).take(height).collect()); |
| 35 | Ok(Self::Pixels { rows, pixels: Some(pixels) }) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | pub(crate) struct DynamicRows<'pixels, 'rows> { |