Skip to content
Merged
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
211 changes: 136 additions & 75 deletions python/Cargo.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions python/python/async_tiff/_ifd.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ from .enums import (
)
from ._geo import GeoKeyDirectory

Value = int | float | str | tuple[int, int] | list[Value]

class ImageFileDirectory:
@property
def new_subfile_type(self) -> int | None: ...
Expand Down Expand Up @@ -96,3 +98,5 @@ class ImageFileDirectory:
def model_pixel_scale(self) -> list[float] | None: ...
@property
def model_tiepoint(self) -> list[float] | None: ...
@property
def other_tags(self) -> dict[int, Value]: ...
13 changes: 13 additions & 0 deletions python/src/ifd.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use async_tiff::ImageFileDirectory;
use pyo3::prelude::*;

Expand All @@ -6,6 +8,7 @@ use crate::enums::{
PyResolutionUnit, PySampleFormat,
};
use crate::geo::PyGeoKeyDirectory;
use crate::value::PyValue;

#[pyclass(name = "ImageFileDirectory")]
pub(crate) struct PyImageFileDirectory(ImageFileDirectory);
Expand Down Expand Up @@ -214,6 +217,16 @@ impl PyImageFileDirectory {
pub fn model_tiepoint(&self) -> Option<&[f64]> {
self.0.model_tiepoint()
}

#[getter]
pub fn other_tags(&self) -> HashMap<u16, PyValue> {
let iter = self
.0
.other_tags()
.iter()
.map(|(key, val)| (key.to_u16(), val.clone().into()));
HashMap::from_iter(iter)
}
}

impl From<ImageFileDirectory> for PyImageFileDirectory {
Expand Down
1 change: 1 addition & 0 deletions python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod ifd;
mod thread_pool;
mod tiff;
mod tile;
mod value;

use pyo3::prelude::*;

Expand Down
49 changes: 49 additions & 0 deletions python/src/value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use async_tiff::tiff::Value;
use pyo3::exceptions::PyRuntimeError;
use pyo3::prelude::*;
use pyo3::IntoPyObjectExt;

pub struct PyValue(Value);

impl<'py> IntoPyObject<'py> for PyValue {
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
type Error = PyErr;

fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
match self.0 {
Value::Byte(val) => val.into_bound_py_any(py),
Value::Short(val) => val.into_bound_py_any(py),
Value::SignedByte(val) => val.into_bound_py_any(py),
Value::SignedShort(val) => val.into_bound_py_any(py),
Value::Signed(val) => val.into_bound_py_any(py),
Value::SignedBig(val) => val.into_bound_py_any(py),
Value::Unsigned(val) => val.into_bound_py_any(py),
Value::UnsignedBig(val) => val.into_bound_py_any(py),
Value::Float(val) => val.into_bound_py_any(py),
Value::Double(val) => val.into_bound_py_any(py),
Value::List(val) => val
.into_iter()
.map(|v| PyValue(v).into_bound_py_any(py))
.collect::<PyResult<Vec<_>>>()?
.into_bound_py_any(py),
Value::Rational(num, denom) => (num, denom).into_bound_py_any(py),
Value::RationalBig(num, denom) => (num, denom).into_bound_py_any(py),
Value::SRational(num, denom) => (num, denom).into_bound_py_any(py),
Value::SRationalBig(num, denom) => (num, denom).into_bound_py_any(py),
Value::Ascii(val) => val.into_bound_py_any(py),
Value::Ifd(_val) => Err(PyRuntimeError::new_err("Unsupported value type 'Ifd'")),
Value::IfdBig(_val) => Err(PyRuntimeError::new_err("Unsupported value type 'IfdBig'")),
v => Err(PyRuntimeError::new_err(format!(
"Unknown value type: {:?}",
v
))),
}
}
}

impl From<Value> for PyValue {
fn from(value: Value) -> Self {
Self(value)
}
}
5 changes: 5 additions & 0 deletions src/ifd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,11 @@ impl ImageFileDirectory {
self.model_tiepoint.as_deref()
}

/// Tags for which the tiff crate doesn't have a hard-coded enum variant.
pub fn other_tags(&self) -> &HashMap<Tag, Value> {
&self.other_tags
}

/// Construct colormap from colormap tag
pub fn colormap(&self) -> Option<HashMap<usize, [u8; 3]>> {
fn cmap_transform(val: u16) -> u8 {
Expand Down
2 changes: 2 additions & 0 deletions src/tiff/ifd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Function for reading TIFF tags

#![allow(missing_docs)]

use std::vec;

use self::Value::{
Expand Down
2 changes: 1 addition & 1 deletion src/tiff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ mod ifd;
pub mod tags;

pub(crate) use error::{TiffError, TiffFormatError, TiffResult, TiffUnsupportedError};
pub(crate) use ifd::Value;
pub use ifd::Value;