Skip to content

Hello World

Kirk Martinez edited this page Oct 25, 2017 · 4 revisions

This page has code for some minimal programs in VIPS and nip. They don't do anything particularly useful, but they are handy for checking that your install is working and getting used to the build environment.

libvips Hello World in C

/* compile with
 *
 * gcc -Wall vipsavg.c `pkg-config vips --cflags --libs`
 */

#include <stdio.h>

#include <vips/vips.h>

int
main (int argc, char **argv)
{
  VipsImage *im;
  double avg;

  if (VIPS_INIT (argv[0]))
    vips_error_exit ("unable to start VIPS");

  if (argc != 2)
    vips_error_exit ("usage: %s <filename>", g_get_prgname ());

  if (!(im = vips_image_new_from_file (argv[1], NULL)))
    vips_error_exit ("unable to open");
  if (vips_avg (im, &avg, NULL))
    vips_error_exit ("unable to find avg");
  g_object_unref (im);

  printf ("Hello World!\nPixel average of %s is %g\n", argv[1], avg);

  return (0);
}

Compile this program on Linux or OS X with:

gcc -Wall vipsavg.c -o vipsavg `pkg-config vips --cflags --libs`

And run with:

./vipsavg ~/pics/fred.jpg

You should see:

Hello World!
Pixel average of /home/john/pics/fred.jpg is 67.0741

You can compile for 32-bit Windows with mingw using something like:

i686-w64-mingw32-gcc -mms-bitfields -march=i686 \
    -I/home/john/GIT/build-win32/7.32/inst/include \
    -I/home/john/GIT/build-win32/7.32/inst/include/glib-2.0 \
    -I/home/john/GIT/build-win32/7.32/inst/lib/glib-2.0/include \
    myprog.c \
    -L/home/john/GIT/build-win32/7.32/inst/lib \
    -lvips -lz -ljpeg -lstdc++ -lxml2 -lfftw3 -lm -lMagickWand -llcms2 \
    -lopenslide -lcfitsio -lpangoft2-1.0 -ltiff -lpng14 -lexif \
    -lMagickCore -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 \
    -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lintl \
    -o myprog.exe

For 64-bit Windows, try changing the first line to be:

x86_64-w64-mingw32-gcc-win32 -mms-bitfields \

You'll need to change it about for MSVC, but the list of libraries might be helpful.

libvips Hello World in C++

This program loads an image and finds the average pixel value.

#include <stdio.h>
#include <vips/vips8>

using namespace vips;

int
main (int argc, char **argv)
{
  if (VIPS_INIT (argv[0]))
    vips_error_exit (NULL);

  try
  {
    if (argc != 2)
      throw (VError ("args: <filename>"));

    VImage fred = VImage::new_from_file (argv[1]);

    printf ("Hello World!\nPixel average of %s is %g\n", argv[1],
            fred.avg ());
  }
  catch (const VError & e)
  {
    printf ("%s\n", e.what ());
  }

  return (0);
}

Compile with something like:

g++ -Wall try.cc `pkg-config vips-cpp --cflags --libs`

And run with:

./a.out ~/pics/fred.jpg

You should see:

Hello World!
Pixel average of /home/john/pics/fred.jpg is 67.0741

Hello World in nip2

This is the same thing, but in nip2. This example uses the nip2 command-line mode.

nip2 -e 'mean (Image_file "~/pics/fred.jpg")'

And you should see:

67.0741

(Image_file is the constructor for the Image class that makes an image from a filename, mean finds the pixel average.)

That example has the filename of the image to be processed embedded in the expression. You can supply the filename as a separate argument and pick it up like this:

nip2 -e 'mean (Image_file argv?1)' ~/pics/fred.jpg

(argv is a list of all the command-line arguments in the form ["script-name", "first-argument", ...], and ? is infix list index.)

To get exactly the same output as the C/C++ versions above you need the wordier:

nip2 -e '"Hello World!\nPixel average of " ++ argv?1 ++ " is " ++ print (mean (Image_file argv?1))' ~/pics/fred.jpg

(++ is infix list concatenation, print turns any expression into a string.)

VIPS Hello World in Python

This will work in Python2 and Python3.

import sys
import gi
gi.require_version('Vips', '8.0')
from gi.repository import Vips

a = Vips.Image.new_from_file(sys.argv[1])
print('Hello World!')
print('Pixel average of %s is %d' % (sys.argv[1], a.avg()))
Clone this wiki locally