Software: Apache/2.4.41 (Ubuntu). PHP/8.0.30 uname -a: Linux apirnd 5.4.0-204-generic #224-Ubuntu SMP Thu Dec 5 13:38:28 UTC 2024 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) Safe-mode: OFF (not secure) /usr/share/gtk-doc/html/libvips/ drwxr-xr-x | |
| Viewing file: Select action/file-type:
IntroductionVIPS comes with a convenient C++ API. It is a very thin wrapper over the C API and adds automatic reference counting, exceptions, operator overloads, and automatic constant expansion. You can drop down to the C API at any point, so all the C API docs also work for C++.
/* compile with:
* g++ -g -Wall example.cc `pkg-config vips-cpp --cflags --libs`
*/
#include <vips/vips8>
using namespace vips;
int
main (int argc, char **argv)
{
if (VIPS_INIT (argv[0]))
vips_error_exit (NULL);
if (argc != 3)
vips_error_exit ("usage: %s input-file output-file", argv[0]);
VImage in = VImage::new_from_file (argv[1],
VImage::option ()->set ("access", VIPS_ACCESS_SEQUENTIAL));
double avg = in.avg ();
printf ("avg = %g\n", avg);
printf ("width = %d\n", in.width ());
in = VImage::new_from_file (argv[1],
VImage::option ()->set ("access", VIPS_ACCESS_SEQUENTIAL));
VImage out = in.embed (10, 10, 1000, 1000,
VImage::option ()->
set ("extend", "background")->
set ("background", 128));
out.write_to_file (argv[2]);
vips_shutdown ();
return (0);
}
Everything before
There are a series of similar constructors which parallel the other
constructors in the C API, see VImage::
The convenience function `VImage::
new_image = image.new_from_image(12);
Now `new_image` has the same size as `image`, but has one band, and every pixel has the value 12. You can pass a `std::vector<double>` as the argument to make a constant image with a different number of bands.
There's also
VImage::
The next line finds the average pixel value, it's the equivalent of the
All other operations follow the same pattern, for example the C API call
int vips_add( VipsImage *left, VipsImage *right, VipsImage **out, ... ); appears in C++ as: VImage VImage::add( VImage right, VOption *options ) const
The next line uses VImage::
Next we reload the image. The VImage::
The next line runs
Finally, VImage:: The API docs have a handy table of all vips operations, if you want to find out how to do something, try searching that. Automatic constant expansionThe C++ API will automatically turn constants into images in some cases. For example, you can join two images together bandwise (the bandwise join of two RGB images would be a six-band image) with: VImage rgb = ...; VImage six_band = rgb.bandjoin( rgb ); You can also bandjoin a constant, for example: VImage rgb_with_alpha = rgb.bandjoin( 255 ); Will add an extra band to an image, with every element in the new band having the value 255. This is quite a general feature. You can use a constant in most places where you can use an image and it will be converted. For example: VImage a = (a < 128).ifthenelse( 128, a );
Will set every band element of The C++ API includes the usual range of arithmetic operator overloads. You can mix constants, vectors and images freely.
The API overloads VImage xyz = VImage::xyz( 256, 256 ) - VImage::to_vectorv( 2, 128.0, 128.0 ); VImage mask = (xyz[0].pow( 2 ) + xyz[1].pow( 2 )).pow( 0.5 ) < 100; to make a circular mask, for example.
The API overloads VImage xyz = VImage::xyz( 256, 256 ) - VImage::to_vectorv( 2, 128.0, 128.0 ); // this will have the value [0, 0] std::vector<double> point = xyz(128, 128);
Enum expansion
VIPS operations which implement several functions with a controlling
enum, such as int vips_math( VipsImage *in, VipsImage **out, VipsOperationMath math, ... );
where VipsOperationMath has the member VIPS_OPERATION_MATH_SIN, has a
C convenience function int vips_sin( VipsImage *in, VipsImage **out, ... );
and a C++ member function VImage:: VImage VImage::sin( VOption *options = 0 ) const
Image metadata
VIPS images can have a lot of metadata attached to them, giving things
like ICC profiles, EXIF data, and so on. You can use the command-line
program
You can read metadata items with the member functions
const char *VImage::get_string( const char *field ) throw( VError );
You can use the void VImage::set( const char *field, const char *value );
You can use these functions to manipulate exif metadata, for example: VImage im = VImage::new_from_file( "x.jpg" ) int orientation = im.get_int( VIPS_META_ORIENTATION ); im.set( VIPS_META_ORIENTATION, 2 ); im.write_to_file( "y.jpg" );
Extending the C++ interface
The C++ interface comes in two parts. First,
The member definition and declaration for each operation, for example
VImage::
You can write the wrapper yourself, of course, they are very simple.
The one for VImage::
VImage VImage::add( VImage right, VOption *options ) const
{
VImage out;
call("add",
(options ? options : VImage::option())->
set("out", &out)->
set("left", *this)->
set("right", right));
return out;
}
Where VImage:: |
:: Command execute :: | |
--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.0065 ]-- |