diff --git a/ChangeLog b/ChangeLog index 0963b57e..bc2a16ba 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +18/6/12 started 7.28.7 +- add vips_flatten() -- flatten RGBA to RGB +- better alpha handling in PNG load +- don't save RGBA PNG as CMYK JPG + 19/4/12 started 7.28.6 - better resolution unit handling in deprecated im_vips2tiff() - use TIFF_CFLAGS output from pkg-config (thanks Jay) diff --git a/TODO b/TODO index 4b0573a9..bf7d9e8b 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,17 @@ - on RGBA -> RGB conversion in foreign.c, need to use A channel to mask off RGB ... Opera.png looks bad without it +- vipsthumbnail.c could use this instead of im_tile_cache() to get seq hint + set + + if( vips_tilecache( in, out, + "tile_width", in->Xsize, + "tile_height", strip_height * nstrips, + "max_tiles", 2, + "strategy", VIPS_CACHE_SEQUENTIAL, + NULL ) ) + return( -1 ); + diff --git a/configure.in b/configure.in index ac54431a..d34ea075 100644 --- a/configure.in +++ b/configure.in @@ -15,7 +15,7 @@ AC_CONFIG_MACRO_DIR([m4]) # user-visible library versioning m4_define([vips_major_version], [7]) m4_define([vips_minor_version], [28]) -m4_define([vips_micro_version], [6]) +m4_define([vips_micro_version], [7]) m4_define([vips_version], [vips_major_version.vips_minor_version.vips_micro_version]) @@ -34,9 +34,9 @@ VIPS_VERSION_STRING=$VIPS_VERSION-`date` # interface changes backwards compatible?: increment age # interface changes not backwards compatible?: reset age to 0 -LIBRARY_CURRENT=32 +LIBRARY_CURRENT=33 LIBRARY_REVISION=0 -LIBRARY_AGE=0 +LIBRARY_AGE=1 # patched into include/vips/version.h AC_SUBST(VIPS_VERSION) diff --git a/libvips/conversion/Makefile.am b/libvips/conversion/Makefile.am index ffed0a3c..fadfdcf0 100644 --- a/libvips/conversion/Makefile.am +++ b/libvips/conversion/Makefile.am @@ -5,6 +5,7 @@ libconversion_la_SOURCES = \ conversion.h \ tilecache.c \ sequential.c \ + flatten.c \ cache.c \ copy.c \ embed.c \ diff --git a/libvips/conversion/conversion.c b/libvips/conversion/conversion.c index 4db7b772..16cb659f 100644 --- a/libvips/conversion/conversion.c +++ b/libvips/conversion/conversion.c @@ -121,6 +121,7 @@ vips_conversion_operation_init( void ) extern GType vips_ifthenelse_get_type( void ); extern GType vips_recomb_get_type( void ); extern GType vips_bandmean_get_type( void ); + extern GType vips_flatten_get_type( void ); vips_copy_get_type(); vips_tile_cache_get_type(); @@ -140,6 +141,7 @@ vips_conversion_operation_init( void ) vips_ifthenelse_get_type(); vips_recomb_get_type(); vips_bandmean_get_type(); + vips_flatten_get_type(); } /* The common part of most binary conversion diff --git a/libvips/conversion/flatten.c b/libvips/conversion/flatten.c new file mode 100644 index 00000000..93946ddc --- /dev/null +++ b/libvips/conversion/flatten.c @@ -0,0 +1,428 @@ +/* flatten the alpha out of an image, replacing it with a constant background + * + * Author: John Cupitt + * Written on: 18/6/12 + */ + +/* + + This file is part of VIPS. + + VIPS is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + */ + +/* + + These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk + + */ + +/* +#define VIPS_DEBUG + */ + +#ifdef HAVE_CONFIG_H +#include +#endif /*HAVE_CONFIG_H*/ +#include + +#include +#include +#include +#include + +#include +#include +#include + +#include "conversion.h" + +typedef struct _VipsFlatten { + VipsConversion parent_instance; + + VipsImage *in; + + /* Background colour. + */ + VipsArea *background; + + /* The [double] converted to the input image format. + */ + VipsPel *ink; + +} VipsFlatten; + +typedef VipsConversionClass VipsFlattenClass; + +G_DEFINE_TYPE( VipsFlatten, vips_flatten, VIPS_TYPE_CONVERSION ); + +/* Shift A down N places, rounding to nearest. + * + * This calculates A / B, rounding the result to nearest, ie. + * + * (a + (b / 2)) / b + * + * We mustn't overflow during the add, so we actually do: + * + * ((a / 2) + (b / 4)) / (b / 2) + * + * slightly less accurate, but safe from overflow. + */ +#define SHIFT_ROUND( A, N ) \ + ((((A) >> 1) + (1 << ((N) - 2)) - 1) >> ((N) - 1)) + +/* Flatten with shift + round, black background. + */ +#define VIPS_FLATTEN_INT_BLACK( TYPE, N ) { \ + TYPE *p = (TYPE *) in; \ + TYPE *q = (TYPE *) out; \ + \ + for( x = 0; x < width; x++ ) { \ + TYPE alpha = p[bands - 1]; \ + int b; \ + \ + for( b = 0; b < bands - 1; b++ ) \ + q[b] = SHIFT_ROUND( p[b] * alpha, N ); \ + \ + p += bands; \ + q += bands - 1; \ + } \ +} + +/* Flatten with shift + round, any background. + */ +#define VIPS_FLATTEN_INT( TYPE, N ) { \ + TYPE *p = (TYPE *) in; \ + TYPE *q = (TYPE *) out; \ + \ + for( x = 0; x < width; x++ ) { \ + TYPE alpha = p[bands - 1]; \ + TYPE nalpha = ((1 << (N)) - 1) - alpha; \ + TYPE *bg = (TYPE *) flatten->ink; \ + int b; \ + \ + for( b = 0; b < bands - 1; b++ ) \ + q[b] = SHIFT_ROUND( p[b] * alpha, (N) ) + \ + SHIFT_ROUND( bg[b] * nalpha, (N) ); \ + \ + p += bands; \ + q += bands - 1; \ + } \ +} + +/* Flatten via float division, black background. + */ +#define VIPS_FLATTEN_FLOAT_BLACK( TYPE, SCALE ) { \ + TYPE *p = (TYPE *) in; \ + TYPE *q = (TYPE *) out; \ + \ + for( x = 0; x < width; x++ ) { \ + TYPE alpha = p[bands - 1]; \ + int b; \ + \ + for( b = 0; b < bands - 1; b++ ) \ + q[b] = ((double) p[b] * alpha) / (SCALE); \ + \ + p += bands; \ + q += bands - 1; \ + } \ +} + +/* Flatten via float division, any background. + */ +#define VIPS_FLATTEN_FLOAT( TYPE, SCALE ) { \ + TYPE *p = (TYPE *) in; \ + TYPE *q = (TYPE *) out; \ + \ + for( x = 0; x < width; x++ ) { \ + TYPE alpha = p[bands - 1]; \ + TYPE nalpha = (SCALE) - alpha; \ + TYPE *bg = (TYPE *) flatten->ink; \ + int b; \ + \ + for( b = 0; b < bands - 1; b++ ) \ + q[b] = ((double) p[b] * alpha) / (SCALE) + \ + ((double) bg[b] * nalpha) / (SCALE); \ + \ + p += bands; \ + q += bands - 1; \ + } \ +} + +static int +vips_flatten_black_gen( VipsRegion *or, void *vseq, void *a, void *b, + gboolean *stop ) +{ + VipsRegion *ir = (VipsRegion *) vseq; + VipsFlatten *flatten = (VipsFlatten *) b; + VipsRect *r = &or->valid; + int width = r->width; + int bands = ir->im->Bands; + + int x, y; + + if( vips_region_prepare( ir, r ) ) + return( -1 ); + + for( y = 0; y < r->height; y++ ) { + VipsPel *in = VIPS_REGION_ADDR( ir, r->left, r->top + y ); + VipsPel *out = VIPS_REGION_ADDR( or, r->left, r->top + y ); + + switch( flatten->in->BandFmt ) { + case VIPS_FORMAT_UCHAR: + VIPS_FLATTEN_INT_BLACK( unsigned char, 8 ); + break; + + case VIPS_FORMAT_CHAR: + /* Alpha is 0 - 127? No idea, really. + */ + VIPS_FLATTEN_INT_BLACK( signed char, 7 ); + break; + + case VIPS_FORMAT_USHORT: + VIPS_FLATTEN_INT_BLACK( unsigned short, 16 ); + break; + + case VIPS_FORMAT_SHORT: + VIPS_FLATTEN_INT_BLACK( signed short, 15 ); + break; + + case VIPS_FORMAT_UINT: + VIPS_FLATTEN_FLOAT_BLACK( unsigned int, UINT_MAX ); + break; + + case VIPS_FORMAT_INT: + VIPS_FLATTEN_FLOAT_BLACK( signed int, INT_MAX ); + break; + + case VIPS_FORMAT_FLOAT: + VIPS_FLATTEN_FLOAT_BLACK( float, 1.0 ); + break; + + case VIPS_FORMAT_DOUBLE: + VIPS_FLATTEN_FLOAT_BLACK( double, 1.0 ); + break; + + case VIPS_FORMAT_COMPLEX: + case VIPS_FORMAT_DPCOMPLEX: + default: + g_assert( 0 ); + } + } + + return( 0 ); +} + +/* Any background. + */ +static int +vips_flatten_gen( VipsRegion *or, void *vseq, void *a, void *b, + gboolean *stop ) +{ + VipsRegion *ir = (VipsRegion *) vseq; + VipsFlatten *flatten = (VipsFlatten *) b; + VipsRect *r = &or->valid; + int width = r->width; + int bands = ir->im->Bands; + + int x, y; + + if( vips_region_prepare( ir, r ) ) + return( -1 ); + + for( y = 0; y < r->height; y++ ) { + VipsPel *in = VIPS_REGION_ADDR( ir, r->left, r->top + y ); + VipsPel *out = VIPS_REGION_ADDR( or, r->left, r->top + y ); + + switch( flatten->in->BandFmt ) { + case VIPS_FORMAT_UCHAR: + VIPS_FLATTEN_INT( unsigned char, 8 ); + break; + + case VIPS_FORMAT_CHAR: + /* Alpha is 0 - 127? No idea, really. + */ + VIPS_FLATTEN_INT( signed char, 7 ); + break; + + case VIPS_FORMAT_USHORT: + VIPS_FLATTEN_INT( unsigned short, 16 ); + break; + + case VIPS_FORMAT_SHORT: + VIPS_FLATTEN_INT( signed short, 15 ); + break; + + case VIPS_FORMAT_UINT: + VIPS_FLATTEN_FLOAT( unsigned int, UINT_MAX ); + break; + + case VIPS_FORMAT_INT: + VIPS_FLATTEN_FLOAT( signed int, INT_MAX ); + break; + + case VIPS_FORMAT_FLOAT: + VIPS_FLATTEN_FLOAT( float, 1.0 ); + break; + + case VIPS_FORMAT_DOUBLE: + VIPS_FLATTEN_FLOAT( double, 1.0 ); + break; + + case VIPS_FORMAT_COMPLEX: + case VIPS_FORMAT_DPCOMPLEX: + default: + g_assert( 0 ); + } + } + + return( 0 ); +} + + +static int +vips_flatten_build( VipsObject *object ) +{ + VipsConversion *conversion = VIPS_CONVERSION( object ); + VipsFlatten *flatten = (VipsFlatten *) object; + int i; + gboolean black; + + if( VIPS_OBJECT_CLASS( vips_flatten_parent_class )->build( object ) ) + return( -1 ); + + /* Trivial case: fall back to copy(). + */ + if( flatten->in->Bands == 1 ) + return( vips_image_write( flatten->in, conversion->out ) ); + + if( vips_check_uncoded( "VipsFlatten", flatten->in ) || + vips_check_noncomplex( "VipsFlatten", flatten->in ) || + vips_image_pio_input( flatten->in ) ) + return( -1 ); + + if( vips_image_copy_fields( conversion->out, flatten->in ) ) + return( -1 ); + vips_demand_hint( conversion->out, + VIPS_DEMAND_STYLE_THINSTRIP, flatten->in, NULL ); + + conversion->out->Bands -= 1; + + /* 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 ) { + black = FALSE; + break; + } + + if( black ) { + if( vips_image_generate( conversion->out, + vips_start_one, vips_flatten_black_gen, vips_stop_one, + flatten->in, flatten ) ) + return( -1 ); + } + else { + /* Convert the background to the image's format. + */ + if( !(flatten->ink = vips__vector_to_ink( + "VipsFlatten", conversion->out, + flatten->background->data, flatten->background->n )) ) + return( -1 ); + + if( vips_image_generate( conversion->out, + vips_start_one, vips_flatten_gen, vips_stop_one, + flatten->in, flatten ) ) + return( -1 ); + } + + return( 0 ); +} + +static void +vips_flatten_class_init( VipsFlattenClass *class ) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS( class ); + VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class ); + + VIPS_DEBUG_MSG( "vips_flatten_class_init\n" ); + + gobject_class->set_property = vips_object_set_property; + gobject_class->get_property = vips_object_get_property; + + vobject_class->nickname = "flatten"; + vobject_class->description = _( "flatten alpha out of an image" ); + vobject_class->build = vips_flatten_build; + + VIPS_ARG_IMAGE( class, "in", 1, + _( "Input" ), + _( "Input image" ), + VIPS_ARGUMENT_REQUIRED_INPUT, + G_STRUCT_OFFSET( VipsFlatten, in ) ); + + VIPS_ARG_BOXED( class, "background", 2, + _( "Background" ), + _( "Background value" ), + VIPS_ARGUMENT_OPTIONAL_INPUT, + G_STRUCT_OFFSET( VipsFlatten, background ), + VIPS_TYPE_ARRAY_DOUBLE ); +} + +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; +} + +/** + * vips_flatten: + * @in: input image + * @out: output image + * @...: %NULL-terminated list of optional named arguments + * + * Optional arguments: + * + * @background: colour for new pixels + * + * Take the last band of @in as an alpha and use it to blend the + * remaining channels with @background. + * + * The alpha channel is 0 - 255 for + * integer images and 0 - 1 for float images, where 255 means 100% image and 0 + * means 100% background. Non-complex images only. + * @background defaults to zero (black). + * + * Useful for flattening PNG images to RGB. + * + * See also: pngload(). + * + * Returns: 0 on success, -1 on error + */ +int +vips_flatten( VipsImage *in, VipsImage **out, ... ) +{ + va_list ap; + int result; + + va_start( ap, out ); + result = vips_call_split( "flatten", ap, in, out ); + va_end( ap ); + + return( result ); +} diff --git a/libvips/include/vips/conversion.h b/libvips/include/vips/conversion.h index 1a957d96..128c8241 100644 --- a/libvips/include/vips/conversion.h +++ b/libvips/include/vips/conversion.h @@ -216,10 +216,12 @@ int vips_black( VipsImage **out, int width, int height, ... ) __attribute__((sentinel)); int vips_rot( VipsImage *in, VipsImage **out, VipsAngle angle, ... ) __attribute__((sentinel)); + int vips_ifthenelse( VipsImage *cond, VipsImage *in1, VipsImage *in2, VipsImage **out, ... ) __attribute__((sentinel)); - +int vips_flatten( VipsImage *in, VipsImage **out, ... ) + __attribute__((sentinel)); diff --git a/libvips/include/vips/internal.h b/libvips/include/vips/internal.h index 86bc83d3..82aaab26 100644 --- a/libvips/include/vips/internal.h +++ b/libvips/include/vips/internal.h @@ -270,7 +270,8 @@ int im__fmaskcir( VipsImage *out, VipsMaskType flag, va_list ap ); /* inplace */ -VipsPel *im__vector_to_ink( const char *domain, VipsImage *im, int n, double *vec ); +VipsPel *vips__vector_to_ink( const char *domain, + VipsImage *im, double *vec, int n ); VipsImage *im__inplace_base( const char *domain, VipsImage *main, VipsImage *sub, VipsImage *out );