Skip to main content

BitWriter

Struct BitWriter 

Source
pub struct BitWriter { /* private fields */ }
Expand description

Writes bits into a growable byte buffer.

Bits are written in little-endian order, with the least significant bit of each value written first. This matches the JXL bitstream format.

§Example

use jxl_encoder::bit_writer::BitWriter;

let mut writer = BitWriter::new();
writer.write(8, 0x12).unwrap();
writer.write(4, 0x3).unwrap();
writer.write(4, 0x4).unwrap();
writer.zero_pad_to_byte();

let bytes = writer.finish();
assert_eq!(bytes, vec![0x12, 0x43]);

Implementations§

Source§

impl BitWriter

Source

pub fn new() -> Self

Creates a new empty BitWriter.

Source

pub fn with_capacity(capacity_bytes: usize) -> Self

Creates a new BitWriter with pre-allocated capacity.

§Arguments
  • capacity_bytes - Initial capacity in bytes.
Source

pub fn bits_written(&self) -> usize

Returns the total number of bits written.

Source

pub fn bytes_written(&self) -> usize

Returns the number of bytes written (rounded up).

Source

pub fn is_byte_aligned(&self) -> bool

Returns true if the writer is aligned to a byte boundary.

Source

pub fn bits_to_byte_boundary(&self) -> usize

Returns the number of bits needed to reach the next byte boundary.

Source

pub fn write(&mut self, n_bits: usize, bits: u64) -> Result<()>

Writes up to 56 bits to the buffer.

Bits are written in little-endian order, least-significant-bit first. The value must fit in n_bits bits.

§Arguments
  • n_bits - Number of bits to write (0-56).
  • bits - The value to write. Only the lower n_bits are used.
§Errors

Returns an error if n_bits > 56 or if allocation fails.

Source

pub fn zero_pad_to_byte(&mut self)

Writes zeros to pad to the next byte boundary.

If already byte-aligned, this is a no-op.

Source

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

Appends byte-aligned data from a slice.

The writer must be byte-aligned before calling this method.

§Errors

Returns an error if the writer is not byte-aligned.

Source

pub fn append_byte_aligned(&mut self, other: &BitWriter) -> Result<()>

Appends another BitWriter’s contents.

Both writers must be byte-aligned.

§Errors

Returns an error if either writer is not byte-aligned.

Source

pub fn append_unaligned(&mut self, other: &BitWriter) -> Result<()>

Appends another BitWriter’s contents, allowing unaligned data.

This is slower than append_byte_aligned but works with any alignment.

Source

pub fn as_bytes(&self) -> &[u8]

Returns a view of the written bytes.

The writer must be byte-aligned.

§Panics

Panics if the writer is not byte-aligned.

Source

pub fn peek_bytes(&self) -> &[u8]

Returns a view of the internal storage for debugging.

Unlike as_bytes(), this does not require byte alignment and returns the raw storage including any partial bytes. Useful for debugging bit-level encoding issues.

Source

pub fn finish(self) -> Vec<u8>

Consumes the writer and returns the written bytes.

The writer must be byte-aligned.

§Panics

Panics if the writer is not byte-aligned.

Source

pub fn finish_with_padding(self) -> Vec<u8>

Consumes the writer and returns the written bytes, padding if necessary.

Unlike finish, this will zero-pad to byte alignment if needed.

Source§

impl BitWriter

Source

pub fn write_bit(&mut self, bit: bool) -> Result<()>

Writes a single bit (0 or 1).

Source

pub fn write_u8(&mut self, value: u8) -> Result<()>

Writes an 8-bit unsigned integer.

Source

pub fn write_u16(&mut self, value: u16) -> Result<()>

Writes a 16-bit unsigned integer in little-endian order.

Source

pub fn write_u32(&mut self, value: u32) -> Result<()>

Writes a 32-bit unsigned integer in little-endian order.

Source

pub fn write_u32_coder( &mut self, value: u32, d0: u32, d1: u32, d2: u32, d3: u32, u_bits: usize, ) -> Result<()>

Writes a U32 value using the JXL variable-length encoding.

The encoding is selector-based:

  • 0: value is d0
  • 1: value is d1
  • 2: value is d2
  • 3: u_bits bits follow, value is d3 + read_bits
§Arguments
  • value - The value to encode.
  • d0, d1, d2, d3 - Direct values for selectors 0-2 and offset for selector 3.
  • u_bits - Number of bits for the variable portion (selector 3).
Source

pub fn write_enum_default(&mut self, value: u32) -> Result<()>

Writes an enum value using the jxl-rs default u2S encoding. This uses u2S(0, 1, Bits(4)+2, Bits(6)+18):

  • selector 0 → value 0
  • selector 1 → value 1
  • selector 2 → 2 + Bits(4) = values 2-17
  • selector 3 → 18 + Bits(6) = values 18-81
Source

pub fn write_u64_coder(&mut self, value: u64) -> Result<()>

Writes a U64 value using the JXL variable-length encoding.

The encoding depends on the selector:

  • 0: value is 0
  • 1: 4 bits follow, value is 1 + read_bits (1-16)
  • 2: 8 bits follow, value is 17 + read_bits (17-272)
  • 3: 12 bits follow, then:
    • If high bit is 0: value is 273 + low 12 bits (273-4368)
    • If high bit is 1: 32 more bits follow, value is combined

Trait Implementations§

Source§

impl Clone for BitWriter

Source§

fn clone(&self) -> BitWriter

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BitWriter

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for BitWriter

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.