Class: JXL::Bit::Reader
- Inherits:
-
Object
- Object
- JXL::Bit::Reader
- Defined in:
- lib/jxl/bit/reader.rb
Constant Summary collapse
- MAX_BITS =
56
Instance Attribute Summary collapse
-
#bits_read ⇒ Object
readonly
Returns the value of attribute bits_read.
Instance Method Summary collapse
- #byte_position ⇒ Object
- #close! ⇒ Object
-
#initialize(data, position = 0) ⇒ Reader
constructor
A new instance of Reader.
- #overrun? ⇒ Boolean
- #peek(bits) ⇒ Object
- #read(bits) ⇒ Object
- #skip(bits) ⇒ Object
- #zero_pad_to_byte ⇒ Object
Constructor Details
#initialize(data, position = 0) ⇒ Reader
Returns a new instance of Reader.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/jxl/bit/reader.rb', line 10 def initialize(data, position = 0) unless position.is_a?(Integer) && position.between?(0, data.bytesize) raise ArgumentError, "invalid byte position" end @data = data.b @byte_position = position @buffer = 0 @buffered_bits = 0 @bits_read = 0 @overrun = false end |
Instance Attribute Details
#bits_read ⇒ Object (readonly)
Returns the value of attribute bits_read.
8 9 10 |
# File 'lib/jxl/bit/reader.rb', line 8 def bits_read @bits_read end |
Instance Method Details
#byte_position ⇒ Object
56 |
# File 'lib/jxl/bit/reader.rb', line 56 def byte_position = @bits_read.div(8) |
#close! ⇒ Object
59 60 61 62 63 |
# File 'lib/jxl/bit/reader.rb', line 59 def close! raise TruncatedError, "bitstream ended early" if overrun? nil end |
#overrun? ⇒ Boolean
57 |
# File 'lib/jxl/bit/reader.rb', line 57 def overrun? = @overrun |
#peek(bits) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/jxl/bit/reader.rb', line 35 def peek(bits) raise ArgumentError, "bit count must be between 0 and #{MAX_BITS}" unless bits.between?(0, MAX_BITS) refill while @buffered_bits < bits @buffer & ((1 << bits) - 1) end |
#read(bits) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/jxl/bit/reader.rb', line 23 def read(bits) raise ArgumentError, "bit count must be between 0 and #{MAX_BITS}" unless bits.between?(0, MAX_BITS) refill while @buffered_bits < bits value = @buffer & ((1 << bits) - 1) @buffer >>= bits @buffered_bits -= bits @bits_read += bits Trace.br(bits, value, @bits_read) value end |
#skip(bits) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/jxl/bit/reader.rb', line 42 def skip(bits) while bits > MAX_BITS read(MAX_BITS) bits -= MAX_BITS end read(bits) nil end |
#zero_pad_to_byte ⇒ Object
51 52 53 54 |
# File 'lib/jxl/bit/reader.rb', line 51 def zero_pad_to_byte padding = (-@bits_read) & 7 raise CorruptError, "non-zero padding bits" unless read(padding).zero? end |