1use crate::layout::RasterLayout;
2
3pub trait TiffSample: Clone + 'static {
5 fn matches_layout(layout: &RasterLayout) -> bool;
6 fn decode_many(bytes: &[u8]) -> Vec<Self>;
7 fn type_name() -> &'static str;
8}
9
10macro_rules! impl_tiff_sample {
11 ($ty:ty, $format:expr, $bits:expr, $chunk:expr, $from_ne:expr) => {
12 impl TiffSample for $ty {
13 fn matches_layout(layout: &RasterLayout) -> bool {
14 layout.sample_format == $format && layout.bits_per_sample == $bits
15 }
16
17 fn decode_many(bytes: &[u8]) -> Vec<Self> {
18 bytes.chunks_exact($chunk).map($from_ne).collect()
19 }
20
21 fn type_name() -> &'static str {
22 stringify!($ty)
23 }
24 }
25 };
26}
27
28impl_tiff_sample!(u8, 1, 8, 1, |chunk: &[u8]| chunk[0]);
29impl_tiff_sample!(i8, 2, 8, 1, |chunk: &[u8]| chunk[0] as i8);
30impl_tiff_sample!(u16, 1, 16, 2, |chunk: &[u8]| u16::from_ne_bytes(
31 chunk.try_into().unwrap()
32));
33impl_tiff_sample!(i16, 2, 16, 2, |chunk: &[u8]| i16::from_ne_bytes(
34 chunk.try_into().unwrap()
35));
36impl_tiff_sample!(u32, 1, 32, 4, |chunk: &[u8]| u32::from_ne_bytes(
37 chunk.try_into().unwrap()
38));
39impl_tiff_sample!(i32, 2, 32, 4, |chunk: &[u8]| i32::from_ne_bytes(
40 chunk.try_into().unwrap()
41));
42impl_tiff_sample!(f32, 3, 32, 4, |chunk: &[u8]| f32::from_ne_bytes(
43 chunk.try_into().unwrap()
44));
45impl_tiff_sample!(u64, 1, 64, 8, |chunk: &[u8]| u64::from_ne_bytes(
46 chunk.try_into().unwrap()
47));
48impl_tiff_sample!(i64, 2, 64, 8, |chunk: &[u8]| i64::from_ne_bytes(
49 chunk.try_into().unwrap()
50));
51impl_tiff_sample!(f64, 3, 64, 8, |chunk: &[u8]| f64::from_ne_bytes(
52 chunk.try_into().unwrap()
53));