webp-rust 0.2.1

Pure Rust implementation of a WebP encoder and decoder
Documentation
//! LSB-first bit writer used by the lossless `VP8L` encoder.

use bin_rs::io::write_byte;

use crate::encoder::EncoderError;

#[derive(Debug, Default, Clone)]
pub(crate) struct BitWriter {
    bytes: Vec<u8>,
    bit_pos: usize,
}

impl BitWriter {
    /// Appends `num_bits` least-significant bits in LSB-first order.
    pub(crate) fn put_bits(&mut self, value: u32, num_bits: usize) -> Result<(), EncoderError> {
        if num_bits > 32 {
            return Err(EncoderError::Bitstream("bit write is too wide"));
        }
        for bit_index in 0..num_bits {
            let byte_index = self.bit_pos >> 3;
            if byte_index == self.bytes.len() {
                write_byte(0, &mut self.bytes);
            }
            let bit = ((value >> bit_index) & 1) as u8;
            self.bytes[byte_index] |= bit << (self.bit_pos & 7);
            self.bit_pos += 1;
        }
        Ok(())
    }

    /// Finishes the writer and returns the accumulated bytes.
    pub(crate) fn into_bytes(self) -> Vec<u8> {
        self.bytes
    }
}