-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathVipsOperation.php
386 lines (343 loc) · 11.8 KB
/
VipsOperation.php
1
2
3
4
5
6
7
8
9
10
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
37
38
39
40
41
42
43
44
45
46
47
48
49
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
77
78
79
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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
239
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php
/**
* Vips is a php binding for the vips image processing library
*
* PHP version 7
*
* LICENSE:
*
* Copyright (c) 2016 John Cupitt
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* @category Images
* @package Jcupitt\Vips
* @author John Cupitt <jcupitt@gmail.com>
* @copyright 2016 John Cupitt
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/libvips/php-vips
*/
namespace Jcupitt\Vips;
/**
* This class holds a pointer to a VipsOperation (the libvips operation base
* class) and manages argument introspection and operation call.
*
* @category Images
* @package Jcupitt\Vips
* @author John Cupitt <jcupitt@gmail.com>
* @copyright 2016 John Cupitt
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/libvips/php-vips
*/
class VipsOperation extends VipsObject
{
/**
* A pointer to the underlying VipsOperation. This is the same as the
* GObject, just cast to VipsOperation to help FFI.
*
* @internal
*/
public \FFI\CData $pointer;
/**
* Introspection data for this operation.
*/
public Introspect $introspect;
public function __construct(\FFI\CData $pointer)
{
$this->pointer = FFI::vips()->
cast(FFI::ctypes("VipsOperation"), $pointer);
parent::__construct($pointer);
}
/**
* @throws Exception
*/
public static function newFromName($name): VipsOperation
{
$pointer = FFI::vips()->vips_operation_new($name);
if ($pointer == null) {
throw new Exception();
}
$operation = new VipsOperation($pointer);
return $operation;
}
public function setMatch($name, $match_image, $value): void
{
$flags = $this->introspect->arguments[$name]["flags"];
$gtype = $this->introspect->arguments[$name]["type"];
if ($match_image != null) {
if ($gtype == FFI::gtypes("VipsImage")) {
$value = $match_image->imageize($value);
} elseif ($gtype == FFI::gtypes("VipsArrayImage") &&
is_array($value)) {
$new_value = [];
foreach ($value as $x) {
$new_value[] = $match_image->imageize($x);
}
$value = $new_value;
}
}
# MODIFY args need to be copied before they are set
if (($flags & ArgumentFlags::MODIFY) != 0) {
# logger.debug('copying MODIFY arg %s', name)
# make sure we have a unique copy
$value = $value->copyMemory();
}
parent::set($name, $value);
}
private static function introspect($name): Introspect
{
static $cache = [];
if (!array_key_exists($name, $cache)) {
$cache[$name] = new Introspect($name);
}
return $cache[$name];
}
private static function findInside($predicate, $x)
{
if ($predicate($x)) {
return $x;
}
if (is_array($x)) {
foreach ($x as $y) {
$result = self::findInside($predicate, $y);
if ($result != null) {
return $result;
}
}
}
return null;
}
/**
* Is $value a VipsImage.
*
* @param mixed $value The thing to test.
*
* @return bool true if this is a ffi VipsImage*.
*
* @internal
*/
private static function isImagePointer($value): bool
{
return $value instanceof \FFI\CData &&
\FFI::typeof($value) == FFI::ctypes("VipsImage");
}
/**
* Wrap up the result of a vips_ call ready to return it to PHP. We do
* two things:
*
* - If the array is a singleton, we strip it off. For example, many
* operations return a single result and there's no sense handling
* this as an array of values, so we transform ['out' => x] -> x.
*
* - Any VipsImage resources are rewrapped as instances of Image.
*
* @param mixed $result Wrap this up.
*
* @return mixed $result, but wrapped up as a php class.
*
* @internal
*/
private static function wrapResult($result)
{
if (!is_array($result)) {
$result = ['x' => $result];
}
array_walk_recursive($result, function (&$item) {
if (self::isImagePointer($item)) {
$item = new Image($item);
}
});
if (count($result) === 1) {
$result = array_shift($result);
}
return $result;
}
/**
* Call any vips operation. The final element of $arguments can be
* (but doesn't have to be) an array of options to pass to the operation.
*
* We can't have a separate arg for the options since this will be run from
* __call(), which cannot know which args are required and which are
* optional. See call() below for a version with the options broken out.
*
* @param string $operation_name The operation name.
* @param Image|null $instance The instance this operation is being invoked
* from.
* @param array $arguments An array of arguments to pass to the
* operation.
*
* @throws Exception
*
* @return mixed The result(s) of the operation.
*/
public static function callBase(
string $operation_name,
?Image $instance,
array $arguments
) {
Utils::debugLog($operation_name, [
'instance' => $instance,
'arguments' => $arguments
]);
$operation = self::newFromName($operation_name);
$operation->introspect = self::introspect($operation_name);
$introspect = $operation->introspect;
/* the first image argument is the thing we expand constants to
* match ... look inside tables for images, since we may be passing
* an array of images as a single param.
*/
if ($instance != null) {
$match_image = $instance;
} else {
$match_image = self::findInside(
fn($x) => $x instanceof Image,
$arguments
);
}
/* Because of the way php callStatic works, we can sometimes be given
* an instance even when no instance was given.
*
* We must loop over the required args and set them from the supplied
* args, using instance if required, and only check the nargs after
* this pass.
*/
$n_required = count($introspect->required_input);
$n_supplied = count($arguments);
$n_used = 0;
foreach ($introspect->required_input as $name) {
if ($name == $introspect->member_this) {
if (!$instance) {
$operation->unrefOutputs();
throw new Exception("instance argument not supplied");
}
$operation->setMatch($name, $match_image, $instance);
} elseif ($n_used < $n_supplied) {
$operation->setMatch($name, $match_image, $arguments[$n_used]);
$n_used += 1;
} else {
$operation->unrefOutputs();
throw new Exception("$n_required arguments required, " .
"but $n_supplied supplied");
}
}
/* If there's one extra arg and it's an array, use it as our options.
*/
$options = [];
if ($n_supplied == $n_used + 1 && is_array($arguments[$n_used])) {
$options = array_pop($arguments);
$n_supplied -= 1;
}
if ($n_supplied != $n_used) {
$operation->unrefOutputs();
throw new Exception("$n_required arguments required, " .
"but $n_supplied supplied");
}
/* set any string options before any args so they can't be
* overridden.
*/
if (array_key_exists("string_options", $options)) {
$string_options = $options["string_options"];
unset($options["string_options"]);
$operation->setString($string_options);
}
/* Set optional.
*/
foreach ($options as $name => $value) {
$name = str_replace("-", "_", $name);
if (!in_array($name, $introspect->optional_input) &&
!in_array($name, $introspect->optional_output)) {
$operation->unrefOutputs();
throw new Exception("optional argument '$name' does not exist");
}
$operation->setMatch($name, $match_image, $value);
}
/* Build the operation
*/
$pointer = FFI::vips()->
vips_cache_operation_build($operation->pointer);
if ($pointer == null) {
$operation->unrefOutputs();
throw new Exception();
}
$operation = new VipsOperation($pointer);
$operation->introspect = $introspect;
# TODO .. need to attach input refs to output, see _find_inside in
# pyvips
/* Fetch required output args (and modified input args).
*/
$result = [];
foreach ($introspect->required_output as $name) {
$result[$name] = $operation->get($name);
}
/* Any optional output args.
*/
$option_keys = array_keys($options);
foreach ($introspect->optional_output as $name) {
$name = str_replace("-", "_", $name);
if (in_array($name, $option_keys)) {
$result[$name] = $operation->get($name);
}
}
/* Free any outputs we've not used.
*/
$operation->unrefOutputs();
$result = self::wrapResult($result);
Utils::debugLog($operation_name, [
'result' => var_export($result, true)
]);
return $result;
}
/**
* Call any vips operation, with an explicit set of options. This is more
* convenient than callBase() if you have a set of known options.
*
* @param string $name The operation name.
* @param Image|null $instance The instance this operation is being invoked
* from.
* @param array $arguments An array of arguments to pass to the
* operation.
* @param array $options An array of optional arguments to pass to
* the operation.
*
* @throws Exception
*
* @return mixed The result(s) of the operation.
*/
public static function call(
string $name,
?Image $instance,
array $arguments,
array $options = []
) {
return self::callBase(
$name,
$instance,
array_merge($arguments, [$options])
);
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: expandtab sw=4 ts=4 fdm=marker
* vim<600: expandtab sw=4 ts=4
*/