Skip to content

Commit

Permalink
Allow setting the AvifEncoder ColorSpace
Browse files Browse the repository at this point in the history
  • Loading branch information
paolobarbolini committed Feb 2, 2021
1 parent 6d55953 commit 80a02d1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
34 changes: 31 additions & 3 deletions src/codecs/avif/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::error::{EncodingError, ParameterError, ParameterErrorKind, Unsupporte

use bytemuck::{Pod, PodCastError, try_cast_slice, try_cast_slice_mut};
use num_traits::Zero;
use ravif::{Img, ColorSpace, Config, RGBA8, encode_rgba};
use ravif::{Img, Config, RGBA8, encode_rgba};
use rgb::AsPixels;

/// AVIF Encoder.
Expand All @@ -27,6 +27,28 @@ pub struct AvifEncoder<W> {
config: Config
}

/// An enumeration over supported AVIF color spaces
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ColorSpace {
/// sRGB colorspace
Srgb,
/// BT.709 colorspace
Bt709,

#[doc(hidden)]
__NonExhaustive(crate::utils::NonExhaustiveMarker),
}

impl ColorSpace {
fn to_ravif(self) -> ravif::ColorSpace {
match self {
Self::Srgb => ravif::ColorSpace::RGB,
Self::Bt709 => ravif::ColorSpace::YCbCr,
Self::__NonExhaustive(marker) => match marker._private {},
}
}
}

impl<W: Write> AvifEncoder<W> {
/// Create a new encoder that writes its output to `w`.
pub fn new(w: W) -> Self {
Expand All @@ -49,11 +71,17 @@ impl<W: Write> AvifEncoder<W> {
alpha_quality: quality,
speed,
premultiplied_alpha: false,
color_space: ColorSpace::RGB,
color_space: ravif::ColorSpace::RGB,
}
}
}

/// Encode with the specified `color_space`.
pub fn with_colorspace(mut self, color_space: ColorSpace) -> Self {
self.config.color_space = color_space.to_ravif();
self
}

/// Encode image data with the indicated color type.
///
/// The encoder currently requires all data to be RGBA8, it will be converted internally if
Expand Down Expand Up @@ -198,4 +226,4 @@ impl<W: Write> AvifEncoder<W> {
)))
}
}
}
}
3 changes: 1 addition & 2 deletions src/codecs/avif/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
/// The [AVIF] specification defines an image derivative of the AV1 bitstream, an open video codec.
///
/// [AVIF]: https://aomediacodec.github.io/av1-avif/

pub use self::decoder::AvifDecoder;
pub use self::encoder::AvifEncoder;
pub use self::encoder::{AvifEncoder, ColorSpace};

mod decoder;
mod encoder;

0 comments on commit 80a02d1

Please sign in to comment.