-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.cpp
404 lines (339 loc) · 11.7 KB
/
utils.cpp
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include "plugin.h"
#include <stdio.h>
#include <stdarg.h>
#include <utf8.h>
#include <cmath>
#include <boost/static_assert.hpp>
using namespace Gatan;
#ifdef ENABLE_DEBUG
void debug(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
char message[2048];
int size = _vsnprintf(message, sizeof(message)-1, fmt, args);
va_end(args);
if (size < 0)
message[sizeof(message)-1] = 0;
char* ptr = message;
char end;
do {
char* next = strchr(ptr, '\n');
if (!next)
next = ptr + strlen(ptr);
end = *next;
*next = '\0';
if (next > ptr) {
#if GMS_VERSION_MAJOR >= 2
DM::Debug("Debug(HDF5_Plugin): ");
DM::Debug(ptr);
DM::Debug("\n");
#else
DM::Result("Debug(HDF5_Plugin): ");
DM::Result(ptr);
DM::Result("\n");
#endif
}
ptr = next + 1;
} while (end != 0);
}
#endif // ENABLE_DEBUG
void warning(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
char message[2048];
int size = _vsnprintf(message, sizeof(message)-1, fmt, args);
va_end(args);
if (size < 0)
message[sizeof(message)-1] = 0;
char* ptr = message;
char end;
do {
char* next = strchr(ptr, '\n');
if (!next)
next = ptr + strlen(ptr);
end = *next;
*next = '\0';
if (next > ptr) {
#if GMS_VERSION_MAJOR >= 2
DM::Debug("Warning(HDF5_Plugin): ");
DM::Debug(ptr);
DM::Debug("\n");
#else
DM::Result("Warning(HDF5_Plugin): ");
DM::Result(ptr);
DM::Result("\n");
#endif
}
ptr = next + 1;
} while (end != 0);
}
#ifdef ENABLE_DEBUG
static herr_t dumpCallback(unsigned n, const H5E_error2_t *err_desc, void* /*client_data*/)
{
debug("\t[%d] %s(%d): %s\n", n, err_desc->file_name, err_desc->line, err_desc->desc ? err_desc->desc : "");
return 0;
}
void dump_HDF_error_stack()
{
H5Ewalk(H5E_DEFAULT, H5E_WALK_DOWNWARD, dumpCallback, NULL);
H5Eclear(H5E_DEFAULT);
}
#endif // ENABLE_DEBUG
BOOST_STATIC_ASSERT(sizeof(wchar_t) == 2); // Else replace utf16 by utf32
DM::String from_UTF8(const std::string &input)
{
std::wstring output;
if (!utf8::is_valid(input.begin(), input.end())) {
std::string replaced;
utf8::replace_invalid(input.begin(), input.end(), std::back_inserter(replaced), 0xfffd);
utf8::unchecked::utf8to16(replaced.begin(), replaced.end(), std::back_inserter(output));
} else
utf8::unchecked::utf8to16(input.begin(), input.end(), std::back_inserter(output));
return output;
}
DM::String from_UTF8(const char* input)
{
if (!input)
return DM::String();
const char* end = input + strlen(input);
std::wstring output;
if (!utf8::is_valid(input, end)) {
std::string replaced;
utf8::replace_invalid(input, end, std::back_inserter(replaced), 0xfffd);
utf8::unchecked::utf8to16(replaced.begin(), replaced.end(), std::back_inserter(output));
} else
utf8::unchecked::utf8to16(input, end, std::back_inserter(output));
return output;
}
std::string to_UTF8(const DM::String& input)
{
std::wstring input_wstr;
input.copy(input_wstr);
std::string result;
utf8::utf16to8(input_wstr.begin(), input_wstr.end(), std::back_inserter(result));
return result;
}
static const char* default_real = "r";
static const char* default_imag = "i";
type_handle_t create_complex_type(int size, const char* realName, const char* imagName) throw()
{
if (!realName)
realName = default_real;
if (!imagName)
imagName = default_imag;
if (size != 8 && size != 16) {
warning("Invalid size in create_complex_type()\n");
return type_handle_t();
}
type_handle_t type(H5Tcreate(H5T_COMPOUND, size));
if (!type.valid()) {
warning("H5Tcreate failed\n");
dump_HDF_error_stack();
return type_handle_t();
}
if (H5Tinsert(type.get(), realName, 0, size == 8 ? H5T_NATIVE_FLOAT : H5T_NATIVE_DOUBLE) < 0
|| H5Tinsert(type.get(), imagName, size / 2, size == 8 ? H5T_NATIVE_FLOAT : H5T_NATIVE_DOUBLE) < 0) {
warning("H5Tinsert failed\n");
dump_HDF_error_stack();
return type_handle_t();
}
return type;
}
type_handle_t create_compatible_complex_type(hid_t type_id) throw()
{
if (H5Tget_class(type_id) != H5T_COMPOUND)
return type_handle_t();
if (H5Tget_nmembers(type_id) != 2)
return type_handle_t();
if (H5Tget_member_class(type_id, 0) != H5T_FLOAT || H5Tget_member_class(type_id, 1) != H5T_FLOAT)
return type_handle_t();
char* field0 = H5Tget_member_name(type_id, 0);
char* field1 = H5Tget_member_name(type_id, 1);
if ((_stricmp("r", field0) != 0 || _stricmp("i", field1) != 0)
&& (_stricmp("re", field0) != 0 || _stricmp("im", field1) != 0)
&& (_stricmp("real", field0) != 0 || _stricmp("imag", field1) != 0)) {
free(field0);
free(field1);
return type_handle_t();
}
type_handle_t memtype;
if (H5Tget_size(type_id) <= 8)
memtype = create_complex_type(8, field0, field1);
else
memtype = create_complex_type(16, field0, field1);
free(field0);
free(field1);
return memtype;
}
bool image_to_HDF(DM::Image image, type_handle_t& type, space_handle_t& space)
{
type = datatype_to_HDF(image.GetDataType());
if (!type.valid()) {
debug("Unsupported image type.");
return false;
}
// Reverse order of dimenesions, HDF uses row-major indices, while DM uses column-major
int rank = image.GetNumDimensions();
std::vector<hsize_t> dims(rank);
for (int i = 0; i < rank; i++)
dims[rank - 1 - i] = image.GetDimensionSize(i);
space.reset(H5Screate_simple(rank, &dims[0], NULL));
if (!space.valid()) {
warning("Creation of dataspace failed.");
dump_HDF_error_stack();
type.reset();
return false;
}
return true;
}
long datatype_from_HDF(hid_t type_id)
{
if (type_id < 0)
return -1;
size_t elemsize = H5Tget_size(type_id);
switch (H5Tget_class(type_id)) {
case H5T_FLOAT:
if (elemsize <= 4)
return ImageData::REAL4_DATA;
else
return ImageData::REAL8_DATA;
break;
case H5T_INTEGER:
if (H5Tget_sign(type_id)) {
if (elemsize == 1)
return ImageData::SIGNED_INT8_DATA;
else if (elemsize == 2)
return ImageData::SIGNED_INT16_DATA;
#if GMS_VERSION_MAJOR >= 2
else if (elemsize <= 4)
return ImageData::SIGNED_INT32_DATA;
else
return undocumented_SIGNED_INT64_DATA;
#else // GMS_VERSION_MAJOR < 2
else
return ImageData::SIGNED_INT32_DATA;
#endif // GMS_VERSION_MAJOR
} else {
if (elemsize == 1)
return ImageData::UNSIGNED_INT8_DATA;
else if (elemsize == 2)
return ImageData::UNSIGNED_INT16_DATA;
#if GMS_VERSION_MAJOR >= 2
else if (elemsize <= 4)
return ImageData::UNSIGNED_INT32_DATA;
else
return undocumented_UNSIGNED_INT64_DATA;
#else // GMS_VERSION_MAJOR < 2
else
return ImageData::UNSIGNED_INT32_DATA;
#endif // GMS_VERSION_MAJOR
}
break;
case H5T_COMPOUND:
// Compound of two floats ?
if (H5Tget_nmembers(type_id) != 2)
return -1;
if (H5Tget_member_class(type_id, 0) != H5T_FLOAT || H5Tget_member_class(type_id, 1) != H5T_FLOAT)
return -1;
// Floats named something like "real" and "imag" ?
{
char* field0 = H5Tget_member_name(type_id, 0);
char* field1 = H5Tget_member_name(type_id, 1);
bool real_and_imag = (_stricmp("r", field0) == 0 && _stricmp("i", field1) == 0)
|| (_stricmp("re", field0) == 0 && _stricmp("im", field1) == 0)
|| (_stricmp("real", field0) == 0 && _stricmp("imag", field1) == 0);
free(field0);
free(field1);
if (!real_and_imag)
return -1;
}
if (H5Tget_size(type_id) <= 8)
return ImageData::COMPLEX8_DATA;
else
return ImageData::COMPLEX16_DATA;
break;
default:
break;
}
return -1;
}
type_handle_t datatype_to_HDF(long datatype)
{
switch (datatype) {
case ImageData::SIGNED_INT8_DATA: return type_handle_t(H5Tcopy(H5T_NATIVE_INT8));
case ImageData::SIGNED_INT16_DATA: return type_handle_t(H5Tcopy(H5T_NATIVE_INT16));
case ImageData::SIGNED_INT32_DATA: return type_handle_t(H5Tcopy(H5T_NATIVE_INT32));
case undocumented_SIGNED_INT64_DATA: return type_handle_t(H5Tcopy(H5T_NATIVE_INT64));
case ImageData::UNSIGNED_INT8_DATA: return type_handle_t(H5Tcopy(H5T_NATIVE_UINT8));
case ImageData::UNSIGNED_INT16_DATA: return type_handle_t(H5Tcopy(H5T_NATIVE_UINT16));
case ImageData::UNSIGNED_INT32_DATA: return type_handle_t(H5Tcopy(H5T_NATIVE_UINT32));
case undocumented_UNSIGNED_INT64_DATA: return type_handle_t(H5Tcopy(H5T_NATIVE_UINT64));
case ImageData::REAL4_DATA: return type_handle_t(H5Tcopy(H5T_NATIVE_FLOAT));
case ImageData::REAL8_DATA: return type_handle_t(H5Tcopy(H5T_NATIVE_DOUBLE));
case ImageData::COMPLEX8_DATA: return create_complex_type(8);
case ImageData::COMPLEX16_DATA: return create_complex_type(16);
default: return type_handle_t();
}
}
DM::Image create_image(long datatype, int rank, const hsize_t* dims)
{
if (datatype < 0)
return DM::Image();
switch (rank) {
case 0: return DM::NewImage("", datatype, (uint32)1); // Scalar: read as one element image.
case 1: return DM::NewImage("", datatype, (uint32)dims[0]);
case 2: return DM::NewImage("", datatype, (uint32)dims[1], (uint32)dims[0]);
case 3: return DM::NewImage("", datatype, (uint32)dims[2], (uint32)dims[1], (uint32)dims[0]);
case 4: return DM::NewImage("", datatype, (uint32)dims[3], (uint32)dims[2], (uint32)dims[1], (uint32)dims[0]);
default:
warning("create_image: Unsupported rank: %d.", rank);
return DM::Image();
}
}
int hsize_array_from_HDF5(int space_id, std::vector<hsize_t>& dims)
{
dims.clear();
if (!H5Sis_simple(space_id))
return -1;
int rank = H5Sget_simple_extent_ndims(space_id);
if (rank < 0)
return -1;
if (rank > 0) {
dims.resize(rank);
if (H5Sget_simple_extent_dims(space_id, &dims[0], NULL) < 0) {
dims.clear();
return -1;
}
}
return dims.size();
}
Gatan::DM::TagGroup taglist_from_hsize_array(const hsize_t* ptr, int size)
{
Gatan::DM::TagGroup list = DM::NewTagList();
for (int i = size - 1; i >= 0; --i)
list.InsertTagAsLong(-1, long(ptr[i]));
return list;
}
std::vector<hsize_t> hsize_array_from_taglist(const Gatan::DM::TagGroup &list)
{
if (!list.IsValid() || !list.IsList())
return std::vector<hsize_t>();
long size = list.CountTags();
std::vector<hsize_t> result(size);
for (long i = 0; i < size; ++i) {
long dim;
if (!list.GetIndexedTagAsLong(i, &dim))
return std::vector<hsize_t>();
result[size - 1 - i] = dim;
}
return result;
}
file_handle_t open_always(const char* filename)
{
file_handle_t file(H5Fopen(filename, H5F_ACC_RDWR, H5P_DEFAULT));
if (!file.valid())
file.reset(H5Fcreate(filename, H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT));
return file;
}