Class: FreeImage::MemoryStream
- Inherits:
-
FFI::AutoPointer
- Object
- FFI::AutoPointer
- FreeImage::MemoryStream
- Defined in:
- lib/free-image/sources/memory.rb
Overview
Wrapper for a FreeImage memory stream which allows images to be read and written to memory. Memory streams are usefule for storing images as blobs in a database or writing them to an to a Internet stream.
Class Method Summary collapse
Instance Method Summary collapse
-
#bytes ⇒ Object
Returns the bytes of the memory stream.
-
#count ⇒ Object
Returns the size of the memory stream.
-
#initialize(bytes = nil) ⇒ MemoryStream
constructor
Create a new memory stream.
Constructor Details
#initialize(bytes = nil) ⇒ MemoryStream
Create a new memory stream.
Parameters
- bytes
-
If specified, a binary encoded Ruby string that stores image data. FreeImage will treat the string as read-only. If not specified, a writable MemoryStream is created.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/free-image/sources/memory.rb', line 48 def initialize(bytes = nil) ptr = if bytes buf = FFI::MemoryPointer.from_string(bytes) FreeImage.FreeImage_OpenMemory(buf, bytes.bytesize) else FreeImage.FreeImage_OpenMemory(nil, 0) end FreeImage.check_last_error super(ptr) end |
Class Method Details
.release(ptr) ⇒ Object
37 38 39 40 |
# File 'lib/free-image/sources/memory.rb', line 37 def self.release(ptr) FreeImage.FreeImage_CloseMemory(ptr) FreeImage.check_last_error end |
Instance Method Details
#bytes ⇒ Object
Returns the bytes of the memory stream.
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/free-image/sources/memory.rb', line 79 def bytes size = FFI::Type::CHAR.size # Reset memory to start FreeImage.FreeImage_SeekMemory(self, 0, ::IO::SEEK_SET) FreeImage.check_last_error buffer = FFI::MemoryPointer.new(FFI::Type::CHAR, size * count) FreeImage.check_last_error size = FreeImage.FreeImage_ReadMemory(buffer, size, count, self) buffer.null? ? nil : buffer.read_string end |
#count ⇒ Object
Returns the size of the memory stream.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/free-image/sources/memory.rb', line 61 def count # Store current position pos = FreeImage.FreeImage_TellMemory(self) FreeImage.check_last_error # Go to end of stream to get length FreeImage.FreeImage_SeekMemory(self, 0, ::IO::SEEK_END) FreeImage.check_last_error count = FreeImage.FreeImage_TellMemory(self) # Restore position FreeImage.FreeImage_SeekMemory(self, pos, ::IO::SEEK_SET) FreeImage.check_last_error count end |