Class: Vips::MutableImage

Inherits:
Object show all
Extended by:
Forwardable
Defined in:
lib/vips/mutableimage.rb

Overview

This class represents a libvips image which can be modified. See Image#mutate.

Defined Under Namespace

Modules: MutableImageLayout Classes: ManagedStruct, Struct

Instance Attribute Summary collapse

Attributes inherited from GObject::GObject

#ptr, #references

Instance Method Summary collapse

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(image) ⇒ MutableImage

Make a Vips::MutableImage from a regular Image.

This is for internal use only. See Image#mutate for the user-facing interface.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/vips/mutableimage.rb', line 50

def initialize(image)
  # We take a copy of the regular Image to ensure we have an unshared
  # (unique) object. We forward things like #width and #height to this, and
  # it's the thing we return at the end of the mutate block.
  copy_image = image.copy

  # use ptr since we need the raw unwrapped pointer inside the image ...
  # and make the ref that gobject will unref when it finishes
  pointer = copy_image.ptr
  ::GObject.g_object_ref pointer
  super pointer

  # and save the copy ready for when we finish mutating
  @image = copy_image
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **options) ⇒ Object

Invoke a vips operation with Operation#call, using self as the first input argument. Operation#call will only allow operations that modify self when passed a Vips::MutableImage.

Parameters:

  • name (String)

    vips operation to call

Returns:

  • result of vips operation



94
95
96
# File 'lib/vips/mutableimage.rb', line 94

def method_missing name, *args, **options
  Vips::Operation.call name.to_s, [self, *args], options
end

Instance Attribute Details

#imageObject (readonly)

Get the Image this Vips::MutableImage is modifying. Only use this once you have finished all modifications.

This is for internal use only. See Image#mutate for the user-facing interface.



44
45
46
# File 'lib/vips/mutableimage.rb', line 44

def image
  @image
end

Instance Method Details

#inspectObject



66
67
68
# File 'lib/vips/mutableimage.rb', line 66

def inspect
  "#<MutableImage #{width}x#{height} #{format}, #{bands} bands, #{interpretation}>"
end

#remove!(name) ⇒ Object

Remove a metadata item from an image.

For example:

x.remove! "icc-profile-data"

Parameters:

  • name (String)

    Metadata field to remove



150
151
152
# File 'lib/vips/mutableimage.rb', line 150

def remove! name
  Vips.vips_image_remove self, name
end

#respond_to?(name, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
79
# File 'lib/vips/mutableimage.rb', line 70

def respond_to? name, include_all = false
  # To support keyword args, we need to tell Ruby that final image
  # arguments cannot be hashes of keywords.
  #
  # https://makandracards.com/makandra/
  #   36013-heads-up-ruby-implicitly-converts-a-hash-to-keyword-arguments
  return false if name == :to_hash

  super
end

#respond_to_missing?(name, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
# File 'lib/vips/mutableimage.rb', line 81

def respond_to_missing? name, include_all = false
  # respond to all vips operations by nickname
  return true if Vips.type_find("VipsOperation", name.to_s) != 0

  super
end

#set!(name, value) ⇒ Object

Set the value of a metadata item on an image. The metadata item must already exist. Ruby types are automatically transformed into the matching GObject::GValue, if possible.

For example, you can use this to set an image's ICC profile:

x.set! "icc-profile-data", profile

where profile is an ICC profile held as a binary string object.

Parameters:

  • name (String)

    Metadata field to set

  • value (Object)

    Value to set

See Also:



137
138
139
# File 'lib/vips/mutableimage.rb', line 137

def set! name, value
  set_type! get_typeof(name), name, value
end

#set_type!(gtype, name, value) ⇒ Object

Create a metadata item on an image of the specifed type. Ruby types are automatically transformed into the matching glib type (eg. GObject::GINT_TYPE), if possible.

For example, you can use this to set an image's ICC profile:

x.set_type! Vips::BLOB_TYPE, "icc-profile-data", profile

where profile is an ICC profile held as a binary string object.

Parameters:

  • gtype (Integer)

    GType of item

  • name (String)

    Metadata field to set

  • value (Object)

    Value to set

See Also:



114
115
116
117
118
119
120
# File 'lib/vips/mutableimage.rb', line 114

def set_type! gtype, name, value
  gvalue = GObject::GValue.alloc
  gvalue.init gtype
  gvalue.set value
  Vips.vips_image_set self, name, gvalue
  gvalue.unset
end