Class: Flipbook::GIF::BitWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/flipbook/gif/bit_writer.rb

Instance Method Summary collapse

Constructor Details

#initialize ⇒ BitWriter

Returns a new instance of BitWriter.



6
7
8
9
10
# File 'lib/flipbook/gif/bit_writer.rb', line 6

def initialize
  @bytes = "".b
  @buffer = 0
  @bits = 0
end

Instance Method Details

#sub_blocks ⇒ Object



22
23
24
25
26
27
# File 'lib/flipbook/gif/bit_writer.rb', line 22

def sub_blocks
  @bytes << (@buffer & 255) if @bits.positive?
  output = "".b
  @bytes.bytes.each_slice(255) { |slice| output << slice.length << slice.pack("C*") }
  output << "\0".b
end

#write(code, width) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/flipbook/gif/bit_writer.rb', line 12

def write(code, width)
  @buffer |= code << @bits
  @bits += width
  while @bits >= 8
    @bytes << (@buffer & 255)
    @buffer >>= 8
    @bits -= 8
  end
end