fn create_icns_file(
  resources_dir: &PathBuf,
  settings: &Settings,
) -> crate::Result<Option<PathBuf>> {
  if settings.icon_files().count() == 0 {
    return Ok(None);
  }

  // If one of the icon files is already an ICNS file, just use that.
  for icon_path in settings.icon_files() {
    let icon_path = icon_path?;
    if icon_path.extension() == Some(OsStr::new("icns")) {
      let mut dest_path = resources_dir.to_path_buf();
      dest_path.push(icon_path.file_name().expect("Could not get icon filename"));
      common::copy_file(&icon_path, &dest_path)?;
      return Ok(Some(dest_path));
    }
  }

  // Otherwise, read available images and pack them into a new ICNS file.
  let mut family = icns::IconFamily::new();

  fn add_icon_to_family(
    icon: image::DynamicImage,
    density: u32,
    family: &mut icns::IconFamily,
  ) -> io::Result<()> {
    // Try to add this image to the icon family.  Ignore images whose sizes
    // don't map to any ICNS icon type; print warnings and skip images that
    // fail to encode.
    match icns::IconType::from_pixel_size_and_density(icon.width(), icon.height(), density) {
      Some(icon_type) => {
        if !family.has_icon_with_type(icon_type) {
          let icon = make_icns_image(icon)?;
          family.add_icon_with_type(&icon, icon_type)?;
        }
        Ok(())
      }
      None => Err(io::Error::new(
        io::ErrorKind::InvalidData,
        "No matching IconType",
      )),
    }
  }

  let mut images_to_resize: Vec<(image::DynamicImage, u32, u32)> = vec![];
  for icon_path in settings.icon_files() {
    let icon_path = icon_path?;
    let icon = image::open(&icon_path)?;
    let density = if common::is_retina(&icon_path) { 2 } else { 1 };
    let (w, h) = icon.dimensions();
    let orig_size = min(w, h);
    let next_size_down = 2f32.powf((orig_size as f32).log2().floor()) as u32;
    if orig_size > next_size_down {
      images_to_resize.push((icon, next_size_down, density));
    } else {
      add_icon_to_family(icon, density, &mut family)?;
    }
  }

  for (icon, next_size_down, density) in images_to_resize {
    let icon = icon.resize_exact(
      next_size_down,
      next_size_down,
      image::imageops::FilterType::Lanczos3,
    );
    add_icon_to_family(icon, density, &mut family)?;
  }

  if !family.is_empty() {
    fs::create_dir_all(resources_dir)?;
    let mut dest_path = resources_dir.clone();
    dest_path.push(settings.bundle_name());
    dest_path.set_extension("icns");
    let icns_file = BufWriter::new(File::create(&dest_path)?);
    family.write(icns_file)?;
    Ok(Some(dest_path))
  } else {
    Err(crate::Error::GenericError(
      "No usable Icon files found".to_owned(),
    ))
  }
}
// Given a list of icon files, try to produce an ICNS file in the resources
// directory and return the path to it.  Returns `Ok(None)` if no usable icons
// were provided.
Given a list of icon files, try to produce an ICNS file in the resources
directory and return the path to it.  Returns `Ok(None)` if no usable icons
were provided.
Given a list of icon files try to produce an ICNS file in the resources directory and return the path to it . Returns ` Ok ( None ) ` if no usable icons were provided .
fn create_icns_file(
  resources_dir: &PathBuf,
  settings: &Settings,
) -> crate::Result<Option<PathBuf>> {
  if settings.icon_files().count() == 0 {
    return Ok(None);
  }
  // If one of the icon files is already an ICNS file, just use that.
  for icon_path in settings.icon_files() {
    let icon_path = icon_path?;
    if icon_path.extension() == Some(OsStr::new("icns")) {
      let mut dest_path = resources_dir.to_path_buf();
      dest_path.push(icon_path.file_name().expect("Could not get icon filename"));
      common::copy_file(&icon_path, &dest_path)?;
      return Ok(Some(dest_path));
    }
  }
  // Otherwise, read available images and pack them into a new ICNS file.
  let mut family = icns::IconFamily::new();
  fn add_icon_to_family(
    icon: image::DynamicImage,
    density: u32,
    family: &mut icns::IconFamily,
  ) -> io::Result<()> {
    // Try to add this image to the icon family.  Ignore images whose sizes
    // don't map to any ICNS icon type; print warnings and skip images that
    // fail to encode.
    match icns::IconType::from_pixel_size_and_density(icon.width(), icon.height(), density) {
      Some(icon_type) => {
        if !family.has_icon_with_type(icon_type) {
          let icon = make_icns_image(icon)?;
          family.add_icon_with_type(&icon, icon_type)?;
        }
        Ok(())
      }
      None => Err(io::Error::new(
        io::ErrorKind::InvalidData,
        "No matching IconType",
      )),
    }
  }
  let mut images_to_resize: Vec<(image::DynamicImage, u32, u32)> = vec![];
  for icon_path in settings.icon_files() {
    let icon_path = icon_path?;
    let icon = image::open(&icon_path)?;
    let density = if common::is_retina(&icon_path) { 2 } else { 1 };
    let (w, h) = icon.dimensions();
    let orig_size = min(w, h);
    let next_size_down = 2f32.powf((orig_size as f32).log2().floor()) as u32;
    if orig_size > next_size_down {
      images_to_resize.push((icon, next_size_down, density));
    } else {
      add_icon_to_family(icon, density, &mut family)?;
    }
  }
  for (icon, next_size_down, density) in images_to_resize {
    let icon = icon.resize_exact(
      next_size_down,
      next_size_down,
      image::imageops::FilterType::Lanczos3,
    );
    add_icon_to_family(icon, density, &mut family)?;
  }
  if !family.is_empty() {
    fs::create_dir_all(resources_dir)?;
    let mut dest_path = resources_dir.clone();
    dest_path.push(settings.bundle_name());
    dest_path.set_extension("icns");
    let icns_file = BufWriter::new(File::create(&dest_path)?);
    family.write(icns_file)?;
    Ok(Some(dest_path))
  } else {
    Err(crate::Error::GenericError(
      "No usable Icon files found".to_owned(),
    ))
  }
}
fn create_icns_file ( resources_dir : & PathBuf , settings : & Settings , ) -> crate :: Result < Option < PathBuf > > { if settings . icon_files ( ) . count ( ) == 0 { return Ok ( None ) ; } // If one of the icon files is already an ICNS file, just use that. for icon_path in settings . icon_files ( ) { let icon_path = icon_path ? ; if icon_path . extension ( ) == Some ( OsStr :: new ( " " ) ) { let mut dest_path = resources_dir . to_path_buf ( ) ; dest_path . push ( icon_path . file_name ( ) . expect ( " " ) ) ; common :: copy_file ( & icon_path , & dest_path ) ? ; return Ok ( Some ( dest_path ) ) ; } } // Otherwise, read available images and pack them into a new ICNS file. let mut family = icns :: IconFamily :: new ( ) ; fn add_icon_to_family ( icon : image :: DynamicImage , density : u32 , family : & mut icns :: IconFamily , ) -> io :: Result < ( ) > { // Try to add this image to the icon family.  Ignore images whose sizes // don't map to any ICNS icon type; print warnings and skip images that // fail to encode. match icns :: IconType :: from_pixel_size_and_density ( icon . width ( ) , icon . height ( ) , density ) { Some ( icon_type ) => { if ! family . has_icon_with_type ( icon_type ) { let icon = make_icns_image ( icon ) ? ; family . add_icon_with_type ( & icon , icon_type ) ? ; } Ok ( ( ) ) } None => Err ( io :: Error :: new ( io :: ErrorKind :: InvalidData , " " , ) ) , } } let mut images_to_resize : Vec < ( image :: DynamicImage , u32 , u32 ) > = vec ! [ ] ; for icon_path in settings . icon_files ( ) { let icon_path = icon_path ? ; let icon = image :: open ( & icon_path ) ? ; let density = if common :: is_retina ( & icon_path ) { 2 } else { 1 } ; let ( w , h ) = icon . dimensions ( ) ; let orig_size = min ( w , h ) ; let next_size_down = 2f32 . powf ( ( orig_size as f32 ) . log2 ( ) . floor ( ) ) as u32 ; if orig_size > next_size_down { images_to_resize . push ( ( icon , next_size_down , density ) ) ; } else { add_icon_to_family ( icon , density , & mut family ) ? ; } } for ( icon , next_size_down , density ) in images_to_resize { let icon = icon . resize_exact ( next_size_down , next_size_down , image :: imageops :: FilterType :: Lanczos3 , ) ; add_icon_to_family ( icon , density , & mut family ) ? ; } if ! family . is_empty ( ) { fs :: create_dir_all ( resources_dir ) ? ; let mut dest_path = resources_dir . clone ( ) ; dest_path . push ( settings . bundle_name ( ) ) ; dest_path . set_extension ( " " ) ; let icns_file = BufWriter :: new ( File :: create ( & dest_path ) ? ) ; family . write ( icns_file ) ? ; Ok ( Some ( dest_path ) ) } else { Err ( crate :: Error :: GenericError ( " " . to_owned ( ) , ) ) } }
Given a list of icon files, try to produce an ICNS file in the resources
directory and return the path to it.
Given a list of icon files try to produce an ICNS file in the resources directory and return the path to it .
resources_dir
&PathBuf
settings
&Settings
resources_dir
&PathBuf
settings
&Settings