From 7deead8911c84a3a45f82a7eef9bf69d6290b32b Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Wed, 13 May 2020 12:45:28 +0100 Subject: [PATCH] add a subifd flag to tiffsave --- libvips/foreign/pforeign.h | 6 ++++-- libvips/foreign/tiffsave.c | 25 ++++++++++++++++++++++--- libvips/foreign/vips2tiff.c | 9 ++++++--- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/libvips/foreign/pforeign.h b/libvips/foreign/pforeign.h index 92b32cc4..c6d3b49d 100644 --- a/libvips/foreign/pforeign.h +++ b/libvips/foreign/pforeign.h @@ -63,7 +63,8 @@ int vips__tiff_write( VipsImage *in, const char *filename, VipsRegionShrink region_shrink, int level, gboolean lossless, - VipsForeignDzDepth depth ); + VipsForeignDzDepth depth, + gboolean subifd ); int vips__tiff_write_buf( VipsImage *in, void **obuf, size_t *olen, @@ -81,7 +82,8 @@ int vips__tiff_write_buf( VipsImage *in, VipsRegionShrink region_shrink, int level, gboolean lossless, - VipsForeignDzDepth depth ); + VipsForeignDzDepth depth, + gboolean subifd ); gboolean vips__istiff_source( VipsSource *source ); gboolean vips__istifftiled_source( VipsSource *source ); diff --git a/libvips/foreign/tiffsave.c b/libvips/foreign/tiffsave.c index 8644a820..b66ce998 100644 --- a/libvips/foreign/tiffsave.c +++ b/libvips/foreign/tiffsave.c @@ -21,6 +21,8 @@ * - xres/yres params were in pixels/cm * 26/1/20 * - add "depth" to set pyr depth + * 12/5/20 + * - add "subifd" to create pyr layers as sub-directories */ /* @@ -98,6 +100,8 @@ typedef struct _VipsForeignSaveTiff { int level; gboolean lossless; VipsForeignDzDepth depth; + gboolean subifd; + } VipsForeignSaveTiff; typedef VipsForeignSaveClass VipsForeignSaveTiffClass; @@ -327,7 +331,7 @@ vips_foreign_save_tiff_class_init( VipsForeignSaveTiffClass *class ) 1, 22, 10 ); VIPS_ARG_BOOL( class, "lossless", 24, - _( "lossless" ), + _( "Lossless" ), _( "Enable WEBP lossless mode" ), VIPS_ARGUMENT_OPTIONAL_INPUT, G_STRUCT_OFFSET( VipsForeignSaveTiff, lossless ), @@ -340,6 +344,13 @@ vips_foreign_save_tiff_class_init( VipsForeignSaveTiffClass *class ) G_STRUCT_OFFSET( VipsForeignSaveTiff, depth ), VIPS_TYPE_FOREIGN_DZ_DEPTH, VIPS_FOREIGN_DZ_DEPTH_ONETILE ); + VIPS_ARG_BOOL( class, "subifd", 24, + _( "Sub-IFD" ), + _( "Save pyr layers as sub-IFDs" ), + VIPS_ARGUMENT_OPTIONAL_INPUT, + G_STRUCT_OFFSET( VipsForeignSaveTiff, subifd ), + FALSE ); + } static void @@ -396,7 +407,8 @@ vips_foreign_save_tiff_file_build( VipsObject *object ) tiff->region_shrink, tiff->level, tiff->lossless, - tiff->depth ) ) + tiff->depth, + tiff->subifd ) ) return( -1 ); return( 0 ); @@ -468,7 +480,8 @@ vips_foreign_save_tiff_buffer_build( VipsObject *object ) tiff->region_shrink, tiff->level, tiff->lossless, - tiff->depth ) ) + tiff->depth, + tiff->subifd ) ) return( -1 ); /* vips__tiff_write_buf() makes a buffer that needs g_free(), not @@ -537,6 +550,7 @@ vips_foreign_save_tiff_buffer_init( VipsForeignSaveTiffBuffer *buffer ) * * @level: %gint, Zstd compression level * * @lossless: %gboolean, WebP losssless mode * * @depth: #VipsForeignDzDepth how deep to make the pyramid + * * @subifd: %gboolean write pyr layers as sub-ifds * * Write a VIPS image to a file as TIFF. * @@ -620,6 +634,10 @@ vips_foreign_save_tiff_buffer_init( VipsForeignSaveTiffBuffer *buffer ) * #VIPS_META_PHOTOSHOP_NAME (if set) is used to set the value of the PHOTOSHOP * tag. * + * By default, pyramid layers are saved as consecutive pages. + * Set @subifd to save pyramid layers as sub-directories of the main image. + * Setting this option can improve compatibility with formats like OME. + * * See also: vips_tiffload(), vips_image_write_to_file(). * * Returns: 0 on success, -1 on error. @@ -665,6 +683,7 @@ vips_tiffsave( VipsImage *in, const char *filename, ... ) * * @level: %gint, Zstd compression level * * @lossless: %gboolean, WebP losssless mode * * @depth: #VipsForeignDzDepth how deep to make the pyramid + * * @subifd: %gboolean write pyr layers as sub-ifds * * As vips_tiffsave(), but save to a memory buffer. * diff --git a/libvips/foreign/vips2tiff.c b/libvips/foreign/vips2tiff.c index c2c46854..24c3ad16 100644 --- a/libvips/foreign/vips2tiff.c +++ b/libvips/foreign/vips2tiff.c @@ -334,6 +334,7 @@ struct _Wtiff { int level; /* zstd compression level */ gboolean lossless; /* webp lossless mode */ VipsForeignDzDepth depth; /* Pyr depth */ + gboolean subifd; /* Write pyr layers into subifds */ /* True if we've detected a toilet-roll image, plus the page height, * which has been checked to be a factor of im->Ysize. page_number @@ -1858,7 +1859,7 @@ wtiff_copy_tiff( Wtiff *wtiff, TIFF *out, TIFF *in ) return( 0 ); } -/* Append all of the lower layers we wrote to the output. +/* Append all of the layers we wrote to the output. */ static int wtiff_gather( Wtiff *wtiff ) @@ -1999,7 +2000,8 @@ vips__tiff_write( VipsImage *input, const char *filename, VipsRegionShrink region_shrink, int level, gboolean lossless, - VipsForeignDzDepth depth ) + VipsForeignDzDepth depth, + gboolean subifd ) { Wtiff *wtiff; @@ -2043,7 +2045,8 @@ vips__tiff_write_buf( VipsImage *input, VipsRegionShrink region_shrink, int level, gboolean lossless, - VipsForeignDzDepth depth ) + VipsForeignDzDepth depth, + gboolean subifd ) { Wtiff *wtiff;