Class: Axon::Image

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

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Image

:call-seq:

Image.new(image_in)

Wraps image_in in an easy API.

Rather than calling Image.new directly, you may want to call Axon.jpeg or Axon.png.

io_in = File.open("image.jpg", "r")
reader = Axon::JPEG::Reader.new(io_in)
image = Axon::Image.new(reader)

image.fit(10, 20)
image.height #=> 20 or less
image.width  #=> 10 or less

io_out = File.open("out.jpg", "w")
image.jpg(io_out) # writes a compressed JPEG file


79
80
81
82
# File 'lib/axon.rb', line 79

def initialize(source)
  @source = source
  self
end

Instance Method Details

#color_modelObject

Gets the color model of the image.



220
221
222
# File 'lib/axon.rb', line 220

def color_model
  @source.color_model
end

#componentsObject

Gets the components in the image.



214
215
216
# File 'lib/axon.rb', line 214

def components
  @source.components
end

#crop(*args) ⇒ Object

:call-seq:

crop(width, height, x_offset = 0, y_offset = 0)

Crops the image and extracts regions.

If the region extends beyond the boundaries of the image then the cropped image will be truncated at the boundaries.

Example

i = Axon::JPEG('test.jpg')
i.crop(50, 75, 10, 20)
c.width  # => 50
c.height # => 75

Example of Cropping Past the Boundaries

i = Axon::JPEG('test.jpg')
i.crop(50, 75, 60, 20)
i.width # => 40 # note that this is not 50


167
168
169
170
# File 'lib/axon.rb', line 167

def crop(*args)
  @source = Cropper.new(@source, *args)
  self
end

#fit(*args) ⇒ Object

:call-seq:

fit(width, height)

Scales the image to fit inside given box dimensions while maintaining the aspect ratio.

Example

i = Axon::JPEG('test.jpg')
i.fit(5, 20)
i.width  # => 5
i.height # => 10


141
142
143
144
# File 'lib/axon.rb', line 141

def fit(*args)
  @source = Fit.new(@source, *args)
  self
end

#getsObject

Gets the next scanline from the image.



244
245
246
# File 'lib/axon.rb', line 244

def gets
  @source.gets
end

#heightObject

Gets the height of the image.



232
233
234
# File 'lib/axon.rb', line 232

def height
  @source.height
end

#jpeg(*args) ⇒ Object

:call-seq:

jpeg(io_out [, options])

Writes the image to io_out as compressed JPEG data. Returns the number of bytes written.

options may contain the following symbols:

  • :bufsize – the maximum size in bytes of the writes that will be made to io_out

  • :quality – JPEG quality on a 0..100 scale.

  • :exif – Raw exif string that will be saved in the header.

  • :icc_profile – Raw ICC profile string that will be saved in the header.

Example

i = Axon::JPEG('input.jpg')
io_out = File.open('output.jpg', 'w')
i.jpeg(io_out, :quality => 88) # writes the image to output.jpg


192
193
194
# File 'lib/axon.rb', line 192

def jpeg(*args)
  JPEG.write(@source, *args)
end

#linenoObject

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



238
239
240
# File 'lib/axon.rb', line 238

def lineno
  @source.lineno
end

#png(*args) ⇒ Object

:call-seq:

png(io_out)

Writes the image to io_out as compressed PNG data. Returns the number of bytes written.

Example

i = Axon::JPEG('input.png')
io_out = File.open('output.png', 'w')
i.png(io_out) # writes the image to output.png


208
209
210
# File 'lib/axon.rb', line 208

def png(*args)
  PNG.write(@source, *args)
end

#scale_bilinear(*args) ⇒ Object

:call-seq:

scale_bilinear(width, height)

Scales the image 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

i = Axon::JPEG('test.jpg')
i.scale_bilinear(50, 75)
i.width  # => 50
i.height # => 75


103
104
105
106
# File 'lib/axon.rb', line 103

def scale_bilinear(*args)
  @source = BilinearScaler.new(@source, *args)
  self
end

#scale_nearest(*args) ⇒ Object

:call-seq:

scale_nearest(width, height)

Scales the image using the nearest-neighbor interpolation method.

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

Example

i = Axon::JPEG('test.jpg')
i.scale_nearest(50, 75)
i.width  # => 50
i.height # => 75


123
124
125
126
# File 'lib/axon.rb', line 123

def scale_nearest(*args)
  @source = NearestNeighborScaler.new(@source, *args)
  self
end

#widthObject

Gets the width of the image.



226
227
228
# File 'lib/axon.rb', line 226

def width
  @source.width
end