python new_from_memory()/write_to_memory() done

This commit is contained in:
John Cupitt 2014-09-05 12:54:07 +01:00
parent 22e075d05a
commit 92570b119c
9 changed files with 80 additions and 55 deletions

12
TODO
View File

@ -1,22 +1,12 @@
- python:
- new_from_memory() needs length as well as data so we can pass it a
string
- do write_to_memory as well
- something to make a mask from a list of lists
- set scale and offset on masko
- set scale and offset on mask
- how about the in-place things? draw a circle?
- add more constructors
- need more writers
- could import like this:
from gi.repository import Vips

View File

@ -340,7 +340,9 @@ vips_falsecolour_build( VipsObject *object )
build( object ) )
return( -1 );
if( !(t[0] = vips_image_new_from_memory( (void *) vips_falsecolour_pet,
if( !(t[0] = vips_image_new_from_memory(
(void *) vips_falsecolour_pet,
sizeof( vips_falsecolour_pet ),
1, VIPS_NUMBER( vips_falsecolour_pet ), 3,
VIPS_FORMAT_UCHAR )) )
return( -1 );

View File

@ -304,7 +304,7 @@ vips__ink_to_vector( const char *domain, VipsImage *im, VipsPel *ink, int *n )
/* Wrap a VipsImage around ink.
*/
t[0] = vips_image_new_from_memory( ink,
t[0] = vips_image_new_from_memory( ink, VIPS_IMAGE_SIZEOF_PEL( im ),
1, 1, VIPS_IMAGE_SIZEOF_PEL( im ), VIPS_FORMAT_UCHAR );
if( vips_copy( t[0], &t[1],
"bands", im->Bands,

View File

@ -1031,6 +1031,7 @@ strip_init( Strip *strip, Layer *layer )
if( !(strip->image = vips_image_new_from_memory(
VIPS_REGION_ADDR( layer->strip, 0, line.top ),
VIPS_IMAGE_SIZEOF_LINE( layer->image ) * line.height,
line.width, line.height,
layer->image->Bands, layer->image->BandFmt )) ) {
strip_free( strip );

View File

@ -416,8 +416,8 @@ VipsImage *vips_image_new_from_file( const char *name, ... )
VipsImage *vips_image_new_from_file_RW( const char *filename );
VipsImage *vips_image_new_from_file_raw( const char *filename,
int xsize, int ysize, int bands, guint64 offset );
VipsImage *vips_image_new_from_memory( void *buffer,
int xsize, int ysize, int bands, VipsBandFormat bandfmt );
VipsImage *vips_image_new_from_memory( void *buffer, size_t size,
int width, int height, int bands, VipsBandFormat format );
VipsImage *vips_image_new_from_buffer( void *buf, size_t len,
const char *option_string, ... )
__attribute__((sentinel));
@ -431,9 +431,9 @@ int vips_image_write( VipsImage *image, VipsImage *out );
int vips_image_write_to_file( VipsImage *image, const char *name, ... )
__attribute__((sentinel));
int vips_image_write_to_buffer( VipsImage *in,
const char *suffix, void **buf, size_t *len, ... )
const char *suffix, void **buf, size_t *size, ... )
__attribute__((sentinel));
int vips_image_write_to_memory( VipsImage *in, void **buf, size_t *len );
void *vips_image_write_to_memory( VipsImage *in, size_t *size );
int vips_image_decode_predict( VipsImage *in,
int *bands, VipsBandFormat *format );

View File

@ -214,7 +214,9 @@ int im_cp_descv (IMAGE * im, ...);
int im_demand_hint (IMAGE * im, VipsDemandStyle hint, ...);
#define im_demand_hint_array( A, B, C ) (vips__demand_hint_array( A, B, C ), 0)
#define im_image vips_image_new_from_memory
#define im_image(P, W, H, B, F) \
vips_image_new_from_memory((P), 0, (W), (H), (B), (F))
#define im_binfile vips_image_new_from_file_raw
#define im__open_temp vips_image_new_temp_file
#define im__test_kill( I ) (vips_image_get_kill( I ))

View File

@ -1944,11 +1944,12 @@ vips_image_new_from_file_raw( const char *filename,
/**
* vips_image_new_from_memory:
* @buffer: start of memory area
* @xsize: image width
* @ysize: image height
* @buffer: (array length=size) (element-type guint8) (transfer full): start of memory area
* @size: length of memory area
* @width: image width
* @height: image height
* @bands: image bands (or bytes per pixel)
* @bandfmt: image format
* @format: image format
*
* This function wraps a #VipsImage around a memory area. The memory area
* must be a simple array, for example RGBRGBRGB, left-to-right,
@ -1966,8 +1967,8 @@ vips_image_new_from_file_raw( const char *filename,
* Returns: the new #VipsImage, or %NULL on error.
*/
VipsImage *
vips_image_new_from_memory( void *buffer,
int xsize, int ysize, int bands, VipsBandFormat bandfmt )
vips_image_new_from_memory( void *buffer, size_t size,
int width, int height, int bands, VipsBandFormat format )
{
VipsImage *image;
@ -1978,16 +1979,29 @@ vips_image_new_from_memory( void *buffer,
"filename", vips_image_temp_name(),
"mode", "m",
"foreign_buffer", buffer,
"width", xsize,
"height", ysize,
"width", width,
"height", height,
"bands", bands,
"format", bandfmt,
"format", format,
NULL );
if( vips_object_build( VIPS_OBJECT( image ) ) ) {
VIPS_UNREF( image );
return( NULL );
}
/* Allow len == 0 meaning don't check. Used for im_image()
* compatibility.
*/
if( size > 0 &&
size < VIPS_IMAGE_SIZEOF_IMAGE( image ) ) {
vips_error( "VipsImage",
_( "buffer too small --- "
"should be %zd bytes, you passed %zd" ),
VIPS_IMAGE_SIZEOF_IMAGE( image ), size );
VIPS_UNREF( image );
return( NULL );
}
return( image );
}
@ -2292,8 +2306,8 @@ vips_image_write_to_file( VipsImage *image, const char *name, ... )
* vips_image_write_to_buffer:
* @in: image to write
* @suffix: format to write
* @buf: return buffer start here
* @len: return buffer length here
* @buf: (array length=size) (element-type guint8) (transfer full): return buffer start here
* @size: return buffer length here
* @...: %NULL-terminated list of optional named arguments
*
* Writes @in to a memory buffer in a format specified by @suffix.
@ -2313,7 +2327,7 @@ vips_image_write_to_file( VipsImage *image, const char *name, ... )
*/
int
vips_image_write_to_buffer( VipsImage *in,
const char *suffix, void **buf, size_t *len,
const char *suffix, void **buf, size_t *size,
... )
{
char filename[VIPS_PATH_MAX];
@ -2327,7 +2341,7 @@ vips_image_write_to_buffer( VipsImage *in,
if( !(operation_name = vips_foreign_find_save_buffer( filename )) )
return( -1 );
va_start( ap, len );
va_start( ap, size );
result = vips_call_split_option_string( operation_name, option_string,
ap, in, &blob );
va_end( ap );
@ -2337,8 +2351,8 @@ vips_image_write_to_buffer( VipsImage *in,
*buf = VIPS_AREA( blob )->data;
VIPS_AREA( blob )->free_fn = NULL;
}
if( len )
*len = VIPS_AREA( blob )->length;
if( size )
*size = VIPS_AREA( blob )->length;
vips_area_unref( VIPS_AREA( blob ) );
}
@ -2349,51 +2363,47 @@ vips_image_write_to_buffer( VipsImage *in,
/**
* vips_image_write_to_memory:
* @in: image to write
* @buf: (transfer full): return buffer start here
* @len: return buffer length here
* @size: return buffer length here
*
* Writes @in to memory as a simple, unformatted C-style array.
*
* The caller is responsible for freeing memory.
* The caller is responsible for freeing this memory.
*
* See also: vips_image_write_to_buffer().
*
* Returns: 0 on success, -1 on error
* Returns: (array length=size) (element-type guint8) (transfer full): return buffer start here
*/
int
vips_image_write_to_memory( VipsImage *in, void **buf_out, size_t *len_out )
void *
vips_image_write_to_memory( VipsImage *in, size_t *size_out )
{
void *buf;
size_t len;
size_t size;
VipsImage *x;
g_assert( buf_out );
len = VIPS_IMAGE_SIZEOF_IMAGE( in );
if( !(buf = g_try_malloc( len )) ) {
size = VIPS_IMAGE_SIZEOF_IMAGE( in );
if( !(buf = g_try_malloc( size )) ) {
vips_error( "vips_image_write_to_memory",
_( "out of memory --- size == %dMB" ),
(int) (len / (1024.0 * 1024.0)) );
(int) (size / (1024.0 * 1024.0)) );
vips_warn( "vips_image_write_to_memory",
_( "out of memory --- size == %dMB" ),
(int) (len / (1024.0*1024.0)) );
return( -1 );
(int) (size / (1024.0*1024.0)) );
return( NULL );
}
x = vips_image_new_from_memory( buf,
x = vips_image_new_from_memory( buf, size,
in->Xsize, in->Ysize, in->Bands, in->BandFmt );
if( vips_image_write( in, x ) ) {
g_object_unref( x );
g_free( buf );
return( -1 );
return( NULL );
}
g_object_unref( x );
*buf_out = buf;
if( len_out )
*len_out = len;
if( size_out )
*size_out = size;
return( 0 );
return( buf );
}
/**

17
python/try7.py Executable file
View File

@ -0,0 +1,17 @@
#!/usr/bin/python
import sys
#import logging
#logging.basicConfig(level = logging.DEBUG)
from gi.repository import Vips
from vips8 import vips
a = Vips.Image.new_from_file(sys.argv[1])
b = a.write_to_memory()
c = Vips.Image.new_from_memory(b, a.width, a.height, a.bands, a.bandfmt)
c.write_to_file("x.v")

View File

@ -218,8 +218,6 @@ def vips_image_new_from_buffer(cls, data, option_string, **kwargs):
raise Error('No known loader for buffer.')
logging.debug('Image.new_from_buffer: loader = %s' % loader)
return _call_base(loader, [data], kwargs, None, option_string)
def vips_image_getattr(self, name):
logging.debug('Image.__getattr__ %s' % name)
@ -227,6 +225,10 @@ def vips_image_getattr(self, name):
if name in dir(self.props):
return getattr(self.props, name)
# 'format' is a reserved word, annoying, have bandfmt as an alias
if name == 'bandfmt':
return getattr(self.props, 'format')
return lambda *args, **kwargs: _call_instance(self, name, args, kwargs)
def vips_image_write_to_file(self, vips_filename, **kwargs):
@ -366,6 +368,7 @@ setattr(Vips.Image, 'new_from_buffer', classmethod(vips_image_new_from_buffer))
# instance methods
Vips.Image.write_to_file = vips_image_write_to_file
Vips.Image.write_to_buffer = vips_image_write_to_buffer
# we can use Vips.Image.write_to_memory() directly
Vips.Image.floor = vips_floor