From f7ea70e422fafdc886d5829d1bb5f9f3e75d726f Mon Sep 17 00:00:00 2001 From: Thomas White Date: Fri, 10 Jul 2020 11:57:47 +0200 Subject: Move image structure creation up to image_read() --- libcrystfel/src/image-cbf.c | 20 ++++++++------------ libcrystfel/src/image-cbf.h | 8 +++++--- libcrystfel/src/image-hdf5.c | 19 +++++++------------ libcrystfel/src/image-hdf5.h | 7 ++++--- libcrystfel/src/image.c | 20 +++++++++++++++----- 5 files changed, 39 insertions(+), 35 deletions(-) diff --git a/libcrystfel/src/image-cbf.c b/libcrystfel/src/image-cbf.c index 5b27c5a5..0d7b914d 100644 --- a/libcrystfel/src/image-cbf.c +++ b/libcrystfel/src/image-cbf.c @@ -599,28 +599,24 @@ static int unpack_panels(struct image *image, DataTemplate *dtempl, } -struct image *image_cbf_read(DataTemplate *dtempl, const char *filename, - const char *event, int gz) +int image_cbf_read(struct image *image, + DataTemplate *dtempl, + const char *filename, + const char *event, + int gz) { - struct image *image; float *data; int w, h; if ( access(filename, R_OK) == -1 ) { ERROR("File does not exist or cannot be read: %s\n", filename); - return NULL; - } - - image = image_new(); - if ( image == NULL ) { - ERROR("Couldn't allocate image structure.\n"); - return NULL; + return 1; } data = read_cbf_data(filename, gz, &w, &h); if ( data == NULL ) { ERROR("Failed to read CBF data\n"); - return NULL; + return 1; } unpack_panels(image, dtempl, data, w, h); @@ -630,5 +626,5 @@ struct image *image_cbf_read(DataTemplate *dtempl, const char *filename, //cbf_fill_in_clen(image->det, f); //fill_in_adu(image); - return image; + return 0; } diff --git a/libcrystfel/src/image-cbf.h b/libcrystfel/src/image-cbf.h index 400d5132..6cb93d83 100644 --- a/libcrystfel/src/image-cbf.h +++ b/libcrystfel/src/image-cbf.h @@ -46,9 +46,11 @@ extern int load_mask_cbf(struct panel_template *p, const char *filename, const char *event, int gz, int *bad, int mask_good, int mask_bad); -extern struct image *image_cbf_read(DataTemplate *dtempl, - const char *filename, - const char *event, int gz); +extern int image_cbf_read(struct image *image, + DataTemplate *dtempl, + const char *filename, + const char *event, + int gz); extern int image_cbf_read_mask(struct panel_template *p, const char *filename, const char *event, diff --git a/libcrystfel/src/image-hdf5.c b/libcrystfel/src/image-hdf5.c index 5461c8a5..5b7a9728 100644 --- a/libcrystfel/src/image-hdf5.c +++ b/libcrystfel/src/image-hdf5.c @@ -525,23 +525,18 @@ static int load_hdf5_hyperslab(struct panel_template *p, } -struct image *image_hdf5_read(DataTemplate *dtempl, - const char *filename, const char *event) +int image_hdf5_read(struct image *image, + DataTemplate *dtempl, + const char *filename, + const char *event) { - struct image *image; int i; - image = image_new(); - if ( image == NULL ) { - ERROR("Couldn't allocate image structure.\n"); - return NULL; - } - image->dp = malloc(dtempl->n_panels*sizeof(float *)); if ( image->dp == NULL ) { ERROR("Failed to allocate data array.\n"); image_free(image); - return NULL; + return 1; } if ( event == NULL ) { @@ -560,14 +555,14 @@ struct image *image_hdf5_read(DataTemplate *dtempl, { ERROR("Failed to load panel data\n"); image_free(image); - return NULL; + return 1; } } image->filename = strdup(filename); image->ev = safe_strdup(event); - return image; + return 0; } diff --git a/libcrystfel/src/image-hdf5.h b/libcrystfel/src/image-hdf5.h index 7a32a354..af57e3d4 100644 --- a/libcrystfel/src/image-hdf5.h +++ b/libcrystfel/src/image-hdf5.h @@ -42,9 +42,10 @@ extern double image_hdf5_get_value(const char *from, const char *filename, const char *ev); -extern struct image *image_hdf5_read(DataTemplate *dtempl, - const char *filename, - const char *event); +extern int image_hdf5_read(struct image *image, + DataTemplate *dtempl, + const char *filename, + const char *event); extern int image_hdf5_read_mask(struct panel_template *p, const char *filename, diff --git a/libcrystfel/src/image.c b/libcrystfel/src/image.c index b70b920f..b9450fc0 100644 --- a/libcrystfel/src/image.c +++ b/libcrystfel/src/image.c @@ -479,27 +479,37 @@ struct image *image_read(DataTemplate *dtempl, const char *filename, { struct image *image; int i; + int r; if ( dtempl == NULL ) { ERROR("NULL data template!\n"); return NULL; } + image = image_new(); + if ( image == NULL ) { + ERROR("Couldn't allocate image structure.\n"); + return NULL; + } + if ( is_hdf5_file(filename) ) { - image = image_hdf5_read(dtempl, filename, event); + r = image_hdf5_read(image, dtempl, filename, event); } else if ( is_cbf_file(filename) ) { - image = image_cbf_read(dtempl, filename, event, 0); + r = image_cbf_read(image, dtempl, filename, event, 0); } else if ( is_cbfgz_file(filename) ) { - image = image_cbf_read(dtempl, filename, event, 1); + r = image_cbf_read(image, dtempl, filename, event, 1); } else { ERROR("Unrecognised file type: %s\n", filename); - return NULL; + r = 1; } - if ( image == NULL ) return NULL; + if ( r ) { + image_free(image); + return NULL; + } /* Wavelength might be needed to create detgeom (adu_per_eV) */ image->lambda = convert_to_m(get_value(image, -- cgit v1.2.3