Class: Origami::Filter::Flate

Inherits:
Object
  • Object
show all
Includes:
Origami::Filter
Defined in:
lib/origami/filters/flate.rb

Overview

Class representing a Filter used to encode and decode data with zlib/Flate compression algorithm.

Defined Under Namespace

Classes: DecodeParms

Constant Summary collapse

EOD =

:nodoc:

257

Instance Method Summary collapse

Constructor Details

#initialize(parameters = {}) ⇒ Flate

Create a new Flate Filter.

parameters

A hash of filter options (ignored).



65
66
67
# File 'lib/origami/filters/flate.rb', line 65

def initialize(parameters = {})
  super(DecodeParms.new(parameters))
end

Instance Method Details

#decode(stream) ⇒ Object

Decodes data using zlib/Inflate decompression method.

stream

The data to decode.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/origami/filters/flate.rb', line 89

def decode(stream)
  
  zlib_stream = Zlib::Inflate.new
  begin
    uncompressed = zlib_stream.inflate(stream)
  rescue Zlib::DataError => zlib_except
    raise InvalidFlateDataError.new(zlib_stream, zlib_except)
  end

  if @params.Predictor.is_a?(Integer)
    colors  = @params.Colors.is_a?(Integer) ? @params.Colors.to_i : 1
    bpc     = @params.BitsPerComponent.is_a?(Integer) ? @params.BitsPerComponent.to_i : 8
    columns = @params.Columns.is_a?(Integer) ? @params.Columns.to_i : 1

    uncompressed = Predictor.do_post_prediction(uncompressed, @params.Predictor.to_i, colors, bpc, columns)
  end

  uncompressed
end

#encode(stream) ⇒ Object

Encodes data using zlib/Deflate compression method.

stream

The data to encode.



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/origami/filters/flate.rb', line 73

def encode(stream)
  if @params.Predictor.is_a?(Integer)
    colors  = @params.Colors.is_a?(Integer) ? @params.Colors.to_i : 1
    bpc     = @params.BitsPerComponent.is_a?(Integer) ? @params.BitsPerComponent.to_i : 8
    columns = @params.Columns.is_a?(Integer) ? @params.Columns.to_i : 1

    stream = Predictor.do_pre_prediction(stream, @params.Predictor.to_i, colors, bpc, columns)
  end       
  
  Zlib::Deflate.deflate(stream, Zlib::BEST_COMPRESSION)
end