Skip to content

Commit

Permalink
jpg stream output passes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jcupitt committed Oct 12, 2019
1 parent d991b73 commit a2d5718
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 226 deletions.
1 change: 0 additions & 1 deletion .gitignore
@@ -1,4 +1,3 @@
.pytest_cache
compile
.pytest_cache
a.out
Expand Down
2 changes: 2 additions & 0 deletions libvips/include/vips/type.h
Expand Up @@ -161,6 +161,8 @@ VipsBlob *vips_blob_new( VipsCallbackFn free_fn,
const void *data, size_t length );
VipsBlob *vips_blob_copy( const void *data, size_t length );
const void *vips_blob_get( VipsBlob *blob, size_t *length );
void vips_blob_set( VipsBlob *blob,
VipsCallbackFn free_fn, const void *data, size_t length );
GType vips_blob_get_type(void);

/**
Expand Down
24 changes: 16 additions & 8 deletions libvips/iofuncs/stream.c
Expand Up @@ -611,6 +611,10 @@ vips_stream_output_finalize( GObject *gobject )
VIPS_DEBUG_MSG( "vips_stream_output_finalize:\n" );

VIPS_FREEF( g_byte_array_unref, output->memory );
if( output->blob ) {
vips_area_unref( VIPS_AREA( output->blob ) );
output->blob = NULL;
}

G_OBJECT_CLASS( vips_stream_output_parent_class )->finalize( gobject );
}
Expand Down Expand Up @@ -639,7 +643,10 @@ vips_stream_output_build( VipsObject *object )

int fd;

if( (fd = vips_tracked_open( filename, MODE_WRITE )) == -1 ) {
/* 0644 is rw user, r group and other.
*/
if( (fd = vips_tracked_open( filename,
MODE_WRITE, 0644 )) == -1 ) {
vips_error_system( errno, STREAM_NAME( stream ),
"%s", _( "unable to open for write" ) );
return( -1 );
Expand Down Expand Up @@ -696,18 +703,22 @@ vips_stream_output_class_init( VipsStreamOutputClass *class )

class->write = vips_stream_output_write_real;

/* SET_ALWAYS means that blob is set by C and the obj system is not
* involved in creation or destruction. It can be read at any time.
*/
VIPS_ARG_BOXED( class, "blob", 1,
_( "Blob" ),
_( "Blob to save to" ),
VIPS_ARGUMENT_OPTIONAL_OUTPUT,
VIPS_ARGUMENT_SET_ALWAYS,
G_STRUCT_OFFSET( VipsStreamOutput, blob ),
VIPS_TYPE_BLOB );

}

static void
vips_stream_output_init( VipsStreamOutput *stream )
vips_stream_output_init( VipsStreamOutput *output )
{
output->blob = vips_blob_new( NULL, NULL, 0 );
}

/**
Expand Down Expand Up @@ -849,11 +860,8 @@ vips_stream_output_finish( VipsStreamOutput *output )
length = output->memory->len;
data = g_byte_array_free( output->memory, FALSE );
output->memory = NULL;

g_object_set( output,
"blob", vips_blob_new( (VipsCallbackFn) g_free,
data, length ),
NULL );
vips_blob_set( output->blob,
(VipsCallbackFn) g_free, data, length );
}

vips_stream_close( VIPS_STREAM( output ) );
Expand Down
48 changes: 43 additions & 5 deletions libvips/iofuncs/type.c
Expand Up @@ -171,6 +171,18 @@ vips_area_copy( VipsArea *area )
return( area );
}

void
vips_area_free( VipsArea *area )
{
if( area->free_fn &&
area->data ) {
area->free_fn( area->data, area );
area->free_fn = NULL;
}

area->data = NULL;
}

void
vips_area_unref( VipsArea *area )
{
Expand All @@ -191,11 +203,7 @@ vips_area_unref( VipsArea *area )
}

if( area->count == 0 ) {
if( area->free_fn && area->data ) {
area->free_fn( area->data, area );
area->data = NULL;
area->free_fn = NULL;
}
vips_area_free( area );

g_mutex_unlock( area->lock );

Expand Down Expand Up @@ -671,6 +679,36 @@ vips_blob_get( VipsBlob *blob, size_t *length )
length, NULL, NULL, NULL ) );
}

/* vips_blob_set:
* @blob: #VipsBlob to set
* @free_fn: (scope async) (allow-none): @data will be freed with this function
* @data: (array length=length) (element-type guint8) (transfer full): data to store
* @length: number of bytes in @data
*
* Any old data is freed and new data attached.
*
* It's sometimes useful to be able to create blobs as empty and then fill
* them later.
*
* See also: vips_blob_new().
*/
void
vips_blob_set( VipsBlob *blob,
VipsCallbackFn free_fn, const void *data, size_t length )
{
VipsArea *area = VIPS_AREA( blob );

g_mutex_lock( area->lock );

vips_area_free( area );

area->free_fn = free_fn;
area->length = length;
area->data = (void *) data;

g_mutex_unlock( area->lock );
}

/* Transform a blob to a G_TYPE_STRING.
*/
static void
Expand Down
4 changes: 2 additions & 2 deletions libvips/iofuncs/vips.c
Expand Up @@ -208,7 +208,7 @@ vips__open_image_write( const char *filename, gboolean temp )

g_info( "vips__open_image_write: opening with O_TMPFILE" );
dirname = g_path_get_dirname( filename );
fd = vips_tracked_open( dirname, O_TMPFILE | O_RDWR , 0666 );
fd = vips_tracked_open( dirname, O_TMPFILE | O_RDWR , 0644 );
g_free( dirname );

if( fd < 0 )
Expand All @@ -230,7 +230,7 @@ vips__open_image_write( const char *filename, gboolean temp )

if( fd < 0 ) {
g_info( "vips__open_image_write: simple open" );
fd = vips_tracked_open( filename, flags, 0666 );
fd = vips_tracked_open( filename, flags, 0644 );
}

if( fd < 0 ) {
Expand Down
1 change: 1 addition & 0 deletions test/.gitignore
@@ -0,0 +1 @@
test_descriptors
210 changes: 0 additions & 210 deletions test/test_descriptors

This file was deleted.

0 comments on commit a2d5718

Please sign in to comment.