async_tiff/
tile.rs

1use bytes::Bytes;
2
3use crate::decoder::DecoderRegistry;
4use crate::error::AsyncTiffResult;
5use crate::tiff::tags::{CompressionMethod, PhotometricInterpretation};
6use crate::tiff::{TiffError, TiffUnsupportedError};
7
8/// A TIFF Tile response.
9///
10/// This contains the required information to decode the tile. Decoding is separated from fetching
11/// so that sync and async operations can be separated and non-blocking.
12///
13/// This is returned by `fetch_tile`.
14#[derive(Debug)]
15pub struct Tile {
16    pub(crate) x: usize,
17    pub(crate) y: usize,
18    pub(crate) compressed_bytes: Bytes,
19    pub(crate) compression_method: CompressionMethod,
20    pub(crate) photometric_interpretation: PhotometricInterpretation,
21    pub(crate) jpeg_tables: Option<Bytes>,
22}
23
24impl Tile {
25    /// The column index of this tile.
26    pub fn x(&self) -> usize {
27        self.x
28    }
29
30    /// The row index of this tile.
31    pub fn y(&self) -> usize {
32        self.y
33    }
34
35    /// Access the compressed bytes underlying this tile.
36    ///
37    /// Note that [`Bytes`] is reference-counted, so it is very cheap to clone if needed.
38    pub fn compressed_bytes(&self) -> &Bytes {
39        &self.compressed_bytes
40    }
41
42    /// Access the compression tag representing this tile.
43    pub fn compression_method(&self) -> CompressionMethod {
44        self.compression_method
45    }
46
47    /// Access the photometric interpretation tag representing this tile.
48    pub fn photometric_interpretation(&self) -> PhotometricInterpretation {
49        self.photometric_interpretation
50    }
51
52    /// Access the JPEG Tables, if any, from the IFD producing this tile.
53    ///
54    /// Note that [`Bytes`] is reference-counted, so it is very cheap to clone if needed.
55    pub fn jpeg_tables(&self) -> Option<&Bytes> {
56        self.jpeg_tables.as_ref()
57    }
58
59    /// Decode this tile.
60    ///
61    /// Decoding is separate from fetching so that sync and async operations do not block the same
62    /// runtime.
63    pub fn decode(self, decoder_registry: &DecoderRegistry) -> AsyncTiffResult<Bytes> {
64        let decoder = decoder_registry
65            .as_ref()
66            .get(&self.compression_method)
67            .ok_or(TiffError::UnsupportedError(
68                TiffUnsupportedError::UnsupportedCompressionMethod(self.compression_method),
69            ))?;
70
71        decoder.decode_tile(
72            self.compressed_bytes.clone(),
73            self.photometric_interpretation,
74            self.jpeg_tables.as_deref(),
75        )
76    }
77}