add @background arg to save

used to flatten A out of images, if necessary

vipsthumbnail rtd-img.png -o x.jpg[background=255]
This commit is contained in:
John Cupitt 2014-09-11 09:18:38 +01:00
parent 2ec0a8b87e
commit b21c47b1c5
5 changed files with 31 additions and 17 deletions

View File

@ -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"

View File

@ -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 );
}
/**

View File

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

View File

@ -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?

View File

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