Skip to main content

RangeDecoder

Struct RangeDecoder 

Source
pub struct RangeDecoder<R: Read> { /* private fields */ }
Expand description

Streaming range‐decoder for arithmetic coding.

The decoder maintains three internal registers:

  • low: the low end of the current coding interval
  • code: the buffered input bits read from the stream
  • range: the size of the current coding interval

On each symbol decode, you first call [get_freq] to map the current code to a frequency within the total frequency range, then call [decode] to narrow the interval and consume bits as needed.

§Example

use std::fs::File;
use ppmd_core::{RangeDecoder, PpmModel, PpmResult};

fn decode_stream() -> PpmResult<()> {
    let file = File::open("data.ppm")?;
    let mut decoder = RangeDecoder::new(file)?;
    let mut model = PpmModel::new(5)?;
    let mut history = Vec::new();
    let mut out_byte = [0u8; 1];

    // Repeatedly call `decode_symbol` until end of stream
    while model.decode_symbol(&mut decoder, &mut history, &mut out_byte).is_ok() {
        print!("{}", out_byte[0] as char);
    }
    Ok(())
}

Implementations§

Source§

impl<R: Read> RangeDecoder<R>

Source

pub fn new(reader: R) -> PpmResult<Self>

Initialize a new RangeDecoder by reading the first 4 bytes from reader into the internal code register.

The range decoder uses these first 32 bits as its starting buffer. Subsequent calls to [get_freq] and [decode] will consume more bytes from reader as needed to renormalize the interval.

§Errors

Returns PpmError::IoError if reading the initial 4-byte code prefix fails.

Auto Trait Implementations§

§

impl<R> Freeze for RangeDecoder<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for RangeDecoder<R>
where R: RefUnwindSafe,

§

impl<R> Send for RangeDecoder<R>
where R: Send,

§

impl<R> Sync for RangeDecoder<R>
where R: Sync,

§

impl<R> Unpin for RangeDecoder<R>
where R: Unpin,

§

impl<R> UnsafeUnpin for RangeDecoder<R>
where R: UnsafeUnpin,

§

impl<R> UnwindSafe for RangeDecoder<R>
where R: 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.