Skip to content

Commit

Permalink
feat: Add Session to expose decoding steps
Browse files Browse the repository at this point in the history
  • Loading branch information
inflation committed Oct 11, 2024
1 parent 421606b commit 03de9a8
Show file tree
Hide file tree
Showing 10 changed files with 639 additions and 266 deletions.
6 changes: 6 additions & 0 deletions .markdownlint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"default": true,
"MD024": {
"siblings_only": true
}
}
98 changes: 49 additions & 49 deletions jpegxl-rs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
# Changelog

## [Unreleased]

## [0.11.1+libjxl-0.11.0](https://github.com/inflation/jpegxl-rs/compare/jpegxl-rs-v0.11.0+libjxl-0.11.0...jpegxl-rs-v0.11.1+libjxl-0.11.0) - 2024-10-01

### 🐛 Bug Fixes

- Fix doc gen

### 📚 Documentation

- Remove docsrs-specific reference to deleted "threads" feature

## [0.11.0+libjxl-0.11.0](https://github.com/inflation/jpegxl-rs/compare/jpegxl-rs-v0.10.4+libjxl-0.10.3...jpegxl-rs-v0.11.0+libjxl-0.11.0) - 2024-09-27

### ⛰️ Features

- Update JPEG quality setting in encoder ([#74](https://github.com/inflation/jpegxl-rs/pull/74))

### 🐛 Bug Fixes

- Update release configuration for jpegxl-rs and jpegxl-sys packages
- Change ffi function types to use `c-unwind` ABI
- Change `JxlBoxType` to use system char type

### 🚜 Refactor

- Move `libjxl` functions into modules
- Remove threads feature and update dependencies.
- Don't use `-sys` in `-rs` with default features enabled

### 📚 Documentation

- Convert `libjxl` doc to rustdoc format with help from @copilot
- Remove unnecessary feature attribute in thread pool implementations
- Update how docs are generated ([#73](https://github.com/inflation/jpegxl-rs/pull/73))

### 📦 Dependencies

- Bump thiserror from 1.0.61 to 1.0.64
- Bump pretty_assertions from 1.4.0 to 1.4.1
- Bump derive_builder from 0.20.0 to 0.20.1
- Bump testresult from 0.4.0 to 0.4.1
- Bump image from 0.25.1 to 0.25.2

### ⚙️ Miscellaneous Tasks

- Update release-plz workflow and config
# Changelog

## [Unreleased]

## [0.11.1+libjxl-0.11.0](https://github.com/inflation/jpegxl-rs/compare/jpegxl-rs-v0.11.0+libjxl-0.11.0...jpegxl-rs-v0.11.1+libjxl-0.11.0) - 2024-10-01

### 🐛 Bug Fixes

- Fix doc gen

### 📚 Documentation

- Remove docsrs-specific reference to deleted "threads" feature

## [0.11.0+libjxl-0.11.0](https://github.com/inflation/jpegxl-rs/compare/jpegxl-rs-v0.10.4+libjxl-0.10.3...jpegxl-rs-v0.11.0+libjxl-0.11.0) - 2024-09-27

### ⛰️ Features

- Update JPEG quality setting in encoder ([#74](https://github.com/inflation/jpegxl-rs/pull/74))

### 🐛 Bug Fixes

- Update release configuration for jpegxl-rs and jpegxl-sys packages
- Change ffi function types to use `c-unwind` ABI
- Change `JxlBoxType` to use system char type

### 🚜 Refactor

- Move `libjxl` functions into modules
- Remove threads feature and update dependencies.
- Don't use `-sys` in `-rs` with default features enabled

### 📚 Documentation

- Convert `libjxl` doc to rustdoc format with help from @copilot
- Remove unnecessary feature attribute in thread pool implementations
- Update how docs are generated ([#73](https://github.com/inflation/jpegxl-rs/pull/73))

### 📦 Dependencies

- Bump thiserror from 1.0.61 to 1.0.64
- Bump pretty_assertions from 1.4.0 to 1.4.1
- Bump derive_builder from 0.20.0 to 0.20.1
- Bump testresult from 0.4.0 to 0.4.1
- Bump image from 0.25.1 to 0.25.2

### ⚙️ Miscellaneous Tasks

- Update release-plz workflow and config
142 changes: 71 additions & 71 deletions jpegxl-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,71 +1,71 @@
[package]
authors = ["Inflation <[email protected]>"]
categories = ["api-bindings", "encoding", "multimedia::images"]
description = "Safe Rust wrapper for JPEG XL reference implementation"
edition = "2021"
keywords = ["jpeg-xl", "jxl"]
license = "GPL-3.0-or-later"
name = "jpegxl-rs"
readme = "README.md"
repository = "https://github.com/inflation/jpegxl-rs"
version = "0.11.1+libjxl-0.11.0"
rust-version.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lints.rust]
missing_docs = "warn"
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(coverage_nightly)'] }

[lints.clippy]
pedantic = "warn"

[features]
default = ["image"]
image = ["dep:image"]
vendored = ["jpegxl-sys/vendored"]
docs = ["jpegxl-sys/docs"]
bench = []

[dependencies]
derive_builder = "0.20.1"
image = { version = "0.25.2", optional = true, default-features = false }
thiserror = "1.0.64"
half = "2.4.1"
byteorder = "1.5.0"

[dependencies.jpegxl-sys]
version = "0.11.1"
path = "../jpegxl-sys"
default-features = false

[dev-dependencies]
image = { version = "0.25.2", default-features = false, features = [
"jpeg",
"png",
] }
lcms2 = "6.1.0"
pretty_assertions = "1.4.1"
testresult = "0.4.1"

[target.'cfg(not(target_family = "wasm"))'.dev-dependencies]
criterion = "0.5.1"


[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "docsrs"]
features = ["docs"]

[lib]
bench = false
path = "src/lib.rs"

[[bench]]
harness = false
name = "decode"
required-features = ["bench"]

[[bench]]
harness = false
name = "encode"
required-features = ["bench"]
[package]
authors = ["Inflation <[email protected]>"]
categories = ["api-bindings", "encoding", "multimedia::images"]
description = "Safe Rust wrapper for JPEG XL reference implementation"
edition = "2021"
keywords = ["jpeg-xl", "jxl"]
license = "GPL-3.0-or-later"
name = "jpegxl-rs"
readme = "README.md"
repository = "https://github.com/inflation/jpegxl-rs"
version = "0.11.1+libjxl-0.11.0"
rust-version.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lints.rust]
missing_docs = "warn"
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(coverage_nightly)'] }

[lints.clippy]
pedantic = "warn"

[features]
default = ["image"]
image = ["dep:image"]
vendored = ["jpegxl-sys/vendored"]
docs = ["jpegxl-sys/docs"]
bench = []

[dependencies]
derive_builder = "0.20.1"
image = { version = "0.25.2", optional = true, default-features = false }
thiserror = "1.0.64"
half = "2.4.1"
byteorder = "1.5.0"

[dependencies.jpegxl-sys]
version = "0.11.1"
path = "../jpegxl-sys"
default-features = false

[dev-dependencies]
image = { version = "0.25.2", default-features = false, features = [
"jpeg",
"png",
] }
lcms2 = "6.1.0"
pretty_assertions = "1.4.1"
testresult = "0.4.1"

[target.'cfg(not(target_family = "wasm"))'.dev-dependencies]
criterion = "0.5.1"


[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "docsrs"]
features = ["docs"]

[lib]
bench = false
path = "src/lib.rs"

[[bench]]
harness = false
name = "decode"
required-features = ["bench"]

[[bench]]
harness = false
name = "encode"
required-features = ["bench"]
22 changes: 22 additions & 0 deletions jpegxl-rs/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ use crate::{
utils::check_valid_signature,
};

mod event;
pub use event::*;
mod result;
pub use result::*;
mod session;
pub use session::*;

/// Basic information
pub type BasicInfo = JxlBasicInfo;
Expand Down Expand Up @@ -414,6 +418,24 @@ impl<'pr, 'mm> JxlDecoder<'pr, 'mm> {
Ok(())
}

/// Start a new decoding session.
///
/// Later event will overwrite the previous one if they are the same.
///
/// # Arguments
///
/// * `events` - The events to subscribe to during the session.
///
/// # Errors
///
/// Returns a [`DecodeError`] if the decoding session encounters an error.
pub fn session<I>(&mut self, events: I) -> Result<Session<'_, 'pr, 'mm>, DecodeError>
where
I: IntoIterator<Item = Event>,
{
Session::new(self, events)
}

Check warning on line 437 in jpegxl-rs/src/decode.rs

View check run for this annotation

Codecov / codecov/patch

jpegxl-rs/src/decode.rs#L432-L437

Added lines #L432 - L437 were not covered by tests

/// Decode a JPEG XL image
///
/// # Errors
Expand Down
55 changes: 55 additions & 0 deletions jpegxl-rs/src/decode/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::ffi::c_int;

use jpegxl_sys::common::types::JxlPixelFormat;
/// Target of the color profile.
pub use jpegxl_sys::decode::JxlColorProfileTarget as ColorProfileTarget;

use super::{ColorEncodingConfig, Config};

/// Events that can be subscribed to.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Event {
/// Basic information.
BasicInfo,
/// Color encoding.
ColorEncoding(ColorEncodingConfig),
/// Preview image.
PreviewImage {
/// Pixel format.
pixel_format: JxlPixelFormat,
},
/// JPEG reconstruction.
JpegReconstruction {
/// Initial buffer size. Increase it to reduce the number of reallocations.
init_buffer_size: usize,
},
}

impl From<Event> for c_int {
fn from(value: Event) -> Self {
match value {
Event::BasicInfo => 0x40,
Event::ColorEncoding(_) => 0x100,
Event::PreviewImage { .. } => 0x200,
Event::JpegReconstruction { .. } => 0x2000,

Check warning on line 34 in jpegxl-rs/src/decode/event.rs

View check run for this annotation

Codecov / codecov/patch

jpegxl-rs/src/decode/event.rs#L32-L34

Added lines #L32 - L34 were not covered by tests
}
}
}

pub(crate) fn parse_events<I>(iter: I) -> (c_int, Config)
where
I: IntoIterator<Item = Event>,
{
iter.into_iter()
.fold((0, Config::default()), |(flag, mut config), x| {
let flag = flag | c_int::from(x);
let config = match x {
Event::ColorEncoding(val) => {
config.color_profile = Some(val);
config

Check warning on line 49 in jpegxl-rs/src/decode/event.rs

View check run for this annotation

Codecov / codecov/patch

jpegxl-rs/src/decode/event.rs#L47-L49

Added lines #L47 - L49 were not covered by tests
}
_ => config,
};
(flag, config)
})
}
Loading

0 comments on commit 03de9a8

Please sign in to comment.