Skip to content

Commit c37e067

Browse files
committed
added docs+doctests
1 parent df407ba commit c37e067

File tree

3 files changed

+69
-13
lines changed

3 files changed

+69
-13
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ mod tile;
1515

1616
pub use cog::TIFF;
1717
pub use ifd::ImageFileDirectory;
18-
pub use tile::Tile;
18+
pub use tile::{PredictorInfo, Tile};

src/predictor.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
//! Predictors for no predictor, horizontal and floating-point
12
use std::collections::HashMap;
23
use std::fmt::Debug;
34

4-
use byteorder::NativeEndian;
55
use bytes::{Bytes, BytesMut};
66
// use tiff::decoder::DecodingResult;
77

@@ -524,8 +524,11 @@ mod test {
524524
#[test]
525525
fn test_fpredict_f32() {
526526
// let's take this 2-value image
527-
let expected: Vec<u8> = [42.0f32, 43.0].iter().flat_map(|f| f.to_le_bytes()).collect();
528-
assert_eq!(expected, vec![0x0,0x0,0x28,0x42,0x0,0x0,0x2c,0x42]);
527+
let expected: Vec<u8> = [42.0f32, 43.0]
528+
.iter()
529+
.flat_map(|f| f.to_le_bytes())
530+
.collect();
531+
assert_eq!(expected, vec![0x0, 0x0, 0x28, 0x42, 0x0, 0x0, 0x2c, 0x42]);
529532
let info = PredictorInfo {
530533
endianness: Endianness::LittleEndian,
531534
image_width: 2,

src/tile.rs

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,45 @@ use crate::tiff::{TiffError, TiffUnsupportedError};
1818
///
1919
#[derive(Debug, Clone, Copy)]
2020
pub struct PredictorInfo<'a> {
21+
/// endianness
2122
pub endianness: Endianness,
23+
/// width of the image in pixels
2224
pub image_width: u32,
25+
/// height of the image in pixels
2326
pub image_height: u32,
27+
/// chunk width in pixels
28+
///
29+
/// If this is a stripped tiff, `chunk_width=image_width`
2430
pub chunk_width: u32,
31+
/// chunk height in pixels
2532
pub chunk_height: u32,
33+
/// bits per sample, as an array
34+
///
35+
/// Can also be a single value, in which case it applies to all samples
2636
pub bits_per_sample: &'a [u16], // maybe say that we only support a single bits_per_sample?
37+
/// number of samples per pixel
2738
pub samples_per_pixel: u16,
39+
/// sample format for each sample
40+
///
41+
/// There is no decoding implementation in this crate (or libtiff) for mixed sample formats
2842
pub sample_format: &'a [SampleFormat], // and a single sample_format?
43+
/// planar configuration
44+
///
45+
/// determines the bits per pixel
2946
pub planar_configuration: PlanarConfiguration,
3047
}
3148

3249
impl PredictorInfo<'_> {
33-
/// chunk height in pixels, taking padding into account
50+
/// chunk width in pixels, taking padding into account
3451
///
3552
/// strips are considered image-width chunks
3653
///
3754
/// # Example
3855
///
3956
/// ```rust
57+
/// # use async_tiff::tiff::tags::{SampleFormat, PlanarConfiguration};
58+
/// # use async_tiff::reader::Endianness;
59+
/// # use async_tiff::PredictorInfo;
4060
/// let info = PredictorInfo {
4161
/// # endianness: Endianness::LittleEndian,
4262
/// image_width: 15,
@@ -47,10 +67,10 @@ impl PredictorInfo<'_> {
4767
/// # samples_per_pixel: 1,
4868
/// # sample_format: &[SampleFormat::IEEEFP],
4969
/// # planar_configuration: PlanarConfiguration::Chunky,
50-
/// }
70+
/// };
5171
///
52-
/// assert_eq!(info.chunk_width_pixels(1).unwrap(), (7))
53-
/// info.chunk_width_pixels(2).unwrap_err()
72+
/// assert_eq!(info.chunk_width_pixels(1).unwrap(), (7));
73+
/// info.chunk_width_pixels(2).unwrap_err();
5474
/// ```
5575
pub fn chunk_width_pixels(&self, x: u32) -> AsyncTiffResult<u32> {
5676
if x >= self.chunks_across() {
@@ -66,6 +86,31 @@ impl PredictorInfo<'_> {
6686
}
6787
}
6888

89+
/// chunk height in pixels, taking padding into account
90+
///
91+
/// strips are considered image-width chunks
92+
///
93+
/// # Example
94+
///
95+
/// ```rust
96+
/// # use async_tiff::tiff::tags::{SampleFormat, PlanarConfiguration};
97+
/// # use async_tiff::reader::Endianness;
98+
/// # use async_tiff::PredictorInfo;
99+
/// let info = PredictorInfo {
100+
/// # endianness: Endianness::LittleEndian,
101+
/// image_width: 15,
102+
/// image_height: 15,
103+
/// chunk_width: 8,
104+
/// chunk_height: 8,
105+
/// # bits_per_sample: &[32],
106+
/// # samples_per_pixel: 1,
107+
/// # sample_format: &[SampleFormat::IEEEFP],
108+
/// # planar_configuration: PlanarConfiguration::Chunky,
109+
/// };
110+
///
111+
/// assert_eq!(info.chunk_height_pixels(1).unwrap(), (7));
112+
/// info.chunk_height_pixels(2).unwrap_err();
113+
/// ```
69114
pub fn chunk_height_pixels(&self, y: u32) -> AsyncTiffResult<u32> {
70115
if y >= self.chunks_down() {
71116
Err(crate::error::AsyncTiffError::TileIndexError(
@@ -91,19 +136,27 @@ impl PredictorInfo<'_> {
91136
/// it to be a single value that applies to all samples.
92137
///
93138
/// Libtiff and image-tiff do not support mixed bits per sample, but we give the possibility
139+
/// unless you also have PlanarConfiguration::Planar, at which point the first is taken
94140
pub fn bits_per_pixel(&self) -> usize {
95-
if self.bits_per_sample.len() == 1 {
96-
self.samples_per_pixel as usize * self.bits_per_sample[0] as usize
97-
} else {
98-
assert_eq!(self.samples_per_pixel as usize, self.bits_per_sample.len());
99-
self.bits_per_sample.iter().map(|v| *v as usize).product()
141+
match self.planar_configuration {
142+
PlanarConfiguration::Chunky => {
143+
if self.bits_per_sample.len() == 1 {
144+
self.samples_per_pixel as usize * self.bits_per_sample[0] as usize
145+
} else {
146+
assert_eq!(self.samples_per_pixel as usize, self.bits_per_sample.len());
147+
self.bits_per_sample.iter().map(|v| *v as usize).product()
148+
}
149+
}
150+
PlanarConfiguration::Planar => self.bits_per_sample[0] as usize,
100151
}
101152
}
102153

154+
/// The number of chunks in the horizontal (x) direction
103155
pub fn chunks_across(&self) -> u32 {
104156
self.image_width.div_ceil(self.chunk_width)
105157
}
106158

159+
/// The number of chunks in the vertical (y) direction
107160
pub fn chunks_down(&self) -> u32 {
108161
self.image_height.div_ceil(self.chunk_height)
109162
}

0 commit comments

Comments
 (0)