diff --git a/ChangeLog b/ChangeLog index d937c726..92cf8fb2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -4,6 +4,7 @@ - add vips_resize() - return of vips_init(), but just for bindings - revised type.c to make it more binding-friendly +- add @background arg to save: the colour to flatten against 8/9/14 started 7.40.9 - support jfif resunit "none" diff --git a/libvips/conversion/embed.c b/libvips/conversion/embed.c index 8a5e9a8b..ac5ad8a4 100644 --- a/libvips/conversion/embed.c +++ b/libvips/conversion/embed.c @@ -82,7 +82,7 @@ typedef struct _VipsEmbed { VipsImage *in; VipsExtend extend; - VipsArea *background; + VipsArrayDouble *background; int x; int y; int width; @@ -362,7 +362,8 @@ vips_embed_build( VipsObject *object ) if( !(embed->ink = vips__vector_to_ink( class->nickname, embed->in, - embed->background->data, NULL, embed->background->n )) ) + VIPS_AREA( embed->background )->data, NULL, + VIPS_AREA( embed->background )->n )) ) return( -1 ); if( !vips_object_argument_isset( object, "extend" ) && @@ -614,9 +615,7 @@ static void vips_embed_init( VipsEmbed *embed ) { embed->extend = VIPS_EXTEND_BLACK; - embed->background = - vips_area_new_array( G_TYPE_DOUBLE, sizeof( double ), 1 ); - ((double *) (embed->background->data))[0] = 0; + embed->background = vips_array_double_newv( 1, 0.0 ); } /** diff --git a/libvips/conversion/flatten.c b/libvips/conversion/flatten.c index f521f49c..df89a246 100644 --- a/libvips/conversion/flatten.c +++ b/libvips/conversion/flatten.c @@ -61,7 +61,7 @@ typedef struct _VipsFlatten { /* Background colour. */ - VipsArea *background; + VipsArrayDouble *background; /* The [double] converted to the input image format. */ @@ -319,8 +319,9 @@ vips_flatten_build( VipsObject *object ) /* Is the background black? We have a special path for this. */ black = TRUE; - for( i = 0; i < flatten->background->n; i++ ) - if( ((double *) flatten->background->data)[i] != 0.0 ) { + for( i = 0; i < VIPS_AREA( flatten->background )->n; i++ ) + if( vips_array_double_get( flatten->background, NULL )[i] != + 0.0 ) { black = FALSE; break; } @@ -336,8 +337,8 @@ vips_flatten_build( VipsObject *object ) */ if( !(flatten->ink = vips__vector_to_ink( class->nickname, conversion->out, - flatten->background->data, NULL, - flatten->background->n )) ) + VIPS_AREA( flatten->background )->data, NULL, + VIPS_AREA( flatten->background )->n )) ) return( -1 ); if( vips_image_generate( conversion->out, @@ -384,9 +385,7 @@ vips_flatten_class_init( VipsFlattenClass *class ) static void vips_flatten_init( VipsFlatten *flatten ) { - flatten->background = - vips_area_new_array( G_TYPE_DOUBLE, sizeof( double ), 1 ); - ((double *) (flatten->background->data))[0] = 0.0; + flatten->background = vips_array_double_newv( 1, 0.0 ); } /** @@ -397,7 +396,7 @@ vips_flatten_init( VipsFlatten *flatten ) * * Optional arguments: * - * @background: colour for new pixels + * @background: #VipsArrayDouble colour for new pixels * * Take the last band of @in as an alpha and use it to blend the * remaining channels with @background. diff --git a/libvips/foreign/foreign.c b/libvips/foreign/foreign.c index f838ea47..e0a80667 100644 --- a/libvips/foreign/foreign.c +++ b/libvips/foreign/foreign.c @@ -1146,7 +1146,9 @@ vips_foreign_convert_saveable( VipsForeignSave *save ) class->saveable == VIPS_SAVEABLE_RGB_CMYK) ) { VipsImage *out; - if( vips_flatten( in, &out, 0, NULL ) ) { + if( vips_flatten( in, &out, + "background", save->background, + NULL ) ) { g_object_unref( in ); return( -1 ); } @@ -1414,11 +1416,19 @@ vips_foreign_save_class_init( VipsForeignSaveClass *class ) VIPS_ARGUMENT_OPTIONAL_INPUT, G_STRUCT_OFFSET( VipsForeignSave, strip ), FALSE ); + + VIPS_ARG_BOXED( class, "background", 101, + _( "Background" ), + _( "Background value" ), + VIPS_ARGUMENT_OPTIONAL_INPUT, + G_STRUCT_OFFSET( VipsForeignSave, background ), + VIPS_TYPE_ARRAY_DOUBLE ); } static void -vips_foreign_save_init( VipsForeignSave *object ) +vips_foreign_save_init( VipsForeignSave *save ) { + save->background = vips_array_double_newv( 1, 0.0 ); } /* Can we write this filename with this file? diff --git a/libvips/include/vips/foreign.h b/libvips/include/vips/foreign.h index bc810423..ed096075 100644 --- a/libvips/include/vips/foreign.h +++ b/libvips/include/vips/foreign.h @@ -256,10 +256,15 @@ typedef enum { typedef struct _VipsForeignSave { VipsForeign parent_object; - /* Dont't attach metadata. + /* Don't attach metadata. */ gboolean strip; + /* If flattening out alpha, the background colour to use. Default to + * 0 (black). + */ + VipsArrayDouble *background; + /*< public >*/ /* The image we are to save, as supplied by our caller.