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
|
/*
* hdf5.c
*
* Read/write HDF5 data files
*
* (c) 2006-2009 Thomas White <thomas.white@desy.de>
*
* pattern_sim - Simulate diffraction patterns from small crystals
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <hdf5.h>
#include "image.h"
#include "hdf5-file.h"
struct hdfile {
const char *path; /* Current data path */
struct image *image;
size_t nx; /* Image width */
size_t ny; /* Image height */
hid_t fh; /* HDF file handle */
hid_t dh; /* Dataset handle */
hid_t sh; /* Dataspace handle */
};
struct hdfile *hdfile_open(const char *filename)
{
struct hdfile *f;
f = malloc(sizeof(struct hdfile));
if ( f == NULL ) return NULL;
f->fh = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);
if ( f->fh < 0 ) {
ERROR("Couldn't open file: %s\n", filename);
free(f);
return NULL;
}
return f;
}
int hdfile_set_image(struct hdfile *f, const char *path)
{
hsize_t size[2];
hsize_t max_size[2];
f->dh = H5Dopen(f->fh, path, H5P_DEFAULT);
if ( f->dh < 0 ) {
ERROR("Couldn't open dataset\n");
return -1;
}
f->sh = H5Dget_space(f->dh);
if ( H5Sget_simple_extent_ndims(f->sh) != 2 ) {
ERROR("Dataset is not two-dimensional\n");
return -1;
}
H5Sget_simple_extent_dims(f->sh, size, max_size);
f->nx = size[0];
f->ny = size[1];
return 0;
}
int hdfile_get_width(struct hdfile *f)
{
return f->nx;
}
int hdfile_get_height(struct hdfile *f)
{
return f->ny;
}
void hdfile_close(struct hdfile *f)
{
H5Fclose(f->fh);
free(f->image);
free(f);
}
static void *hdfile_bin(uint16_t *in, int inw, int inh,
int binning, uint16_t *maxp)
{
uint16_t *data;
int x, y;
int w, h;
uint16_t max;
w = inw / binning;
h = inh / binning; /* Some pixels might get discarded */
data = malloc(w*h*sizeof(uint16_t));
max = 0;
for ( x=0; x<w; x++ ) {
for ( y=0; y<h; y++ ) {
/* Big enough to hold large values */
unsigned long long int total;
size_t xb, yb;
total = 0;
for ( xb=0; xb<binning; xb++ ) {
for ( yb=0; yb<binning; yb++ ) {
total += in[inh*(binning*x+xb)+binning*y+yb];
}
}
data[y+h*(w-1-x)] = total / (binning * binning);
if ( data[y+h*(w-1-x)] > max ) max = data[y+h*(w-1-x)];
}
}
*maxp = max;
return data;
}
uint16_t *hdfile_get_image_binned(struct hdfile *f, int binning, uint16_t *max)
{
struct image *image;
uint16_t *data;
image = malloc(sizeof(struct image));
if ( image == NULL ) return NULL;
hdf5_read(f, image);
f->image = image;
data = hdfile_bin(image->data, f->nx, f->ny, binning, max);
return data;
}
int hdf5_write(const char *filename, const uint16_t *data,
int width, int height)
{
hid_t fh, gh, sh, dh; /* File, group, dataspace and data handles */
herr_t r;
hsize_t size[2];
hsize_t max_size[2];
fh = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
if ( fh < 0 ) {
ERROR("Couldn't create file: %s\n", filename);
return 1;
}
gh = H5Gcreate(fh, "data", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
if ( gh < 0 ) {
ERROR("Couldn't create group\n");
H5Fclose(fh);
return 1;
}
size[0] = width;
size[1] = height;
max_size[0] = width;
max_size[1] = height;
sh = H5Screate_simple(2, size, max_size);
dh = H5Dcreate(gh, "data", H5T_NATIVE_UINT16, sh,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
if ( dh < 0 ) {
ERROR("Couldn't create dataset\n");
H5Fclose(fh);
return 1;
}
/* Muppet check */
H5Sget_simple_extent_dims(sh, size, max_size);
r = H5Dwrite(dh, H5T_NATIVE_UINT16, H5S_ALL,
H5S_ALL, H5P_DEFAULT, data);
if ( r < 0 ) {
ERROR("Couldn't write data\n");
H5Dclose(dh);
H5Fclose(fh);
return 1;
}
H5Gclose(gh);
H5Dclose(dh);
H5Fclose(fh);
return 0;
}
int hdf5_read(struct hdfile *f, struct image *image)
{
herr_t r;
uint16_t *buf;
buf = malloc(sizeof(float)*f->nx*f->ny);
r = H5Dread(f->dh, H5T_NATIVE_UINT16, H5S_ALL, H5S_ALL,
H5P_DEFAULT, buf);
if ( r < 0 ) {
ERROR("Couldn't read data\n");
H5Dclose(f->dh);
return 1;
}
image->data = buf;
image->height = f->nx;
image->width = f->ny;
image->x_centre = image->width/2;
image->y_centre = image->height/2;
return 0;
}
|