Skip to content

Commit

Permalink
⬆️ (deps): Update jpegxl-rs to 0.10.4 (#50)
Browse files Browse the repository at this point in the history
* update jpegxl-rs v0.10.4

* Fix linux CI

* Fix linux CI
  • Loading branch information
Isotr0py authored Jul 2, 2024
1 parent ec39b8a commit 5550f15
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 41 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,20 @@ jobs:
manylinux: auto
docker-options: |
-e RUST_BACKTRACE=1
-e DEP_JXL_LIB=${{ github.workspace }}/jpegxl-rs/jpegxl-src/libjxl/build/lib
-e DEP_BROTLI_LIB=${{ github.workspace }}/jpegxl-rs/jpegxl-src/libjxl/build/third_party/brotli
-e DEP_HWY_LIB=${{ github.workspace }}/jpegxl-rs/jpegxl-src/libjxl/build/third_party/highway
-e DEP_JXL_LIB=${{ github.workspace }}/libjxl/build/lib
-e DEP_BROTLI_LIB=${{ github.workspace }}/libjxl/build/third_party/brotli
-e DEP_HWY_LIB=${{ github.workspace }}/libjxl/build/third_party/highway
before-script-linux: |
cd jpegxl-rs/jpegxl-src/libjxl
git clone --recurse-submodules --depth 1 -b v0.10.3 \
https://github.com/libjxl/libjxl.git
cd libjxl
cmake -B build -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=OFF \
-DJPEGXL_ENABLE_TOOLS=OFF -DJPEGXL_ENABLE_DOXYGEN=OFF -DJPEGXL_ENABLE_MANPAGES=OFF \
-DJPEGXL_ENABLE_BENCHMARKS=OFF -DJPEGXL_ENABLE_EXAMPLES=OFF -DJPEGXL_ENABLE_JNI=OFF \
-DJPEGXL_ENABLE_SJPEG=OFF -DJPEGXL_ENABLE_OPENEXR=OFF
cmake --build build
cmake --install build
cd ../../../
cd ..
- name: Upload wheels
uses: actions/upload-artifact@v4
Expand Down
4 changes: 0 additions & 4 deletions .gitmodules

This file was deleted.

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 = { path="./jpegxl-rs/jpegxl-rs", default-features = false, features = ["threads"] }
jpegxl-rs = { version="0.10.4", features = ["threads"] }

[features]
# Enables parallel processing support by enabling the "rayon" feature of jpeg-decoder.
Expand Down
1 change: 0 additions & 1 deletion jpegxl-rs
Submodule jpegxl-rs deleted from 108024
15 changes: 1 addition & 14 deletions pillow_jxl/JpegXLImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,6 @@ def tell(self):
return self.__frame


def _quality_to_frame_distance(q):
# Translate quality to frame distance using the same mapping as
# libjxl's JxlEncoderDistanceFromQuality, which roughly maps to
# jpeg's quality values.
if q >= 100:
fd = 0
elif q >= 30:
fd = 0.1 + (100 - q) * 0.09
else:
fd = 53.0 / 3000.0 * q * q - 23.0 / 20.0 * q + 25.0
return fd


def _save(im, fp, filename, save_all=False):
if im.mode not in _VALID_JXL_MODES:
raise NotImplementedError("Only RGB, RGBA, L, LA are supported.")
Expand All @@ -101,7 +88,7 @@ def _save(im, fp, filename, save_all=False):

# default quality is 90
lossless = info.get("lossless", False)
quality = 0 if lossless else _quality_to_frame_distance(info.get("quality", 90))
quality = 0 if lossless else info.get("quality", 90)

decoding_speed = info.get("decoding_speed", 0)
effort = info.get("effort", 7)
Expand Down
36 changes: 32 additions & 4 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use pyo3::prelude::*;
use jpegxl_rs::decode::{Data, Metadata, Pixels};
use jpegxl_rs::decoder_builder;
use jpegxl_rs::parallel::threads_runner::ThreadsRunner;

// it works even if the item is not documented:

#[pyclass(module = "pillow_jxl")]
Expand Down Expand Up @@ -43,6 +44,30 @@ impl ImageInfo {
}
}

pub fn convert_pixels(pixels: Pixels) -> Vec<u8> {
let mut result = Vec::new();
match pixels {
Pixels::Uint8(pixels) => {
for pixel in pixels {
result.push(pixel);
}
}
Pixels::Uint16(pixels) => {
for pixel in pixels {
result.push((pixel >> 8) as u8);
result.push(pixel as u8);
}
}
Pixels::Float(pixels) => {
for pixel in pixels {
result.push((pixel * 255.0) as u8);
}
}
Pixels::Float16(_) => panic!("Float16 is not supported yet"),
}
result
}

#[pyclass(module = "pillow_jxl")]
pub struct Decoder {
parallel: bool,
Expand All @@ -57,7 +82,11 @@ impl Decoder {
}

#[pyo3(signature = (data))]
fn __call__(&self, _py: Python, data: &[u8]) -> (bool, ImageInfo, Cow<'_, [u8]>, Cow<'_, [u8]>) {
fn __call__(
&self,
_py: Python,
data: &[u8],
) -> (bool, ImageInfo, Cow<'_, [u8]>, Cow<'_, [u8]>) {
let parallel_runner: ThreadsRunner;
let decoder = match self.parallel {
true => {
Expand All @@ -70,11 +99,10 @@ impl Decoder {
}
false => decoder_builder().icc_profile(true).build().unwrap(),
};
let (info, img) = decoder.reconstruct_with::<u8>(&data).unwrap();
let (info, img) = decoder.reconstruct(&data).unwrap();
let (jpeg, img) = match img {
Data::Jpeg(x) => (true, x),
Data::Pixels(Pixels::Uint8(x)) => (false, x),
_ => panic!("Unsupported dtype for decoding"),
Data::Pixels(x) => (false, convert_pixels(x)),
};
let icc_profile: Vec<u8> = match &info.icc_profile {
Some(x) => x.to_vec(),
Expand Down
29 changes: 17 additions & 12 deletions src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use std::borrow::Cow;

use pyo3::prelude::*;

use jpegxl_rs::encode::{
ColorEncoding, EncoderFrame, EncoderResult, EncoderSpeed, Metadata as EncoderMetadata,
};
use jpegxl_rs::encode::{ColorEncoding, EncoderFrame, EncoderResult, EncoderSpeed, Metadata};
use jpegxl_rs::encoder_builder;
use jpegxl_rs::parallel::threads_runner::ThreadsRunner;

Expand Down Expand Up @@ -79,20 +77,24 @@ impl Encoder {
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()
encoder_builder.set_jpeg_quality(self.quality);
encoder_builder
.parallel_runner(&parallel_runner)
.build()
.unwrap()
}
false => encoder_builder().build().unwrap(),
false => {
encoder_builder.set_jpeg_quality(self.quality);
encoder_builder.build().unwrap()
}
};
encoder.uses_original_profile = self.use_original_profile;
encoder.has_alpha = self.has_alpha;
encoder.lossless = self.lossless;
encoder.quality = self.quality;
encoder.use_container = self.use_container;
encoder.decoding_speed = self.decoding_speed;
encoder.color_encoding = match self.num_channels {
Expand All @@ -116,13 +118,16 @@ impl Encoder {
true => encoder.encode_jpeg(&data).unwrap(),
false => {
let frame = EncoderFrame::new(data).num_channels(self.num_channels);
let metadata = EncoderMetadata::new()
.exif(exif.unwrap())
.jumb(jumb.unwrap())
.xmp(xmp.unwrap());
encoder
.encode_frame_with_metadata(&frame, width, height, metadata)
.unwrap()
.add_metadata(&Metadata::Exif(exif.unwrap()), true)
.unwrap();
encoder
.add_metadata(&Metadata::Xmp(xmp.unwrap()), true)
.unwrap();
encoder
.add_metadata(&Metadata::Jumb(jumb.unwrap()), true)
.unwrap();
encoder.encode_frame(&frame, width, height).unwrap()
}
};
Cow::Owned(buffer.data)
Expand Down

0 comments on commit 5550f15

Please sign in to comment.