move openslide to a new-style class

This commit is contained in:
John Cupitt 2011-12-11 22:01:39 +00:00
parent 7fe4843250
commit 230e9ce63e
13 changed files with 322 additions and 93 deletions

9
TODO
View File

@ -1,3 +1,12 @@
- still some size_t that should be guint64, search for all use of
VIPS_IMAGE_SIZEOF_IMAGE(), vips__parse_size()
- some mysterious error in tilecahce for new-style openslide read
- how about
vips max add[babe.jpg,babe2.jpg]

View File

@ -306,7 +306,8 @@ copy_region( VipsRegion *from, VipsRegion *to, VipsRect *area )
/* Generate func.
*/
static int
vips_tile_cache_gen( VipsRegion *or, void *seq, void *a, void *b, gboolean *stop )
vips_tile_cache_gen( VipsRegion *or,
void *seq, void *a, void *b, gboolean *stop )
{
VipsRegion *in = (VipsRegion *) seq;
VipsTileCache *cache = (VipsTileCache *) b;

View File

@ -1,6 +1,9 @@
noinst_LTLIBRARIES = libforeign.la
libforeign_la_SOURCES = \
vipsopenslide.h \
vipsopenslide.c \
openslideload.c \
tiff.h \
vips2tiff.c \
tiff2vips.c \

View File

@ -428,11 +428,11 @@ vips_foreign_load_new_from_string( const char *string )
return( VIPS_OBJECT( load ) );
}
static size_t
static guint64
vips_get_disc_threshold( void )
{
static gboolean done = FALSE;
static size_t threshold;
static guint64 threshold;
if( !done ) {
const char *env;
@ -460,14 +460,14 @@ vips_get_disc_threshold( void )
* on the new image.
*/
static void *
vips_foreign_load_start_cb( VipsImage *out, void *a, void *dummy )
vips_foreign_load_start( VipsImage *out, void *a, void *dummy )
{
VipsForeignLoad *load = VIPS_FOREIGN_LOAD( a );
VipsForeignLoadClass *class = VIPS_FOREIGN_LOAD_GET_CLASS( a );
if( !load->real ) {
const size_t disc_threshold = vips_get_disc_threshold();
const size_t image_size = VIPS_IMAGE_SIZEOF_IMAGE( load->out );
const guint64 disc_threshold = vips_get_disc_threshold();
const guint64 image_size = VIPS_IMAGE_SIZEOF_IMAGE( load->out );
/* We open via disc if:
* - 'disc' is set
@ -502,7 +502,7 @@ vips_foreign_load_start_cb( VipsImage *out, void *a, void *dummy )
/* Just pointer-copy.
*/
static int
vips_foreign_load_generate_cb( VipsRegion *or,
vips_foreign_load_generate( VipsRegion *or,
void *seq, void *a, void *b, gboolean *stop )
{
VipsRegion *ir = (VipsRegion *) seq;
@ -552,19 +552,17 @@ vips_foreign_load_build( VipsObject *object )
* convert pixels on demand.
*/
if( class->load ) {
/* THINSTRIP since this is probably a disc file.
* We can't tell yet whether we will be opening
* to memory, sadly, so we can't suggest ANY.
/* ->header() should set the dhint. It'll default to the safe
* SMALLTILE if header() did not set it.
*/
vips_demand_hint( load->out,
VIPS_DEMAND_STYLE_THINSTRIP, NULL );
vips_demand_hint( load->out, load->out->dhint, NULL );
/* Then 'start' creates the real image and 'gen' fetches
* pixels for @out from @real on demand.
*/
if( vips_image_generate( load->out,
vips_foreign_load_start_cb,
vips_foreign_load_generate_cb,
vips_foreign_load_start,
vips_foreign_load_generate,
vips_stop_one,
load, NULL ) )
return( -1 );
@ -1011,6 +1009,7 @@ vips_foreign_write( VipsImage *in, const char *filename, ... )
void
vips_foreign_operation_init( void )
{
extern GType vips_foreign_load_openslide_get_type( void );
extern GType vips_foreign_load_jpeg_file_get_type( void );
extern GType vips_foreign_load_jpeg_buffer_get_type( void );
extern GType vips_foreign_save_jpeg_file_get_type( void );
@ -1034,6 +1033,10 @@ vips_foreign_operation_init( void )
vips_foreign_save_tiff_get_type();
#endif /*HAVE_TIFF*/
#ifdef HAVE_OPENSLIDE
vips_foreign_load_openslide_get_type();
#endif /*HAVE_OPENSLIDE*/
vips_foreign_load_vips_get_type();
vips_foreign_save_vips_get_type();
}

View File

@ -0,0 +1,211 @@
/* load openslide from a file
*
* 5/12/11
* - from openslideload.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
*/
/*
#define DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vips/vips.h>
#include <vips/buf.h>
#include <vips/internal.h>
#include "vipsopenslide.h"
typedef struct _VipsForeignLoadOpenslide {
VipsForeignLoad parent_object;
/* Filename for load.
*/
char *filename;
/* Load this layer.
*/
int layer;
/* Load this associated image.
*/
char *associated;
} VipsForeignLoadOpenslide;
typedef VipsForeignLoadClass VipsForeignLoadOpenslideClass;
G_DEFINE_TYPE( VipsForeignLoadOpenslide, vips_foreign_load_openslide,
VIPS_TYPE_FOREIGN_LOAD );
static VipsForeignFlags
vips_foreign_load_openslide_get_flags( VipsForeignLoad *load )
{
VipsForeignLoadOpenslide *openslide = (VipsForeignLoadOpenslide *) load;
VipsForeignFlags flags;
flags = 0;
if( !openslide->associated )
flags |= VIPS_FOREIGN_PARTIAL;
return( flags );
}
static int
vips_foreign_load_openslide_header( VipsForeignLoad *load )
{
VipsForeignLoadOpenslide *openslide = (VipsForeignLoadOpenslide *) load;
if( vips__openslide_read_header( openslide->filename, load->out,
openslide->layer, openslide->associated ) )
return( -1 );
return( 0 );
}
static int
vips_foreign_load_openslide_load( VipsForeignLoad *load )
{
VipsForeignLoadOpenslide *openslide = (VipsForeignLoadOpenslide *) load;
if( !openslide->associated ) {
if( vips__openslide_read( openslide->filename, load->real,
openslide->layer ) )
return( -1 );
}
else {
if( vips__openslide_read_associated(
openslide->filename, load->real,
openslide->associated ) )
return( -1 );
}
return( 0 );
}
static void
vips_foreign_load_openslide_class_init( VipsForeignLoadOpenslideClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsForeignClass *foreign_class = (VipsForeignClass *) class;
VipsForeignLoadClass *load_class = (VipsForeignLoadClass *) class;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "openslideload";
object_class->description = _( "load file with OpenSlide" );
/* We need to be ahead of the tiff sniffer since many OpenSlide
* formats are tiff derivatives. If we see a tiff which would be
* better handled by the vips tiff loader we are careful to say no.
*/
foreign_class->priority = 100;
load_class->is_a = vips__openslide_isslide;
load_class->get_flags = vips_foreign_load_openslide_get_flags;
load_class->header = vips_foreign_load_openslide_header;
load_class->load = vips_foreign_load_openslide_load;
VIPS_ARG_STRING( class, "filename", 1,
_( "Filename" ),
_( "Filename to load from" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsForeignLoadOpenslide, filename ),
NULL );
VIPS_ARG_INT( class, "layer", 10,
_( "Layer" ),
_( "Load this layer from the file" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignLoadOpenslide, layer ),
0, 100000, 0 );
VIPS_ARG_STRING( class, "associated", 11,
_( "Associated" ),
_( "Load this associated image" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignLoadOpenslide, associated ),
NULL );
}
static void
vips_foreign_load_openslide_init( VipsForeignLoadOpenslide *openslide )
{
}
/**
* vips_openslideload:
* @filename: file to load
* @out: decompressed image
* @layer: load this layer
* @associated: load this associated image
* @...: %NULL-terminated list of optional named arguments
*
* Read a virtual slide supported by the OpenSlide library into a VIPS image.
* OpenSlide supports images in Aperio, Hamamatsu VMS, Hamamatsu VMU, MIRAX,
* and Trestle formats.
*
* To facilitate zooming, virtual slide formats include multiple scaled-down
* versions of the high-resolution image. These are typically called
* "levels", though OpenSlide and im_openslide2vips() call them "layers".
* By default, vips_openslideload() reads the highest-resolution layer
* (layer 0). Set @layer to the layer number you want.
*
* In addition to the slide image itself, virtual slide formats sometimes
* include additional images, such as a scan of the slide's barcode.
* OpenSlide calls these "associated images". To read an associated image,
* set @associated to the image's.
* A slide's associated images are listed in the
* "slide-associated-images" metadata item.
*
* See also: vips_image_new_from_file().
*
* Returns: 0 on success, -1 on error.
*/
int
vips_openslideload( const char *filename, VipsImage **out, ... )
{
va_list ap;
int result;
va_start( ap, out );
result = vips_call_split( "openslideload", ap, filename, out );
va_end( ap );
return( result );
}

View File

@ -65,7 +65,7 @@
#include <openslide.h>
#include "openslide.h"
#include "vipsopenslide.h"
/* We run our own tile cache. The OpenSlide one can't always keep enough for a
* complete lines of pixels.
@ -75,7 +75,7 @@
typedef struct {
openslide_t *osr;
const char *associated;
char *associated;
/* Only valid if associated == NULL.
*/
@ -115,6 +115,7 @@ static void
readslide_destroy_cb( VipsImage *image, ReadSlide *rslide )
{
VIPS_FREEF( openslide_close, rslide->osr );
VIPS_FREE( rslide->associated );
}
static int
@ -134,64 +135,53 @@ check_associated_image( openslide_t *osr, const char *name )
}
static ReadSlide *
readslide_new( const char *filename, VipsImage *out )
readslide_new( const char *filename, VipsImage *out,
int layer, const char *associated )
{
ReadSlide *rslide;
char name[FILENAME_MAX];
char mode[FILENAME_MAX];
char *endp;
int64_t w, h;
const char *background;
const char * const *properties;
char *associated;
rslide = VIPS_NEW( out, ReadSlide );
memset( rslide, 0, sizeof( *rslide ) );
g_signal_connect( out, "close", G_CALLBACK( readslide_destroy_cb ),
rslide );
vips_filename_split( filename, name, mode );
rslide->osr = openslide_open( name );
rslide->layer = layer;
rslide->associated = g_strdup( associated );
rslide->osr = openslide_open( filename );
if( rslide->osr == NULL ) {
vips_error( "im_openslide2vips",
"%s", _( "failure opening slide" ) );
return( NULL );
}
/* Parse optional mode.
*/
rslide->layer = strtol( mode, &endp, 10 );
if( *mode != 0 && *endp == 0 ) {
/* Mode specifies slide layer.
*/
if( rslide->layer < 0 || rslide->layer >=
openslide_get_layer_count( rslide->osr ) ) {
vips_error( "im_openslide2vips",
"%s", _( "invalid slide layer" ) );
return( NULL );
}
}
else if( *mode != 0 ) {
/* Mode specifies associated image.
*/
if ( check_associated_image( rslide->osr, mode ) )
return( NULL );
rslide->associated = vips_strdup( VIPS_OBJECT( out ), mode );
if( layer < 0 ||
layer >= openslide_get_layer_count( rslide->osr ) ) {
vips_error( "im_openslide2vips",
"%s", _( "invalid slide layer" ) );
return( NULL );
}
if( rslide->associated ) {
if( associated &&
check_associated_image( rslide->osr, associated ) )
return( NULL );
if( associated ) {
openslide_get_associated_image_dimensions( rslide->osr,
rslide->associated, &w, &h );
associated, &w, &h );
vips_image_set_string( out, "slide-associated-image",
rslide->associated );
associated );
vips_demand_hint( out, VIPS_DEMAND_STYLE_THINSTRIP, NULL );
}
else {
openslide_get_layer_dimensions( rslide->osr, rslide->layer,
&w, &h );
openslide_get_layer_dimensions( rslide->osr,
layer, &w, &h );
rslide->downsample = openslide_get_layer_downsample(
rslide->osr, rslide->layer );
vips_image_set_int( out, "slide-layer", rslide->layer );
rslide->osr, layer );
vips_image_set_int( out, "slide-layer", layer );
vips_demand_hint( out, VIPS_DEMAND_STYLE_SMALLTILE, NULL );
}
@ -229,7 +219,6 @@ readslide_new( const char *filename, VipsImage *out )
associated = g_strjoinv( ", ", (char **)
openslide_get_associated_image_names( rslide->osr ) );
vips_image_set_string( out, "slide-associated-images", associated );
g_free( associated );
return( rslide );
}
@ -247,8 +236,8 @@ vips__openslide_read_header( const char *filename, VipsImage *out,
}
static int
fill_region( VipsRegion *out, void *seq, void *_rslide, void *unused,
gboolean *stop )
vips__openslide_generate( VipsRegion *out,
void *seq, void *_rslide, void *unused, gboolean *stop )
{
ReadSlide *rslide = _rslide;
VipsRect *r = &out->valid;
@ -288,36 +277,44 @@ fill_region( VipsRegion *out, void *seq, void *_rslide, void *unused,
}
int
vips__openslide_read_file( const char *filename, VipsImage *out, int layer )
vips__openslide_read( const char *filename, VipsImage *out, int layer )
{
ReadSlide *rslide;
VipsImage *raw;
VipsImage *t;
VIPS_DEBUG_MSG( "vips__openslide_read_file: %s %d\n",
VIPS_DEBUG_MSG( "vips__openslide_read: %s %d\n",
filename, layer );
/* Tile cache: keep enough for two complete rows of tiles.
* This lets us do (smallish) area ops, like im_conv(), while
* still only hitting each tile once.
*/
if( !(raw = im_open_local( out, "cache", "p" )) )
return( -1 );
raw = vips_image_new();
vips_object_local( out, raw );
if( !(rslide = readslide_new( filename, raw, layer, NULL )) )
return( -1 );
if( vips_image_pio_output( raw ) ||
vips_image_generate( raw,
NULL, fill_region, NULL, rslide, NULL ) )
NULL, vips__openslide_generate, NULL, rslide, NULL ) )
return( -1 );
/* Copy to out, adding a cache. Enough tiles for a complete row, plus
* 50%.
*/
if( im_tile_cache( raw, out,
TILE_WIDTH, TILE_HEIGHT,
1.5 * (1 + raw->Xsize / TILE_WIDTH) ) )
if( vips_tilecache( raw, &t,
"tile_width", TILE_WIDTH,
"tile_height", TILE_WIDTH,
"max_tiles", 1.5 * (1 + raw->Xsize / TILE_WIDTH),
NULL ) )
return( -1 );
if( vips_image_write( t, out ) ) {
g_object_unref( t );
return( -1 );
}
g_object_unref( t );
return( 0 );
}
@ -328,37 +325,23 @@ vips__openslide_read_associated( const char *filename, VipsImage *out,
{
ReadSlide *rslide;
VipsImage *raw;
uint32_t *buf;
int64_t w, h;
int y;
const char *error;
VIPS_DEBUG_MSG( "vips__openslide_read_associated: %s %s\n",
filename, associated );
/* Tile cache: keep enough for two complete rows of tiles.
* This lets us do (smallish) area ops, like im_conv(), while
* still only hitting each tile once.
/* Memory buffer. Get associated directly to this, then copy to out.
*/
if( !(raw = im_open_local( out, "cache", "p" )) )
return( -1 );
raw = vips_image_new_buffer();
vips_object_local( out, raw );
if( !(rslide = readslide_new( filename, raw, 0, associated )) )
return( -1 );
openslide_get_associated_image_dimensions( rslide->osr,
rslide->associated, &w, &h );
if( w == -1 ||
h == -1 ) {
vips_error( "im_openslide2vips",
_( "getting dimensions: %s" ),
openslide_get_error( rslide->osr ) );
if( vips_image_wio_output( raw ) )
return( -1 );
}
buf = VIPS_ARRAY( out, w * h, uint32_t );
openslide_read_associated_image( rslide->osr, rslide->associated, buf );
openslide_read_associated_image( rslide->osr, rslide->associated,
(uint32_t *) VIPS_IMAGE_ADDR( raw, 0, 0 ) );
error = openslide_get_error( rslide->osr );
if( error ) {
vips_error( "im_openslide2vips",
@ -366,11 +349,8 @@ vips__openslide_read_associated( const char *filename, VipsImage *out,
return( -1 );
}
if( vips_image_wio_output( out ) )
if( vips_image_write( raw, out ) )
return( -1 );
for( y = 0; y < h; y++ )
if( vips_image_write_line( out, y, (PEL *) (buf + y * w) ) )
return( -1 );
return( 0 );
}

View File

@ -35,8 +35,10 @@ extern "C" {
#endif /*__cplusplus*/
int vips__openslide_isslide( const char *filename );
int vips__openslide_read_header( const char *filename, VipsImage *out );
int vips__openslide_read_file( const char *filename, VipsImage *out );
int vips__openslide_read_header( const char *filename, VipsImage *out,
int layer, char *associated );
int vips__openslide_read( const char *filename, VipsImage *out,
int layer );
int vips__openslide_read_associated( const char *filename, VipsImage *out,
const char *associated );

View File

@ -232,6 +232,9 @@ int vips_foreign_write( VipsImage *in, const char *filename, ... )
void vips_foreign_operation_init( void );
int vips_openslideload( const char *filename, VipsImage **out, ... )
__attribute__((sentinel));
int vips_jpegload( const char *filename, VipsImage **out, ... )
__attribute__((sentinel));
int vips_jpegload_buffer( void *buf, size_t len, VipsImage **out, ... )

View File

@ -468,6 +468,7 @@ void vips_image_set_kill( VipsImage *image, gboolean kill );
VipsImage *vips_image_new( void );
VipsImage *vips_image_new_mode( const char *filename, const char *mode );
VipsImage *vips_image_new_buffer( void );
VipsImage *vips_image_new_from_file( const char *filename );
VipsImage *vips_image_new_from_file_raw( const char *filename,
int xsize, int ysize, int bands, guint64 offset );

View File

@ -294,7 +294,7 @@ void vips__init_wrap7_classes( void );
void vips_arithmetic_operation_init( void );
void vips_conversion_operation_init( void );
size_t vips__parse_size( const char *size_string );
guint64 vips__parse_size( const char *size_string );
IMAGE *vips__deprecated_open_read( const char *filename );
IMAGE *vips__deprecated_open_write( const char *filename );

View File

@ -1358,6 +1358,23 @@ vips_image_new_mode( const char *filename, const char *mode )
return( image );
}
/**
* vips_image_new_buffer:
*
* vips_image_new_buffer() creates a new VipsImage which when written to will
* create a memory buffer. It is a convenience function for
* vips_image_new_mode(vips_image_temp_name(), "t").
*
* See also: vips_image_new().
*
* Returns: the new #VipsImage, or %NULL on error.
*/
VipsImage *
vips_image_new_buffer( void )
{
return( vips_image_new_mode( vips_image_temp_name(), "t" ) );
}
/**
* vips_image_new_from_file:
* @filename: file to open
@ -1919,8 +1936,7 @@ vips_image_wio_input( VipsImage *image )
/* Change to VIPS_IMAGE_SETBUF. First, make a memory
* buffer and copy into that.
*/
if( !(t1 = vips_image_new_mode( "wio_input", "t" )) )
return( -1 );
t1 = vips_image_new_buffer();
if( vips_image_write( image, t1 ) ) {
g_object_unref( t1 );
return( -1 );

View File

@ -28,8 +28,8 @@
*/
/*
#define VIPS_DEBUG
*/
#define VIPS_DEBUG
#ifdef HAVE_CONFIG_H
#include <config.h>

View File

@ -1460,7 +1460,7 @@ typedef struct {
int multiplier;
} Unit;
size_t
guint64
vips__parse_size( const char *size_string )
{
static Unit units[] = {
@ -1469,7 +1469,7 @@ vips__parse_size( const char *size_string )
{ 'g', 1024 * 1024 * 1024 }
};
size_t size;
guint64 size;
int n;
int i, j;
char *unit;
@ -1489,7 +1489,7 @@ vips__parse_size( const char *size_string )
}
g_free( unit );
VIPS_DEBUG_MSG( "parse_size: parsed \"%s\" as %zd\n",
VIPS_DEBUG_MSG( "parse_size: parsed \"%s\" as %lld\n",
size_string, size );
return( size );