Class: Vissen::Output::PixelBuffer

Inherits:
Object
  • Object
show all
Includes:
Buffer
Defined in:
lib/vissen/output/pixel_buffer.rb

Overview

The pixel buffer is an implementation of the general ‘Buffer` module that places `Pixel` objects in each buffer element.

Instance Attribute Summary

Attributes included from Buffer

#context, #elements

Instance Method Summary collapse

Methods included from Buffer

#[], #each_with_position, #share_context?

Constructor Details

#initialize(context, filters = []) ⇒ PixelBuffer

Returns a new instance of PixelBuffer.

Parameters:

  • context (Context)

    the context in which the pixel buffer exists.

  • filters (Array<Filter>) (defaults to: [])

    the output filters to apply when finalizing the buffer.

Raises:

  • (ContextError)

    if any of the filters does not share the same context.



20
21
22
23
24
25
26
27
28
# File 'lib/vissen/output/pixel_buffer.rb', line 20

def initialize(context, filters = [])
  super context, Pixel
  # Verify that all filters share the same context
  # before adding them.
  filters.each { |f| raise ContextError unless share_context? f }
  @filters = filters

  freeze
end

Instance Method Details

#clear!Object

Zeros the RGB components of each pixel in the buffer.



39
40
41
# File 'lib/vissen/output/pixel_buffer.rb', line 39

def clear!
  pixels.each(&:clear!)
end

#copy!(other) ⇒ Object

Replaces the pixel values of this buffer with those of the given object.

Parameters:

  • other (PixelBuffer)

    the pixel buffer to copy values from.

Raises:

  • (TypeError)

    if the other object is not a PixelBuffer.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vissen/output/pixel_buffer.rb', line 48

def copy!(other)
  raise TypeError unless other.is_a? self.class

  other.each_with_index do |src_pixel, index|
    dst_pixel = pixels[index]

    dst_pixel.r = src_pixel.r
    dst_pixel.g = src_pixel.g
    dst_pixel.b = src_pixel.b
  end
end

#finalize!self

Signals to the ‘PixelBuffer` that the user is done updating the color values and that the buffer should perform any post processing that is needed.

Returns:

  • (self)


65
66
67
68
# File 'lib/vissen/output/pixel_buffer.rb', line 65

def finalize!
  @filters.each { |filter| filter.apply self }
  self
end

#freezeself

Prevent any more filters from being added.

Returns:

  • (self)


33
34
35
36
# File 'lib/vissen/output/pixel_buffer.rb', line 33

def freeze
  @filters.freeze
  super
end