Class: Vissen::Output::Filter::Quantizer

Inherits:
Object
  • Object
show all
Includes:
Vissen::Output::Filter
Defined in:
lib/vissen/output/filter/quantizer.rb

Overview

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

Instance Attribute Summary

Attributes included from Vissen::Output::Filter

#context

Instance Method Summary collapse

Constructor Details

#initialize(*args, steps: 256, range: 0...steps) ⇒ Quantizer

Returns a new instance of Quantizer.

Parameters:

  • steps (Integer) (defaults to: 256)

    the number of quantized steps.

  • range (Range) (defaults to: 0...steps)

    the range in which the quantized values should lie.

Raises:

  • (RangeError)

    if steps < 2.

  • (ArgumentError)

    if the range is exclusive and has a floating point end value.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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

Instance Method Details

#apply(pixel_buffer) ⇒ Object

Applies the filter to the given pixel cloud.

Parameters:

  • pixel_buffer (PixelBuffer)

    the pixel Buffer to perform the filter operation on.

See Also:



40
41
42
43
44
45
46
# 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