aboutsummaryrefslogtreecommitdiff
path: root/libcrystfel/src/image.c
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2020-05-20 22:42:49 +0200
committerThomas White <taw@physics.org>2020-07-29 18:42:57 +0200
commit23ea67dc03ac19f7a1457ecfdc8d5ee9cac68632 (patch)
treec9cf8cbc343b842c3da3803d67343f73071b96dd /libcrystfel/src/image.c
parent302de26924528b31a2320c90fd944224674bd835 (diff)
Port indexamajig to new API, part II
This also removes a large chunk of legacy code
Diffstat (limited to 'libcrystfel/src/image.c')
-rw-r--r--libcrystfel/src/image.c439
1 files changed, 0 insertions, 439 deletions
diff --git a/libcrystfel/src/image.c b/libcrystfel/src/image.c
index 3ba71118..c7d46978 100644
--- a/libcrystfel/src/image.c
+++ b/libcrystfel/src/image.c
@@ -48,14 +48,6 @@
/** \file image.h */
-struct imagefile
-{
- enum imagefile_type type;
- char *filename;
- struct hdfile *hdfile;
-};
-
-
struct _imagefeaturelist
{
struct imagefeature *features;
@@ -331,233 +323,6 @@ void free_all_crystals(struct image *image)
}
-/**************************** Image field lists *******************************/
-
-struct imagefile_field_list
-{
- char **fields;
- int n_fields;
- int max_fields;
-};
-
-
-struct imagefile_field_list *new_imagefile_field_list()
-{
- struct imagefile_field_list *n;
-
- n = calloc(1, sizeof(struct imagefile_field_list));
- if ( n == NULL ) return NULL;
-
- n->max_fields = 32;
- n->fields = malloc(n->max_fields*sizeof(char *));
- if ( n->fields == NULL ) {
- free(n);
- return NULL;
- }
-
- return n;
-}
-
-
-void free_imagefile_field_list(struct imagefile_field_list *n)
-{
- int i;
- for ( i=0; i<n->n_fields; i++ ) {
- free(n->fields[i]);
- }
- free(n->fields);
- free(n);
-}
-
-
-void add_imagefile_field(struct imagefile_field_list *copyme, const char *name)
-{
- int i;
-
- /* Already on the list? Don't re-add if so. */
- for ( i=0; i<copyme->n_fields; i++ ) {
- if ( strcmp(copyme->fields[i], name) == 0 ) return;
- }
-
- /* Need more space? */
- if ( copyme->n_fields == copyme->max_fields ) {
-
- char **nfields;
- int nmax = copyme->max_fields + 32;
-
- nfields = realloc(copyme->fields, nmax*sizeof(char *));
- if ( nfields == NULL ) {
- ERROR("Failed to allocate space for new HDF5 field.\n");
- return;
- }
-
- copyme->max_fields = nmax;
- copyme->fields = nfields;
-
- }
-
- copyme->fields[copyme->n_fields] = strdup(name);
- if ( copyme->fields[copyme->n_fields] == NULL ) {
- ERROR("Failed to add field for copying '%s'\n", name);
- return;
- }
-
- copyme->n_fields++;
-}
-
-
-/******************************* CBF files ************************************/
-
-static int unpack_panels(struct image *image, float *data, int data_width,
- int data_height)
-{
- int pi;
-
- /* FIXME: Load these masks from an HDF5 file, if filenames are
- * given in the geometry file */
- uint16_t *flags = NULL;
- float *sat = NULL;
-
- image->dp = malloc(image->det->n_panels * sizeof(float *));
- image->bad = malloc(image->det->n_panels * sizeof(int *));
- image->sat = malloc(image->det->n_panels * sizeof(float *));
- if ( (image->dp == NULL) || (image->bad == NULL)
- || (image->sat == NULL) )
- {
- ERROR("Failed to allocate panels.\n");
- return 1;
- }
-
- for ( pi=0; pi<image->det->n_panels; pi++ ) {
-
- struct panel *p;
- int fs, ss;
-
- p = &image->det->panels[pi];
- image->dp[pi] = malloc(p->w*p->h*sizeof(float));
- image->bad[pi] = calloc(p->w*p->h, sizeof(int));
- image->sat[pi] = malloc(p->w*p->h*sizeof(float));
- if ( (image->dp[pi] == NULL) || (image->bad[pi] == NULL)
- || (image->sat[pi] == NULL) )
- {
- ERROR("Failed to allocate panel\n");
- return 1;
- }
-
- if ( p->mask != NULL ) {
- ERROR("WARNING: Bad pixel masks do not currently work "
- "with CBF files\n");
- ERROR(" (bad pixel regions specified in the geometry "
- "file will be used, however)\n");
- }
-
- if ( p->satmap != NULL ) {
- ERROR("WARNING: Saturation maps do not currently work "
- "with CBF files\n");
- }
-
- if ( (p->orig_min_fs + p->w > data_width)
- || (p->orig_min_ss + p->h > data_height) )
- {
- ERROR("Panel %s is outside range of data in CBF file\n",
- p->name);
- return 1;
- }
-
- for ( ss=0; ss<p->h; ss++ ) {
- for ( fs=0; fs<p->w; fs++ ) {
-
- int idx;
- int cfs, css;
- int bad = 0;
-
- cfs = fs+p->orig_min_fs;
- css = ss+p->orig_min_ss;
- idx = cfs + css*data_width;
-
- image->dp[pi][fs+p->w*ss] = data[idx];
-
- if ( sat != NULL ) {
- image->sat[pi][fs+p->w*ss] = sat[idx];
- } else {
- image->sat[pi][fs+p->w*ss] = INFINITY;
- }
-
- if ( p->no_index ) bad = 1;
-
- if ( in_bad_region(image->det, p, cfs, css) ) {
- bad = 1;
- }
-
- if ( isnan(data[idx]) || isinf(data[idx]) ) bad = 1;
-
- if ( flags != NULL ) {
-
- int f;
-
- f = flags[idx];
-
- /* Bad if it's missing any of the "good" bits */
- if ( (f & image->det->mask_good)
- != image->det->mask_good ) bad = 1;
-
- /* Bad if it has any of the "bad" bits. */
- if ( f & image->det->mask_bad ) bad = 1;
-
- }
- image->bad[pi][fs+p->w*ss] = bad;
-
- }
- }
-
- }
-
- return 0;
-}
-
-
-static void cbf_fill_in_beam_parameters(struct beam_params *beam,
- struct imagefile *f,
- struct image *image)
-{
- double eV;
-
- if ( beam->photon_energy_from == NULL ) {
-
- /* Explicit value given */
- eV = beam->photon_energy;
-
- } else {
-
- ERROR("Can't get photon energy from CBF yet.\n");
- eV = 0.0;
-
- }
-
- image->lambda = ph_en_to_lambda(eV_to_J(eV))*beam->photon_energy_scale;
-}
-
-
-static void cbf_fill_in_clen(struct detector *det, struct imagefile *f)
-{
- int i;
-
- for ( i=0; i<det->n_panels; i++ ) {
-
- struct panel *p = &det->panels[i];
-
- if ( p->clen_from != NULL ) {
-
- ERROR("Can't get clen from CBF yet.\n");
-
- }
-
- adjust_centering_for_rail(p);
-
- }
-}
-
-
static void add_out(float val, float *data_out, int nmemb_out,
int *outpos, int *nrej)
{
@@ -1017,75 +782,6 @@ static float *read_cbf_data(const char *filename, int gz, int *w, int *h)
}
-static int read_cbf(struct imagefile *f, struct image *image)
-{
- float *data;
- int w, h;
-
- data = read_cbf_data(f->filename, f->type == IMAGEFILE_CBF,
- &w, &h);
- if ( data == NULL ) {
- ERROR("Failed to read CBF data\n");
- return 1;
- }
-
- unpack_panels(image, data, w, h);
- free(data);
-
- if ( image->beam != NULL ) {
- cbf_fill_in_beam_parameters(image->beam, f, image);
- if ( image->lambda > 1000 ) {
- ERROR("WARNING: Missing or nonsensical wavelength "
- "(%e m) for %s.\n",
- image->lambda, image->filename);
- }
- }
- cbf_fill_in_clen(image->det, f);
- fill_in_adu(image);
-
- return 0;
-}
-
-
-static int read_cbf_simple(struct imagefile *f, struct image *image)
-{
- float *data;
- int w, h;
-
- data = read_cbf_data(f->filename, f->type == IMAGEFILE_CBF,
- &w, &h);
- if ( data == NULL ) {
- ERROR("Failed to read CBF data\n");
- return 1;
- }
-
- 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;
- }
-
- image->dp[0] = data;
-
- if ( image->beam != NULL ) {
- cbf_fill_in_beam_parameters(image->beam, f, image);
- if ( image->lambda > 1000 ) {
- ERROR("WARNING: Missing or nonsensical wavelength "
- "(%e m) for %s.\n",
- image->lambda, image->filename);
- }
- }
- cbf_fill_in_clen(image->det, f);
- fill_in_adu(image);
-
- return 0;
-}
-
-
-/****************************** Image files ***********************************/
-
-
signed int is_cbf_file(const char *filename)
{
FILE *fh;
@@ -1123,141 +819,6 @@ signed int is_cbfgz_file(const char *filename)
}
-struct imagefile *imagefile_open(const char *filename)
-{
- struct imagefile *f;
-
- f = malloc(sizeof(struct imagefile));
- if ( f == NULL ) return NULL;
-
- if ( H5Fis_hdf5(filename) > 0 ) {
-
- /* This is an HDF5, pass through to HDF5 layer */
- f->type = IMAGEFILE_HDF5;
- f->hdfile = hdfile_open(filename);
-
- if ( f->hdfile == NULL ) {
- free(f);
- return NULL;
- }
-
- } else if ( is_cbf_file(filename) > 0 ) {
-
- f->type = IMAGEFILE_CBF;
-
- } else if ( is_cbfgz_file(filename) ) {
-
- f->type = IMAGEFILE_CBFGZ;
-
- } else {
-
- ERROR("Unrecognised file type: %s\n", filename);
- return NULL;
-
- }
-
- f->filename = strdup(filename);
- return f;
-}
-
-
-int imagefile_read(struct imagefile *f, struct image *image,
- struct event *event)
-{
- if ( f->type == IMAGEFILE_HDF5 ) {
- return hdf5_read2(f->hdfile, image, event, 0);
- } else if ( f->type == IMAGEFILE_CBF ) {
- return read_cbf(f, image);
- } else if ( f->type == IMAGEFILE_CBFGZ ) {
- return read_cbf(f, image);
- } else {
- ERROR("Unknown file type %i\n", f->type);
- return 1;
- }
-}
-
-
-/* Read a simple file, no multi-event, no prior geometry etc, and
- * generate a geometry for it */
-int imagefile_read_simple(struct imagefile *f, struct image *image)
-{
- if ( f->type == IMAGEFILE_HDF5 ) {
- return hdf5_read(f->hdfile, image, NULL, 0);
- } else if ( f->type == IMAGEFILE_CBF ) {
- return read_cbf_simple(f, image);
- } else if ( f->type == IMAGEFILE_CBFGZ ) {
- return read_cbf_simple(f, image);
- } else {
- ERROR("Unknown file type %i\n", f->type);
- return 1;
- }
-}
-
-
-enum imagefile_type imagefile_get_type(struct imagefile *f)
-{
- assert(f != NULL);
- return f->type;
-}
-
-
-struct hdfile *imagefile_get_hdfile(struct imagefile *f)
-{
- if ( f == NULL ) return NULL;
-
- if ( f->type != IMAGEFILE_HDF5 ) {
- ERROR("Not an HDF5 file!\n");
- return NULL;
- }
-
- return f->hdfile;
-}
-
-
-void imagefile_copy_fields(struct imagefile *f,
- const struct imagefile_field_list *copyme,
- FILE *fh, struct event *ev)
-{
- int i;
-
- if ( copyme == NULL ) return;
-
- for ( i=0; i<copyme->n_fields; i++ ) {
-
- char *val;
- char *field;
-
- field = copyme->fields[i];
-
- if ( f->type == IMAGEFILE_HDF5 ) {
- val = hdfile_get_string_value(f->hdfile, field, ev);
- if ( field[0] == '/' ) {
- fprintf(fh, "hdf5%s = %s\n", field, val);
- } else {
- fprintf(fh, "hdf5/%s = %s\n", field, val);
- }
- free(val);
-
- } else {
- STATUS("Mock CBF variable\n");
- fprintf(fh, "cbf/%s = %s\n", field, "(FIXME)");
- }
-
-
- }
-}
-
-
-void imagefile_close(struct imagefile *f)
-{
- if ( f->type == IMAGEFILE_HDF5 ) {
- hdfile_close(f->hdfile);
- }
- free(f->filename);
- free(f);
-}
-
-
/************************** New API (DataTemplate) ****************************/
struct image *image_new()