diff --git a/ChangeLog b/ChangeLog index d28743c8..5c39326d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -11,9 +11,12 @@ - added vips_error_freeze() / vips_error_thaw() - used freeze() / thaw() to stop file format sniffers logging spurious errors - vipsthumbnail uses embedded jpg thumbnails if it can +<<<<<<< HEAD - rename vips_diag() as vips_info(), add --vips-info flag - deprecate im_hsp() - support alpha for 8, 16 and 32-bit greyscale tiff images, thanks Robert +- added vips_webpload(), vips_webpload_buffer(), vips_webpsave(), + vips_webpsave_buffer(), vips_webpsave_mime() 3/7/13 started 7.34.2 - lower priority for Matlab load to reduce segvs from Mat_Open(), thanks diff --git a/TODO b/TODO index b06fe2f1..2af2c98d 100644 --- a/TODO +++ b/TODO @@ -6,6 +6,9 @@ not showing in 7.34, perhaps because asserts are off? +- use the webp advanced encoding api to set a write function for webp save to + file + - support planar config in tiff reader - is vips_hist_local() correct? it seems to leave white dots everywhere diff --git a/configure.ac b/configure.ac index a672c99e..ffedc5b5 100644 --- a/configure.ac +++ b/configure.ac @@ -528,6 +528,20 @@ if test x"$with_cfitsio" != "xno"; then ]) fi +# libwebp +AC_ARG_WITH([libwebp], + AS_HELP_STRING([--without-libwebp], [build without libwebp (default: test)])) + +if test x"$with_libwebp" != "xno"; then + PKG_CHECK_MODULES(LIBWEBP, libwebp, + [AC_DEFINE(HAVE_LIBWEBP,1,[define if you have libwebp installed.]) + with_libwebp=yes + PACKAGES_USED="$PACKAGES_USED libwebp"], + [AC_MSG_WARN([libwebp not found; disabling libwebp support]) + with_libwebp=no + ]) +fi + # pangoft2 AC_ARG_WITH([pangoft2], AS_HELP_STRING([--without-pangoft2], @@ -675,14 +689,14 @@ fi # Gather all up for VIPS_CFLAGS, VIPS_INCLUDES, VIPS_LIBS and VIPS_CXX_LIBS # sort includes to get longer, more specific dirs first # helps, for example, selecting graphicsmagick over imagemagick -VIPS_CFLAGS=`for i in $VIPS_CFLAGS $GTHREAD_CFLAGS $REQUIRED_CFLAGS $PANGOFT2_CFLAGS $FFTW_CFLAGS $MAGICK_CFLAGS $PNG_CFLAGS $EXIF_CFLAGS $MATIO_CFLAGS $CFITSIO_CFLAGS $OPENEXR_CFLAGS $OPENSLIDE_CFLAGS $ORC_CFLAGS $TIFF_CFLAGS $LCMS_CFLAGS +VIPS_CFLAGS=`for i in $VIPS_CFLAGS $GTHREAD_CFLAGS $REQUIRED_CFLAGS $PANGOFT2_CFLAGS $FFTW_CFLAGS $MAGICK_CFLAGS $PNG_CFLAGS $EXIF_CFLAGS $MATIO_CFLAGS $CFITSIO_CFLAGS $LIBWEBP_CFLAGS $OPENEXR_CFLAGS $OPENSLIDE_CFLAGS $ORC_CFLAGS $TIFF_CFLAGS $LCMS_CFLAGS do echo $i done | sort -ru` VIPS_CFLAGS=`echo $VIPS_CFLAGS` VIPS_CFLAGS="$VIPS_DEBUG_FLAGS $VIPS_CFLAGS" VIPS_INCLUDES="$PNG_INCLUDES $TIFF_INCLUDES $ZIP_INCLUDES $JPEG_INCLUDES" -VIPS_LIBS="$MAGICK_LIBS $PNG_LIBS $TIFF_LIBS $ZIP_LIBS $JPEG_LIBS $GTHREAD_LIBS $REQUIRED_LIBS $PANGOFT2_LIBS $FFTW_LIBS $ORC_LIBS $LCMS_LIBS $OPENEXR_LIBS $OPENSLIDE_LIBS $CFITSIO_LIBS $MATIO_LIBS $EXIF_LIBS -lm" +VIPS_LIBS="$MAGICK_LIBS $PNG_LIBS $TIFF_LIBS $ZIP_LIBS $JPEG_LIBS $GTHREAD_LIBS $REQUIRED_LIBS $PANGOFT2_LIBS $FFTW_LIBS $ORC_LIBS $LCMS_LIBS $OPENEXR_LIBS $OPENSLIDE_LIBS $CFITSIO_LIBS $LIBWEBP_LIBS $MATIO_LIBS $EXIF_LIBS -lm" # we need this to generate paths in swig/python/setup.py.in AC_SUBST(top_srcdir) @@ -759,6 +773,7 @@ file import with OpenSlide: $with_openslide (requires openslide-3.3.0 or later) file import with matio: $with_matio file import with cfitsio: $with_cfitsio +file import/export with libwebp: $with_libwebp text rendering with pangoft2: $with_pangoft2 file import/export with libpng: $with_png (requires libpng-1.2.9 or later) diff --git a/libvips/foreign/Makefile.am b/libvips/foreign/Makefile.am index e332a5da..a027cf84 100644 --- a/libvips/foreign/Makefile.am +++ b/libvips/foreign/Makefile.am @@ -50,6 +50,11 @@ libforeign_la_SOURCES = \ openslide2vips.h \ openslide2vips.c \ openslideload.c \ + webp.h \ + webpload.c \ + webpsave.c \ + webp2vips.c \ + vips2webp.c \ vips2jpeg.c \ jpeg2vips.c \ jpeg.h \ diff --git a/libvips/foreign/foreign.c b/libvips/foreign/foreign.c index feafc6fb..e0e87ac1 100644 --- a/libvips/foreign/foreign.c +++ b/libvips/foreign/foreign.c @@ -1609,6 +1609,10 @@ vips_foreign_operation_init( void ) extern GType vips_foreign_save_raw_fd_get_type( void ); extern GType vips_foreign_load_magick_get_type( void ); extern GType vips_foreign_save_dz_get_type( void ); + extern GType vips_foreign_load_webp_file_get_type( void ); + extern GType vips_foreign_load_webp_buffer_get_type( void ); + extern GType vips_foreign_save_webp_file_get_type( void ); + extern GType vips_foreign_save_webp_buffer_get_type( void ); vips_foreign_load_rad_get_type(); vips_foreign_save_rad_get_type(); @@ -1645,6 +1649,13 @@ vips_foreign_operation_init( void ) vips_foreign_save_jpeg_mime_get_type(); #endif /*HAVE_JPEG*/ +#ifdef HAVE_LIBWEBP + vips_foreign_load_webp_file_get_type(); + vips_foreign_load_webp_buffer_get_type(); + vips_foreign_save_webp_file_get_type(); + vips_foreign_save_webp_buffer_get_type(); +#endif /*HAVE_LIBWEBP*/ + #ifdef HAVE_TIFF vips_foreign_load_tiff_get_type(); vips_foreign_save_tiff_get_type(); @@ -1946,29 +1957,51 @@ vips_jpegload( const char *filename, VipsImage **out, ... ) } /** - * vips_jpegsave_mime: + * vips_jpegsave: * @in: image to save + * @filename: file to write to * @...: %NULL-terminated list of optional named arguments * * Optional arguments: * - * @Q: JPEG quality factor + * @Q: quality factor * @profile: attach this ICC profile * - * As vips_jpegsave(), but save as a mime jpeg on stdout. + * Write a VIPS image to a file as JPEG. * - * See also: vips_jpegsave(), vips_image_write_to_file(). + * Use @Q to set the JPEG compression factor. Default 75. + * + * Use @profile to give the filename of a profile to be embedded in the JPEG. + * This does not affect the pixels which are written, just the way + * they are tagged. You can use the special string "none" to mean + * "don't attach a profile". + * + * If no profile is specified and the VIPS header + * contains an ICC profile named VIPS_META_ICC_NAME ("icc-profile-data"), the + * profile from the VIPS header will be attached. + * + * The image is automatically converted to RGB, Monochrome or CMYK before + * saving. + * + * EXIF data is constructed from @VIPS_META_EXIF_NAME ("exif-data"), then + * modified with any other related tags on the image before being written to + * the file. + * + * IPCT as @VIPS_META_IPCT_NAME ("ipct-data") and XMP as VIPS_META_XMP_NAME + * ("xmp-data") are coded and attached. + * + * See also: vips_jpegsave_buffer(), vips_image_write_file(). * * Returns: 0 on success, -1 on error. */ int -vips_jpegsave_mime( VipsImage *in, ... ) +vips_jpegsave( VipsImage *in, const char *filename, ... ) { va_list ap; int result; - va_start( ap, in ); - result = vips_call_split( "jpegsave_mime", ap, in ); + va_start( ap, filename ); + result = vips_call_split( "jpegsave", ap, in, filename ); va_end( ap ); return( result ); @@ -2025,7 +2058,95 @@ vips_jpegsave_buffer( VipsImage *in, void **buf, size_t *len, ... ) } /** - * vips_jpegsave: + * vips_jpegsave_mime: + * @in: image to save + * @...: %NULL-terminated list of optional named arguments + * + * Optional arguments: + * + * @Q: JPEG quality factor + * @profile: attach this ICC profile + * + * As vips_jpegsave(), but save as a mime jpeg on stdout. + * + * See also: vips_jpegsave(), vips_image_write_to_file(). + * + * Returns: 0 on success, -1 on error. + */ +int +vips_jpegsave_mime( VipsImage *in, ... ) +{ + va_list ap; + int result; + + va_start( ap, in ); + result = vips_call_split( "jpegsave_mime", ap, in ); + va_end( ap ); + + return( result ); +} + +/** + * vips_webpload: + * @filename: file to load + * @out: decompressed image + * @...: %NULL-terminated list of optional named arguments + * + * Optional arguments: + * + * + * Read a webp file into a VIPS image. + * + * See also: + * + * Returns: 0 on success, -1 on error. + */ +int +vips_webpload( const char *filename, VipsImage **out, ... ) +{ + va_list ap; + int result; + + va_start( ap, out ); + result = vips_call_split( "webpload", ap, filename, out ); + va_end( ap ); + + return( result ); +} + +/** + * vips_webpload_buffer: + * @buf: memory area to load + * @len: size of memory area + * @out: image to write + * @...: %NULL-terminated list of optional named arguments + * + * See also: + * + * Returns: 0 on success, -1 on error. + */ +int +vips_webpload_buffer( void *buf, size_t len, VipsImage **out, ... ) +{ + va_list ap; + VipsArea *area; + int result; + + /* We don't take a copy of the data or free it. + */ + area = vips_area_new_blob( NULL, buf, len ); + + va_start( ap, out ); + result = vips_call_split( "webpload_buffer", ap, area, out ); + va_end( ap ); + + vips_area_unref( area ); + + return( result ); +} + +/** + * vips_webpsave: * @in: image to save * @filename: file to write to * @...: %NULL-terminated list of optional named arguments @@ -2033,43 +2154,88 @@ vips_jpegsave_buffer( VipsImage *in, void **buf, size_t *len, ... ) * Optional arguments: * * @Q: quality factor - * @profile: attach this ICC profile * - * Write a VIPS image to a file as JPEG. - * - * Use @Q to set the JPEG compression factor. Default 75. - * - * Use @profile to give the filename of a profile to be embedded in the JPEG. - * This does not affect the pixels which are written, just the way - * they are tagged. You can use the special string "none" to mean - * "don't attach a profile". - * - * If no profile is specified and the VIPS header - * contains an ICC profile named VIPS_META_ICC_NAME ("icc-profile-data"), the - * profile from the VIPS header will be attached. - * - * The image is automatically converted to RGB, Monochrome or CMYK before - * saving. - * - * EXIF data is constructed from @VIPS_META_EXIF_NAME ("exif-data"), then - * modified with any other related tags on the image before being written to - * the file. - * - * IPCT as @VIPS_META_IPCT_NAME ("ipct-data") and XMP as VIPS_META_XMP_NAME - * ("xmp-data") are coded and attached. - * - * See also: vips_jpegsave_buffer(), vips_image_write_file(). + * See also: * * Returns: 0 on success, -1 on error. */ int -vips_jpegsave( VipsImage *in, const char *filename, ... ) +vips_webpsave( VipsImage *in, const char *filename, ... ) { va_list ap; int result; va_start( ap, filename ); - result = vips_call_split( "jpegsave", ap, in, filename ); + result = vips_call_split( "webpsave", ap, in, filename ); + va_end( ap ); + + return( result ); +} + +/** + * vips_webpsave_buffer: + * @in: image to save + * @buf: return output buffer here + * @len: return output length here + * @...: %NULL-terminated list of optional named arguments + * + * Optional arguments: + * + * @Q: JPEG quality factor + * + * See also: + * + * Returns: 0 on success, -1 on error. + */ +int +vips_webpsave_buffer( VipsImage *in, void **buf, size_t *len, ... ) +{ + va_list ap; + VipsArea *area; + int result; + + area = NULL; + + va_start( ap, len ); + result = vips_call_split( "webpsave_buffer", ap, in, &area ); + va_end( ap ); + + if( !result && + area ) { + if( buf ) { + *buf = area->data; + area->free_fn = NULL; + } + if( buf ) + *len = area->length; + + vips_area_unref( area ); + } + + return( result ); +} + +/** + * vips_webpsave_mime: + * @in: image to save + * @...: %NULL-terminated list of optional named arguments + * + * Optional arguments: + * + * @Q: quality factor + * + * See also: + * + * Returns: 0 on success, -1 on error. + */ +int +vips_webpsave_mime( VipsImage *in, ... ) +{ + va_list ap; + int result; + + va_start( ap, in ); + result = vips_call_split( "webpsave_mime", ap, in ); va_end( ap ); return( result ); diff --git a/libvips/foreign/vips2webp.c b/libvips/foreign/vips2webp.c new file mode 100644 index 00000000..e7186b68 --- /dev/null +++ b/libvips/foreign/vips2webp.c @@ -0,0 +1,149 @@ +/* wrap libwebp libray for write + * + * 6/8/13 + * - from vips2jpeg.c + */ + +/* + + 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., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA + + */ + +/* + + These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk + + */ + +/* +#define DEBUG +#define VIPS_DEBUG + */ + +#ifdef HAVE_CONFIG_H +#include +#endif /*HAVE_CONFIG_H*/ +#include + +#ifdef HAVE_LIBWEBP + +#include + +#include + +#include + +#include "webp.h" + +typedef size_t (*webp_encoder)( const uint8_t *rgb, + int width, int height, int stride, + float quality_factor, uint8_t **output ); + +typedef size_t (*webp_encoder_lossless)( const uint8_t *rgb, + int width, int height, int stride, uint8_t **output ); + +int +vips__webp_write_file( VipsImage *in, const char *filename, + int Q, gboolean lossless ) +{ + size_t len; + uint8_t *buffer; + FILE *fp; + + if( vips_image_wio_input( in ) ) + return( -1 ); + + if( lossless ) { + webp_encoder_lossless encoder; + + if( in->Bands == 4 ) + encoder = WebPEncodeLosslessRGBA; + else + encoder = WebPEncodeLosslessRGB; + + if( !(len = encoder( VIPS_IMAGE_ADDR( in, 0, 0 ), + in->Xsize, in->Ysize, + VIPS_IMAGE_SIZEOF_LINE( in ), + &buffer )) ) { + vips_error( "vips2webp", + "%s", _( "unable to encode" ) ); + return( -1 ); + } + } + else { + webp_encoder encoder; + + if( in->Bands == 4 ) + encoder = WebPEncodeRGBA; + else + encoder = WebPEncodeRGB; + + if( !(len = encoder( VIPS_IMAGE_ADDR( in, 0, 0 ), + in->Xsize, in->Ysize, + VIPS_IMAGE_SIZEOF_LINE( in ), + Q, &buffer )) ) { + vips_error( "vips2webp", + "%s", _( "unable to encode" ) ); + return( -1 ); + } + } + + if( !(fp = vips__file_open_write( filename, FALSE )) ) { + free( buffer ); + return( -1 ); + } + + if( vips__file_write( buffer, len, 1, fp ) ) { + fclose( fp ); + free( buffer ); + return( -1 ); + } + + fclose( fp ); + free( buffer ); + + return( 0 ); +} + +int +vips__webp_write_buffer( VipsImage *in, void **obuf, size_t *olen, + int Q, gboolean lossless ) +{ + webp_encoder encoder; + + if( vips_image_wio_input( in ) ) + return( -1 ); + + if( in->Bands == 4 ) + encoder = WebPEncodeRGBA; + else + encoder = WebPEncodeRGB; + + if( !(*olen = encoder( VIPS_IMAGE_ADDR( in, 0, 0 ), + in->Xsize, in->Ysize, + VIPS_IMAGE_SIZEOF_LINE( in ), + Q, (uint8_t **) obuf )) ) { + vips_error( "vips2webp", "%s", _( "unable to encode" ) ); + return( -1 ); + } + + return( 0 ); +} + +#endif /*HAVE_LIBWEBP*/ diff --git a/libvips/foreign/webp.h b/libvips/foreign/webp.h new file mode 100644 index 00000000..7441b893 --- /dev/null +++ b/libvips/foreign/webp.h @@ -0,0 +1,57 @@ +/* common defs for webp read/write + */ + +/* + + Copyright (C) 1991-2005 The National Gallery + + This library 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.1 of the License, or (at your option) any later version. + + This library 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 library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA + + */ + +/* + + These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk + + */ + +#ifndef VIPS_WEBP_H +#define VIPS_WEBP_H + +#ifdef __cplusplus +extern "C" { +#endif /*__cplusplus*/ + +extern const char *vips__webp_suffs[]; + +int vips__iswebp( const char *filename ); + +int vips__webp_read_file_header( const char *name, VipsImage *out ); +int vips__webp_read_file( const char *name, VipsImage *out ); + +int vips__webp_read_buffer_header( void *buf, size_t len, VipsImage *out ); +int vips__webp_read_buffer( void *buf, size_t len, VipsImage *out ); + +int vips__webp_write_file( VipsImage *out, const char *filename, + int Q, gboolean lossless ); +int vips__webp_write_buffer( VipsImage *out, void **buf, size_t *len, + int Q, gboolean lossless ); + +#ifdef __cplusplus +} +#endif /*__cplusplus*/ + +#endif /*VIPS_WEBP_H*/ diff --git a/libvips/foreign/webp2vips.c b/libvips/foreign/webp2vips.c new file mode 100644 index 00000000..4996e5c2 --- /dev/null +++ b/libvips/foreign/webp2vips.c @@ -0,0 +1,295 @@ +/* read with libwebp + * + * 6/8/13 + * - from webp2vips.c + */ + +/* + + 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., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA + + */ + +/* + + These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk + + */ + +/* + */ +#define DEBUG_VERBOSE +#define DEBUG + +#ifdef HAVE_CONFIG_H +#include +#endif /*HAVE_CONFIG_H*/ +#include + +#ifdef HAVE_LIBWEBP + +#include + +#include + +#include +#include + +#include "webp.h" + +/* How many bytes do we need to read from the start of the file to be able to + * validate the header? + * + * This doesn't seem to be documented anywhere :-( guess a value. + */ +#define MINIMAL_HEADER (100) + +/* What we track during a read. + */ +typedef struct { + /* File source. + */ + char *filename; + + /* Memory source. + */ + void *buf; + size_t len; + + /* Decoder config. + */ + WebPDecoderConfig config; + + /* Incremental decoder state. + */ + WebPIDecoder *idec; +} Read; + +int +vips__iswebp( const char *filename ) +{ + unsigned char header[MINIMAL_HEADER]; + + if( vips__get_bytes( filename, header, MINIMAL_HEADER ) && + WebPGetInfo( header, MINIMAL_HEADER, NULL, NULL ) ) + return( 1 ); + + return( 0 ); +} + +static int +read_free( Read *read ) +{ + VIPS_FREE( read->filename ); + VIPS_FREEF( WebPIDelete, read->idec ); + WebPFreeDecBuffer( &read->config.output ); + VIPS_FREE( read ); + + return( 0 ); +} + +static Read * +read_new( const char *filename, void *buf, size_t len ) +{ + Read *read; + unsigned char header[MINIMAL_HEADER]; + + if( !(read = VIPS_NEW( NULL, Read )) ) + return( NULL ); + + read->filename = g_strdup( filename ); + read->buf = buf; + read->len = len; + read->idec = NULL; + + WebPInitDecoderConfig( &read->config ); + if( filename ) { + if( vips__get_bytes( filename, header, MINIMAL_HEADER ) && + WebPGetFeatures( header, MINIMAL_HEADER, + &read->config.input ) != VP8_STATUS_OK ) { + read_free( read ); + return( NULL ); + } + } + else { + if( WebPGetFeatures( read->buf, read->len, + &read->config.input ) != VP8_STATUS_OK ) { + read_free( read ); + return( NULL ); + } + } + + if( read->config.input.has_alpha ) + read->config.output.colorspace = MODE_RGBA; + else + read->config.output.colorspace = MODE_RGB; + read->config.options.use_threads = TRUE; + + return( read ); +} + +static int +read_header( Read *read, VipsImage *out ) +{ + vips_image_init_fields( out, + read->config.input.width, read->config.input.height, + read->config.input.has_alpha ? 4 : 3, + VIPS_FORMAT_UCHAR, VIPS_CODING_NONE, + VIPS_INTERPRETATION_sRGB, + 1.0, 1.0 ); + + vips_demand_hint( out, VIPS_DEMAND_STYLE_THINSTRIP, NULL ); + + return( 0 ); +} + +int +vips__webp_read_file_header( const char *filename, VipsImage *out ) +{ + Read *read; + + if( !(read = read_new( filename, NULL, 0 )) ) { + vips_error( "webp2vips", + _( "unable to open \"%s\"" ), filename ); + return( -1 ); + } + + if( read_header( read, out ) ) + return( -1 ); + + read_free( read ); + + return( 0 ); +} + +typedef uint8_t *(*webp_decoder)( const uint8_t* data, size_t data_size, + uint8_t* output_buffer, size_t output_buffer_size, + int output_stride ); + +static int +read_image( Read *read, VipsImage *out ) +{ + VipsImage **t = (VipsImage **) + vips_object_local_array( VIPS_OBJECT( out ), 3 ); + guint64 length; + void *data; + int fd; + webp_decoder decoder; + + /* libwebp makes streaming very hard. We have to read to a full memory + * buffer, then copy to out. + * + * mmap the input file, it's slightly quicker. + */ + t[0] = vips_image_new_buffer(); + if( read_header( read, t[0] ) ) + return( -1 ); + if( vips_image_write_prepare( t[0] ) ) + return( -1 ); + + if( !(fd = vips__open_image_read( read->filename )) ) + return( -1 ); + if( (length = vips_file_length( fd )) < 0 ) { + vips_tracked_close( fd ); + return( -1 ); + } + if( !(data = vips__mmap( fd, FALSE, length, 0 )) ) { + vips_tracked_close( fd ); + return( -1 ); + } + + if( t[0]->Bands == 3 ) + decoder = WebPDecodeRGBInto; + else + decoder = WebPDecodeRGBAInto; + + if( !decoder( (uint8_t *) data, length, + VIPS_IMAGE_ADDR( t[0], 0, 0 ), + VIPS_IMAGE_SIZEOF_IMAGE( t[0] ), + VIPS_IMAGE_SIZEOF_LINE( t[0] ) ) ) { + vips__munmap( data, length ); + vips_tracked_close( fd ); + vips_error( "webp2vips", "%s", _( "unable to read pixels" ) ); + return( -1 ); + } + + vips__munmap( data, length ); + vips_tracked_close( fd ); + + if( vips_image_write( t[0], out ) ) + return( -1 ); + + return( 0 ); +} + +int +vips__webp_read_file( const char *filename, VipsImage *out ) +{ + Read *read; + + if( !(read = read_new( filename, NULL, 0 )) ) { + vips_error( "webp2vips", + _( "unable to open \"%s\"" ), filename ); + return( -1 ); + } + + if( read_image( read, out ) ) + return( -1 ); + + read_free( read ); + + return( 0 ); +} + +int +vips__webp_read_buffer_header( void *buf, size_t len, VipsImage *out ) +{ + Read *read; + + if( !(read = read_new( NULL, buf, len )) ) { + vips_error( "webp2vips", + "%s", _( "unable to open buffer" ) ); + return( -1 ); + } + + if( read_header( read, out ) ) + return( -1 ); + + read_free( read ); + + return( 0 ); +} + +int +vips__webp_read_buffer( void *buf, size_t len, VipsImage *out ) +{ + Read *read; + + if( !(read = read_new( NULL, buf, len )) ) { + vips_error( "webp2vips", + "%s", _( "unable to open buffer" ) ); + return( -1 ); + } + + if( read_image( read, out ) ) + return( -1 ); + + return( 0 ); +} + +#endif /*HAVE_LIBWEBP*/ diff --git a/libvips/foreign/webpload.c b/libvips/foreign/webpload.c new file mode 100644 index 00000000..d29314f2 --- /dev/null +++ b/libvips/foreign/webpload.c @@ -0,0 +1,258 @@ +/* load webp images + * + * 6/8/13 + * - from webpload.c + */ + +/* + + 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., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA + + */ + +/* + + These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk + + */ + +/* +#define DEBUG_VERBOSE +#define DEBUG + */ + +#ifdef HAVE_CONFIG_H +#include +#endif /*HAVE_CONFIG_H*/ +#include + +#ifdef HAVE_LIBWEBP + +#include + +#include "webp.h" + +typedef struct _VipsForeignLoadWebp { + VipsForeignLoad parent_object; + +} VipsForeignLoadWebp; + +typedef VipsForeignLoadClass VipsForeignLoadWebpClass; + +G_DEFINE_ABSTRACT_TYPE( VipsForeignLoadWebp, vips_foreign_load_webp, + VIPS_TYPE_FOREIGN_LOAD ); + +static VipsForeignFlags +vips_foreign_load_webp_get_flags( VipsForeignLoad *load ) +{ + return( 0 ); +} + +static int +vips_foreign_load_webp_build( VipsObject *object ) +{ + VipsForeignLoadWebp *webp = (VipsForeignLoadWebp *) object; + + if( VIPS_OBJECT_CLASS( vips_foreign_load_webp_parent_class )-> + build( object ) ) + return( -1 ); + + return( 0 ); +} + +static void +vips_foreign_load_webp_class_init( VipsForeignLoadWebpClass *class ) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS( class ); + VipsObjectClass *object_class = (VipsObjectClass *) class; + VipsForeignLoadClass *load_class = (VipsForeignLoadClass *) class; + + gobject_class->set_property = vips_object_set_property; + gobject_class->get_property = vips_object_get_property; + + object_class->nickname = "webpload_base"; + object_class->description = _( "load webp" ); + object_class->build = vips_foreign_load_webp_build; + + load_class->get_flags = vips_foreign_load_webp_get_flags; + +} + +static void +vips_foreign_load_webp_init( VipsForeignLoadWebp *webp ) +{ +} + +typedef struct _VipsForeignLoadWebpFile { + VipsForeignLoadWebp parent_object; + + /* Filename for load. + */ + char *filename; + +} VipsForeignLoadWebpFile; + +typedef VipsForeignLoadWebpClass VipsForeignLoadWebpFileClass; + +G_DEFINE_TYPE( VipsForeignLoadWebpFile, vips_foreign_load_webp_file, + vips_foreign_load_webp_get_type() ); + +static VipsForeignFlags +vips_foreign_load_webp_file_get_flags_filename( const char *filename ) +{ + return( 0 ); +} + +static gboolean +vips_foreign_load_webp_file_is_a( const char *filename ) +{ + return( vips__iswebp( filename ) ); +} + +static int +vips_foreign_load_webp_file_header( VipsForeignLoad *load ) +{ + VipsForeignLoadWebp *webp = (VipsForeignLoadWebp *) load; + VipsForeignLoadWebpFile *file = (VipsForeignLoadWebpFile *) load; + + if( vips__webp_read_file_header( file->filename, load->out ) ) + return( -1 ); + + return( 0 ); +} + +static int +vips_foreign_load_webp_file_load( VipsForeignLoad *load ) +{ + VipsForeignLoadWebp *webp = (VipsForeignLoadWebp *) load; + VipsForeignLoadWebpFile *file = (VipsForeignLoadWebpFile *) load; + + if( vips__webp_read_file( file->filename, load->real ) ) + return( -1 ); + + return( 0 ); +} + +const char *vips__webp_suffs[] = { ".webp", NULL }; + +static void +vips_foreign_load_webp_file_class_init( VipsForeignLoadWebpFileClass *class ) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS( class ); + VipsObjectClass *object_class = (VipsObjectClass *) class; + VipsForeignClass *foreign_class = (VipsForeignClass *) class; + VipsForeignLoadClass *load_class = (VipsForeignLoadClass *) class; + + gobject_class->set_property = vips_object_set_property; + gobject_class->get_property = vips_object_get_property; + + object_class->nickname = "webpload"; + object_class->description = _( "load webp from file" ); + + foreign_class->suffs = vips__webp_suffs; + + load_class->get_flags_filename = + vips_foreign_load_webp_file_get_flags_filename; + load_class->is_a = vips_foreign_load_webp_file_is_a; + load_class->header = vips_foreign_load_webp_file_header; + load_class->load = vips_foreign_load_webp_file_load; + + VIPS_ARG_STRING( class, "filename", 1, + _( "Filename" ), + _( "Filename to load from" ), + VIPS_ARGUMENT_REQUIRED_INPUT, + G_STRUCT_OFFSET( VipsForeignLoadWebpFile, filename ), + NULL ); +} + +static void +vips_foreign_load_webp_file_init( VipsForeignLoadWebpFile *file ) +{ +} + +typedef struct _VipsForeignLoadWebpBuffer { + VipsForeignLoadWebp parent_object; + + /* Load from a buffer. + */ + VipsArea *buf; + +} VipsForeignLoadWebpBuffer; + +typedef VipsForeignLoadWebpClass VipsForeignLoadWebpBufferClass; + +G_DEFINE_TYPE( VipsForeignLoadWebpBuffer, vips_foreign_load_webp_buffer, + vips_foreign_load_webp_get_type() ); + +static int +vips_foreign_load_webp_buffer_header( VipsForeignLoad *load ) +{ + VipsForeignLoadWebp *webp = (VipsForeignLoadWebp *) load; + VipsForeignLoadWebpBuffer *buffer = (VipsForeignLoadWebpBuffer *) load; + + if( vips__webp_read_buffer_header( buffer->buf->data, + buffer->buf->length, load->out ) ) + return( -1 ); + + return( 0 ); +} + +static int +vips_foreign_load_webp_buffer_load( VipsForeignLoad *load ) +{ + VipsForeignLoadWebp *webp = (VipsForeignLoadWebp *) load; + VipsForeignLoadWebpBuffer *buffer = (VipsForeignLoadWebpBuffer *) load; + + if( vips__webp_read_buffer( buffer->buf->data, buffer->buf->length, + load->real ) ) + return( -1 ); + + return( 0 ); +} + +static void +vips_foreign_load_webp_buffer_class_init( + VipsForeignLoadWebpBufferClass *class ) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS( class ); + VipsObjectClass *object_class = (VipsObjectClass *) class; + VipsForeignLoadClass *load_class = (VipsForeignLoadClass *) class; + + gobject_class->set_property = vips_object_set_property; + gobject_class->get_property = vips_object_get_property; + + object_class->nickname = "webpload_buffer"; + object_class->description = _( "load webp from buffer" ); + + load_class->header = vips_foreign_load_webp_buffer_header; + load_class->load = vips_foreign_load_webp_buffer_load; + + VIPS_ARG_BOXED( class, "buffer", 1, + _( "Buffer" ), + _( "Buffer to load from" ), + VIPS_ARGUMENT_REQUIRED_INPUT, + G_STRUCT_OFFSET( VipsForeignLoadWebpBuffer, buf ), + VIPS_TYPE_BLOB ); +} + +static void +vips_foreign_load_webp_buffer_init( VipsForeignLoadWebpBuffer *buffer ) +{ +} + +#endif /*HAVE_LIBWEBP*/ diff --git a/libvips/foreign/webpsave.c b/libvips/foreign/webpsave.c new file mode 100644 index 00000000..432e6e96 --- /dev/null +++ b/libvips/foreign/webpsave.c @@ -0,0 +1,302 @@ +/* save to webp + * + * 24/11/11 + * - wrap a class around the webp writer + */ + +/* + + 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., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA + + */ + +/* + + These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk + + */ + +/* +#define DEBUG_VERBOSE +#define DEBUG + */ + +#ifdef HAVE_CONFIG_H +#include +#endif /*HAVE_CONFIG_H*/ +#include + +#ifdef HAVE_LIBWEBP + +#include + +#include + +#include "webp.h" + +typedef struct _VipsForeignSaveWebp { + VipsForeignSave parent_object; + + /* Quality factor. + */ + int Q; + + /* Turn on lossless encode. + */ + gboolean lossless; + +} VipsForeignSaveWebp; + +typedef VipsForeignSaveClass VipsForeignSaveWebpClass; + +G_DEFINE_ABSTRACT_TYPE( VipsForeignSaveWebp, vips_foreign_save_webp, + VIPS_TYPE_FOREIGN_SAVE ); + +#define UC VIPS_FORMAT_UCHAR + +/* Type promotion for save ... just always go to uchar. + */ +static int bandfmt_webp[10] = { +/* UC C US S UI I F X D DX */ + UC, UC, UC, UC, UC, UC, UC, UC, UC, UC +}; + +static void +vips_foreign_save_webp_class_init( VipsForeignSaveWebpClass *class ) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS( class ); + VipsObjectClass *object_class = (VipsObjectClass *) class; + VipsForeignSaveClass *save_class = (VipsForeignSaveClass *) class; + + gobject_class->set_property = vips_object_set_property; + gobject_class->get_property = vips_object_get_property; + + object_class->nickname = "webpsave_base"; + object_class->description = _( "save webp" ); + + save_class->saveable = VIPS_SAVEABLE_RGBA; + save_class->format_table = bandfmt_webp; + + VIPS_ARG_INT( class, "Q", 10, + _( "Q" ), + _( "Q factor" ), + VIPS_ARGUMENT_OPTIONAL_INPUT, + G_STRUCT_OFFSET( VipsForeignSaveWebp, Q ), + 1, 100, 75 ); + + VIPS_ARG_BOOL( class, "lossless", 11, + _( "lossless" ), + _( "enable lossless compression" ), + VIPS_ARGUMENT_OPTIONAL_INPUT, + G_STRUCT_OFFSET( VipsForeignSaveWebp, lossless ), + FALSE ); + +} + +static void +vips_foreign_save_webp_init( VipsForeignSaveWebp *webp ) +{ + webp->Q = 80; +} + +typedef struct _VipsForeignSaveWebpFile { + VipsForeignSaveWebp parent_object; + + /* Filename for save. + */ + char *filename; + +} VipsForeignSaveWebpFile; + +typedef VipsForeignSaveWebpClass VipsForeignSaveWebpFileClass; + +G_DEFINE_TYPE( VipsForeignSaveWebpFile, vips_foreign_save_webp_file, + vips_foreign_save_webp_get_type() ); + +static int +vips_foreign_save_webp_file_build( VipsObject *object ) +{ + VipsForeignSave *save = (VipsForeignSave *) object; + VipsForeignSaveWebp *webp = (VipsForeignSaveWebp *) object; + VipsForeignSaveWebpFile *file = (VipsForeignSaveWebpFile *) object; + + if( VIPS_OBJECT_CLASS( vips_foreign_save_webp_file_parent_class )-> + build( object ) ) + return( -1 ); + + if( vips__webp_write_file( save->ready, file->filename, + webp->Q, webp->lossless ) ) + return( -1 ); + + return( 0 ); +} + +static void +vips_foreign_save_webp_file_class_init( VipsForeignSaveWebpFileClass *class ) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS( class ); + VipsObjectClass *object_class = (VipsObjectClass *) class; + VipsForeignClass *foreign_class = (VipsForeignClass *) class; + + gobject_class->set_property = vips_object_set_property; + gobject_class->get_property = vips_object_get_property; + + object_class->nickname = "webpsave"; + object_class->description = _( "save image to webp file" ); + object_class->build = vips_foreign_save_webp_file_build; + + foreign_class->suffs = vips__webp_suffs; + + VIPS_ARG_STRING( class, "filename", 1, + _( "Filename" ), + _( "Filename to save to" ), + VIPS_ARGUMENT_REQUIRED_INPUT, + G_STRUCT_OFFSET( VipsForeignSaveWebpFile, filename ), + NULL ); +} + +static void +vips_foreign_save_webp_file_init( VipsForeignSaveWebpFile *file ) +{ +} + +typedef struct _VipsForeignSaveWebpBuffer { + VipsForeignSaveWebp parent_object; + + /* Save to a buffer. + */ + VipsArea *buf; + +} VipsForeignSaveWebpBuffer; + +typedef VipsForeignSaveWebpClass VipsForeignSaveWebpBufferClass; + +G_DEFINE_TYPE( VipsForeignSaveWebpBuffer, vips_foreign_save_webp_buffer, + vips_foreign_save_webp_get_type() ); + +static int +vips_foreign_save_webp_buffer_build( VipsObject *object ) +{ + VipsForeignSave *save = (VipsForeignSave *) object; + VipsForeignSaveWebp *webp = (VipsForeignSaveWebp *) object; + VipsForeignSaveWebpBuffer *file = (VipsForeignSaveWebpBuffer *) object; + + void *obuf; + size_t olen; + VipsArea *area; + + if( VIPS_OBJECT_CLASS( vips_foreign_save_webp_buffer_parent_class )-> + build( object ) ) + return( -1 ); + + if( vips__webp_write_buffer( save->ready, &obuf, &olen, + webp->Q, webp->lossless ) ) + return( -1 ); + + area = vips_area_new_blob( (VipsCallbackFn) vips_free, obuf, olen ); + g_object_set( file, "buffer", area, NULL ); + vips_area_unref( area ); + + return( 0 ); +} + +static void +vips_foreign_save_webp_buffer_class_init( + VipsForeignSaveWebpBufferClass *class ) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS( class ); + VipsObjectClass *object_class = (VipsObjectClass *) class; + + gobject_class->set_property = vips_object_set_property; + gobject_class->get_property = vips_object_get_property; + + object_class->nickname = "webpsave_buffer"; + object_class->description = _( "save image to webp buffer" ); + object_class->build = vips_foreign_save_webp_buffer_build; + + VIPS_ARG_BOXED( class, "buffer", 1, + _( "Buffer" ), + _( "Buffer to save to" ), + VIPS_ARGUMENT_REQUIRED_OUTPUT, + G_STRUCT_OFFSET( VipsForeignSaveWebpBuffer, buf ), + VIPS_TYPE_BLOB ); +} + +static void +vips_foreign_save_webp_buffer_init( VipsForeignSaveWebpBuffer *file ) +{ +} + +typedef struct _VipsForeignSaveWebpMime { + VipsForeignSaveWebp parent_object; + +} VipsForeignSaveWebpMime; + +typedef VipsForeignSaveWebpClass VipsForeignSaveWebpMimeClass; + +G_DEFINE_TYPE( VipsForeignSaveWebpMime, vips_foreign_save_webp_mime, + vips_foreign_save_webp_get_type() ); + +static int +vips_foreign_save_webp_mime_build( VipsObject *object ) +{ + VipsForeignSave *save = (VipsForeignSave *) object; + VipsForeignSaveWebp *webp = (VipsForeignSaveWebp *) object; + + void *obuf; + size_t olen; + + if( VIPS_OBJECT_CLASS( vips_foreign_save_webp_mime_parent_class )-> + build( object ) ) + return( -1 ); + + if( vips__webp_write_buffer( save->ready, &obuf, &olen, + webp->Q, webp->lossless ) ) + return( -1 ); + + printf( "Content-length: %zd\r\n", olen ); + printf( "Content-type: image/webp\r\n" ); + printf( "\r\n" ); + if( fwrite( obuf, sizeof( char ), olen, stdout ) != olen ) { + vips_error( "VipsWebp", "%s", _( "error writing output" ) ); + return( -1 ); + } + fflush( stdout ); + + g_free( obuf ); + + return( 0 ); +} + +static void +vips_foreign_save_webp_mime_class_init( VipsForeignSaveWebpMimeClass *class ) +{ + VipsObjectClass *object_class = (VipsObjectClass *) class; + + object_class->nickname = "webpsave_mime"; + object_class->description = _( "save image to webp mime" ); + object_class->build = vips_foreign_save_webp_mime_build; + +} + +static void +vips_foreign_save_webp_mime_init( VipsForeignSaveWebpMime *mime ) +{ +} + +#endif /*HAVE_LIBWEBP*/ diff --git a/libvips/include/vips/foreign.h b/libvips/include/vips/foreign.h index 0e60626b..a845bbd5 100644 --- a/libvips/include/vips/foreign.h +++ b/libvips/include/vips/foreign.h @@ -319,6 +319,18 @@ int vips_jpegsave_buffer( VipsImage *in, void **buf, size_t *len, ... ) int vips_jpegsave_mime( VipsImage *in, ... ) __attribute__((sentinel)); +int vips_webpload( const char *filename, VipsImage **out, ... ) + __attribute__((sentinel)); +int vips_webpload_buffer( void *buf, size_t len, VipsImage **out, ... ) + __attribute__((sentinel)); + +int vips_webpsave( VipsImage *in, const char *filename, ... ) + __attribute__((sentinel)); +int vips_webpsave_buffer( VipsImage *in, void **buf, size_t *len, ... ) + __attribute__((sentinel)); +int vips_webpsave_mime( VipsImage *in, ... ) + __attribute__((sentinel)); + /** * VipsForeignTiffCompression: * @VIPS_FOREIGN_TIFF_COMPRESSION_NONE: no compression