class Vips::SourceCustom

A source you can attach action signal handlers to to implement custom input types.

For example:

“‘ruby 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 {Source#new_from_file} to read from a named file)

Public Class Methods

new() click to toggle source
Calls superclass method GObject::GObject::new
# File lib/vips/sourcecustom.rb, line 46
def initialize
  pointer = Vips::vips_source_custom_new
  raise Vips::Error if pointer.null?

  super pointer
end

Public Instance Methods

on_read(&block) click to toggle source

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.

@yieldparam length [Integer] Read and return up to this many bytes @yieldreturn [String] Up to length bytes of data, or nil for EOF

# 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(&block) click to toggle source

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, {Source} will treat your source like an unseekable pipe object and do extra caching.

@yieldparam offset [Integer] Seek offset @yieldparam whence [Integer] Seek whence @yieldreturn [Integer] the new read position, or -1 on error

# File lib/vips/sourcecustom.rb, line 83
def on_seek &block
  signal_connect "seek" do |offset, whence|
    block.call offset, whence
  end
end