/usr/share/gtk-doc/html/libvips/extending.html is in libvips-doc 8.2.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Extending VIPS: VIPS Reference Manual</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="VIPS Reference Manual">
<link rel="up" href="ch01.html" title="VIPS Overview">
<link rel="prev" href="binding.html" title="Writing bindings for libvips">
<link rel="next" href="func-list.html" title="VIPS function list">
<meta name="generator" content="GTK-Doc V1.24 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
<td width="100%" align="left" class="shortcuts"></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
<td><a accesskey="u" href="ch01.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
<td><a accesskey="p" href="binding.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
<td><a accesskey="n" href="func-list.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
</tr></table>
<div class="refentry">
<a name="extending"></a><div class="titlepage"></div>
<div class="refnamediv"><table width="100%"><tr>
<td valign="top">
<h2><span class="refentrytitle">Extending VIPS</span></h2>
<p>Extending — How to add operations to VIPS</p>
</td>
<td class="gallery_image" valign="top" align="right"></td>
</tr></table></div>
<div class="refsect3">
<a name="extending-intro"></a><h4>Introduction</h4>
<p>
This section runs quickly through adding a simple operator to VIPS.
For more information, see <span class="type">VipsOperation</span> and <span class="type">VipsRegion</span>. A good
starting point for a new operation is a similar one in the VIPS library.
</p>
<p>
All VIPS operations are subclasses of <span class="type">VipsOperation</span>, which in turn
subclasses <span class="type">VipsObject</span> and then <code class="literal">GObject</code>. You add an operation to VIPS
by defining a new subclass of <span class="type">VipsOperation</span> and arranging for its
<code class="code"><code class="function">class_init()</code></code> to be called, perhaps by calling its <code class="function">get_type()</code>
function.
</p>
</div>
<div class="refsect3">
<a name="extending-classstruct"></a><h4>The class and object structures</h4>
<p>
First you need to define a new
object struct and a new class struct.
</p>
<pre class="programlisting">
typedef struct _Negative {
VipsOperation parent_instance;
VipsImage *in;
VipsImage *out;
int image_max;
} Negative;
typedef struct _NegativeClass {
VipsOperationClass parent_class;
/* No new class members needed for this op.
*/
} NegativeClass;
</pre>
<p>
</p>
<p>
This operation will find the photographic negative of an unsigned
8-bit image, optionally letting you specify the value which the pixels
"pivot" about. It doesn't need any class members (ie. values common
to all operations of this type), so the second struct is empty. See
the source to <a class="link" href="libvips-arithmetic.html#vips-invert" title="vips_invert ()"><code class="function">vips_invert()</code></a> for a more complete version of this
operation that's actually in the library.
</p>
<p>
<code class="literal">GObject</code> has a handy macro to write some of the boilerplate for you.
</p>
<pre class="programlisting">
G_DEFINE_TYPE( Negative, negative, VIPS_TYPE_OPERATION );
</pre>
<p>
<code class="function">G_DEFINE_TYPE()</code> defines a function called <code class="function">negative_get_type()</code>,
which registers this new class and returns its <code class="literal">GType</code> (a
pointer-sized integer). <code class="function">negative_get_type()</code> in turn needs two
functions, <code class="function">negative_init()</code>, to initialise a new instance, and
<code class="function">negative_class_init()</code>, to initialise a new class.
</p>
</div>
<div class="refsect3">
<a name="extending-init"></a><h4>Class and object initialisation</h4>
<p>
<code class="function">negative_init()</code> is very simple, it just sets the default value for
our optional parameter.
</p>
<pre class="programlisting">
static void
negative_init( Negative *negative )
{
negative->image_max = 255;
}
</pre>
<p>
</p>
<p>
<code class="function">negative_class_init()</code> is more complicated: it has to set various
fields in various superclasses and define the operation's parameters.
</p>
<pre class="programlisting">
static void
negative_class_init( NegativeClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = VIPS_OBJECT_CLASS( class );
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "negative";
object_class->description = "photographic negative";
object_class->build = negative_build;
VIPS_ARG_IMAGE( class, "in", 1,
"Input",
"Input image",
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( Negative, in ) );
VIPS_ARG_IMAGE( class, "out", 2,
"Output",
"Output image",
VIPS_ARGUMENT_REQUIRED_OUTPUT,
G_STRUCT_OFFSET( Negative, out ) );
VIPS_ARG_INT( class, "image_max", 4,
"Image maximum",
"Maximum value in image: pivot about this",
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( Negative, image_max ),
0, 255, 255 );
}
</pre>
<p>
</p>
<p>
In <code class="literal">GObject</code>, it needs to set the getters and setters for this class. vips
has a generic get/set system, so any subclass of <span class="type">VipsObject</span> needs to
use the vips ones.
</p>
<p>
In <span class="type">VipsObject</span>, it needs to set the operation <em class="parameter"><code>nickname</code></em> and <em class="parameter"><code>description</code></em>,
and set a build function (see below). <em class="parameter"><code>nickname</code></em> is used to refer to
this operation in the API, <em class="parameter"><code>description</code></em> is used to explain this
operation to users and will be translated into their language.
</p>
<p>
Finally, it needs to define the arguments the constructor for this class
takes. There are a set of handy macros for doing this, see
<a class="link" href="libvips-VipsObject.html#VIPS-ARG-INT:CAPS" title="VIPS_ARG_INT()"><code class="function">VIPS_ARG_INT()</code></a> and friends.
</p>
<p>
The first few
parameters are always the same and mean: class pointer for argument,
argument name, argument priority (bindings expect required arguments in
order of priority), long argument name (this one is internationalised
and displayed to users), description (again, users can see this),
some flags describing the argument, and finally the position of the
member in the struct.
</p>
<p>
Integer arguments take three more values: the minimum, maximum and
default value for the argument.
</p>
</div>
<div class="refsect3">
<a name="extending-build"></a><h4>The <code class="function">build()</code> function</h4>
<p>
The build function is the thing <span class="type">VipsObject</span> calls during object
construction, after all arguments have been supplied and before the
object is used. It has two roles: to verify that arguments are correct,
and then to construct the object. After <code class="function">build()</code>, the object is expected
to be ready for use.
</p>
<pre class="programlisting">
static int
negative_build( VipsObject *object )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
Negative *negative = (Negative *) object;
if( VIPS_OBJECT_CLASS( negative_parent_class )->build( object ) )
return( -1 );
if( vips_check_uncoded( class->nickname, negative->in ) ||
vips_check_format( class->nickname, negative->in, VIPS_FORMAT_UCHAR ) )
return( -1 );
g_object_set( object, "out", vips_image_new(), NULL );
if( vips_image_pipelinev( negative->out,
VIPS_DEMAND_STYLE_THINSTRIP, negative->in, NULL ) )
return( -1 );
if( vips_image_generate( negative->out,
vips_start_one,
negative_generate,
vips_stop_one,
negative->in, negative ) )
return( -1 );
return( 0 );
}
</pre>
<p>
</p>
<p>
<code class="function">negative_build()</code> first chains up to the superclass: this will check
that all input arguments have been supplied and are sane.
</p>
<p>
Next, it adds its own checks. This is a demo operation, so we just
work for uncoded, unsigned 8-bit images. There are a lot of
convenience functions like <a class="link" href="libvips-error.html#vips-check-format" title="vips_check_format ()"><code class="function">vips_check_format()</code></a>, see the docs.
</p>
<p>
Next, it creates the output image. This needs to be set with
<code class="function">g_object_set()</code> so that vips can see that it has been assigned. vips
will also handle the reference counting for you.
</p>
<p>
<a class="link" href="libvips-generate.html#vips-image-pipelinev" title="vips_image_pipelinev ()"><code class="function">vips_image_pipelinev()</code></a> links our new image onto the input image and
notes that this operation prefers to work in lines. You can request
other input geometries, see <a class="link" href="libvips-VipsImage.html#VipsDemandStyle" title="enum VipsDemandStyle"><span class="type">VipsDemandStyle</span></a>.
</p>
<p>
The geometry hint is just a hint, an operation needs to be able to
supply any size
<span class="type">VipsRegion</span> on request. If you must have a certain size request, you can
put a cache in the pipeline after your operation, see <a class="link" href="libvips-conversion.html#vips-linecache" title="vips_linecache ()"><code class="function">vips_linecache()</code></a>
and <a class="link" href="libvips-conversion.html#vips-tilecache" title="vips_tilecache ()"><code class="function">vips_tilecache()</code></a>. You can also make requests to your operation
ordered, see <a class="link" href="libvips-conversion.html#vips-sequential" title="vips_sequential ()"><code class="function">vips_sequential()</code></a>.
</p>
<p>
Finally, <a class="link" href="libvips-generate.html#vips-image-generate" title="vips_image_generate ()"><code class="function">vips_image_generate()</code></a> attaches a set of callbacks to the
output image to generate chunks of it on request. <a class="link" href="libvips-generate.html#vips-start-one" title="vips_start_one ()"><code class="function">vips_start_one()</code></a>
and <a class="link" href="libvips-generate.html#vips-stop-one" title="vips_stop_one ()"><code class="function">vips_stop_one()</code></a> are convenience functions that make the input
region for you, see below.
</p>
</div>
<div class="refsect3">
<a name="extending-gen"></a><h4>The <code class="function">generate()</code> function</h4>
<p>
The <code class="function">generate()</code> function does the actual image processing.
<code class="function">negative_generate()</code> (of type <a class="link" href="libvips-VipsImage.html#VipsGenerateFn" title="VipsGenerateFn ()"><span class="type">VipsGenerateFn</span></a>, supplied to
<a class="link" href="libvips-generate.html#vips-image-generate" title="vips_image_generate ()"><code class="function">vips_image_generate()</code></a> above) is
called whenever some pixels of our output image are required.
</p>
<pre class="programlisting">
static int
negative_generate( VipsRegion *or,
void *vseq, void *a, void *b, gboolean *stop )
{
/* The area of the output region we have been asked to make.
*/
VipsRect *r = &or->valid;
/* The sequence value ... the thing returned by vips_start_one().
*/
VipsRegion *ir = (VipsRegion *) vseq;
VipsImage *in = (VipsImage *) a;
Negative *negative = (Negative *) b;
int line_size = r->width * negative->in->Bands;
int x, y;
/* Request matching part of input region.
*/
if( vips_region_prepare( ir, r ) )
return( -1 );
for( y = 0; y < r->height; y++ ) {
unsigned char *p = (unsigned char *)
VIPS_REGION_ADDR( ir, r->left, r->top + y );
unsigned char *q = (unsigned char *)
VIPS_REGION_ADDR( or, r->left, r->top + y );
for( x = 0; x < line_size; x++ )
q[x] = negative->image_max - p[x];
}
return( 0 );
}
</pre>
<p>
</p>
<p>
This has to calculate a section of the output image. The output
<span class="type">VipsRegion</span>, <em class="parameter"><code>or</code></em>, contains a <a class="link" href="libvips-rect.html#VipsRect" title="VipsRect"><span class="type">VipsRect</span></a> called <em class="parameter"><code>valid</code></em> which is the
area needing calculation. This call to <code class="function">negative_generate()</code> must
somehow make this part of <em class="parameter"><code>or</code></em> contain pixel data.
</p>
<p>
<em class="parameter"><code>vseq</code></em> is the sequence value. This is the
per-thread state for this generate, created (in this example) by
<a class="link" href="libvips-generate.html#vips-start-one" title="vips_start_one ()"><code class="function">vips_start_one()</code></a>. In this simple case it's just a <span class="type">VipsRegion</span> defined on
the input image. If you need more per-thread state you can write your
own start and stop functions and have a struct you create and pass as a
sequence value. There are plenty of examples in the VIPS source code,
see <a class="link" href="libvips-morphology.html#vips-rank" title="vips_rank ()"><code class="function">vips_rank()</code></a>.
</p>
<p>
<em class="parameter"><code>a</code></em> and <em class="parameter"><code>b</code></em> are the last two arguments to <a class="link" href="libvips-generate.html#vips-image-generate" title="vips_image_generate ()"><code class="function">vips_image_generate()</code></a> above.
<em class="parameter"><code>stop</code></em> is a bool pointer you can set to stop computation early. <a class="link" href="libvips-arithmetic.html#vips-min" title="vips_min ()"><code class="function">vips_min()</code></a>
on an unsigned int image, for example, will set <em class="parameter"><code>stop</code></em> as soon as it sees
a zero, and will not scan the entire image.
</p>
<p>
The first thing <code class="function">negative_generate()</code> does is
use <a class="link" href="libvips-VipsRegion.html#vips-region-prepare" title="vips_region_prepare ()"><code class="function">vips_region_prepare()</code></a> to
ask for the corresponding pixels from the input image. Operations which
do coordinate transforms or which need an area of input for each output
point will need to calculate a new rect before calling
<a class="link" href="libvips-VipsRegion.html#vips-region-prepare" title="vips_region_prepare ()"><code class="function">vips_region_prepare()</code></a>.
</p>
<p>
Finally, it can calculate some pixels. <code class="function">negative_generate()</code> loops
over the valid area of the output and calls <a class="link" href="libvips-VipsRegion.html#VIPS-REGION-ADDR:CAPS" title="VIPS_REGION_ADDR()"><code class="function">VIPS_REGION_ADDR()</code></a> for each
line. This macro is reasonaly quick, but it's best not to call it for
each pixel. Once per line is fine though.
</p>
</div>
<div class="refsect3">
<a name="extending-add"></a><h4>Adding to VIPS</h4>
<p>
To add the operation to vips, just call <code class="function">negative_get_type()</code>. You can
include the source in your program, or use <code class="literal">GModule</code> to make a binary
plugin that will be loaded by libvips at startup. There are some <a class="ulink" href="https://github.com/jcupitt/vips-gmic" target="_top">example
plugins available</a>.
</p>
<p>
You
can then use <em class="parameter"><code>negative</code></em> from any of the vips interfaces. For example,
in Python you'd use it like this:
</p>
<pre class="programlisting">
out = in.negative(image_max = 128)
</pre>
<p>
</p>
<p>
From the command-line it'd look like this:
</p>
<pre class="programlisting">
$ vips negative in.png out.tif --image-max 128
</pre>
<p>
</p>
<p>
And from C like this:
</p>
<pre class="programlisting">
VipsImage *in;
VipsImage *out;
if( vips_call( "negative", in, &out, "image_max", 128, NULL ) )
... error
</pre>
<p>
</p>
<p>
Unfortunately that will do almost no compile-time type checking,
so all vips operations have a tiny extra wrapper to add a bit of
safety. For example:
</p>
<pre class="programlisting">
static int
negative( VipsImage *in, VipsImage **out, ... )
{
va_list ap;
int result;
va_start( ap, out );
result = vips_call_split( "negative", ap, in, out );
va_end( ap );
return( result );
}
</pre>
<p>
</p>
<p>
And now you can write:
</p>
<pre class="programlisting">
if( negative( in, &out, "image_max", 128, NULL ) )
... error
</pre>
<p>
and it's at least a bit safer.
</p>
</div>
<div class="refsect3">
<a name="extending-othertypes"></a><h4>Other types of operation</h4>
<p>
Change the <code class="function">_build()</code> function to make other types of operation.
</p>
<p>
Use <a class="link" href="libvips-generate.html#vips-image-generate" title="vips_image_generate ()"><code class="function">vips_image_generate()</code></a> with <a class="link" href="libvips-generate.html#vips-start-many" title="vips_start_many ()"><code class="function">vips_start_many()</code></a> to make operations
which demand pixels from more than one image at once, such as image
plus image.
</p>
<p>
Use <a class="link" href="libvips-generate.html#vips-sink" title="vips_sink ()"><code class="function">vips_sink()</code></a> instead of <a class="link" href="libvips-generate.html#vips-image-generate" title="vips_image_generate ()"><code class="function">vips_image_generate()</code></a> to loop over an image
and calculate a value. vips uses this for the statistics operations,
like <a class="link" href="libvips-arithmetic.html#vips-avg" title="vips_avg ()"><code class="function">vips_avg()</code></a>.
</p>
<p>
Use <a class="link" href="libvips-VipsImage.html#vips-image-wio-input" title="vips_image_wio_input ()"><code class="function">vips_image_wio_input()</code></a> to get an entire image into memory so you
can read it with a pointer. This will obviously not scale well to
very large images, but some operations, like FFTs or flood-fill, need
the whole image to be available at once.
</p>
<p>
Make area operations, like filters, by enlarging the <a class="link" href="libvips-rect.html#VipsRect" title="VipsRect"><span class="type">VipsRect</span></a> that
<code class="function">_generate()</code> is given before calling <a class="link" href="libvips-VipsRegion.html#vips-region-prepare" title="vips_region_prepare ()"><code class="function">vips_region_prepare()</code></a>. You can
enlarge the input image, so that the output image is the same size as
the original input, by using <a class="link" href="libvips-conversion.html#vips-embed" title="vips_embed ()"><code class="function">vips_embed()</code></a> within the <code class="function">_build()</code> function.
</p>
<p>
Make things like flips and rotates by making larger changes to the
<a class="link" href="libvips-rect.html#VipsRect" title="VipsRect"><span class="type">VipsRect</span></a> in <code class="function">_generate()</code>.
</p>
<p>
Make zero-copy operations, like <a class="link" href="libvips-conversion.html#vips-insert" title="vips_insert ()"><code class="function">vips_insert()</code></a>, with <a class="link" href="libvips-VipsRegion.html#vips-region-region" title="vips_region_region ()"><code class="function">vips_region_region()</code></a>.
</p>
</div>
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.24</div>
</body>
</html>
|