Skip to content

Instantly share code, notes, and snippets.

@jcupitt
Created June 10, 2016 11:02
Show Gist options
  • Save jcupitt/c516325ebef7601b5da610af5619c9b2 to your computer and use it in GitHub Desktop.
Save jcupitt/c516325ebef7601b5da610af5619c9b2 to your computer and use it in GitHub Desktop.
sum a lot of images in constant memory
/* compile with
* gcc -g -Wall try236.c `pkg-config vips --cflags --libs`
*/
#include <stdio.h>
#include <stdlib.h>
#include <vips/vips.h>
int
main( int argc, char **argv )
{
VipsImage *accumulator;
int i;
if( VIPS_INIT( argv[0] ) )
vips_error_exit( NULL );
vips_cache_set_max( 0 );
vips_leak_set( TRUE );
accumulator = NULL;
for( i = 2; i < argc; i++) {
VipsImage *im;
VipsImage *memory;
printf( "image %d ...\r", i - 1 );
fflush( stdout );
im = vips_image_new_from_file( argv[i],
"access", VIPS_ACCESS_SEQUENTIAL,
NULL );
if( accumulator ) {
VipsImage *sum;
if( vips_add( im, accumulator, &sum, NULL ) )
vips_error_exit( NULL );
g_object_unref( im );
im = sum;
}
memory = vips_image_new_memory();
if( vips_image_write( im, memory ) )
vips_error_exit( NULL );
g_object_unref( im );
if( accumulator )
g_object_unref( accumulator );
accumulator = memory;
}
if( vips_image_write_to_file( accumulator, argv[1], NULL ) )
vips_error_exit( NULL );
g_object_unref( accumulator );
printf( "\n" );
return( 0 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment