2014-07-25 10:22:49 +02:00
|
|
|
<?xml version="1.0"?>
|
2014-11-16 19:01:19 +01:00
|
|
|
<!-- vim: set ts=2 sw=2 expandtab: -->
|
2014-07-25 10:22:49 +02:00
|
|
|
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
|
|
|
|
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
|
|
|
|
]>
|
|
|
|
<refentry id="using-from-c">
|
|
|
|
<refmeta>
|
2014-08-06 22:59:13 +02:00
|
|
|
<refentrytitle>VIPS from C</refentrytitle>
|
2014-07-25 10:22:49 +02:00
|
|
|
<manvolnum>3</manvolnum>
|
|
|
|
<refmiscinfo>VIPS Library</refmiscinfo>
|
|
|
|
</refmeta>
|
|
|
|
|
|
|
|
<refnamediv>
|
|
|
|
<refname>Using VIPS</refname>
|
2014-08-06 22:59:13 +02:00
|
|
|
<refpurpose>How to use the VIPS library from C</refpurpose>
|
2014-07-25 10:22:49 +02:00
|
|
|
</refnamediv>
|
|
|
|
|
2015-02-23 13:32:56 +01:00
|
|
|
<refsect3 id="using-C-intro">
|
2014-11-16 13:57:27 +01:00
|
|
|
<title>Introduction</title>
|
2014-07-25 10:22:49 +02:00
|
|
|
<para>
|
|
|
|
VIPS comes with a convenient, high-level C API. You should read the API
|
|
|
|
docs for full details, but this section will try to give a brief
|
2016-01-25 10:35:06 +01:00
|
|
|
overview.
|
2014-07-26 12:19:46 +02:00
|
|
|
</para>
|
2015-02-23 13:32:56 +01:00
|
|
|
</refsect3>
|
2014-07-25 10:22:49 +02:00
|
|
|
|
2015-02-23 13:32:56 +01:00
|
|
|
<refsect3 id="using-C-startup">
|
2015-01-28 12:37:16 +01:00
|
|
|
<title>Library startup</title>
|
2014-07-26 12:19:46 +02:00
|
|
|
<para>
|
2015-01-28 12:37:16 +01:00
|
|
|
When your program starts, use VIPS_INIT()
|
2014-11-16 13:57:27 +01:00
|
|
|
to start up the VIPS library. You should pass it the name
|
|
|
|
of your program, usually <code>argv[0]</code>. Use
|
2015-02-18 17:02:19 +01:00
|
|
|
vips_shutdown() when you exit. VIPS_INIT() is a macro to let it check
|
|
|
|
that the libvips library you have linked to matches the libvips headers
|
|
|
|
you included.
|
2014-07-26 12:19:46 +02:00
|
|
|
</para>
|
2014-07-25 10:22:49 +02:00
|
|
|
|
2014-07-26 12:19:46 +02:00
|
|
|
<para>
|
2014-08-19 18:57:27 +02:00
|
|
|
You can add the VIPS flags to your %GObject command-line processing
|
2016-01-25 10:35:06 +01:00
|
|
|
with vips_add_option_entries().
|
2014-07-26 12:19:46 +02:00
|
|
|
</para>
|
2015-02-23 13:32:56 +01:00
|
|
|
</refsect3>
|
2014-07-25 10:22:49 +02:00
|
|
|
|
2015-02-23 13:32:56 +01:00
|
|
|
<refsect3 id="using-C-vipsimage">
|
2015-01-28 12:37:16 +01:00
|
|
|
<title>The #VipsImage class</title>
|
2014-07-26 12:19:46 +02:00
|
|
|
<para>
|
2014-11-16 19:01:19 +01:00
|
|
|
The basic data object is the #VipsImage. You can create an
|
|
|
|
image from a file on disc or from an area of memory, either
|
|
|
|
as a C-style array, or as a formatted object, like JPEG. See
|
|
|
|
vips_image_new_from_file() and friends. Loading an
|
2015-01-28 12:37:16 +01:00
|
|
|
image is fast: VIPS read just enough of the image to be able to get
|
|
|
|
the various properties, such as width, but no decoding occurs until
|
|
|
|
pixel values are really needed.
|
2014-07-26 12:19:46 +02:00
|
|
|
</para>
|
2014-07-25 10:22:49 +02:00
|
|
|
|
2014-07-26 12:19:46 +02:00
|
|
|
<para>
|
2014-07-25 10:22:49 +02:00
|
|
|
Once you have an image, you can get properties from it in the usual way.
|
2014-07-26 12:19:46 +02:00
|
|
|
You can use projection functions, like vips_image_get_width() or
|
2014-11-16 19:01:19 +01:00
|
|
|
g_object_get(), to get %GObject properties. All VIPS objects are
|
2014-07-26 12:19:46 +02:00
|
|
|
immutable, meaning you can only get properties, you can't set them.
|
|
|
|
See <link linkend="libvips-header">VIPS Header</link> to read about
|
|
|
|
image properties.
|
|
|
|
</para>
|
2015-02-23 13:32:56 +01:00
|
|
|
</refsect3>
|
2014-07-25 10:22:49 +02:00
|
|
|
|
2015-02-23 13:32:56 +01:00
|
|
|
<refsect3 id="using-C-ref">
|
2015-01-28 12:37:16 +01:00
|
|
|
<title>Reference counting</title>
|
2014-07-26 12:19:46 +02:00
|
|
|
<para>
|
2015-01-28 12:37:16 +01:00
|
|
|
VIPS is based on the %GObject library and is therefore reference counted.
|
2014-07-25 10:22:49 +02:00
|
|
|
vips_image_new_from_file() returns an object with a count of 1.
|
|
|
|
When you are done with an image, use g_object_unref() to dispose of it.
|
|
|
|
If you pass an image to an operation and that operation needs to keep a
|
|
|
|
copy of the image, it will ref it. So you can unref an image as soon as
|
|
|
|
you no longer need it, you don't need to hang on to it in case anyone
|
|
|
|
else is still using it.
|
2014-07-26 12:19:46 +02:00
|
|
|
</para>
|
2014-07-25 10:22:49 +02:00
|
|
|
|
2014-07-26 12:19:46 +02:00
|
|
|
<para>
|
2015-01-28 12:37:16 +01:00
|
|
|
See #VipsOperation for more detail on VIPS
|
|
|
|
reference counting conventions.
|
|
|
|
</para>
|
2015-02-23 13:32:56 +01:00
|
|
|
</refsect3>
|
2015-01-28 12:37:16 +01:00
|
|
|
|
2015-02-23 13:32:56 +01:00
|
|
|
<refsect3 id="using-C-operations">
|
2015-01-28 12:37:16 +01:00
|
|
|
<title>VIPS operations</title>
|
|
|
|
<para>
|
2016-01-25 10:35:06 +01:00
|
|
|
Use things like vips_embed() to manipulate your images. You use it from
|
|
|
|
C like this:
|
|
|
|
|
|
|
|
<programlisting language="C">
|
|
|
|
const char *filename;
|
|
|
|
VipsImage *in = vips_image_new_from_file (filename, NULL);
|
|
|
|
const int x = 10;
|
|
|
|
const int y = 10;
|
|
|
|
const int width = 1000;
|
|
|
|
const int height = 1000;
|
|
|
|
VipsImage *out;
|
|
|
|
|
|
|
|
if (vips_embed (in, &out, x, y, width, height, NULL))
|
|
|
|
error_handling();
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
Now <code>out</code> will hold a reference to a 1000 by 1000 pixel
|
|
|
|
image, with <code>in</code> pasted 10 right and 10 down from the top
|
|
|
|
left-hand corner. The remainder of the image will be black. If
|
|
|
|
<code>in</code> is too large, it will be clipped at the image edges.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2017-02-20 12:10:04 +01:00
|
|
|
Operations can take optional arguments. You give these as a set of
|
|
|
|
NULL-terminated name-value pairs at the end of the call. For example,
|
|
|
|
you can write:
|
2016-01-25 10:35:06 +01:00
|
|
|
|
|
|
|
<programlisting language="C">
|
|
|
|
if (vips_embed (in, &out, x, y, width, height,
|
|
|
|
"extend", VIPS_EXTEND_COPY,
|
|
|
|
NULL))
|
|
|
|
error_handling();
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
And now the new edge pixels, which were black, will be filled with a copy
|
|
|
|
of the edge pixels of <code>in</code>. Operation options are listed
|
|
|
|
at the top of each operation's entry in the docs. Alternatively,
|
|
|
|
the <command>vips</command> program is handy for getting a
|
|
|
|
summary of an operation's parameters. For example:
|
|
|
|
|
|
|
|
<programlisting language="none">
|
|
|
|
$ vips embed
|
|
|
|
embed an image in a larger image
|
|
|
|
usage:
|
|
|
|
embed in out x y width height
|
|
|
|
where:
|
|
|
|
in - Input image, input VipsImage
|
|
|
|
out - Output image, output VipsImage
|
|
|
|
x - Left edge of input in output, input gint
|
|
|
|
default: 0
|
|
|
|
min: -1000000000, max: 1000000000
|
|
|
|
y - Top edge of input in output, input gint
|
|
|
|
default: 0
|
|
|
|
min: -1000000000, max: 1000000000
|
|
|
|
width - Image width in pixels, input gint
|
|
|
|
default: 1
|
|
|
|
min: 1, max: 1000000000
|
|
|
|
height - Image height in pixels, input gint
|
|
|
|
default: 1
|
|
|
|
min: 1, max: 1000000000
|
|
|
|
optional arguments:
|
|
|
|
extend - How to generate the extra pixels, input VipsExtend
|
|
|
|
default: black
|
|
|
|
allowed: black, copy, repeat, mirror, white, background
|
|
|
|
background - Colour for background pixels, input VipsArrayDouble
|
|
|
|
operation flags: sequential-unbuffered
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
See #VipsOperation for more information on running operations on images.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
When you are done, you can write
|
2014-07-26 12:19:46 +02:00
|
|
|
the final image to a disc file, to a formatted memory buffer, or to
|
|
|
|
C-style memory array. See vips_image_write_to_file() and friends.
|
|
|
|
</para>
|
2015-02-23 13:32:56 +01:00
|
|
|
</refsect3>
|
2014-07-25 10:22:49 +02:00
|
|
|
|
2015-02-23 13:32:56 +01:00
|
|
|
<refsect3 id="using-C-pixels">
|
2015-01-28 12:37:16 +01:00
|
|
|
<title>Getting pixels</title>
|
2014-08-19 18:57:27 +02:00
|
|
|
<para>
|
|
|
|
Use #VipsRegion to read pixels out of images. You can use
|
|
|
|
VIPS_IMAGE_ADDR() as well, but this can need a large amount of
|
|
|
|
memory to work. See <link linkend="extending">extending</link>
|
2014-10-31 21:09:24 +01:00
|
|
|
for an introduction to writing your own operations.
|
2014-08-19 18:57:27 +02:00
|
|
|
</para>
|
2015-02-23 13:32:56 +01:00
|
|
|
</refsect3>
|
2015-01-28 12:37:16 +01:00
|
|
|
|
2015-02-23 13:32:56 +01:00
|
|
|
<refsect3 id="using-C-errors">
|
2015-01-28 12:37:16 +01:00
|
|
|
<title>Error handling</title>
|
2014-08-19 18:57:27 +02:00
|
|
|
|
2014-07-26 12:19:46 +02:00
|
|
|
<para>
|
|
|
|
VIPS keeps a log of error message, see <link
|
|
|
|
linkend="libvips-error">VIPS Error</link> to find out how to get and
|
2015-01-28 12:37:16 +01:00
|
|
|
clear the error log.
|
2014-07-26 12:19:46 +02:00
|
|
|
</para>
|
2015-02-23 13:32:56 +01:00
|
|
|
</refsect3>
|
2015-01-28 12:37:16 +01:00
|
|
|
|
2015-02-23 13:32:56 +01:00
|
|
|
<refsect3 id="using-C-example">
|
2015-01-28 12:37:16 +01:00
|
|
|
<title>Example</title>
|
2014-07-25 10:22:49 +02:00
|
|
|
|
2014-07-26 12:19:46 +02:00
|
|
|
<example>
|
|
|
|
<title>VIPS from C example</title>
|
|
|
|
<programlisting language="C">
|
|
|
|
/* compile with:
|
|
|
|
*
|
|
|
|
* gcc -g -Wall try211.c `pkg-config vips --cflags --libs`
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <vips/vips.h>
|
|
|
|
|
|
|
|
int
|
|
|
|
main( int argc, char **argv )
|
|
|
|
{
|
2015-01-28 12:37:16 +01:00
|
|
|
VipsImage *in;
|
|
|
|
double mean;
|
|
|
|
VipsImage *out;
|
2014-07-26 12:19:46 +02:00
|
|
|
|
2015-01-28 12:37:16 +01:00
|
|
|
if( VIPS_INIT( argv[0] ) )
|
|
|
|
vips_error_exit( NULL );
|
2014-07-26 12:19:46 +02:00
|
|
|
|
2015-01-28 12:37:16 +01:00
|
|
|
if( argc != 3 )
|
|
|
|
vips_error_exit( "usage: %s infile outfile", argv[0] );
|
|
|
|
|
|
|
|
if( !(in = vips_image_new_from_file( argv[1], NULL )) )
|
|
|
|
vips_error_exit( NULL );
|
2014-07-26 12:19:46 +02:00
|
|
|
|
2015-01-28 12:37:16 +01:00
|
|
|
printf( "image width = %d\n", vips_image_get_width( in ) );
|
2014-07-26 12:19:46 +02:00
|
|
|
|
2015-01-28 12:37:16 +01:00
|
|
|
if( vips_avg( in, &mean, NULL ) )
|
|
|
|
vips_error_exit( NULL );
|
2014-07-26 12:19:46 +02:00
|
|
|
|
2015-01-28 12:37:16 +01:00
|
|
|
printf( "mean pixel value = %g\n", mean );
|
2014-07-26 12:19:46 +02:00
|
|
|
|
2015-01-28 12:37:16 +01:00
|
|
|
if( vips_invert( in, &out, NULL ) )
|
|
|
|
vips_error_exit( NULL );
|
2014-07-26 12:19:46 +02:00
|
|
|
|
2015-01-28 12:37:16 +01:00
|
|
|
g_object_unref( in );
|
2014-07-26 12:19:46 +02:00
|
|
|
|
2015-01-28 12:37:16 +01:00
|
|
|
if( vips_image_write_to_file( out, argv[2], NULL ) )
|
|
|
|
vips_error_exit( NULL );
|
2014-07-26 12:19:46 +02:00
|
|
|
|
2015-01-28 12:37:16 +01:00
|
|
|
g_object_unref( out );
|
2014-07-26 12:19:46 +02:00
|
|
|
|
2015-01-28 12:37:16 +01:00
|
|
|
return( 0 );
|
2014-07-26 12:19:46 +02:00
|
|
|
}
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
2014-07-25 10:22:49 +02:00
|
|
|
|
2015-02-23 13:32:56 +01:00
|
|
|
</refsect3>
|
2014-07-25 10:22:49 +02:00
|
|
|
|
|
|
|
</refentry>
|