use crate::bit_depth::BitDepth;
use crate::colorspace::ColorSpace;
#[derive(Copy, Debug, Clone, Default)]
struct EncoderFlags {
jpeg_encode_progressive: bool,
jpeg_optimize_huffman: bool,
image_strip_metadata: bool
}
#[derive(Debug, Copy, Clone)]
pub struct EncoderOptions {
width: usize,
height: usize,
colorspace: ColorSpace,
quality: u8,
depth: BitDepth,
num_threads: u8,
effort: u8,
flags: EncoderFlags
}
impl Default for EncoderOptions {
fn default() -> Self {
Self {
width: 0,
height: 0,
colorspace: ColorSpace::RGB,
quality: 80,
depth: BitDepth::Eight,
num_threads: 4,
effort: 4,
flags: EncoderFlags::default()
}
}
}
impl EncoderOptions {
pub fn new(
width: usize, height: usize, colorspace: ColorSpace, depth: BitDepth
) -> EncoderOptions {
EncoderOptions {
width,
height,
colorspace,
depth,
..Default::default()
}
}
pub const fn get_width(&self) -> usize {
self.width
}
pub fn get_height(&self) -> usize {
assert_ne!(self.height, 0);
self.height
}
pub const fn get_depth(&self) -> BitDepth {
self.depth
}
pub const fn get_quality(&self) -> u8 {
self.quality
}
pub const fn get_colorspace(&self) -> ColorSpace {
self.colorspace
}
pub const fn get_effort(&self) -> u8 {
self.effort
}
pub fn set_width(mut self, width: usize) -> Self {
self.width = width;
self
}
pub fn set_height(mut self, height: usize) -> Self {
self.height = height;
self
}
pub fn set_depth(mut self, depth: BitDepth) -> Self {
self.depth = depth;
self
}
pub fn set_quality(mut self, quality: u8) -> Self {
self.quality = quality.clamp(0, 100);
self
}
pub fn set_colorspace(mut self, colorspace: ColorSpace) -> Self {
self.colorspace = colorspace;
self
}
pub fn set_num_threads(mut self, threads: u8) -> Self {
self.num_threads = threads;
self
}
pub fn set_effort(mut self, effort: u8) -> Self {
self.effort = effort;
self
}
pub const fn get_num_threads(&self) -> u8 {
self.num_threads
}
pub fn set_strip_metadata(mut self, yes: bool) -> Self {
self.flags.image_strip_metadata = yes;
self
}
pub const fn strip_metadata(&self) -> bool {
!self.flags.image_strip_metadata
}
}
impl EncoderOptions {
pub const fn jpeg_encode_progressive(&self) -> bool {
self.flags.jpeg_encode_progressive
}
pub const fn jpeg_optimized_huffman_tables(&self) -> bool {
self.flags.jpeg_optimize_huffman
}
pub fn set_jpeg_encode_progressive(mut self, yes: bool) -> Self {
self.flags.jpeg_optimize_huffman = yes;
self
}
}