diff options
author | Thomas White <taw@physics.org> | 2023-05-05 11:36:22 +0200 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2023-05-05 11:51:09 +0200 |
commit | d8e17d6154ce644449ad5979eb6846e9e9816f6e (patch) | |
tree | aae2cc870ca3f7c0e3bcc1a8aa0de0ac4f7b3e38 | |
parent | 309b65d75993abbc9804b392117baea3a1101e3a (diff) |
Move is_{hdf5,cbf,cbfgz}_file() to image.c
This also improves is_hdf5_file() to use the proper file signature, not
just the extension. See 9040220452a9cc02706159c21c78e43b374446cf.
-rw-r--r-- | libcrystfel/src/image-cbf.c | 46 | ||||
-rw-r--r-- | libcrystfel/src/image-cbf.h | 4 | ||||
-rw-r--r-- | libcrystfel/src/image-hdf5.c | 13 | ||||
-rw-r--r-- | libcrystfel/src/image-hdf5.h | 2 | ||||
-rw-r--r-- | libcrystfel/src/image.c | 140 | ||||
-rw-r--r-- | libcrystfel/src/image.h | 5 |
6 files changed, 126 insertions, 84 deletions
diff --git a/libcrystfel/src/image-cbf.c b/libcrystfel/src/image-cbf.c index f8a15c1b..0fb3b61c 100644 --- a/libcrystfel/src/image-cbf.c +++ b/libcrystfel/src/image-cbf.c @@ -513,52 +513,6 @@ static float *read_cbf_data(const char *filename, int gz, int *w, int *h) } -signed int is_cbf_file(const char *filename) -{ - FILE *fh; - char line[1024]; - - fh = fopen(filename, "r"); - if ( fh == NULL ) return -1; - - if ( fgets(line, 1024, fh) == NULL ) { - fclose(fh); - return 0; - } - - fclose(fh); - - if ( strstr(line, "CBF") == NULL ) { - return 0; - } - - return 1; -} - - -signed int is_cbfgz_file(const char *filename) -{ - #ifdef HAVE_ZLIB - gzFile gzfh; - char line[1024]; - - gzfh = gzopen(filename, "rb"); - if ( gzfh == NULL ) return -1; - if ( gzgets(gzfh, line, 1024) == NULL ) return 0; - gzclose(gzfh); - - if ( strstr(line, "CBF") == NULL ) { - return 0; - } - - return 1; - - #else /* No zlib */ - return 0; - #endif -} - - int image_cbf_read_mask(struct panel_template *p, const char *filename, const char *event, int gz, int *bad, int mask_good, int mask_bad) diff --git a/libcrystfel/src/image-cbf.h b/libcrystfel/src/image-cbf.h index 5eca1062..44f682f1 100644 --- a/libcrystfel/src/image-cbf.h +++ b/libcrystfel/src/image-cbf.h @@ -34,10 +34,6 @@ #include "datatemplate_priv.h" -extern signed int is_cbf_file(const char *filename); - -extern signed int is_cbfgz_file(const char *filename); - 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); diff --git a/libcrystfel/src/image-hdf5.c b/libcrystfel/src/image-hdf5.c index c7c75903..dd9aecee 100644 --- a/libcrystfel/src/image-hdf5.c +++ b/libcrystfel/src/image-hdf5.c @@ -1993,17 +1993,4 @@ char **image_hdf5_expand_frames(const DataTemplate *dtempl, } -int is_hdf5_file(const char *filename) -{ - const char *ext = NULL; - - ext = filename_extension(filename, NULL); - if ( ext == NULL ) return 0; - - return ( (strcmp(ext, ".h5") == 0) - || (strcmp(ext, ".cxi") == 0) - || (strcmp(ext, ".nx5") == 0) ); -} - - #endif /* HAVE_HDF5 */ diff --git a/libcrystfel/src/image-hdf5.h b/libcrystfel/src/image-hdf5.h index cbb55e53..30064e96 100644 --- a/libcrystfel/src/image-hdf5.h +++ b/libcrystfel/src/image-hdf5.h @@ -65,6 +65,4 @@ extern char **image_hdf5_expand_frames(const DataTemplate *dtempl, const char *filename, int *n_frames); -extern int is_hdf5_file(const char *filename); - #endif /* IMAGE_HDF5_H */ diff --git a/libcrystfel/src/image.c b/libcrystfel/src/image.c index 1bc9fb52..c1c59eab 100644 --- a/libcrystfel/src/image.c +++ b/libcrystfel/src/image.c @@ -3,12 +3,11 @@ * * Handle images and image features * - * Copyright © 2012-2021 Deutsches Elektronen-Synchrotron DESY, + * Copyright © 2012-2023 Deutsches Elektronen-Synchrotron DESY, * a research centre of the Helmholtz Association. - * * Authors: * 2014 Kenneth Beyerlein <kenneth.beyerlein@desy.de> - * 2011-2021 Thomas White <taw@physics.org> + * 2011-2023 Thomas White <taw@physics.org> * * This file is part of CrystFEL. * @@ -36,6 +35,10 @@ #include <sys/stat.h> #include <fenv.h> +#ifdef HAVE_ZLIB +#include <zlib.h> +#endif + #include "image.h" #include "utils.h" #include "detgeom.h" @@ -50,12 +53,96 @@ /** \file image.h */ -#ifndef HAVE_HDF5 -int is_hdf5_file(const char *filename) +int is_hdf5_file(const char *filename, int *err) +{ + FILE *fh; + unsigned char bytes[8]; + unsigned char sig[8] = {137, 'H', 'D', 'F', '\r', '\n', 26, '\n'}; + size_t n; + int i; + + fh = fopen(filename, "r"); + if ( fh == NULL ) { + if ( err != NULL ) *err = 1; + return 0; + } + + n = fread(bytes, 1, 8, fh); + fclose(fh); + + if ( n != 8 ) { + if ( err != NULL ) *err = 1; + return 0; + } + + if ( err != NULL ) *err = 0; + + /* HDF5 superblock signature from the specification document */ + for ( i=0; i<8; i++ ) { + if ( bytes[i] != sig[i] ) return 0; + } + + return 1; +} + +int is_cbf_file(const char *filename, int *err) +{ + FILE *fh; + char line[1024]; + + fh = fopen(filename, "r"); + if ( fh == NULL ) { + if ( err != NULL ) *err = 1; + return 0; + } + + if ( fgets(line, 1024, fh) == NULL ) { + fclose(fh); + if ( err != NULL ) *err = 1; + return 0; + } + + fclose(fh); + + if ( err != NULL ) *err = 0; + if ( strncmp(line, "###CBF: VERSION", 15) == 0 ) { + return 1; + } else { + return 0; + } +} + + +int is_cbfgz_file(const char *filename, int *err) { + #ifdef HAVE_ZLIB + gzFile gzfh; + char line[1024]; + + gzfh = gzopen(filename, "rb"); + if ( gzfh == NULL ) { + if ( err != NULL ) *err = 1; + return 0; + } + if ( gzgets(gzfh, line, 1024) == NULL ) { + if ( err != NULL ) *err = 1; + return 0; + } + gzclose(gzfh); + + if ( err != NULL ) *err = 0; + if ( strncmp(line, "###CBF: VERSION", 15) == 0 ) { + return 1; + } else { + return 0; + } + + #else /* No zlib */ + if ( err != NULL ) *err = 1; return 0; + #endif } -#endif + struct _imagefeaturelist { @@ -462,24 +549,39 @@ int image_read_header_float(struct image *image, const char *from, double *val) static DataSourceType file_type(const char *filename) { + int err; + if ( !file_exists(filename) ) { ERROR("File not found: %s (file_type)\n", filename); return DATA_SOURCE_TYPE_NONE; } - if ( is_hdf5_file(filename) ) { + if ( is_hdf5_file(filename, &err) ) { return DATA_SOURCE_TYPE_HDF5; + } + if ( err ) { + ERROR("Couldn't check for HDF5 file: %s\n", filename); + return DATA_SOURCE_TYPE_NONE; + } - } else if ( is_cbf_file(filename) ) { + if ( is_cbf_file(filename, &err) ) { return DATA_SOURCE_TYPE_CBF; + } + if ( err ) { + ERROR("Couldn't check for CBF file: %s\n", filename); + return DATA_SOURCE_TYPE_NONE; + } - } else if ( is_cbfgz_file(filename) ) { + if ( is_cbfgz_file(filename, &err) ) { return DATA_SOURCE_TYPE_CBFGZ; - - } else { - ERROR("Unrecognised file type: %s (file_type)\n", filename); - return DATA_SOURCE_TYPE_UNKNOWN; } + if ( err ) { + ERROR("Couldn't check for CBF.gz file: %s\n", filename); + return DATA_SOURCE_TYPE_NONE; + } + + ERROR("Unrecognised file type: %s (file_type)\n", filename); + return DATA_SOURCE_TYPE_UNKNOWN; } @@ -815,17 +917,17 @@ static int load_mask(struct panel_template *p, unsigned int mask_good, unsigned int mask_bad) { - if ( is_hdf5_file(mask_fn) ) { + if ( is_hdf5_file(mask_fn, NULL) ) { #ifdef HAVE_HDF5 return image_hdf5_read_mask(p, mask_fn, ev, bad, mask_location, mask_good, mask_bad); #endif - } else if ( is_cbf_file(mask_fn) ) { + } else if ( is_cbf_file(mask_fn, NULL) ) { return image_cbf_read_mask(p, mask_fn, ev, 0, bad, mask_good, mask_bad); - } else if ( is_cbfgz_file(mask_fn) ) { + } else if ( is_cbfgz_file(mask_fn, NULL) ) { return image_cbf_read_mask(p, mask_fn, ev, 1, bad, mask_good, mask_bad); @@ -1006,7 +1108,7 @@ static int create_satmap(struct image *image, map_fn = p->satmap_file; } - if ( is_hdf5_file(map_fn) ) { + if ( is_hdf5_file(map_fn, NULL) ) { #ifdef HAVE_HDF5 image_hdf5_read_satmap(p, map_fn, image->ev, p->satmap); #endif @@ -1308,7 +1410,7 @@ ImageFeatureList *image_read_peaks(const DataTemplate *dtempl, const char *event, int half_pixel_shift) { - if ( is_hdf5_file(filename) ) { + if ( is_hdf5_file(filename, NULL) ) { #ifdef HAVE_HDF5 enum peak_layout layout; @@ -1366,7 +1468,7 @@ ImageFeatureList *image_read_peaks(const DataTemplate *dtempl, char **image_expand_frames(const DataTemplate *dtempl, const char *filename, int *n_frames) { - if ( is_hdf5_file(filename) ) { + if ( is_hdf5_file(filename, NULL) ) { #ifdef HAVE_HDF5 return image_hdf5_expand_frames(dtempl, filename, n_frames); diff --git a/libcrystfel/src/image.h b/libcrystfel/src/image.h index 3fbfff55..949ac7d1 100644 --- a/libcrystfel/src/image.h +++ b/libcrystfel/src/image.h @@ -185,6 +185,11 @@ struct image extern "C" { #endif +/* File classifiers */ +extern int is_hdf5_file(const char *filename, int *err); +extern int is_cbf_file(const char *filename, int *err); +extern int is_cbfgz_file(const char *filename, int *err); + /* Feature lists */ extern ImageFeatureList *image_feature_list_new(void); |