Class: Vips::TargetCustom

Inherits:
Target show all
Defined in:
lib/vips/targetcustom.rb

Overview

A target you can attach action signal handlers to to implememt custom output types.

For example:

file = File.open "some/file/name", "wb"
target = Vips::TargetCustom.new
target.on_write { |bytes| file.write bytes }
image.write_to_target target, ".png"

(just an example -- of course in practice you'd use Vips::Target.new_to_file to write to a named file)

Defined Under Namespace

Modules: TargetCustomLayout Classes: ManagedStruct, Struct

Instance Method Summary collapse

Methods inherited from Target

new_to_descriptor, new_to_file, new_to_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

#initializeTargetCustom

Returns a new instance of TargetCustom.

Raises:



46
47
48
49
50
51
# File 'lib/vips/targetcustom.rb', line 46

def initialize
  pointer = Vips::vips_target_custom_new
  raise Vips::Error if pointer.null?

  super pointer
end

Instance Method Details

#on_finish(&block) ⇒ Object

The block is executed at the end of write. It should do any necessary finishing action, such as closing a file.



71
72
73
74
75
# File 'lib/vips/targetcustom.rb', line 71

def on_finish &block
  signal_connect "finish" do 
    block.call()
  end
end

#on_write {|bytes| ... } ⇒ Object

The block is executed to write data to the source. The interface is exactly as IO::write, ie. it should write the string and return the number of bytes written.

Yield Parameters:

  • bytes (String)

    Write these bytes to the file

Yield Returns:

  • (Integer)

    The number of bytes written, or -1 on error



59
60
61
62
63
64
65
66
67
# File 'lib/vips/targetcustom.rb', line 59

def on_write &block
  signal_connect "write" do |p, len|
    chunk = p.get_bytes(0, len)
    bytes_written = block.call chunk
    chunk.clear

    bytes_written
  end
end