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)


107
108
109
110
111
112
113
114
115
# File 'lib/axon/scalers.rb', line 107

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.



96
97
98
# File 'lib/axon/scalers.rb', line 96

def height
  @height
end

#linenoObject (readonly)

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



99
100
101
# File 'lib/axon/scalers.rb', line 99

def lineno
  @lineno
end

#widthObject (readonly)

The width of the generated image.



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

def width
  @width
end

Instance Method Details

#color_modelObject

Gets the color model of the scaled image. Same as the color model of the source image.



127
128
129
# File 'lib/axon/scalers.rb', line 127

def color_model
  @source.color_model
end

#componentsObject

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



120
121
122
# File 'lib/axon/scalers.rb', line 120

def components
  @source.components
end

#getsObject

Gets the next scanline from the cropped image.



133
134
135
136
137
138
139
140
141
142
# File 'lib/axon/scalers.rb', line 133

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