Skip to content

Commit

Permalink
refactor: Move libjxl functions into modules
Browse files Browse the repository at this point in the history
  • Loading branch information
inflation committed Sep 19, 2024
1 parent dd2d8e3 commit 6b09685
Show file tree
Hide file tree
Showing 28 changed files with 139 additions and 1,043 deletions.
4 changes: 2 additions & 2 deletions jpegxl-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "GPL-3.0-or-later"
name = "jpegxl-rs"
readme = "README.md"
repository = "https://github.com/inflation/jpegxl-rs"
version = "0.10.4+libjxl-0.10.3"
version = "0.10.5+libjxl-0.11.0"
rust-version.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -35,7 +35,7 @@ half = "2.4.0"
byteorder = "1.5.0"

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

Expand Down
4 changes: 2 additions & 2 deletions jpegxl-rs/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ along with jpegxl-rs. If not, see <https://www.gnu.org/licenses/>.
use byteorder::{ByteOrder, NativeEndian, BE, LE};
use half::f16;

use jpegxl_sys::types::{JxlDataType, JxlPixelFormat};
use jpegxl_sys::common::types::{JxlDataType, JxlPixelFormat};

/// Endianness of the pixels
pub type Endianness = jpegxl_sys::types::JxlEndianness;
pub type Endianness = jpegxl_sys::common::types::JxlEndianness;

mod private {
pub trait Sealed {}
Expand Down
65 changes: 33 additions & 32 deletions jpegxl-rs/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ use std::{mem::MaybeUninit, ptr::null};

#[allow(clippy::wildcard_imports)]
use jpegxl_sys::{
codestream_header::{JxlBasicInfo, JxlOrientation},
common::types::{JxlDataType, JxlPixelFormat},
decode::*,
types::{JxlDataType, JxlPixelFormat},
metadata::codestream_header::{JxlBasicInfo, JxlOrientation},
};

use crate::{
common::{Endianness, PixelType},
errors::{check_dec_status, DecodeError},
memory::MemoryManager,
parallel::JxlParallelRunner,
parallel::ParallelRunner,
utils::check_valid_signature,
};

Expand Down Expand Up @@ -87,7 +87,7 @@ impl Default for PixelFormat {
pub struct JxlDecoder<'pr, 'mm> {
/// Opaque pointer to the underlying decoder
#[builder(setter(skip))]
dec: *mut jpegxl_sys::decode::JxlDecoder,
ptr: *mut jpegxl_sys::decode::JxlDecoder,

/// Override desired pixel format
pub pixel_format: Option<PixelFormat>,
Expand Down Expand Up @@ -150,7 +150,7 @@ pub struct JxlDecoder<'pr, 'mm> {
pub init_jpeg_buffer: usize,

/// Set parallel runner
pub parallel_runner: Option<&'pr dyn JxlParallelRunner>,
pub parallel_runner: Option<&'pr dyn ParallelRunner>,

/// Set memory manager
pub memory_manager: Option<&'mm dyn MemoryManager>,
Expand All @@ -161,7 +161,7 @@ impl<'pr, 'mm> JxlDecoderBuilder<'pr, 'mm> {
///
/// # Errors
/// Return [`DecodeError::CannotCreateDecoder`] if it fails to create the decoder.
pub fn build(&self) -> Result<JxlDecoder<'pr, 'mm>, DecodeError> {
pub fn build(&mut self) -> Result<JxlDecoder<'pr, 'mm>, DecodeError> {
let mm = self.memory_manager.flatten();
let dec = unsafe {
mm.map_or_else(
Expand All @@ -175,7 +175,7 @@ impl<'pr, 'mm> JxlDecoderBuilder<'pr, 'mm> {
}

Ok(JxlDecoder {
dec,
ptr: dec,
pixel_format: self.pixel_format.flatten(),
skip_reorientation: self.skip_reorientation.flatten(),
unpremul_alpha: self.unpremul_alpha.flatten(),
Expand Down Expand Up @@ -217,22 +217,22 @@ impl<'pr, 'mm> JxlDecoder<'pr, 'mm> {
let next_in = data.as_ptr();
let avail_in = std::mem::size_of_val(data) as _;

check_dec_status(unsafe { JxlDecoderSetInput(self.dec, next_in, avail_in) })?;
unsafe { JxlDecoderCloseInput(self.dec) };
check_dec_status(unsafe { JxlDecoderSetInput(self.ptr, next_in, avail_in) })?;
unsafe { JxlDecoderCloseInput(self.ptr) };

let mut status;
loop {
use JxlDecoderStatus as s;

status = unsafe { JxlDecoderProcessInput(self.dec) };
status = unsafe { JxlDecoderProcessInput(self.ptr) };

match status {
s::NeedMoreInput | s::Error => return Err(DecodeError::GenericError),

// Get the basic info
s::BasicInfo => {
check_dec_status(unsafe {
JxlDecoderGetBasicInfo(self.dec, basic_info.as_mut_ptr())
JxlDecoderGetBasicInfo(self.ptr, basic_info.as_mut_ptr())
})?;

if let Some(pr) = self.parallel_runner {
Expand All @@ -246,26 +246,26 @@ impl<'pr, 'mm> JxlDecoder<'pr, 'mm> {
}

// Get JPEG reconstruction buffer
s::JpegReconstruction => {
s::JPEGReconstruction => {
// Safety: JpegReconstruction is only called when reconstruct_jpeg_buffer
// is not None
let buf = unsafe { reconstruct_jpeg_buffer.as_mut().unwrap_unchecked() };
buf.resize(self.init_jpeg_buffer, 0);
check_dec_status(unsafe {
JxlDecoderSetJPEGBuffer(self.dec, buf.as_mut_ptr(), buf.len())
JxlDecoderSetJPEGBuffer(self.ptr, buf.as_mut_ptr(), buf.len())
})?;
}

// JPEG buffer need more space
s::JpegNeedMoreOutput => {
s::JPEGNeedMoreOutput => {
// Safety: JpegNeedMoreOutput is only called when reconstruct_jpeg_buffer
// is not None
let buf = unsafe { reconstruct_jpeg_buffer.as_mut().unwrap_unchecked() };
let need_to_write = unsafe { JxlDecoderReleaseJPEGBuffer(self.dec) };
let need_to_write = unsafe { JxlDecoderReleaseJPEGBuffer(self.ptr) };

buf.resize(buf.len() + need_to_write, 0);
check_dec_status(unsafe {
JxlDecoderSetJPEGBuffer(self.dec, buf.as_mut_ptr(), buf.len())
JxlDecoderSetJPEGBuffer(self.ptr, buf.as_mut_ptr(), buf.len())
})?;
}

Expand All @@ -277,13 +277,13 @@ impl<'pr, 'mm> JxlDecoder<'pr, 'mm> {
s::FullImage => continue,
s::Success => {
if let Some(buf) = reconstruct_jpeg_buffer.as_mut() {
let remaining = unsafe { JxlDecoderReleaseJPEGBuffer(self.dec) };
let remaining = unsafe { JxlDecoderReleaseJPEGBuffer(self.ptr) };

buf.truncate(buf.len() - remaining);
buf.shrink_to_fit();
}

unsafe { JxlDecoderReset(self.dec) };
unsafe { JxlDecoderReset(self.ptr) };

let info = unsafe { basic_info.assume_init() };
return Ok(Metadata {
Expand All @@ -304,6 +304,7 @@ impl<'pr, 'mm> JxlDecoder<'pr, 'mm> {
s::PreviewImage => todo!(),
s::Frame => todo!(),
s::Box => todo!(),
s::BoxComplete => todo!(),
s::FrameProgression => todo!(),
}
}
Expand All @@ -312,39 +313,39 @@ impl<'pr, 'mm> JxlDecoder<'pr, 'mm> {
fn setup_decoder(&self, icc: bool, reconstruct_jpeg: bool) -> Result<(), DecodeError> {
if let Some(runner) = self.parallel_runner {
check_dec_status(unsafe {
JxlDecoderSetParallelRunner(self.dec, runner.runner(), runner.as_opaque_ptr())
JxlDecoderSetParallelRunner(self.ptr, runner.runner(), runner.as_opaque_ptr())
})?;
}

let events = {
use JxlDecoderStatus::{BasicInfo, ColorEncoding, FullImage, JpegReconstruction};
use JxlDecoderStatus::{BasicInfo, ColorEncoding, FullImage, JPEGReconstruction};

let mut events = BasicInfo as i32 | FullImage as i32;
if icc {
events |= ColorEncoding as i32;
}
if reconstruct_jpeg {
events |= JpegReconstruction as i32;
events |= JPEGReconstruction as i32;
}

events
};
check_dec_status(unsafe { JxlDecoderSubscribeEvents(self.dec, events) })?;
check_dec_status(unsafe { JxlDecoderSubscribeEvents(self.ptr, events) })?;

if let Some(val) = self.skip_reorientation {
check_dec_status(unsafe { JxlDecoderSetKeepOrientation(self.dec, val.into()) })?;
check_dec_status(unsafe { JxlDecoderSetKeepOrientation(self.ptr, val.into()) })?;
}
if let Some(val) = self.unpremul_alpha {
check_dec_status(unsafe { JxlDecoderSetUnpremultiplyAlpha(self.dec, val.into()) })?;
check_dec_status(unsafe { JxlDecoderSetUnpremultiplyAlpha(self.ptr, val.into()) })?;
}
if let Some(val) = self.render_spotcolors {
check_dec_status(unsafe { JxlDecoderSetRenderSpotcolors(self.dec, val.into()) })?;
check_dec_status(unsafe { JxlDecoderSetRenderSpotcolors(self.ptr, val.into()) })?;
}
if let Some(val) = self.coalescing {
check_dec_status(unsafe { JxlDecoderSetCoalescing(self.dec, val.into()) })?;
check_dec_status(unsafe { JxlDecoderSetCoalescing(self.ptr, val.into()) })?;
}
if let Some(val) = self.desired_intensity_target {
check_dec_status(unsafe { JxlDecoderSetDesiredIntensityTarget(self.dec, val) })?;
check_dec_status(unsafe { JxlDecoderSetDesiredIntensityTarget(self.ptr, val) })?;
}

Ok(())
Expand All @@ -353,13 +354,13 @@ impl<'pr, 'mm> JxlDecoder<'pr, 'mm> {
fn get_icc_profile(&self, icc_profile: &mut Vec<u8>) -> Result<(), DecodeError> {
let mut icc_size = 0;
check_dec_status(unsafe {
JxlDecoderGetICCProfileSize(self.dec, JxlColorProfileTarget::Data, &mut icc_size)
JxlDecoderGetICCProfileSize(self.ptr, JxlColorProfileTarget::Data, &mut icc_size)
})?;
icc_profile.resize(icc_size, 0);

check_dec_status(unsafe {
JxlDecoderGetColorAsICCProfile(
self.dec,
self.ptr,
JxlColorProfileTarget::Data,
icc_profile.as_mut_ptr(),
icc_size,
Expand Down Expand Up @@ -401,12 +402,12 @@ impl<'pr, 'mm> JxlDecoder<'pr, 'mm> {

let mut size = 0;
check_dec_status(unsafe {
JxlDecoderImageOutBufferSize(self.dec, &pixel_format, &mut size)
JxlDecoderImageOutBufferSize(self.ptr, &pixel_format, &mut size)
})?;
pixels.resize(size, 0);

check_dec_status(unsafe {
JxlDecoderSetImageOutBuffer(self.dec, &pixel_format, pixels.as_mut_ptr().cast(), size)
JxlDecoderSetImageOutBuffer(self.ptr, &pixel_format, pixels.as_mut_ptr().cast(), size)
})?;

unsafe { *format = pixel_format };
Expand Down Expand Up @@ -496,7 +497,7 @@ impl<'pr, 'mm> JxlDecoder<'pr, 'mm> {

impl<'prl, 'mm> Drop for JxlDecoder<'prl, 'mm> {
fn drop(&mut self) {
unsafe { JxlDecoderDestroy(self.dec) };
unsafe { JxlDecoderDestroy(self.ptr) };
}
}

Expand Down
2 changes: 1 addition & 1 deletion jpegxl-rs/src/decode/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ along with jpegxl-rs. If not, see <https://www.gnu.org/licenses/>.
*/

use half::f16;
use jpegxl_sys::types::{JxlDataType, JxlPixelFormat};
use jpegxl_sys::common::types::{JxlDataType, JxlPixelFormat};

use super::Orientation;
use crate::common::PixelType;
Expand Down
14 changes: 7 additions & 7 deletions jpegxl-rs/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ along with jpegxl-rs. If not, see <https://www.gnu.org/licenses/>.
use std::{marker::PhantomData, mem::MaybeUninit, ops::Deref, ptr::null};

#[allow(clippy::wildcard_imports)]
use jpegxl_sys::encode::*;
use jpegxl_sys::encoder::encode::*;

use crate::{
common::PixelType, errors::EncodeError, memory::MemoryManager, parallel::JxlParallelRunner,
common::PixelType, errors::EncodeError, memory::MemoryManager, parallel::ParallelRunner,
};

mod options;
Expand Down Expand Up @@ -62,7 +62,7 @@ impl<U: PixelType> Deref for EncoderResult<U> {
pub struct JxlEncoder<'prl, 'mm> {
/// Opaque pointer to the underlying encoder
#[builder(setter(skip))]
enc: *mut jpegxl_sys::encode::JxlEncoder,
enc: *mut jpegxl_sys::encoder::encode::JxlEncoder,
/// Opaque pointer to the encoder options
#[builder(setter(skip))]
options_ptr: *mut JxlEncoderFrameSettings,
Expand Down Expand Up @@ -121,7 +121,7 @@ pub struct JxlEncoder<'prl, 'mm> {
/// Set parallel runner
///
/// Default: `None`, indicating single thread execution
pub parallel_runner: Option<&'prl dyn JxlParallelRunner>,
pub parallel_runner: Option<&'prl dyn ParallelRunner>,

/// Whether box is used in encoder
use_box: bool,
Expand Down Expand Up @@ -211,7 +211,7 @@ impl JxlEncoder<'_, '_> {
self.check_enc_status(unsafe {
JxlEncoderFrameSettingsSetOption(
self.options_ptr,
FrameSetting::Effort,
JxlEncoderFrameSettingId::Effort,
self.speed as _,
)
})?;
Expand All @@ -221,7 +221,7 @@ impl JxlEncoder<'_, '_> {
self.check_enc_status(unsafe {
JxlEncoderFrameSettingsSetOption(
self.options_ptr,
FrameSetting::DecodingSpeed,
JxlEncoderFrameSettingId::DecodingSpeed,
self.decoding_speed,
)
})?;
Expand Down Expand Up @@ -365,7 +365,7 @@ impl<'prl, 'mm> JxlEncoder<'prl, 'mm> {
/// Return [`EncodeError`] if it fails to set frame option
pub fn set_frame_option(
&mut self,
option: FrameSetting,
option: JxlEncoderFrameSettingId,
value: i64,
) -> Result<(), EncodeError> {
self.check_enc_status(unsafe {
Expand Down
2 changes: 1 addition & 1 deletion jpegxl-rs/src/encode/frame.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::marker::PhantomData;

use jpegxl_sys::types::{JxlEndianness, JxlPixelFormat};
use jpegxl_sys::common::types::{JxlEndianness, JxlPixelFormat};

use crate::{common::PixelType, EncodeError};

Expand Down
2 changes: 1 addition & 1 deletion jpegxl-rs/src/encode/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use jpegxl_sys::types::JxlBoxType;
use jpegxl_sys::common::types::JxlBoxType;

/// Metadata box
pub enum Metadata<'d> {
Expand Down
2 changes: 1 addition & 1 deletion jpegxl-rs/src/encode/options.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::mem::MaybeUninit;

use jpegxl_sys::{color_encoding::JxlColorEncoding, encode as api};
use jpegxl_sys::{color::color_encoding::JxlColorEncoding, encoder::encode as api};

/// Encoding speed
#[derive(Debug, Clone, Copy)]
Expand Down
Loading

0 comments on commit 6b09685

Please sign in to comment.