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: