Class: FreeImage::Memory

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractSource

#open

Constructor Details

#initialize(bytes = nil) ⇒ Memory

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.



141
142
143
# File 'lib/free-image/sources/memory.rb', line 141

def initialize(bytes = nil)
  @memory = MemoryStream.new(bytes)
end

Instance Attribute Details

#memoryObject (readonly)

MemoryStream used to read and write data



111
112
113
# File 'lib/free-image/sources/memory.rb', line 111

def memory
  @memory
end

Instance Method Details

#formatObject

call-seq:

memory.format -> :format

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



150
151
152
153
154
# File 'lib/free-image/sources/memory.rb', line 150

def format
  result = FreeImage.FreeImage_GetFileTypeFromMemory(@memory, 0)
  FreeImage.check_last_error
  result
end

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

:call-seq:

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::Encoder module.  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


175
176
177
178
179
# 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