current example of calling vips functions in C

#include 
#include 

int
main( int argc, char **argv )
{
  VipsImage *in;
  double mean;
  VipsImage *out;

  if( VIPS_INIT( argv[0] ) )
    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 );
}

old Example of VIPS code from the 90s

In this simple example of a VIPS program to invert an image (ie make the "negative"). It includes lots of comments and all error checking. The loops at the end show how simple pointer access to the image is. This is the simplest was to write VIPS code but even this can be integrated within the ip interface. It assumes the image is one byte per band - which is typical for a start-up program. Coping with int, float, etc bands makes life more complicated!


/* Simple image processing example --- find the photographic negative of an
 * image. Use this code as a start point for WIO functions.
 *
 * JC, 19/10/95
 */

#include stdio.h
#include vips.h
#include vips/util.h

/* Our image processing function. Call this from other parts of your C
 * application.
 */
int 
my_invert( IMAGE *in, IMAGE *out )
{
	int x, y, b;			/* Scan input image */
	unsigned char *buf, *p;		/* Output buffer & pointer */
	unsigned char *q;		/* Input pointer */

	/* Check input image. Make sure we are set up for WIO processing.
	 */
	if( im_iocheck( in, out ) )
		return( -1 );

	/* Make sure this is the kind of image we can process: we only do
	 * unsigned char images.
	 */
	if( in->Coding != NOCODING || in->Coding != FMTUCHAR ) {
		im_errormsg( "invert: uncoded uchar only" );
		return( -1 );
	}

	/* Prepare the output image. We set fields in out for the kind of
	 * image we are going to write, and call im_setupout(). Since the
	 * output image is going to be the same as the input (same size, type,
	 * number of bands, and so on), we can just copy values over from in.
	 */
	if( im_cp_desc( out, in ) )
		return( -1 );
	if( im_setupout( out ) )
		return( -1 );

	/* Make output buffer. We build each line of the output image in here 
	 * before writing it to out. The ARRAY() macro allocates memory local
	 * to an image descriptor: in this case, since we allocate local to
	 * out, when out is closed by our caller, the memory will be freed for
	 * us. lsize() is a macro which returns sizeof( one line of image ).
	 */
	if( !(buf = ARRAY( out, lsize( out ), unsigned char )) )
		return( -1 );

	/* Do the processing. p steps through the input image top-to-bottom,
	 * left-to-right. q steps along buf[] for each line of the output
	 * image. We could make this a little faster --- but we try to keep it
	 * simple for this example.
	 */
	p = (unsigned char *) in->data;
	for( y = 0; y < in->Ysize; y++ ) {
		q = buf;

		for( x = 0; x < in->Xsize; x++ ) 
			for( b = 0; b < in->Bands; b++ ) 
				*q++ = 255 - *p++;

		if( im_writeline( y, out, buf ) )
			return( -1 );
	}

	/* Success!
	 */
	return( 0 );
}

vips/ip