Class: Axon::NearestNeighborScaler
- Inherits:
-
Object
- Object
- Axon::NearestNeighborScaler
- 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
-
#height ⇒ Object
readonly
The height of the generated image.
-
#lineno ⇒ Object
readonly
The index of the next line that will be fetched by gets, starting at 0.
-
#width ⇒ Object
readonly
The width of the generated image.
Instance Method Summary collapse
-
#components ⇒ Object
Gets the components in the scaled image.
-
#gets ⇒ Object
Gets the next scanline from the cropped image.
-
#initialize(source, width, height) ⇒ NearestNeighborScaler
constructor
:call-seq: NearestNeighborScaler.new(image_in, width, height).
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.
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
#height ⇒ Object (readonly)
The height of the generated image.
22 23 24 |
# File 'lib/axon/scalers.rb', line 22 def height @height end |
#lineno ⇒ Object (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 |
#width ⇒ Object (readonly)
The width of the generated image.
19 20 21 |
# File 'lib/axon/scalers.rb', line 19 def width @width end |
Instance Method Details
#components ⇒ Object
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 |
#gets ⇒ Object
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 |