Class: PNGlitch::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/pnglitch/filter.rb

Overview

Filter represents the filtering functions that is defined in PNG spec.

Constant Summary collapse

NONE =
0
SUB =
1
UP =
2
AVERAGE =
3
PAETH =
4
@@types =
Filter.constants.sort_by {|c| const_get c }.collect(&:downcase)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter_type, sample_size) ⇒ Filter

Returns a new instance of Filter.



33
34
35
36
37
38
39
# File 'lib/pnglitch/filter.rb', line 33

def initialize filter_type, sample_size
  @filter_type = Filter.guess(filter_type) || 0
  @filter_type_name = @@types[@filter_type]
  @sample_size = sample_size
  @encoder = self.method ('encode_%s' % @filter_type_name.to_s).to_sym
  @decoder = self.method ('decode_%s' % @filter_type_name.to_s).to_sym
end

Instance Attribute Details

#decoderObject

Returns the value of attribute decoder.



31
32
33
# File 'lib/pnglitch/filter.rb', line 31

def decoder
  @decoder
end

#encoderObject

Returns the value of attribute encoder.



31
32
33
# File 'lib/pnglitch/filter.rb', line 31

def encoder
  @encoder
end

#filter_typeObject (readonly)

Returns the value of attribute filter_type.



30
31
32
# File 'lib/pnglitch/filter.rb', line 30

def filter_type
  @filter_type
end

Class Method Details

.guess(filter_type) ⇒ Object

Guesses and retuens the filter type as a number.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pnglitch/filter.rb', line 18

def self.guess filter_type
  type = nil
  if filter_type.is_a?(Numeric) && filter_type.between?(NONE, PAETH)
    type = filter_type.to_i
  elsif filter_type.is_a?(String) && filter_type =~ /^[0-4]$/
    type = filter_type.to_i
  else
    type = @@types.collect{|c| c.to_s[0] }.index(filter_type.to_s[0].downcase)
  end
  type
end

Instance Method Details

#decode(data, prev_data = nil) ⇒ Object

Reconstruct with a specified filter type.



51
52
53
# File 'lib/pnglitch/filter.rb', line 51

def decode data, prev_data = nil
  @decoder.call data.dup, prev_data
end

#encode(data, prev_data = nil) ⇒ Object

Filter with a specified filter type.



44
45
46
# File 'lib/pnglitch/filter.rb', line 44

def encode data, prev_data = nil
  @encoder.call data.dup, prev_data
end