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

Constants included from Origami::Filter

A85, AHx, CCF, Fl, RL

Instance Method Summary collapse

Methods included from Origami::Filter

included

Constructor Details

#initialize(parameters = {}) ⇒ Flate

Create a new Flate Filter.

parameters

A hash of filter options (ignored).



56
57
58
# File 'lib/origami/filters/flate.rb', line 56

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.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/origami/filters/flate.rb', line 80

def decode(stream)
  
  zlib_stream = Zlib::Inflate.new
  begin
    uncompressed = zlib_stream.inflate(stream)
  rescue Zlib::DataError => zlib_except
    uncompressed = zlib_stream.flush_next_out

    unless Origami::OPTIONS[:ignore_zlib_errors]
      raise InvalidFlateDataError.new(zlib_except.message, uncompressed)
    end
  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.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/origami/filters/flate.rb', line 64

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