tauri_icns

Struct Image

Source
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

Source

pub fn read_png<R: Read>(input: R) -> Result<Image>

Reads an image from a PNG file.

Examples found in repository?
examples/png2icns.rs (line 43)
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");
}
Source

pub fn decode_from_png<R: Read>(input: R) -> Result<Image>

Internal function to decode a PNG.

Source

pub fn write_png<W: Write>(&self, output: W) -> Result<()>

Writes the image to a PNG file.

Examples found in repository?
examples/icns2png.rs (line 60)
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

Source

pub fn new(format: PixelFormat, width: u32, height: u32) -> Image

Creates a new image with all pixel data set to zero.

Source

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.

Source

pub fn pixel_format(&self) -> PixelFormat

Returns the format in which this image’s pixel data is stored.

Source

pub fn width(&self) -> u32

Returns the width of the image, in pixels.

Source

pub fn height(&self) -> u32

Returns the height of the image, in pixels.

Source

pub fn data(&self) -> &[u8]

Returns a reference to the image’s pixel data.

Source

pub fn data_mut(&mut self) -> &mut [u8]

Returns a mutable reference to the image’s pixel data.

Source

pub fn into_data(self) -> Box<[u8]>

Consumes the image, returning the pixel data without cloning it.

Source

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().

Trait Implementations§

Source§

impl Clone for Image

Source§

fn clone(&self) -> Image

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for Image

§

impl RefUnwindSafe for Image

§

impl Send for Image

§

impl Sync for Image

§

impl Unpin for Image

§

impl UnwindSafe for Image

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.