Class: Vips::SourceCustom

Inherits:
Source show all
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

#ptr, #references

Instance Method Summary collapse

Methods inherited from Source

new_from_descriptor, new_from_file, new_from_memory

Methods inherited from Connection

#filename, #nick

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

#initializeSourceCustom

Returns a new instance of SourceCustom.

Raises:



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.

Yield Parameters:

  • length (Integer)

    Read and return up to this many bytes

Yield Returns:

  • (String)

    Up to length bytes of data, or nil for EOF



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.

Yield Parameters:

  • offset (Integer)

    Seek offset

  • whence (Integer)

    Seek whence

Yield Returns:

  • (Integer)

    the new read position, or -1 on error



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