Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📷 (CMS): Add support for ICC profiles #40

Merged
merged 10 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Test Python

on: [push, pull_request]

env:
RUSTFLAGS: -C debuginfo=0 # Do not produce debug symbols to keep memory usage down
RUST_BACKTRACE: 1

jobs:
test-python:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
# we only use macos for fast testing
os: [macos-latest]
python-version: ['3.10', '3.11']

steps:
- uses: actions/checkout@v4
with:
submodules: recursive

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Create virtual environment
env:
BIN: ${{ matrix.os == 'macos-latest' && 'Scripts' || 'bin' }}
run: |
python -m venv venv

- name: Set up Rust
run: rustup show

- name: Cache Rust
uses: Swatinem/rust-cache@v2

- name: Install Plugin
run: |
export DEP_JXL_LIB=/usr/local/Cellar/jpeg-xl/0.10.2/lib
export DEP_BROTLI_LIB=/usr/local/Cellar/brotli/1.1.0/lib
export DEP_HWY_LIB=/usr/local/Cellar/highway/1.1.0/lib
source venv/bin/activate
pip install maturin
maturin develop --features dynamic

- name: Test with pytest
run: |
source venv/bin/activate
pip install -r requirements-dev.txt
pytest test/ --junitxml=junit/test-results-${{ matrix.python-version }}.xml

- name: Upload pytest test results
uses: actions/upload-artifact@v3
with:
name: pytest-results-${{ matrix.python-version }}
path: junit/test-results-${{ matrix.python-version }}.xml
# Use always() to always run this step to publish test results when there are test failures
if: ${{ always() }}


5 changes: 0 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
/target
Cargo.lock

# for test
*.ipynb
*.jxl
*.jpeg

# Byte-compiled / optimized / DLL files
__pycache__/
.pytest_cache/
Expand Down
3 changes: 2 additions & 1 deletion pillow_jxl/JpegXLImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ def _open(self):
self.fc = self.fp.read()
self._decoder = Decoder()

self.jpeg, self._jxlinfo, self._data = self._decoder(self.fc)
self.jpeg, self._jxlinfo, self._data, icc_profile = self._decoder(self.fc)
# FIXME (Isotr0py): Maybe slow down jpeg reconstruction
if self.jpeg:
with Image.open(BytesIO(self._data)) as im:
self._data = im.tobytes()
self._size = (self._jxlinfo.width, self._jxlinfo.height)
self.rawmode = self._jxlinfo.mode
self.info["icc_profile"] = icc_profile
# NOTE (Isotr0py): PIL 10.1.0 changed the mode to property, use _mode instead
if parse(PIL.__version__) >= parse("10.1.0"):
self._mode = self.rawmode
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest
21 changes: 15 additions & 6 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use std::borrow::Cow;
use pyo3::prelude::*;

use jpegxl_rs::decode::{Data, Metadata, Pixels};
use jpegxl_rs::parallel::threads_runner::ThreadsRunner;
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")]
struct ImageInfo {
#[pyo3(get, set)]
Expand Down Expand Up @@ -58,28 +57,38 @@ impl Decoder {
}

#[pyo3(signature = (data))]
fn __call__(&self, _py: Python, data: &[u8]) -> (bool, ImageInfo, 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 => {
parallel_runner = ThreadsRunner::default();
decoder_builder()
.icc_profile(true)
.parallel_runner(&parallel_runner)
.build()
.unwrap()
}
false => decoder_builder().build().unwrap(),
false => decoder_builder().icc_profile(true).build().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"),
};
(jpeg, ImageInfo::from(info), Cow::Owned(img))
let icc_profile: Vec<u8> = match &info.icc_profile {
Some(x) => x.to_vec(),
None => Vec::new(),
};
(
jpeg,
ImageInfo::from(info),
Cow::Owned(img),
Cow::Owned(icc_profile),
)
}

fn __repr__(&self) -> PyResult<String> {
Ok(format!("Decoder(parallel={})", self.parallel))
}
}
}
18 changes: 12 additions & 6 deletions src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use std::borrow::Cow;

use pyo3::prelude::*;

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

use jpegxl_rs::parallel::threads_runner::ThreadsRunner;

#[pyclass(module = "pillow_jxl")]
pub struct Encoder {
Expand Down Expand Up @@ -75,7 +76,7 @@ impl Encoder {
jpeg_encode: bool,
exif: Option<&[u8]>,
jumb: Option<&[u8]>,
xmp: Option<&[u8]>
xmp: Option<&[u8]>,
) -> Cow<'_, [u8]> {
let parallel_runner: ThreadsRunner;
let mut encoder = match self.parallel {
Expand Down Expand Up @@ -115,8 +116,13 @@ 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()
let metadata = EncoderMetadata::new()
.exif(exif.unwrap())
.jumb(jumb.unwrap())
.xmp(xmp.unwrap());
encoder
.encode_frame_with_metadata(&frame, width, height, metadata)
.unwrap()
}
};
Cow::Owned(buffer.data)
Expand Down
Binary file added test/images/icc_profile/62AHB.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/images/icc_profile/62AHB.jxl
Binary file not shown.
14 changes: 14 additions & 0 deletions test/test_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from PIL import Image

import pillow_jxl


def test_icc_profile():
# Load a JPEG image
img_ori = Image.open("test/images/icc_profile/62AHB.jpg")
img_jxl = Image.open("test/images/icc_profile/62AHB.jxl")

# Compare the two images
assert img_ori.size == img_jxl.size
assert img_ori.mode == img_jxl.mode
assert img_ori.info["icc_profile"] == img_jxl.info["icc_profile"]
Loading