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
impl BitWriter
Sourcepub fn with_capacity(capacity_bytes: usize) -> Self
pub fn with_capacity(capacity_bytes: usize) -> Self
Creates a new BitWriter with pre-allocated capacity.
§Arguments
capacity_bytes- Initial capacity in bytes.
Sourcepub fn bits_written(&self) -> usize
pub fn bits_written(&self) -> usize
Returns the total number of bits written.
Sourcepub fn bytes_written(&self) -> usize
pub fn bytes_written(&self) -> usize
Returns the number of bytes written (rounded up).
Sourcepub fn is_byte_aligned(&self) -> bool
pub fn is_byte_aligned(&self) -> bool
Returns true if the writer is aligned to a byte boundary.
Sourcepub fn bits_to_byte_boundary(&self) -> usize
pub fn bits_to_byte_boundary(&self) -> usize
Returns the number of bits needed to reach the next byte boundary.
Sourcepub fn write(&mut self, n_bits: usize, bits: u64) -> Result<()>
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 lowern_bitsare used.
§Errors
Returns an error if n_bits > 56 or if allocation fails.
Sourcepub fn zero_pad_to_byte(&mut self)
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.
Sourcepub fn append_bytes(&mut self, data: &[u8]) -> Result<()>
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.
Sourcepub fn append_byte_aligned(&mut self, other: &BitWriter) -> Result<()>
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.
Sourcepub fn append_unaligned(&mut self, other: &BitWriter) -> Result<()>
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.
Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
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.
Sourcepub fn peek_bytes(&self) -> &[u8] ⓘ
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.
Sourcepub fn finish(self) -> Vec<u8> ⓘ
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.
Sourcepub fn finish_with_padding(self) -> Vec<u8> ⓘ
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
impl BitWriter
Sourcepub fn write_u16(&mut self, value: u16) -> Result<()>
pub fn write_u16(&mut self, value: u16) -> Result<()>
Writes a 16-bit unsigned integer in little-endian order.
Sourcepub fn write_u32(&mut self, value: u32) -> Result<()>
pub fn write_u32(&mut self, value: u32) -> Result<()>
Writes a 32-bit unsigned integer in little-endian order.
Sourcepub fn write_u32_coder(
&mut self,
value: u32,
d0: u32,
d1: u32,
d2: u32,
d3: u32,
u_bits: usize,
) -> Result<()>
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_bitsbits follow, value isd3 + 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).
Sourcepub fn write_enum_default(&mut self, value: u32) -> Result<()>
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
Sourcepub fn write_u64_coder(&mut self, value: u64) -> Result<()>
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