Skip to content

Commit

Permalink
Merge pull request #385 from libvips/add-ref-to-new-from-source
Browse files Browse the repository at this point in the history
new_from_source should ref the source object
  • Loading branch information
jcupitt authored Feb 18, 2024
2 parents dde924b + aaf7e33 commit f61f7a7
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 22 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## master

* `new_from_source` keeps a ref to the source object [taylorthurlow]
* some fixes to object references system

## Version 2.2.0 (2023-10-18)

* add `draw_point!` [jcupitt]
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.0
2.2.1
9 changes: 8 additions & 1 deletion lib/vips/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,14 @@ def self.new_from_source source, option_string, **opts
loader = Vips.vips_foreign_find_load_source source
raise Vips::Error if loader.nil?

Vips::Operation.call loader, [source], opts, option_string
image = Vips::Operation.call loader, [source], opts, option_string

# keep a secret ref to the source object ... the libvips loader will
# keep a ref to the C source object, but we need the ruby wrapper object
# to stay alive too
image.references << source

image
end

def self.matrix_from_array width, height, array
Expand Down
6 changes: 3 additions & 3 deletions lib/vips/interpolate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ class ManagedStruct < Vips::Object::ManagedStruct

def initialize name
name = name.to_s if name.is_a? Symbol
ptr = Vips.vips_interpolate_new name
raise Vips::Error if ptr.nil?
pointer = Vips.vips_interpolate_new name
raise Vips::Error if pointer.nil?

super ptr
super(pointer)
end
end
end
2 changes: 1 addition & 1 deletion lib/vips/mutableimage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def initialize(image)
# See also the comment on set_type! before changing this.
pointer = copy_image.ptr
::GObject.g_object_ref pointer
super pointer
super(pointer)

# and save the copy ready for when we finish mutating
@image = copy_image
Expand Down
25 changes: 15 additions & 10 deletions lib/vips/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def initialize value
raise Vips::Error if value.null?
end

super value
super(value)
end

def build
Expand Down Expand Up @@ -283,7 +283,7 @@ def set name, value, match_image, flags, gtype, destructive
value = value.map { |x| Operation.imageize match_image, x }
end

super name, value
super(name, value)
end

public
Expand Down Expand Up @@ -440,14 +440,12 @@ def self.call name, supplied, optional = {}, option_string = ""
end
end

# collect a list of all input references here
references = Set.new
# dedupe all input references here
deduped_references = Set.new

add_reference = lambda do |x|
if x.is_a?(Vips::Image)
x.references.each do |i|
references << i
end
deduped_references.merge x.references
end
false
end
Expand Down Expand Up @@ -482,20 +480,27 @@ def self.call name, supplied, optional = {}, option_string = ""

op = op.build

# we need an array of references for output objects
references = deduped_references.to_a

# attach all input refs to output x
set_reference = lambda do |x|
# stop early if there are no refs to attach
return true if references == []

if x.is_a? Vips::Image
x.references += references
references.each { |i| x.references << i }
end

false
end

# get all required results
result = []
required_output.each do |details|
value = details[:arg_name]
value = op.get(details[:arg_name])
flat_find value, &set_reference
result << op.get(value)
result << value
end

# fetch all optional ones
Expand Down
6 changes: 3 additions & 3 deletions lib/vips/region.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ class ManagedStruct < Vips::Object::ManagedStruct
end

def initialize(name)
ptr = Vips.vips_region_new name
raise Vips::Error if ptr.null?
pointer = Vips.vips_region_new name
raise Vips::Error if pointer.null?

super ptr
super(pointer)
end

def width
Expand Down
2 changes: 1 addition & 1 deletion lib/vips/sourcecustom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def initialize
pointer = Vips.vips_source_custom_new
raise Vips::Error if pointer.null?

super pointer
super(pointer)
end

# The block is executed to read data from the source. The interface is
Expand Down
2 changes: 1 addition & 1 deletion lib/vips/targetcustom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def initialize
pointer = Vips.vips_target_custom_new
raise Vips::Error if pointer.null?

super pointer
super(pointer)
end

# The block is executed to write data to the source. The interface is
Expand Down
2 changes: 1 addition & 1 deletion lib/vips/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Vips
VERSION = "2.2.0"
VERSION = "2.2.1"
end

0 comments on commit f61f7a7

Please sign in to comment.