use crate::{InterlaceType, ImageResource, ImageConfig, compute_output_size_sharpen, fetch_magic_wand, magick_rust::bindings, starts_ends_with_caseless::EndsWithCaseless};
#[derive(Debug)]
pub struct PNGConfig {
pub width: u16,
pub height: u16,
pub shrink_only: bool,
pub sharpen: f64,
pub ppi: f64,
}
impl PNGConfig {
pub fn new() -> PNGConfig {
PNGConfig {
width: 0u16,
height: 0u16,
shrink_only: true,
sharpen: -1f64,
ppi: 72f64,
}
}
}
impl ImageConfig for PNGConfig {
fn get_width(&self) -> u16 {
self.width
}
fn get_height(&self) -> u16 {
self.height
}
fn get_sharpen(&self) -> f64 {
self.sharpen
}
fn is_shrink_only(&self) -> bool {
self.shrink_only
}
}
pub fn to_png(output: &mut ImageResource, input: &ImageResource, config: &PNGConfig) -> Result<(), &'static str> {
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)?;
}
mw.profile_image("*", None)?;
mw.set_image_compression_quality(100)?;
mw.set_interlace_scheme(InterlaceType::LineInterlace.ordinal() as bindings::InterlaceType)?;
mw.set_image_format("PNG")?;
if config.ppi >= 0f64 {
mw.set_image_resolution(config.ppi, config.ppi)?;
mw.set_image_units(bindings::ResolutionType_PixelsPerInchResolution)?;
}
match output {
ImageResource::Path(p) => {
if !p.ends_with_caseless_ascii(".png") {
return Err("The file extension name is not png.");
}
mw.write_image(p.as_str())?;
}
ImageResource::Data(b) => {
let mut temp = mw.write_image_blob("PNG")?;
b.append(&mut temp);
}
ImageResource::MagickWand(mw_2) => {
*mw_2 = mw;
}
}
Ok(())
}