Class: PNG::Filters

Inherits:
Object
  • Object
show all
Defined in:
lib/spittle/png/filters.rb

Class Method Summary collapse

Class Method Details

.decode(filter_type, value, index, row, last_row, record_width) ⇒ Object

filter methods are inlined here for performance



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/spittle/png/filters.rb', line 9

def decode( filter_type, value, index, row, last_row, record_width )
  case filter_type
  when 0
    #no filter
    value
  when 1
    #left
    (value + fetch_pixel(index - record_width, row)) % 256
  when 2
    #left
    (value + fetch_pixel(index, last_row)) % 256
  when 3
    #avg
    (value + ( (fetch_pixel(index - record_width, row) + fetch_pixel(index, last_row)) / 2 ).floor) % 256
  when 4
    #paeth
    left = fetch_pixel(index - record_width, row)
    above = fetch_pixel(index, last_row)
    upper_left = fetch_pixel(index - record_width, last_row)

    pr = paeth_predictor( left, above, upper_left )

    (value + pr) % 256
  else
    raise "Invalid filter type (#{filter_type})"
  end
end

.encode(filter_type, value, index, row, last_row, record_width) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/spittle/png/filters.rb', line 36

def encode( filter_type, value, index, row, last_row, record_width )
  case filter_type
  when 0
    value
  when 1
    #left
    (value - fetch_pixel(index - record_width, row)) % 256
  when 2
    #up
    (value - fetch_pixel(index, last_row)) % 256
  when 3
    #avg
    (value - ( (fetch_pixel(index - record_width, row) + fetch_pixel(index, last_row)) / 2 ).floor) % 256
  when 4
    #paeth
    left = fetch_pixel(index - record_width, row)
    above = fetch_pixel(index, last_row)
    upper_left = fetch_pixel(index - record_width, last_row)

    pr = paeth_predictor( left, above, upper_left )
    (value - pr) % 256
  else
    raise "Filter type (#{filter_type}) is not supported"
  end
end

.fetch_pixel(idx, row) ⇒ Object



4
5
6
# File 'lib/spittle/png/filters.rb', line 4

def fetch_pixel(idx, row)
  idx < 0 ? 0 : row[idx] || 0
end