Skip to main content

tiff_core/
layout.rs

1/// Raster layout information normalized from TIFF tags.
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub struct RasterLayout {
4    pub width: usize,
5    pub height: usize,
6    pub samples_per_pixel: usize,
7    pub bits_per_sample: u16,
8    pub bytes_per_sample: usize,
9    pub sample_format: u16,
10    pub planar_configuration: u16,
11    pub predictor: u16,
12}
13
14impl RasterLayout {
15    pub fn pixel_stride_bytes(&self) -> usize {
16        self.samples_per_pixel * self.bytes_per_sample
17    }
18
19    pub fn packed_row_bytes_for_width(&self, width: usize) -> usize {
20        width
21            .checked_mul(self.samples_per_pixel)
22            .and_then(|samples| samples.checked_mul(self.bits_per_sample as usize))
23            .map(|bits| bits.div_ceil(8))
24            .unwrap_or(usize::MAX)
25    }
26
27    pub fn row_bytes(&self) -> usize {
28        self.width * self.pixel_stride_bytes()
29    }
30
31    pub fn packed_row_bytes(&self) -> usize {
32        self.packed_row_bytes_for_width(self.width)
33    }
34
35    pub fn packed_sample_plane_row_bytes_for_width(&self, width: usize) -> usize {
36        width
37            .checked_mul(self.bits_per_sample as usize)
38            .map(|bits| bits.div_ceil(8))
39            .unwrap_or(usize::MAX)
40    }
41
42    pub fn sample_plane_row_bytes(&self) -> usize {
43        self.width * self.bytes_per_sample
44    }
45
46    pub fn packed_sample_plane_row_bytes(&self) -> usize {
47        self.packed_sample_plane_row_bytes_for_width(self.width)
48    }
49}