## VIPS from C Using VIPS — How to use the VIPS library from C |

#### Introduction

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 overview. The vips program is handy for getting a summary of an operation's parameters.

#### Library startup

When your program starts, use `VIPS_INIT()` to start up the VIPS library. You should pass it the name of your program, usually `argv[0]`. Use `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.

You can add the VIPS flags to your `GObject` command-line processing with `vips_add_option_entries()`, see below.

#### The VipsImage class

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 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.

Once you have an image, you can get properties from it in the usual way. You can use projection functions, like `vips_image_get_width()` or `g_object_get()`, to get `GObject` properties. All VIPS objects are immutable, meaning you can only get properties, you can't set them. See VIPS Header to read about image properties.

#### Reference counting

VIPS is based on the `GObject` library and is therefore reference counted. `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.

See VipsOperation for more detail on VIPS reference counting conventions.

#### VIPS operations

Use things like `vips_invert()` to manipulate your images. See VipsOperation for information on running operations on images. When you are done, you can write 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.

#### Getting pixels

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 extending for an introduction to writing your own operations.

#### Error handling

VIPS keeps a log of error message, see VIPS Error to find out how to get and clear the error log.

#### Example

Example 1. VIPS from C example

    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
    /* 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 )
    {
      GOptionContext *context;
      GOptionGroup *main_group;
      GError *error = NULL;
      VipsImage *in;
      double mean;
      VipsImage *out;

      if( VIPS_INIT( argv[0] ) )
        vips_error_exit( NULL );

      context = g_option_context_new( "hello infile outfile - VIPS demo" );

      main_group = g_option_group_new( NULL, NULL, NULL, NULL, NULL );
      g_option_context_set_main_group( context, main_group );
      vips_add_option_entries( main_group );

      if( !g_option_context_parse( context, &argc, &argv, &error ) ) {
        if( error ) {
          fprintf( stderr, "%s\n", error->message );
          g_error_free( error );
        }

        vips_error_exit( NULL );
      }

      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 );

      printf( "image width = %d\n", vips_image_get_width( in ) );

      if( vips_avg( in, &mean, NULL ) )
        vips_error_exit( NULL );

      printf( "mean pixel value = %g\n", mean );

      if( vips_invert( in, &out, NULL ) )
        vips_error_exit( NULL );

      g_object_unref( in );

      if( vips_image_write_to_file( out, argv[2], NULL ) )
        vips_error_exit( NULL );

      g_object_unref( out );

      return( 0 );
    }