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

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Expand Up @@ -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};

Expand Down Expand Up @@ -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();
}

Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
17 changes: 15 additions & 2 deletions src/ifd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod error;
pub mod geo;
mod ifd;
pub mod metadata;
pub mod predictor;
pub mod tiff;
mod tile;

Expand Down
9 changes: 6 additions & 3 deletions src/metadata/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
Loading
Loading