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.



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

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

Instance Attribute Details

#memoryObject (readonly)

MemoryStream used to read and write data



121
122
123
# File 'lib/free-image/sources/memory.rb', line 121

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.



160
161
162
163
164
165
166
167
# File 'lib/free-image/sources/memory.rb', line 160

def format
  # Reset memory to start
  self.memory.seek(0, ::IO::SEEK_SET)

  result = FreeImage.FreeImage_GetFileTypeFromMemory(self.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 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


188
189
190
191
192
# File 'lib/free-image/sources/memory.rb', line 188

def save(bitmap, format, flags = 0)
  result = FreeImage.FreeImage_SaveToMemory(format, bitmap, self.memory, flags)
  FreeImage.check_last_error
  result
end