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


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

def initialize(source)
  @source = source
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



331
332
333
# File 'lib/axon.rb', line 331

def method_missing(name, *args)
  @source.send(name, *args)
end

Instance Method Details

#componentsObject

Gets the components in the image.



303
304
305
# File 'lib/axon.rb', line 303

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


211
212
213
214
# File 'lib/axon.rb', line 211

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


185
186
187
188
# File 'lib/axon.rb', line 185

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

#getsObject

Gets the next scanline from the image.



327
328
329
# File 'lib/axon.rb', line 327

def gets
  @source.gets
end

#heightObject

Gets the height of the image.



315
316
317
# File 'lib/axon.rb', line 315

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.

If the image has an alpha channel it will be stripped.

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


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

def jpeg(*args)
  case @source.components
  when 2,4 then @source = AlphaStripper.new(@source)
  end
  JPEG.write(@source, *args)
end

#jpeg_file(path, *args) ⇒ Object

:call-seq:

jpeg_file(path [, options])

Writes the image to a new file at path as compressed JPEG data. Returns the number of bytes written.

If the image has an alpha channel it will be stripped.

See Axon#jpeg for a description of options.

Example

Axon.png_file("image.png") do |image|
  image.jpeg_file("image.jpg") # saves the image to "image.jpeg"
end


261
262
263
264
265
# File 'lib/axon.rb', line 261

def jpeg_file(path, *args)
  File.open(path, 'wb') do |f|
    jpeg(f, *args)
  end
end

#linenoObject

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



321
322
323
# File 'lib/axon.rb', line 321

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


279
280
281
# File 'lib/axon.rb', line 279

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

#png_file(path, *args) ⇒ Object

:call-seq:

png_file(path)

Writes the image to a new file at path as compressed PNG data. Returns the number of bytes written.

Example

Axon.jpeg_file("image.jpg") do |image|
  image.png_file("image.png") # saves the image to "image.jpeg"
end


295
296
297
298
299
# File 'lib/axon.rb', line 295

def png_file(path, *args)
  File.open(path, 'wb') do |f|
    png(f, *args)
  end
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


147
148
149
150
# File 'lib/axon.rb', line 147

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


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

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

#widthObject

Gets the width of the image.



309
310
311
# File 'lib/axon.rb', line 309

def width
  @source.width
end