From c90b8be0b8f5a428f7a8085635822ce621f2e3cd Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Sat, 15 Oct 2016 12:29:14 +0100 Subject: [PATCH] add tiffsave_buffer add tests also some cleanups to foreign include files see https://github.com/jcupitt/libvips/issues/417 --- ChangeLog | 1 + libvips/deprecated/im_csv2vips.c | 2 +- libvips/deprecated/im_exr2vips.c | 2 +- libvips/deprecated/im_jpeg2vips.c | 4 +- libvips/deprecated/im_magick2vips.c | 2 +- libvips/deprecated/im_png2vips.c | 2 +- libvips/deprecated/im_tiff2vips.c | 2 +- libvips/deprecated/im_webp2vips.c | 2 +- libvips/foreign/Makefile.am | 14 +- libvips/foreign/analyze2vips.c | 2 +- libvips/foreign/analyze2vips.h | 46 --- libvips/foreign/analyzeload.c | 2 +- libvips/foreign/csv.c | 2 +- libvips/foreign/csv.h | 62 ---- libvips/foreign/csvload.c | 2 +- libvips/foreign/csvsave.c | 2 +- libvips/foreign/dzsave.h | 3 - libvips/foreign/fits.c | 2 +- libvips/foreign/fits.h | 50 --- libvips/foreign/fitsload.c | 2 +- libvips/foreign/fitssave.c | 2 +- libvips/foreign/foreign.c | 8 +- libvips/foreign/jpeg2vips.c | 3 +- libvips/foreign/jpegload.c | 4 +- libvips/foreign/jpegsave.c | 4 +- libvips/foreign/magick.h | 52 ---- libvips/foreign/magick2vips.c | 2 +- libvips/foreign/magickload.c | 2 +- libvips/foreign/matlab.c | 2 +- libvips/foreign/matlab.h | 48 --- libvips/foreign/matload.c | 2 +- libvips/foreign/matrixload.c | 2 +- libvips/foreign/matrixsave.c | 2 +- libvips/foreign/openexr2vips.c | 2 +- libvips/foreign/openexr2vips.h | 47 --- libvips/foreign/openexrload.c | 2 +- libvips/foreign/openslide2vips.c | 4 +- libvips/foreign/openslide2vips.h | 50 --- libvips/foreign/openslideload.c | 4 +- libvips/foreign/pforeign.h | 230 ++++++++++++++ libvips/foreign/pngload.c | 4 +- libvips/foreign/pngsave.c | 4 +- libvips/foreign/ppm.c | 2 +- libvips/foreign/ppm.h | 51 ---- libvips/foreign/ppmload.c | 4 +- libvips/foreign/ppmsave.c | 4 +- libvips/foreign/radiance.c | 2 +- libvips/foreign/radiance.h | 51 ---- libvips/foreign/radload.c | 4 +- libvips/foreign/radsave.c | 4 +- libvips/foreign/tiff.c | 459 ++++++++++++++++++++++++++++ libvips/foreign/tiff.h | 37 +-- libvips/foreign/tiff2vips.c | 131 +------- libvips/foreign/tiffload.c | 4 +- libvips/foreign/tiffsave.c | 235 ++++++++++++-- libvips/foreign/vips2jpeg.c | 3 +- libvips/foreign/vips2tiff.c | 233 +++++++------- libvips/foreign/vips2webp.c | 4 +- libvips/foreign/vipsjpeg.h | 64 ---- libvips/foreign/vipspng.c | 6 +- libvips/foreign/vipspng.h | 60 ---- libvips/foreign/webp.h | 64 ---- libvips/foreign/webp2vips.c | 2 +- libvips/foreign/webpload.c | 4 +- libvips/foreign/webpsave.c | 4 +- test/test_foreign.py | 1 + 66 files changed, 1104 insertions(+), 1015 deletions(-) delete mode 100644 libvips/foreign/analyze2vips.h delete mode 100644 libvips/foreign/csv.h delete mode 100644 libvips/foreign/dzsave.h delete mode 100644 libvips/foreign/fits.h delete mode 100644 libvips/foreign/magick.h delete mode 100644 libvips/foreign/matlab.h delete mode 100644 libvips/foreign/openexr2vips.h delete mode 100644 libvips/foreign/openslide2vips.h create mode 100644 libvips/foreign/pforeign.h delete mode 100644 libvips/foreign/ppm.h delete mode 100644 libvips/foreign/radiance.h create mode 100644 libvips/foreign/tiff.c delete mode 100644 libvips/foreign/vipsjpeg.h delete mode 100644 libvips/foreign/vipspng.h delete mode 100644 libvips/foreign/webp.h diff --git a/ChangeLog b/ChangeLog index 72ed989c..b0aac74c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,6 @@ 13/10/16 started 8.5.0 - rewritten buffer system is safer and frees memory earlier +- added tiff save to buffer 27/9/16 started 8.4.2 - small doc improvements diff --git a/libvips/deprecated/im_csv2vips.c b/libvips/deprecated/im_csv2vips.c index 4c41dafb..55900b93 100644 --- a/libvips/deprecated/im_csv2vips.c +++ b/libvips/deprecated/im_csv2vips.c @@ -44,7 +44,7 @@ #include -#include "../foreign/csv.h" +#include "../foreign/pforeign.h" int im_csv2vips( const char *filename, IMAGE *out ) diff --git a/libvips/deprecated/im_exr2vips.c b/libvips/deprecated/im_exr2vips.c index 7e478f16..3996bf52 100644 --- a/libvips/deprecated/im_exr2vips.c +++ b/libvips/deprecated/im_exr2vips.c @@ -49,7 +49,7 @@ #include #include -#include "../foreign/openexr2vips.h" +#include "../foreign/pforeign.h" int im_exr2vips( const char *filename, IMAGE *out ) diff --git a/libvips/deprecated/im_jpeg2vips.c b/libvips/deprecated/im_jpeg2vips.c index 5b4c2c06..2e3e5d09 100644 --- a/libvips/deprecated/im_jpeg2vips.c +++ b/libvips/deprecated/im_jpeg2vips.c @@ -48,9 +48,7 @@ #include -#ifdef HAVE_JPEG -#include "../foreign/vipsjpeg.h" -#endif /*HAVE_JPEG*/ +#include "../foreign/pforeign.h" static int jpeg2vips( const char *name, IMAGE *out, gboolean header_only ) diff --git a/libvips/deprecated/im_magick2vips.c b/libvips/deprecated/im_magick2vips.c index 4113dfb1..06b6d56f 100644 --- a/libvips/deprecated/im_magick2vips.c +++ b/libvips/deprecated/im_magick2vips.c @@ -42,7 +42,7 @@ #include -#include "../foreign/magick.h" +#include "../foreign/pforeign.h" int im_magick2vips( const char *filename, IMAGE *out ) diff --git a/libvips/deprecated/im_png2vips.c b/libvips/deprecated/im_png2vips.c index fb84cdbb..a1b24579 100644 --- a/libvips/deprecated/im_png2vips.c +++ b/libvips/deprecated/im_png2vips.c @@ -45,7 +45,7 @@ #include #include -#include "../foreign/vipspng.h" +#include "../foreign/pforeign.h" static int png2vips( const char *name, IMAGE *out, gboolean header_only ) diff --git a/libvips/deprecated/im_tiff2vips.c b/libvips/deprecated/im_tiff2vips.c index 62edab6e..6fa25122 100644 --- a/libvips/deprecated/im_tiff2vips.c +++ b/libvips/deprecated/im_tiff2vips.c @@ -49,7 +49,7 @@ #include #include -#include "../foreign/tiff.h" +#include "../foreign/pforeign.h" static int tiff2vips( const char *name, IMAGE *out, gboolean header_only ) diff --git a/libvips/deprecated/im_webp2vips.c b/libvips/deprecated/im_webp2vips.c index 44ae9053..e478f862 100644 --- a/libvips/deprecated/im_webp2vips.c +++ b/libvips/deprecated/im_webp2vips.c @@ -39,7 +39,7 @@ #include #include -#include "../foreign/webp.h" +#include "../foreign/pforeign.h" static int webp2vips( const char *name, IMAGE *out, gboolean header_only ) diff --git a/libvips/foreign/Makefile.am b/libvips/foreign/Makefile.am index 7923a513..9db9e35f 100644 --- a/libvips/foreign/Makefile.am +++ b/libvips/foreign/Makefile.am @@ -1,19 +1,17 @@ noinst_LTLIBRARIES = libforeign.la libforeign_la_SOURCES = \ + pforeign.h \ gifload.c \ cairo.c \ pdfload.c \ svgload.c \ - radiance.h \ radiance.c \ radload.c \ radsave.c \ - ppm.h \ ppm.c \ ppmload.c \ ppmsave.c \ - csv.h \ csv.c \ csvload.c \ csvsave.c \ @@ -27,35 +25,28 @@ libforeign_la_SOURCES = \ dbh.h \ analyzeload.c \ analyze2vips.c \ - analyze2vips.h \ foreign.c \ - matlab.h \ matlab.c \ matload.c \ - magick.h \ magick2vips.c \ magickload.c \ magick7load.c \ pngload.c \ pngsave.c \ - vipspng.h \ vipspng.c \ - openexr2vips.h \ openexr2vips.c \ openexrload.c \ - fits.h \ fits.c \ fitsload.c \ fitssave.c \ tiff.h \ + tiff.c \ vips2tiff.c \ tiff2vips.c \ tiffload.c \ tiffsave.c \ - openslide2vips.h \ openslide2vips.c \ openslideload.c \ - webp.h \ webpload.c \ webpsave.c \ webp2vips.c \ @@ -63,7 +54,6 @@ libforeign_la_SOURCES = \ vips2jpeg.c \ jpeg2vips.c \ jpeg.h \ - vipsjpeg.h \ jpegload.c \ jpegsave.c diff --git a/libvips/foreign/analyze2vips.c b/libvips/foreign/analyze2vips.c index 77771753..489d036f 100644 --- a/libvips/foreign/analyze2vips.c +++ b/libvips/foreign/analyze2vips.c @@ -61,7 +61,7 @@ #include #include "dbh.h" -#include "analyze2vips.h" +#include "pforeign.h" /* The things we can have in header fields. Can't use GType, since we want a * static value we can use in a declaration. diff --git a/libvips/foreign/analyze2vips.h b/libvips/foreign/analyze2vips.h deleted file mode 100644 index e407d86e..00000000 --- a/libvips/foreign/analyze2vips.h +++ /dev/null @@ -1,46 +0,0 @@ -/* common defs for tiff 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_ANALYZE_H -#define VIPS_ANALYZE_H - -#ifdef __cplusplus -extern "C" { -#endif /*__cplusplus*/ - -int vips__isanalyze( const char *filename ); -int vips__analyze_read_header( const char *filename, VipsImage *out ); -int vips__analyze_read( const char *filename, VipsImage *out ); - -#ifdef __cplusplus -} -#endif /*__cplusplus*/ - -#endif /*VIPS_ANALYZE_H*/ diff --git a/libvips/foreign/analyzeload.c b/libvips/foreign/analyzeload.c index 691d1edc..eeb2fbe7 100644 --- a/libvips/foreign/analyzeload.c +++ b/libvips/foreign/analyzeload.c @@ -50,7 +50,7 @@ #ifdef HAVE_ANALYZE -#include "analyze2vips.h" +#include "pforeign.h" typedef struct _VipsForeignLoadAnalyze { VipsForeignLoad parent_object; diff --git a/libvips/foreign/csv.c b/libvips/foreign/csv.c index 4c45e8ba..5ea4cbcf 100644 --- a/libvips/foreign/csv.c +++ b/libvips/foreign/csv.c @@ -75,7 +75,7 @@ #include -#include "csv.h" +#include "pforeign.h" /* Skip to the start of the next line (ie. read until we see a '\n'), return * zero if we are at EOF. diff --git a/libvips/foreign/csv.h b/libvips/foreign/csv.h deleted file mode 100644 index 15d1e090..00000000 --- a/libvips/foreign/csv.h +++ /dev/null @@ -1,62 +0,0 @@ -/* common defs for csv 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_CSV_H -#define VIPS_CSV_H - -#ifdef __cplusplus -extern "C" { -#endif /*__cplusplus*/ - -extern const char *vips__foreign_csv_suffs[]; - -int vips__csv_read( const char *filename, VipsImage *out, - int skip, int lines, const char *whitespace, const char *separator ); -int vips__csv_read_header( const char *filename, VipsImage *out, - int skip, int lines, const char *whitespace, const char *separator ); - -int vips__csv_write( VipsImage *in, const char *filename, - const char *separator ); - -int vips__matrix_read_header( const char *filename, - int *width, int *height, double *scale, double *offset ); -int vips__matrix_ismatrix( const char *filename ); -VipsImage *vips__matrix_read_file( FILE *fp ); -VipsImage *vips__matrix_read( const char *filename ); -int vips__matrix_write( VipsImage *in, const char *filename ); -int vips__matrix_write_file( VipsImage *in, FILE *fp ); - -extern const char *vips__foreign_matrix_suffs[]; - -#ifdef __cplusplus -} -#endif /*__cplusplus*/ - -#endif /*VIPS_CSV_H*/ diff --git a/libvips/foreign/csvload.c b/libvips/foreign/csvload.c index d4deed4b..9ee62135 100644 --- a/libvips/foreign/csvload.c +++ b/libvips/foreign/csvload.c @@ -48,7 +48,7 @@ #include #include -#include "csv.h" +#include "pforeign.h" typedef struct _VipsForeignLoadCsv { VipsForeignLoad parent_object; diff --git a/libvips/foreign/csvsave.c b/libvips/foreign/csvsave.c index 483d2fb3..5dfd21dd 100644 --- a/libvips/foreign/csvsave.c +++ b/libvips/foreign/csvsave.c @@ -47,7 +47,7 @@ #include -#include "csv.h" +#include "pforeign.h" typedef struct _VipsForeignSaveCsv { VipsForeignSave parent_object; diff --git a/libvips/foreign/dzsave.h b/libvips/foreign/dzsave.h deleted file mode 100644 index d377c7b9..00000000 --- a/libvips/foreign/dzsave.h +++ /dev/null @@ -1,3 +0,0 @@ -void shrink_region_uncoded( VipsRegion *from, VipsRegion *to, - VipsRect *target ); - diff --git a/libvips/foreign/fits.c b/libvips/foreign/fits.c index 068a27d4..583c272a 100644 --- a/libvips/foreign/fits.c +++ b/libvips/foreign/fits.c @@ -73,7 +73,7 @@ #include -#include "fits.h" +#include "pforeign.h" /* diff --git a/libvips/foreign/fits.h b/libvips/foreign/fits.h deleted file mode 100644 index 0eb1ef49..00000000 --- a/libvips/foreign/fits.h +++ /dev/null @@ -1,50 +0,0 @@ -/* defs for fits 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_FITS_H -#define VIPS_FITS_H - -#ifdef __cplusplus -extern "C" { -#endif /*__cplusplus*/ - -extern const char *vips__fits_suffs[]; - -int vips__fits_isfits( const char *filename ); -int vips__fits_read_header( const char *filename, VipsImage *out ); -int vips__fits_read( const char *filename, VipsImage *out ); - -int vips__fits_write( VipsImage *in, const char *filename ); - -#ifdef __cplusplus -} -#endif /*__cplusplus*/ - -#endif /*VIPS_FITS_H*/ diff --git a/libvips/foreign/fitsload.c b/libvips/foreign/fitsload.c index 4b5dea8e..07454651 100644 --- a/libvips/foreign/fitsload.c +++ b/libvips/foreign/fitsload.c @@ -50,7 +50,7 @@ #ifdef HAVE_CFITSIO -#include "fits.h" +#include "pforeign.h" typedef struct _VipsForeignLoadFits { VipsForeignLoad parent_object; diff --git a/libvips/foreign/fitssave.c b/libvips/foreign/fitssave.c index 50fbd9f7..e9887f4a 100644 --- a/libvips/foreign/fitssave.c +++ b/libvips/foreign/fitssave.c @@ -51,7 +51,7 @@ #ifdef HAVE_CFITSIO -#include "fits.h" +#include "pforeign.h" typedef struct _VipsForeignSaveFits { VipsForeignSave parent_object; diff --git a/libvips/foreign/foreign.c b/libvips/foreign/foreign.c index 68ae9083..3095ed55 100644 --- a/libvips/foreign/foreign.c +++ b/libvips/foreign/foreign.c @@ -59,6 +59,8 @@ #include #include +#include "pforeign.h" + /** * SECTION: foreign * @short_description: load and save images in a variety of formats @@ -1636,7 +1638,8 @@ vips_foreign_operation_init( void ) extern GType vips_foreign_save_jpeg_mime_get_type( void ); extern GType vips_foreign_load_tiff_file_get_type( void ); extern GType vips_foreign_load_tiff_buffer_get_type( void ); - extern GType vips_foreign_save_tiff_get_type( void ); + extern GType vips_foreign_save_tiff_file_get_type( void ); + extern GType vips_foreign_save_tiff_buffer_get_type( void ); extern GType vips_foreign_load_vips_get_type( void ); extern GType vips_foreign_save_vips_get_type( void ); extern GType vips_foreign_load_raw_get_type( void ); @@ -1738,7 +1741,8 @@ vips_foreign_operation_init( void ) #ifdef HAVE_TIFF vips_foreign_load_tiff_file_get_type(); vips_foreign_load_tiff_buffer_get_type(); - vips_foreign_save_tiff_get_type(); + vips_foreign_save_tiff_file_get_type(); + vips_foreign_save_tiff_buffer_get_type(); #endif /*HAVE_TIFF*/ #ifdef HAVE_OPENSLIDE diff --git a/libvips/foreign/jpeg2vips.c b/libvips/foreign/jpeg2vips.c index 2a4eba1b..676ccd7e 100644 --- a/libvips/foreign/jpeg2vips.c +++ b/libvips/foreign/jpeg2vips.c @@ -143,8 +143,9 @@ #include #include +#include "pforeign.h" + #include "jpeg.h" -#include "vipsjpeg.h" /* Stuff we track during a read. */ diff --git a/libvips/foreign/jpegload.c b/libvips/foreign/jpegload.c index 79e6b783..186fd28f 100644 --- a/libvips/foreign/jpegload.c +++ b/libvips/foreign/jpegload.c @@ -52,6 +52,8 @@ #include #include +#include "pforeign.h" + #ifdef HAVE_JPEG #ifdef HAVE_EXIF @@ -68,8 +70,6 @@ #endif /*UNTAGGED_EXIF*/ #endif /*HAVE_EXIF*/ -#include "vipsjpeg.h" - typedef struct _VipsForeignLoadJpeg { VipsForeignLoad parent_object; diff --git a/libvips/foreign/jpegsave.c b/libvips/foreign/jpegsave.c index bf5f1283..2c5dc321 100644 --- a/libvips/foreign/jpegsave.c +++ b/libvips/foreign/jpegsave.c @@ -50,6 +50,8 @@ #include #include +#include "pforeign.h" + #ifdef HAVE_JPEG #ifdef HAVE_EXIF @@ -66,8 +68,6 @@ #endif /*UNTAGGED_EXIF*/ #endif /*HAVE_EXIF*/ -#include "vipsjpeg.h" - typedef struct _VipsForeignSaveJpeg { VipsForeignSave parent_object; diff --git a/libvips/foreign/magick.h b/libvips/foreign/magick.h deleted file mode 100644 index 84021c8a..00000000 --- a/libvips/foreign/magick.h +++ /dev/null @@ -1,52 +0,0 @@ -/* common defs for libMagick 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_MAGICK_H -#define VIPS_MAGICK_H - -#ifdef __cplusplus -extern "C" { -#endif /*__cplusplus*/ - -int vips__magick_read( const char *filename, - VipsImage *out, gboolean all_frames, const char *density, int page ); -int vips__magick_read_header( const char *filename, - VipsImage *out, gboolean all_frames, const char *density, int page ); - -int vips__magick_read_buffer( const void *buf, const size_t len, - VipsImage *out, gboolean all_frames, const char *density, int page ); -int vips__magick_read_buffer_header( const void *buf, const size_t len, - VipsImage *out, gboolean all_frames, const char *density, int page ); - -#ifdef __cplusplus -} -#endif /*__cplusplus*/ - -#endif /*VIPS_MAGICK_H*/ diff --git a/libvips/foreign/magick2vips.c b/libvips/foreign/magick2vips.c index 288cbf51..98730280 100644 --- a/libvips/foreign/magick2vips.c +++ b/libvips/foreign/magick2vips.c @@ -100,7 +100,7 @@ #include -#include "magick.h" +#include "pforeign.h" /* pre-float Magick used to call this MaxRGB. */ diff --git a/libvips/foreign/magickload.c b/libvips/foreign/magickload.c index d533e2c9..9b5eb0db 100644 --- a/libvips/foreign/magickload.c +++ b/libvips/foreign/magickload.c @@ -56,7 +56,7 @@ #ifdef HAVE_MAGICK -#include "magick.h" +#include "pforeign.h" typedef struct _VipsForeignLoadMagick { VipsForeignLoad parent_object; diff --git a/libvips/foreign/matlab.c b/libvips/foreign/matlab.c index a4b3d576..b2d6a01a 100644 --- a/libvips/foreign/matlab.c +++ b/libvips/foreign/matlab.c @@ -73,7 +73,7 @@ #include -#include "matlab.h" +#include "pforeign.h" /* What we track during a Mat-file read. */ diff --git a/libvips/foreign/matlab.h b/libvips/foreign/matlab.h deleted file mode 100644 index 8bed6b9a..00000000 --- a/libvips/foreign/matlab.h +++ /dev/null @@ -1,48 +0,0 @@ -/* common defs for matio 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_MAT_H -#define VIPS_MAT_H - -#ifdef __cplusplus -extern "C" { -#endif /*__cplusplus*/ - -extern const char *vips__mat_suffs[]; - -int vips__mat_load( const char *filename, VipsImage *out ); -int vips__mat_header( const char *filename, VipsImage *out ); -int vips__mat_ismat( const char *filename ); - -#ifdef __cplusplus -} -#endif /*__cplusplus*/ - -#endif /*VIPS_MAT_H*/ diff --git a/libvips/foreign/matload.c b/libvips/foreign/matload.c index e33a2fd9..1d749ed8 100644 --- a/libvips/foreign/matload.c +++ b/libvips/foreign/matload.c @@ -52,7 +52,7 @@ #ifdef HAVE_MATIO -#include "matlab.h" +#include "pforeign.h" typedef struct _VipsForeignLoadMat { VipsForeignLoad parent_object; diff --git a/libvips/foreign/matrixload.c b/libvips/foreign/matrixload.c index 260a78b7..30b3632e 100644 --- a/libvips/foreign/matrixload.c +++ b/libvips/foreign/matrixload.c @@ -48,7 +48,7 @@ #include #include -#include "csv.h" +#include "pforeign.h" typedef struct _VipsForeignLoadMatrix { VipsForeignLoad parent_object; diff --git a/libvips/foreign/matrixsave.c b/libvips/foreign/matrixsave.c index d3e1934b..fc851fbd 100644 --- a/libvips/foreign/matrixsave.c +++ b/libvips/foreign/matrixsave.c @@ -47,7 +47,7 @@ #include -#include "csv.h" +#include "pforeign.h" typedef struct _VipsForeignSaveMatrix { VipsForeignSave parent_object; diff --git a/libvips/foreign/openexr2vips.c b/libvips/foreign/openexr2vips.c index 6d89fe0b..085ea682 100644 --- a/libvips/foreign/openexr2vips.c +++ b/libvips/foreign/openexr2vips.c @@ -86,7 +86,7 @@ #include -#include "openexr2vips.h" +#include "pforeign.h" /* What we track during a OpenEXR read. */ diff --git a/libvips/foreign/openexr2vips.h b/libvips/foreign/openexr2vips.h deleted file mode 100644 index e162b8f2..00000000 --- a/libvips/foreign/openexr2vips.h +++ /dev/null @@ -1,47 +0,0 @@ -/* common defs for jpeg 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_OPENEXR2VIPS_H -#define VIPS_OPENEXR2VIPS_H - -#ifdef __cplusplus -extern "C" { -#endif /*__cplusplus*/ - -int vips__openexr_isexr( const char *filename ); -gboolean vips__openexr_istiled( const char *filename ); -int vips__openexr_read_header( const char *filename, VipsImage *out ); -int vips__openexr_read( const char *filename, VipsImage *out ); - -#ifdef __cplusplus -} -#endif /*__cplusplus*/ - -#endif /*VIPS_OPENEXR2VIPS_H*/ diff --git a/libvips/foreign/openexrload.c b/libvips/foreign/openexrload.c index 94a32287..a03f051e 100644 --- a/libvips/foreign/openexrload.c +++ b/libvips/foreign/openexrload.c @@ -50,7 +50,7 @@ #ifdef HAVE_OPENEXR -#include "openexr2vips.h" +#include "pforeign.h" typedef struct _VipsForeignLoadOpenexr { VipsForeignLoad parent_object; diff --git a/libvips/foreign/openslide2vips.c b/libvips/foreign/openslide2vips.c index 93b05a6b..bd94a196 100644 --- a/libvips/foreign/openslide2vips.c +++ b/libvips/foreign/openslide2vips.c @@ -91,9 +91,9 @@ #include #include -#include +#include "pforeign.h" -#include "openslide2vips.h" +#include typedef struct { openslide_t *osr; diff --git a/libvips/foreign/openslide2vips.h b/libvips/foreign/openslide2vips.h deleted file mode 100644 index 3b903a6b..00000000 --- a/libvips/foreign/openslide2vips.h +++ /dev/null @@ -1,50 +0,0 @@ -/* common defs for jpeg 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_OPENSLIDE2VIPS_H -#define VIPS_OPENSLIDE2VIPS_H - -#ifdef __cplusplus -extern "C" { -#endif /*__cplusplus*/ - -int vips__openslide_isslide( const char *filename ); -int vips__openslide_read_header( const char *filename, VipsImage *out, - int level, gboolean autocrop, char *associated ); -int vips__openslide_read( const char *filename, VipsImage *out, - int level, gboolean autocrop ); -int vips__openslide_read_associated( const char *filename, VipsImage *out, - const char *associated ); - -#ifdef __cplusplus -} -#endif /*__cplusplus*/ - -#endif /*VIPS_OPENSLIDE2VIPS_H*/ diff --git a/libvips/foreign/openslideload.c b/libvips/foreign/openslideload.c index 515f208f..a02927fb 100644 --- a/libvips/foreign/openslideload.c +++ b/libvips/foreign/openslideload.c @@ -55,9 +55,9 @@ #include #include -#ifdef HAVE_OPENSLIDE +#include "pforeign.h" -#include "openslide2vips.h" +#ifdef HAVE_OPENSLIDE typedef struct _VipsForeignLoadOpenslide { VipsForeignLoad parent_object; diff --git a/libvips/foreign/pforeign.h b/libvips/foreign/pforeign.h new file mode 100644 index 00000000..95b63ea8 --- /dev/null +++ b/libvips/foreign/pforeign.h @@ -0,0 +1,230 @@ +/* Private decls shared by all foreign. + */ + +/* + + 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_PARITHMETIC_H +#define VIPS_PARITHMETIC_H + +#ifdef __cplusplus +extern "C" { +#endif /*__cplusplus*/ + +void vips__tiff_init( void ); + +int vips__tiff_write( VipsImage *in, const char *filename, + VipsForeignTiffCompression compression, int Q, + VipsForeignTiffPredictor predictor, + char *profile, + gboolean tile, int tile_width, int tile_height, + gboolean pyramid, + gboolean squash, + gboolean miniswhite, + VipsForeignTiffResunit resunit, double xres, double yres, + gboolean bigtiff, + gboolean rgbjpeg, + gboolean properties, + gboolean strip ); + +int vips__tiff_write_buf( VipsImage *in, + void **obuf, size_t *olen, + VipsForeignTiffCompression compression, int Q, + VipsForeignTiffPredictor predictor, + char *profile, + gboolean tile, int tile_width, int tile_height, + gboolean pyramid, + gboolean squash, + gboolean miniswhite, + VipsForeignTiffResunit resunit, double xres, double yres, + gboolean bigtiff, + gboolean rgbjpeg, + gboolean properties, gboolean strip ); + +int vips__tiff_read_header( const char *filename, VipsImage *out, + int page, gboolean autorotate ); +int vips__tiff_read( const char *filename, VipsImage *out, + int page, gboolean autorotate, gboolean readbehind ); +gboolean vips__istifftiled( const char *filename ); +gboolean vips__istiff_buffer( const void *buf, size_t len ); +gboolean vips__istiff( const char *filename ); + +int vips__tiff_read_header_buffer( const void *buf, size_t len, VipsImage *out, + int page, gboolean autorotate ); +int vips__tiff_read_buffer( const void *buf, size_t len, VipsImage *out, + int page, gboolean autorotate, gboolean readbehind ); + +extern const char *vips__foreign_tiff_suffs[]; + +int vips__isanalyze( const char *filename ); +int vips__analyze_read_header( const char *filename, VipsImage *out ); +int vips__analyze_read( const char *filename, VipsImage *out ); + +extern const char *vips__foreign_csv_suffs[]; + +int vips__csv_read( const char *filename, VipsImage *out, + int skip, int lines, const char *whitespace, const char *separator ); +int vips__csv_read_header( const char *filename, VipsImage *out, + int skip, int lines, const char *whitespace, const char *separator ); + +int vips__csv_write( VipsImage *in, const char *filename, + const char *separator ); + +int vips__matrix_read_header( const char *filename, + int *width, int *height, double *scale, double *offset ); +int vips__matrix_ismatrix( const char *filename ); +VipsImage *vips__matrix_read_file( FILE *fp ); +VipsImage *vips__matrix_read( const char *filename ); +int vips__matrix_write( VipsImage *in, const char *filename ); +int vips__matrix_write_file( VipsImage *in, FILE *fp ); + +extern const char *vips__foreign_matrix_suffs[]; + +int vips__openexr_isexr( const char *filename ); +gboolean vips__openexr_istiled( const char *filename ); +int vips__openexr_read_header( const char *filename, VipsImage *out ); +int vips__openexr_read( const char *filename, VipsImage *out ); + +extern const char *vips__fits_suffs[]; + +int vips__fits_isfits( const char *filename ); +int vips__fits_read_header( const char *filename, VipsImage *out ); +int vips__fits_read( const char *filename, VipsImage *out ); + +int vips__fits_write( VipsImage *in, const char *filename ); + +int vips__magick_read( const char *filename, + VipsImage *out, gboolean all_frames, const char *density, int page ); +int vips__magick_read_header( const char *filename, + VipsImage *out, gboolean all_frames, const char *density, int page ); + +int vips__magick_read_buffer( const void *buf, const size_t len, + VipsImage *out, gboolean all_frames, const char *density, int page ); +int vips__magick_read_buffer_header( const void *buf, const size_t len, + VipsImage *out, gboolean all_frames, const char *density, int page ); + +extern const char *vips__mat_suffs[]; + +int vips__mat_load( const char *filename, VipsImage *out ); +int vips__mat_header( const char *filename, VipsImage *out ); +int vips__mat_ismat( const char *filename ); + +int vips__ppm_header( const char *name, VipsImage *out ); +int vips__ppm_load( const char *name, VipsImage *out ); +int vips__ppm_isppm( const char *filename ); +VipsForeignFlags vips__ppm_flags( const char *filename ); +extern const char *vips__ppm_suffs[]; + +int vips__ppm_save( VipsImage *in, const char *filename, + gboolean ascii, gboolean squash ); + +int vips__rad_israd( const char *filename ); +int vips__rad_header( const char *filename, VipsImage *out ); +int vips__rad_load( const char *filename, VipsImage *out, gboolean readbehind ); + +int vips__rad_save( VipsImage *in, const char *filename ); +int vips__rad_save_buf( VipsImage *in, void **obuf, size_t *olen ); + +extern const char *vips__rad_suffs[]; + +extern const char *vips__jpeg_suffs[]; + +int vips__jpeg_write_file( VipsImage *in, + const char *filename, int Q, const char *profile, + gboolean optimize_coding, gboolean progressive, gboolean strip, + gboolean no_subsample, gboolean trellis_quant, + gboolean overshoot_deringing, gboolean optimize_scans, int quant_table ); +int vips__jpeg_write_buffer( VipsImage *in, + void **obuf, size_t *olen, int Q, const char *profile, + gboolean optimize_coding, gboolean progressive, gboolean strip, + gboolean no_subsample, gboolean trellis_quant, + gboolean overshoot_deringing, gboolean optimize_scans, int quant_table ); + +int vips__isjpeg_buffer( const void *buf, size_t len ); +int vips__isjpeg( const char *filename ); +int vips__jpeg_read_file( const char *name, VipsImage *out, + gboolean header_only, int shrink, gboolean fail, gboolean readbehind, + gboolean autorotate ); +int vips__jpeg_read_buffer( const void *buf, size_t len, VipsImage *out, + gboolean header_only, int shrink, int fail, gboolean readbehind, + gboolean autorotate ); + +int vips__png_header( const char *name, VipsImage *out ); +int vips__png_read( const char *name, VipsImage *out, gboolean readbehind ); +gboolean vips__png_ispng_buffer( const void *buf, size_t len ); +int vips__png_ispng( const char *filename ); +gboolean vips__png_isinterlaced( const char *filename ); +extern const char *vips__png_suffs[]; +int vips__png_read_buffer( const void *buffer, size_t length, + VipsImage *out, gboolean readbehind ); +int vips__png_header_buffer( const void *buffer, size_t length, + VipsImage *out ); + +int vips__png_write( VipsImage *in, const char *filename, + int compress, int interlace, const char *profile, + VipsForeignPngFilter filter, gboolean strip ); +int vips__png_write_buf( VipsImage *in, + void **obuf, size_t *olen, int compression, int interlace, + const char *profile, VipsForeignPngFilter filter, gboolean strip ); + +extern const char *vips__webp_suffs[]; + +int vips__iswebp_buffer( const void *buf, size_t len ); +int vips__iswebp( const char *filename ); + +int vips__webp_read_file_header( const char *name, VipsImage *out, int shrink ); +int vips__webp_read_file( const char *name, VipsImage *out, int shrink ); + +int vips__webp_read_buffer_header( const void *buf, size_t len, + VipsImage *out, int shrink ); +int vips__webp_read_buffer( const void *buf, size_t len, + VipsImage *out, int shrink ); + +int vips__webp_write_file( VipsImage *out, const char *filename, + int Q, gboolean lossless, VipsForeignWebpPreset preset, + gboolean smart_subsample, gboolean near_lossless, + int alpha_q ); +int vips__webp_write_buffer( VipsImage *out, void **buf, size_t *len, + int Q, gboolean lossless, VipsForeignWebpPreset preset, + gboolean smart_subsample, gboolean near_lossless, + int alpha_q ); + +int vips__openslide_isslide( const char *filename ); +int vips__openslide_read_header( const char *filename, VipsImage *out, + int level, gboolean autocrop, char *associated ); +int vips__openslide_read( const char *filename, VipsImage *out, + int level, gboolean autocrop ); +int vips__openslide_read_associated( const char *filename, VipsImage *out, + const char *associated ); + +#ifdef __cplusplus +} +#endif /*__cplusplus*/ + +#endif /*VIPS_PARITHMETIC_H*/ + + diff --git a/libvips/foreign/pngload.c b/libvips/foreign/pngload.c index 4bb7cdfc..ad1305b0 100644 --- a/libvips/foreign/pngload.c +++ b/libvips/foreign/pngload.c @@ -48,9 +48,9 @@ #include #include -#ifdef HAVE_PNG +#include "pforeign.h" -#include "vipspng.h" +#ifdef HAVE_PNG typedef struct _VipsForeignLoadPng { VipsForeignLoad parent_object; diff --git a/libvips/foreign/pngsave.c b/libvips/foreign/pngsave.c index 959c270a..ff1d63e6 100644 --- a/libvips/foreign/pngsave.c +++ b/libvips/foreign/pngsave.c @@ -49,9 +49,9 @@ #include -#ifdef HAVE_PNG +#include "pforeign.h" -#include "vipspng.h" +#ifdef HAVE_PNG typedef struct _VipsForeignSavePng { VipsForeignSave parent_object; diff --git a/libvips/foreign/ppm.c b/libvips/foreign/ppm.c index d6e4ecae..22315526 100644 --- a/libvips/foreign/ppm.c +++ b/libvips/foreign/ppm.c @@ -80,7 +80,7 @@ #include #include -#include "ppm.h" +#include "pforeign.h" /* The largest number/field/whatever we can read. */ diff --git a/libvips/foreign/ppm.h b/libvips/foreign/ppm.h deleted file mode 100644 index adc6325a..00000000 --- a/libvips/foreign/ppm.h +++ /dev/null @@ -1,51 +0,0 @@ -/* common defs for ppm 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_PPM_H -#define VIPS_PPM_H - -#ifdef __cplusplus -extern "C" { -#endif /*__cplusplus*/ - -int vips__ppm_header( const char *name, VipsImage *out ); -int vips__ppm_load( const char *name, VipsImage *out ); -int vips__ppm_isppm( const char *filename ); -VipsForeignFlags vips__ppm_flags( const char *filename ); -extern const char *vips__ppm_suffs[]; - -int vips__ppm_save( VipsImage *in, const char *filename, - gboolean ascii, gboolean squash ); - -#ifdef __cplusplus -} -#endif /*__cplusplus*/ - -#endif /*VIPS_PPM_H*/ diff --git a/libvips/foreign/ppmload.c b/libvips/foreign/ppmload.c index df458c9f..81d41686 100644 --- a/libvips/foreign/ppmload.c +++ b/libvips/foreign/ppmload.c @@ -48,9 +48,9 @@ #include #include -#ifdef HAVE_PPM +#include "pforeign.h" -#include "ppm.h" +#ifdef HAVE_PPM typedef struct _VipsForeignLoadPpm { VipsForeignLoad parent_object; diff --git a/libvips/foreign/ppmsave.c b/libvips/foreign/ppmsave.c index f6ac8988..102d6297 100644 --- a/libvips/foreign/ppmsave.c +++ b/libvips/foreign/ppmsave.c @@ -47,9 +47,9 @@ #include -#ifdef HAVE_PPM +#include "pforeign.h" -#include "ppm.h" +#ifdef HAVE_PPM typedef struct _VipsForeignSavePpm { VipsForeignSave parent_object; diff --git a/libvips/foreign/radiance.c b/libvips/foreign/radiance.c index 8e4fc1ef..73903470 100644 --- a/libvips/foreign/radiance.c +++ b/libvips/foreign/radiance.c @@ -143,7 +143,7 @@ #include #include -#include "radiance.h" +#include "pforeign.h" /* Begin copy-paste from Radiance sources. */ diff --git a/libvips/foreign/radiance.h b/libvips/foreign/radiance.h deleted file mode 100644 index 71a259b8..00000000 --- a/libvips/foreign/radiance.h +++ /dev/null @@ -1,51 +0,0 @@ -/* common defs for ppm 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_RADIANCE_H -#define VIPS_RADIANCE_H - -#ifdef __cplusplus -extern "C" { -#endif /*__cplusplus*/ - -int vips__rad_israd( const char *filename ); -int vips__rad_header( const char *filename, VipsImage *out ); -int vips__rad_load( const char *filename, VipsImage *out, gboolean readbehind ); - -int vips__rad_save( VipsImage *in, const char *filename ); -int vips__rad_save_buf( VipsImage *in, void **obuf, size_t *olen ); - -extern const char *vips__rad_suffs[]; - -#ifdef __cplusplus -} -#endif /*__cplusplus*/ - -#endif /*VIPS_RADIANCE_H*/ diff --git a/libvips/foreign/radload.c b/libvips/foreign/radload.c index 8d46d3a7..518ae8a8 100644 --- a/libvips/foreign/radload.c +++ b/libvips/foreign/radload.c @@ -48,9 +48,9 @@ #include #include -#ifdef HAVE_RADIANCE +#include "pforeign.h" -#include "radiance.h" +#ifdef HAVE_RADIANCE typedef struct _VipsForeignLoadRad { VipsForeignLoad parent_object; diff --git a/libvips/foreign/radsave.c b/libvips/foreign/radsave.c index da720086..6d4e2481 100644 --- a/libvips/foreign/radsave.c +++ b/libvips/foreign/radsave.c @@ -49,9 +49,9 @@ #include -#ifdef HAVE_RADIANCE +#include "pforeign.h" -#include "radiance.h" +#ifdef HAVE_RADIANCE typedef struct _VipsForeignSaveRad { VipsForeignSave parent_object; diff --git a/libvips/foreign/tiff.c b/libvips/foreign/tiff.c new file mode 100644 index 00000000..ba82f4ee --- /dev/null +++ b/libvips/foreign/tiff.c @@ -0,0 +1,459 @@ +/* Some shared TIFF utilities. + * + * 14/10/16 + * - from vips2tiff.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 + */ + +#ifdef HAVE_CONFIG_H +#include +#endif /*HAVE_CONFIG_H*/ +#include + +#ifdef HAVE_TIFF + +#include +#include +#ifdef HAVE_UNISTD_H +#include +#endif /*HAVE_UNISTD_H*/ +#include +#include + +#include +#include + +#include + +#include "tiff.h" + +/* Handle TIFF errors here. Shared with vips2tiff.c. These can be called from + * more than one thread, but vips_error and vips_warn have mutexes in, so that's + * OK. + */ +static void +vips__thandler_error( const char *module, const char *fmt, va_list ap ) +{ + vips_verror( module, fmt, ap ); +} + +static void +vips__thandler_warning( const char *module, const char *fmt, va_list ap ) +{ + char buf[256]; + + vips_vsnprintf( buf, 256, fmt, ap ); + vips_warn( module, "%s", buf ); +} + +/* Call this during startup. Other libraries may be using libtiff and we want + * to capture any messages they send as well. + */ +void +vips__tiff_init( void ) +{ + TIFFSetErrorHandler( vips__thandler_error ); + TIFFSetWarningHandler( vips__thandler_warning ); +} + +/* Open TIFF for output. + */ +TIFF * +vips__tiff_openout( const char *path, gboolean bigtiff ) +{ + TIFF *tif; + const char *mode = bigtiff ? "w8" : "w"; + +#ifdef DEBUG + printf( "vips__tiff_openout( \"%s\", \"%s\" )\n", path, mode ); +#endif /*DEBUG*/ + + /* Need the utf-16 version on Windows. + */ +#ifdef OS_WIN32 +{ + GError *error = NULL; + wchar_t *path16; + + if( !(path16 = (wchar_t *) + g_utf8_to_utf16( path, -1, NULL, NULL, &error )) ) { + vips_g_error( &error ); + return( NULL ); + } + + tif = TIFFOpenW( path16, mode ); + + g_free( path16 ); +} +#else /*!OS_WIN32*/ + tif = TIFFOpen( path, mode ); +#endif /*OS_WIN32*/ + + if( !tif ) { + vips_error( "tiff", + _( "unable to open \"%s\" for output" ), path ); + return( NULL ); + } + + return( tif ); +} + +/* Open TIFF for input from a file. + */ +TIFF * +vips__tiff_openin( const char *path ) +{ + /* No mmap --- no performance advantage with libtiff, and it burns up + * our VM if the tiff file is large. + */ + const char *mode = "rm"; + + TIFF *tif; + +#ifdef DEBUG + printf( "vips__tiff_openin( \"%s\" )\n", path ); +#endif /*DEBUG*/ + + /* Need the utf-16 version on Windows. + */ +#ifdef OS_WIN32 +{ + GError *error = NULL; + wchar_t *path16; + + if( !(path16 = (wchar_t *) + g_utf8_to_utf16( path, -1, NULL, NULL, &error )) ) { + vips_g_error( &error ); + return( NULL ); + } + + tif = TIFFOpenW( path16, mode ); + + g_free( path16 ); +} +#else /*!OS_WIN32*/ + tif = TIFFOpen( path, mode ); +#endif /*OS_WIN32*/ + + if( !tif ) { + vips_error( "tiff", + _( "unable to open \"%s\" for input" ), path ); + return( NULL ); + } + + return( tif ); +} + +/* TIFF input from a memory buffer. + */ + +typedef struct _VipsTiffOpeninBuffer { + size_t position; + const void *data; + size_t length; +} VipsTiffOpeninBuffer; + +static tsize_t +openin_buffer_read( thandle_t st, tdata_t data, tsize_t size ) +{ + VipsTiffOpeninBuffer *buffer = (VipsTiffOpeninBuffer *) st; + + size_t available; + size_t copied; + + if( buffer->position > buffer->length ) { + vips_error( "openin_buffer_read", + "%s", _( "read beyond end of buffer" ) ); + return( 0 ); + } + + available = buffer->length - buffer->position; + copied = VIPS_MIN( size, available ); + memcpy( data, + (unsigned char *) buffer->data + buffer->position, copied ); + buffer->position += copied; + + return( copied ); +} + +static tsize_t +openin_buffer_write( thandle_t st, tdata_t buffer, tsize_t size ) +{ + g_assert_not_reached(); + + return( 0 ); +} + +static int +openin_buffer_close( thandle_t st ) +{ + return( 0 ); +} + +/* After calling this, ->pos is not bound by the size of the buffer, it can + * have any positive value. + */ +static toff_t +openin_buffer_seek( thandle_t st, toff_t position, int whence ) +{ + VipsTiffOpeninBuffer *buffer = (VipsTiffOpeninBuffer *) st; + + if( whence == SEEK_SET ) + buffer->position = position; + else if( whence == SEEK_CUR ) + buffer->position += position; + else if( whence == SEEK_END ) + buffer->position = buffer->length + position; + else + g_assert_not_reached(); + + return( buffer->position ); +} + +static toff_t +openin_buffer_size( thandle_t st ) +{ + VipsTiffOpeninBuffer *buffer = (VipsTiffOpeninBuffer *) st; + + return( buffer->length ); +} + +static int +openin_buffer_map( thandle_t st, tdata_t *start, toff_t *len ) +{ + g_assert_not_reached(); + + return( 0 ); +} + +static void +openin_buffer_unmap( thandle_t st, tdata_t start, toff_t len ) +{ + g_assert_not_reached(); + + return; +} + +TIFF * +vips__tiff_openin_buffer( VipsImage *image, const void *data, size_t length ) +{ + VipsTiffOpeninBuffer *buffer; + TIFF *tiff; + +#ifdef DEBUG + printf( "vips__tiff_openin_buffer:\n" ); +#endif /*DEBUG*/ + + buffer = VIPS_NEW( image, VipsTiffOpeninBuffer ); + buffer->position = 0; + buffer->data = data; + buffer->length = length; + + if( !(tiff = TIFFClientOpen( "memory input", "rm", + (thandle_t) buffer, + openin_buffer_read, + openin_buffer_write, + openin_buffer_seek, + openin_buffer_close, + openin_buffer_size, + openin_buffer_map, + openin_buffer_unmap )) ) { + vips_error( "vips__tiff_openin_buffer", "%s", + _( "unable to open memory buffer for input" ) ); + return( NULL ); + } + + return( tiff ); +} + +/* TIFF output to a memory buffer. + */ + +typedef struct _VipsTiffOpenoutBuffer { + size_t position; + void *data; + size_t allocated; + size_t length; + + /* On close, consolidate and write the output here. + */ + void **out_data; + size_t *out_length; +} VipsTiffOpenoutBuffer; + +static tsize_t +openout_buffer_read( thandle_t st, tdata_t data, tsize_t size ) +{ + g_assert_not_reached(); + + return( 0 ); +} + +static tsize_t +openout_buffer_write( thandle_t st, tdata_t data, tsize_t size ) +{ + VipsTiffOpenoutBuffer *buffer = (VipsTiffOpenoutBuffer *) st; + size_t new_length = VIPS_MAX( buffer->position + size, buffer->length ); + +#ifdef DEBUG + printf( "openout_buffer_write: %zd bytes\n", size ); +#endif /*DEBUG*/ + + /* We could make a chain of blocks, but libtiff does a lot of seeking + * during writes, so we'd have to handle writes being + * split over blocks, which would be annoying. + * + * Instead, go for exponential realloc: the number of reallocs grows as + * log(final size), and we never over allocate by more than 30%. + * + * realloc can be very, very slow on Windows, but maybe g_realloc() is + * smarter. + */ + if( new_length > buffer->allocated ) { + buffer->allocated = VIPS_MAX( (16 + buffer->allocated) * 3 / 2, + new_length ); + buffer->data = g_try_realloc( buffer->data, buffer->allocated ); + + if( buffer->data == NULL ) { + vips_error( "tiff memory write", + "%s", _( "Out of memory." ) ); + return( 0 ); + } + } + + memcpy( buffer->data + buffer->position, data, size ); + buffer->position += size; + buffer->length = new_length; + + return( size ); +} + +static int +openout_buffer_close( thandle_t st ) +{ + VipsTiffOpenoutBuffer *buffer = (VipsTiffOpenoutBuffer *) st; + + *(buffer->out_data) = buffer->data; + *(buffer->out_length) = buffer->length; + + return( 0 ); +} + +static toff_t +openout_buffer_seek( thandle_t st, toff_t position, int whence ) +{ + VipsTiffOpenoutBuffer *buffer = (VipsTiffOpenoutBuffer *) st; + +#ifdef DEBUG + printf( "openout_buffer_seek: position %zd, whence %d\n", + position, whence ); +#endif /*DEBUG*/ + + if( whence == SEEK_SET ) + buffer->position = position; + else if( whence == SEEK_CUR ) + buffer->position += position; + else if( whence == SEEK_END ) + buffer->position = buffer->length + position; + else + g_assert_not_reached(); + + return( buffer->position ); +} + +static toff_t +openout_buffer_size( thandle_t st ) +{ + g_assert_not_reached(); + + return( 0 ); +} + +static int +openout_buffer_map( thandle_t st, tdata_t *start, toff_t *len ) +{ + g_assert_not_reached(); + + return( 0 ); +} + +static void +openout_buffer_unmap( thandle_t st, tdata_t start, toff_t len ) +{ + g_assert_not_reached(); + + return; +} + +/* On TIFFClose(), @data and @length are set to point to the output buffer. + */ +TIFF * +vips__tiff_openout_buffer( VipsImage *image, + gboolean bigtiff, void **out_data, size_t *out_length ) +{ + const char *mode = bigtiff ? "w8" : "w"; + + VipsTiffOpenoutBuffer *buffer; + TIFF *tiff; + +#ifdef DEBUG + printf( "vips__tiff_openout_buffer:\n" ); +#endif /*DEBUG*/ + + buffer = VIPS_NEW( image, VipsTiffOpenoutBuffer ); + buffer->position = 0; + buffer->data = NULL; + buffer->allocated = 0; + buffer->length = 0; + buffer->out_data = out_data; + buffer->out_length = out_length; + + if( !(tiff = TIFFClientOpen( "memory output", mode, + (thandle_t) buffer, + openout_buffer_read, + openout_buffer_write, + openout_buffer_seek, + openout_buffer_close, + openout_buffer_size, + openout_buffer_map, + openout_buffer_unmap )) ) { + vips_error( "vips__tiff_openout_buffer", "%s", + _( "unable to open memory buffer for output" ) ); + return( NULL ); + } + + return( tiff ); +} + +#endif /*HAVE_TIFF*/ + diff --git a/libvips/foreign/tiff.h b/libvips/foreign/tiff.h index 9689687d..e86f8c37 100644 --- a/libvips/foreign/tiff.h +++ b/libvips/foreign/tiff.h @@ -31,40 +31,19 @@ #ifndef VIPS_TIFF_H #define VIPS_TIFF_H +#include + #ifdef __cplusplus extern "C" { #endif /*__cplusplus*/ -extern const char *vips__foreign_tiff_suffs[]; +TIFF *vips__tiff_openout( const char *path, gboolean bigtiff ); +TIFF *vips__tiff_openin( const char *path ); -void vips__tiff_init( void ); - -int vips__tiff_write( VipsImage *in, const char *filename, - VipsForeignTiffCompression compression, int Q, - VipsForeignTiffPredictor predictor, - char *profile, - gboolean tile, int tile_width, int tile_height, - gboolean pyramid, - gboolean squash, - gboolean miniswhite, - VipsForeignTiffResunit resunit, double xres, double yres, - gboolean bigtiff, - gboolean rgbjpeg, - gboolean properties, - gboolean strip ); - -int vips__tiff_read_header( const char *filename, VipsImage *out, - int page, gboolean autorotate ); -int vips__tiff_read( const char *filename, VipsImage *out, - int page, gboolean autorotate, gboolean readbehind ); -gboolean vips__istifftiled( const char *filename ); -gboolean vips__istiff_buffer( const void *buf, size_t len ); -gboolean vips__istiff( const char *filename ); - -int vips__tiff_read_header_buffer( const void *buf, size_t len, VipsImage *out, - int page, gboolean autorotate ); -int vips__tiff_read_buffer( const void *buf, size_t len, VipsImage *out, - int page, gboolean autorotate, gboolean readbehind ); +TIFF *vips__tiff_openin_buffer( VipsImage *image, + const void *data, size_t length ); +TIFF *vips__tiff_openout_buffer( VipsImage *image, + gboolean bigtiff, void **out_data, size_t *out_length ); #ifdef __cplusplus } diff --git a/libvips/foreign/tiff2vips.c b/libvips/foreign/tiff2vips.c index 36b36389..4276d9d5 100644 --- a/libvips/foreign/tiff2vips.c +++ b/libvips/foreign/tiff2vips.c @@ -215,8 +215,7 @@ #include #include -#include - +#include "pforeign.h" #include "tiff.h" /* Scanline-type process function. @@ -231,8 +230,6 @@ typedef struct _ReadTiff { /* Parameters. */ char *filename; - const void *buf; - size_t len; VipsImage *out; int page; gboolean autorotate; @@ -282,35 +279,6 @@ typedef struct _ReadTiff { tdata_t contig_buf; } ReadTiff; -/* Handle TIFF errors here. Shared with vips2tiff.c. These can be called from - * more than one thread, but vips_error and vips_warn have mutexes in, so that's - * OK. - */ -static void -vips__thandler_error( const char *module, const char *fmt, va_list ap ) -{ - vips_verror( module, fmt, ap ); -} - -static void -vips__thandler_warning( const char *module, const char *fmt, va_list ap ) -{ - char buf[256]; - - vips_vsnprintf( buf, 256, fmt, ap ); - vips_warn( module, "%s", buf ); -} - -/* Call this during startup. Other libraries may be using libtiff and we want - * to capture any messages they send as well. - */ -void -vips__tiff_init( void ) -{ - TIFFSetErrorHandler( vips__thandler_error ); - TIFFSetWarningHandler( vips__thandler_warning ); -} - /* Test for field exists. */ static int @@ -1829,8 +1797,6 @@ readtiff_new( VipsImage *out, return( NULL ); rtiff->filename = NULL; - rtiff->buf = NULL; - rtiff->len = 0; rtiff->out = out; rtiff->page = page; rtiff->autorotate = autorotate; @@ -1858,10 +1824,6 @@ readtiff_new( VipsImage *out, return( rtiff ); } -/* Can't declare this in tiff.h, we need that to be TIFF-free. - */ -TIFF *vips__tiff_openin( const char *name ); - static ReadTiff * readtiff_new_filename( const char *filename, VipsImage *out, int page, gboolean autorotate, gboolean readbehind ) @@ -1894,86 +1856,6 @@ readtiff_new_filename( const char *filename, VipsImage *out, return( rtiff ); } -static tsize_t -my_tiff_read( thandle_t st, tdata_t buffer, tsize_t size ) -{ - ReadTiff *rtiff = (ReadTiff *) st; - - size_t available; - size_t copy; - - if( rtiff->pos > rtiff->len ) { - vips_error( "tiff2vips", - "%s", _( "read beyond end of buffer" ) ); - return( 0 ); - } - - available = rtiff->len - rtiff->pos; - copy = VIPS_MIN( size, available ); - memcpy( buffer, (unsigned char *) rtiff->buf + rtiff->pos, copy ); - rtiff->pos += copy; - - return( copy ); -} - -static tsize_t -my_tiff_write( thandle_t st, tdata_t buffer, tsize_t size ) -{ - g_assert_not_reached(); - - return( 0 ); -} - -static int -my_tiff_close( thandle_t st ) -{ - return 0; -} - -/* After calling this, ->pos is not bound by the size of the buffer, it can - * have any positive value. - */ -static toff_t -my_tiff_seek( thandle_t st, toff_t pos, int whence ) -{ - ReadTiff *rtiff = (ReadTiff *) st; - - if( whence == SEEK_SET ) - rtiff->pos = pos; - else if( whence == SEEK_CUR ) - rtiff->pos += pos; - else if( whence == SEEK_END ) - rtiff->pos = rtiff->len + pos; - else - g_assert_not_reached(); - - return( rtiff->pos ); -} - -static toff_t -my_tiff_size( thandle_t st ) -{ - ReadTiff *rtiff = (ReadTiff *) st; - - return( rtiff->len ); -} - -static int -my_tiff_map( thandle_t st, tdata_t *start, toff_t *len ) -{ - g_assert_not_reached(); - - return 0; -} - -static void -my_tiff_unmap( thandle_t st, tdata_t start, toff_t len ) -{ - g_assert_not_reached(); - - return; -} - static ReadTiff * readtiff_new_buffer( const void *buf, size_t len, VipsImage *out, int page, gboolean autorotate, gboolean readbehind ) @@ -1984,17 +1866,8 @@ readtiff_new_buffer( const void *buf, size_t len, VipsImage *out, if( !(rtiff = readtiff_new( out, page, autorotate, readbehind )) ) return( NULL ); - rtiff->buf = buf; - rtiff->len = len; - - if( !(rtiff->tiff = TIFFClientOpen( "memory buffer", "rm", - (thandle_t) rtiff, - my_tiff_read, my_tiff_write, my_tiff_seek, my_tiff_close, - my_tiff_size, my_tiff_map, my_tiff_unmap )) ) { - vips_error( "tiff2vips", "%s", - _( "unable to open memory buffer for input" ) ); + if( !(rtiff->tiff = vips__tiff_openin_buffer( out, buf, len )) ) return( NULL ); - } for( i = 0; i < page; i++ ) if( !TIFFReadDirectory( rtiff->tiff ) ) { diff --git a/libvips/foreign/tiffload.c b/libvips/foreign/tiffload.c index 97b5109a..d5aec59b 100644 --- a/libvips/foreign/tiffload.c +++ b/libvips/foreign/tiffload.c @@ -48,9 +48,9 @@ #include #include -#ifdef HAVE_TIFF +#include "pforeign.h" -#include "tiff.h" +#ifdef HAVE_TIFF typedef struct _VipsForeignLoadTiff { VipsForeignLoad parent_object; diff --git a/libvips/foreign/tiffsave.c b/libvips/foreign/tiffsave.c index bcc2b5ec..bfbc9f26 100644 --- a/libvips/foreign/tiffsave.c +++ b/libvips/foreign/tiffsave.c @@ -56,6 +56,8 @@ #include #include +#include "pforeign.h" + #ifdef HAVE_TIFF #include "tiff.h" @@ -63,10 +65,6 @@ typedef struct _VipsForeignSaveTiff { VipsForeignSave parent_object; - /* Filename for save. - */ - char *filename; - /* Many options argh. */ VipsForeignTiffCompression compression; @@ -89,7 +87,7 @@ typedef struct _VipsForeignSaveTiff { typedef VipsForeignSaveClass VipsForeignSaveTiffClass; -G_DEFINE_TYPE( VipsForeignSaveTiff, vips_foreign_save_tiff, +G_DEFINE_ABSTRACT_TYPE( VipsForeignSaveTiff, vips_foreign_save_tiff, VIPS_TYPE_FOREIGN_SAVE ); #define UC VIPS_FORMAT_UCHAR @@ -156,20 +154,6 @@ vips_foreign_save_tiff_build( VipsObject *object ) tiff->yres *= 2.54; } - if( vips__tiff_write( save->ready, tiff->filename, - tiff->compression, tiff->Q, tiff->predictor, - tiff->profile, - tiff->tile, tiff->tile_width, tiff->tile_height, - tiff->pyramid, - tiff->squash, - tiff->miniswhite, - tiff->resunit, tiff->xres, tiff->yres, - tiff->bigtiff, - tiff->rgbjpeg, - tiff->properties, - save->strip ) ) - return( -1 ); - return( 0 ); } @@ -184,7 +168,7 @@ vips_foreign_save_tiff_class_init( VipsForeignSaveTiffClass *class ) gobject_class->set_property = vips_object_set_property; gobject_class->get_property = vips_object_get_property; - object_class->nickname = "tiffsave"; + object_class->nickname = "tiffsave_base"; object_class->description = _( "save image to tiff file" ); object_class->build = vips_foreign_save_tiff_build; @@ -193,13 +177,6 @@ vips_foreign_save_tiff_class_init( VipsForeignSaveTiffClass *class ) save_class->saveable = VIPS_SAVEABLE_ANY; save_class->coding[VIPS_CODING_LABQ] = TRUE; - VIPS_ARG_STRING( class, "filename", 1, - _( "Filename" ), - _( "Filename to save to" ), - VIPS_ARGUMENT_REQUIRED_INPUT, - G_STRUCT_OFFSET( VipsForeignSaveTiff, filename ), - NULL ); - VIPS_ARG_ENUM( class, "compression", 6, _( "Compression" ), _( "Compression for this file" ), @@ -329,6 +306,146 @@ vips_foreign_save_tiff_init( VipsForeignSaveTiff *tiff ) tiff->yres = 1.0; } +typedef struct _VipsForeignSaveTiffFile { + VipsForeignSaveTiff parent_object; + + char *filename; +} VipsForeignSaveTiffFile; + +typedef VipsForeignSaveTiffClass VipsForeignSaveTiffFileClass; + +G_DEFINE_TYPE( VipsForeignSaveTiffFile, vips_foreign_save_tiff_file, + vips_foreign_save_tiff_get_type() ); + +static int +vips_foreign_save_tiff_file_build( VipsObject *object ) +{ + VipsForeignSave *save = (VipsForeignSave *) object; + VipsForeignSaveTiff *tiff = (VipsForeignSaveTiff *) object; + VipsForeignSaveTiffFile *file = (VipsForeignSaveTiffFile *) object; + + if( VIPS_OBJECT_CLASS( vips_foreign_save_tiff_file_parent_class )-> + build( object ) ) + return( -1 ); + + if( vips__tiff_write( save->ready, file->filename, + tiff->compression, tiff->Q, tiff->predictor, + tiff->profile, + tiff->tile, tiff->tile_width, tiff->tile_height, + tiff->pyramid, + tiff->squash, + tiff->miniswhite, + tiff->resunit, tiff->xres, tiff->yres, + tiff->bigtiff, + tiff->rgbjpeg, + tiff->properties, + save->strip ) ) + return( -1 ); + + return( 0 ); +} + +static void +vips_foreign_save_tiff_file_class_init( VipsForeignSaveTiffFileClass *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 = "tiffsave"; + object_class->description = _( "save image to tiff file" ); + object_class->build = vips_foreign_save_tiff_file_build; + + VIPS_ARG_STRING( class, "filename", 1, + _( "Filename" ), + _( "Filename to save to" ), + VIPS_ARGUMENT_REQUIRED_INPUT, + G_STRUCT_OFFSET( VipsForeignSaveTiffFile, filename ), + NULL ); +} + +static void +vips_foreign_save_tiff_file_init( VipsForeignSaveTiffFile *file ) +{ +} + +typedef struct _VipsForeignSaveTiffBuffer { + VipsForeignSaveTiff parent_object; + + VipsArea *buf; +} VipsForeignSaveTiffBuffer; + +typedef VipsForeignSaveTiffClass VipsForeignSaveTiffBufferClass; + +G_DEFINE_TYPE( VipsForeignSaveTiffBuffer, vips_foreign_save_tiff_buffer, + vips_foreign_save_tiff_get_type() ); + +static int +vips_foreign_save_tiff_buffer_build( VipsObject *object ) +{ + VipsForeignSave *save = (VipsForeignSave *) object; + VipsForeignSaveTiff *tiff = (VipsForeignSaveTiff *) object; + + void *obuf; + size_t olen; + VipsBlob *blob; + + if( VIPS_OBJECT_CLASS( vips_foreign_save_tiff_buffer_parent_class )-> + build( object ) ) + return( -1 ); + + if( vips__tiff_write_buf( save->ready, &obuf, &olen, + tiff->compression, tiff->Q, tiff->predictor, + tiff->profile, + tiff->tile, tiff->tile_width, tiff->tile_height, + tiff->pyramid, + tiff->squash, + tiff->miniswhite, + tiff->resunit, tiff->xres, tiff->yres, + tiff->bigtiff, + tiff->rgbjpeg, + tiff->properties, + save->strip ) ) + return( -1 ); + + /* vips__tiff_write_buf() makes a buffer that needs g_free(), not + * vips_free(). + */ + blob = vips_blob_new( (VipsCallbackFn) g_free, obuf, olen ); + g_object_set( object, "buffer", blob, NULL ); + vips_area_unref( VIPS_AREA( blob ) ); + + return( 0 ); +} + +static void +vips_foreign_save_tiff_buffer_class_init( VipsForeignSaveTiffBufferClass *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 = "tiffsave_buffer"; + object_class->description = _( "save image to tiff buffer" ); + object_class->build = vips_foreign_save_tiff_buffer_build; + + VIPS_ARG_BOXED( class, "buffer", 1, + _( "Buffer" ), + _( "Buffer to save to" ), + VIPS_ARGUMENT_REQUIRED_OUTPUT, + G_STRUCT_OFFSET( VipsForeignSaveTiffBuffer, buf ), + VIPS_TYPE_BLOB ); +} + +static void +vips_foreign_save_tiff_buffer_init( VipsForeignSaveTiffBuffer *buffer ) +{ +} + #endif /*HAVE_TIFF*/ /** @@ -440,3 +557,67 @@ vips_tiffsave( VipsImage *in, const char *filename, ... ) return( result ); } + +/** + * vips_tiffsave_buffer: + * @in: image to save + * @buf: return output buffer here + * @len: return output length here + * @...: %NULL-terminated list of optional named arguments + * + * Optional arguments: + * + * * @compression: use this #VipsForeignTiffCompression + * * @Q: %gint quality factor + * * @predictor: use this #VipsForeignTiffPredictor + * * @profile: filename of ICC profile to attach + * * @tile: set %TRUE to write a tiled tiff + * * @tile_width: %gint for tile size + * * @tile_height: %gint for tile size + * * @pyramid: set %TRUE to write an image pyramid + * * @squash: set %TRUE to squash 8-bit images down to 1 bit + * * @miniswhite: set %TRUE to write 1-bit images as MINISWHITE + * * @resunit: #VipsForeignTiffResunit for resolution unit + * * @xres: %gdouble horizontal resolution in pixels/mm + * * @yres: %gdouble vertical resolution in pixels/mm + * * @bigtiff: set %TRUE to write a BigTiff file + * * @properties: set %TRUE to write an IMAGEDESCRIPTION tag + * * @strip: set %TRUE to block metadata save + * + * As vips_tiffsave(), but save to a memory buffer. + * + * The address of the buffer is returned in @buf, the length of the buffer in + * @len. You are responsible for freeing the buffer with g_free() when you + * are done with it. + * + * See also: vips_tiffsave(), vips_image_write_to_file(). + * + * Returns: 0 on success, -1 on error. + */ +int +vips_tiffsave_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( "tiffsave_buffer", ap, in, &area ); + va_end( ap ); + + if( !result && + area ) { + if( buf ) { + *buf = area->data; + area->free_fn = NULL; + } + if( len ) + *len = area->length; + + vips_area_unref( area ); + } + + return( result ); +} diff --git a/libvips/foreign/vips2jpeg.c b/libvips/foreign/vips2jpeg.c index ceb36c89..9a6834b3 100644 --- a/libvips/foreign/vips2jpeg.c +++ b/libvips/foreign/vips2jpeg.c @@ -131,6 +131,8 @@ #include #include +#include "pforeign.h" + #ifdef HAVE_EXIF #ifdef UNTAGGED_EXIF #include @@ -146,7 +148,6 @@ #endif /*HAVE_EXIF*/ #include "jpeg.h" -#include "vipsjpeg.h" /* New output message method - send to VIPS. */ diff --git a/libvips/foreign/vips2tiff.c b/libvips/foreign/vips2tiff.c index de7ddcf7..ce4162d4 100644 --- a/libvips/foreign/vips2tiff.c +++ b/libvips/foreign/vips2tiff.c @@ -166,6 +166,8 @@ * - tag alpha as UNASSALPHA since it's not pre-multiplied, thanks Peter * 17/8/16 * - use wchar_t TIFFOpen on Windows + * 14/10/16 + * - add buffer output */ /* @@ -218,8 +220,7 @@ #include #include -#include - +#include "pforeign.h" #include "tiff.h" /* Max number of alpha channels we allow. @@ -234,9 +235,17 @@ typedef struct _Write Write; struct _Layer { Write *write; /* Main write struct */ + /* The filename for this layer, for file output. + */ + char *lname; + + /* The memory area for this layer, for memory output. + */ + void *buf; + size_t len; + int width, height; /* Layer size */ int sub; /* Subsample factor for this layer */ - char *lname; /* Name of this TIFF file */ TIFF *tif; /* TIFF file we write this layer to */ /* The image we build. We only keep a few scanlines of this around in @@ -263,8 +272,16 @@ struct _Layer { */ struct _Write { VipsImage *im; /* Original input image */ + + /* File to write to, or NULL. + */ char *filename; /* Name we write to */ + /* Memory area to output, or NULL. + */ + void **obuf; + size_t *olen; + Layer *layer; /* Top of pyramid */ VipsPel *tbuf; /* TIFF output buffer */ int tls; /* Tile line size */ @@ -287,94 +304,6 @@ struct _Write { int strip; /* Don't write metadata */ }; -/* Open TIFF for output. - */ -TIFF * -vips__tiff_openout( const char *path, gboolean bigtiff ) -{ - TIFF *tif; - const char *mode = bigtiff ? "w8" : "w"; - -#ifdef DEBUG - printf( "vips__tiff_openout( \"%s\", \"%s\" )\n", path, mode ); -#endif /*DEBUG*/ - - /* Need the utf-16 version on Windows. - */ -#ifdef OS_WIN32 -{ - GError *error = NULL; - wchar_t *path16; - - if( !(path16 = (wchar_t *) - g_utf8_to_utf16( path, -1, NULL, NULL, &error )) ) { - vips_g_error( &error ); - return( NULL ); - } - - tif = TIFFOpenW( path16, mode ); - - g_free( path16 ); -} -#else /*!OS_WIN32*/ - tif = TIFFOpen( path, mode ); -#endif /*OS_WIN32*/ - - if( !tif ) { - vips_error( "tiff", - _( "unable to open \"%s\" for output" ), path ); - return( NULL ); - } - - return( tif ); -} - -/* Open TIFF for input. - */ -TIFF * -vips__tiff_openin( const char *path ) -{ - /* No mmap --- no performance advantage with libtiff, and it burns up - * our VM if the tiff file is large. - */ - const char *mode = "rm"; - - TIFF *tif; - -#ifdef DEBUG - printf( "vips__tiff_openin( \"%s\" )\n", path ); -#endif /*DEBUG*/ - - /* Need the utf-16 version on Windows. - */ -#ifdef OS_WIN32 -{ - GError *error = NULL; - wchar_t *path16; - - if( !(path16 = (wchar_t *) - g_utf8_to_utf16( path, -1, NULL, NULL, &error )) ) { - vips_g_error( &error ); - return( NULL ); - } - - tif = TIFFOpenW( path16, mode ); - - g_free( path16 ); -} -#else /*!OS_WIN32*/ - tif = TIFFOpen( path, mode ); -#endif /*OS_WIN32*/ - - if( !tif ) { - vips_error( "tiff", - _( "unable to open \"%s\" for input" ), path ); - return( NULL ); - } - - return( tif ); -} - static Layer * pyramid_new( Write *write, Layer *above, int width, int height ) { @@ -393,6 +322,8 @@ pyramid_new( Write *write, Layer *above, int width, int height ) layer->sub = above->sub * 2; layer->lname = NULL; + layer->buf = NULL; + layer->len = 0; layer->tif = NULL; layer->image = NULL; layer->write_y = 0; @@ -414,15 +345,18 @@ pyramid_new( Write *write, Layer *above, int width, int height ) * We need lname to be freed automatically: it has to stay * alive until after write_gather(). */ - if( !above ) - layer->lname = vips_strdup( VIPS_OBJECT( write->im ), - write->filename ); - else { - char *lname; + if( write->filename ) { + if( !above ) + layer->lname = vips_strdup( VIPS_OBJECT( write->im ), + write->filename ); + else { + char *lname; - lname = vips__temp_name( "%s.tif" ); - layer->lname = vips_strdup( VIPS_OBJECT( write->im ), lname ); - g_free( lname ); + lname = vips__temp_name( "%s.tif" ); + layer->lname = + vips_strdup( VIPS_OBJECT( write->im ), lname ); + g_free( lname ); + } } return( layer ); @@ -805,9 +739,17 @@ pyramid_fill( Write *write ) if( vips_region_buffer( layer->strip, &strip_size ) ) return( -1 ); - if( !(layer->tif = - vips__tiff_openout( layer->lname, write->bigtiff )) || - write_tiff_header( write, layer ) ) + if( layer->lname ) + layer->tif = vips__tiff_openout( + layer->lname, write->bigtiff ); + else { + layer->tif = vips__tiff_openout_buffer( write->im, + write->bigtiff, &layer->buf, &layer->len ); + } + if( !layer->tif ) + return( -1 ); + + if( write_tiff_header( write, layer ) ) return( -1 ); } @@ -829,6 +771,7 @@ write_delete_temps( Write *write ) if( layer->lname ) { #ifndef DEBUG unlink( layer->lname ); + VIPS_FREE( layer->buf ); #else printf( "write_delete_temps: leaving %s\n", layer->lname ); @@ -846,7 +789,6 @@ layer_free( Layer *layer ) VIPS_UNREF( layer->strip ); VIPS_UNREF( layer->copy ); VIPS_UNREF( layer->image ); - VIPS_FREEF( TIFFClose, layer->tif ); } @@ -920,9 +862,9 @@ get_resunit( VipsForeignTiffResunit resunit ) /* Make and init a Write. */ static Write * -write_new( VipsImage *im, const char *filename, +write_new( VipsImage *im, const char *filename, VipsForeignTiffCompression compression, int Q, - VipsForeignTiffPredictor predictor, + VipsForeignTiffPredictor predictor, char *profile, gboolean tile, int tile_width, int tile_height, gboolean pyramid, @@ -939,7 +881,8 @@ write_new( VipsImage *im, const char *filename, if( !(write = VIPS_NEW( im, Write )) ) return( NULL ); write->im = im; - write->filename = vips_strdup( VIPS_OBJECT( im ), filename ); + write->filename = filename ? + vips_strdup( VIPS_OBJECT( im ), filename ) : NULL; write->layer = NULL; write->tbuf = NULL; write->compression = get_compression( compression ); @@ -1687,7 +1630,7 @@ vips__tiff_write( VipsImage *in, const char *filename, /* Make output image. */ - if( !(write = write_new( in, filename, + if( !(write = write_new( in, filename, compression, Q, predictor, profile, tile, tile_width, tile_height, pyramid, squash, miniswhite, resunit, xres, yres, bigtiff, rgbjpeg, @@ -1723,4 +1666,80 @@ vips__tiff_write( VipsImage *in, const char *filename, return( 0 ); } +int +vips__tiff_write_buf( VipsImage *in, + void **obuf, size_t *olen, + VipsForeignTiffCompression compression, int Q, + VipsForeignTiffPredictor predictor, + char *profile, + gboolean tile, int tile_width, int tile_height, + gboolean pyramid, + gboolean squash, + gboolean miniswhite, + VipsForeignTiffResunit resunit, double xres, double yres, + gboolean bigtiff, + gboolean rgbjpeg, + gboolean properties, gboolean strip ) +{ + Write *write; + + vips__tiff_init(); + + if( vips_check_coding_known( "vips2tiff", in ) ) + return( -1 ); + + /* Make output image. + */ + if( !(write = write_new( in, NULL, + compression, Q, predictor, profile, + tile, tile_width, tile_height, pyramid, squash, + miniswhite, resunit, xres, yres, bigtiff, rgbjpeg, + properties, strip )) ) + return( -1 ); + + write->obuf = obuf; + write->olen = olen; + + if( vips_sink_disc( write->im, write_strip, write ) ) { + write_free( write ); + return( -1 ); + } + + if( !TIFFWriteDirectory( write->layer->tif ) ) + return( -1 ); + + if( write->pyramid ) { + /* Free lower pyramid resources ... this will TIFFClose() (but + * not delete) the smaller layers ready for us to read from + * them again. + */ + if( write->layer->below ) + pyramid_free( write->layer->below ); + + /* Append smaller layers to the main file. + */ + if( write_gather( write ) ) { + write_free( write ); + return( -1 ); + } + } + + /* Now close the top layer, and we'll get a pointer we can return + * to our caller. + */ + TIFFClose( write->layer->tif ); + write->layer->tif = NULL; + + *obuf = write->layer->buf; + *olen = write->layer->len; + + /* Now our caller owns it, we must not free it. + */ + write->layer->buf = NULL; + + write_free( write ); + + return( 0 ); +} + #endif /*HAVE_TIFF*/ diff --git a/libvips/foreign/vips2webp.c b/libvips/foreign/vips2webp.c index 05301d85..662aae5b 100644 --- a/libvips/foreign/vips2webp.c +++ b/libvips/foreign/vips2webp.c @@ -52,9 +52,9 @@ #include -#include +#include "pforeign.h" -#include "webp.h" +#include typedef int (*webp_import)( WebPPicture *picture, const uint8_t *rgb, int stride ); diff --git a/libvips/foreign/vipsjpeg.h b/libvips/foreign/vipsjpeg.h deleted file mode 100644 index a9bf86e6..00000000 --- a/libvips/foreign/vipsjpeg.h +++ /dev/null @@ -1,64 +0,0 @@ -/* simple interface to our jpg functions - */ - -/* - - 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_VIPSJPEG_H -#define VIPS_VIPSJPEG_H - -#ifdef __cplusplus -extern "C" { -#endif /*__cplusplus*/ - -extern const char *vips__jpeg_suffs[]; - -int vips__jpeg_write_file( VipsImage *in, - const char *filename, int Q, const char *profile, - gboolean optimize_coding, gboolean progressive, gboolean strip, - gboolean no_subsample, gboolean trellis_quant, - gboolean overshoot_deringing, gboolean optimize_scans, int quant_table ); -int vips__jpeg_write_buffer( VipsImage *in, - void **obuf, size_t *olen, int Q, const char *profile, - gboolean optimize_coding, gboolean progressive, gboolean strip, - gboolean no_subsample, gboolean trellis_quant, - gboolean overshoot_deringing, gboolean optimize_scans, int quant_table ); - -int vips__isjpeg_buffer( const void *buf, size_t len ); -int vips__isjpeg( const char *filename ); -int vips__jpeg_read_file( const char *name, VipsImage *out, - gboolean header_only, int shrink, gboolean fail, gboolean readbehind, - gboolean autorotate ); -int vips__jpeg_read_buffer( const void *buf, size_t len, VipsImage *out, - gboolean header_only, int shrink, int fail, gboolean readbehind, - gboolean autorotate ); - -#ifdef __cplusplus -} -#endif /*__cplusplus*/ - -#endif /*VIPS_VIPSJPEG_H*/ diff --git a/libvips/foreign/vipspng.c b/libvips/foreign/vipspng.c index 0d35ba31..3e85ab32 100644 --- a/libvips/foreign/vipspng.c +++ b/libvips/foreign/vipspng.c @@ -107,9 +107,9 @@ #include #include -#include +#include "pforeign.h" -#include "vipspng.h" +#include #if PNG_LIBPNG_VER < 10003 #error "PNG library too old." @@ -1003,7 +1003,7 @@ write_grow( Write *write, size_t grow_len ) write->alloc = VIPS_MAX( proposed_alloc, new_len ); - /* Our result mujst be freedd with g_free(), so it's OK to use + /* Our result must be freedd with g_free(), so it's OK to use * g_realloc(). */ write->buf = g_realloc( write->buf, write->alloc ); diff --git a/libvips/foreign/vipspng.h b/libvips/foreign/vipspng.h deleted file mode 100644 index 62fe38ba..00000000 --- a/libvips/foreign/vipspng.h +++ /dev/null @@ -1,60 +0,0 @@ -/* common defs for png 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_PNG_H -#define VIPS_PNG_H - -#ifdef __cplusplus -extern "C" { -#endif /*__cplusplus*/ - -int vips__png_header( const char *name, VipsImage *out ); -int vips__png_read( const char *name, VipsImage *out, gboolean readbehind ); -gboolean vips__png_ispng_buffer( const void *buf, size_t len ); -int vips__png_ispng( const char *filename ); -gboolean vips__png_isinterlaced( const char *filename ); -extern const char *vips__png_suffs[]; -int vips__png_read_buffer( const void *buffer, size_t length, - VipsImage *out, gboolean readbehind ); -int vips__png_header_buffer( const void *buffer, size_t length, - VipsImage *out ); - -int vips__png_write( VipsImage *in, const char *filename, - int compress, int interlace, const char *profile, - VipsForeignPngFilter filter, gboolean strip ); -int vips__png_write_buf( VipsImage *in, - void **obuf, size_t *olen, int compression, int interlace, - const char *profile, VipsForeignPngFilter filter, gboolean strip ); - -#ifdef __cplusplus -} -#endif /*__cplusplus*/ - -#endif /*VIPS_PNG_H*/ diff --git a/libvips/foreign/webp.h b/libvips/foreign/webp.h deleted file mode 100644 index 37f126e8..00000000 --- a/libvips/foreign/webp.h +++ /dev/null @@ -1,64 +0,0 @@ -/* common defs for webp read/write - */ - -/* - - Copyright (C) 1991-2005 The National Gallery - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301 USA - - */ - -/* - - These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk - - */ - -#ifndef VIPS_WEBP_H -#define VIPS_WEBP_H - -#ifdef __cplusplus -extern "C" { -#endif /*__cplusplus*/ - -extern const char *vips__webp_suffs[]; - -int vips__iswebp_buffer( const void *buf, size_t len ); -int vips__iswebp( const char *filename ); - -int vips__webp_read_file_header( const char *name, VipsImage *out, int shrink ); -int vips__webp_read_file( const char *name, VipsImage *out, int shrink ); - -int vips__webp_read_buffer_header( const void *buf, size_t len, - VipsImage *out, int shrink ); -int vips__webp_read_buffer( const void *buf, size_t len, - VipsImage *out, int shrink ); - -int vips__webp_write_file( VipsImage *out, const char *filename, - int Q, gboolean lossless, VipsForeignWebpPreset preset, - gboolean smart_subsample, gboolean near_lossless, - int alpha_q ); -int vips__webp_write_buffer( VipsImage *out, void **buf, size_t *len, - int Q, gboolean lossless, VipsForeignWebpPreset preset, - gboolean smart_subsample, gboolean near_lossless, - int alpha_q ); - -#ifdef __cplusplus -} -#endif /*__cplusplus*/ - -#endif /*VIPS_WEBP_H*/ diff --git a/libvips/foreign/webp2vips.c b/libvips/foreign/webp2vips.c index 97fcba87..5ef8cf18 100644 --- a/libvips/foreign/webp2vips.c +++ b/libvips/foreign/webp2vips.c @@ -55,7 +55,7 @@ #include #include -#include "webp.h" +#include "pforeign.h" /* How many bytes do we need to read from the start of the file to be able to * validate the header? diff --git a/libvips/foreign/webpload.c b/libvips/foreign/webpload.c index 65c513bc..a17385e9 100644 --- a/libvips/foreign/webpload.c +++ b/libvips/foreign/webpload.c @@ -47,9 +47,9 @@ #include -#ifdef HAVE_LIBWEBP +#include "pforeign.h" -#include "webp.h" +#ifdef HAVE_LIBWEBP typedef struct _VipsForeignLoadWebp { VipsForeignLoad parent_object; diff --git a/libvips/foreign/webpsave.c b/libvips/foreign/webpsave.c index 1f123f76..8ee91350 100644 --- a/libvips/foreign/webpsave.c +++ b/libvips/foreign/webpsave.c @@ -45,9 +45,9 @@ #include -#ifdef HAVE_LIBWEBP +#include "pforeign.h" -#include "webp.h" +#ifdef HAVE_LIBWEBP typedef struct _VipsForeignSaveWebp { VipsForeignSave parent_object; diff --git a/test/test_foreign.py b/test/test_foreign.py index 61de9ce0..1ca7e1eb 100755 --- a/test/test_foreign.py +++ b/test/test_foreign.py @@ -249,6 +249,7 @@ class TestForeign(unittest.TestCase): self.file_loader("tiffload", self.tiff_file, tiff_valid) self.buffer_loader("tiffload_buffer", self.tiff_file, tiff_valid) + self.save_load_buffer("tiffsave_buffer", "tiffload_buffer", self.colour) self.save_load("%s.tif", self.mono) self.save_load("%s.tif", self.colour) self.save_load("%s.tif", self.cmyk)