aboutsummaryrefslogtreecommitdiff
path: root/src/image.c
blob: 5290cb4262d250efcf6e08d2c08cdb419a631aed (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
/*
 * image.c
 *
 * Handle images and image features
 *
 * (c) 2007 Thomas White <taw27@cam.ac.uk>
 *
 *  dtr - Diffraction Tomography Reconstruction
 *
 */

#include <stdlib.h>
#include <assert.h>
#include <math.h>

#include "control.h"
#include "image.h"
#include "utils.h"

int image_add(ImageList *list, uint16_t *image, int width, int height, double tilt, ControlContext *ctx) {

	if ( list->images ) {
		list->images = realloc(list->images, (list->n_images+1)*sizeof(ImageRecord));
	} else {
		assert(list->n_images == 0);
		list->images = malloc(sizeof(ImageRecord));
	}
	
	list->images[list->n_images].tilt = tilt;
	list->images[list->n_images].omega = ctx->omega;
	list->images[list->n_images].image = image;
	list->images[list->n_images].width = width;
	list->images[list->n_images].height = height;
	list->images[list->n_images].lambda = ctx->lambda;
	list->images[list->n_images].fmode = ctx->fmode;
	list->images[list->n_images].x_centre = ctx->x_centre;
	list->images[list->n_images].y_centre = ctx->y_centre;
	list->images[list->n_images].slop = 0.0;
	list->images[list->n_images].features = NULL;
	
	if ( ctx->fmode == FORMULATION_PIXELSIZE ) {
		list->images[list->n_images].pixel_size = ctx->pixel_size;
		list->images[list->n_images].camera_len = 0;
		list->images[list->n_images].resolution = 0;
	} else if ( ctx->fmode == FORMULATION_CLEN ) {
		list->images[list->n_images].pixel_size = 0;
		list->images[list->n_images].camera_len = ctx->camera_length;
		list->images[list->n_images].resolution = ctx->resolution;
	}
	
	list->n_images++;
	
	return list->n_images - 1;

}

ImageList *image_list_new() {

	ImageList *list;
	
	list = malloc(sizeof(ImageList));
	
	list->n_images = 0;
	list->images = NULL;
	
	return list;

}

void image_add_feature_index(ImageFeatureList *flist, double x, double y, ImageRecord *parent, double intensity,
					signed int h, signed int k, signed int l) {

	if ( flist->features ) {
		flist->features = realloc(flist->features, (flist->n_features+1)*sizeof(ImageFeature));
	} else {
		assert(flist->n_features == 0);
		flist->features = malloc(sizeof(ImageFeature));
	}
	
	flist->features[flist->n_features].x = x;
	flist->features[flist->n_features].y = y;
	flist->features[flist->n_features].intensity = intensity;
	flist->features[flist->n_features].parent = parent;
	flist->features[flist->n_features].partner = NULL;
	flist->features[flist->n_features].partner_d = 0.0;
	flist->features[flist->n_features].h = h;
	flist->features[flist->n_features].k = k;
	flist->features[flist->n_features].l = l;
	
	flist->n_features++;

}

void image_add_feature(ImageFeatureList *flist, double x, double y, ImageRecord *parent, double intensity) {
	image_add_feature_index(flist, x, y, parent, intensity, 0, 0, 0);
}

ImageFeatureList *image_feature_list_new() {

	ImageFeatureList *flist;
	
	flist = malloc(sizeof(ImageFeatureList));
	
	flist->n_features = 0;
	flist->features = NULL;
	
	return flist;
}

void image_feature_list_free(ImageFeatureList *flist) {

	if ( !flist ) return;
	
	if ( flist->features ) free(flist->features);
	free(flist);

}

ImageFeature *image_feature_closest(ImageFeatureList *flist, double x, double y, double *d) {

	int i;
	double dmin = +HUGE_VAL;
	int closest;
	closest = 0;
	
	for ( i=0; i<flist->n_features; i++ ) {
	
		double d;
		
		d = distance(flist->features[i].x, flist->features[i].y, x, y);
	
		if ( d < dmin ) {
			dmin = d;
			closest = i;
		}
	
	}
	
	if ( dmin <= 20.0 ) {
		*d = dmin;
		return &flist->features[closest];
	}

	return NULL;

}