aboutsummaryrefslogtreecommitdiff
path: root/src/qdrp.c
blob: 86384bdd7a0c428a35c60462c0e1fccd1e0fbb57 (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * qdrp.c
 *
 * Handle QDRP-style control files
 *
 * (c) 2007 Thomas White <taw27@cam.ac.uk>
 *  dtr - Diffraction Tomography Reconstruction
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "readpng.h"
#include "control.h"
#include "reflections.h"

static void qdrp_chomp(char *str) {

	size_t i;
	
	for ( i=0; i<strlen(str); i++ ) {
		if ( str[i] == '\r' ) {
			str[i] = '\0';
			return;
		}
		if ( str[i] == '\n' ) {
			str[i] = '\0';
			return;
		}
	}
}

#define skip_whitespace										\
	while ( (( line[pos] == ' ' ) || ( line[pos] == '\t' )) && ( pos < strlen(line) ) ) {	\
		pos++;										\
	}

#define skip_chars										\
	while ( ( line[pos] != ' ' ) && ( line[pos] != '\t' ) && ( pos < strlen(line) ) ) {	\
		pos++;										\
	}

static int qdrp_parsefileline(ControlContext *ctx, const char *line) {

	size_t pos = 0;
	size_t mark = 0;
	char *tilt_s;
	char *file;
	double tilt;
					
	skip_whitespace;
	mark = pos;
	skip_chars;
	tilt_s = strndup(line+mark, pos-mark);
	
	skip_whitespace;
	mark = pos;
	skip_chars;
	file = strndup(line+mark, pos-mark);
					
	tilt = strtod(tilt_s, NULL);
	free(tilt_s);
	printf("QD: Reading file: Tilt = %f deg, File='%s'\n", tilt, file);
	
	if ( readpng_read(file, tilt, ctx) ) {
		printf("Reconstruction failed.\n");
		return 1;
	}
	
	return 0;
	
}

static int qdrp_parseline(ControlContext *ctx, const char *line) {

	if ( ctx->started ) {
		return qdrp_parsefileline(ctx, line);						
	}
	
	if ( ( line[0] == '-' ) && ( line[1] == '-') ) {
		
		if ( !ctx->camera_length_set ) {
			printf("QD: Parameter 'camera-length' not specified!\n");
			return 1;
		}
					
		if ( !ctx->lambda_set ) {
			printf("QD: Parameter 'lambda' not specified!\n");
			return 1;
		}
					
		if ( !ctx->resolution_set ) {
			printf("QD: Parameter 'resolution' not specified!\n");
			return 1;
		}
					
		if ( !ctx->omega_set ) {
			printf("QD: Parameter 'omega' not specified, and not using tilt-axis-search.\n");
			return 1;
		}
				
		ctx->started = 1;
		ctx->reflectionctx = reflection_init();
						
	}
					
	if ( !ctx->started ) {
						
		size_t pos = 0;
		size_t mark = 0;
						
		skip_whitespace;
						
		if ( strncasecmp(line+pos, "resolution", 10) == 0 ) {
			
			char *resolution_s;
						
			skip_chars;
			skip_whitespace;
			mark = pos;
			skip_chars;
			resolution_s = strndup(line+mark, pos-mark);
			ctx->resolution = strtod(resolution_s, NULL);
			free(resolution_s);
			ctx->resolution_set = 1;
			printf("QD: resolution = %f pixels/m\n", ctx->resolution);	
			
		}
		
		if ( strncasecmp(line+pos, "lambda", 6) == 0 ) {
			
			char *lambda_s;
						
			skip_chars;
			skip_whitespace;
			mark = pos;
			skip_chars;
			lambda_s = strndup(line+mark, pos-mark);
			ctx->lambda = strtod(lambda_s, NULL);
			free(lambda_s);
			ctx->lambda_set = 1;
			printf("QD: lambda = %e m\n", ctx->lambda);	
			
		}
		
		if ( strncasecmp(line+pos, "camera-length", 13) == 0 ) {
			
			char *camera_length_s;
						
			skip_chars;
			skip_whitespace;
			mark = pos;
			skip_chars;
			camera_length_s = strndup(line+mark, pos-mark);
			ctx->camera_length = strtod(camera_length_s, NULL);
			free(camera_length_s);
			ctx->camera_length_set = 1;
			printf("QD: camera-length = %f m\n", ctx->camera_length);	
			
		}
		
		if ( strncasecmp(line+pos, "omega", 5) == 0 ) {
			
			char *omega_s;
			
			skip_chars;
			skip_whitespace;
			mark = pos;
			skip_chars;
			omega_s = strndup(line+mark, pos-mark);
			ctx->omega = strtod(omega_s, NULL);
			free(omega_s);
			ctx->omega_set = 1;
			printf("QD: omega = %f deg\n", ctx->omega);
				
		}
		
		if ( strncasecmp(line+pos, "centre", 6) == 0 ) {
			
			float xc, yc;
			
			if ( sscanf(line+pos, "centre %f %f", &xc, &yc) != 2 ) {
				fprintf(stderr, "Couldn't parse 'centre' line\n");
				return 1;
			}
			ctx->x_centre = xc;
			ctx->y_centre = yc;
			ctx->have_centres = 1;
			printf("QD: centre %.2f, %.2f px\n", ctx->x_centre, ctx->y_centre);

		}
		
	}

	return 0;

}

int qdrp_read(ControlContext *ctx) {
	
	char *line;
	
	line = malloc(256);

	ctx->camera_length_set = 0;
	ctx->omega_set = 0;
	ctx->resolution_set = 0;
	ctx->lambda_set = 0;
	ctx->started = 0;
	
	FILE *fh;
	
	fh = fopen(ctx->filename, "r");
	if ( !fh ) {
		printf("QD: Couldn't open control file '%s'\n", ctx->filename);
		return -1;
	}
	
	while ( !feof(fh) ) {			
		fgets(line, 256, fh);			
		if ( !feof(fh) ) {				
			qdrp_chomp(line);
			if ( strlen(line) == 0 ) continue;			
			if ( line[0] != '#' ) {
				if ( qdrp_parseline(ctx, line) ) {
					fclose(fh);
					free(line);
					return -1;
				}			
			}			
		}
	}
	
	fclose(fh);
	
	free(line);
	
	return 0;	/* Success */
	
}

unsigned int qdrp_is_qdrprc(const char *filename) {

	if ( strstr(filename, "qdrp") ) {
		return 1;
	}

	return 0;

}