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).



51
52
53
# File 'lib/origami/filters/flate.rb', line 51

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.



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
# File 'lib/origami/filters/flate.rb', line 79

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, input_data: stream, decoded_data: 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, 
                                                    predictor: @params.Predictor.to_i, 
                                                    colors: colors, 
                                                    bpc: bpc, 
                                                    columns: columns)
    end

    uncompressed
end

#encode(stream) ⇒ Object

Encodes data using zlib/Deflate compression method.

stream

The data to encode.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/origami/filters/flate.rb', line 59

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, 
                                             predictor: @params.Predictor.to_i, 
                                             colors: colors, 
                                             bpc: bpc, 
                                             columns: columns)
    end

    Zlib::Deflate.deflate(stream, Zlib::BEST_COMPRESSION)
end