class FreeImage::Memory

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

Summary

Supports loading and saving images to a Ruby string.

Usage

# Read an image from a byte string
bytes = ::File.read('test/fixtures/lena.png', :encoding => Encoding::BINARY)
image = FreeImage::Memory.open(bytes)

# Save an image to a byte string
dest = FreeImage::Memory.new
image.save(dest, :jpeg)
dest.bytes

Methods

Public Class

  1. new

Public Instance

  1. format
  2. memory
  3. save

Attributes

memory [R]

MemoryStream used to read and write data

Public Class methods

new (bytes = nil)

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

Parameters

bytes

If specified, FreeImage will read image from the bytes string and treat it as readonly. If not specified, then FreeImage will create a writable memory stream.

[show source]
# File lib/free-image/sources/memory.rb, line 141
def initialize(bytes = nil)
  @memory = MemoryStream.new(bytes)
end

Public Instance methods

memory.format -> :format

Returns the image format for a memory stream. If the image format cannot be determined the :unknown will be returned.

[show source]
# File lib/free-image/sources/memory.rb, line 150
def format
  result = FreeImage.FreeImage_GetFileTypeFromMemory(@memory, 0)
  FreeImage.check_last_error
  result
end
memory.save(format = nil, flags = 0) -> boolean

Saves an image to memory.

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 = FreeImage::Memory.new
dst.save(image, :jpeg, AbtractSource::JPEG_QUALITYSUPERB | AbtractSource::JPEG_PROGRESSIVE)
dst.bytes
[show source]
# File lib/free-image/sources/memory.rb, line 175
def save(bitmap, format, flags = 0)
  result = FreeImage.FreeImage_SaveToMemory(format, bitmap, @memory, flags)
  FreeImage.check_last_error
  result
end