Class: Axon::Noise

Inherits:
Object
  • Object
show all
Defined in:
lib/axon/generators.rb

Overview

A Noise Image Generator

Axon::Noise will generate images with random pixel color values.

Example

Axon::Noise.new(100, 200, :components => 1)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, options = nil) ⇒ Noise

:call-seq:

Noise.new(width, height, options = {})

Creates a new noise image object with dimensions width x height.

options may contain the following optional hash key values:

  • :components – The number of components in the generated image.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/axon/generators.rb', line 32

def initialize(width, height, options=nil)
  options ||= {}

  @width = width
  @height = height
  @components = options[:components] || 3
  @lineno = 0
  @empty_string = String.new
  if @empty_string.respond_to? :force_encoding
    @empty_string.force_encoding('BINARY')
  end
end

Instance Attribute Details

#componentsObject (readonly)

The components in the generated image.



18
19
20
# File 'lib/axon/generators.rb', line 18

def components
  @components
end

#heightObject (readonly)

The height of the generated image.



15
16
17
# File 'lib/axon/generators.rb', line 15

def height
  @height
end

#linenoObject (readonly)

The index of the next line that will be fetched by gets, starting at 0.



21
22
23
# File 'lib/axon/generators.rb', line 21

def lineno
  @lineno
end

#widthObject (readonly)

The width of the generated image.



12
13
14
# File 'lib/axon/generators.rb', line 12

def width
  @width
end

Instance Method Details

#getsObject

Gets the next scanline from the generated image.



47
48
49
50
51
52
53
# File 'lib/axon/generators.rb', line 47

def gets
  return nil if @lineno >= @height
  sl = @empty_string.dup
  (@width * @components).times{ sl << rand(2**8) }
  @lineno += 1
  sl
end