Class: Cabriolet::Decompressors::Quantum::MSBBitstream
- Inherits:
-
Object
- Object
- Cabriolet::Decompressors::Quantum::MSBBitstream
- Defined in:
- lib/cabriolet/decompressors/quantum.rb
Overview
MSB-first bitstream for Quantum (reads 16-bit words MSB first)
Instance Attribute Summary collapse
-
#bits_left ⇒ Object
readonly
Returns the value of attribute bits_left.
Instance Method Summary collapse
- #byte_align ⇒ Object
-
#initialize(io_system, handle, buffer_size) ⇒ MSBBitstream
constructor
A new instance of MSBBitstream.
-
#read_bits(num_bits) ⇒ Object
Read bits MSB first (matching Quantum’s READ_BITS macro).
- #read_byte ⇒ Object
Constructor Details
#initialize(io_system, handle, buffer_size) ⇒ MSBBitstream
Returns a new instance of MSBBitstream.
188 189 190 191 192 193 194 195 196 |
# File 'lib/cabriolet/decompressors/quantum.rb', line 188 def initialize(io_system, handle, buffer_size) @io_system = io_system @handle = handle @buffer_size = buffer_size @buffer = "" @buffer_pos = 0 @bit_buffer = 0 @bits_left = 0 end |
Instance Attribute Details
#bits_left ⇒ Object (readonly)
Returns the value of attribute bits_left.
186 187 188 |
# File 'lib/cabriolet/decompressors/quantum.rb', line 186 def bits_left @bits_left end |
Instance Method Details
#byte_align ⇒ Object
226 227 228 |
# File 'lib/cabriolet/decompressors/quantum.rb', line 226 def byte_align @bits_left -= (@bits_left % 8) end |
#read_bits(num_bits) ⇒ Object
Read bits MSB first (matching Quantum’s READ_BITS macro)
199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/cabriolet/decompressors/quantum.rb', line 199 def read_bits(num_bits) while @bits_left < num_bits # Read 16-bit word MSB first b0 = read_byte b1 = read_byte word = (b0 << 8) | b1 @bit_buffer = (@bit_buffer << 16) | word @bits_left += 16 end # Extract bits from MSB side @bits_left -= num_bits (@bit_buffer >> @bits_left) & ((1 << num_bits) - 1) end |
#read_byte ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/cabriolet/decompressors/quantum.rb', line 214 def read_byte if @buffer_pos >= @buffer.bytesize @buffer = @io_system.read(@handle, @buffer_size) @buffer_pos = 0 return 0 if @buffer.empty? end byte = @buffer.getbyte(@buffer_pos) @buffer_pos += 1 byte end |