From da1611e070b180ccd73ebe652eba135d35261173 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Fri, 27 Mar 2015 10:07:59 +0000 Subject: [PATCH] add miniswhite tiffsave option --- ChangeLog | 3 + libvips/foreign/foreign.c | 59 +++++++++-------- libvips/foreign/tiff.h | 1 + libvips/foreign/tiffsave.c | 12 +++- libvips/foreign/vips2tiff.c | 125 +++++++++++++++++++++++++++++++++--- 5 files changed, 162 insertions(+), 38 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6bb916bb..1d2d336f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 26/3/15 started 7.42.4 - im_maxpos_avg() avoids NaN +- small tiffsave doc improvements +- better thresholding for tiffsave "squash" mode +- add @miniswhite mode to tiffsave 6/2/15 started 7.42.3 - bump version for back-compat ABI change diff --git a/libvips/foreign/foreign.c b/libvips/foreign/foreign.c index 61ccce47..82f4c81e 100644 --- a/libvips/foreign/foreign.c +++ b/libvips/foreign/foreign.c @@ -1714,7 +1714,7 @@ vips_tiffload( const char *filename, VipsImage **out, ... ) * * Optional arguments: * - * @page: load this page + * @page: %gint, load this page * * Read a TIFF-formatted memory block into a VIPS image. Exactly as * vips_tiffload(), but read from a memory source. @@ -1754,19 +1754,20 @@ vips_tiffload_buffer( void *buf, size_t len, VipsImage **out, ... ) * * Optional arguments: * - * @compression; use this compression scheme - * @Q: quality factor - * @predictor; compress with this prediction - * @profile: attach this ICC profile - * @tile; set %TRUE to write a tiled tiff - * @tile_width; set tile size - * @tile_height; set tile size - * @pyramid; set %TRUE to write an image pyramid - * @squash; squash 8-bit images down to 1 bit - * @resunit; convert resolution to pixels per inch or cm during write - * @xres; horizontal resolution in pixels/mm - * @yres; vertical resolution in pixels/mm - * @bigtiff; write a BigTiff file + * @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 * * Write a VIPS image to a file as TIFF. * @@ -1804,8 +1805,12 @@ vips_tiffload_buffer( void *buf, size_t len, VipsImage **out, ... ) * Set @pyramid to write the image as a set of images, one per page, of * decreasing size. * - * Set @squash to make 8-bit uchar images write as 1-bit TIFFs with zero - * pixels written as 0 and non-zero as 1. + * Set @squash to make 8-bit uchar images write as 1-bit TIFFs. Values >128 + * are written as white, values <=128 as black. Normally vips will write + * MINISBLACK TIFFs where black is a 0 bit, but if you set @miniswhite, it + * will use 0 for a white bit. Many pre-press applications only work with + * images which use this sense. @miniswhite only affects one-bit images, it + * does nothing for greyscale images. * * Use @resunit to override the default resolution unit. * The default @@ -1849,9 +1854,9 @@ vips_tiffsave( VipsImage *in, const char *filename, ... ) * * Optional arguments: * - * @shrink: shrink by this much on load - * @fail: fail on warnings - * @autorotate: use exif Orientation tag to rotate the image during load + * @shrink: %gint, shrink by this much on load + * @fail: %gboolean, fail on warnings + * @autorotate: %gboolean, use exif Orientation tag to rotate the image during load * * Read a JPEG file into a VIPS image. It can read most 8-bit JPEG images, * including CMYK and YCbCr. @@ -1927,8 +1932,8 @@ vips_jpegload( const char *filename, VipsImage **out, ... ) * * Optional arguments: * - * @shrink: shrink by this much on load - * @fail: fail on warnings + * @shrink: %gint, shrink by this much on load + * @fail: %gboolean, fail on warnings * * Read a JPEG-formatted memory block into a VIPS image. Exactly as * vips_jpegload(), but read from a memory buffer. @@ -1968,12 +1973,12 @@ vips_jpegload_buffer( void *buf, size_t len, VipsImage **out, ... ) * * Optional arguments: * - * @Q: quality factor - * @profile: attach this ICC profile - * @optimize_coding: compute optimal Huffman coding tables - * @interlace: write an interlaced (progressive) jpeg - * @strip: remove all metadata from image - * @no-subsample: disable chroma subsampling + * @Q: %gint, quality factor + * @profile: filename of ICC profile to attach + * @optimize_coding: %gboolean, compute optimal Huffman coding tables + * @interlace: %gboolean, write an interlaced (progressive) jpeg + * @strip: %gboolean, remove all metadata from image + * @no-subsample: %gboolean, disable chroma subsampling * * Write a VIPS image to a file as JPEG. * diff --git a/libvips/foreign/tiff.h b/libvips/foreign/tiff.h index 4cb489d1..50644094 100644 --- a/libvips/foreign/tiff.h +++ b/libvips/foreign/tiff.h @@ -46,6 +46,7 @@ int vips__tiff_write( VipsImage *in, const char *filename, gboolean tile, int tile_width, int tile_height, gboolean pyramid, gboolean squash, + gboolean miniswhite, VipsForeignTiffResunit resunit, double xres, double yres, gboolean bigtiff, gboolean rgbjpeg ); diff --git a/libvips/foreign/tiffsave.c b/libvips/foreign/tiffsave.c index e2991833..af53a436 100644 --- a/libvips/foreign/tiffsave.c +++ b/libvips/foreign/tiffsave.c @@ -73,6 +73,7 @@ typedef struct _VipsForeignSaveTiff { int tile_height; gboolean pyramid; gboolean squash; + gboolean miniswhite; VipsForeignTiffResunit resunit; double xres; double yres; @@ -125,6 +126,7 @@ vips_foreign_save_tiff_build( VipsObject *object ) tiff->tile, tiff->tile_width, tiff->tile_height, tiff->pyramid, tiff->squash, + tiff->miniswhite, tiff->resunit, tiff->xres, tiff->yres, tiff->bigtiff, tiff->rgbjpeg ) ) @@ -225,13 +227,19 @@ vips_foreign_save_tiff_class_init( VipsForeignSaveTiffClass *class ) G_STRUCT_OFFSET( VipsForeignSaveTiff, squash ), FALSE ); + VIPS_ARG_BOOL( class, "miniswhite", 14, + _( "Miniswhite" ), + _( "Use 0 for white in 1-bit images" ), + VIPS_ARGUMENT_OPTIONAL_INPUT, + G_STRUCT_OFFSET( VipsForeignSaveTiff, miniswhite ), + FALSE ); + VIPS_ARG_ENUM( class, "resunit", 15, _( "Resolution unit" ), _( "Resolution unit" ), VIPS_ARGUMENT_OPTIONAL_INPUT, G_STRUCT_OFFSET( VipsForeignSaveTiff, resunit ), - VIPS_TYPE_FOREIGN_TIFF_RESUNIT, - VIPS_FOREIGN_TIFF_RESUNIT_CM ); + VIPS_TYPE_FOREIGN_TIFF_RESUNIT, VIPS_FOREIGN_TIFF_RESUNIT_CM ); VIPS_ARG_DOUBLE( class, "xres", 16, _( "Xres" ), diff --git a/libvips/foreign/vips2tiff.c b/libvips/foreign/vips2tiff.c index 986cfc60..30a8ac03 100644 --- a/libvips/foreign/vips2tiff.c +++ b/libvips/foreign/vips2tiff.c @@ -146,6 +146,9 @@ * - zero out edge tile buffers before jpeg write, thanks iwbh15 * 19/1/15 * - disable chroma subsample if Q >= 90 + * 27/3/15 + * - squash >128 rather than >0, nicer results for shrink + * - add miniswhite option */ /* @@ -266,6 +269,7 @@ typedef struct tiff_write { int tilew, tileh; /* Tile size */ int pyramid; /* Write pyramid */ int onebit; /* Write as 1-bit TIFF */ + int miniswhite; /* Write as 0 == white */ int resunit; /* Resolution unit (inches or cm) */ double xres; /* Resolution in X */ double yres; /* Resolution in Y */ @@ -337,16 +341,23 @@ LabQ2LabC( VipsPel *q, VipsPel *p, int n ) /* Pack 8 bit VIPS to 1 bit TIFF. */ static void -eightbit2onebit( VipsPel *q, VipsPel *p, int n ) +eightbit2onebit( TiffWrite *tw, VipsPel *q, VipsPel *p, int n ) { int x; VipsPel bits; + /* Invert in miniswhite mode. + */ + int white = tw->miniswhite ? 0 : 1; + int black = white ^ 1; + bits = 0; for( x = 0; x < n; x++ ) { bits <<= 1; - if( p[x] ) - bits |= 1; + if( p[x] > 128 ) + bits |= white; + else + bits |= black; if( (x & 0x7) == 0x7 ) { *q++ = bits; @@ -360,6 +371,75 @@ eightbit2onebit( VipsPel *q, VipsPel *p, int n ) *q++ = bits << (8 - (x & 0x7)); } +/* Swap the sense of the first channel, if necessary. + */ +#define GREY_LOOP( TYPE, MAX ) { \ + TYPE *p1; \ + TYPE *q1; \ + \ + p1 = (TYPE *) p; \ + q1 = (TYPE *) q; \ + for( x = 0; x < n; x++ ) { \ + if( invert ) \ + q1[0] = MAX - p1[0]; \ + else \ + q1[0] = p1[0]; \ + \ + for( i = 1; i < im->Bands; i++ ) \ + q1[i] = p1[i]; \ + \ + q1 += im->Bands; \ + p1 += im->Bands; \ + } \ +} + +/* If we're writing a 1 or 2 band image as a greyscale and MINISWHITE, we need + * to swap the sense of the first band. See tiff2vips.c, greyscale_line() for + * the opposite conversion. + */ +static void +invert_band0( TiffWrite *tw, VipsPel *q, VipsPel *p, int n ) +{ + VipsImage *im = tw->im; + gboolean invert = tw->miniswhite; + + int x, i; + + switch( im->BandFmt ) { + case VIPS_FORMAT_UCHAR: + case VIPS_FORMAT_CHAR: + GREY_LOOP( guchar, UCHAR_MAX ); + break; + + case VIPS_FORMAT_SHORT: + GREY_LOOP( gshort, SHRT_MAX ); + break; + + case VIPS_FORMAT_USHORT: + GREY_LOOP( gushort, USHRT_MAX ); + break; + + case VIPS_FORMAT_INT: + GREY_LOOP( gint, INT_MAX ); + break; + + case VIPS_FORMAT_UINT: + GREY_LOOP( guint, UINT_MAX ); + break; + + case VIPS_FORMAT_FLOAT: + GREY_LOOP( float, 1.0 ); + break; + + case VIPS_FORMAT_DOUBLE: + GREY_LOOP( double, 1.0 ); + break; + + default: + g_assert( 0 ); + } +} + /* Convert VIPS LABS to TIFF 16 bit LAB. */ static void @@ -406,7 +486,10 @@ pack2tiff( TiffWrite *tw, VipsRegion *in, VipsPel *q, VipsRect *area ) if( in->im->Coding == VIPS_CODING_LABQ ) LabQ2LabC( q, p, area->width ); else if( tw->onebit ) - eightbit2onebit( q, p, area->width ); + eightbit2onebit( tw, q, p, area->width ); + else if( (in->im->Bands == 1 || in->im->Bands == 2) && + tw->miniswhite ) + invert_band0( tw, q, p, area->width ); else if( in->im->BandFmt == VIPS_FORMAT_SHORT && in->im->Type == VIPS_INTERPRETATION_LABS ) LabS2Lab16( q, p, area->width ); @@ -541,8 +624,10 @@ write_tiff_header( TiffWrite *tw, TIFF *tif, int width, int height ) else if( tw->onebit ) { TIFFSetField( tif, TIFFTAG_SAMPLESPERPIXEL, 1 ); TIFFSetField( tif, TIFFTAG_BITSPERSAMPLE, 1 ); - TIFFSetField( tif, - TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK ); + TIFFSetField( tif, TIFFTAG_PHOTOMETRIC, + tw->miniswhite ? + PHOTOMETRIC_MINISWHITE : + PHOTOMETRIC_MINISBLACK ); } else { int photometric; @@ -554,7 +639,9 @@ write_tiff_header( TiffWrite *tw, TIFF *tif, int width, int height ) switch( tw->im->Bands ) { case 1: case 2: - photometric = PHOTOMETRIC_MINISBLACK; + photometric = tw->miniswhite ? + PHOTOMETRIC_MINISWHITE : + PHOTOMETRIC_MINISBLACK; if( tw->im->Bands == 2 ) { v[0] = EXTRASAMPLE_ASSOCALPHA; TIFFSetField( tif, TIFFTAG_EXTRASAMPLES, 1, v ); @@ -1195,7 +1282,12 @@ write_tif_block( VipsRegion *region, VipsRect *area, void *a ) p = tw->tbuf; } else if( tw->onebit ) { - eightbit2onebit( tw->tbuf, p, im->Xsize ); + eightbit2onebit( tw, tw->tbuf, p, im->Xsize ); + p = tw->tbuf; + } + else if( (im->Bands == 1 || im->Bands == 2) && + tw->miniswhite ) { + invert_band0( tw, tw->tbuf, p, im->Xsize ); p = tw->tbuf; } @@ -1340,6 +1432,7 @@ make_tiff_write( VipsImage *im, const char *filename, gboolean tile, int tile_width, int tile_height, gboolean pyramid, gboolean squash, + gboolean miniswhite, VipsForeignTiffResunit resunit, double xres, double yres, gboolean bigtiff, gboolean rgbjpeg ) @@ -1362,6 +1455,7 @@ make_tiff_write( VipsImage *im, const char *filename, tw->tileh = tile_height; tw->pyramid = pyramid; tw->onebit = squash; + tw->miniswhite = miniswhite; tw->icc_profile = profile; tw->bigtiff = bigtiff; tw->rgbjpeg = rgbjpeg; @@ -1417,6 +1511,18 @@ make_tiff_write( VipsImage *im, const char *filename, tw->compression = COMPRESSION_NONE; } + /* We can only MINISWHITE non-complex images of 1 or 2 bands. + */ + if( tw->miniswhite && + (im->Coding != VIPS_CODING_NONE || + vips_band_format_iscomplex( im->BandFmt ) || + im->Bands > 2) ) { + vips_warn( "vips2tiff", + "%s", _( "can only save non-complex greyscale images " + "as miniswhite -- disabling miniswhite" ) ); + tw->miniswhite = FALSE; + } + /* Sizeof a line of bytes in the TIFF tile. */ if( im->Coding == VIPS_CODING_LABQ ) @@ -1603,6 +1709,7 @@ vips__tiff_write( VipsImage *in, const char *filename, gboolean tile, int tile_width, int tile_height, gboolean pyramid, gboolean squash, + gboolean miniswhite, VipsForeignTiffResunit resunit, double xres, double yres, gboolean bigtiff, gboolean rgbjpeg ) @@ -1627,7 +1734,7 @@ vips__tiff_write( VipsImage *in, const char *filename, if( !(tw = make_tiff_write( in, filename, compression, Q, predictor, profile, tile, tile_width, tile_height, pyramid, squash, - resunit, xres, yres, bigtiff, rgbjpeg )) ) + miniswhite, resunit, xres, yres, bigtiff, rgbjpeg )) ) return( -1 ); if( tw->pyramid ) { if( !(tw->bname = vips__temp_name( "%s.tif" )) ||