Skip to main content

IncrementalDecoder

Struct IncrementalDecoder 

Source
pub struct IncrementalDecoder<'a> { /* private fields */ }
Expand description

A decoder that takes a still WebP file in pieces and lends the rows decoded so far (C: WebPIDecoder).

It borrows the Config and, with IncrementalDecoder::with_output, the caller’s memory, for its lifetime. IncrementalDecoder::append copies each piece; IncrementalDecoder::update takes the whole data so far each time, for a caller who keeps it. The two cannot be mixed. The decoder is neither Send nor Sync.

§Examples

use webp::api::decode::{ColorMode, Config, IncrementalDecoder, Status};

let config = Config::new(ColorMode::Rgb);
let mut decoder = IncrementalDecoder::new(&config)?;
let mut status = Status::NeedsMoreData;
for piece in file.chunks(100) {
    status = decoder.append(piece)?;
    // The rows decoded so far, lent until the next call.
    if let Some(rows) = decoder.rows() {
        assert_eq!(rows.len(), decoder.decoded_rows());
    }
}
assert_eq!(status, Status::Complete);
let image = decoder.into_image()?;
assert_eq!((image.width(), image.height()), (64, 48));

Implementations§

Source§

impl<'a> IncrementalDecoder<'a>

Source

pub fn new(config: &'a Config) -> Result<Self, DecodeError>

An incremental decoding into memory that libwebp allocates when the header arrives (C: WebPINewDecoder with a buffer in the colorspace of config, and config’s options, as WebPIDecode sets them).

§Errors

The errors of Config::validate.

§Examples
use webp::api::decode::{ColorMode, Config, IncrementalDecoder};

let config = Config::new(ColorMode::Yuva).flip(true);
let decoder = IncrementalDecoder::new(&config)?;
assert_eq!(decoder.decoded_rows(), 0);
Source

pub fn with_output( config: &'a Config, output: Output<'a>, ) -> Result<Self, DecodeError>

An incremental decoding into the caller’s memory (C: WebPINewRGB or WebPINewYUVA, and config’s options). The image’s size is not known yet, so the API checks only the shape of output (rows for the RGB modes, planes for the YUV modes, an alpha plane exactly for ColorMode::Yuva), strides of 1 to i32::MAX bytes and non-empty slices; libwebp checks the rest when the header arrives and returns DecodeError::InvalidParam for memory that does not fit. Check before with Config::output_size and the rules of Output.

§Errors

The errors of Config::validate, DecodeError::OutputMismatch, DecodeError::InvalidStride and DecodeError::BufferTooShort.

§Examples
use webp::api::decode::{ColorMode, Config, IncrementalDecoder, Output, Status};

let config = Config::new(ColorMode::Rgb);
let mut rows = vec![0_u8; 2 * 9];
{
    let output = Output::Rows { data: &mut rows, stride: 9 };
    let mut decoder = IncrementalDecoder::with_output(&config, output)?;
    assert_eq!(decoder.update(&file)?, Status::Complete);
}
assert_eq!(rows, [50_u8, 60, 70].repeat(6));
Source

pub fn append(&mut self, data: &[u8]) -> Result<Status, DecodeError>

Copies the next piece of the file and decodes as far as it reaches (C: WebPIAppend). An empty piece before any data is Status::NeedsMoreData: libwebp asserts against that call, so the API answers it without calling the decoder.

§Errors

DecodeError::BitstreamError for an invalid file (and for every call after a failure), DecodeError::UnsupportedFeature for an animation, DecodeError::InvalidParam for caller memory or a crop that does not fit the image, or after IncrementalDecoder::update, and DecodeError::OutOfMemory.

§Examples
use webp::api::decode::{ColorMode, Config, DecodeError, IncrementalDecoder, Status};

let config = Config::new(ColorMode::Rgb);
let mut decoder = IncrementalDecoder::new(&config)?;
assert_eq!(decoder.append(b"RIFF"), Ok(Status::NeedsMoreData));
assert_eq!(decoder.append(b"\0\0\0\0JPEG"), Err(DecodeError::BitstreamError));
Source

pub fn update(&mut self, data: &[u8]) -> Result<Status, DecodeError>

Decodes as far as data, the whole file so far, reaches, without copying it (C: WebPIUpdate). Each call passes the previous data and more; the data may move between calls.

§Errors

The errors of IncrementalDecoder::append; DecodeError::InvalidParam also for data shorter than before, or after append.

§Examples
use webp::api::decode::{ColorMode, Config, IncrementalDecoder, Status};

let config = Config::new(ColorMode::Rgba);
let mut decoder = IncrementalDecoder::new(&config)?;
let mut received = Vec::new();
for piece in file.chunks(7) {
    received.extend_from_slice(piece);
    let status = decoder.update(&received)?;
    assert_eq!(status == Status::Complete, received.len() == file.len());
}
Source

pub fn output_size(&self) -> Option<(u32, u32)>

The width and height of the output, once libwebp has set the output up after the header (C: the width and height of the output buffer that WebPIDecodedArea returns, not its height out-parameter last_y, which decoded_rows reports); None before, including the moment after a lossless header when the decoded area exists with a width of 0.

§Examples
use webp::api::decode::{ColorMode, Config, IncrementalDecoder};

let config = Config::new(ColorMode::Rgb).scale(3, 2);
let mut decoder = IncrementalDecoder::new(&config)?;
assert_eq!(decoder.output_size(), None);
decoder.append(&file)?;
assert_eq!(decoder.output_size(), Some((3, 2)));
Source

pub fn decoded_rows(&self) -> u32

The number of output rows decoded so far, from the top of the unflipped output (C: last_y).

§Examples
use webp::api::decode::{ColorMode, Config, IncrementalDecoder};

let config = Config::new(ColorMode::Rgba);
let mut decoder = IncrementalDecoder::new(&config)?;
decoder.append(&file)?;
assert_eq!(decoder.decoded_rows(), 3);
Source

pub fn rows(&self) -> Option<Rows<'_>>

The rows of an RGB mode decoded so far, lent until the next call that takes &mut self (C: WebPIDecGetRGB); None for a YUV mode or before the output exists. Row 0 is the first decoded row: while a flipped decoding runs, that is the bottom row of the output and the stride is negative; once complete, the stride is positive and row 0 is the top.

§Examples
use webp::api::decode::{ColorMode, Config, IncrementalDecoder};

let config = Config::new(ColorMode::Rgba);
let mut decoder = IncrementalDecoder::new(&config)?;
assert!(decoder.rows().is_none());
decoder.append(&file)?;
assert_eq!(decoder.rows().unwrap().row(1), Some(&[4_u8, 3, 2, 1, 4, 3, 2, 1][..]));
Source

pub fn planes(&self) -> Option<YuvaRows<'_>>

The planes of a YUV mode decoded so far, lent like IncrementalDecoder::rows (C: WebPIDecGetYUVA); None for an RGB mode or before the output exists. Luma and alpha have IncrementalDecoder::decoded_rows rows, U and V half as many, rounded up; with a scale, libwebp writes chroma and alpha rows later than luma rows, so those are final only once the decoding completes.

§Examples
use webp::api::decode::{ColorMode, Config, IncrementalDecoder};

let config = Config::new(ColorMode::Yuv);
let mut decoder = IncrementalDecoder::new(&config)?;
decoder.append(&file)?;
let planes = decoder.planes().unwrap();
assert_eq!((planes.y.len(), planes.u.len()), (3, 2));
assert_eq!(planes.y.row(0), Some(&[16_u8, 16, 16][..])); // black, limited range
Source

pub fn into_image(self) -> Result<Image, DecodeError>

The decoded image, in the memory that libwebp allocated for a decoder of IncrementalDecoder::new, once the decoding has completed.

§Errors

DecodeError::Suspended before the decoding has completed, and DecodeError::OutputMismatch for a decoder of IncrementalDecoder::with_output, whose samples are in the caller’s memory.

§Examples
use webp::api::decode::{ColorMode, Config, DecodeError, IncrementalDecoder};

let config = Config::new(ColorMode::Rgba);
let mut decoder = IncrementalDecoder::new(&config)?;
decoder.append(&file[..file.len() / 2])?;
assert_eq!(decoder.into_image().unwrap_err(), DecodeError::Suspended);

Trait Implementations§

Source§

impl Debug for IncrementalDecoder<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for IncrementalDecoder<'a>

§

impl<'a> !Send for IncrementalDecoder<'a>

§

impl<'a> !Sync for IncrementalDecoder<'a>

§

impl<'a> !UnwindSafe for IncrementalDecoder<'a>

§

impl<'a> Forget for IncrementalDecoder<'a>

§

impl<'a> Freeze for IncrementalDecoder<'a>

§

impl<'a> Move for IncrementalDecoder<'a>

§

impl<'a> Unpin for IncrementalDecoder<'a>

§

impl<'a> UnsafeUnpin for IncrementalDecoder<'a>

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.