Skip to content

added predictors #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Jun 12, 2025
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d49342c
added predictors
feefladder Apr 2, 2025
df407ba
improved tests, fixed some bugs
feefladder Apr 2, 2025
c37e067
added docs+doctests
feefladder Apr 2, 2025
9f704d8
fixed clippy, doctest improvements
feefladder Apr 2, 2025
6c9a8cf
added padding test, fixed corresponding bugs, removed printlns
feefladder Apr 2, 2025
a3291b0
Remove registry
kylebarron Apr 2, 2025
b3640c0
Rename to Unpredict
kylebarron Apr 2, 2025
67eca9c
Change to pub(crate) fields
kylebarron Apr 2, 2025
a50fb81
Change to pub(crate)
kylebarron Apr 2, 2025
2b3fe03
Remove lifetime and store a single element for bits_per_pixel
kylebarron Apr 2, 2025
d754e14
Remove unused planar configuration
kylebarron Apr 2, 2025
cda2bcc
chunk_width and chunk_height without unwrap
kylebarron Apr 2, 2025
4787983
Move PredictorInfo into predictor.rs
kylebarron Apr 2, 2025
931f58d
Remove unnecessary doctests and unused code
kylebarron Apr 2, 2025
2ac868a
Only call chunks_* once
kylebarron Apr 2, 2025
be74506
Ensure no copies when endianness matches system endianness
kylebarron Apr 2, 2025
a0ff033
added planar_configuration back, updated bits_per_pixel and added tests
feefladder Apr 2, 2025
26176f9
silly clippy things
feefladder Apr 3, 2025
111630b
removed UnPredict trait in favour of separate functions; small change…
feefladder Apr 3, 2025
73d4894
added doc comment clarifying that strips are also tiles
feefladder Apr 3, 2025
5286e17
Update src/tiff/error.rs
feefladder Jun 10, 2025
dc46f8a
Apply suggestions from @weji14
feefladder Jun 10, 2025
9b3afc8
made floating point predictor(s) use
feefladder Jun 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/cog.rs
Original file line number Diff line number Diff line change
@@ -23,7 +23,6 @@ mod test {
use std::io::BufReader;
use std::sync::Arc;

use crate::decoder::DecoderRegistry;
use crate::metadata::{PrefetchBuffer, TiffMetadataReader};
use crate::reader::{AsyncFileReader, ObjectReader};

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

let ifd = &tiff.ifds[1];
let decoder_registry = DecoderRegistry::default();
let tile = ifd.fetch_tile(0, 0, reader.as_ref()).await.unwrap();
let tile = tile.decode(&decoder_registry).unwrap();
let tile = tile.decode(&Default::default()).unwrap();
std::fs::write("img.buf", tile).unwrap();
}

4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -15,6 +15,10 @@ pub enum AsyncTiffError {
#[error("General error: {0}")]
General(String),

/// Tile index error
#[error("Tile index out of bounds: {0}, {1}")]
TileIndexError(u32, u32),

/// IO Error.
#[error(transparent)]
IOError(#[from] std::io::Error),
17 changes: 15 additions & 2 deletions src/ifd.rs
Original file line number Diff line number Diff line change
@@ -6,7 +6,8 @@ use num_enum::TryFromPrimitive;

use crate::error::{AsyncTiffError, AsyncTiffResult};
use crate::geo::{GeoKeyDirectory, GeoKeyTag};
use crate::reader::AsyncFileReader;
use crate::predictor::PredictorInfo;
use crate::reader::{AsyncFileReader, Endianness};
use crate::tiff::tags::{
CompressionMethod, PhotometricInterpretation, PlanarConfiguration, Predictor, ResolutionUnit,
SampleFormat, Tag,
@@ -21,6 +22,8 @@ const DOCUMENT_NAME: u16 = 269;
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct ImageFileDirectory {
pub(crate) endianness: Endianness,

pub(crate) new_subfile_type: Option<u32>,

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

impl ImageFileDirectory {
/// Create a new ImageFileDirectory from tag data
pub fn from_tags(tag_data: HashMap<Tag, Value>) -> AsyncTiffResult<Self> {
pub fn from_tags(
tag_data: HashMap<Tag, Value>,
endianness: Endianness,
) -> AsyncTiffResult<Self> {
let mut new_subfile_type = None;
let mut image_width = None;
let mut image_height = None;
@@ -349,6 +355,7 @@ impl ImageFileDirectory {
PlanarConfiguration::Chunky
};
Ok(Self {
endianness,
new_subfile_type,
image_width: image_width.expect("image_width not found"),
image_height: image_height.expect("image_height not found"),
@@ -689,6 +696,8 @@ impl ImageFileDirectory {
Ok(Tile {
x,
y,
predictor: self.predictor.unwrap_or(Predictor::None),
predictor_info: PredictorInfo::from_ifd(self),
compressed_bytes,
compression_method: self.compression,
photometric_interpretation: self.photometric_interpretation,
@@ -705,6 +714,8 @@ impl ImageFileDirectory {
) -> AsyncTiffResult<Vec<Tile>> {
assert_eq!(x.len(), y.len(), "x and y should have same len");

let predictor_info = PredictorInfo::from_ifd(self);

// 1: Get all the byte ranges for all tiles
let byte_ranges = x
.iter()
@@ -724,6 +735,8 @@ impl ImageFileDirectory {
let tile = Tile {
x,
y,
predictor: self.predictor.unwrap_or(Predictor::None),
predictor_info,
compressed_bytes,
compression_method: self.compression,
photometric_interpretation: self.photometric_interpretation,
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ pub mod error;
pub mod geo;
mod ifd;
pub mod metadata;
pub mod predictor;
pub mod tiff;
mod tile;

9 changes: 6 additions & 3 deletions src/metadata/reader.rs
Original file line number Diff line number Diff line change
@@ -226,7 +226,7 @@ impl ImageFileDirectoryReader {
let (tag, value) = self.read_tag(fetch, tag_idx).await?;
tags.insert(tag, value);
}
ImageFileDirectory::from_tags(tags)
ImageFileDirectory::from_tags(tags, self.endianness)
}

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

#[cfg(test)]
mod test {
use crate::{
metadata::{reader::read_tag, MetadataFetch},
reader::Endianness,
tiff::{tags::Tag, Value},
};
use bytes::Bytes;
use futures::FutureExt;

use super::*;

impl MetadataFetch for Bytes {
fn fetch(
&self,
753 changes: 753 additions & 0 deletions src/predictor.rs

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/tiff/error.rs
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ use std::sync::Arc;
use jpeg::UnsupportedFeature;

use super::ifd::Value;
use super::tags::Predictor;
use super::tags::{
CompressionMethod, PhotometricInterpretation, PlanarConfiguration, SampleFormat, Tag,
};
@@ -152,6 +153,7 @@ pub enum TiffUnsupportedError {
UnknownInterpretation,
UnknownCompressionMethod,
UnsupportedCompressionMethod(CompressionMethod),
UnsupportedPredictor(Predictor),
UnsupportedSampleDepth(u8),
UnsupportedSampleFormat(Vec<SampleFormat>),
// UnsupportedColorType(ColorType),
@@ -193,6 +195,9 @@ impl fmt::Display for TiffUnsupportedError {
UnsupportedCompressionMethod(method) => {
write!(fmt, "Compression method {:?} is unsupported", method)
}
UnsupportedPredictor(p) => {
write!(fmt, "Predictor {p:?} is unsupported")
}
UnsupportedSampleDepth(samples) => {
write!(fmt, "{} samples per pixel is unsupported.", samples)
}
25 changes: 22 additions & 3 deletions src/tile.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,8 @@ use bytes::Bytes;

use crate::decoder::DecoderRegistry;
use crate::error::AsyncTiffResult;
use crate::tiff::tags::{CompressionMethod, PhotometricInterpretation};
use crate::predictor::{fix_endianness, unpredict_float, unpredict_hdiff, PredictorInfo};
use crate::tiff::tags::{CompressionMethod, PhotometricInterpretation, Predictor};
use crate::tiff::{TiffError, TiffUnsupportedError};

/// A TIFF Tile response.
@@ -11,10 +12,14 @@ use crate::tiff::{TiffError, TiffUnsupportedError};
/// so that sync and async operations can be separated and non-blocking.
///
/// This is returned by `fetch_tile`.
///
/// A strip of a stripped tiff is an image-width, rows-per-strip tile.
#[derive(Debug)]
pub struct Tile {
pub(crate) x: usize,
pub(crate) y: usize,
pub(crate) predictor: Predictor,
pub(crate) predictor_info: PredictorInfo,
pub(crate) compressed_bytes: Bytes,
pub(crate) compression_method: CompressionMethod,
pub(crate) photometric_interpretation: PhotometricInterpretation,
@@ -68,10 +73,24 @@ impl Tile {
TiffUnsupportedError::UnsupportedCompressionMethod(self.compression_method),
))?;

decoder.decode_tile(
let decoded_tile = decoder.decode_tile(
self.compressed_bytes.clone(),
self.photometric_interpretation,
self.jpeg_tables.as_deref(),
)
)?;

match self.predictor {
Predictor::None => Ok(fix_endianness(
decoded_tile,
self.predictor_info.endianness(),
self.predictor_info.bits_per_sample(),
)),
Predictor::Horizontal => {
unpredict_hdiff(decoded_tile, &self.predictor_info, self.x as _)
}
Predictor::FloatingPoint => {
unpredict_float(decoded_tile, &self.predictor_info, self.x as _, self.y as _)
}
Comment on lines +83 to +93
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the trait and only have crate-public functions now

Copy link
Contributor Author

@feefladder feefladder Apr 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also help with less-copy as mocked up in #87, since float predictor doesn't do in-place modification and having a shared trait doesn't allow the differentiation

}
}
}