Class: Vips::SourceCustom
- Inherits:
-
Source
- Object
- GObject::GObject
- Object
- Connection
- Source
- Vips::SourceCustom
- Defined in:
- lib/vips/sourcecustom.rb
Overview
A source you can attach action signal handlers to to implement custom input types.
For example:
file = File.open "some/file/name", "rb"
source = Vips::SourceCustom.new
source.on_read { |length| file.read length }
image = Vips::Image.new_from_source source
(just an example – of course in practice you'd use Vips::Source.new_from_file to read from a named file)
Defined Under Namespace
Modules: SourceCustomLayout Classes: ManagedStruct, Struct
Instance Attribute Summary
Attributes inherited from GObject::GObject
Instance Method Summary collapse
-
#initialize ⇒ SourceCustom
constructor
A new instance of SourceCustom.
-
#on_read {|length| ... } ⇒ Object
The block is executed to read data from the source.
-
#on_seek {|offset, whence| ... } ⇒ Object
The block is executed to seek the source.
Methods inherited from Source
new_from_descriptor, new_from_file, new_from_memory
Methods inherited from Connection
Methods inherited from Object
#get, #get_pspec, #get_typeof, #get_typeof_error, print_all, #set, #signal_connect
Methods inherited from GObject::GObject
#ffi_managed_struct, ffi_managed_struct, #ffi_struct, ffi_struct
Constructor Details
#initialize ⇒ SourceCustom
Returns a new instance of SourceCustom.
46 47 48 49 50 51 |
# File 'lib/vips/sourcecustom.rb', line 46 def initialize pointer = Vips.vips_source_custom_new raise Vips::Error if pointer.null? super pointer end |
Instance Method Details
#on_read {|length| ... } ⇒ Object
The block is executed to read data from the source. The interface is exactly as IO::read, ie. it takes a maximum number of bytes to read and returns a string of bytes from the source, or nil if the source is already at end of file.
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/vips/sourcecustom.rb', line 60 def on_read &block signal_connect "read" do |buf, len| chunk = block.call len return 0 if chunk.nil? bytes_read = chunk.bytesize buf.put_bytes(0, chunk, 0, bytes_read) chunk.clear bytes_read end end |
#on_seek {|offset, whence| ... } ⇒ Object
The block is executed to seek the source. The interface is exactly as IO::seek, ie. it should take an offset and whence, and return the new read position.
This handler is optional – if you do not attach a seek handler, Vips::Source will treat your source like an unseekable pipe object and do extra caching.
83 84 85 86 87 |
# File 'lib/vips/sourcecustom.rb', line 83 def on_seek &block signal_connect "seek" do |offset, whence| block.call offset, whence end end |