Skip to content

Commit 8d642b3

Browse files
authored
Expose compressed tile data to Python (#47)
1 parent e46831b commit 8d642b3

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

python/python/async_tiff/_tile.pyi

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
from collections.abc import Buffer
22

3+
from .enums import CompressionMethod
34
from ._decoder import DecoderRegistry
45
from ._thread_pool import ThreadPool
56

67
class Tile:
8+
@property
9+
def x(self) -> int: ...
10+
@property
11+
def y(self) -> int: ...
12+
@property
13+
def compressed_bytes(self) -> Buffer: ...
14+
@property
15+
def compression_method(self) -> CompressionMethod: ...
716
async def decode(
817
self,
918
*,

python/src/tile.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use async_tiff::Tile;
2+
use pyo3::exceptions::PyValueError;
23
use pyo3::prelude::*;
34
use pyo3_async_runtimes::tokio::future_into_py;
45
use pyo3_bytes::PyBytes;
56
use tokio_rayon::AsyncThreadPool;
67

78
use crate::decoder::get_default_decoder_registry;
9+
use crate::enums::PyCompressionMethod;
810
use crate::thread_pool::{get_default_pool, PyThreadPool};
911
use crate::PyDecoderRegistry;
1012

@@ -13,6 +15,39 @@ pub(crate) struct PyTile(Option<Tile>);
1315

1416
#[pymethods]
1517
impl PyTile {
18+
#[getter]
19+
fn x(&self) -> PyResult<usize> {
20+
self.0
21+
.as_ref()
22+
.ok_or(PyValueError::new_err("Tile has been consumed"))
23+
.map(|t| t.x())
24+
}
25+
26+
#[getter]
27+
fn y(&self) -> PyResult<usize> {
28+
self.0
29+
.as_ref()
30+
.ok_or(PyValueError::new_err("Tile has been consumed"))
31+
.map(|t| t.y())
32+
}
33+
34+
#[getter]
35+
fn compressed_bytes(&self) -> PyResult<PyBytes> {
36+
let tile = self
37+
.0
38+
.as_ref()
39+
.ok_or(PyValueError::new_err("Tile has been consumed"))?;
40+
Ok(tile.compressed_bytes().clone().into())
41+
}
42+
43+
#[getter]
44+
fn compression_method(&self) -> PyResult<PyCompressionMethod> {
45+
self.0
46+
.as_ref()
47+
.ok_or(PyValueError::new_err("Tile has been consumed"))
48+
.map(|t| t.compression_method().into())
49+
}
50+
1651
#[pyo3(signature = (*, decoder_registry=None, pool=None))]
1752
fn decode_async(
1853
&mut self,

0 commit comments

Comments
 (0)