37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/pdf/reader/lzw.rb', line 37
def read
bits_left_in_chunk = @bits_in_chunk
chunk = -1
while bits_left_in_chunk > 0 and @current_pos < @data.size
chunk = 0 if chunk < 0
codepoint = @data[@current_pos, 1].unpack("C*")[0]
current_byte = codepoint & (2**@bits_left_in_byte - 1)
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
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?
@current_pos += 1
@bits_left_in_byte = 8
end
end
chunk
end
|