Class: Pdfrb::Filter::RunLengthDecode

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/pdfrb/filter/run_length_decode.rb

Overview

RunLengthDecode (s7.4.5). Length byte 0..127 = N+1 literal bytes; 129..255 = repeat next byte (257 - N) times; 128 = EOD.

Class Method Summary collapse

Class Method Details

.decoder(bytes, _parms, _document) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pdfrb/filter/run_length_decode.rb', line 13

def decoder(bytes, _parms, _document)
  out = +""
  i = 0
  bs = bytes.bytes
  while i < bs.length
    n = bs[i]
    i += 1
    case n
    when 128 then break
    when 0..127
      out << bs[i, n + 1].pack("C*")
      i += n + 1
    when 129..255
      count = 257 - n
      out << (bs[i] || 0).chr * count if count.positive? && bs[i]
      i += 1
    end
  end
  out.force_encoding(Encoding::BINARY)
end

.encoder(bytes, _parms, _document) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pdfrb/filter/run_length_decode.rb', line 34

def encoder(bytes, _parms, _document)
  out = +""
  bs = bytes.bytes
  i = 0
  while i < bs.length
    # Try to find a run of identical bytes (3+).
    run_len = 1
    while i + run_len < bs.length && bs[i + run_len] == bs[i] && run_len < 128
      run_len += 1
    end
    if run_len >= 3
      out << (257 - run_len).chr
      out << bs[i].chr
      i += run_len
    else
      # Accumulate literals until next run or 128 bytes.
      literal = []
      while i < bs.length && literal.length < 128
        # Stop if a run of >= 3 starts.
        ahead_run = 0
        while i + ahead_run < bs.length && bs[i + ahead_run] == bs[i] && ahead_run < 3
          ahead_run += 1
        end
        break if ahead_run >= 3

        literal << bs[i]
        i += 1
      end
      out << (literal.length - 1).chr
      out << literal.pack("C*")
    end
  end
  out << 128.chr # EOD
  out.force_encoding(Encoding::BINARY)
end