-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathVipsObject.php
194 lines (170 loc) · 5.45 KB
/
VipsObject.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
<?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 VipsObject (the libvips base class) and
* manages properties.
*
* @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
*/
abstract class VipsObject extends GObject
{
/**
* A pointer to the underlying VipsObject.
*
* @internal
*/
private \FFI\CData $pointer;
/**
* A pointer to the underlying GObject. This is the same as the
* VipsObject, just cast.
*
* @internal
*/
private \FFI\CData $gObject;
public function __construct(\FFI\CData $pointer)
{
$this->pointer = \FFI::cast(FFI::ctypes("VipsObject"), $pointer);
$this->gObject = \FFI::cast(FFI::ctypes("GObject"), $pointer);
parent::__construct($pointer);
}
// print a table of all active vipsobjects ... handy for debugging
public static function printAll(): void
{
FFI::vips()->vips_object_print_all();
}
public function getDescription(): string
{
return FFI::vips()->vips_object_get_description($this->pointer);
}
// get the pspec for a property
// NULL for no such name
// very slow! avoid if possible
// FIXME add a cache for this thing
public function getPspec(string $name): ?\FFI\CData
{
$name = str_replace("-", "_", $name);
$pspec = FFI::gobject()->new("GParamSpec*[1]");
$argument_class = FFI::vips()->new("VipsArgumentClass*[1]");
$argument_instance = FFI::vips()->new("VipsArgumentInstance*[1]");
$result = FFI::vips()->vips_object_get_argument(
$this->pointer,
$name,
$pspec,
$argument_class,
$argument_instance
);
if ($result != 0) {
return null;
} else {
return $pspec[0];
}
}
// get the type of a property from a VipsObject
// 0 if no such property
public function getType(string $name): int
{
$pspec = $this->getPspec($name);
if (\FFI::isNull($pspec)) {
# need to clear any error, this is horrible
FFI::vips()->vips_error_clear();
return 0;
} else {
return $pspec->value_type;
}
}
public function getBlurb(string $name): string
{
$pspec = $this->getPspec($name);
return FFI::gobject()->g_param_spec_get_blurb($pspec);
}
public function getArgumentDescription(string $name): string
{
$pspec = $this->getPspec($name);
return FFI::gobject()->g_param_spec_get_description($pspec);
}
/**
* @throws Exception
*/
public function get(string $name)
{
$name = str_replace("-", "_", $name);
$gvalue = new GValue();
$gvalue->setType($this->getType($name));
FFI::gobject()->
g_object_get_property($this->gObject, $name, $gvalue->pointer);
$value = $gvalue->get();
Utils::debugLog("get", [$name => var_export($value, true)]);
return $value;
}
/**
* @throws Exception
*/
public function set(string $name, $value): void
{
Utils::debugLog("set", [$name => $value]);
$name = str_replace("-", "_", $name);
$gvalue = new GValue();
$gvalue->setType($this->getType($name));
$gvalue->set($value);
FFI::gobject()->
g_object_set_property($this->gObject, $name, $gvalue->pointer);
}
public function setString(string $string_options): bool
{
$result = FFI::vips()->
vips_object_set_from_string($this->pointer, $string_options);
return $result == 0;
}
public function unrefOutputs(): void
{
FFI::vips()->vips_object_unref_outputs($this->pointer);
}
}
/*
* 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
*/