Skip to content

Commit 6fe3da9

Browse files
feefladderkylebarronweiji14
authored
added predictors (#86)
Added crate-public unpredict_float/hdiff functions. Added a PredictorInfo struct, inspired by tiff2. * added predictors * improved tests, fixed some bugs * added docs+doctests * added padding test, fixed corresponding bugs, removed printlns * Remove registry * Rename to Unpredict * Change to pub(crate) fields * Change to pub(crate) * Remove lifetime and store a single element for bits_per_pixel * Remove unused planar configuration * chunk_width and chunk_height without unwrap * Move PredictorInfo into predictor.rs * Remove unnecessary doctests and unused code * Only call chunks_* once * Ensure no copies when endianness matches system endianness * added planar_configuration back, updated bits_per_pixel and added tests * removed UnPredict trait in favour of separate functions; small change to fix_endianness * added doc comment clarifying that strips are also tiles * made floating point predictor(s) use --------- Co-authored-by: Kyle Barron <[email protected]> Co-authored-by: Wei Ji <[email protected]>
1 parent b1ee9ac commit 6fe3da9

File tree

8 files changed

+807
-11
lines changed

8 files changed

+807
-11
lines changed

src/cog.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ mod test {
2323
use std::io::BufReader;
2424
use std::sync::Arc;
2525

26-
use crate::decoder::DecoderRegistry;
2726
use crate::metadata::{PrefetchBuffer, TiffMetadataReader};
2827
use crate::reader::{AsyncFileReader, ObjectReader};
2928

@@ -51,9 +50,8 @@ mod test {
5150
let tiff = TIFF::new(ifds);
5251

5352
let ifd = &tiff.ifds[1];
54-
let decoder_registry = DecoderRegistry::default();
5553
let tile = ifd.fetch_tile(0, 0, reader.as_ref()).await.unwrap();
56-
let tile = tile.decode(&decoder_registry).unwrap();
54+
let tile = tile.decode(&Default::default()).unwrap();
5755
std::fs::write("img.buf", tile).unwrap();
5856
}
5957

src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ pub enum AsyncTiffError {
1515
#[error("General error: {0}")]
1616
General(String),
1717

18+
/// Tile index error
19+
#[error("Tile index out of bounds: {0}, {1}")]
20+
TileIndexError(u32, u32),
21+
1822
/// IO Error.
1923
#[error(transparent)]
2024
IOError(#[from] std::io::Error),

src/ifd.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use num_enum::TryFromPrimitive;
66

77
use crate::error::{AsyncTiffError, AsyncTiffResult};
88
use crate::geo::{GeoKeyDirectory, GeoKeyTag};
9-
use crate::reader::AsyncFileReader;
9+
use crate::predictor::PredictorInfo;
10+
use crate::reader::{AsyncFileReader, Endianness};
1011
use crate::tiff::tags::{
1112
CompressionMethod, PhotometricInterpretation, PlanarConfiguration, Predictor, ResolutionUnit,
1213
SampleFormat, Tag,
@@ -21,6 +22,8 @@ const DOCUMENT_NAME: u16 = 269;
2122
#[allow(dead_code)]
2223
#[derive(Debug, Clone)]
2324
pub struct ImageFileDirectory {
25+
pub(crate) endianness: Endianness,
26+
2427
pub(crate) new_subfile_type: Option<u32>,
2528

2629
/// The number of columns in the image, i.e., the number of pixels per row.
@@ -143,7 +146,10 @@ pub struct ImageFileDirectory {
143146

144147
impl ImageFileDirectory {
145148
/// Create a new ImageFileDirectory from tag data
146-
pub fn from_tags(tag_data: HashMap<Tag, Value>) -> AsyncTiffResult<Self> {
149+
pub fn from_tags(
150+
tag_data: HashMap<Tag, Value>,
151+
endianness: Endianness,
152+
) -> AsyncTiffResult<Self> {
147153
let mut new_subfile_type = None;
148154
let mut image_width = None;
149155
let mut image_height = None;
@@ -349,6 +355,7 @@ impl ImageFileDirectory {
349355
PlanarConfiguration::Chunky
350356
};
351357
Ok(Self {
358+
endianness,
352359
new_subfile_type,
353360
image_width: image_width.expect("image_width not found"),
354361
image_height: image_height.expect("image_height not found"),
@@ -689,6 +696,8 @@ impl ImageFileDirectory {
689696
Ok(Tile {
690697
x,
691698
y,
699+
predictor: self.predictor.unwrap_or(Predictor::None),
700+
predictor_info: PredictorInfo::from_ifd(self),
692701
compressed_bytes,
693702
compression_method: self.compression,
694703
photometric_interpretation: self.photometric_interpretation,
@@ -705,6 +714,8 @@ impl ImageFileDirectory {
705714
) -> AsyncTiffResult<Vec<Tile>> {
706715
assert_eq!(x.len(), y.len(), "x and y should have same len");
707716

717+
let predictor_info = PredictorInfo::from_ifd(self);
718+
708719
// 1: Get all the byte ranges for all tiles
709720
let byte_ranges = x
710721
.iter()
@@ -724,6 +735,8 @@ impl ImageFileDirectory {
724735
let tile = Tile {
725736
x,
726737
y,
738+
predictor: self.predictor.unwrap_or(Predictor::None),
739+
predictor_info,
727740
compressed_bytes,
728741
compression_method: self.compression,
729742
photometric_interpretation: self.photometric_interpretation,

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub mod error;
99
pub mod geo;
1010
mod ifd;
1111
pub mod metadata;
12+
pub mod predictor;
1213
pub mod tiff;
1314
mod tile;
1415

src/metadata/reader.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl ImageFileDirectoryReader {
226226
let (tag, value) = self.read_tag(fetch, tag_idx).await?;
227227
tags.insert(tag, value);
228228
}
229-
ImageFileDirectory::from_tags(tags)
229+
ImageFileDirectory::from_tags(tags, self.endianness)
230230
}
231231

232232
/// Finish this reader, reading the byte offset of the next IFD
@@ -623,11 +623,14 @@ async fn read_tag_value<F: MetadataFetch>(
623623

624624
#[cfg(test)]
625625
mod test {
626+
use crate::{
627+
metadata::{reader::read_tag, MetadataFetch},
628+
reader::Endianness,
629+
tiff::{tags::Tag, Value},
630+
};
626631
use bytes::Bytes;
627632
use futures::FutureExt;
628633

629-
use super::*;
630-
631634
impl MetadataFetch for Bytes {
632635
fn fetch(
633636
&self,

0 commit comments

Comments
 (0)