|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::sync::Arc; |
| 3 | + |
1 | 4 | use async_tiff::decoder::{Decoder, DecoderRegistry};
|
2 | 5 | use async_tiff::error::AiocogeoError;
|
3 | 6 | use async_tiff::tiff::tags::PhotometricInterpretation;
|
4 | 7 | use bytes::Bytes;
|
5 | 8 | use pyo3::exceptions::PyTypeError;
|
6 | 9 | use pyo3::intern;
|
7 | 10 | use pyo3::prelude::*;
|
| 11 | +use pyo3::sync::GILOnceCell; |
8 | 12 | use pyo3::types::{PyDict, PyTuple};
|
9 | 13 | use pyo3_bytes::PyBytes;
|
10 | 14 |
|
11 | 15 | use crate::enums::PyCompressionMethod;
|
12 | 16 |
|
13 |
| -#[pyclass(name = "DecoderRegistry")] |
14 |
| -pub(crate) struct PyDecoderRegistry(DecoderRegistry); |
| 17 | +static DEFAULT_DECODER_REGISTRY: GILOnceCell<Arc<DecoderRegistry>> = GILOnceCell::new(); |
| 18 | + |
| 19 | +pub fn get_default_decoder_registry(py: Python<'_>) -> Arc<DecoderRegistry> { |
| 20 | + let registry = |
| 21 | + DEFAULT_DECODER_REGISTRY.get_or_init(py, || Arc::new(DecoderRegistry::default())); |
| 22 | + registry.clone() |
| 23 | +} |
| 24 | + |
| 25 | +#[pyclass(name = "DecoderRegistry", frozen)] |
| 26 | +#[derive(Debug, Default)] |
| 27 | +pub(crate) struct PyDecoderRegistry(Arc<DecoderRegistry>); |
15 | 28 |
|
16 | 29 | #[pymethods]
|
17 | 30 | impl PyDecoderRegistry {
|
18 | 31 | #[new]
|
19 |
| - fn new() -> Self { |
20 |
| - Self(DecoderRegistry::default()) |
| 32 | + #[pyo3(signature = (decoders = None))] |
| 33 | + pub(crate) fn new(decoders: Option<HashMap<PyCompressionMethod, PyDecoder>>) -> Self { |
| 34 | + let mut decoder_registry = DecoderRegistry::default(); |
| 35 | + if let Some(decoders) = decoders { |
| 36 | + for (compression, decoder) in decoders.into_iter() { |
| 37 | + decoder_registry |
| 38 | + .as_mut() |
| 39 | + .insert(compression.into(), Box::new(decoder)); |
| 40 | + } |
| 41 | + } |
| 42 | + Self(Arc::new(decoder_registry)) |
21 | 43 | }
|
22 |
| - |
23 |
| - fn add(&mut self, compression: PyCompressionMethod, decoder: PyDecoder) { |
24 |
| - self.0 |
25 |
| - .as_mut() |
26 |
| - .insert(compression.into(), Box::new(decoder)); |
| 44 | +} |
| 45 | +impl PyDecoderRegistry { |
| 46 | + pub(crate) fn inner(&self) -> &Arc<DecoderRegistry> { |
| 47 | + &self.0 |
27 | 48 | }
|
28 | 49 | }
|
29 | 50 |
|
30 | 51 | #[derive(Debug)]
|
31 |
| -struct PyDecoder(PyObject); |
| 52 | +pub(crate) struct PyDecoder(PyObject); |
32 | 53 |
|
33 | 54 | impl PyDecoder {
|
34 | 55 | fn call(&self, py: Python, buffer: Bytes) -> PyResult<PyBytes> {
|
|
0 commit comments