Class: Axon::NearestNeighborScaler

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

Overview

A Nearest-neighbor Image Scaler

Axon::NearestNeighborScaler scales images quickly using the nearest-neighbor interpolation method.

Nearest-neighbor interpolation selects the value of the nearest pixel when calculating colors in the scaled image.

Example

n = Axon::NearestNeighborScaler.new(image_in, 50, 75)
n.width  # => 50
n.height # => 75
n.gets   # => String

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, width, height) ⇒ NearestNeighborScaler

:call-seq:

NearestNeighborScaler.new(image_in, width, height)

Scales image_in to the size width x height using the nearest-neighbor interpolation method.

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
# File 'lib/axon/scalers.rb', line 33

def initialize(source, width, height)
  raise ArgumentError if width < 1 || height < 1
  @width = width
  @height = height
  @source = source
  @lineno = 0
  @buf = nil
end

Instance Attribute Details

#heightObject (readonly)

The height of the generated image.



22
23
24
# File 'lib/axon/scalers.rb', line 22

def height
  @height
end

#linenoObject (readonly)

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



25
26
27
# File 'lib/axon/scalers.rb', line 25

def lineno
  @lineno
end

#widthObject (readonly)

The width of the generated image.



19
20
21
# File 'lib/axon/scalers.rb', line 19

def width
  @width
end

Instance Method Details

#componentsObject

Gets the components in the scaled image. Same as the components of the source image.



45
46
47
# File 'lib/axon/scalers.rb', line 45

def components
  @source.components
end

#getsObject

Gets the next scanline from the cropped image.



51
52
53
54
55
56
# File 'lib/axon/scalers.rb', line 51

def gets
  return nil if @lineno >= @height
  sample = (@lineno * @source.height / @height.to_f).floor
  @lineno += 1
  Interpolation.nearest(get_buf(sample), @width, components)
end