Skip to content

Commit b21c47b

Browse files
committed
add @background arg to save
used to flatten A out of images, if necessary vipsthumbnail rtd-img.png -o x.jpg[background=255]
1 parent 2ec0a8b commit b21c47b

5 files changed

Lines changed: 31 additions & 17 deletions

File tree

‎ChangeLog‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- add vips_resize()
55
- return of vips_init(), but just for bindings
66
- revised type.c to make it more binding-friendly
7+
- add @background arg to save: the colour to flatten against
78

89
8/9/14 started 7.40.9
910
- support jfif resunit "none"

‎libvips/conversion/embed.c‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ typedef struct _VipsEmbed {
8282
VipsImage *in;
8383

8484
VipsExtend extend;
85-
VipsArea *background;
85+
VipsArrayDouble *background;
8686
int x;
8787
int y;
8888
int width;
@@ -362,7 +362,8 @@ vips_embed_build( VipsObject *object )
362362

363363
if( !(embed->ink = vips__vector_to_ink(
364364
class->nickname, embed->in,
365-
embed->background->data, NULL, embed->background->n )) )
365+
VIPS_AREA( embed->background )->data, NULL,
366+
VIPS_AREA( embed->background )->n )) )
366367
return( -1 );
367368

368369
if( !vips_object_argument_isset( object, "extend" ) &&
@@ -614,9 +615,7 @@ static void
614615
vips_embed_init( VipsEmbed *embed )
615616
{
616617
embed->extend = VIPS_EXTEND_BLACK;
617-
embed->background =
618-
vips_area_new_array( G_TYPE_DOUBLE, sizeof( double ), 1 );
619-
((double *) (embed->background->data))[0] = 0;
618+
embed->background = vips_array_double_newv( 1, 0.0 );
620619
}
621620

622621
/**

‎libvips/conversion/flatten.c‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ typedef struct _VipsFlatten {
6161

6262
/* Background colour.
6363
*/
64-
VipsArea *background;
64+
VipsArrayDouble *background;
6565

6666
/* The [double] converted to the input image format.
6767
*/
@@ -319,8 +319,9 @@ vips_flatten_build( VipsObject *object )
319319
/* Is the background black? We have a special path for this.
320320
*/
321321
black = TRUE;
322-
for( i = 0; i < flatten->background->n; i++ )
323-
if( ((double *) flatten->background->data)[i] != 0.0 ) {
322+
for( i = 0; i < VIPS_AREA( flatten->background )->n; i++ )
323+
if( vips_array_double_get( flatten->background, NULL )[i] !=
324+
0.0 ) {
324325
black = FALSE;
325326
break;
326327
}
@@ -336,8 +337,8 @@ vips_flatten_build( VipsObject *object )
336337
*/
337338
if( !(flatten->ink = vips__vector_to_ink( class->nickname,
338339
conversion->out,
339-
flatten->background->data, NULL,
340-
flatten->background->n )) )
340+
VIPS_AREA( flatten->background )->data, NULL,
341+
VIPS_AREA( flatten->background )->n )) )
341342
return( -1 );
342343

343344
if( vips_image_generate( conversion->out,
@@ -384,9 +385,7 @@ vips_flatten_class_init( VipsFlattenClass *class )
384385
static void
385386
vips_flatten_init( VipsFlatten *flatten )
386387
{
387-
flatten->background =
388-
vips_area_new_array( G_TYPE_DOUBLE, sizeof( double ), 1 );
389-
((double *) (flatten->background->data))[0] = 0.0;
388+
flatten->background = vips_array_double_newv( 1, 0.0 );
390389
}
391390

392391
/**
@@ -397,7 +396,7 @@ vips_flatten_init( VipsFlatten *flatten )
397396
*
398397
* Optional arguments:
399398
*
400-
* @background: colour for new pixels
399+
* @background: #VipsArrayDouble colour for new pixels
401400
*
402401
* Take the last band of @in as an alpha and use it to blend the
403402
* remaining channels with @background.

‎libvips/foreign/foreign.c‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,9 @@ vips_foreign_convert_saveable( VipsForeignSave *save )
11461146
class->saveable == VIPS_SAVEABLE_RGB_CMYK) ) {
11471147
VipsImage *out;
11481148

1149-
if( vips_flatten( in, &out, 0, NULL ) ) {
1149+
if( vips_flatten( in, &out,
1150+
"background", save->background,
1151+
NULL ) ) {
11501152
g_object_unref( in );
11511153
return( -1 );
11521154
}
@@ -1414,11 +1416,19 @@ vips_foreign_save_class_init( VipsForeignSaveClass *class )
14141416
VIPS_ARGUMENT_OPTIONAL_INPUT,
14151417
G_STRUCT_OFFSET( VipsForeignSave, strip ),
14161418
FALSE );
1419+
1420+
VIPS_ARG_BOXED( class, "background", 101,
1421+
_( "Background" ),
1422+
_( "Background value" ),
1423+
VIPS_ARGUMENT_OPTIONAL_INPUT,
1424+
G_STRUCT_OFFSET( VipsForeignSave, background ),
1425+
VIPS_TYPE_ARRAY_DOUBLE );
14171426
}
14181427

14191428
static void
1420-
vips_foreign_save_init( VipsForeignSave *object )
1429+
vips_foreign_save_init( VipsForeignSave *save )
14211430
{
1431+
save->background = vips_array_double_newv( 1, 0.0 );
14221432
}
14231433

14241434
/* Can we write this filename with this file?

‎libvips/include/vips/foreign.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,15 @@ typedef enum {
256256
typedef struct _VipsForeignSave {
257257
VipsForeign parent_object;
258258

259-
/* Dont't attach metadata.
259+
/* Don't attach metadata.
260260
*/
261261
gboolean strip;
262262

263+
/* If flattening out alpha, the background colour to use. Default to
264+
* 0 (black).
265+
*/
266+
VipsArrayDouble *background;
267+
263268
/*< public >*/
264269

265270
/* The image we are to save, as supplied by our caller.

0 commit comments

Comments
 (0)