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:

  • :color_model – The color model of the generated image.

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



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/axon/generators.rb', line 36

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

  @width = width
  @height = height
  @color_model = options[:color_model] || :RGB
  @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

#color_modelObject (readonly)

The color model of the generated image.



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

def color_model
  @color_model
end

#componentsObject (readonly)

The components in the generated image.



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

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.



24
25
26
# File 'lib/axon/generators.rb', line 24

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.



52
53
54
55
56
57
58
# File 'lib/axon/generators.rb', line 52

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