From 6c3b8ad96d7f3d3dc3a1e9af3091d82ea479c1f4 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Sun, 11 Dec 2011 11:58:50 +0000 Subject: [PATCH] add im_argb2rgba() for openslide im_openslide2vips() now does not repack, we have a separate operator for that --- TODO | 51 ++--------- libvips/colour/Makefile.am | 1 + libvips/colour/colour_dispatch.c | 20 ++++ libvips/colour/im_argb2rgba.c | 110 ++++++++++++++++++++++ libvips/format/openslide.c | 153 ++++++++++--------------------- libvips/include/vips/colour.h | 1 + 6 files changed, 188 insertions(+), 148 deletions(-) create mode 100644 libvips/colour/im_argb2rgba.c diff --git a/TODO b/TODO index a81c8814..f5960e9c 100644 --- a/TODO +++ b/TODO @@ -1,53 +1,16 @@ -- why does openslide make argb? can we ask for rgb instead? +- add ".ndpi" to openslide suffs? though we don't really use suffs for loaders - alternatively, have a new coding type and a separate unpack operation +- check lazy load hint bug in format-hacking branch - can we get the underlying tile size? we could use that to size the vips tile - cache +- everywhere must set dhint in header load - can we size the openslide tile cache? needs to hold enough tiles for a - complete line in the image +- openslide sets a g_log() handler, argh, must just set temp +- make an argb coding type, add to nip2 and known coding +- "background-rgb" should be a macro - - with dhint fixed, no tile cache (vips is asking for 128x128 tiles, so - everything gets decoded 4 times) - - $ time vips im_copy CMU-2.svs x.v - real 15m27.833s - user 5m4.907s - sys 1m31.110s - - dhint fix plus a tile cache - - $ time vips im_copy CMU-2.svs x.v - real 9m4.254s - user 3m36.842s - sys 0m46.271s - - read directly to vips tile cache, no unpacking - - $ time vips im_copy CMU-2.svs x.v - real 7m23.040s - user 2m44.262s - sys 0m47.791s - - copy from a 256x256 tiled tiff - - $ time vips im_copy cmu-2.tif x.v - real 6m4.798s - user 0m49.567s - sys 0m17.973s - - copy from a 128x128 tiled tiff - - $ vips im_copy x.v cmu-2.tif:jpeg,tile - $ time vips im_copy cmu-2.tif x.v - real 3m32.521s - user 0m55.295s - sys 0m11.693s - +- nip2 should use zooming support, if possible diff --git a/libvips/colour/Makefile.am b/libvips/colour/Makefile.am index 9fe708c6..465fa53b 100644 --- a/libvips/colour/Makefile.am +++ b/libvips/colour/Makefile.am @@ -6,6 +6,7 @@ libcolour_la_SOURCES = \ colour_dispatch.c \ derived.c \ im_icc_transform.c \ + im_argb2rgba.c \ im_LCh2Lab.c \ im_LCh2UCS.c \ im_Lab2LCh.c \ diff --git a/libvips/colour/colour_dispatch.c b/libvips/colour/colour_dispatch.c index 4534bd2d..eb4663ae 100644 --- a/libvips/colour/colour_dispatch.c +++ b/libvips/colour/colour_dispatch.c @@ -640,6 +640,25 @@ static im_function rad2float_desc = { one_in_one_out /* Arg list */ }; +/* Call im_argb2rgba() via arg vector. + */ +static int +argb2rgba_vec( im_object *argv ) +{ + return( im_argb2rgba( argv[0], argv[1] ) ); +} + +/* Description of im_argb2rgba. + */ +static im_function argb2rgba_desc = { + "im_argb2rgba", /* Name */ + "convert pre-multipled argb to png-style rgba", /* Description */ + IM_FN_PIO, /* Flags */ + argb2rgba_vec, /* Dispatch function */ + IM_NUMBER( one_in_one_out ), /* Size of arg list */ + one_in_one_out /* Arg list */ +}; + /* Call im_float2rad() via arg vector. */ static int @@ -1185,6 +1204,7 @@ static im_function *colour_list[] = { &disp2Lab_desc, &disp2XYZ_desc, &float2rad_desc, + &argb2rgba_desc, &icc_ac2rc_desc, &icc_export_depth_desc, &icc_import_desc, diff --git a/libvips/colour/im_argb2rgba.c b/libvips/colour/im_argb2rgba.c new file mode 100644 index 00000000..13d19b2e --- /dev/null +++ b/libvips/colour/im_argb2rgba.c @@ -0,0 +1,110 @@ +/* Convert pre-multipled argb to rgba + * + * 11/12/11 + * - from im_rad2float.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + */ + +/* + + These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk + + */ + +#ifdef HAVE_CONFIG_H +#include +#endif /*HAVE_CONFIG_H*/ +#include + +#include +#include + +#include + +static void +argb2rgba( guint32 *in, PEL *out, int n, void *_bg ) +{ + guint32 bg = GPOINTER_TO_UINT( _bg ); + + int i; + + for( i = 0; i < n; i++ ) { + guint32 x = in[i]; + guint8 a = x >> 24; + + /* Convert from ARGB to RGBA and undo premultiplication. + */ + if( a != 0 ) { + out[0] = 255 * ((x >> 16) & 255) / a; + out[1] = 255 * ((x >> 8) & 255) / a; + out[2] = 255 * (x & 255) / a; + } + else { + /* Use background color. + */ + out[0] = (bg >> 16) & 255; + out[1] = (bg >> 8) & 255; + out[2] = bg & 255; + } + out[3] = a; + + out += 4; + } +} + +/** + * im_argb2rgba: + * @in: input image + * @out: output image + * + * Convert Cairo-style pre-multiplied argb to png-style rgba. Background + * pixels are painted with the metadata item "background-rgb". + * + * See also: im_openslide2vips(). + * + * Returns: 0 on success, -1 on error. + */ +int +im_argb2rgba( IMAGE *in, IMAGE *out ) +{ + guint32 bg; + + /* check for RAD coding + if( in->Coding != IM_CODING_RAD ) { + im_error( "im_rad2float", "%s", _( "not a RAD image" ) ); + return( -1 ); + } + */ + + if( im_cp_desc( out, in ) ) + return( -1 ); + out->Coding = IM_CODING_NONE; + + if( im_meta_get_int( in, "background-rgb", (int *) &bg ) ) + bg = 0xffffff; + + if( im_wrapone( in, out, + (im_wrapone_fn) argb2rgba, GUINT_TO_POINTER( bg ), NULL ) ) + return( -1 ); + + return( 0 ); +} diff --git a/libvips/format/openslide.c b/libvips/format/openslide.c index a3f530f1..2e6c66a1 100644 --- a/libvips/format/openslide.c +++ b/libvips/format/openslide.c @@ -21,6 +21,8 @@ * - add more exposition to documentation * 9/12/11 * - unpack to a tile cache + * 11/12/11 + * - move argb->rgba into conversion */ /* @@ -64,9 +66,14 @@ #include +/* We run our own tile cache. The OpenSlide one can't always keep enough for a + * complete lines of pixels. + */ +#define TILE_WIDTH (256) +#define TILE_HEIGHT (256) + typedef struct { openslide_t *osr; - uint32_t background; const char *associated; /* Only valid if associated == NULL. @@ -103,9 +110,9 @@ readslide_new( const char *filename, VipsImage *out ) ReadSlide *rslide; char name[FILENAME_MAX]; char mode[FILENAME_MAX]; - const char *background; char *endp; int64_t w, h; + const char *background; const char * const *properties; char *associated; @@ -122,13 +129,6 @@ readslide_new( const char *filename, VipsImage *out ) return( NULL ); } - background = openslide_get_property_value( rslide->osr, - OPENSLIDE_PROPERTY_NAME_BACKGROUND_COLOR ); - if( background != NULL ) - rslide->background = strtoul( background, NULL, 16 ); - else - rslide->background = 0xffffff; - /* Parse optional mode. */ rslide->layer = strtol( mode, &endp, 10 ); @@ -155,6 +155,7 @@ readslide_new( const char *filename, VipsImage *out ) rslide->associated, &w, &h ); vips_image_set_string( out, "slide-associated-image", rslide->associated ); + vips_demand_hint( out, VIPS_DEMAND_STYLE_THINSTRIP, NULL ); } else { openslide_get_layer_dimensions( rslide->osr, rslide->layer, @@ -162,13 +163,26 @@ readslide_new( const char *filename, VipsImage *out ) rslide->downsample = openslide_get_layer_downsample( rslide->osr, rslide->layer ); vips_image_set_int( out, "slide-layer", rslide->layer ); + vips_demand_hint( out, VIPS_DEMAND_STYLE_SMALLTILE, NULL ); } + + /* This tag is used by argb2rgba() to paint fully-transparent pixels. + */ + background = openslide_get_property_value( rslide->osr, + OPENSLIDE_PROPERTY_NAME_BACKGROUND_COLOR ); + if( background != NULL ) + im_meta_set_int( out, + "background-rgb", strtoul( background, NULL, 16 ) ); + else + im_meta_set_int( out, "background-rgb", 0xffffff ); + if( w < 0 || h < 0 || rslide->downsample < 0 ) { vips_error( "im_openslide2vips", _( "getting dimensions: %s" ), openslide_get_error( rslide->osr ) ); return( NULL ); } - if( w > INT_MAX || h > INT_MAX ) { + if( w > INT_MAX || + h > INT_MAX ) { vips_error( "im_openslide2vips", "%s", _( "image dimensions overflow int" ) ); return( NULL ); @@ -191,66 +205,21 @@ readslide_new( const char *filename, VipsImage *out ) return( rslide ); } -/* The maximum size of the tiles we read from OpenSlide. It can fail with - * very large tile shapes (eg. 78000 x 1). Also, limiting the tile size means - * we can have a per-thread buffer for unpacking. - */ -#define TILE_WIDTH (256) -#define TILE_HEIGHT (256) - -/* Allocate a per-thread tile buffer. - */ -static void * -seq_start( VipsImage *out, void *a, void *b ) -{ - return( (void *) VIPS_ARRAY( NULL, - TILE_WIDTH * TILE_HEIGHT, uint32_t ) ); -} - -static void -copy_line( ReadSlide *rslide, uint32_t *in, int count, PEL *out ) -{ - int i; - - for( i = 0; i < count; i++ ) { - uint32_t x = in[i]; - uint8_t a = x >> 24; - - /* Convert from ARGB to RGBA and undo premultiplication. - */ - if( a != 0 ) { - out[0] = 255 * ((x >> 16) & 255) / a; - out[1] = 255 * ((x >> 8) & 255) / a; - out[2] = 255 * (x & 255) / a; - } - else { - /* Use background color. - */ - out[0] = (rslide->background >> 16) & 255; - out[1] = (rslide->background >> 8) & 255; - out[2] = rslide->background & 255; - } - out[3] = a; - - out += 4; - } -} - static int fill_region( VipsRegion *out, void *seq, void *_rslide, void *unused, gboolean *stop ) { ReadSlide *rslide = _rslide; - uint32_t *buf = (uint32_t *) seq; VipsRect *r = &out->valid; const char *error; - int x, y, z; + int x, y; VIPS_DEBUG_MSG( "fill_region: %dx%d @ %dx%d\n", r->width, r->height, r->left, r->top ); - /* Loop over the region to be filled calling openslide_read_region(). + /* Fill in tile-sized chunks. Some versions of OpenSlide can fail for + * very large dimensions. */ for( y = 0; y < r->height; y += TILE_HEIGHT ) for( x = 0; x < r->width; x += TILE_WIDTH ) { @@ -258,71 +227,46 @@ fill_region( VipsRegion *out, void *seq, void *_rslide, void *unused, int h = VIPS_MIN( TILE_HEIGHT, r->height - y ); openslide_read_region( rslide->osr, - /* or read directly to the output with this: - VIPS_REGION_ADDR( out, - r->left + x, - r->top + y ), - */ - buf, + (uint32_t *) VIPS_REGION_ADDR( out, + r->left + x, r->top + y ), (r->left + x) * rslide->downsample, (r->top + y) * rslide->downsample, rslide->layer, w, h ); - - for( z = 0; z < h; z++ ) - copy_line( rslide, - buf + z * w, - w, - VIPS_REGION_ADDR( out, - r->left + x, - r->top + y + z ) ); } error = openslide_get_error( rslide->osr ); if( error ) { - vips_error( "im_openslide2vips", _( "reading region: %s" ), - error ); + vips_error( "im_openslide2vips", + _( "reading region: %s" ), error ); + return( -1 ); } return( 0 ); } -static int -seq_stop( void *seq, void *a, void *b ) -{ - vips_free( seq ); - - return( 0 ); -} - static int fill_associated( VipsImage *out, ReadSlide *rslide ) { uint32_t *buf; - PEL *line; int64_t w, h; int y; const char *error; openslide_get_associated_image_dimensions( rslide->osr, rslide->associated, &w, &h ); - if( w == -1 || h == -1 ) { - vips_error( "im_openslide2vips", _( "getting dimensions: %s" ), + if( w == -1 || + h == -1 ) { + vips_error( "im_openslide2vips", + _( "getting dimensions: %s" ), openslide_get_error( rslide->osr ) ); + return( -1 ); } buf = VIPS_ARRAY( out, w * h, uint32_t ); - line = VIPS_ARRAY( out, VIPS_IMAGE_SIZEOF_LINE( out ), PEL ); - openslide_read_associated_image( rslide->osr, rslide->associated, - buf ); - for( y = 0; y < h; y++ ) { - copy_line( rslide, buf + y * w, w, line ); - if( vips_image_write_line( out, y, line ) ) - return( -1 ); - } - + openslide_read_associated_image( rslide->osr, rslide->associated, buf ); error = openslide_get_error( rslide->osr ); if( error ) { vips_error( "im_openslide2vips", @@ -330,6 +274,12 @@ fill_associated( VipsImage *out, ReadSlide *rslide ) return( -1 ); } + if( vips_image_wio_output( out ) ) + return( -1 ); + for( y = 0; y < h; y++ ) + if( vips_image_write_line( out, y, (PEL *) (buf + y * w) ) ) + return( -1 ); + return( 0 ); } @@ -393,19 +343,13 @@ im_openslide2vips( const char *filename, VipsImage *out ) if( rslide->associated ) { VIPS_DEBUG_MSG( "fill_associated:\n" ); - if( vips_image_wio_output( raw ) ) - return( -1 ); - if( fill_associated( raw, rslide ) ) return( -1 ); } else { - if( vips_image_pio_output( raw ) ) - return( -1 ); - vips_demand_hint( raw, VIPS_DEMAND_STYLE_SMALLTILE, NULL ); - - if( vips_image_generate( raw, - seq_start, fill_region, seq_stop, rslide, NULL ) ) + if( vips_image_pio_output( raw ) || + vips_image_generate( raw, + NULL, fill_region, NULL, rslide, NULL ) ) return( -1 ); } @@ -457,7 +401,8 @@ slide_flags( const char *filename ) vips_filename_split( filename, name, mode ); strtol( mode, &endp, 10 ); - if( *mode == 0 || *endp == 0 ) + if( *mode == 0 || + *endp == 0 ) /* Slide layer or no mode specified. */ return( VIPS_FORMAT_PARTIAL ); diff --git a/libvips/include/vips/colour.h b/libvips/include/vips/colour.h index 0a97dd17..545ebff6 100644 --- a/libvips/include/vips/colour.h +++ b/libvips/include/vips/colour.h @@ -119,6 +119,7 @@ float im_col_dE00( int im_LCh2Lab( VipsImage *in, VipsImage *out ); int im_LabQ2XYZ( VipsImage *in, VipsImage *out ); int im_rad2float( VipsImage *in, VipsImage *out ); +int im_argb2rgba( VipsImage *in, VipsImage *out ); int im_float2rad( VipsImage *in, VipsImage *out ); int im_LCh2UCS( VipsImage *in, VipsImage *out ); int im_Lab2LCh( VipsImage *in, VipsImage *out );