aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libcrystfel/src/image.c162
1 files changed, 39 insertions, 123 deletions
diff --git a/libcrystfel/src/image.c b/libcrystfel/src/image.c
index 20f53849..31125725 100644
--- a/libcrystfel/src/image.c
+++ b/libcrystfel/src/image.c
@@ -619,7 +619,7 @@ static float *convert_sint16_float(int16_t *data, int w, int h)
}
-static int read_cbf(struct imagefile *f, struct image *image)
+static float *read_cbf_data(struct imagefile *f, int *w, int *h, cbf_handle *pcbfh)
{
cbf_handle cbfh;
FILE *fh;
@@ -631,27 +631,22 @@ static int read_cbf(struct imagefile *f, struct image *image)
void *data;
float *dataf;
- if ( image->det == NULL ) {
- ERROR("read_cbf() needs a geometry\n");
- return 1;
- }
-
if ( cbf_make_handle(&cbfh) ) {
ERROR("Failed to allocate CBF handle\n");
- return 1;
+ return NULL;
}
fh = fopen(f->filename, "rb");
if ( fh == NULL ) {
ERROR("Failed to open '%s'\n", f->filename);
- return 1;
+ return NULL;
}
/* CBFlib calls fclose(fh) when it's ready */
if ( cbf_read_widefile(cbfh, fh, 0) ) {
ERROR("Failed to read CBF file '%s'\n", f->filename);
cbf_free_handle(cbfh);
- return 1;
+ return NULL;
}
/* Select row 0 in data column inside array_data */
@@ -672,46 +667,46 @@ static int read_cbf(struct imagefile *f, struct image *image)
ERROR("Failed to read CBF array parameters: %s\n", err);
free(err);
cbf_free_handle(cbfh);
- return 1;
+ return NULL;
}
if ( dimslow != 0 ) {
ERROR("CBF data array is 3D - don't know what to do with it\n");
cbf_free_handle(cbfh);
- return 1;
+ return NULL;
}
if ( dimfast*dimmid*elsize > 10e9 ) {
ERROR("CBF data is far too big (%i x %i x %i bytes).\n",
(int)dimfast, (int)dimmid, (int)elsize);
cbf_free_handle(cbfh);
- return 1;
+ return NULL;
}
if ( (elsize != 4) && (elsize != 2) ) {
STATUS("Don't know what to do with element size %i\n",
(int)elsize);
cbf_free_handle(cbfh);
- return 1;
+ return NULL;
}
if ( !elsigned ) {
STATUS("Don't know what to do with unsigned data (yet)\n");
cbf_free_handle(cbfh);
- return 1;
+ return NULL;
}
if ( strcmp(byteorder, "little_endian") != 0 ) {
STATUS("Don't know what to do with non-little-endian datan\n");
cbf_free_handle(cbfh);
- return 1;
+ return NULL;
}
data = malloc(elsize*dimfast*dimmid);
if ( data == NULL ) {
ERROR("Failed to allocate memory for CBF data\n");
cbf_free_handle(cbfh);
- return 1;
+ return NULL;
}
r = cbf_get_integerarray(cbfh, &binary_id, data, elsize, 1,
@@ -721,7 +716,7 @@ static int read_cbf(struct imagefile *f, struct image *image)
ERROR("Failed to read CBF array: %s\n", err);
free(err);
cbf_free_handle(cbfh);
- return 1;
+ return NULL;
}
if ( elsize == 4 ) {
@@ -730,10 +725,29 @@ static int read_cbf(struct imagefile *f, struct image *image)
if ( elsize == 2 ) {
dataf = convert_sint16_float(data, dimfast, dimmid);
}
+ free(data);
- unpack_panels(image, dataf, dimfast, dimmid);
+ *w = dimfast;
+ *h = dimmid;
+ *pcbfh = cbfh;
+ return dataf;
+}
+
+
+static int read_cbf(struct imagefile *f, struct image *image)
+{
+ float *data;
+ int w, h;
+ cbf_handle cbfh;
+
+ data = read_cbf_data(f, &w, &h, &cbfh);
+ if ( data == NULL ) {
+ ERROR("Failed to read CBF data\n");
+ return 1;
+ }
+
+ unpack_panels(image, data, w, h);
free(data);
- free(dataf);
if ( image->beam != NULL ) {
cbf_fill_in_beam_parameters(image->beam, f, image);
@@ -753,122 +767,24 @@ static int read_cbf(struct imagefile *f, struct image *image)
static int read_cbf_simple(struct imagefile *f, struct image *image)
{
+ float *data;
+ int w, h;
cbf_handle cbfh;
- FILE *fh;
- int r;
- unsigned int compression;
- int binary_id, minelement, maxelement, elsigned, elunsigned;
- size_t elsize, elements, elread, dimfast, dimmid, dimslow, padding;
- const char *byteorder;
- void *data;
-
- if ( cbf_make_handle(&cbfh) ) {
- ERROR("Failed to allocate CBF handle\n");
- return 1;
- }
-
- fh = fopen(f->filename, "rb");
- if ( fh == NULL ) {
- ERROR("Failed to open '%s'\n", f->filename);
- return 1;
- }
- /* CBFlib calls fclose(fh) when it's ready */
- if ( cbf_read_widefile(cbfh, fh, 0) ) {
- ERROR("Failed to read CBF file '%s'\n", f->filename);
- cbf_free_handle(cbfh);
- return 1;
- }
-
- /* Select row 0 in data column inside array_data */
- cbf_find_category(cbfh, "array_data");
- cbf_find_column(cbfh, "data");
- cbf_select_row(cbfh, 0);
-
- /* Get parameters for array read */
- r = cbf_get_integerarrayparameters_wdims(cbfh, &compression, &binary_id,
- &elsize, &elsigned, &elunsigned,
- &elements,
- &minelement, &maxelement,
- &byteorder,
- &dimfast, &dimmid, &dimslow,
- &padding);
- if ( r ) {
- char *err = cbf_strerr(r);
- ERROR("Failed to read CBF array parameters: %s\n", err);
- free(err);
- cbf_free_handle(cbfh);
- return 1;
- }
-
- if ( dimslow != 0 ) {
- ERROR("CBF data array is 3D - don't know what to do with it\n");
- cbf_free_handle(cbfh);
- return 1;
- }
-
- if ( dimfast*dimmid*elsize > 10e9 ) {
- ERROR("CBF data is far too big (%i x %i x %i bytes).\n",
- (int)dimfast, (int)dimmid, (int)elsize);
- cbf_free_handle(cbfh);
- return 1;
- }
-
- if ( (elsize != 4) && (elsize != 2) ) {
- STATUS("Don't know what to do with element size %i\n",
- (int)elsize);
- cbf_free_handle(cbfh);
- return 1;
- }
-
- if ( !elsigned ) {
- STATUS("Don't know what to do with unsigned data (yet)\n");
- cbf_free_handle(cbfh);
- return 1;
- }
-
- if ( strcmp(byteorder, "little_endian") != 0 ) {
- STATUS("Don't know what to do with non-little-endian data\n");
- cbf_free_handle(cbfh);
- return 1;
- }
-
- data = malloc(elsize*dimfast*dimmid);
+ data = read_cbf_data(f, &w, &h, &cbfh);
if ( data == NULL ) {
- ERROR("Failed to allocate memory for CBF data\n");
- cbf_free_handle(cbfh);
- return 1;
- }
-
- r = cbf_get_integerarray(cbfh, &binary_id, data, elsize, 1,
- elsize*dimfast*dimmid, &elread);
- if ( r ) {
- char *err = cbf_strerr(r);
- ERROR("Failed to read CBF array: %s\n", err);
- free(err);
- cbf_free_handle(cbfh);
+ ERROR("Failed to read CBF data\n");
return 1;
}
- image->det = simple_geometry(image, dimfast, dimmid);
+ image->det = simple_geometry(image, w, h);
image->dp = malloc(sizeof(float *));
if ( image->dp == NULL ) {
ERROR("Failed to allocate dp array\n");
return 1;
}
- if ( elsize == 4 ) {
- image->dp[0] = convert_sint32_float(data, dimfast, dimmid);
- }
- if ( elsize == 2 ) {
- image->dp[0] = convert_sint16_float(data, dimfast, dimmid);
- }
- free(data);
-
- if ( image->dp[0] == NULL ) {
- ERROR("Failed to allocate dp array\n");
- return 1;
- }
+ image->dp[0] = data;
if ( image->beam != NULL ) {
cbf_fill_in_beam_parameters(image->beam, f, image);