use magick_rust::{bindings, MagickError};
use str_utils::EndsWithIgnoreAsciiCase;
use crate::{
compute_output_size_sharpen, fetch_magic_wand, Crop, ImageConfig, ImageResource, InterlaceType,
};
#[derive(Debug)]
pub struct GIFConfig {
pub remain_profile: bool,
pub width: u16,
pub height: u16,
pub crop: Option<Crop>,
pub shrink_only: bool,
pub sharpen: f64,
}
impl GIFConfig {
#[inline]
pub fn new() -> GIFConfig {
GIFConfig {
remain_profile: false,
width: 0u16,
height: 0u16,
crop: None,
shrink_only: true,
sharpen: -1f64,
}
}
}
impl Default for GIFConfig {
#[inline]
fn default() -> Self {
GIFConfig::new()
}
}
impl ImageConfig for GIFConfig {
#[inline]
fn is_remain_profile(&self) -> bool {
self.remain_profile
}
#[inline]
fn get_width(&self) -> u16 {
self.width
}
#[inline]
fn get_height(&self) -> u16 {
self.height
}
#[inline]
fn get_crop(&self) -> Option<Crop> {
self.crop
}
#[inline]
fn get_sharpen(&self) -> f64 {
self.sharpen
}
#[inline]
fn is_shrink_only(&self) -> bool {
self.shrink_only
}
}
pub fn to_gif(
output: &mut ImageResource,
input: &ImageResource,
config: &GIFConfig,
) -> Result<(), MagickError> {
let (mut mw, vector) = fetch_magic_wand(input, config)?;
if !vector {
let (width, height, sharpen) = compute_output_size_sharpen(&mw, config);
mw.resize_image(width as usize, height as usize, bindings::FilterType_LanczosFilter);
mw.sharpen_image(0f64, sharpen)?;
}
if !config.remain_profile {
mw.profile_image("*", None)?;
}
mw.set_image_compression_quality(100)?;
mw.set_interlace_scheme(InterlaceType::LineInterlace.ordinal() as bindings::InterlaceType)?;
mw.set_image_format("GIF")?;
match output {
ImageResource::Path(p) => {
if !p.ends_with_ignore_ascii_case_with_lowercase(".gif") {
return Err("The file extension name is not gif.".into());
}
mw.write_image(p.as_str())?;
}
ImageResource::Data(b) => {
let mut temp = mw.write_image_blob("GIF")?;
b.append(&mut temp);
}
ImageResource::MagickWand(mw_2) => {
*mw_2 = mw;
}
}
Ok(())
}