-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
233 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use std::marker::PhantomData; | ||
|
||
use jpegxl_sys::types::{JxlEndianness, JxlPixelFormat}; | ||
|
||
use crate::{common::PixelType, EncodeError}; | ||
|
||
use super::{EncoderResult, JxlEncoder}; | ||
|
||
/// A frame for the encoder, consisting of the pixels and its options | ||
#[allow(clippy::module_name_repetitions)] | ||
pub struct EncoderFrame<'data, T: PixelType> { | ||
pub(crate) data: &'data [T], | ||
num_channels: Option<u32>, | ||
endianness: Option<JxlEndianness>, | ||
align: Option<usize>, | ||
} | ||
|
||
impl<'data, T: PixelType> EncoderFrame<'data, T> { | ||
/// Create a default frame from the data. | ||
/// | ||
/// Use RGB(3) channels, native endianness and no alignment. | ||
pub fn new(data: &'data [T]) -> Self { | ||
Self { | ||
data, | ||
num_channels: None, | ||
endianness: None, | ||
align: None, | ||
} | ||
} | ||
|
||
/// Set the number of channels of the source. | ||
/// | ||
/// _Note_: If you want to use alpha channel, add here | ||
#[must_use] | ||
pub fn num_channels(mut self, value: u32) -> Self { | ||
self.num_channels = Some(value); | ||
self | ||
} | ||
|
||
/// Set the endianness of the source. | ||
#[must_use] | ||
pub fn endianness(mut self, value: JxlEndianness) -> Self { | ||
self.endianness = Some(value); | ||
self | ||
} | ||
|
||
/// Set the align of the source. | ||
/// Align scanlines to a multiple of align bytes, or 0 to require no alignment at all | ||
#[must_use] | ||
pub fn align(mut self, value: usize) -> Self { | ||
self.align = Some(value); | ||
self | ||
} | ||
|
||
pub(crate) fn pixel_format(&self) -> JxlPixelFormat { | ||
JxlPixelFormat { | ||
num_channels: self.num_channels.unwrap_or(3), | ||
data_type: T::pixel_type(), | ||
endianness: self.endianness.unwrap_or(JxlEndianness::Native), | ||
align: self.align.unwrap_or(0), | ||
} | ||
} | ||
} | ||
|
||
/// A wrapper type for encoding multiple frames | ||
pub struct MultiFrames<'enc, 'prl, 'mm, U>( | ||
pub(crate) &'enc mut JxlEncoder<'prl, 'mm>, | ||
pub(crate) PhantomData<U>, | ||
) | ||
where | ||
'prl: 'enc, | ||
'mm: 'enc; | ||
|
||
impl<U: PixelType> MultiFrames<'_, '_, '_, U> { | ||
/// Add a frame to the encoder | ||
/// # Errors | ||
/// Return [`EncodeError`] if the internal encoder fails to add a frame | ||
pub fn add_frame<T: PixelType>(self, frame: &EncoderFrame<T>) -> Result<Self, EncodeError> { | ||
self.0.add_frame(frame)?; | ||
Ok(self) | ||
} | ||
|
||
/// Add a JPEG raw frame to the encoder | ||
/// # Errors | ||
/// Return [`EncodeError`] if the internal encoder fails to add a jpeg frame | ||
pub fn add_jpeg_frame(self, data: &[u8]) -> Result<Self, EncodeError> { | ||
self.0.add_jpeg_frame(data)?; | ||
Ok(self) | ||
} | ||
|
||
/// Encode a JPEG XL image from the frames | ||
/// # Errors | ||
/// Return [`EncodeError`] if the internal encoder fails to encode | ||
pub fn encode(self) -> Result<EncoderResult<U>, EncodeError> { | ||
self.0.start_encoding() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use jpegxl_sys::types::JxlBoxType; | ||
|
||
/// Metadata box | ||
pub enum Metadata<'d> { | ||
/// EXIF | ||
/// The contents of this box must be prepended by a 4-byte tiff header offset, | ||
/// which may be 4 zero bytes in case the tiff header follows immediately. | ||
Exif(&'d [u8]), | ||
/// XMP/IPTC metadata | ||
Xmp(&'d [u8]), | ||
/// JUMBF superbox | ||
Jumb(&'d [u8]), | ||
/// Custom Metadata. | ||
/// Type should not start with `jxl`, `JXL`, or conflict with other box type, | ||
/// and should be registered with MP4RA (mp4ra.org). | ||
Custom([u8; 4], &'d [u8]), | ||
} | ||
|
||
impl Metadata<'_> { | ||
#[must_use] | ||
pub(crate) fn box_type(t: [u8; 4]) -> JxlBoxType { | ||
JxlBoxType(unsafe { std::mem::transmute(t) }) | ||
} | ||
} |
Oops, something went wrong.