Skip to content

Commit

Permalink
deprecate jpegxl-rs thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Isotr0py committed Sep 30, 2024
1 parent 5c20892 commit 41fb1c2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ crate-type = ["cdylib"]

[dependencies]
pyo3 = { version="0.22.0", features = ["extension-module"] }
jpegxl-rs = { version="0.11.0", features = ["threads"] }
jpegxl-rs = { version="0.11.0" }

[features]
# Enables parallel processing support by enabling the "rayon" feature of jpeg-decoder.
Expand Down
29 changes: 10 additions & 19 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,13 @@ pub fn convert_pixels(pixels: Pixels) -> Vec<u8> {
}

#[pyclass(module = "pillow_jxl")]
pub struct Decoder {
parallel: bool,
}
pub struct Decoder;

#[pymethods]
impl Decoder {
#[new]
#[pyo3(signature = (parallel=true))]
fn new(parallel: bool) -> Self {
Self { parallel: parallel }
fn new() -> Self {
Self
}

#[pyo3(signature = (data))]
Expand All @@ -87,18 +84,12 @@ impl Decoder {
_py: Python,
data: &[u8],
) -> (bool, ImageInfo, Cow<'_, [u8]>, Cow<'_, [u8]>) {
let parallel_runner: ThreadsRunner;
let decoder = match self.parallel {
true => {
parallel_runner = ThreadsRunner::default();
decoder_builder()
.icc_profile(true)
.parallel_runner(&parallel_runner)
.build()
.unwrap()
}
false => decoder_builder().icc_profile(true).build().unwrap(),
};
let parallel_runner = ThreadsRunner::default();
let decoder = decoder_builder()
.icc_profile(true)
.parallel_runner(&parallel_runner)
.build()
.unwrap();
let (info, img) = decoder.reconstruct(&data).unwrap();
let (jpeg, img) = match img {
Data::Jpeg(x) => (true, x),
Expand All @@ -117,6 +108,6 @@ impl Decoder {
}

fn __repr__(&self) -> PyResult<String> {
Ok(format!("Decoder(parallel={})", self.parallel))
Ok(format!("Decoder"))
}
}
39 changes: 13 additions & 26 deletions src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use jpegxl_rs::parallel::threads_runner::ThreadsRunner;

#[pyclass(module = "pillow_jxl")]
pub struct Encoder {
parallel: bool,
num_channels: u32,
has_alpha: bool,
lossless: bool,
Expand All @@ -22,10 +21,9 @@ pub struct Encoder {
#[pymethods]
impl Encoder {
#[new]
#[pyo3(signature = (mode, parallel=true, lossless=false, quality=1.0, decoding_speed=0, effort=7, use_container=true, use_original_profile=false))]
#[pyo3(signature = (mode, lossless=false, quality=1.0, decoding_speed=0, effort=7, use_container=true, use_original_profile=false))]
fn new(
mode: &str,
parallel: bool,
lossless: bool,
quality: f32,
decoding_speed: i64,
Expand All @@ -52,7 +50,6 @@ impl Encoder {
};

Self {
parallel,
num_channels,
has_alpha,
lossless,
Expand All @@ -76,27 +73,17 @@ impl Encoder {
jumb: Option<&[u8]>,
xmp: Option<&[u8]>,
) -> Cow<'_, [u8]> {
let parallel_runner: ThreadsRunner;
let mut encoder_builder = encoder_builder();
let mut encoder = match self.parallel {
true => {
parallel_runner = ThreadsRunner::default();
encoder_builder.set_jpeg_quality(self.quality);
encoder_builder
.parallel_runner(&parallel_runner)
.build()
.unwrap()
}
false => {
encoder_builder.set_jpeg_quality(self.quality);
encoder_builder.build().unwrap()
}
};
let parallel_runner = ThreadsRunner::default();
let mut encoder = encoder_builder()
.parallel_runner(&parallel_runner)
.jpeg_quality(self.quality)
.has_alpha(self.has_alpha)
.lossless(self.lossless)
.use_container(self.use_container)
.decoding_speed(self.decoding_speed)
.build()
.unwrap();
encoder.uses_original_profile = self.use_original_profile;
encoder.has_alpha = self.has_alpha;
encoder.lossless = self.lossless;
encoder.use_container = self.use_container;
encoder.decoding_speed = self.decoding_speed;
encoder.color_encoding = match self.num_channels {
1 | 2 => ColorEncoding::SrgbLuma,
3 | 4 => ColorEncoding::Srgb,
Expand Down Expand Up @@ -141,8 +128,8 @@ impl Encoder {

fn __repr__(&self) -> PyResult<String> {
Ok(format!(
"Encoder(parallel={}, has_alpha={}, lossless={}, quality={}, decoding_speed={}, effort={})",
self.parallel, self.has_alpha, self.lossless, self.quality, self.decoding_speed, self.effort
"Encoder(has_alpha={}, lossless={}, quality={}, decoding_speed={}, effort={})",
self.has_alpha, self.lossless, self.quality, self.decoding_speed, self.effort
))
}
}

0 comments on commit 41fb1c2

Please sign in to comment.