Skip to content

added Endianness access from IFD to python #106

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 3 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
35 changes: 32 additions & 3 deletions python/src/enums.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
use async_tiff::tiff::tags::{
CompressionMethod, PhotometricInterpretation, PlanarConfiguration, Predictor, ResolutionUnit,
SampleFormat,
use async_tiff::{
reader::Endianness,
tiff::tags::{
CompressionMethod, PhotometricInterpretation, PlanarConfiguration, Predictor,
ResolutionUnit, SampleFormat,
},
};
use pyo3::prelude::*;
use pyo3::types::{PyString, PyTuple};
use pyo3::{intern, IntoPyObjectExt};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[pyclass(eq, eq_int, name = "Endianness")]
#[repr(u16)]
pub(crate) enum PyEndianness {
LittleEndian = 0x4949, // b"II"
BigEndian = 0x4D4D, // b"MM"
}

impl From<Endianness> for PyEndianness {
fn from(value: Endianness) -> Self {
match value {
Endianness::LittleEndian => Self::LittleEndian,
Endianness::BigEndian => Self::BigEndian,
}
}
}

impl From<PyEndianness> for Endianness {
fn from(value: PyEndianness) -> Self {
match value {
PyEndianness::LittleEndian => Self::LittleEndian,
PyEndianness::BigEndian => Self::BigEndian,
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct PyCompressionMethod(CompressionMethod);

Expand Down
9 changes: 7 additions & 2 deletions python/src/ifd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use async_tiff::ImageFileDirectory;
use pyo3::prelude::*;

use crate::enums::{
PyCompressionMethod, PyPhotometricInterpretation, PyPlanarConfiguration, PyPredictor,
PyResolutionUnit, PySampleFormat,
PyCompressionMethod, PyEndianness, PyPhotometricInterpretation, PyPlanarConfiguration,
PyPredictor, PyResolutionUnit, PySampleFormat,
};
use crate::geo::PyGeoKeyDirectory;
use crate::value::PyValue;
Expand All @@ -15,6 +15,11 @@ pub(crate) struct PyImageFileDirectory(ImageFileDirectory);

#[pymethods]
impl PyImageFileDirectory {
#[getter]
pub fn endianness(&self) -> PyEndianness {
self.0.endianness().into()
}

#[getter]
pub fn new_subfile_type(&self) -> Option<u32> {
self.0.new_subfile_type()
Expand Down
2 changes: 2 additions & 0 deletions python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod value;
use pyo3::prelude::*;

use crate::decoder::PyDecoderRegistry;
use crate::enums::PyEndianness;
use crate::geo::PyGeoKeyDirectory;
use crate::ifd::PyImageFileDirectory;
use crate::thread_pool::PyThreadPool;
Expand Down Expand Up @@ -50,6 +51,7 @@ fn _async_tiff(py: Python, m: &Bound<PyModule>) -> PyResult<()> {
check_debug_build(py)?;

m.add_wrapped(wrap_pyfunction!(___version))?;
m.add_class::<PyEndianness>()?;
m.add_class::<PyDecoderRegistry>()?;
m.add_class::<PyGeoKeyDirectory>()?;
m.add_class::<PyImageFileDirectory>()?;
Expand Down
5 changes: 5 additions & 0 deletions src/ifd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,11 @@ impl ImageFileDirectory {
})
}

/// The Endianness of the file
pub fn endianness(&self) -> Endianness {
self.endianness
}

/// A general indication of the kind of data contained in this subfile.
/// <https://web.archive.org/web/20240329145250/https://www.awaresystems.be/imaging/tiff/tifftags/newsubfiletype.html>
pub fn new_subfile_type(&self) -> Option<u32> {
Expand Down