diff --git a/ChangeLog b/ChangeLog index ae2fb895..0a949df2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -15,6 +15,12 @@ - added vips_info_set(), vips_progress_set(), vips_profile_set() ... bindings can now support all the vips command-line options if they wish +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 - added vips_image_memory(), an alias for vips_image_new_memory() diff --git a/libvips/deprecated/im_maxpos_avg.c b/libvips/deprecated/im_maxpos_avg.c index 36ae6580..9d062b12 100644 --- a/libvips/deprecated/im_maxpos_avg.c +++ b/libvips/deprecated/im_maxpos_avg.c @@ -17,6 +17,8 @@ * - now handles many bands, complex, faster * 27/7/14 * - fix a race ... did not merge states if max was equal + * 26/3/15 + * - avoid NaN */ /* @@ -62,32 +64,15 @@ #include #include -/* Get the value of pixel (0, 0). Use this to init the min/max value for - * im_max()/im_stats()/etc. - */ -int -im__value( IMAGE *im, double *value ) -{ - IMAGE *t; - - if( !(t = im_open( "im__value", "p" )) ) - return( -1 ); - if( im_extract_areabands( im, t, 0, 0, 1, 1, 0, 1 ) || - im_avg( t, value ) ) { - im_close( t ); - return( -1 ); - } - im_close( t ); - - return( 0 ); -} - /* A position and maximum. */ typedef struct _Maxposavg { int xpos; int ypos; double max; + + /* occurences == 0 means we found no points, or we are uninitialised. + */ int occurences; } Maxposavg; @@ -116,7 +101,9 @@ maxposavg_stop( void *seq, void *a, void *b ) /* Merge. */ - if( maxposavg->max > global_maxposavg->max ) + if( maxposavg->occurences == 0 ) { + } + else if( maxposavg->max > global_maxposavg->max ) *global_maxposavg = *maxposavg; else if( maxposavg->max == global_maxposavg->max ) { global_maxposavg->xpos += maxposavg->xpos; @@ -129,7 +116,9 @@ maxposavg_stop( void *seq, void *a, void *b ) return( 0 ); } -#define LOOP( TYPE ) { \ +/* int loop. + */ +#define ILOOP( TYPE ) { \ TYPE *p = (TYPE *) in; \ TYPE m; \ \ @@ -138,22 +127,53 @@ maxposavg_stop( void *seq, void *a, void *b ) for( x = 0; x < sz; x++ ) { \ TYPE v = p[x]; \ \ - if( v == m ) { \ - xpos += r->left + x / reg->im->Bands; \ - ypos += r->top + y; \ - occurences += 1; \ - } \ - else if( v > m ) { \ + if( occurences == 0 || v > m ) { \ m = v; \ xpos = r->left + x / reg->im->Bands; \ ypos = r->top + y; \ occurences = 1; \ } \ + else if( v == m ) { \ + xpos += r->left + x / reg->im->Bands; \ + ypos += r->top + y; \ + occurences += 1; \ + } \ } \ \ max = m; \ } +/* float/double loop ... avoid NaN. + */ +#define FLOOP( TYPE ) { \ + TYPE *p = (TYPE *) in; \ + TYPE m; \ + \ + m = max; \ + \ + for( x = 0; x < sz; x++ ) { \ + TYPE v = p[x]; \ + \ + if( isnan( v ) ) { \ + } \ + else if( occurences == 0 || v > m ) { \ + m = v; \ + xpos = r->left + x / reg->im->Bands; \ + ypos = r->top + y; \ + occurences = 1; \ + } \ + else if( v == m ) { \ + xpos += r->left + x / reg->im->Bands; \ + ypos += r->top + y; \ + occurences += 1; \ + } \ + } \ + \ + max = m; \ +} + +/* complex/dpcomplex loop ... avoid NaN. + */ #define CLOOP( TYPE ) { \ TYPE *p = (TYPE *) in; \ \ @@ -165,17 +185,19 @@ maxposavg_stop( void *seq, void *a, void *b ) p += 2; \ mod = re * re + im * im; \ \ - if( mod == max ) { \ - xpos += r->left + x / reg->im->Bands; \ - ypos += r->top + y; \ - occurences += 1; \ + if( isnan( mod ) ) { \ } \ - else if( mod > max ) { \ + else if( occurences == 0 || mod > max ) { \ max = mod; \ xpos = r->left + x / reg->im->Bands; \ ypos = r->top + y; \ occurences = 1; \ } \ + else if( mod == max ) { \ + xpos += r->left + x / reg->im->Bands; \ + ypos += r->top + y; \ + occurences += 1; \ + } \ } \ } @@ -201,14 +223,14 @@ maxposavg_scan( REGION *reg, void *seq, void *a, void *b, gboolean *stop ) VipsPel *in = VIPS_REGION_ADDR( reg, r->left, r->top + y ); switch( reg->im->BandFmt ) { - case IM_BANDFMT_UCHAR: LOOP( unsigned char ); break; - case IM_BANDFMT_CHAR: LOOP( signed char ); break; - case IM_BANDFMT_USHORT: LOOP( unsigned short ); break; - case IM_BANDFMT_SHORT: LOOP( signed short ); break; - case IM_BANDFMT_UINT: LOOP( unsigned int ); break; - case IM_BANDFMT_INT: LOOP( signed int ); break; - case IM_BANDFMT_FLOAT: LOOP( float ); break; - case IM_BANDFMT_DOUBLE: LOOP( double ); break; + case IM_BANDFMT_UCHAR: ILOOP( unsigned char ); break; + case IM_BANDFMT_CHAR: ILOOP( signed char ); break; + case IM_BANDFMT_USHORT: ILOOP( unsigned short ); break; + case IM_BANDFMT_SHORT: ILOOP( signed short ); break; + case IM_BANDFMT_UINT: ILOOP( unsigned int ); break; + case IM_BANDFMT_INT: ILOOP( signed int ); break; + case IM_BANDFMT_FLOAT: FLOOP( float ); break; + case IM_BANDFMT_DOUBLE: FLOOP( double ); break; case IM_BANDFMT_COMPLEX: CLOOP( float ); break; case IM_BANDFMT_DPCOMPLEX: CLOOP( double ); break; @@ -251,34 +273,32 @@ im_maxpos_avg( IMAGE *in, double *xpos, double *ypos, double *out ) if( !(global_maxposavg = IM_NEW( in, Maxposavg )) ) return( -1 ); - if( im__value( in, &global_maxposavg->max ) ) - return( -1 ); - global_maxposavg->xpos = 0; - global_maxposavg->ypos = 0; - global_maxposavg->occurences = 1; - - /* We use square mod for scanning, for speed. - */ - if( vips_band_format_iscomplex( in->BandFmt ) ) - global_maxposavg->max *= global_maxposavg->max; + global_maxposavg->occurences = 0; if( vips_sink( in, maxposavg_start, maxposavg_scan, maxposavg_stop, in, global_maxposavg ) ) return( -1 ); - /* Back to modulus. - */ - if( vips_band_format_iscomplex( in->BandFmt ) ) - global_maxposavg->max = sqrt( global_maxposavg->max ); + if( global_maxposavg->occurences == 0 ) { + *xpos = nan(""); + *ypos = nan(""); + *out = nan(""); + } + else { + /* Back to modulus. + */ + if( vips_band_format_iscomplex( in->BandFmt ) ) + global_maxposavg->max = sqrt( global_maxposavg->max ); - if( xpos ) - *xpos = (double) global_maxposavg->xpos / - global_maxposavg->occurences; - if( ypos ) - *ypos = (double) global_maxposavg->ypos / - global_maxposavg->occurences; - if( out ) - *out = global_maxposavg->max; + if( xpos ) + *xpos = (double) global_maxposavg->xpos / + global_maxposavg->occurences; + if( ypos ) + *ypos = (double) global_maxposavg->ypos / + global_maxposavg->occurences; + if( out ) + *out = global_maxposavg->max; + } return( 0 ); } diff --git a/libvips/foreign/foreign.c b/libvips/foreign/foreign.c index 9e777dba..be2dff69 100644 --- a/libvips/foreign/foreign.c +++ b/libvips/foreign/foreign.c @@ -1785,7 +1785,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. @@ -1825,19 +1825,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. * @@ -1875,8 +1876,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 @@ -1920,9 +1925,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. @@ -1998,8 +2003,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. @@ -2039,12 +2044,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 4ad6e2da..3a48491a 100644 --- a/libvips/foreign/vips2tiff.c +++ b/libvips/foreign/vips2tiff.c @@ -150,6 +150,9 @@ * - append later layers, don't copy the base image * - use the nice dzsave pyramid code, much faster and simpler * - we now allow strip pyramids + * 27/3/15 + * - squash >128 rather than >0, nicer results for shrink + * - add miniswhite option */ /* @@ -255,6 +258,7 @@ struct _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 */ @@ -486,8 +490,10 @@ write_tiff_header( Write *write, Layer *layer ) else if( write->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; @@ -499,7 +505,9 @@ write_tiff_header( Write *write, Layer *layer ) switch( write->im->Bands ) { case 1: case 2: - photometric = PHOTOMETRIC_MINISBLACK; + photometric = tw->miniswhite ? + PHOTOMETRIC_MINISWHITE : + PHOTOMETRIC_MINISBLACK; if( write->im->Bands == 2 ) { v[0] = EXTRASAMPLE_ASSOCALPHA; TIFFSetField( tif, TIFFTAG_EXTRASAMPLES, 1, v ); @@ -752,6 +760,7 @@ write_new( 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 ) @@ -772,6 +781,7 @@ write_new( VipsImage *im, const char *filename, write->tileh = tile_height; write->pyramid = pyramid; write->onebit = squash; + write->miniswhite = miniswhite; write->icc_profile = profile; write->bigtiff = bigtiff; write->rgbjpeg = rgbjpeg; @@ -817,6 +827,18 @@ write_new( VipsImage *im, const char *filename, "%s", _( "can't have 1-bit JPEG -- disabling JPEG" ) ); write->compression = COMPRESSION_NONE; } + + /* We can only MINISWHITE non-complex images of 1 or 2 bands. + */ + if( write->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" ) ); + write->miniswhite = FALSE; + } /* Sizeof a line of bytes in the TIFF tile. */ @@ -874,16 +896,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; @@ -897,6 +926,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 @@ -944,7 +1042,10 @@ pack2tiff( Write *write, Layer *layer, if( write->im->Coding == VIPS_CODING_LABQ ) LabQ2LabC( q, p, area->width ); else if( write->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( write->im->BandFmt == VIPS_FORMAT_SHORT && write->im->Type == VIPS_INTERPRETATION_LABS ) LabS2Lab16( q, p, area->width ); @@ -1034,7 +1135,12 @@ layer_write_strip( Write *write, Layer *layer, VipsRegion *strip ) p = write->tbuf; } else if( write->onebit ) { - eightbit2onebit( write->tbuf, p, im->Xsize ); + eightbit2onebit( tw, tw->tbuf, p, im->Xsize ); + p = write->tbuf; + } + else if( (im->Bands == 1 || im->Bands == 2) && + tw->miniswhite ) { + invert_band0( tw, tw->tbuf, p, im->Xsize ); p = write->tbuf; } @@ -1372,6 +1478,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 ) @@ -1392,7 +1499,7 @@ vips__tiff_write( VipsImage *in, const char *filename, if( !(write = write_new( 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( vips_sink_disc( write->im, write_strip, write ) ) {