pub struct Image { /* private fields */ }Expand description
A decoded icon image.
An Image struct consists of a width, a height, a
PixelFormat, and a data array encoding the image
pixels in that format.
Regardless of format, pixel data for an image is always stored one complete pixel at a time, in row-major order (that is, the top-left pixel comes first, followed by the rest of the top row from left to right; then comes the second row down, again from left to right, and so on until finally the bottom-right pixel comes last).
Implementations§
Source§impl Image
impl Image
Sourcepub fn read_png<R: BufRead + Seek>(input: R) -> Result<Image>
pub fn read_png<R: BufRead + Seek>(input: R) -> Result<Image>
Reads an image from a PNG 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)
43 .expect("failed to open PNG file"));
44 let image = Image::read_png(png_file).expect("failed to read PNG file");
45 let mut family = IconFamily::new();
46 let icns_path = if num_args == 3 {
47 let ostype = OSType::from_str(&env::args().nth(2).unwrap()).unwrap();
48 let icon_type = IconType::from_ostype(ostype)
49 .expect("unsupported ostype");
50 family.add_icon_with_type(&image, icon_type)
51 .expect("failed to encode image");
52 png_path.with_extension(format!("{}.icns", ostype))
53 } else {
54 family.add_icon(&image).expect("failed to encode image");
55 png_path.with_extension("icns")
56 };
57 let icns_file = BufWriter::new(File::create(icns_path)
58 .expect("failed to create ICNS file"));
59 family.write(icns_file).expect("failed to write ICNS file");
60}Sourcepub fn write_png<W: Write>(&self, output: W) -> Result<()>
pub fn write_png<W: Write>(&self, output: W) -> Result<()>
Writes the image to a PNG file.
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>|all]");
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)
40 .expect("failed to open ICNS file"));
41 let family = IconFamily::read(icns_file)
42 .expect("failed to read ICNS file");
43
44 let (icon_type, typed_filename) = if num_args == 3 {
45 let type_name = &env::args().nth(2).unwrap();
46 if type_name == "all" {
47 (None, true)
48 } else {
49 let ostype = OSType::from_str(type_name).unwrap();
50 let icon_type = IconType::from_ostype(ostype)
51 .expect("unsupported ostype");
52 (Some(icon_type), true)
53 }
54 } else {
55 // If no OSType is specified, extract the highest-resolution icon.
56 let &icon_type = family.available_icons()
57 .iter()
58 .max_by_key(|icon_type| {
59 icon_type.pixel_width() * icon_type.pixel_height()
60 })
61 .expect("ICNS file contains no icons");
62 (Some(icon_type), true)
63 };
64
65 if let Some(icon_type) = icon_type {
66 // Extract specific icon
67 let image = family.get_icon_with_type(icon_type)
68 .expect("ICNS file does not contain that icon type");
69 let ext = if typed_filename {
70 format!("{}.png", icon_type.ostype())
71 } else {
72 "png".to_string()
73 };
74 let png_path = icns_path.with_extension(ext);
75
76 let png_file = BufWriter::new(File::create(png_path)
77 .expect("failed to create PNG file"));
78 image.write_png(png_file).expect("failed to write PNG file");
79
80 } else {
81 // Extract all icons (assuming no duplicates)
82 for icon_type in family.available_icons() {
83 let image = family.get_icon_with_type(icon_type)
84 .expect("ICNS file does not contain that icon type");
85 let png_path = icns_path.with_extension(format!("{}.png", icon_type.ostype()));
86 let png_file = BufWriter::new(File::create(png_path)
87 .expect("failed to create PNG file"));
88 image.write_png(png_file).expect("failed to write PNG file");
89 }
90 }
91}Source§impl Image
impl Image
Sourcepub fn new(format: PixelFormat, width: u32, height: u32) -> Image
pub fn new(format: PixelFormat, width: u32, height: u32) -> Image
Creates a new image with all pixel data set to zero.
Panics if the required memory exceeds isize::MAX, and on allocation failure.
Sourcepub fn from_data(
format: PixelFormat,
width: u32,
height: u32,
data: Vec<u8>,
) -> Result<Image>
pub fn from_data( format: PixelFormat, width: u32, height: u32, data: Vec<u8>, ) -> Result<Image>
Creates a new image using the given pixel data. Returns an error if the data array is not the correct length.
Sourcepub fn pixel_format(&self) -> PixelFormat
pub fn pixel_format(&self) -> PixelFormat
Returns the format in which this image’s pixel data is stored.
Sourcepub fn data_mut(&mut self) -> &mut [u8] ⓘ
pub fn data_mut(&mut self) -> &mut [u8] ⓘ
Returns a mutable reference to the image’s pixel data.
Sourcepub fn into_data(self) -> Box<[u8]>
pub fn into_data(self) -> Box<[u8]>
Consumes the image, returning the pixel data without cloning it.
Sourcepub fn convert_to(&self, format: PixelFormat) -> Image
pub fn convert_to(&self, format: PixelFormat) -> Image
Creates a copy of this image by converting to the specified pixel
format. This operation always succeeds, but may lose information (e.g.
converting from RGBA to RGB will silently drop the alpha channel). If
the source image is already in the requested format, this is equivalant
to simply calling clone().