#[allow(warnings)]
#[cfg(feature = "images")]
pub mod images{
    use image::ImageFormat;
    use image::{DynamicImage, ImageBuffer, Rgba, RgbaImage};
    use image::GenericImageView;
    /// # image_add_padding
    /// padding_color [[r,g,b,a]]
    /// ```ignore
    /// use doe::images::image_add_padding;
    /// image_add_padding(
    ///     "demo.png",
    ///     [255,255,255,255], 10, 
    ///     10,
    ///     "new_demo.png"
    /// );
    /// ````
    /// 
    pub fn image_add_padding(path:&str,padding_color:[u8; 4],padding_vertical:u32,padding_horisontal:u32,new_path:&str) -> DynamicImage{
        let image = image::open(path).unwrap();
        let (width, height) = (image.width(),image.height());
        let padded_width = width + 2 * padding_horisontal;
        let padded_height = height + 2 * padding_vertical;
        let color = Rgba(padding_color); //color
        let mut padded_image: RgbaImage = ImageBuffer::from_pixel(padded_width, padded_height, color);
        for (x, y, pixel) in image.pixels() {
            padded_image.put_pixel(x + padding_horisontal, y + padding_vertical, pixel);
        }
        DynamicImage::ImageRgba8(padded_image)
    }

    pub fn image_resize(path:&str,size:(u32,u32))->DynamicImage{
        // 打开图像文件
        let img = image::open(path).expect("无法打开图像文件");
        // 调整图像大小
        let resized_img = img.resize_exact(size.0, size.1, image::imageops::FilterType::Lanczos3);
        resized_img
    }
    /// # image_convert
    /// ```ignore
    /// let new_format = [
    ///     "Png",
    ///     "Jpeg",
    ///     "Gif",
    ///     "WebP",
    ///     "Pnm",
    ///     "Tiff",
    ///     "Tga",
    ///     "Dds",
    ///     "Bmp",
    ///     "Ico",
    ///     "Hdr",
    ///     "OpenExr",
    ///     "Farbfeld",
    ///     "Avif",
    ///     "Qoi",
    /// ]
    /// ```
    /// ```ignore
    /// use doe::images::image_convert;
    /// image_convert("dvwa_23_14.png","Jpeg","demo.jpg");
    /// ```
    /// 
    pub fn image_convert(path: &str, new_format: &str,new_path:&str){
        // 打开图像文件
        let img = image::open(path).expect("无法打开图像文件");
        let format = match new_format.as_ref() {
            "Png"=>{(ImageFormat::Png,"png")},
            "Jpeg"=>{(ImageFormat::Jpeg,"jpg")},
            "Gif"=>{(ImageFormat::Gif,"gif")},
            "WebP"=>{(ImageFormat::WebP,"webp")},
            "Pnm"=>{(ImageFormat::Pnm,"pnm")},
            "Tiff"=>{(ImageFormat::Tiff,"tiff")},
            "Tga"=>{(ImageFormat::Tga,"tga")},
            "Dds"=>{(ImageFormat::Dds,"dds")},
            "Bmp"=>{(ImageFormat::Bmp,"bmp")},
            "Ico"=>{(ImageFormat::Ico,"ico")},
            "Hdr"=>{(ImageFormat::Hdr,"hdr")},
            "OpenExr"=>{(ImageFormat::OpenExr,"exr")},
            "Farbfeld"=>{(ImageFormat::Farbfeld,"ff")},
            "Avif"=>{(ImageFormat::Avif,"avif")},
            "Qoi"=>{(ImageFormat::Qoi,"qoi")},
            _=>{(ImageFormat::Png,"png")}
        };
        img.save_with_format(new_path, format.0).unwrap();
    }

    pub fn convert_to_icns(png_path: &str, icns_path: &str) -> Result<(), Box<dyn std::error::Error>> {
        use icns::{IconFamily, IconType, Image};
        let file = std::io::BufReader::new(std::fs::File::open(png_path).unwrap());
        let image = Image::read_png(file).unwrap();
        let w = image.width();
        let h = image.height();
        let new_file = std::io::BufWriter::new(std::fs::File::create(icns_path).unwrap());
        let mut icon_family = IconFamily::new();
        icon_family.add_icon(&image).unwrap();
        icon_family.write(new_file).unwrap();
        Ok(())
    }
    
    pub fn convert_to_ico(png_path: &str, ico_path: &str) -> Result<(), Box<dyn std::error::Error>> {
        use std::fs::write;
        use std::fs::File;
        use std::io::Write;
        // Create a new, empty icon collection:
        let mut icon_dir = ico::IconDir::new(ico::ResourceType::Icon);
        // Read a PNG file from disk and add it to the collection:
        let file = std::fs::File::open(png_path).unwrap();
        let image = ico::IconImage::read_png(file).unwrap();
        icon_dir.add_entry(ico::IconDirEntry::encode(&image).unwrap());
        let file = std::fs::File::create(ico_path).unwrap();
        icon_dir.write(file).unwrap();
        Ok(())
    }
    

}
#[cfg(feature = "images")]
pub use images::*;