Module: HexaPDF::Filter::LZWDecode

Defined in:
lib/hexapdf/filter/lzw_decode.rb

Overview

Implements the LZW filter.

Since LZW uses a tightly packed bit stream in which codes are of varying bit lengths and are not aligned to byte boundaries, this filter is not as fast as the other filters. If speed is a concern, the FlateDecode filter should be used instead.

See: HexaPDF::Filter, PDF2.0 s7.4.4

Constant Summary collapse

CLEAR_TABLE =

:nodoc:

256
EOD =

:nodoc:

257
INITIAL_ENCODER_TABLE =

:nodoc:

{}
INITIAL_DECODER_TABLE =

:nodoc:

{}

Class Method Summary collapse

Class Method Details

.decoder(source, options = nil) ⇒ Object

See HexaPDF::Filter



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/hexapdf/filter/lzw_decode.rb', line 68

def self.decoder(source, options = nil)
  fib = Fiber.new do
    # initialize decoder state
    code_length = 9
    table = INITIAL_DECODER_TABLE.dup

    stream = HexaPDF::Utils::BitStreamReader.new
    result = ''.b
    finished = false
    last_code = CLEAR_TABLE

    while !finished && source.alive? && (data = source.resume)
      stream.append_data(data)

      while (code = stream.read(code_length))

        # Decoder is one step behind => subtract 1!
        # We check the table size before entering the next code into it => subtract 1, but
        # there is one exception: After table entry 4095 is written, the clear table code
        # also gets written with code length 12,
        case table.size
        when 510, 1022, 2046
          code_length += 1
        when 4096
          if code != CLEAR_TABLE
            raise FilterError, "Maximum of 12bit for codes in LZW stream exceeded"
          end
        end

        if code == EOD
          finished = true
          break
        elsif code == CLEAR_TABLE
          # reset decoder state
          code_length = 9
          table = INITIAL_DECODER_TABLE.dup
        elsif last_code == CLEAR_TABLE
          unless table.key?(code)
            raise FilterError, "Unknown code in LZW encoded stream found"
          end
          result << table[code]
        else
          unless table.key?(last_code)
            raise FilterError, "Unknown code in LZW encoded stream found"
          end
          last_str = table[last_code]

          str = if table.key?(code)
                  table[code]
                else
                  last_str + last_str[0]
                end
          result << str
          table[table.size] = last_str + str[0]
        end

        last_code = code
      end

      Fiber.yield(result)
      result = ''.b
    end
  end

  if options && options[:Predictor]
    Predictor.decoder(fib, options)
  else
    fib
  end
end

.encoder(source, options = nil) ⇒ Object

See HexaPDF::Filter



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/hexapdf/filter/lzw_decode.rb', line 140

def self.encoder(source, options = nil)
  if options && options[:Predictor]
    source = Predictor.encoder(source, options)
  end

  Fiber.new do
    # initialize encoder state
    code_length = 9
    table = INITIAL_ENCODER_TABLE.dup

    # initialize the bit stream with the clear-table marker
    stream = HexaPDF::Utils::BitStreamWriter.new
    result = stream.write(CLEAR_TABLE, 9)
    str = ''.b

    while source.alive? && (data = source.resume)
      data.each_char do |char|
        newstr = str + char
        if table.key?(newstr)
          str = newstr
        else
          result << stream.write(table[str], code_length)
          table[newstr.freeze] = table.size
          str = char
        end

        case table.size
        when 512 then code_length = 10
        when 1024 then code_length = 11
        when 2048 then code_length = 12
        when 4096
          result << stream.write(CLEAR_TABLE, code_length)
          # reset encoder state
          code_length = 9
          table = INITIAL_ENCODER_TABLE.dup
        end
      end

      Fiber.yield(result)
      result = ''.b
    end

    result = stream.write(table[str], code_length)
    result << stream.write(EOD, code_length)
    result << stream.finalize

    result
  end
end