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
Attributes
memory | [R] |
MemoryStream used to read and write data |
Public Class methods
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. |
# File lib/free-image/sources/memory.rb, line 141 def initialize(bytes = nil) @memory = MemoryStream.new(bytes) end
Public Instance methods
Returns the image format for a memory stream. If the image format cannot be determined the :unknown will be returned.
# File lib/free-image/sources/memory.rb, line 150 def format result = FreeImage.FreeImage_GetFileTypeFromMemory(@memory, 0) FreeImage.check_last_error result end
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
# 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