diff options
author | Thomas White <taw@physics.org> | 2018-09-20 16:36:03 +0200 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2018-09-20 16:37:04 +0200 |
commit | ffe31a3582c4b65084ad7bfb0a5833b5ac83d58d (patch) | |
tree | 5e3a838a3f7946b83f20b304a9710fffc4d460ef /libcrystfel | |
parent | f2f18a07828d5299c082b84f8bdb7efee8144699 (diff) |
Allow cbf.gz buffer to grow if the file is large
Diffstat (limited to 'libcrystfel')
-rw-r--r-- | libcrystfel/src/image.c | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/libcrystfel/src/image.c b/libcrystfel/src/image.c index a51f415a..cbefae4f 100644 --- a/libcrystfel/src/image.c +++ b/libcrystfel/src/image.c @@ -51,9 +51,6 @@ #include "hdf5-file.h" #include "detector.h" -/* Maximum size of uncompressed CBF file in gzip format */ -#define GZIP_BUFFER_SIZE (16*1024*1024) - /** * SECTION:image * @short_description: Data structure representing an image @@ -647,7 +644,9 @@ static float *read_cbf_data(struct imagefile *f, int *w, int *h, cbf_handle *pcb } else if ( f->type == IMAGEFILE_CBFGZ ) { gzFile gzfh; - size_t len; + size_t len, len_read; + const size_t bufinc = 8*1024*1024; /* Allocate buffer in 8Mb chunks */ + size_t bufsz = bufinc; gzfh = gzopen(f->filename, "rb"); if ( gzfh == NULL ) return NULL; @@ -655,11 +654,23 @@ static float *read_cbf_data(struct imagefile *f, int *w, int *h, cbf_handle *pcb /* Set larger buffer size for hopefully faster uncompression */ gzbuffer(gzfh, 128*1024); - buf = malloc(GZIP_BUFFER_SIZE); + buf = malloc(bufsz); if ( buf == NULL ) return NULL; - len = gzread(gzfh, buf, GZIP_BUFFER_SIZE); - if ( len == -1 ) return NULL; + len = 0; + do { + + len_read = gzread(gzfh, buf+len, bufinc); + if ( len_read == -1 ) return NULL; + len += len_read; + + if ( len_read == bufinc ) { + bufsz += bufinc; + buf = realloc(buf, bufsz); + if ( buf == NULL ) return NULL; + } + + } while ( len_read == bufinc ); fh = fmemopen(buf, len, "rb"); if ( fh == NULL ) return NULL; |