start adding tiff load from buffer

see

https://github.com/jcupitt/libvips/issues/25
This commit is contained in:
John Cupitt 2014-04-19 16:14:54 +01:00
parent 5c751fb364
commit 6144aecccb
5 changed files with 289 additions and 78 deletions

View File

@ -1651,7 +1651,8 @@ vips_foreign_operation_init( void )
extern GType vips_foreign_save_jpeg_file_get_type( void );
extern GType vips_foreign_save_jpeg_buffer_get_type( void );
extern GType vips_foreign_save_jpeg_mime_get_type( void );
extern GType vips_foreign_load_tiff_get_type( void );
extern GType vips_foreign_load_tiff_file_get_type( void );
extern GType vips_foreign_load_tiff_buffer_get_type( void );
extern GType vips_foreign_save_tiff_get_type( void );
extern GType vips_foreign_load_vips_get_type( void );
extern GType vips_foreign_save_vips_get_type( void );
@ -1709,7 +1710,8 @@ vips_foreign_operation_init( void )
#endif /*HAVE_LIBWEBP*/
#ifdef HAVE_TIFF
vips_foreign_load_tiff_get_type();
vips_foreign_load_tiff_file_get_type();
vips_foreign_load_tiff_buffer_get_type();
vips_foreign_save_tiff_get_type();
#endif /*HAVE_TIFF*/
@ -1807,6 +1809,44 @@ vips_tiffload( const char *filename, VipsImage **out, ... )
return( result );
}
/**
* vips_tiffload_buffer:
* @buf: memory area to load
* @len: size of memory area
* @out: image to write
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* @page: load this page
*
* Read a TIFF-formatted memory block into a VIPS image. Exactly as
* vips_tiffload(), but read from a memory source.
*
* See also: vips_tiffload().
*
* Returns: 0 on success, -1 on error.
*/
int
vips_tiffload_buffer( void *buf, size_t len, VipsImage **out, ... )
{
va_list ap;
VipsArea *area;
int result;
/* We don't take a copy of the data or free it.
*/
area = vips_area_new_blob( NULL, buf, len );
va_start( ap, out );
result = vips_call_split( "tiffload_buffer", ap, area, out );
va_end( ap );
vips_area_unref( area );
return( result );
}
/**
* vips_tiffsave:
* @in: image to save
@ -1899,46 +1939,6 @@ vips_tiffsave( VipsImage *in, const char *filename, ... )
return( result );
}
/**
* vips_jpegload_buffer:
* @buf: memory area to load
* @len: size of memory area
* @out: image to write
* @...: %NULL-terminated list of optional named arguments
*
* Read a JPEG-formatted memory block into a VIPS image. It can read most
* 8-bit JPEG images, including CMYK and YCbCr.
*
* This function is handy for processing JPEG image thumbnails.
*
* Caution: on return only the header will have been read, the pixel data is
* not decompressed until the first pixel is read. Therefore you must not free
* @buf until you have read pixel data from @out.
*
* See also: vips_jpegload().
*
* Returns: 0 on success, -1 on error.
*/
int
vips_jpegload_buffer( void *buf, size_t len, VipsImage **out, ... )
{
va_list ap;
VipsArea *area;
int result;
/* We don't take a copy of the data or free it.
*/
area = vips_area_new_blob( NULL, buf, len );
va_start( ap, out );
result = vips_call_split( "jpegload_buffer", ap, area, out );
va_end( ap );
vips_area_unref( area );
return( result );
}
/**
* vips_jpegload:
* @filename: file to load
@ -2008,6 +2008,45 @@ vips_jpegload( const char *filename, VipsImage **out, ... )
return( result );
}
/**
* vips_jpegload_buffer:
* @buf: memory area to load
* @len: size of memory area
* @out: image to write
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* @shrink: shrink by this much on load
* @fail: fail on warnings
*
* Read a JPEG-formatted memory block into a VIPS image. Exactly as
* vips_jpegload(), but read from a memory buffer.
*
* See also: vips_jpegload().
*
* Returns: 0 on success, -1 on error.
*/
int
vips_jpegload_buffer( void *buf, size_t len, VipsImage **out, ... )
{
va_list ap;
VipsArea *area;
int result;
/* We don't take a copy of the data or free it.
*/
area = vips_area_new_blob( NULL, buf, len );
va_start( ap, out );
result = vips_call_split( "jpegload_buffer", ap, area, out );
va_end( ap );
vips_area_unref( area );
return( result );
}
/**
* vips_jpegsave:
* @in: image to save

View File

@ -50,12 +50,17 @@ int vips__tiff_write( VipsImage *in, const char *filename,
gboolean bigtiff,
gboolean rgbjpeg );
int vips__tiff_read( const char *filename, VipsImage *out, int page,
gboolean readbehind );
int vips__tiff_read_header( const char *filename, VipsImage *out, int page );
int vips__tiff_read( const char *filename, VipsImage *out,
int page, gboolean readbehind );
gboolean vips__istifftiled( const char *filename );
gboolean vips__istiff( const char *filename );
int vips__tiff_read_header_buffer( void *buf, size_t len, VipsImage *out,
int page );
int vips__tiff_read_buffer( void *buf, size_t len, VipsImage *out,
int page, gboolean readbehind );
#ifdef __cplusplus
}
#endif /*__cplusplus*/

View File

@ -1807,4 +1807,64 @@ vips__istiff( const char *filename )
return( FALSE );
}
int
vips__tiff_read_header_buffer( void *buf, size_t len, VipsImage *out, int page )
{
ReadTiff *rtiff;
vips__tiff_init();
move the TIFFOpen into readtiff_new
put the get_dir in there as well
if( !(rtiff = readtiff_new_buffer( buf, len, out, page, FALSE )) )
return( -1 );
if( !(rtiff->tiff = get_directory( rtiff, rtiff->page )) ) {
vips_error( "tiff2vips",
_( "TIFF does not contain page %d" ), rtiff->page );
return( -1 );
}
if( parse_header( rtiff, out ) )
return( -1 );
return( 0 );
}
int
vips__tiff_read_buffer( void *buf, size_t len, VipsImage *out,
int page, gboolean readbehind )
{
ReadTiff *rtiff;
#ifdef DEBUG
printf( "tiff2vips: libtiff version is \"%s\"\n", TIFFGetVersion() );
printf( "tiff2vips: libtiff starting for %s\n", filename );
#endif /*DEBUG*/
vips__tiff_init();
if( !(rtiff = readtiff_new_buffer( buf, len, out, page, readbehind )) )
return( -1 );
if( !(rtiff->tiff = get_directory( rtiff, rtiff->page )) ) {
vips_error( "tiff2vips",
_( "TIFF does not contain page %d" ), rtiff->page );
return( -1 );
}
if( TIFFIsTiled( rtiff->tiff ) ) {
if( read_tilewise( rtiff, out ) )
return( -1 );
}
else {
if( read_stripwise( rtiff, out ) )
return( -1 );
}
return( 0 );
}
#endif /*HAVE_TIFF*/

View File

@ -55,10 +55,6 @@
typedef struct _VipsForeignLoadTiff {
VipsForeignLoad parent_object;
/* Filename for load.
*/
char *filename;
/* Load this page.
*/
int page;
@ -67,11 +63,58 @@ typedef struct _VipsForeignLoadTiff {
typedef VipsForeignLoadClass VipsForeignLoadTiffClass;
G_DEFINE_TYPE( VipsForeignLoadTiff, vips_foreign_load_tiff,
G_DEFINE_ABSTRACT_TYPE( VipsForeignLoadTiff, vips_foreign_load_tiff,
VIPS_TYPE_FOREIGN_LOAD );
static void
vips_foreign_load_tiff_class_init( VipsForeignLoadTiffClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) class;
/* Other libraries may be using libtiff, we want to capture tiff
* warning and error as soon as we can.
*
* This class init will be triggered during startup.
*/
vips__tiff_init();
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "tiffload_base";
object_class->description = _( "load tiff" );
VIPS_ARG_INT( class, "page", 10,
_( "Page" ),
_( "Load this page from the image" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignLoadTiff, page ),
0, 100000, 0 );
}
static void
vips_foreign_load_tiff_init( VipsForeignLoadTiff *tiff )
{
tiff->page = 0;
}
typedef struct _VipsForeignLoadTiffFile {
VipsForeignLoadTiff parent_object;
/* Filename for load.
*/
char *filename;
} VipsForeignLoadTiffFile;
typedef VipsForeignLoadTiffClass VipsForeignLoadTiffFileClass;
G_DEFINE_TYPE( VipsForeignLoadTiffFile, vips_foreign_load_tiff_file,
vips_foreign_load_tiff_get_type() );
static VipsForeignFlags
vips_foreign_load_tiff_get_flags_filename( const char *filename )
vips_foreign_load_tiff_file_get_flags_filename( const char *filename )
{
VipsForeignFlags flags;
@ -85,30 +128,33 @@ vips_foreign_load_tiff_get_flags_filename( const char *filename )
}
static VipsForeignFlags
vips_foreign_load_tiff_get_flags( VipsForeignLoad *load )
vips_foreign_load_tiff_file_get_flags( VipsForeignLoad *load )
{
VipsForeignLoadTiff *tiff = (VipsForeignLoadTiff *) load;
VipsForeignLoadTiffFile *file = (VipsForeignLoadTiffFile *) load;
return( vips_foreign_load_tiff_get_flags_filename( tiff->filename ) );
return( vips_foreign_load_tiff_file_get_flags_filename(
file->filename ) );
}
static int
vips_foreign_load_tiff_header( VipsForeignLoad *load )
vips_foreign_load_tiff_file_header( VipsForeignLoad *load )
{
VipsForeignLoadTiff *tiff = (VipsForeignLoadTiff *) load;
VipsForeignLoadTiffFile *file = (VipsForeignLoadTiffFile *) load;
if( vips__tiff_read_header( tiff->filename, load->out, tiff->page ) )
if( vips__tiff_read_header( file->filename, load->out, tiff->page ) )
return( -1 );
return( 0 );
}
static int
vips_foreign_load_tiff_load( VipsForeignLoad *load )
vips_foreign_load_tiff_file_load( VipsForeignLoad *load )
{
VipsForeignLoadTiff *tiff = (VipsForeignLoadTiff *) load;
VipsForeignLoadTiffFile *file = (VipsForeignLoadTiffFile *) load;
if( vips__tiff_read( tiff->filename, load->real, tiff->page,
if( vips__tiff_read( file->filename, load->real, tiff->page,
load->access == VIPS_ACCESS_SEQUENTIAL ) )
return( -1 );
@ -118,20 +164,13 @@ vips_foreign_load_tiff_load( VipsForeignLoad *load )
const char *vips__foreign_tiff_suffs[] = { ".tif", ".tiff", NULL };
static void
vips_foreign_load_tiff_class_init( VipsForeignLoadTiffClass *class )
vips_foreign_load_tiff_file_class_init( VipsForeignLoadTiffFileClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsForeignClass *foreign_class = (VipsForeignClass *) class;
VipsForeignLoadClass *load_class = (VipsForeignLoadClass *) class;
/* Other libraries may be using libtiff, we want to capture tiff
* warning and error as soon as we can.
*
* This class init will be triggered during startup.
*/
vips__tiff_init();
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
@ -142,28 +181,94 @@ vips_foreign_load_tiff_class_init( VipsForeignLoadTiffClass *class )
load_class->is_a = vips__istiff;
load_class->get_flags_filename =
vips_foreign_load_tiff_get_flags_filename;
load_class->get_flags = vips_foreign_load_tiff_get_flags;
load_class->header = vips_foreign_load_tiff_header;
load_class->load = vips_foreign_load_tiff_load;
vips_foreign_load_tiff_file_get_flags_filename;
load_class->get_flags = vips_foreign_load_tiff_file_get_flags;
load_class->header = vips_foreign_load_tiff_file_header;
load_class->load = vips_foreign_load_tiff_file_load;
VIPS_ARG_STRING( class, "filename", 1,
_( "Filename" ),
_( "Filename to load from" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsForeignLoadTiff, filename ),
G_STRUCT_OFFSET( VipsForeignLoadTiffFile, filename ),
NULL );
VIPS_ARG_INT( class, "page", 10,
_( "Page" ),
_( "Load this page from the file" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignLoadTiff, page ),
0, 100000, 0 );
}
static void
vips_foreign_load_tiff_init( VipsForeignLoadTiff *tiff )
vips_foreign_load_tiff_file_init( VipsForeignLoadTiffFile *file )
{
}
typedef struct _VipsForeignLoadTiffBuffer {
VipsForeignLoadTiff parent_object;
/* Load from a buffer.
*/
VipsArea *buf;
} VipsForeignLoadTiffBuffer;
typedef VipsForeignLoadTiffClass VipsForeignLoadTiffBufferClass;
G_DEFINE_TYPE( VipsForeignLoadTiffBuffer, vips_foreign_load_tiff_buffer,
vips_foreign_load_tiff_get_type() );
static int
vips_foreign_load_tiff_buffer_header( VipsForeignLoad *load )
{
VipsForeignLoadTiff *tiff = (VipsForeignLoadTiff *) load;
VipsForeignLoadTiffBuffer *buffer = (VipsForeignLoadTiffBuffer *) load;
if( vips__tiff_read_header_buffer(
buffer->buf->data, buffer->buf->length, load->out,
tiff->page ) )
return( -1 );
return( 0 );
}
static int
vips_foreign_load_tiff_buffer_load( VipsForeignLoad *load )
{
VipsForeignLoadTiff *tiff = (VipsForeignLoadTiff *) load;
VipsForeignLoadTiffBuffer *buffer = (VipsForeignLoadTiffBuffer *) load;
if( vips__tiff_read_buffer(
buffer->buf->data, buffer->buf->length, load->real,
tiff->page,
load->access == VIPS_ACCESS_SEQUENTIAL ) )
return( -1 );
return( 0 );
}
static void
vips_foreign_load_tiff_buffer_class_init(
VipsForeignLoadTiffBufferClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) 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 = "tiffload_buffer";
object_class->description = _( "load tiff from buffer" );
load_class->header = vips_foreign_load_tiff_buffer_header;
load_class->load = vips_foreign_load_tiff_buffer_load;
VIPS_ARG_BOXED( class, "buffer", 1,
_( "Buffer" ),
_( "Buffer to load from" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsForeignLoadTiffBuffer, buf ),
VIPS_TYPE_BLOB );
}
static void
vips_foreign_load_tiff_buffer_init( VipsForeignLoadTiffBuffer *buffer )
{
}

View File

@ -397,6 +397,8 @@ typedef enum {
int vips_tiffload( const char *filename, VipsImage **out, ... )
__attribute__((sentinel));
int vips_tiffload_buffer( void *buf, size_t len, VipsImage **out, ... )
__attribute__((sentinel));
int vips_tiffsave( VipsImage *in, const char *filename, ... )
__attribute__((sentinel));