Class: Axon::BilinearScaler

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

Overview

A Bilinear Image Scaler

Axon::BilinearScaler scales images using the bilinear interpolation method.

Bilinear interpolation calculates the color values in the resulting image by looking at the four nearest pixels for each pixel in the resulting image.

This gives a more accurate representation than nearest-neighbor interpolation, at the expense of slightly blurring the resulting image.

Example

n = Axon::BilinearScaler.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) ⇒ BilinearScaler

:call-seq:

BilinearScaler.new(image_in, width, height)

Scales image_in to the size width x height using the bilinear interpolation method.

Raises:

  • (ArgumentError)


100
101
102
103
104
105
106
107
108
# File 'lib/axon/scalers.rb', line 100

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

Instance Attribute Details

#heightObject (readonly)

The height of the generated image.



89
90
91
# File 'lib/axon/scalers.rb', line 89

def height
  @height
end

#linenoObject (readonly)

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



92
93
94
# File 'lib/axon/scalers.rb', line 92

def lineno
  @lineno
end

#widthObject (readonly)

The width of the generated image.



86
87
88
# File 'lib/axon/scalers.rb', line 86

def width
  @width
end

Instance Method Details

#componentsObject

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



113
114
115
# File 'lib/axon/scalers.rb', line 113

def components
  @source.components
end

#getsObject

Gets the next scanline from the cropped image.



119
120
121
122
123
124
125
126
127
128
# File 'lib/axon/scalers.rb', line 119

def gets
  return nil if @lineno >= @height
  sample = @lineno * @source.height / @height.to_f
  sample_i = sample.to_i
  ty = sample - sample_i
  @lineno += 1
  get_buf(sample_i)

  Interpolation.bilinear(@buf1, @buf2, @width, ty, components)
end