Class: PureJPEG::BitWriter
- Inherits:
-
Object
- Object
- PureJPEG::BitWriter
- Defined in:
- lib/pure_jpeg/bit_writer.rb
Instance Method Summary collapse
- #bytes ⇒ Object
-
#flush ⇒ Object
Pad remaining bits with 1s and flush (per JPEG spec).
-
#initialize ⇒ BitWriter
constructor
A new instance of BitWriter.
-
#write_bits(value, num_bits) ⇒ Object
Write ‘num_bits` of `value` (MSB first) into the output stream.
Constructor Details
#initialize ⇒ BitWriter
Returns a new instance of BitWriter.
5 6 7 8 9 |
# File 'lib/pure_jpeg/bit_writer.rb', line 5 def initialize @data = String.new(capacity: 4096, encoding: Encoding::BINARY) @buffer = 0 @bits_in_buffer = 0 end |
Instance Method Details
#bytes ⇒ Object
34 35 36 |
# File 'lib/pure_jpeg/bit_writer.rb', line 34 def bytes @data end |
#flush ⇒ Object
Pad remaining bits with 1s and flush (per JPEG spec).
28 29 30 31 32 |
# File 'lib/pure_jpeg/bit_writer.rb', line 28 def flush return unless @bits_in_buffer > 0 padding = 8 - @bits_in_buffer write_bits((1 << padding) - 1, padding) end |
#write_bits(value, num_bits) ⇒ Object
Write ‘num_bits` of `value` (MSB first) into the output stream.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/pure_jpeg/bit_writer.rb', line 12 def write_bits(value, num_bits) return if num_bits == 0 @buffer = (@buffer << num_bits) | (value & ((1 << num_bits) - 1)) @bits_in_buffer += num_bits while @bits_in_buffer >= 8 @bits_in_buffer -= 8 byte = (@buffer >> @bits_in_buffer) & 0xFF @data << byte @data << 0x00 if byte == 0xFF # byte stuffing end @buffer &= (1 << @bits_in_buffer) - 1 end |