class PDF::Reader::LZW

:nodoc:
The PDF spec also has some data on the algorithm.
ref marknelson.us/1989/10/01/lzw-data-compression/
ref www.fileformat.info/format/tiff/corion-lzw.htm<br><br>See the following links for more information:
from a TIFF file.
used in PDF files to compresses streams, usually for image data sourced
A general class for decoding LZW compressed data. LZW can be

def self.create_new_string(string_table, some_code, other_code)

def self.create_new_string(string_table, some_code, other_code)
  raise MalformedPDFError, "invalid LZW data" if some_code.nil? || other_code.nil?
  item_one = string_table[some_code]
  item_two = string_table[other_code]
  if item_one && item_two
    item_one + item_two.chr
  else
    raise MalformedPDFError, "invalid LZW data"
  end
end

def self.decode(data)


Decompresses a LZW compressed string.
def self.decode(data)
  stream = BitStream.new(data.to_s, 9) # size of codes between 9 and 12 bits
  string_table = StringTable.new
  result = "".dup
  until (code = stream.read) == CODE_EOD
    if code == CODE_CLEAR_TABLE
      stream.set_bits_in_chunk(9)
      string_table = StringTable.new
      code = stream.read
      break if code == CODE_EOD
      result << string_table[code]
      old_code = code
    else
      string = string_table[code]
      if string
        result << string
        string_table.add create_new_string(string_table, old_code, code)
        old_code = code
      else
        new_string = create_new_string(string_table, old_code, old_code)
        result << new_string
        string_table.add new_string
        old_code = code
      end
      #increase de size of the codes when limit reached
      if string_table.string_table_pos == 511
        stream.set_bits_in_chunk(10)
      elsif string_table.string_table_pos == 1023
        stream.set_bits_in_chunk(11)
      elsif string_table.string_table_pos == 2047
        stream.set_bits_in_chunk(12)
      end
    end
  end
  result
end