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: Read>(input: R) -> Result<Image>
pub fn read_png<R: Read>(input: R) -> Result<Image>
Reads an image from a PNG file.
Examples found in repository?
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
fn main() {
let num_args = env::args().count();
if num_args < 2 || num_args > 3 {
println!("Usage: png2icns <path> [<ostype>]");
return;
}
let png_path = env::args().nth(1).unwrap();
let png_path = Path::new(&png_path);
let png_file = BufReader::new(File::open(png_path).expect("failed to open PNG file"));
let image = Image::read_png(png_file).expect("failed to read PNG file");
let mut family = IconFamily::new();
let icns_path = if num_args == 3 {
let ostype = OSType::from_str(&env::args().nth(2).unwrap()).unwrap();
let icon_type = IconType::from_ostype(ostype).expect("unsupported ostype");
family
.add_icon_with_type(&image, icon_type)
.expect("failed to encode image");
png_path.with_extension(format!("{}.icns", ostype))
} else {
family.add_icon(&image).expect("failed to encode image");
png_path.with_extension("icns")
};
let icns_file = BufWriter::new(File::create(icns_path).expect("failed to create ICNS file"));
family.write(icns_file).expect("failed to write ICNS file");
}
Sourcepub fn decode_from_png<R: Read>(input: R) -> Result<Image>
pub fn decode_from_png<R: Read>(input: R) -> Result<Image>
Internal function to decode a PNG.
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?
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
fn main() {
let num_args = env::args().count();
if num_args < 2 || num_args > 3 {
println!("Usage: icns2png <path> [<ostype>]");
return;
}
let icns_path = env::args().nth(1).unwrap();
let icns_path = Path::new(&icns_path);
let icns_file = BufReader::new(File::open(icns_path).expect("failed to open ICNS file"));
let family = IconFamily::read(icns_file).expect("failed to read ICNS file");
let (icon_type, png_path) = if num_args == 3 {
let ostype = OSType::from_str(&env::args().nth(2).unwrap()).unwrap();
let icon_type = IconType::from_ostype(ostype).expect("unsupported ostype");
let png_path = icns_path.with_extension(format!("{}.png", ostype));
(icon_type, png_path)
} else {
// If no OSType is specified, extract the highest-resolution icon.
let &icon_type = family
.available_icons()
.iter()
.max_by_key(|icon_type| icon_type.pixel_width() * icon_type.pixel_height())
.expect("ICNS file contains no icons");
let png_path = icns_path.with_extension("png");
(icon_type, png_path)
};
let image = family
.get_icon_with_type(icon_type)
.expect("ICNS file does not contain that icon type");
let png_file = BufWriter::new(File::create(png_path).expect("failed to create PNG file"));
image.write_png(png_file).expect("failed to write PNG file");
}
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.
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()
.