From 1af06d5ac936a23875340b90c4b6d1c4fd7a78a0 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Tue, 6 Aug 2013 10:01:50 +0100 Subject: [PATCH 1/8] boilerplate for webp read --- configure.ac | 15 ++ libvips/foreign/Makefile.am | 3 + libvips/foreign/foreign.c | 35 +++++ libvips/foreign/webp.h | 50 +++++++ libvips/foreign/webp2vips.c | 98 ++++++++++++ libvips/foreign/webpload.c | 262 +++++++++++++++++++++++++++++++++ libvips/include/vips/foreign.h | 3 + 7 files changed, 466 insertions(+) create mode 100644 libvips/foreign/webp.h create mode 100644 libvips/foreign/webp2vips.c create mode 100644 libvips/foreign/webpload.c diff --git a/configure.ac b/configure.ac index a672c99e..10577455 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], @@ -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..4605be97 100644 --- a/libvips/foreign/Makefile.am +++ b/libvips/foreign/Makefile.am @@ -50,6 +50,9 @@ libforeign_la_SOURCES = \ openslide2vips.h \ openslide2vips.c \ openslideload.c \ + webp.h \ + webpload.c \ + webp2vips.c \ vips2jpeg.c \ jpeg2vips.c \ jpeg.h \ diff --git a/libvips/foreign/foreign.c b/libvips/foreign/foreign.c index feafc6fb..4d0007a2 100644 --- a/libvips/foreign/foreign.c +++ b/libvips/foreign/foreign.c @@ -1609,6 +1609,8 @@ 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 ); vips_foreign_load_rad_get_type(); vips_foreign_save_rad_get_type(); @@ -1645,6 +1647,11 @@ 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(); +#endif /*HAVE_LIBWEBP*/ + #ifdef HAVE_TIFF vips_foreign_load_tiff_get_type(); vips_foreign_save_tiff_get_type(); @@ -2075,6 +2082,34 @@ vips_jpegsave( VipsImage *in, const char *filename, ... ) 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_openexrload: * @filename: file to load diff --git a/libvips/foreign/webp.h b/libvips/foreign/webp.h new file mode 100644 index 00000000..6cc623a1 --- /dev/null +++ b/libvips/foreign/webp.h @@ -0,0 +1,50 @@ +/* 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*/ + +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 ); + +#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..734a233d --- /dev/null +++ b/libvips/foreign/webp2vips.c @@ -0,0 +1,98 @@ +/* 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 "webp.h" + +int +vips__iswebp( const char *filename ) +{ + unsigned char buf[2]; + + if( vips__get_bytes( filename, buf, 2 ) ) + if( (int) buf[0] == 0xff && (int) buf[1] == 0xd8 ) + return( 1 ); + + return( 0 ); +} + +int +vips__webp_read_file_header( const char *filename, VipsImage *out ) +{ + printf( "vips__webp_read_file_header\n" ); + + return( 0 ); +} + +int +vips__webp_read_file( const char *filename, VipsImage *out ) +{ + printf( "vips__webp_read_file\n" ); + + return( 0 ); +} + +int +vips__webp_read_buffer_header( void *buf, size_t len, VipsImage *out ) +{ + printf( "vips__webp_read_buffer_header\n" ); + + return( 0 ); +} + +int +vips__webp_read_buffer( void *buf, size_t len, VipsImage *out ) +{ + printf( "vips__webp_read_buffer\n" ); + + return( 0 ); +} + +#endif /*HAVE_LIBWEBP*/ + + diff --git a/libvips/foreign/webpload.c b/libvips/foreign/webpload.c new file mode 100644 index 00000000..6c65c977 --- /dev/null +++ b/libvips/foreign/webpload.c @@ -0,0 +1,262 @@ +/* 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 ) +{ + /* The libwebp reader supports sequential read. + */ + return( VIPS_FOREIGN_SEQUENTIAL ); +} + +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 ) +{ + /* The webp reader supports sequential read. + */ + return( VIPS_FOREIGN_SEQUENTIAL ); +} + +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 ); +} + +static const char *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 = 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/include/vips/foreign.h b/libvips/include/vips/foreign.h index 0e60626b..25fb1f1a 100644 --- a/libvips/include/vips/foreign.h +++ b/libvips/include/vips/foreign.h @@ -319,6 +319,9 @@ 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)); + /** * VipsForeignTiffCompression: * @VIPS_FOREIGN_TIFF_COMPRESSION_NONE: no compression From 165e7c97176f24ee2a131d546e75aac6beea919c Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Tue, 6 Aug 2013 15:51:23 +0100 Subject: [PATCH 2/8] basic webp load done --- ChangeLog | 1 + configure.ac | 4 +- libvips/foreign/webp2vips.c | 205 ++++++++++++++++++++++++++++++++++-- libvips/iofuncs/type.c | 2 +- 4 files changed, 198 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index 70bf3200..b3e7d218 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,6 +7,7 @@ - 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 +- added vips_webpload(), vips_webpload_buffer() 3/7/13 started 7.34.2 - lower priority for Matlab load to reduce segvs from Mat_Open(), thanks diff --git a/configure.ac b/configure.ac index 10577455..ffedc5b5 100644 --- a/configure.ac +++ b/configure.ac @@ -689,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) diff --git a/libvips/foreign/webp2vips.c b/libvips/foreign/webp2vips.c index 734a233d..1b2449ce 100644 --- a/libvips/foreign/webp2vips.c +++ b/libvips/foreign/webp2vips.c @@ -32,9 +32,9 @@ */ /* + */ #define DEBUG_VERBOSE #define DEBUG - */ #ifdef HAVE_CONFIG_H #include @@ -43,20 +43,116 @@ #ifdef HAVE_LIBWEBP +#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 buf[2]; + unsigned char header[MINIMAL_HEADER]; - if( vips__get_bytes( filename, buf, 2 ) ) - if( (int) buf[0] == 0xff && (int) buf[1] == 0xd8 ) - return( 1 ); + 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 ); } @@ -64,7 +160,65 @@ vips__iswebp( const char *filename ) int vips__webp_read_file_header( const char *filename, VipsImage *out ) { - printf( "vips__webp_read_file_header\n" ); + 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_reader)( 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 ); + char *data; + unsigned int len; + webp_reader reader; + + /* libwebp makes streaming very hard. We have to read to a full memory + * buffer, then copy to out. + */ + t[0] = vips_image_new_buffer(); + if( read_header( read, t[0] ) ) + return( -1 ); + if( vips_image_write_prepare( t[0] ) ) + return( -1 ); + + if( !(data = vips__file_read_name( read->filename, NULL, &len )) ) + return( -1 ); + + if( t[0]->Bands == 3 ) + reader = WebPDecodeRGBInto; + else + reader = WebPDecodeRGBAInto; + + if( !reader( (uint8_t *) data, len, + VIPS_IMAGE_ADDR( t[0], 0, 0 ), + VIPS_IMAGE_SIZEOF_IMAGE( t[0] ), + VIPS_IMAGE_SIZEOF_LINE( t[0] ) ) ) { + g_free( data ); + vips_error( "webp2vips", "%s", _( "unable to read pixels" ) ); + return( -1 ); + } + + g_free( data ); + + if( vips_image_write( t[0], out ) ) + return( -1 ); return( 0 ); } @@ -72,7 +226,18 @@ vips__webp_read_file_header( const char *filename, VipsImage *out ) int vips__webp_read_file( const char *filename, VipsImage *out ) { - printf( "vips__webp_read_file\n" ); + 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 ); } @@ -80,7 +245,18 @@ vips__webp_read_file( const char *filename, VipsImage *out ) int vips__webp_read_buffer_header( void *buf, size_t len, VipsImage *out ) { - printf( "vips__webp_read_buffer_header\n" ); + 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 ); } @@ -88,11 +264,18 @@ vips__webp_read_buffer_header( void *buf, size_t len, VipsImage *out ) int vips__webp_read_buffer( void *buf, size_t len, VipsImage *out ) { - printf( "vips__webp_read_buffer\n" ); + 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/iofuncs/type.c b/libvips/iofuncs/type.c index c2303173..7295778f 100644 --- a/libvips/iofuncs/type.c +++ b/libvips/iofuncs/type.c @@ -37,9 +37,9 @@ */ /* - */ #define VIPS_DEBUG #define DEBUG + */ #ifdef HAVE_CONFIG_H #include From e9afbe59e29d32c215cf3744ef9c5b3b2c9563bb Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Tue, 6 Aug 2013 16:18:30 +0100 Subject: [PATCH 3/8] added webpsave boilerplate --- libvips/foreign/Makefile.am | 2 + libvips/foreign/foreign.c | 203 ++++++++++++++++++----- libvips/foreign/vips2webp.c | 70 ++++++++ libvips/foreign/webp.h | 5 + libvips/foreign/webpload.c | 12 +- libvips/foreign/webpsave.c | 288 +++++++++++++++++++++++++++++++++ libvips/include/vips/foreign.h | 9 ++ 7 files changed, 545 insertions(+), 44 deletions(-) create mode 100644 libvips/foreign/vips2webp.c create mode 100644 libvips/foreign/webpsave.c diff --git a/libvips/foreign/Makefile.am b/libvips/foreign/Makefile.am index 4605be97..a027cf84 100644 --- a/libvips/foreign/Makefile.am +++ b/libvips/foreign/Makefile.am @@ -52,7 +52,9 @@ libforeign_la_SOURCES = \ 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 4d0007a2..e0e87ac1 100644 --- a/libvips/foreign/foreign.c +++ b/libvips/foreign/foreign.c @@ -1611,6 +1611,8 @@ vips_foreign_operation_init( 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(); @@ -1650,6 +1652,8 @@ vips_foreign_operation_init( void ) #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 @@ -1953,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 ); @@ -2032,51 +2058,29 @@ vips_jpegsave_buffer( VipsImage *in, void **buf, size_t *len, ... ) } /** - * vips_jpegsave: + * vips_jpegsave_mime: * @in: image to save - * @filename: file to write to * @...: %NULL-terminated list of optional named arguments * * Optional arguments: * - * @Q: quality factor + * @Q: JPEG quality factor * @profile: attach this ICC profile * - * Write a VIPS image to a file as JPEG. + * As vips_jpegsave(), but save as a mime jpeg on stdout. * - * 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: vips_jpegsave(), vips_image_write_to_file(). * * Returns: 0 on success, -1 on error. */ int -vips_jpegsave( VipsImage *in, const char *filename, ... ) +vips_jpegsave_mime( VipsImage *in, ... ) { va_list ap; int result; - va_start( ap, filename ); - result = vips_call_split( "jpegsave", ap, in, filename ); + va_start( ap, in ); + result = vips_call_split( "jpegsave_mime", ap, in ); va_end( ap ); return( result ); @@ -2110,6 +2114,133 @@ vips_webpload( const char *filename, VipsImage **out, ... ) 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 + * + * Optional arguments: + * + * @Q: quality factor + * + * See also: + * + * Returns: 0 on success, -1 on error. + */ +int +vips_webpsave( VipsImage *in, const char *filename, ... ) +{ + va_list ap; + int result; + + va_start( ap, 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 ); +} + /** * vips_openexrload: * @filename: file to load diff --git a/libvips/foreign/vips2webp.c b/libvips/foreign/vips2webp.c new file mode 100644 index 00000000..5cfeec67 --- /dev/null +++ b/libvips/foreign/vips2webp.c @@ -0,0 +1,70 @@ +/* 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" + +int +vips__webp_write_file( VipsImage *in, const char *filename, int Q ) +{ + printf( "vips__webp_write_file:\n" ); + + return( 0 ); +} + +int +vips__webp_write_buffer( VipsImage *in, void **obuf, size_t *olen, int Q ) +{ + printf( "vips__webp_write_buffer:\n" ); + + return( 0 ); +} + +#endif /*HAVE_LIBWEBP*/ diff --git a/libvips/foreign/webp.h b/libvips/foreign/webp.h index 6cc623a1..e5b7f8a4 100644 --- a/libvips/foreign/webp.h +++ b/libvips/foreign/webp.h @@ -35,6 +35,8 @@ 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 ); @@ -43,6 +45,9 @@ 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 ); +int vips__webp_write_buffer( VipsImage *out, void **buf, size_t *len, int Q ); + #ifdef __cplusplus } #endif /*__cplusplus*/ diff --git a/libvips/foreign/webpload.c b/libvips/foreign/webpload.c index 6c65c977..d29314f2 100644 --- a/libvips/foreign/webpload.c +++ b/libvips/foreign/webpload.c @@ -60,9 +60,7 @@ G_DEFINE_ABSTRACT_TYPE( VipsForeignLoadWebp, vips_foreign_load_webp, static VipsForeignFlags vips_foreign_load_webp_get_flags( VipsForeignLoad *load ) { - /* The libwebp reader supports sequential read. - */ - return( VIPS_FOREIGN_SEQUENTIAL ); + return( 0 ); } static int @@ -117,9 +115,7 @@ G_DEFINE_TYPE( VipsForeignLoadWebpFile, vips_foreign_load_webp_file, static VipsForeignFlags vips_foreign_load_webp_file_get_flags_filename( const char *filename ) { - /* The webp reader supports sequential read. - */ - return( VIPS_FOREIGN_SEQUENTIAL ); + return( 0 ); } static gboolean @@ -152,7 +148,7 @@ vips_foreign_load_webp_file_load( VipsForeignLoad *load ) return( 0 ); } -static const char *webp_suffs[] = { ".webp", NULL }; +const char *vips__webp_suffs[] = { ".webp", NULL }; static void vips_foreign_load_webp_file_class_init( VipsForeignLoadWebpFileClass *class ) @@ -168,7 +164,7 @@ vips_foreign_load_webp_file_class_init( VipsForeignLoadWebpFileClass *class ) object_class->nickname = "webpload"; object_class->description = _( "load webp from file" ); - foreign_class->suffs = webp_suffs; + foreign_class->suffs = vips__webp_suffs; load_class->get_flags_filename = vips_foreign_load_webp_file_get_flags_filename; diff --git a/libvips/foreign/webpsave.c b/libvips/foreign/webpsave.c new file mode 100644 index 00000000..0f0c5cc8 --- /dev/null +++ b/libvips/foreign/webpsave.c @@ -0,0 +1,288 @@ +/* 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; + +} 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 ); + +} + +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 ) ) + 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 ) ) + 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 ) ) + 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 25fb1f1a..a845bbd5 100644 --- a/libvips/include/vips/foreign.h +++ b/libvips/include/vips/foreign.h @@ -321,6 +321,15 @@ int vips_jpegsave_mime( VipsImage *in, ... ) 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: From 4a65af9196363fe95f1994fd976df4ced82d0528 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Tue, 6 Aug 2013 18:15:18 +0100 Subject: [PATCH 4/8] add save functions --- ChangeLog | 3 +- TODO | 4 +-- libvips/foreign/vips2webp.c | 57 +++++++++++++++++++++++++++++++++++-- libvips/foreign/webp2vips.c | 10 +++---- 4 files changed, 63 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index b3e7d218..0dd82dbe 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,7 +7,8 @@ - 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 -- added vips_webpload(), vips_webpload_buffer() +- 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 ab2c95a9..5a017acd 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,4 @@ -- support webp, see: - - https://github.com/jcupitt/libvips/issues/68 +- use mmap to read encoded webp image - rename vips_diag() as vips_info(), make it work like a log thing ... normally no output, can be turned on with a --vips-verbose flag diff --git a/libvips/foreign/vips2webp.c b/libvips/foreign/vips2webp.c index 5cfeec67..3c60f0c6 100644 --- a/libvips/foreign/vips2webp.c +++ b/libvips/foreign/vips2webp.c @@ -51,10 +51,47 @@ #include "webp.h" +typedef size_t (*webp_encoder)( const uint8_t *rgb, + int width, int height, int stride, + float quality_factor, uint8_t **output ); + int vips__webp_write_file( VipsImage *in, const char *filename, int Q ) { - printf( "vips__webp_write_file:\n" ); + webp_encoder encoder; + size_t len; + uint8_t *buffer; + FILE *fp; + + if( vips_image_wio_input( in ) ) + return( -1 ); + + 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 ); } @@ -62,7 +99,23 @@ vips__webp_write_file( VipsImage *in, const char *filename, int Q ) int vips__webp_write_buffer( VipsImage *in, void **obuf, size_t *olen, int Q ) { - printf( "vips__webp_write_buffer:\n" ); + 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 ); } diff --git a/libvips/foreign/webp2vips.c b/libvips/foreign/webp2vips.c index 1b2449ce..838ca88a 100644 --- a/libvips/foreign/webp2vips.c +++ b/libvips/foreign/webp2vips.c @@ -176,7 +176,7 @@ vips__webp_read_file_header( const char *filename, VipsImage *out ) return( 0 ); } -typedef uint8_t *(*webp_reader)( const uint8_t* data, size_t data_size, +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 ); @@ -187,7 +187,7 @@ read_image( Read *read, VipsImage *out ) vips_object_local_array( VIPS_OBJECT( out ), 3 ); char *data; unsigned int len; - webp_reader reader; + webp_decoder decoder; /* libwebp makes streaming very hard. We have to read to a full memory * buffer, then copy to out. @@ -202,11 +202,11 @@ read_image( Read *read, VipsImage *out ) return( -1 ); if( t[0]->Bands == 3 ) - reader = WebPDecodeRGBInto; + decoder = WebPDecodeRGBInto; else - reader = WebPDecodeRGBAInto; + decoder = WebPDecodeRGBAInto; - if( !reader( (uint8_t *) data, len, + if( !decoder( (uint8_t *) data, len, VIPS_IMAGE_ADDR( t[0], 0, 0 ), VIPS_IMAGE_SIZEOF_IMAGE( t[0] ), VIPS_IMAGE_SIZEOF_LINE( t[0] ) ) ) { From ddc5f6977d35b522726485a2f671dcf8ccdf569f Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Tue, 6 Aug 2013 21:46:48 +0100 Subject: [PATCH 5/8] read webp input with mmap --- TODO | 2 - libvips/arithmetic/hist_find.c | 424 +++++++++++++++++++++++++++++++++ libvips/foreign/webp2vips.c | 26 +- 3 files changed, 444 insertions(+), 8 deletions(-) create mode 100644 libvips/arithmetic/hist_find.c diff --git a/TODO b/TODO index 5a017acd..fc79bbfd 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,3 @@ -- use mmap to read encoded webp image - - rename vips_diag() as vips_info(), make it work like a log thing ... normally no output, can be turned on with a --vips-verbose flag diff --git a/libvips/arithmetic/hist_find.c b/libvips/arithmetic/hist_find.c new file mode 100644 index 00000000..379e7a4c --- /dev/null +++ b/libvips/arithmetic/hist_find.c @@ -0,0 +1,424 @@ +/* find histograms + * + * Copyright: 1990, 1991, N. Dessipris. + * + * Author: Nicos Dessipris. + * Written on: 09/07/1990 + * Modified on : 11/03/1991 + * 19/7/93 JC + * - test for Coding type added + * 26/10/94 JC + * - rewritten for ANSI + * - now does USHORT too + * - 5 x faster! + * 2/6/95 JC + * - rewritten for partials + * 3/3/01 JC + * - tiny speed ups + * 21/1/07 + * - number bands from zero + * 24/3/10 + * - gtkdoc + * - small cleanups + * 25/1/12 + * - cast @in to u8/u16. + */ + +/* + + 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 + + */ + +#ifdef HAVE_CONFIG_H +#include +#endif /*HAVE_CONFIG_H*/ +#include + +#include + +/* Accumulate a histogram in one of these. + */ +typedef struct { + int bands; /* Number of bands in output */ + int which; /* If one band in out, which band of input */ + int size; /* Length of bins */ + int mx; /* Maximum value we have seen */ + unsigned int **bins; /* All the bins! */ +} Histogram; + +typedef struct _VipsHistFind { + VipsStatistic parent_instance; + + VipsImage *in; + int band; + + /* Main image histogram. Subhists accumulate to this. + */ + Histogram *hist; +} VipsHistFind; + +typedef VipsStatisticClass VipsHistFindClass; + +G_DEFINE_TYPE( VipsHistFind, vips_hist_find, VIPS_TYPE_STATISTIC ); + +/* Build a Histogram. + */ +static Histogram * +histogram_new( VipsImage *out, int bands, int which, int size ) +{ + Histogram *hist; + int i; + + if( !(hist = VIPS_NEW( out, Histogram )) || + !(hist->bins = VIPS_ARRAY( out, bands, unsigned int * )) ) + return( NULL ); + + for( i = 0; i < bands; i++ ) { + if( !(hist->bins[i] = VIPS_ARRAY( out, size, unsigned int )) ) + return( NULL ); + memset( hist->bins[i], 0, size * sizeof( unsigned int ) ); + } + + hist->bands = bands; + hist->which = which; + hist->size = size; + hist->mx = 0; + + return( hist ); +} + +/* Build a sub-hist, based on the main hist. + */ +static void * +vips_hist_find_start( VipsImage *out, void *a, void *b ) +{ + VipsHistFind *hist_find = (VipsHistFind *) a; + Histogram *hist = hist_find->hist; + + return( (void *) + histogram_new( out, hist->bands, hist->which, hist->size ) ); +} + +/* Join a sub-hist onto the main hist. + */ +static int +vips_hist_find_stop( void *seq, void *a, void *b ) +{ + Histogram *sub_hist = (Histogram *) seq; + VipsHistFind *hist_find = (VipsHistFind *) a; + Histogram *hist = hist_find->hist; + + int i, j; + + g_assert( sub_hist->bands == hist->bands && + sub_hist->size == hist->size ); + + /* Add on sub-data. + */ + hist->mx = VIPS_MAX( hist->mx, sub_hist->mx ); + for( i = 0; i < hist->bands; i++ ) + for( j = 0; j < hist->size; j++ ) + hist->bins[i][j] += sub_hist->bins[i][j]; + + /* Blank out sub-hist to make sure we can't add it again. + */ + sub_hist->mx = 0; + for( i = 0; i < sub_hist->bands; i++ ) + sub_hist->bins[i] = NULL; + + return( 0 ); +} + +/* Histogram of all bands of a uchar image. + */ +static int +vips_hist_find_uchar_hist( VipsRegion *reg, + void *seq, void *a, void *b, gboolean *stop ) +{ + Histogram *hist = (Histogram *) seq; + VipsRect *r = ®->valid; + VipsImage *im = reg->im; + int le = r->left; + int to = r->top; + int bo = VIPS_RECT_BOTTOM(r); + int nb = im->Bands; + + int x, y, z; + + /* Accumulate! + */ + for( y = to; y < bo; y++ ) { + VipsPel *p = VIPS_REGION_ADDR( reg, le, y ); + int i; + + for( i = 0, x = 0; x < r->width; x++ ) + for( z = 0; z < nb; z++, i++ ) + hist->bins[z][p[i]] += 1; + } + + /* Note the maximum. + */ + hist->mx = 255; + + return( 0 ); +} + +/* Histogram of a selected band of a uchar image. + */ +static int +vips_hist_find_uchar_hist_extract( VipsRegion *reg, + void *seq, void *a, void *b, gboolean *stop ) +{ + Histogram *hist = (Histogram *) seq; + VipsRect *r = ®->valid; + VipsImage *im = reg->im; + int le = r->left; + int to = r->top; + int bo = VIPS_RECT_BOTTOM(r); + unsigned int *bins = hist->bins[0]; + int nb = im->Bands; + int max = r->width * nb; + + int x, y; + + /* Accumulate! + */ + for( y = to; y < bo; y++ ) { + VipsPel *p = VIPS_REGION_ADDR( reg, le, y ); + + for( x = hist->which; x < max; x += nb ) + bins[p[x]] += 1; + } + + /* Note the maximum. + */ + hist->mx = 255; + + return( 0 ); +} + +/* Histogram of all bands of a ushort image. + */ +static int +vips_hist_find_ushort_hist( VipsRegion *reg, + void *seq, void *a, void *b, gboolean *stop ) +{ + Histogram *hist = (Histogram *) seq; + VipsRect *r = ®->valid; + VipsImage *im = reg->im; + int le = r->left; + int to = r->top; + int bo = VIPS_RECT_BOTTOM(r); + int mx = hist->mx; + int nb = im->Bands; + + int x, y, z; + + /* Accumulate! + */ + for( y = to; y < bo; y++ ) { + unsigned short *p = (unsigned short *) + VIPS_REGION_ADDR( reg, le, y ); + int i; + + for( i = 0, x = 0; x < r->width; x++ ) + for( z = 0; z < nb; z++, i++ ) { + int v = p[i]; + + /* Adjust maximum. + */ + if( v > mx ) + mx = v; + + hist->bins[z][v] += 1; + } + } + + /* Note the maximum. + */ + hist->mx = mx; + + return( 0 ); +} + +/* Histogram of one band of a ushort image. + */ +static int +vips_hist_find_ushort_hist_extract( VipsRegion *reg, + void *seq, void *a, void *b, gboolean *stop ) +{ + Histogram *hist = (Histogram *) seq; + VipsRect *r = ®->valid; + VipsImage *im = reg->im; + int le = r->left; + int to = r->top; + int bo = VIPS_RECT_BOTTOM(r); + int mx = hist->mx; + unsigned int *bins = hist->bins[0]; + int nb = im->Bands; + int max = nb * r->width; + + int x, y; + + /* Accumulate! + */ + for( y = to; y < bo; y++ ) { + unsigned short *p = (unsigned short *) + VIPS_REGION_ADDR( reg, le, y ) + hist->which; + + for( x = hist->which; x < max; x += nb ) { + int v = p[x]; + + /* Adjust maximum. + */ + if( v > mx ) + mx = v; + + bins[v] += 1; + } + } + + /* Note the maximum. + */ + hist->mx = mx; + + return( 0 ); +} + +/* Save a bit of typing. + */ +#define UC VIPS_FORMAT_UCHAR +#define US VIPS_FORMAT_USHORT +#define UI VIPS_FORMAT_UINT + +/* Type mapping: go to uchar or ushort. + */ +static int bandfmt_histgr[10] = { +/* UC C US S UI I F X D DX */ + UC, UC, US, US, US, US, US, US, US, US +}; + +/** + * im_histgr: + * @in: input image + * @out: output image + * @bandno: band to equalise + * + * Find the histogram of @in. Find the histogram for band @bandno (producing a + * one-band histogram), or for all bands (producing an n-band histogram) if + * @bandno is -1. + * + * @in is cast to u8 or u16. @out is always u32. + * + * See also: im_hist_indexed(), im_histeq(). + * + * Returns: 0 on success, -1 on error + */ +int +im_histgr( VipsImage *in, VipsImage *out, int bandno ) +{ + VipsImage *t; + int size; /* Length of hist */ + int bands; /* Number of bands in output */ + Histogram *mhist; + VipsGenerateFn scanfn; + int i, j; + unsigned int *obuffer, *q; + + /* Check images. PIO from in, WIO to out. + */ + if( im_check_uncoded( "im_histgr", in ) || + im_check_bandno( "im_histgr", in, bandno ) || + im_pincheck( in ) || + im_outcheck( out ) ) + return( -1 ); + + /* Cast in to u8/u16. + */ + if( !(t = im_open_local( out, "im_histgr", "p" )) || + im_clip2fmt( in, t, bandfmt_histgr[in->BandFmt] ) ) + return( -1 ); + in = t; + + /* Find the range of pixel values we must handle. + */ + size = in->BandFmt == VIPS_FORMAT_UCHAR ? 256 : 65536; + + /* How many output bands? + */ + if( bandno == -1 ) + bands = in->Bands; + else + bands = 1; + + /* Build main hist we accumulate data in. + */ + if( !(mhist = build_hist( out, bands, bandno, size )) ) + return( -1 ); + + /* Select scan function. + */ + if( in->BandFmt == VIPS_FORMAT_UCHAR && bandno == -1 ) + scanfn = vips_hist_find_uchar_hist; + else if( in->BandFmt == VIPS_FORMAT_UCHAR ) + scanfn = vips_hist_find_uchar_hist_extract; + else if( in->BandFmt == VIPS_FORMAT_USHORT && bandno == -1 ) + scanfn = vips_hist_find_ushort_hist; + else + scanfn = vips_hist_find_ushort_hist_extract; + + /* Accumulate data. + */ + if( vips_sink( in, + vips_hist_find_start, scanfn, vips_hist_find_stop, + mhist, NULL ) ) + return( -1 ); + + /* Make the output image. + */ + if( im_cp_desc( out, in ) ) + return( -1 ); + im_initdesc( out, + mhist->mx + 1, 1, bands, VIPS_FORMAT_UINT, + VIPS_CODING_NONE, VIPS_TYPE_HISTOGRAM, 1.0, 1.0, 0, 0 ); + if( im_setupout( out ) ) + return( -1 ); + + /* Interleave for output. + */ + if( !(obuffer = VIPS_ARRAY( out, + VIPS_IMAGE_N_ELEMENTS( out ), unsigned int )) ) + return( -1 ); + for( q = obuffer, j = 0; j < out->Xsize; j++ ) + for( i = 0; i < out->Bands; i++ ) + *q++ = mhist->bins[i][j]; + + /* Write interleaved buffer into hist. + */ + if( im_writeline( 0, out, (VipsPel *) obuffer ) ) + return( -1 ); + + return( 0 ); +} diff --git a/libvips/foreign/webp2vips.c b/libvips/foreign/webp2vips.c index 838ca88a..4996e5c2 100644 --- a/libvips/foreign/webp2vips.c +++ b/libvips/foreign/webp2vips.c @@ -48,6 +48,7 @@ #include #include +#include #include "webp.h" @@ -185,12 +186,15 @@ read_image( Read *read, VipsImage *out ) { VipsImage **t = (VipsImage **) vips_object_local_array( VIPS_OBJECT( out ), 3 ); - char *data; - unsigned int len; + 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] ) ) @@ -198,24 +202,34 @@ read_image( Read *read, VipsImage *out ) if( vips_image_write_prepare( t[0] ) ) return( -1 ); - if( !(data = vips__file_read_name( read->filename, NULL, &len )) ) + 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, len, + 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] ) ) ) { - g_free( data ); + vips__munmap( data, length ); + vips_tracked_close( fd ); vips_error( "webp2vips", "%s", _( "unable to read pixels" ) ); return( -1 ); } - g_free( data ); + vips__munmap( data, length ); + vips_tracked_close( fd ); if( vips_image_write( t[0], out ) ) return( -1 ); From ea3b2e3ef1ded5ca6a95098f0c10bad4ffabce69 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Tue, 6 Aug 2013 21:47:45 +0100 Subject: [PATCH 6/8] oops --- libvips/arithmetic/hist_find.c | 424 --------------------------------- 1 file changed, 424 deletions(-) delete mode 100644 libvips/arithmetic/hist_find.c diff --git a/libvips/arithmetic/hist_find.c b/libvips/arithmetic/hist_find.c deleted file mode 100644 index 379e7a4c..00000000 --- a/libvips/arithmetic/hist_find.c +++ /dev/null @@ -1,424 +0,0 @@ -/* find histograms - * - * Copyright: 1990, 1991, N. Dessipris. - * - * Author: Nicos Dessipris. - * Written on: 09/07/1990 - * Modified on : 11/03/1991 - * 19/7/93 JC - * - test for Coding type added - * 26/10/94 JC - * - rewritten for ANSI - * - now does USHORT too - * - 5 x faster! - * 2/6/95 JC - * - rewritten for partials - * 3/3/01 JC - * - tiny speed ups - * 21/1/07 - * - number bands from zero - * 24/3/10 - * - gtkdoc - * - small cleanups - * 25/1/12 - * - cast @in to u8/u16. - */ - -/* - - 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 - - */ - -#ifdef HAVE_CONFIG_H -#include -#endif /*HAVE_CONFIG_H*/ -#include - -#include - -/* Accumulate a histogram in one of these. - */ -typedef struct { - int bands; /* Number of bands in output */ - int which; /* If one band in out, which band of input */ - int size; /* Length of bins */ - int mx; /* Maximum value we have seen */ - unsigned int **bins; /* All the bins! */ -} Histogram; - -typedef struct _VipsHistFind { - VipsStatistic parent_instance; - - VipsImage *in; - int band; - - /* Main image histogram. Subhists accumulate to this. - */ - Histogram *hist; -} VipsHistFind; - -typedef VipsStatisticClass VipsHistFindClass; - -G_DEFINE_TYPE( VipsHistFind, vips_hist_find, VIPS_TYPE_STATISTIC ); - -/* Build a Histogram. - */ -static Histogram * -histogram_new( VipsImage *out, int bands, int which, int size ) -{ - Histogram *hist; - int i; - - if( !(hist = VIPS_NEW( out, Histogram )) || - !(hist->bins = VIPS_ARRAY( out, bands, unsigned int * )) ) - return( NULL ); - - for( i = 0; i < bands; i++ ) { - if( !(hist->bins[i] = VIPS_ARRAY( out, size, unsigned int )) ) - return( NULL ); - memset( hist->bins[i], 0, size * sizeof( unsigned int ) ); - } - - hist->bands = bands; - hist->which = which; - hist->size = size; - hist->mx = 0; - - return( hist ); -} - -/* Build a sub-hist, based on the main hist. - */ -static void * -vips_hist_find_start( VipsImage *out, void *a, void *b ) -{ - VipsHistFind *hist_find = (VipsHistFind *) a; - Histogram *hist = hist_find->hist; - - return( (void *) - histogram_new( out, hist->bands, hist->which, hist->size ) ); -} - -/* Join a sub-hist onto the main hist. - */ -static int -vips_hist_find_stop( void *seq, void *a, void *b ) -{ - Histogram *sub_hist = (Histogram *) seq; - VipsHistFind *hist_find = (VipsHistFind *) a; - Histogram *hist = hist_find->hist; - - int i, j; - - g_assert( sub_hist->bands == hist->bands && - sub_hist->size == hist->size ); - - /* Add on sub-data. - */ - hist->mx = VIPS_MAX( hist->mx, sub_hist->mx ); - for( i = 0; i < hist->bands; i++ ) - for( j = 0; j < hist->size; j++ ) - hist->bins[i][j] += sub_hist->bins[i][j]; - - /* Blank out sub-hist to make sure we can't add it again. - */ - sub_hist->mx = 0; - for( i = 0; i < sub_hist->bands; i++ ) - sub_hist->bins[i] = NULL; - - return( 0 ); -} - -/* Histogram of all bands of a uchar image. - */ -static int -vips_hist_find_uchar_hist( VipsRegion *reg, - void *seq, void *a, void *b, gboolean *stop ) -{ - Histogram *hist = (Histogram *) seq; - VipsRect *r = ®->valid; - VipsImage *im = reg->im; - int le = r->left; - int to = r->top; - int bo = VIPS_RECT_BOTTOM(r); - int nb = im->Bands; - - int x, y, z; - - /* Accumulate! - */ - for( y = to; y < bo; y++ ) { - VipsPel *p = VIPS_REGION_ADDR( reg, le, y ); - int i; - - for( i = 0, x = 0; x < r->width; x++ ) - for( z = 0; z < nb; z++, i++ ) - hist->bins[z][p[i]] += 1; - } - - /* Note the maximum. - */ - hist->mx = 255; - - return( 0 ); -} - -/* Histogram of a selected band of a uchar image. - */ -static int -vips_hist_find_uchar_hist_extract( VipsRegion *reg, - void *seq, void *a, void *b, gboolean *stop ) -{ - Histogram *hist = (Histogram *) seq; - VipsRect *r = ®->valid; - VipsImage *im = reg->im; - int le = r->left; - int to = r->top; - int bo = VIPS_RECT_BOTTOM(r); - unsigned int *bins = hist->bins[0]; - int nb = im->Bands; - int max = r->width * nb; - - int x, y; - - /* Accumulate! - */ - for( y = to; y < bo; y++ ) { - VipsPel *p = VIPS_REGION_ADDR( reg, le, y ); - - for( x = hist->which; x < max; x += nb ) - bins[p[x]] += 1; - } - - /* Note the maximum. - */ - hist->mx = 255; - - return( 0 ); -} - -/* Histogram of all bands of a ushort image. - */ -static int -vips_hist_find_ushort_hist( VipsRegion *reg, - void *seq, void *a, void *b, gboolean *stop ) -{ - Histogram *hist = (Histogram *) seq; - VipsRect *r = ®->valid; - VipsImage *im = reg->im; - int le = r->left; - int to = r->top; - int bo = VIPS_RECT_BOTTOM(r); - int mx = hist->mx; - int nb = im->Bands; - - int x, y, z; - - /* Accumulate! - */ - for( y = to; y < bo; y++ ) { - unsigned short *p = (unsigned short *) - VIPS_REGION_ADDR( reg, le, y ); - int i; - - for( i = 0, x = 0; x < r->width; x++ ) - for( z = 0; z < nb; z++, i++ ) { - int v = p[i]; - - /* Adjust maximum. - */ - if( v > mx ) - mx = v; - - hist->bins[z][v] += 1; - } - } - - /* Note the maximum. - */ - hist->mx = mx; - - return( 0 ); -} - -/* Histogram of one band of a ushort image. - */ -static int -vips_hist_find_ushort_hist_extract( VipsRegion *reg, - void *seq, void *a, void *b, gboolean *stop ) -{ - Histogram *hist = (Histogram *) seq; - VipsRect *r = ®->valid; - VipsImage *im = reg->im; - int le = r->left; - int to = r->top; - int bo = VIPS_RECT_BOTTOM(r); - int mx = hist->mx; - unsigned int *bins = hist->bins[0]; - int nb = im->Bands; - int max = nb * r->width; - - int x, y; - - /* Accumulate! - */ - for( y = to; y < bo; y++ ) { - unsigned short *p = (unsigned short *) - VIPS_REGION_ADDR( reg, le, y ) + hist->which; - - for( x = hist->which; x < max; x += nb ) { - int v = p[x]; - - /* Adjust maximum. - */ - if( v > mx ) - mx = v; - - bins[v] += 1; - } - } - - /* Note the maximum. - */ - hist->mx = mx; - - return( 0 ); -} - -/* Save a bit of typing. - */ -#define UC VIPS_FORMAT_UCHAR -#define US VIPS_FORMAT_USHORT -#define UI VIPS_FORMAT_UINT - -/* Type mapping: go to uchar or ushort. - */ -static int bandfmt_histgr[10] = { -/* UC C US S UI I F X D DX */ - UC, UC, US, US, US, US, US, US, US, US -}; - -/** - * im_histgr: - * @in: input image - * @out: output image - * @bandno: band to equalise - * - * Find the histogram of @in. Find the histogram for band @bandno (producing a - * one-band histogram), or for all bands (producing an n-band histogram) if - * @bandno is -1. - * - * @in is cast to u8 or u16. @out is always u32. - * - * See also: im_hist_indexed(), im_histeq(). - * - * Returns: 0 on success, -1 on error - */ -int -im_histgr( VipsImage *in, VipsImage *out, int bandno ) -{ - VipsImage *t; - int size; /* Length of hist */ - int bands; /* Number of bands in output */ - Histogram *mhist; - VipsGenerateFn scanfn; - int i, j; - unsigned int *obuffer, *q; - - /* Check images. PIO from in, WIO to out. - */ - if( im_check_uncoded( "im_histgr", in ) || - im_check_bandno( "im_histgr", in, bandno ) || - im_pincheck( in ) || - im_outcheck( out ) ) - return( -1 ); - - /* Cast in to u8/u16. - */ - if( !(t = im_open_local( out, "im_histgr", "p" )) || - im_clip2fmt( in, t, bandfmt_histgr[in->BandFmt] ) ) - return( -1 ); - in = t; - - /* Find the range of pixel values we must handle. - */ - size = in->BandFmt == VIPS_FORMAT_UCHAR ? 256 : 65536; - - /* How many output bands? - */ - if( bandno == -1 ) - bands = in->Bands; - else - bands = 1; - - /* Build main hist we accumulate data in. - */ - if( !(mhist = build_hist( out, bands, bandno, size )) ) - return( -1 ); - - /* Select scan function. - */ - if( in->BandFmt == VIPS_FORMAT_UCHAR && bandno == -1 ) - scanfn = vips_hist_find_uchar_hist; - else if( in->BandFmt == VIPS_FORMAT_UCHAR ) - scanfn = vips_hist_find_uchar_hist_extract; - else if( in->BandFmt == VIPS_FORMAT_USHORT && bandno == -1 ) - scanfn = vips_hist_find_ushort_hist; - else - scanfn = vips_hist_find_ushort_hist_extract; - - /* Accumulate data. - */ - if( vips_sink( in, - vips_hist_find_start, scanfn, vips_hist_find_stop, - mhist, NULL ) ) - return( -1 ); - - /* Make the output image. - */ - if( im_cp_desc( out, in ) ) - return( -1 ); - im_initdesc( out, - mhist->mx + 1, 1, bands, VIPS_FORMAT_UINT, - VIPS_CODING_NONE, VIPS_TYPE_HISTOGRAM, 1.0, 1.0, 0, 0 ); - if( im_setupout( out ) ) - return( -1 ); - - /* Interleave for output. - */ - if( !(obuffer = VIPS_ARRAY( out, - VIPS_IMAGE_N_ELEMENTS( out ), unsigned int )) ) - return( -1 ); - for( q = obuffer, j = 0; j < out->Xsize; j++ ) - for( i = 0; i < out->Bands; i++ ) - *q++ = mhist->bins[i][j]; - - /* Write interleaved buffer into hist. - */ - if( im_writeline( 0, out, (VipsPel *) obuffer ) ) - return( -1 ); - - return( 0 ); -} From 3d14709bdbd4b20667ffdeb78cd29ea38768c950 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Tue, 6 Aug 2013 21:56:31 +0100 Subject: [PATCH 7/8] support webp lossless encoding --- libvips/foreign/vips2webp.c | 52 +++++++++++++++++++++++++++---------- libvips/foreign/webp.h | 6 +++-- libvips/foreign/webpsave.c | 20 +++++++++++--- 3 files changed, 60 insertions(+), 18 deletions(-) diff --git a/libvips/foreign/vips2webp.c b/libvips/foreign/vips2webp.c index 3c60f0c6..e7186b68 100644 --- a/libvips/foreign/vips2webp.c +++ b/libvips/foreign/vips2webp.c @@ -55,10 +55,13 @@ 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 ) +vips__webp_write_file( VipsImage *in, const char *filename, + int Q, gboolean lossless ) { - webp_encoder encoder; size_t len; uint8_t *buffer; FILE *fp; @@ -66,17 +69,39 @@ vips__webp_write_file( VipsImage *in, const char *filename, int Q ) if( vips_image_wio_input( in ) ) return( -1 ); - if( in->Bands == 4 ) - encoder = WebPEncodeRGBA; - else - encoder = WebPEncodeRGB; + if( lossless ) { + webp_encoder_lossless encoder; - 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( 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 )) ) { @@ -97,7 +122,8 @@ vips__webp_write_file( VipsImage *in, const char *filename, int Q ) } int -vips__webp_write_buffer( VipsImage *in, void **obuf, size_t *olen, int Q ) +vips__webp_write_buffer( VipsImage *in, void **obuf, size_t *olen, + int Q, gboolean lossless ) { webp_encoder encoder; diff --git a/libvips/foreign/webp.h b/libvips/foreign/webp.h index e5b7f8a4..7441b893 100644 --- a/libvips/foreign/webp.h +++ b/libvips/foreign/webp.h @@ -45,8 +45,10 @@ 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 ); -int vips__webp_write_buffer( VipsImage *out, void **buf, size_t *len, int Q ); +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 } diff --git a/libvips/foreign/webpsave.c b/libvips/foreign/webpsave.c index 0f0c5cc8..432e6e96 100644 --- a/libvips/foreign/webpsave.c +++ b/libvips/foreign/webpsave.c @@ -56,6 +56,10 @@ typedef struct _VipsForeignSaveWebp { */ int Q; + /* Turn on lossless encode. + */ + gboolean lossless; + } VipsForeignSaveWebp; typedef VipsForeignSaveClass VipsForeignSaveWebpClass; @@ -95,6 +99,13 @@ vips_foreign_save_webp_class_init( VipsForeignSaveWebpClass *class ) 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 @@ -128,7 +139,8 @@ vips_foreign_save_webp_file_build( VipsObject *object ) build( object ) ) return( -1 ); - if( vips__webp_write_file( save->ready, file->filename, webp->Q ) ) + if( vips__webp_write_file( save->ready, file->filename, + webp->Q, webp->lossless ) ) return( -1 ); return( 0 ); @@ -192,7 +204,8 @@ vips_foreign_save_webp_buffer_build( VipsObject *object ) build( object ) ) return( -1 ); - if( vips__webp_write_buffer( save->ready, &obuf, &olen, webp->Q ) ) + 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 ); @@ -252,7 +265,8 @@ vips_foreign_save_webp_mime_build( VipsObject *object ) build( object ) ) return( -1 ); - if( vips__webp_write_buffer( save->ready, &obuf, &olen, webp->Q ) ) + if( vips__webp_write_buffer( save->ready, &obuf, &olen, + webp->Q, webp->lossless ) ) return( -1 ); printf( "Content-length: %zd\r\n", olen ); From 44a0a876ba3d1599e8fc89e237e10c8f5575fa0a Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Tue, 6 Aug 2013 21:59:35 +0100 Subject: [PATCH 8/8] sync --- TODO | 3 +++ 1 file changed, 3 insertions(+) diff --git a/TODO b/TODO index fc79bbfd..c9fc30f3 100644 --- a/TODO +++ b/TODO @@ -1,3 +1,6 @@ +- use the webp advanced encoding api to set a write function for webp save to + file + - rename vips_diag() as vips_info(), make it work like a log thing ... normally no output, can be turned on with a --vips-verbose flag