Added support for writing buffers with magicksave.
Added --format for the filename.
This commit is contained in:
parent
e22282a844
commit
8f4179f496
@ -1776,6 +1776,7 @@ vips_foreign_operation_init( void )
|
||||
extern GType vips_foreign_load_magick7_file_get_type( void );
|
||||
extern GType vips_foreign_load_magick7_buffer_get_type( void );
|
||||
extern GType vips_foreign_save_magick_file_get_type( void );
|
||||
extern GType vips_foreign_save_magick_buffer_get_type( void );
|
||||
extern GType vips_foreign_save_dz_file_get_type( void );
|
||||
extern GType vips_foreign_save_dz_buffer_get_type( void );
|
||||
extern GType vips_foreign_load_webp_file_get_type( void );
|
||||
@ -1879,15 +1880,17 @@ vips_foreign_operation_init( void )
|
||||
#endif /*HAVE_OPENSLIDE*/
|
||||
|
||||
#ifdef HAVE_MAGICK
|
||||
vips_foreign_load_magick_file_get_type();
|
||||
vips_foreign_load_magick_buffer_get_type();
|
||||
vips_foreign_load_magick_file_get_type();
|
||||
vips_foreign_load_magick_buffer_get_type();
|
||||
vips_foreign_save_magick_file_get_type();
|
||||
vips_foreign_save_magick_buffer_get_type();
|
||||
#endif /*HAVE_MAGICK*/
|
||||
|
||||
#ifdef HAVE_MAGICK7
|
||||
vips_foreign_load_magick7_file_get_type();
|
||||
vips_foreign_load_magick7_buffer_get_type();
|
||||
vips_foreign_load_magick7_file_get_type();
|
||||
vips_foreign_load_magick7_buffer_get_type();
|
||||
vips_foreign_save_magick_file_get_type();
|
||||
vips_foreign_save_magick_buffer_get_type();
|
||||
#endif /*HAVE_MAGICK7*/
|
||||
|
||||
#ifdef HAVE_CFITSIO
|
||||
|
@ -109,8 +109,10 @@
|
||||
#if !defined(QuantumRange)
|
||||
# define QuantumRange MaxRGB
|
||||
#endif
|
||||
#define TextExtent MagickTextExtent
|
||||
#elif HAVE_MAGICK7
|
||||
#include <MagickCore/MagickCore.h>
|
||||
#define TextExtent MagickPathExtent
|
||||
#endif
|
||||
|
||||
/* And this used to be UseHDRI.
|
||||
@ -122,10 +124,9 @@
|
||||
/* What we track during a write call.
|
||||
*/
|
||||
typedef struct _Write {
|
||||
char *filename;
|
||||
VipsImage *im;
|
||||
|
||||
Image *image;
|
||||
Image *images;
|
||||
ImageInfo *image_info;
|
||||
ExceptionInfo *exception;
|
||||
|
||||
@ -919,9 +920,8 @@ vips__magick_read_buffer_header( const void *buf, const size_t len,
|
||||
static void
|
||||
write_free( Write *write )
|
||||
{
|
||||
VIPS_FREE( write->filename );
|
||||
VIPS_FREE( write->map );
|
||||
VIPS_FREEF( DestroyImageList, write->image );
|
||||
VIPS_FREEF( DestroyImageList, write->images );
|
||||
VIPS_FREEF( DestroyImageInfo, write->image_info );
|
||||
VIPS_FREEF( DestroyExceptionInfo, write->exception );
|
||||
}
|
||||
@ -937,7 +937,7 @@ write_close( VipsImage *im, Write *write )
|
||||
}
|
||||
|
||||
static Write *
|
||||
write_new( const char *filename, VipsImage *im)
|
||||
write_new( VipsImage *im, const char *filename, const char *format )
|
||||
{
|
||||
Write *write;
|
||||
static int inited = 0;
|
||||
@ -949,9 +949,8 @@ write_new( const char *filename, VipsImage *im)
|
||||
|
||||
if( !(write = VIPS_NEW( im, Write )) )
|
||||
return( NULL );
|
||||
write->filename = filename ? g_strdup( filename ) : NULL;
|
||||
write->im = im;
|
||||
write->image = NULL;
|
||||
write->images = NULL;
|
||||
|
||||
write->storageType = UndefinedPixel;
|
||||
switch( im->BandFmt ) {
|
||||
@ -1008,6 +1007,21 @@ write_new( const char *filename, VipsImage *im)
|
||||
return( NULL );
|
||||
}
|
||||
|
||||
if( format ) {
|
||||
vips_strncpy( write->image_info->magick,
|
||||
format, TextExtent );
|
||||
if ( filename ) {
|
||||
va_list ap;
|
||||
|
||||
(void) vips_snprintf( write->image_info->filename,
|
||||
TextExtent, "%s:%s", format, filename );
|
||||
}
|
||||
}
|
||||
else if ( filename ) {
|
||||
vips_strncpy( write->image_info->filename,
|
||||
filename, TextExtent );
|
||||
}
|
||||
|
||||
write->exception = AcquireExceptionInfo();
|
||||
if( !write->exception) {
|
||||
write_free(write);
|
||||
@ -1042,15 +1056,15 @@ magick_create_image( Write *write, VipsImage *im )
|
||||
{
|
||||
Image *image;
|
||||
|
||||
if( write->image == NULL ) {
|
||||
if( write->images == NULL ) {
|
||||
image = AcquireImage( write->image_info, write->exception );
|
||||
if( image == NULL )
|
||||
return( -1 );
|
||||
|
||||
write->image = image;
|
||||
write->images = image;
|
||||
}
|
||||
else {
|
||||
image=GetLastImageInList( write->image );
|
||||
image=GetLastImageInList( write->images );
|
||||
AcquireNextImage( write->image_info, image, write->exception );
|
||||
if( GetNextImageInList( image ) == NULL )
|
||||
return( -1 );
|
||||
@ -1096,7 +1110,20 @@ magick_create_images( Write *write )
|
||||
static int
|
||||
magick_write_images( Write *write )
|
||||
{
|
||||
if( !WriteImages( write->image_info, write->image, write->filename, write->exception ) )
|
||||
if( !WriteImages( write->image_info, write->images,
|
||||
write->image_info->filename, write->exception ) )
|
||||
return( -1 );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
static int
|
||||
magick_write_images_buf( Write *write, void **obuf, size_t *olen )
|
||||
{
|
||||
*obuf=ImagesToBlob( write->image_info, write->images, olen,
|
||||
write->exception );
|
||||
|
||||
if( !*obuf )
|
||||
return( -1 );
|
||||
|
||||
return( 0 );
|
||||
@ -1105,11 +1132,12 @@ magick_write_images( Write *write )
|
||||
#endif /*HAVE_MAGICK7 */
|
||||
|
||||
int
|
||||
vips__magick_write( VipsImage *im, const char *filename )
|
||||
vips__magick_write( VipsImage *im, const char *filename,
|
||||
const char *format )
|
||||
{
|
||||
Write *write;
|
||||
|
||||
if( !(write = write_new( filename, im )) )
|
||||
if( !(write = write_new( im, filename, format )) )
|
||||
return( -1 );
|
||||
|
||||
if ( magick_create_images( write ) ) {
|
||||
@ -1131,5 +1159,31 @@ vips__magick_write( VipsImage *im, const char *filename )
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
int
|
||||
vips__magick_write_buf( VipsImage *im, void **obuf, size_t *olen,
|
||||
const char *format )
|
||||
{
|
||||
Write *write;
|
||||
|
||||
if( !(write = write_new( im, NULL, format )) )
|
||||
return( -1 );
|
||||
|
||||
if ( magick_create_images( write ) ) {
|
||||
vips_error( "magick2vips", _( "unable to write buffer \n"
|
||||
"libMagick error: %s %s" ),
|
||||
write->exception->reason, write->exception->description );
|
||||
return( -1 );
|
||||
}
|
||||
|
||||
if( magick_write_images_buf( write, obuf, olen ) ) {
|
||||
vips_error( "magick2vips", _( "unable to write buffer \n"
|
||||
"libMagick error: %s %s" ),
|
||||
write->exception->reason, write->exception->description );
|
||||
return( -1 );
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
#endif /*HAVE_MAGICK | HAVE_MAGICK7*/
|
||||
|
||||
|
@ -98,6 +98,7 @@ typedef struct _VipsForeignSaveMagickFile {
|
||||
VipsForeignSaveMagick parent_object;
|
||||
|
||||
char *filename;
|
||||
char *format;
|
||||
|
||||
} VipsForeignSaveMagickFile;
|
||||
|
||||
@ -111,13 +112,13 @@ vips_foreign_save_magick_file_build( VipsObject *object )
|
||||
{
|
||||
VipsForeignSave *save = (VipsForeignSave *) object;
|
||||
VipsForeignSaveMagick *magick = (VipsForeignSaveMagick *) object;
|
||||
VipsForeignSaveMagickFile *magick_file = (VipsForeignSaveMagickFile *) object;
|
||||
VipsForeignSaveMagickFile *file = (VipsForeignSaveMagickFile *) object;
|
||||
|
||||
if( VIPS_OBJECT_CLASS( vips_foreign_save_magick_file_parent_class )->
|
||||
build( object ) )
|
||||
build( object ) )
|
||||
return( -1 );
|
||||
|
||||
if( vips__magick_write( save->ready, magick_file->filename ) )
|
||||
if( vips__magick_write( save->ready, file->filename, file->format ) )
|
||||
return( -1 );
|
||||
|
||||
return( 0 );
|
||||
@ -137,13 +138,20 @@ vips_foreign_save_magick_file_class_init(
|
||||
object_class->description = _( "save file with ImageMagick" );
|
||||
object_class->build = vips_foreign_save_magick_file_build;
|
||||
|
||||
VIPS_ARG_STRING( class, "filename", 1,
|
||||
VIPS_ARG_STRING( class, "filename", 1,
|
||||
_( "Filename" ),
|
||||
_( "Filename to save to" ),
|
||||
VIPS_ARGUMENT_REQUIRED_INPUT,
|
||||
VIPS_ARGUMENT_REQUIRED_INPUT,
|
||||
G_STRUCT_OFFSET( VipsForeignSaveMagickFile, filename ),
|
||||
NULL );
|
||||
|
||||
VIPS_ARG_STRING( class, "format", 2,
|
||||
_( "Format" ),
|
||||
_( "Format to save in" ),
|
||||
VIPS_ARGUMENT_OPTIONAL_INPUT,
|
||||
G_STRUCT_OFFSET( VipsForeignSaveMagickFile, format ),
|
||||
NULL );
|
||||
|
||||
}
|
||||
|
||||
static void
|
||||
@ -151,4 +159,80 @@ vips_foreign_save_magick_file_init( VipsForeignSaveMagickFile *file )
|
||||
{
|
||||
}
|
||||
|
||||
typedef struct _VipsForeignSaveMagickBuffer {
|
||||
VipsForeignSaveMagick parent_object;
|
||||
|
||||
/* Save to a buffer.
|
||||
*/
|
||||
VipsArea *buf;
|
||||
char *format;
|
||||
|
||||
} VipsForeignSaveMagickBuffer;
|
||||
|
||||
typedef VipsForeignSaveMagickClass VipsForeignSaveMagickBufferClass;
|
||||
|
||||
G_DEFINE_TYPE( VipsForeignSaveMagickBuffer, vips_foreign_save_magick_buffer,
|
||||
vips_foreign_save_magick_get_type() );
|
||||
|
||||
static int
|
||||
vips_foreign_save_magick_buffer_build( VipsObject *object )
|
||||
{
|
||||
VipsForeignSave *save = (VipsForeignSave *) object;
|
||||
VipsForeignSaveMagick *magick = (VipsForeignSaveMagick *) object;
|
||||
VipsForeignSaveMagickBuffer *buffer = (VipsForeignSaveMagickBuffer *) object;
|
||||
|
||||
void *obuf;
|
||||
size_t olen;
|
||||
VipsBlob *blob;
|
||||
|
||||
if( VIPS_OBJECT_CLASS( vips_foreign_save_magick_buffer_parent_class )->
|
||||
build( object ) )
|
||||
return( -1 );
|
||||
|
||||
if( vips__magick_write_buf( save->ready, &obuf, &olen,
|
||||
buffer->format ) )
|
||||
return( -1 );
|
||||
|
||||
/* obuf is a g_free() buffer, not vips_free().
|
||||
*/
|
||||
blob = vips_blob_new( (VipsCallbackFn) g_free, obuf, olen );
|
||||
g_object_set( buffer, "buffer", blob, NULL );
|
||||
vips_area_unref( VIPS_AREA( blob ) );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
static void
|
||||
vips_foreign_save_magick_buffer_class_init( VipsForeignSaveMagickBufferClass *class )
|
||||
{
|
||||
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
|
||||
VipsObjectClass *object_class = (VipsObjectClass *) class;
|
||||
|
||||
gobject_class->set_property = vips_object_set_property;
|
||||
gobject_class->get_property = vips_object_get_property;
|
||||
|
||||
object_class->nickname = "magicksave_buffer";
|
||||
object_class->description = _( "save image to magick buffer" );
|
||||
object_class->build = vips_foreign_save_magick_buffer_build;
|
||||
|
||||
VIPS_ARG_BOXED( class, "buffer", 1,
|
||||
_( "Buffer" ),
|
||||
_( "Buffer to save to" ),
|
||||
VIPS_ARGUMENT_REQUIRED_OUTPUT,
|
||||
G_STRUCT_OFFSET( VipsForeignSaveMagickBuffer, buf ),
|
||||
VIPS_TYPE_BLOB );
|
||||
|
||||
VIPS_ARG_STRING( class, "format", 2,
|
||||
_( "Format" ),
|
||||
_( "Format to save in" ),
|
||||
VIPS_ARGUMENT_REQUIRED_INPUT,
|
||||
G_STRUCT_OFFSET( VipsForeignSaveMagickFile, format ),
|
||||
NULL );
|
||||
}
|
||||
|
||||
static void
|
||||
vips_foreign_save_magick_buffer_init( VipsForeignSaveMagickBuffer *buffer )
|
||||
{
|
||||
}
|
||||
|
||||
#endif /*HAVE_MAGICK || HAVE_MAGICK7*/
|
||||
|
@ -132,7 +132,10 @@ int vips__magick_read_buffer( const void *buf, const size_t len,
|
||||
int vips__magick_read_buffer_header( const void *buf, const size_t len,
|
||||
VipsImage *out, const char *density, int page, int n );
|
||||
|
||||
int vips__magick_write( VipsImage *in, const char *filename);
|
||||
int vips__magick_write( VipsImage *in, const char *filename,
|
||||
const char *format );
|
||||
int vips__magick_write_buf( VipsImage *in, void **obuf, size_t *olen,
|
||||
const char *format );
|
||||
|
||||
extern const char *vips__mat_suffs[];
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user