class Vissen::Output::Filter::Quantizer

Scales and rounds the color components to only take discrete values.

Public Class Methods

new(*args, steps: 256, range: 0...steps) click to toggle source

@raise [RangeError] if steps < 2. @raise [ArgumentError] if the range is exclusive and has a floating

point end value.

@param args (see Filter) @param steps [Integer] the number of quantized steps. @param range [Range] the range in which the quantized values should

lie.
Calls superclass method Vissen::Output::Filter::new
# File lib/vissen/output/filter/quantizer.rb, line 18
def initialize(*args, steps: 256, range: 0...steps)
  super(*args)

  raise RangeError if steps < 2

  from = range.begin
  to   = if range.exclude_end?
           raise ArgumentError if range.end.is_a?(Float)
           range.end - 1
         else range.end
         end

  design_function from, to, steps

  freeze
end

Public Instance Methods

apply(pixel_buffer) click to toggle source

Applies the filter to the given pixel cloud.

@see Filter @param pixel_buffer [PixelBuffer] the pixel Buffer to perform the

filter operation on.
# File lib/vissen/output/filter/quantizer.rb, line 40
def apply(pixel_buffer)
  pixel_buffer.each do |pixel|
    pixel.r = @fn.call pixel.r
    pixel.g = @fn.call pixel.g
    pixel.b = @fn.call pixel.b
  end
end

Private Instance Methods

design_function(from, to, steps) click to toggle source
# File lib/vissen/output/filter/quantizer.rb, line 50
def design_function(from, to, steps)
  steps -= 1
  @fn =
    if from.zero? && to == steps
      ->(v) { (v * to).round }
    else
      factor = (to - from).to_f / steps
      ->(v) { from + (v * steps).round * factor }
    end
end