Skip to content

Commit

Permalink
support python 3.13 (#184)
Browse files Browse the repository at this point in the history
* support python 3.13

* disable publish to test

* bump to 0.23.0

* arc mutex workaround

* Update python_release.yml

* enable again publish
  • Loading branch information
edgarriba authored Dec 17, 2024
1 parent b423d92 commit 5add818
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
15 changes: 11 additions & 4 deletions .github/workflows/python_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
target: [x86_64, aarch64]

steps:
Expand All @@ -23,7 +23,7 @@ jobs:
target: ${{ matrix.target }}
manylinux: auto
command: build
args: --release --out dist -i python${{ matrix.python-version }} -m kornia-py/Cargo.toml --sdist
args: --release --out dist -i python${{ matrix.python-version }} -m kornia-py/Cargo.toml --sdist
- name: Upload wheels
uses: actions/upload-artifact@v3
with:
Expand Down Expand Up @@ -71,6 +71,12 @@ jobs:
- runs-on: macos-14
python-version: "3.12"
target: aarch64
- runs-on: macos-14
python-version: "3.13"
target: x86_64
- runs-on: macos-14
python-version: "3.13"
target: aarch64
steps:
- uses: ilammy/setup-nasm@v1
- uses: actions/checkout@v4
Expand Down Expand Up @@ -99,15 +105,16 @@ jobs:
runs-on: windows-latest
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
target: [x64, x86]
steps:
- uses: ilammy/setup-nasm@v1
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
architecture: ${{ matrix.target }}
# x86 python needs to be available for the win32 wheel
architecture: ${{ ( matrix.os == 'windows-latest' && matrix.target == 'i686' ) && 'x86' || null }}
- uses: actions-rs/toolchain@v1
with:
profile: minimal
Expand Down
1 change: 1 addition & 0 deletions crates/kornia-imgproc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ thiserror = { workspace = true }

[dev-dependencies]
criterion = { workspace = true }
image = "0.25"
kornia-io = { workspace = true }
ndarray = { version = "0.15", features = ["rayon"] }

Expand Down
24 changes: 16 additions & 8 deletions crates/kornia-io/src/jpeg.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::sync::{Arc, Mutex};
use turbojpeg;

use kornia_image::{Image, ImageError, ImageSize};
Expand All @@ -21,13 +22,13 @@ pub enum JpegError {
/// A JPEG decoder using the turbojpeg library.
pub struct ImageDecoder {
/// The turbojpeg decompressor.
pub decompressor: turbojpeg::Decompressor,
pub decompressor: Arc<Mutex<turbojpeg::Decompressor>>,
}

/// A JPEG encoder using the turbojpeg library.
pub struct ImageEncoder {
/// The turbojpeg compressor.
pub compressor: turbojpeg::Compressor,
pub compressor: Arc<Mutex<turbojpeg::Compressor>>,
}

impl Default for ImageDecoder {
Expand Down Expand Up @@ -61,7 +62,9 @@ impl ImageEncoder {
/// Panics if the compressor cannot be created.
pub fn new() -> Result<Self, JpegError> {
let compressor = turbojpeg::Compressor::new()?;
Ok(Self { compressor })
Ok(Self {
compressor: Arc::new(Mutex::new(compressor)),
})
}

/// Encodes the given data into a JPEG image.
Expand All @@ -87,7 +90,7 @@ impl ImageEncoder {
};

// encode the image
Ok(self.compressor.compress_to_vec(buf)?)
Ok(self.compressor.lock().unwrap().compress_to_vec(buf)?)
}

/// Sets the quality of the encoder.
Expand All @@ -96,7 +99,7 @@ impl ImageEncoder {
///
/// * `quality` - The quality to set.
pub fn set_quality(&mut self, quality: i32) -> Result<(), JpegError> {
Ok(self.compressor.set_quality(quality)?)
Ok(self.compressor.lock().unwrap().set_quality(quality)?)
}
}

Expand All @@ -109,7 +112,9 @@ impl ImageDecoder {
/// A new `ImageDecoder` instance.
pub fn new() -> Result<Self, JpegError> {
let decompressor = turbojpeg::Decompressor::new()?;
Ok(ImageDecoder { decompressor })
Ok(ImageDecoder {
decompressor: Arc::new(Mutex::new(decompressor)),
})
}

/// Reads the header of a JPEG image.
Expand All @@ -127,7 +132,7 @@ impl ImageDecoder {
/// Panics if the header cannot be read.
pub fn read_header(&mut self, jpeg_data: &[u8]) -> Result<ImageSize, JpegError> {
// read the JPEG header with image size
let header = self.decompressor.read_header(jpeg_data)?;
let header = self.decompressor.lock().unwrap().read_header(jpeg_data)?;

Ok(ImageSize {
width: header.width,
Expand Down Expand Up @@ -161,7 +166,10 @@ impl ImageDecoder {
};

// decompress the JPEG data
self.decompressor.decompress(jpeg_data, buf)?;
self.decompressor
.lock()
.unwrap()
.decompress(jpeg_data, buf)?;

Ok(Image::new(image_size, pixels)?)
}
Expand Down
4 changes: 2 additions & 2 deletions kornia-py/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ kornia-imgproc = { path = "../crates/kornia-imgproc" }
kornia-io = { path = "../crates/kornia-io", features = ["jpegturbo"] }

# external
pyo3 = { version = "0.21.2", features = ["extension-module"] }
numpy = { version = "0.21.0" }
pyo3 = { version = "0.23.0", features = ["extension-module"] }
numpy = { version = "0.23.0" }
2 changes: 1 addition & 1 deletion kornia-py/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub trait ToPyImage {
impl<const C: usize> ToPyImage for Image<u8, C> {
fn to_pyimage(self) -> PyImage {
Python::with_gil(|py| unsafe {
let array = PyArray::<u8, _>::new_bound(py, [self.height(), self.width(), C], false);
let array = PyArray::<u8, _>::new(py, [self.height(), self.width(), C], false);
// TODO: verify that the data is contiguous, otherwise iterate over the image and copy
std::ptr::copy_nonoverlapping(self.as_ptr(), array.data(), self.numel());
array.unbind()
Expand Down

0 comments on commit 5add818

Please sign in to comment.