Class: PDF::Reader::Filter::RunLength

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

Overview

implementation of the run length stream filter

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RunLength

Returns a new instance of RunLength.



11
12
13
# File 'lib/pdf/reader/filter/run_length.rb', line 11

def initialize(options = {})
  @options = options
end

Instance Method Details

#filter(data) ⇒ Object

Decode the specified data with the RunLengthDecode compression algorithm



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pdf/reader/filter/run_length.rb', line 17

def filter(data)
  pos = 0
  out = "".dup

  while pos < data.length
    length = data.getbyte(pos)
    pos += 1

    unless length.nil?
      case
        # nothing
      when length == 128
        break
      when length < 128
        # When the length is < 128, we copy the following length+1 bytes
        # literally.
        out << data[pos, length + 1]
        pos += length
      else
        # When the length is > 128, we copy the next byte (257 - length)
        # times; i.e., "\xFA\x00" ([250, 0]) will expand to
        # "\x00\x00\x00\x00\x00\x00\x00".
        previous_byte = data[pos, 1] || ""
        out << previous_byte * (257 - length)
      end
    end

    pos += 1
  end

  Depredict.new(@options).filter(out)
end