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 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
17 changes: 17 additions & 0 deletions python/python/async_tiff/enums.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import sys
from enum import IntEnum

if sys.version_info >= (3, 11):
from enum import StrEnum
else:
from enum import Enum
class StrEnum(str, Enum):
def __str__(self):
return str(self.value)

class Endianness(StrEnum):
Copy link
Member

Choose a reason for hiding this comment

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

We need to add Python formatting to CI; as this would fail black/ruff fmt.

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 see you opened an issue for that. I could pull it into this PR though?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I don't know how to do that...

Copy link
Member

Choose a reason for hiding this comment

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

"""
endianness of the underlying tiff file
"""

LittleEndian = "LittleEndian"
BigEndian = "BigEndian"


class CompressionMethod(IntEnum):
"""
Expand Down
41 changes: 38 additions & 3 deletions python/src/enums.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,46 @@
use async_tiff::tiff::tags::{
CompressionMethod, PhotometricInterpretation, PlanarConfiguration, Predictor, ResolutionUnit,
SampleFormat,
use async_tiff::{
reader::Endianness,
tiff::tags::{
CompressionMethod, PhotometricInterpretation, PlanarConfiguration, Predictor,
ResolutionUnit, SampleFormat,
},
Copy link
Member

Choose a reason for hiding this comment

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

Can we keep this as module imports instead of crate imports.

};
use pyo3::prelude::*;
use pyo3::types::{PyString, PyTuple};
use pyo3::{intern, IntoPyObjectExt};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct PyEndianness(Endianness);

impl From<Endianness> for PyEndianness {
fn from(value: Endianness) -> Self {
Self(value)
}
}

impl From<PyEndianness> for Endianness {
fn from(value: PyEndianness) -> Self {
value.0
}
}

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

fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
// import the python module
let enums_mod = py.import(intern!(py, "async_tiff.enums"))?;
// get our python enum
let enum_cls = enums_mod.getattr(intern!(py, "Endianness"))?;
match self.0 {
Endianness::LittleEndian => enum_cls.getattr(intern!(py, "LittleEndian")),
Endianness::BigEndian => enum_cls.getattr(intern!(py, "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()
}
Comment on lines +18 to +21
Copy link
Member

Choose a reason for hiding this comment

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

Also add this to the pyi file with the ImageFileDirectory type hint.

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 think you mean Endianness type hint?

Copy link
Member

Choose a reason for hiding this comment

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

No I mean you need to describe this here:

class ImageFileDirectory:

Copy link
Contributor Author

@feefladder feefladder Jun 17, 2025

Choose a reason for hiding this comment

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

I did that (lines 15-16, also comment). What I meant is a -> Endianness type hint on the declaration


#[getter]
pub fn new_subfile_type(&self) -> Option<u32> {
self.0.new_subfile_type()
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
2 changes: 1 addition & 1 deletion src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl AsyncFileReader for ReqwestReader {
}

/// Endianness
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Endianness {
/// Little Endian
LittleEndian,
Expand Down