Class: Branding::PNG::Filter
- Inherits:
-
Object
- Object
- Branding::PNG::Filter
- Defined in:
- lib/branding/png.rb
Overview
private class to handle reconstructing filtered data www.w3.org/TR/PNG/#9Filters
Instance Method Summary collapse
-
#a ⇒ Object
a: the byte corresponding to x in the pixel immediately before the pixel containing x (or the byte immediately before x, when the bit depth is less than 8).
- #average(x) ⇒ Object
- #b ⇒ Object
- #c ⇒ Object
-
#initialize(filter_bit, filtered_scanline, previous_scanline, color_channels) ⇒ Filter
constructor
A new instance of Filter.
- #none(x) ⇒ Object
- #paeth(x) ⇒ Object
- #paeth_predictor(a, b, c) ⇒ Object
-
#reconstructed_scanline ⇒ Object
yields a reconstructed byte.
- #sub(x) ⇒ Object
- #up(x) ⇒ Object
Constructor Details
#initialize(filter_bit, filtered_scanline, previous_scanline, color_channels) ⇒ Filter
Returns a new instance of Filter.
66 67 68 69 70 71 72 |
# File 'lib/branding/png.rb', line 66 def initialize(filter_bit, filtered_scanline, previous_scanline, color_channels) @type = filter_bit @filtered = filtered_scanline @previous = previous_scanline || [] @pixel_width = color_channels @position = 0 end |
Instance Method Details
#a ⇒ Object
a: the byte corresponding to x in the pixel immediately before the pixel containing x (or the byte immediately before x, when the bit depth is less than 8)
102 103 104 105 106 107 |
# File 'lib/branding/png.rb', line 102 def a offset = @position - @pixel_width return 0x00 if offset < 0 @reconstructed_scanline[offset] end |
#average(x) ⇒ Object
132 133 134 |
# File 'lib/branding/png.rb', line 132 def average(x) x + ((a + b) / 2.0).floor end |
#b ⇒ Object
109 110 111 |
# File 'lib/branding/png.rb', line 109 def b @previous[@position] || 0x00 end |
#c ⇒ Object
113 114 115 116 117 118 |
# File 'lib/branding/png.rb', line 113 def c offset = @position - @pixel_width return 0x00 if offset < 0 @previous[offset] end |
#none(x) ⇒ Object
120 121 122 |
# File 'lib/branding/png.rb', line 120 def none(x) x end |
#paeth(x) ⇒ Object
137 138 139 |
# File 'lib/branding/png.rb', line 137 def paeth(x) x + paeth_predictor(a, b, c) end |
#paeth_predictor(a, b, c) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/branding/png.rb', line 141 def paeth_predictor(a, b, c) p = a + b - c pa = (p - a).abs pb = (p - b).abs pc = (p - c).abs return a if pa <= pb && pa <= pc return b if pb <= pc c end |
#reconstructed_scanline ⇒ Object
yields a reconstructed byte
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/branding/png.rb', line 76 def reconstructed_scanline @reconstructed_scanline = [] @filtered.each do |byte| recon = case @type when 0 none(byte) when 1 sub(byte) when 2 up(byte) when 3 average(byte) when 4 paeth(byte) end @reconstructed_scanline << (recon % 256) @position += 1 end @reconstructed_scanline end |
#sub(x) ⇒ Object
124 125 126 |
# File 'lib/branding/png.rb', line 124 def sub(x) x + a end |
#up(x) ⇒ Object
128 129 130 |
# File 'lib/branding/png.rb', line 128 def up(x) x + b end |