pub struct IconFamily {
pub elements: Vec<IconElement>,
}
Expand description
A set of icons stored in a single ICNS file.
Fields§
§elements: Vec<IconElement>
The icon elements stored in the ICNS file.
Implementations§
Source§impl IconFamily
impl IconFamily
Sourcepub fn new() -> IconFamily
pub fn new() -> IconFamily
Creates a new, empty icon family.
Examples found in repository?
34fn main() {
35 let num_args = env::args().count();
36 if num_args < 2 || num_args > 3 {
37 println!("Usage: png2icns <path> [<ostype>]");
38 return;
39 }
40 let png_path = env::args().nth(1).unwrap();
41 let png_path = Path::new(&png_path);
42 let png_file = BufReader::new(File::open(png_path).expect("failed to open PNG file"));
43 let image = Image::read_png(png_file).expect("failed to read PNG file");
44 let mut family = IconFamily::new();
45 let icns_path = if num_args == 3 {
46 let ostype = OSType::from_str(&env::args().nth(2).unwrap()).unwrap();
47 let icon_type = IconType::from_ostype(ostype).expect("unsupported ostype");
48 family
49 .add_icon_with_type(&image, icon_type)
50 .expect("failed to encode image");
51 png_path.with_extension(format!("{}.icns", ostype))
52 } else {
53 family.add_icon(&image).expect("failed to encode image");
54 png_path.with_extension("icns")
55 };
56 let icns_file = BufWriter::new(File::create(icns_path).expect("failed to create ICNS file"));
57 family.write(icns_file).expect("failed to write ICNS file");
58}
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the icon family contains no icons nor any other elements.
Sourcepub fn add_icon(&mut self, image: &Image) -> Result<()>
pub fn add_icon(&mut self, image: &Image) -> Result<()>
Encodes the image into the family, automatically choosing an appropriate icon type based on the dimensions of the image. Returns an error if there is no supported icon type matching the image dimensions.
Examples found in repository?
34fn main() {
35 let num_args = env::args().count();
36 if num_args < 2 || num_args > 3 {
37 println!("Usage: png2icns <path> [<ostype>]");
38 return;
39 }
40 let png_path = env::args().nth(1).unwrap();
41 let png_path = Path::new(&png_path);
42 let png_file = BufReader::new(File::open(png_path).expect("failed to open PNG file"));
43 let image = Image::read_png(png_file).expect("failed to read PNG file");
44 let mut family = IconFamily::new();
45 let icns_path = if num_args == 3 {
46 let ostype = OSType::from_str(&env::args().nth(2).unwrap()).unwrap();
47 let icon_type = IconType::from_ostype(ostype).expect("unsupported ostype");
48 family
49 .add_icon_with_type(&image, icon_type)
50 .expect("failed to encode image");
51 png_path.with_extension(format!("{}.icns", ostype))
52 } else {
53 family.add_icon(&image).expect("failed to encode image");
54 png_path.with_extension("icns")
55 };
56 let icns_file = BufWriter::new(File::create(icns_path).expect("failed to create ICNS file"));
57 family.write(icns_file).expect("failed to write ICNS file");
58}
Sourcepub fn add_icon_with_type(
&mut self,
image: &Image,
icon_type: IconType,
) -> Result<()>
pub fn add_icon_with_type( &mut self, image: &Image, icon_type: IconType, ) -> Result<()>
Encodes the image into the family using the given icon type. If the selected type has an associated mask type, the image mask will also be added to the family. Returns an error if the image has the wrong dimensions for the selected type.
Examples found in repository?
34fn main() {
35 let num_args = env::args().count();
36 if num_args < 2 || num_args > 3 {
37 println!("Usage: png2icns <path> [<ostype>]");
38 return;
39 }
40 let png_path = env::args().nth(1).unwrap();
41 let png_path = Path::new(&png_path);
42 let png_file = BufReader::new(File::open(png_path).expect("failed to open PNG file"));
43 let image = Image::read_png(png_file).expect("failed to read PNG file");
44 let mut family = IconFamily::new();
45 let icns_path = if num_args == 3 {
46 let ostype = OSType::from_str(&env::args().nth(2).unwrap()).unwrap();
47 let icon_type = IconType::from_ostype(ostype).expect("unsupported ostype");
48 family
49 .add_icon_with_type(&image, icon_type)
50 .expect("failed to encode image");
51 png_path.with_extension(format!("{}.icns", ostype))
52 } else {
53 family.add_icon(&image).expect("failed to encode image");
54 png_path.with_extension("icns")
55 };
56 let icns_file = BufWriter::new(File::create(icns_path).expect("failed to create ICNS file"));
57 family.write(icns_file).expect("failed to write ICNS file");
58}
Sourcepub fn available_icons(&self) -> Vec<IconType>
pub fn available_icons(&self) -> Vec<IconType>
Returns a list of all (non-mask) icon types for which the icon family
contains the necessary element(s) for a complete icon image (including
alpha channel). These icon types can be passed to the
get_icon_with_type
method to decode the
icons.
Examples found in repository?
31fn main() {
32 let num_args = env::args().count();
33 if num_args < 2 || num_args > 3 {
34 println!("Usage: icns2png <path> [<ostype>]");
35 return;
36 }
37 let icns_path = env::args().nth(1).unwrap();
38 let icns_path = Path::new(&icns_path);
39 let icns_file = BufReader::new(File::open(icns_path).expect("failed to open ICNS file"));
40 let family = IconFamily::read(icns_file).expect("failed to read ICNS file");
41 let (icon_type, png_path) = if num_args == 3 {
42 let ostype = OSType::from_str(&env::args().nth(2).unwrap()).unwrap();
43 let icon_type = IconType::from_ostype(ostype).expect("unsupported ostype");
44 let png_path = icns_path.with_extension(format!("{}.png", ostype));
45 (icon_type, png_path)
46 } else {
47 // If no OSType is specified, extract the highest-resolution icon.
48 let &icon_type = family
49 .available_icons()
50 .iter()
51 .max_by_key(|icon_type| icon_type.pixel_width() * icon_type.pixel_height())
52 .expect("ICNS file contains no icons");
53 let png_path = icns_path.with_extension("png");
54 (icon_type, png_path)
55 };
56 let image = family
57 .get_icon_with_type(icon_type)
58 .expect("ICNS file does not contain that icon type");
59 let png_file = BufWriter::new(File::create(png_path).expect("failed to create PNG file"));
60 image.write_png(png_file).expect("failed to write PNG file");
61}
Sourcepub fn has_icon_with_type(&self, icon_type: IconType) -> bool
pub fn has_icon_with_type(&self, icon_type: IconType) -> bool
Determines whether the icon family contains a complete icon with the given type (including the mask, if the given icon type has an associated mask type).
Sourcepub fn get_icon_with_type(&self, icon_type: IconType) -> Result<Image>
pub fn get_icon_with_type(&self, icon_type: IconType) -> Result<Image>
Decodes an image from the family with the given icon type. If the selected type has an associated mask type, the two elements will decoded together into a single image. Returns an error if the element(s) for the selected type are not present in the icon family, or the if the encoded data is malformed.
Examples found in repository?
31fn main() {
32 let num_args = env::args().count();
33 if num_args < 2 || num_args > 3 {
34 println!("Usage: icns2png <path> [<ostype>]");
35 return;
36 }
37 let icns_path = env::args().nth(1).unwrap();
38 let icns_path = Path::new(&icns_path);
39 let icns_file = BufReader::new(File::open(icns_path).expect("failed to open ICNS file"));
40 let family = IconFamily::read(icns_file).expect("failed to read ICNS file");
41 let (icon_type, png_path) = if num_args == 3 {
42 let ostype = OSType::from_str(&env::args().nth(2).unwrap()).unwrap();
43 let icon_type = IconType::from_ostype(ostype).expect("unsupported ostype");
44 let png_path = icns_path.with_extension(format!("{}.png", ostype));
45 (icon_type, png_path)
46 } else {
47 // If no OSType is specified, extract the highest-resolution icon.
48 let &icon_type = family
49 .available_icons()
50 .iter()
51 .max_by_key(|icon_type| icon_type.pixel_width() * icon_type.pixel_height())
52 .expect("ICNS file contains no icons");
53 let png_path = icns_path.with_extension("png");
54 (icon_type, png_path)
55 };
56 let image = family
57 .get_icon_with_type(icon_type)
58 .expect("ICNS file does not contain that icon type");
59 let png_file = BufWriter::new(File::create(png_path).expect("failed to create PNG file"));
60 image.write_png(png_file).expect("failed to write PNG file");
61}
Sourcepub fn read<R: Read>(reader: R) -> Result<IconFamily>
pub fn read<R: Read>(reader: R) -> Result<IconFamily>
Reads an icon family from an ICNS file.
Examples found in repository?
8fn main() {
9 if env::args().count() != 2 {
10 println!("Usage: readicns <path>");
11 return;
12 }
13 let path = env::args().nth(1).unwrap();
14 let file = File::open(path).expect("failed to open file");
15 let buffered = BufReader::new(file);
16 let family = IconFamily::read(buffered).expect("failed to read ICNS file");
17 println!("ICNS file contains {} element(s).", family.elements.len());
18 for (index, element) in family.elements.iter().enumerate() {
19 println!(
20 "Element {}: {} ({} byte payload)",
21 index,
22 element.ostype,
23 element.data.len()
24 );
25 }
26}
More examples
31fn main() {
32 let num_args = env::args().count();
33 if num_args < 2 || num_args > 3 {
34 println!("Usage: icns2png <path> [<ostype>]");
35 return;
36 }
37 let icns_path = env::args().nth(1).unwrap();
38 let icns_path = Path::new(&icns_path);
39 let icns_file = BufReader::new(File::open(icns_path).expect("failed to open ICNS file"));
40 let family = IconFamily::read(icns_file).expect("failed to read ICNS file");
41 let (icon_type, png_path) = if num_args == 3 {
42 let ostype = OSType::from_str(&env::args().nth(2).unwrap()).unwrap();
43 let icon_type = IconType::from_ostype(ostype).expect("unsupported ostype");
44 let png_path = icns_path.with_extension(format!("{}.png", ostype));
45 (icon_type, png_path)
46 } else {
47 // If no OSType is specified, extract the highest-resolution icon.
48 let &icon_type = family
49 .available_icons()
50 .iter()
51 .max_by_key(|icon_type| icon_type.pixel_width() * icon_type.pixel_height())
52 .expect("ICNS file contains no icons");
53 let png_path = icns_path.with_extension("png");
54 (icon_type, png_path)
55 };
56 let image = family
57 .get_icon_with_type(icon_type)
58 .expect("ICNS file does not contain that icon type");
59 let png_file = BufWriter::new(File::create(png_path).expect("failed to create PNG file"));
60 image.write_png(png_file).expect("failed to write PNG file");
61}
Sourcepub fn write<W: Write>(&self, writer: W) -> Result<()>
pub fn write<W: Write>(&self, writer: W) -> Result<()>
Writes the icon family to an ICNS file.
Examples found in repository?
34fn main() {
35 let num_args = env::args().count();
36 if num_args < 2 || num_args > 3 {
37 println!("Usage: png2icns <path> [<ostype>]");
38 return;
39 }
40 let png_path = env::args().nth(1).unwrap();
41 let png_path = Path::new(&png_path);
42 let png_file = BufReader::new(File::open(png_path).expect("failed to open PNG file"));
43 let image = Image::read_png(png_file).expect("failed to read PNG file");
44 let mut family = IconFamily::new();
45 let icns_path = if num_args == 3 {
46 let ostype = OSType::from_str(&env::args().nth(2).unwrap()).unwrap();
47 let icon_type = IconType::from_ostype(ostype).expect("unsupported ostype");
48 family
49 .add_icon_with_type(&image, icon_type)
50 .expect("failed to encode image");
51 png_path.with_extension(format!("{}.icns", ostype))
52 } else {
53 family.add_icon(&image).expect("failed to encode image");
54 png_path.with_extension("icns")
55 };
56 let icns_file = BufWriter::new(File::create(icns_path).expect("failed to create ICNS file"));
57 family.write(icns_file).expect("failed to write ICNS file");
58}
Sourcepub fn total_length(&self) -> u32
pub fn total_length(&self) -> u32
Returns the encoded length of the file, in bytes, including the length of the header.