aboutsummaryrefslogtreecommitdiff
path: root/src/image.c
blob: b3825b380febd91f601db8d97141daad8ba52108 (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
/*
 * image.c
 *
 * Handle images and image features
 *
 * (c) 2006-2009 Thomas White <thomas.white@desy.de>
 *
 * template_index - Indexing diffraction patterns by template matching
 *
 */


#define _GNU_SOURCE 1
#include <stdlib.h>
#include <assert.h>
#include <math.h>

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


int image_add(ImageList *list, struct image *image)
{
	if ( list->images ) {
		list->images = realloc(list->images,
		                       (list->n_images+1)*sizeof(struct image));
	} else {
		assert(list->n_images == 0);
		list->images = malloc(sizeof(struct image));
	}

	/* Copy the metadata */
	list->images[list->n_images] = *image;

	/* Fill out some extra fields */
	list->images[list->n_images].features = NULL;
	list->images[list->n_images].rflist = NULL;

	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_reflection(ImageFeatureList *flist, double x, double y,
                                  struct image *parent, double intensity)
{
	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->n_features++;

}


void image_add_feature(ImageFeatureList *flist, double x, double y,
                       struct image *parent, double intensity)
{
	image_add_feature_reflection(flist, x, y, parent, intensity);
}


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 *idx)
{
	int i;
	double dmin = +HUGE_VAL;
	int 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 < +HUGE_VAL ) {
		*d = dmin;
		*idx = closest;
		return &flist->features[closest];
	}

	*d = +INFINITY;
	return NULL;
}


ImageFeature *image_feature_second_closest(ImageFeatureList *flist,
                                           double x, double y, double *d,
                                           int *idx)
{
	int i;
	double dmin = +HUGE_VAL;
	int closest = 0;
	double dfirst;
	int idxfirst;

	image_feature_closest(flist, x, y, &dfirst, &idxfirst);

	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) && (i != idxfirst) ) {
			dmin = d;
			closest = i;
		}

	}

	if ( dmin < +HUGE_VAL ) {
		*d = dmin;
		*idx = closest;
		return &flist->features[closest];
	}

	return NULL;

}