pub struct Encoder<'e> { /* private fields */ }Expand description
One encoding: a configuration, and optionally a progress hook and
statistics, applied to a Picture with Encoder::encode or
Encoder::encode_to.
§Examples
use webp::api::encode::{Config, Encoder, Picture, WebPAuxStats};
let mut argb = vec![0xff40_8020_u32; 32 * 32];
let mut picture = Picture::from_argb(&mut argb, 32, 32, 32)?;
let config = Config::new().quality(60.0);
let mut stats = WebPAuxStats::default();
let mut reports = Vec::new();
let webp = Encoder::new(&config)
.progress(|percent| {
reports.push(percent);
true
})
.stats(&mut stats)
.encode(&mut picture)?;
assert_eq!(stats.coded_size as usize, webp.len());
assert!(!reports.is_empty());Implementations§
Source§impl<'e> Encoder<'e>
impl<'e> Encoder<'e>
Sourcepub const fn new(config: &'e Config) -> Self
pub const fn new(config: &'e Config) -> Self
An encoding with config, no progress hook and no statistics.
§Examples
use webp::api::encode::{Config, Encoder};
let config = Config::new();
let encoder = Encoder::new(&config);Sourcepub fn progress(self, hook: impl FnMut(i32) -> bool + 'e) -> Self
pub fn progress(self, hook: impl FnMut(i32) -> bool + 'e) -> Self
Reports progress to hook (C: WebPPicture::progress_hook). It
receives libwebp’s percentages, 0 to 100, each time the value changes,
on the calling thread. Returning false stops the encoding with
EncodeError::UserAbort; the writer keeps what it received.
libwebp ignores the answer at two places, the end of a lossy analysis
without segments (C: ResetAllMBInfo) and the statistics pass of lossy
methods 0 to 2 (C: VP8EncLoop drops the result of StatLoop): the
encoding then goes on and can succeed. A hook that cancels should
therefore keep returning false.
§Examples
use webp::api::encode::{Config, EncodeError, Encoder, Picture};
let mut argb = vec![0xff00_0000_u32; 64 * 64];
let mut picture = Picture::from_argb(&mut argb, 64, 64, 64)?;
let config = Config::new();
let result = Encoder::new(&config).progress(|percent| percent < 50).encode(&mut picture);
assert_eq!(result, Err(EncodeError::UserAbort));Sourcepub fn stats(self, stats: &'e mut WebPAuxStats) -> Self
pub fn stats(self, stats: &'e mut WebPAuxStats) -> Self
Fills stats with the encoding’s statistics (C:
WebPPicture::stats). They are reset once the configuration and the
picture pass libwebp’s validation and filled as libwebp fills them, on
failure too.
§Examples
use webp::api::encode::{Config, Encoder, Picture, WebPAuxStats};
let mut argb = vec![0xff80_8080_u32; 16 * 16];
let mut picture = Picture::from_argb(&mut argb, 16, 16, 16)?;
let config = Config::new().quality(90.0);
let mut stats = WebPAuxStats::default();
let webp = Encoder::new(&config).stats(&mut stats).encode(&mut picture)?;
assert_eq!(stats.coded_size as usize, webp.len());
assert!(stats.psnr[3] > 40.0); // Y, U and V together, in dBSourcepub fn encode(self, picture: &mut Picture<'_>) -> Result<Vec<u8>, EncodeError>
pub fn encode(self, picture: &mut Picture<'_>) -> Result<Vec<u8>, EncodeError>
Encodes picture into memory, through libwebp’s memory writer
(C: WebPEncode with WebPMemoryWrite), and returns the WebP file.
The encoder may write into the picture’s planes, as the module
documentation describes, and leaves the planes of its conversions with
the picture. If the progress hook panics, the panic propagates and the
picture is left without planes: a later encoding of it returns
EncodeError::NullParameter.
§Errors
The errors of Config::validate, and the code libwebp records:
EncodeError::BadWrite when the output cannot grow,
EncodeError::UserAbort from the progress hook, and the others of
EncodeError.
§Examples
use webp::api::encode::{Config, Encoder, Picture, PixelLayout, PixelRows, SampleForm};
let rgb = [200_u8, 100, 50].repeat(16 * 16);
let rows = PixelRows { layout: PixelLayout::Rgb, data: &rgb, width: 16, height: 16, stride: 48 };
let mut picture = Picture::import(rows, SampleForm::Yuva)?;
let webp = Encoder::new(&Config::new()).encode(&mut picture)?;
assert_eq!(&webp[..4], b"RIFF");Sourcepub fn encode_to(
self,
picture: &mut Picture<'_>,
writer: impl FnMut(&[u8]) -> bool,
) -> Result<(), EncodeError>
pub fn encode_to( self, picture: &mut Picture<'_>, writer: impl FnMut(&[u8]) -> bool, ) -> Result<(), EncodeError>
Encodes picture through writer (C: WebPEncode with
WebPPicture::writer). The writer receives the WebP file in order, in
chunks that may be empty, on the calling thread; returning false
stops the encoding with EncodeError::BadWrite, and the bytes it
accepted stay with it.
The encoder may write into the picture’s planes, as the module
documentation describes, and leaves the planes of its conversions with
the picture. If the writer or the progress hook panics, the panic
propagates and the picture is left without planes: a later encoding of
it returns EncodeError::NullParameter.
§Errors
The errors of Config::validate, and the code libwebp records:
EncodeError::BadWrite from the writer, EncodeError::UserAbort
from the progress hook, and the others of EncodeError.
§Examples
use webp::api::encode::{Config, EncodeError, Encoder, Picture};
let mut argb = vec![0xff12_3456_u32; 10 * 10];
let mut picture = Picture::from_argb(&mut argb, 10, 10, 10)?;
let config = Config::new();
let mut file = Vec::new();
Encoder::new(&config).encode_to(&mut picture, |chunk| {
file.extend_from_slice(chunk);
true
})?;
assert_eq!(&file[..4], b"RIFF");
let refused = Encoder::new(&config).encode_to(&mut picture, |_| false);
assert_eq!(refused, Err(EncodeError::BadWrite));