class FreeImage::File

  1. lib/free-image/sources/file.rb
Superclass: AbstractSource

Summary

Supports loading and saving images to a file.

Usage

# Open a file
src = FreeImage::File.new('test/fixtures/lena.png')
image = src.open

# Save a file
dest = FreeImage::File.new('test/fixtures/lena_new.jpeg')
image.save(dest, :jpeg)

Methods

Public Class

  1. new

Public Instance

  1. format
  2. save

Public Class methods

new (image_path)

Create a new FreeImage::File instance that can read and write image data from a file.

Parameters

image_path

The full path to a image file.

[show source]
# File lib/free-image/sources/file.rb, line 58
def initialize(image_path)
  @image_path = image_path
end

Public Instance methods

file.format -> :format

Returns the image format for a file. If the image format cannot be determined then will return :unknown.

[show source]
# File lib/free-image/sources/file.rb, line 68
def format
  result = FreeImage.FreeImage_GetFileType(@image_path, 0)
  FreeImage.check_last_error

  if result == :unknown
    # Try to guess the file format from the file extension
    result = FreeImage.FreeImage_GetFIFFromFilename(@image_path)
    FreeImage.check_last_error
  end
  result
end
file.save(format = nil, flags = 0) -> boolean

Saves an image to a file.

Parameters

format

The format to save the image to.

flags

Format specific flags that control how a bitmap is saved. These flags are defined as constants on the AbstractSource class. Flags can be combined using Ruby's bitwise or operator (|)

Usage

image = Bimap.open('<path_to_file>')
dst = File.new('<path_to_new_file>')
dst.save(image, :jpeg, AbtractSource::JPEG_QUALITYSUPERB | AbtractSource::JPEG_PROGRESSIVE)
[show source]
# File lib/free-image/sources/file.rb, line 98
def save(bitmap, format, flags = 0)
  result = FreeImage.FreeImage_Save(format, bitmap, @image_path, flags)
  FreeImage.check_last_error
  result
end