Class: PDF::Reader::LZW::BitStream

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/reader/lzw.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(data, bits_in_chunk) ⇒ BitStream

:nodoc:



22
23
24
25
26
27
28
# File 'lib/pdf/reader/lzw.rb', line 22

def initialize(data, bits_in_chunk)
  @data = data
  @data.force_encoding("BINARY") if @data.respond_to?(:force_encoding)
  @bits_in_chunk = bits_in_chunk
  @current_pos = 0
  @bits_left_in_byte = 8
end

Instance Method Details

#readObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pdf/reader/lzw.rb', line 34

def read
  bits_left_in_chunk = @bits_in_chunk
  chunk = nil
  while bits_left_in_chunk > 0 and @current_pos < @data.size
    chunk = 0 if chunk.nil?
    codepoint = @data[@current_pos, 1].unpack("C*")[0]
    current_byte = codepoint & (2**@bits_left_in_byte -1) #clear consumed bits
    dif = bits_left_in_chunk - @bits_left_in_byte
    if dif > 0 then  current_byte <<= dif
    elsif dif < 0 then  current_byte >>= dif.abs
    end
    chunk |= current_byte #add bits to result
    bits_left_in_chunk = if dif >= 0 then dif else 0 end
    @bits_left_in_byte = if dif < 0 then dif.abs else 0 end
    if @bits_left_in_byte.zero? #next byte
      @current_pos += 1
      @bits_left_in_byte = 8
    end
  end
  chunk
end

#set_bits_in_chunk(bits_in_chunk) ⇒ Object



30
31
32
# File 'lib/pdf/reader/lzw.rb', line 30

def set_bits_in_chunk(bits_in_chunk)
  @bits_in_chunk = bits_in_chunk
end