From d836166087ae295da654a674f4540dd2547513aa Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Fri, 9 Sep 2022 10:14:59 +0100 Subject: [PATCH] add some more examples --- examples/meson.build | 2 + examples/new-from-buffer.c | 54 ++++++++++++++++++++++++++ examples/progress-cancel.c | 78 ++++++++++++++++++++++++++++++++++++++ meson.build | 5 ++- meson_options.txt | 5 +++ 5 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 examples/new-from-buffer.c create mode 100644 examples/progress-cancel.c diff --git a/examples/meson.build b/examples/meson.build index 663deaa0..f75ac3b4 100644 --- a/examples/meson.build +++ b/examples/meson.build @@ -1,5 +1,7 @@ examples = [ 'annotate-animated', + 'new-from-buffer', + 'progress-cancel', ] foreach example : examples diff --git a/examples/new-from-buffer.c b/examples/new-from-buffer.c new file mode 100644 index 00000000..db3f979a --- /dev/null +++ b/examples/new-from-buffer.c @@ -0,0 +1,54 @@ +/* Read and write formatted images to memory. + * + * Compile with: + * + * gcc -g -Wall new-from-buffer.c `pkg-config vips --cflags --libs` + * + */ + +#include + +int +main( int argc, char **argv ) +{ + gchar *buf; + gsize len; + int i; + + if( VIPS_INIT( argv[0] ) ) + vips_error_exit( NULL ); + + if( argc != 2 ) + vips_error_exit( "usage: %s FILENAME", argv[0] ); + + if( !g_file_get_contents( argv[1], &buf, &len, NULL ) ) + vips_error_exit( NULL ); + + for( i = 0; i < 10; i++ ) { + VipsImage *image; + void *new_buf; + size_t new_len; + + printf( "loop %d ...\n", i ); + + if( !(image = vips_image_new_from_buffer( buf, len, "", + "access", VIPS_ACCESS_SEQUENTIAL, + NULL )) ) + vips_error_exit( NULL ); + + if( vips_image_write_to_buffer( image, + ".jpg", &new_buf, &new_len, + "Q", 95, + NULL ) ) + vips_error_exit( NULL ); + + g_object_unref( image ); + g_free( new_buf ); + } + + g_free( buf ); + + vips_shutdown(); + + return( 0 ); +} diff --git a/examples/progress-cancel.c b/examples/progress-cancel.c new file mode 100644 index 00000000..86b7b51c --- /dev/null +++ b/examples/progress-cancel.c @@ -0,0 +1,78 @@ +/* Show progress feedback and computation cancel. + * + * compile with + * + * gcc -g -Wall progress-cancel.c `pkg-config vips --cflags --libs` + */ + +#include +#include +#include + +void +preeval_callback( VipsImage *image, VipsProgress *progress, void* pdata ) +{ + printf( "preeval_callback:\n" ); +} + +void +eval_callback( VipsImage *image, VipsProgress *progress, void* pdata ) +{ + printf( "eval_callback: percent = %d\n", progress->percent ); + + if( progress->percent >= 25 ) { + printf( "calling vips_image_set_kill() ...\n" ); + vips_image_set_kill( image, TRUE ); + } +} + +void +posteval_callback( VipsImage *image, VipsProgress *progress, void* pdata ) +{ + printf( "posteval_callback: finished in %.3gs\n", + g_timer_elapsed( progress->start, NULL ) ); +} + +int +main( int argc, char **argv ) +{ + VipsImage *image; + VipsImage *out; + void *output; + size_t output_length; + + if( VIPS_INIT( argv[0] ) ) + vips_error_exit( NULL ); + + if( argc != 3 ) + vips_error_exit( "usage: %s INPUT-FILE OUTPUT-FILE", argv[0] ); + + if( !(image = vips_image_new_from_file( argv[1], + "access", VIPS_ACCESS_SEQUENTIAL, + NULL )) ) + vips_error_exit( NULL ); + + if( vips_resize( image, &out, 0.5, NULL ) ) + vips_error_exit( NULL ); + + vips_image_set_progress( out, TRUE ); + g_signal_connect( out, "preeval", + G_CALLBACK( preeval_callback ), NULL ); + g_signal_connect( out, "eval", + G_CALLBACK( eval_callback ), NULL ); + g_signal_connect( out, "posteval", + G_CALLBACK( posteval_callback ), NULL ); + + output = NULL; + if( vips_image_write_to_buffer( out, argv[2], &output, &output_length, + NULL ) ) + printf( "error return from vips_image_write_to_buffer()\n" ); + + g_object_unref( out ); + g_object_unref( image ); + if( output ) + g_free( output ); + vips_shutdown(); + + return 0; +} diff --git a/meson.build b/meson.build index 6dd1c10e..f241dd73 100644 --- a/meson.build +++ b/meson.build @@ -633,6 +633,7 @@ build_summary = { 'enable gtk-doc': [get_option('gtk_doc')], 'enable doxygen': [get_option('doxygen')], 'enable introspection': [get_option('introspection')], + 'enable examples': [get_option('examples')], 'enable RAD load/save': [get_option('radiance')], 'enable Analyze7 load/save': [get_option('analyze')], 'enable PPM load/save': [get_option('ppm')], @@ -686,6 +687,8 @@ subdir('cplusplus') subdir('man') subdir('po') subdir('tools') -subdir('examples') +if get_option('examples') + subdir('examples') +endif subdir('test') subdir('fuzz') diff --git a/meson_options.txt b/meson_options.txt index 0fa8f645..5afc5ef5 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -5,6 +5,11 @@ option('deprecated', value: true, description: 'Build deprecated components') +option('examples', + type: 'boolean', + value: true, + description: 'Build example programs') + option('doxygen', type: 'boolean', value: false,