Class: FreeImage::File

Inherits:
AbstractSource show all
Defined in:
lib/free-image/sources/file.rb

Overview

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)

Instance Method Summary collapse

Methods inherited from AbstractSource

#open

Constructor Details

#initialize(image_path) ⇒ File

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.



58
59
60
# File 'lib/free-image/sources/file.rb', line 58

def initialize(image_path)
  @image_path = image_path
end

Instance Method Details

#formatObject

:call-seq:

file.format -> :format

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



68
69
70
71
72
73
74
75
76
77
78
# 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

#save(bitmap, format, flags = 0) ⇒ Object

:call-seq:

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)


98
99
100
101
102
# 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