aboutsummaryrefslogtreecommitdiff
path: root/libcrystfel/src/image-seedee.c
blob: 2a4328ece3b7f6083edb2519b53969aa60512f67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * image-seedee.c
 *
 * Image loading with Seedee
 *
 * Copyright © 2017-2022 Deutsches Elektronen-Synchrotron DESY,
 *                       a research centre of the Helmholtz Association.
 *
 * Authors:
 *   2018-2022 Thomas White <taw@physics.org>
 *   2014      Valerio Mariani
 *   2017      Stijn de Graaf
 *
 * This file is part of CrystFEL.
 *
 * CrystFEL is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CrystFEL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with CrystFEL.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <libcrystfel-config.h>

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <unistd.h>

#include <image.h>
#include <utils.h>
#include <profile.h>

#include "datatemplate_priv.h"


#if defined(HAVE_SEEDEE)

#include <seedee/seedee.h>
#include <cjson/cJSON.h>


static int load_seedee_data(struct panel_template *p,
                            struct SeedeeNDArray *array,
                            float *data, int *bad)
{
	int data_size_fs, data_size_ss;

	data_size_ss = array->shape[0];
	data_size_fs = array->shape[1];

	if ( (p->orig_min_fs + PANEL_WIDTH(p) > data_size_fs)
	  || (p->orig_min_ss + PANEL_HEIGHT(p) > data_size_ss) )
	{
		ERROR("Data for panel %s (%i x %i + %i + %i) is outside data "
		      "array bounds (%i x %i)\n",
		      p->name,
		      PANEL_WIDTH(p), PANEL_HEIGHT(p),
		      p->orig_min_fs, p->orig_min_ss,
		      data_size_fs, data_size_ss);
		return 1;
	}

	if ( (array->datatype == 'u')
	  && (array->itemsize == 2)
	  && (array->byteorder == '<') )
	{
		int fs, ss;
		uint16_t *in_data = (uint16_t *)array->data;

		for ( ss=0; ss<PANEL_HEIGHT(p); ss++ ) {
			for ( fs=0; fs<PANEL_WIDTH(p); fs++ ) {
				size_t idx = fs+p->orig_min_fs + (ss+p->orig_min_ss)*data_size_fs;
				data[fs+ss*PANEL_WIDTH(p)] = in_data[idx];
			}
		}

	} else if ( (array->datatype == 'u')
	  && (array->itemsize == 4)
	  && (array->byteorder == '<') )
	{
		int fs, ss;
		uint32_t *in_data = (uint32_t *)array->data;

		for ( ss=0; ss<PANEL_HEIGHT(p); ss++ ) {
			for ( fs=0; fs<PANEL_WIDTH(p); fs++ ) {
				size_t idx = fs+p->orig_min_fs + (ss+p->orig_min_ss)*data_size_fs;
				data[fs+ss*PANEL_WIDTH(p)] = in_data[idx];
			}
		}

	} else if ( (array->datatype == 'i')
	  && (array->itemsize == 4)
	  && (array->byteorder == '<') )
	{
		int fs, ss;
		int32_t *in_data = (int32_t *)array->data;

		for ( ss=0; ss<PANEL_HEIGHT(p); ss++ ) {
			for ( fs=0; fs<PANEL_WIDTH(p); fs++ ) {
				size_t idx = fs+p->orig_min_fs + (ss+p->orig_min_ss)*data_size_fs;
				data[fs+ss*PANEL_WIDTH(p)] = in_data[idx];
			}
		}

	} else if ( (array->datatype == 'f')
	  && (array->itemsize == 8)
	  && (array->byteorder == '<') )
	{
		int fs, ss;
		double *in_data = (double *)array->data;

		for ( ss=0; ss<PANEL_HEIGHT(p); ss++ ) {
			for ( fs=0; fs<PANEL_WIDTH(p); fs++ ) {
				size_t idx = fs+p->orig_min_fs + (ss+p->orig_min_ss)*data_size_fs;
				data[fs+ss*PANEL_WIDTH(p)] = in_data[idx];
			}
		}

	} else {
		ERROR("Unrecognised data type %c%i%c\n",
		      array->datatype, array->itemsize, array->byteorder);
		return 1;
	}
	/* Note: when adding a new data type, don't forget that you are
	 * responsible for marking NaN/inf pixels as bad. */

	return 0;
}


/* Read the image data from 'data_block' into 'image', according to 'dtempl' */
int image_seedee_read(struct image *image,
                      DataTemplate *dtempl,
                      void *data_block,
                      size_t data_block_size,
                      char *meta_data)
{
	struct SeedeeNDArray array;
	int r;
	bool zero_copy;
	int i;
	cJSON *json;
	cJSON *data_format_str;

	json = cJSON_Parse(meta_data);
	if ( json == NULL ) {
		ERROR("Failed to parse JSON\n");
		return 1;
	}

	data_format_str = cJSON_GetObjectItemCaseSensitive(json, "_data_format");
	if ( !cJSON_IsString(data_format_str) ) {
		ERROR("_data_format isn't a string");
		cJSON_Delete(json);
		return 1;
	}

	profile_start("seedee-get-size");
	array.size = seedee_get_data_size(data_format_str->valuestring,
	                                  data_block, data_block_size,
	                                  &zero_copy, &array);
	profile_end("seedee-get-size");
	array.data = malloc(array.size);
	array.shape = malloc(array.ndims*sizeof(int));
	if ( (array.data == NULL) || (array.shape == NULL) ) {
		cJSON_Delete(json);
		free(array.data);
		free(array.shape);
		return 1;
	}

	if ( array.ndims != 2 ) {
		ERROR("Seedee data has unexpected number of dimensions "
		      "(%i, expected 2)\n", array.ndims);
		free(array.data);
		free(array.shape);
		return 1;
	}

	profile_start("seedee-deserialize");
	r = seedee_deserialize_ndarray(data_format_str->valuestring,
	                               data_block, data_block_size,
	                               0, &array);
	profile_end("seedee-deserialize");
	cJSON_Delete(json);
	if ( r < 0 ) {
		ERROR("Seedee deserialiation failed.\n");
		free(array.data);
		free(array.shape);
		return 1;
	}

	profile_start("seedee-panel");
	for ( i=0; i<dtempl->n_panels; i++ ) {
		if ( load_seedee_data(&dtempl->panels[i], &array,
		                      image->dp[i], image->bad[i]) )
		{
			ERROR("Failed to load data for panel '%s'\n",
			      dtempl->panels[i].name);
			profile_end("seedee-panel");
			free(array.data);
			free(array.shape);
			return 1;
		}
	}
	profile_end("seedee-panel");

	free(array.data);
	free(array.shape);

	return 0;
}


#else /* defined(HAVE_SEEDEE) */

int image_seedee_read(struct image *image,
                      const DataTemplate *dtempl,
                      void *data,
                      size_t data_size,
                      char *meta_data)
{
	ERROR("Seedee is not supported in this installation (read).\n");
	return 1;
}

#endif /* defined(HAVE_SEEDEE) */