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>
impl<W: Write, F> Encoder<W, F>
Sourcepub fn finish(&mut self) -> Result<()>
pub fn finish(&mut self) -> Result<()>
Finish encoding an image, writing any delayed data.
Examples found in repository?
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§impl<W: Write, F: Filter> Encoder<W, F>
impl<W: Write, F: Filter> Encoder<W, F>
Sourcepub fn new(opts: &Options, sink: W) -> Result<Self>
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.
Sourcepub fn write(&mut self, rows: &[u8]) -> Result<()>
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?
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}Sourcepub fn reset(&mut self, opts: &Options) -> Result<()>
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!