Class: Vips::Call

Inherits:
Object
  • Object
show all
Defined in:
lib/vips/call.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, supplied_values) ⇒ Call

Returns a new instance of Call.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vips/call.rb', line 11

def initialize(name, supplied_values)
    @name = name
    @supplied_values = supplied_values
    @instance = nil
    @option_string = nil

    if @supplied_values.last.is_a? Hash
        @optional_values = @supplied_values.last
        @supplied_values.delete_at -1
    else
        @optional_values = {}
    end

    begin
        @op = Vips::Operation.new @name
    rescue
        raise Vips::Error, "no operator '#{@name}'"
    end

    log "Vips::Call.init"
    log "name = #{@name}"
    log "supplied_values are:"
    @supplied_values.each {|x| log "   #{x}"}
    log "optional_values are:"
    @optional_values.each {|x| log "   #{x}"}
end

Instance Attribute Details

#instance=(value) ⇒ Object (writeonly)

Sets the attribute instance

Parameters:

  • value

    the value to set the attribute instance to.



9
10
11
# File 'lib/vips/call.rb', line 9

def instance=(value)
  @instance = value
end

#option_string=(value) ⇒ Object (writeonly)

Sets the attribute option_string

Parameters:

  • value

    the value to set the attribute option_string to.



9
10
11
# File 'lib/vips/call.rb', line 9

def option_string=(value)
  @option_string = value
end

Instance Method Details

#buildObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/vips/call.rb', line 170

def build
    log "building ..."

    op2 = Vips::cache_operation_lookup @op
    if op2
        log "cache hit"

        hit = true
        @op = op2
    else
        log "cache miss ... building"

        hit = false
        if @op.build() != 0
            raise Vips::Error
        end
        # showall

        log "adding to cache ... "
        Vips::cache_operation_add @op
    end

    return hit
end

#fetch_output(optional_output) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/vips/call.rb', line 195

def fetch_output(optional_output)
    log "fetching outputs ..."

    # gather output args 
    out = []

    all_args = @op.get_args
    all_args.each do |arg|
        # required output
        if (arg.flags & :output) != 0 and
            (arg.flags & :required) != 0 
            log "fetching required output #{arg.name}"
            out << arg.get_value
        end

        # modified input arg ... this will get the result of the 
        # copy() we did in Argument.set_value 
        if (arg.flags & :input) != 0 and
            (arg.flags & :modify) != 0 
            log "fetching modified input arg ..."
            out << arg.get_value
        end
    end

    opts = {}
    @optional_values.each do |name, value|
        # we are passed symbols as keys
        name = name.to_s
        if optional_output.has_key? name
            log "fetching optional output arg ..."
            opts[name] = optional_output[name].get_value
        end
    end
    out << opts if opts != {}

    if out.length == 1
        out = out[0]
    elsif out.length == 0
        out = nil
    end

    return out

end

#find_inputObject

look through the operation args and find required and optional input args



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/vips/call.rb', line 80

def find_input
    all_args = @op.get_args

    # find unassigned required input args
    log "finding unassigned required input arguments ..."
    required_input = all_args.select do |arg|
        not arg.isset and
        (arg.flags & :input) != 0 and
        (arg.flags & :required) != 0 
    end

    # find optional unassigned input args
    log "finding optional unassigned input arguments ..."
    optional_input = all_args.select do |arg|
        not arg.isset and
        (arg.flags & :input) != 0 and
        (arg.flags & :required) == 0 
    end

    # make a hash from name to arg for the options
    optional_input = Hash[
        optional_input.map(&:name).zip(optional_input)]

    return required_input, optional_input
end

#find_match_imageObject

set @match_image to be the image we build constants to match



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/vips/call.rb', line 50

def find_match_image
    # the instance, if supplied, must be a vips image ... we use it for
    # match_image, below
    if @instance and not @instance.is_a? Vips::Image
        raise Vips::Error, "@instance is not a Vips::Image."
    end

    # if the op needs images but the user supplies constants, we expand
    # them to match the first input image argument ... find the first
    # image
    log "searching for first image argument ..."
    match_image = @instance
    if match_image == nil
        match_image = @supplied_values.find {|x| x.is_a? Vips::Image}
    end
    if match_image == nil
        match = @optional_values.find do |name, value|
            value.is_a? Vips::Image
        end
        # if we found a match, it'll be [name, value]
        if match
            match_image = match[1]
        end
    end

    return match_image
end

#find_optional_outputObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/vips/call.rb', line 106

def find_optional_output
    all_args = @op.get_args

    log "finding optional output arguments ..."
    optional_output = all_args.select do |arg|
        (arg.flags & :output) != 0 and
        (arg.flags & :required) == 0 
    end
    optional_output = Hash[
        optional_output.map(&:name).zip(optional_output)]

    return optional_output

end

#invokeObject



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/vips/call.rb', line 240

def invoke
    set_string_args()
    match_image = find_match_image()
    required_input, optional_input = find_input()
    optional_output = find_optional_output()

    set_required_input(match_image, required_input)
    set_optional_input(match_image, optional_input, optional_output)

    hit = build()

    # if there was a cache hit, we need to refind this since all the arg
    # pointers will have changed
    if hit
        optional_output = find_optional_output()
    end
    out = fetch_output(optional_output)

    log "unreffing outputs ..."
    @op.unref_outputs()
    @op = nil
    # showall

    log "success! #{@name}.out = #{out}"

    return out
end

#set_optional_input(match_image, optional_input, optional_output) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/vips/call.rb', line 153

def set_optional_input(match_image, optional_input, optional_output)
    log "setting optional input args ..."
    @optional_values.each do |name, value|
        # we are passed symbols as keys
        name = name.to_s
        if optional_input.has_key? name
            log "setting #{name} to #{value}"
            optional_input[name].set_value match_image, value
        elsif optional_output.has_key? name and value != true
            raise Vips::Error, 
                "Optional output argument #{name} must be true."
        elsif not optional_output.has_key? name 
            raise Vips::Error, "No such option '#{name}',"
        end
    end
end

#set_required_input(match_image, required_input) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/vips/call.rb', line 121

def set_required_input(match_image, required_input)
    # do we have a non-nil instance? set the first image arg with this
    if @instance != nil
        log "setting first image arg with instance ..."
        x = required_input.find do |arg|
            gtype = GLib::Type["VipsImage"]
            vtype = arg.prop.value_type

            vtype.type_is_a? gtype
        end
        if x == nil
            raise Vips::Error, 
                "No #{@instance.class} argument to #{@name}."
        end
        x.set_value match_image, @instance
        required_input.delete x
    end

    if required_input.length != @supplied_values.length
        raise Vips::Error, 
            "Wrong number of arguments. '#{@name}' requires " +
            "#{required_input.length} arguments, you supplied " +
            "#{@supplied_values.length}."
    end

    log "setting required input arguments ..."
    required_input.zip(@supplied_values).each do |arg, value|
        arg.set_value match_image, value
    end

end

#set_string_argsObject

set any string options on the operation



39
40
41
42
43
44
45
46
47
# File 'lib/vips/call.rb', line 39

def set_string_args
    if @option_string
        log "setting string options #{@option_string} ..."

        if @op.set_from_string(@option_string) != 0
            raise Error
        end
    end
end