Encoder

Struct Encoder 

Source
pub struct Encoder<W, F> { /* private fields */ }
Expand description

The main object, which does all of the encoding work.

Implementations§

Source§

impl<W: Write, F> Encoder<W, F>

Source

pub fn finish(&mut self) -> Result<()>

Finish encoding an image, writing any delayed data.

Examples found in repository?
examples/flag.rs (line 37)
8fn main() {
9    let mut enc = Options::smallest(480, 360)
10        .build(File::create("flag.png").unwrap())
11        .unwrap();
12
13    meta::text(
14        &mut enc,
15        meta::Keyword::Author,
16        "Ram Kaniyur",
17    ).unwrap();
18
19    let mut row = [255; 480 * 4];
20
21    for x in 0..480 {
22        let i = x * 4;
23
24        if x < 160 {
25            row[i] = 0;
26        } else if x < 320 {
27            row[i + 1] = 0;
28        } else {
29            row[i + 2] = 0;
30        }
31    }
32
33    for _y in 0..360 {
34        enc.write(&row).unwrap();
35    }
36
37    enc.finish().unwrap();
38}
Source

pub fn chunk(&mut self, kind: &[u8; 4], data: &[u8]) -> Result<()>

Write a chunk with the given type and data.

Source

pub fn writer(&mut self) -> &mut W

Get access to the writer that this was built with.

Be careful, because you can easily corrupt the image with this method.

Source§

impl<W: Write, F: Filter> Encoder<W, F>

Source

pub fn new(opts: &Options, sink: W) -> Result<Self>

Make a new encoder, which writes to a given sink, with a custom filter.

This method also immediately writes the PNG headers to the sink.

Source

pub fn write(&mut self, rows: &[u8]) -> Result<()>

Feed zero or more rows to the encoder.

Rows are specified in top-to-bottom and left-to-right order.

§Panics

This method panics if you try to write data that doesn’t fit into a whole number of rows.

Examples found in repository?
examples/flag.rs (line 34)
8fn main() {
9    let mut enc = Options::smallest(480, 360)
10        .build(File::create("flag.png").unwrap())
11        .unwrap();
12
13    meta::text(
14        &mut enc,
15        meta::Keyword::Author,
16        "Ram Kaniyur",
17    ).unwrap();
18
19    let mut row = [255; 480 * 4];
20
21    for x in 0..480 {
22        let i = x * 4;
23
24        if x < 160 {
25            row[i] = 0;
26        } else if x < 320 {
27            row[i + 1] = 0;
28        } else {
29            row[i + 2] = 0;
30        }
31    }
32
33    for _y in 0..360 {
34        enc.write(&row).unwrap();
35    }
36
37    enc.finish().unwrap();
38}
Source

pub fn reset(&mut self, opts: &Options) -> Result<()>

Reset the encoder to encode another image with the given options.

There’s a good chance that, before calling this method, you’ll want to call writer() to get a mutable reference to the writer, and swap that out with a different writer. This method also writes the headers of the new image to the sink.

§Warning

This currently doesn’t change the compression level!

Auto Trait Implementations§

§

impl<W, F> Freeze for Encoder<W, F>
where F: Freeze, W: Freeze,

§

impl<W, F> RefUnwindSafe for Encoder<W, F>

§

impl<W, F> Send for Encoder<W, F>
where F: Send, W: Send,

§

impl<W, F> Sync for Encoder<W, F>
where F: Sync, W: Sync,

§

impl<W, F> Unpin for Encoder<W, F>
where F: Unpin, W: Unpin,

§

impl<W, F> UnwindSafe for Encoder<W, F>
where F: UnwindSafe, W: UnwindSafe,

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, 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.