class Branding::PNG::Filter

private class to handle reconstructing filtered data www.w3.org/TR/PNG/#9Filters

Public Class Methods

new(filter_bit, filtered_scanline, previous_scanline, color_channels) click to toggle source
# 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

Public Instance Methods

a() click to toggle source

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)

# File lib/branding/png.rb, line 102
def a
  offset = @position - @pixel_width
  return 0x00 if offset < 0

  @reconstructed_scanline[offset]
end
average(x) click to toggle source
# File lib/branding/png.rb, line 132
def average(x)
  x + ((a + b) / 2.0).floor
end
b() click to toggle source
# File lib/branding/png.rb, line 109
def b
  @previous[@position] || 0x00
end
c() click to toggle source
# File lib/branding/png.rb, line 113
def c
  offset = @position - @pixel_width
  return 0x00 if offset < 0

  @previous[offset]
end
none(x) click to toggle source
# File lib/branding/png.rb, line 120
def none(x)
  x
end
paeth(x) click to toggle source

www.w3.org/TR/PNG/#9Filter-type-4-Paeth

# File lib/branding/png.rb, line 137
def paeth(x)
  x + paeth_predictor(a, b, c)
end
paeth_predictor(a, b, c) click to toggle source
# 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() click to toggle source

yields a reconstructed byte

# 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) click to toggle source
# File lib/branding/png.rb, line 124
def sub(x)
  x + a
end
up(x) click to toggle source
# File lib/branding/png.rb, line 128
def up(x)
  x + b
end