IcnsEncoder

Struct IcnsEncoder 

Source
pub struct IcnsEncoder { /* private fields */ }
Expand description

The main encoder struct Create a new encoder with IcnsEncoder::new()

Implementations§

Source§

impl IcnsEncoder

Source

pub fn new() -> Self

Creates a new IcnsEncoder

Usage:

use icns_rs::{IcnsEncoder, IconFormats};
use image::open;
use std::fs::File;
use std::io::prelude::*;

// Open the image
let image = match open("512x512@2.png") {
    Ok(image) => image,
    Err(e) => {
        println!("Error: {}", e);
        std::process::exit(1);
    }
};

// Create the encoder
let mut encoder = IcnsEncoder::new();

encoder.data(image);
encoder.formats(IconFormats::recommended());

// Encode the image
let data = match encoder.build() {
    Ok(data) => data,
    Err(e) => {
        println!("Error ould not encode image");
        std::process::exit(1);
    }
};

// Write data to file
let mut file = match File::create("example.icns") {
    Ok(file) => file,
    Err(e) => {
        println!("Error: {}", e);
        std::process::exit(1);
    }
};

match file.write_all(&data) {
    Ok(_) => println!("Successfully wrote to file"),
    Err(e) => {
        println!("Error: {}", e);
        std::process::exit(1);
    }
};
Examples found in repository?
examples/encode.rs (line 17)
6fn main() -> std::io::Result<()> {
7    // Open the image
8    let image = match open("example.png") {
9        Ok(image) => image,
10        Err(e) => {
11            println!("Error opening file: {}", e);
12            return Ok(());
13        }
14    };
15
16    // Create the encoder
17    let mut encoder = IcnsEncoder::new();
18
19    encoder.data(image);
20    encoder.formats(IconFormats::recommended());
21
22    // Encode the image
23    let data = match encoder.build() {
24        Ok(data) => data,
25        Err(e) => {
26            println!("Error encoding image: {}", e);
27            return Ok(());
28        }
29    };
30
31    // Write data to file
32    let mut file = File::create("example.icns")?;
33    file.write_all(&data)?;
34
35    Ok(())
36}
Source

pub fn data(&mut self, data: DynamicImage) -> &mut Self

Sets the image data. Encode a png and pass it as a DynamicImage.

Examples found in repository?
examples/encode.rs (line 19)
6fn main() -> std::io::Result<()> {
7    // Open the image
8    let image = match open("example.png") {
9        Ok(image) => image,
10        Err(e) => {
11            println!("Error opening file: {}", e);
12            return Ok(());
13        }
14    };
15
16    // Create the encoder
17    let mut encoder = IcnsEncoder::new();
18
19    encoder.data(image);
20    encoder.formats(IconFormats::recommended());
21
22    // Encode the image
23    let data = match encoder.build() {
24        Ok(data) => data,
25        Err(e) => {
26            println!("Error encoding image: {}", e);
27            return Ok(());
28        }
29    };
30
31    // Write data to file
32    let mut file = File::create("example.icns")?;
33    file.write_all(&data)?;
34
35    Ok(())
36}
Source

pub fn formats(&mut self, formats: Vec<IconFormats>) -> &mut Self

Sets the image formats to be encoded

Examples found in repository?
examples/encode.rs (line 20)
6fn main() -> std::io::Result<()> {
7    // Open the image
8    let image = match open("example.png") {
9        Ok(image) => image,
10        Err(e) => {
11            println!("Error opening file: {}", e);
12            return Ok(());
13        }
14    };
15
16    // Create the encoder
17    let mut encoder = IcnsEncoder::new();
18
19    encoder.data(image);
20    encoder.formats(IconFormats::recommended());
21
22    // Encode the image
23    let data = match encoder.build() {
24        Ok(data) => data,
25        Err(e) => {
26            println!("Error encoding image: {}", e);
27            return Ok(());
28        }
29    };
30
31    // Write data to file
32    let mut file = File::create("example.icns")?;
33    file.write_all(&data)?;
34
35    Ok(())
36}
Source

pub fn build(&self) -> Result<Box<[u8]>, String>

Encodes the image as an ICNS file

Examples found in repository?
examples/encode.rs (line 23)
6fn main() -> std::io::Result<()> {
7    // Open the image
8    let image = match open("example.png") {
9        Ok(image) => image,
10        Err(e) => {
11            println!("Error opening file: {}", e);
12            return Ok(());
13        }
14    };
15
16    // Create the encoder
17    let mut encoder = IcnsEncoder::new();
18
19    encoder.data(image);
20    encoder.formats(IconFormats::recommended());
21
22    // Encode the image
23    let data = match encoder.build() {
24        Ok(data) => data,
25        Err(e) => {
26            println!("Error encoding image: {}", e);
27            return Ok(());
28        }
29    };
30
31    // Write data to file
32    let mut file = File::create("example.icns")?;
33    file.write_all(&data)?;
34
35    Ok(())
36}

Auto Trait Implementations§

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.