From ef7157ef517cf66dfc5b2c2cfc6602e30a31d060 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Wed, 21 Sep 2022 16:22:47 +0200 Subject: Move create_detgeom to DataTemplate module It seems to make more sense here, because it's all about interpreting the contents of the DataTemplate structure. --- libcrystfel/src/image.c | 225 ------------------------------------------------ 1 file changed, 225 deletions(-) (limited to 'libcrystfel/src/image.c') diff --git a/libcrystfel/src/image.c b/libcrystfel/src/image.c index 3448421e..3ce8914e 100644 --- a/libcrystfel/src/image.c +++ b/libcrystfel/src/image.c @@ -486,231 +486,6 @@ static DataSourceType file_type(const char *filename) } -static int separate_value_and_units(const char *from, - char **pvalue, - char **punits) -{ - char *sp; - char *fromcpy; - char *unitscpy; - - if ( from == NULL ) return 1; - - fromcpy = strdup(from); - if ( fromcpy == NULL ) return 1; - - sp = strchr(fromcpy, ' '); - if ( sp == NULL ) { - unitscpy = NULL; - } else { - unitscpy = strdup(sp+1); - sp[0] = '\0'; - } - - *pvalue = fromcpy; - *punits = unitscpy; - return 0; -} - - -/* default_scale is a value to be used if both of the following - * conditions are met: - * - * 1. The value is a reference to image headers/metadata, - * rather than a literal number. - * 2. No units are specified in the number. - * - * This is totally horrible. Sorry. Blame history. - */ -static int im_get_length(struct image *image, const char *from, - double default_scale, double *pval) -{ - char *value_str; - char *units; - - if ( from == NULL ) return 1; - - if ( separate_value_and_units(from, &value_str, &units) ) return 1; - - if ( units == NULL ) { - - /* No units given */ - - if ( convert_float(value_str, pval) == 0 ) { - - /* Literal value with no units */ - free(value_str); - return 0; - - } else { - - int r; - r = image_read_header_float(image, value_str, pval); - free(value_str); - - if ( r == 0 ) { - /* Value read from headers with no units */ - *pval *= default_scale; - return 0; - } else { - /* Failed to read value from headers */ - return 1; - } - } - - } else { - - /* Units are specified */ - - double scale; - - if ( strcmp(units, "mm") == 0 ) { - scale = 1e-3; - } else if ( strcmp(units, "m") == 0 ) { - scale = 1.0; - } else { - ERROR("Invalid length unit '%s'\n", units); - free(value_str); - free(units); - return 1; - } - - if ( convert_float(value_str, pval) == 0 ) { - - /* Literal value, units specified */ - free(value_str); - free(units); - *pval *= scale; - return 0; - - } else { - - int r; - r = image_read_header_float(image, value_str, pval); - free(value_str); - - if ( r == 0 ) { - /* Value read from headers, units specified */ - *pval *= scale; - return 0; - } else { - /* Failed to read value from headers */ - return 1; - } - } - } -} - - -int create_detgeom(struct image *image, const DataTemplate *dtempl) -{ - struct detgeom *detgeom; - int i; - - if ( dtempl == NULL ) { - ERROR("NULL data template!\n"); - return 1; - } - - detgeom = malloc(sizeof(struct detgeom)); - if ( detgeom == NULL ) return 1; - - detgeom->panels = malloc(dtempl->n_panels*sizeof(struct detgeom_panel)); - if ( detgeom->panels == NULL ) { - free(detgeom); - return 1; - } - - detgeom->n_panels = dtempl->n_panels; - - for ( i=0; in_panels; i++ ) { - - struct detgeom_panel *p = &detgeom->panels[i]; - struct panel_template *tmpl = &dtempl->panels[i]; - double shift_x, shift_y; - - p->name = safe_strdup(tmpl->name); - - p->pixel_pitch = tmpl->pixel_pitch; - - /* NB cnx,cny are in pixels, cnz is in m */ - p->cnx = tmpl->cnx; - p->cny = tmpl->cny; - if ( im_get_length(image, tmpl->cnz_from, - 1e-3, &p->cnz) ) - { - ERROR("Failed to read length from '%s'\n", tmpl->cnz_from); - return 1; - } - - /* Apply offset (in m) and then convert cnz from - * m to pixels */ - p->cnz += tmpl->cnz_offset; - p->cnz /= p->pixel_pitch; - - /* Apply overall shift (already in m) */ - if ( dtempl->shift_x_from != NULL ) { - if ( im_get_length(image, dtempl->shift_x_from, 1.0, &shift_x) ) { - ERROR("Failed to read length from '%s'\n", - dtempl->shift_x_from); - return 1; - } - if ( im_get_length(image, dtempl->shift_y_from, 1.0, &shift_y) ) { - ERROR("Failed to read length from '%s'\n", - dtempl->shift_y_from); - return 1; - } - } else { - shift_x = 0.0; - shift_y = 0.0; - } - - if ( !isnan(shift_x) ) { - p->cnx += shift_x / p->pixel_pitch; - } - if ( !isnan(shift_y) ) { - p->cny += shift_y / p->pixel_pitch; - } - - p->max_adu = tmpl->max_adu; - - switch ( tmpl->adu_scale_unit ) { - - case ADU_PER_PHOTON: - p->adu_per_photon = tmpl->adu_scale; - break; - - case ADU_PER_EV: - p->adu_per_photon = tmpl->adu_scale - * ph_lambda_to_eV(image->lambda); - break; - - default: - p->adu_per_photon = 1.0; - ERROR("Invalid ADU/ph scale unit (%i)\n", - tmpl->adu_scale_unit); - break; - - } - - p->w = tmpl->orig_max_fs - tmpl->orig_min_fs + 1; - p->h = tmpl->orig_max_ss - tmpl->orig_min_ss + 1; - - p->fsx = tmpl->fsx; - p->fsy = tmpl->fsy; - p->fsz = tmpl->fsz; - p->ssx = tmpl->ssx; - p->ssy = tmpl->ssy; - p->ssz = tmpl->ssz; - - } - - image->detgeom = detgeom; - - return 0; -} - - int image_set_zero_data(struct image *image, const DataTemplate *dtempl) { -- cgit v1.2.3 From 7c42f8e2b675e017ab1144ca38c9e74c24d68266 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Wed, 21 Sep 2022 16:32:20 +0200 Subject: create_detgeom: Return detgeom structure rather than altering image argument --- libcrystfel/src/image.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'libcrystfel/src/image.c') diff --git a/libcrystfel/src/image.c b/libcrystfel/src/image.c index 3ce8914e..45b61bec 100644 --- a/libcrystfel/src/image.c +++ b/libcrystfel/src/image.c @@ -1074,7 +1074,8 @@ struct image *image_create_for_simulation(const DataTemplate *dtempl) return NULL; } - if ( create_detgeom(image, dtempl) ) { + image->detgeom = create_detgeom(image, dtempl); + if ( image->detgeom == NULL ) { image_free(image); return NULL; } @@ -1125,9 +1126,9 @@ static int do_image_read(struct image *image, const DataTemplate *dtempl, } profile_start("create-detgeom"); - r = create_detgeom(image, dtempl); + image->detgeom = create_detgeom(image, dtempl); profile_end("create-detgeom"); - if ( r ) { + if ( image->detgeom == NULL ) { ERROR("Failed to read geometry information\n"); return 1; } -- cgit v1.2.3 From e8053e5386146bf88b2b45d028774da9f4439069 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Thu, 22 Sep 2022 14:07:34 +0200 Subject: Implement data_template_get_2d_detgeom_if_possible It is horrible. But it's contained inside the DataTemplate module. --- libcrystfel/src/image.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'libcrystfel/src/image.c') diff --git a/libcrystfel/src/image.c b/libcrystfel/src/image.c index 45b61bec..84088ead 100644 --- a/libcrystfel/src/image.c +++ b/libcrystfel/src/image.c @@ -412,11 +412,6 @@ static struct header_cache_entry *cached_header(struct image *image, const char { struct header_cache_entry *ce; - if ( image == NULL ) { - ERROR("Attempt to retrieve a header value without an image\n"); - return NULL; - } - ce = find_cache_entry(image, from); if ( ce != NULL ) return ce; @@ -434,6 +429,8 @@ int image_read_header_float(struct image *image, const char *from, double *val) { struct header_cache_entry *ce; + if ( image == NULL ) return 1; + ce = cached_header(image, from); if ( ce == NULL ) return 1; @@ -1074,7 +1071,7 @@ struct image *image_create_for_simulation(const DataTemplate *dtempl) return NULL; } - image->detgeom = create_detgeom(image, dtempl); + image->detgeom = create_detgeom(image, dtempl, 0); if ( image->detgeom == NULL ) { image_free(image); return NULL; @@ -1126,7 +1123,7 @@ static int do_image_read(struct image *image, const DataTemplate *dtempl, } profile_start("create-detgeom"); - image->detgeom = create_detgeom(image, dtempl); + image->detgeom = create_detgeom(image, dtempl, 0); profile_end("create-detgeom"); if ( image->detgeom == NULL ) { ERROR("Failed to read geometry information\n"); -- cgit v1.2.3