-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbimreadh5.c
172 lines (125 loc) · 5.11 KB
/
bimreadh5.c
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
/*******************************************************************************
Matlab interface for bioImage formats library
Input:
fname - string with file name of image to decode
page_num - the number of the page to decode, 0 if ommited
in time series pages are time points
in the z series pages are depth points
Output:
im - matrix of the image in the native format with channels in the 3d dimension
format - string with format name used to decode the image
pages - number of pages in the image
xyzres - pixel size on x,y,z dim in microns double[3]
metatxt - string with all meta-data extracted from the image
ex:
[im, format, pages, xyzr, metatxt] = bimreadh5(fname, page_num);
[im] = bimreadh5(fname);
Notes:
Currently only creates 8 or 16 bit arrays!!!
Extend if you want to read other type of data.
Author: Dima V. Fedorov <mailto:dima@dimin.net> <http://www.dimin.net/>
History:
10/13/2006 16:00 - First creation
Ver : 4
*******************************************************************************/
#include <libH5AR.hh>
// our interface to imgcnv includes:
#include <bimreadh5.hh>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>
//------------------------------------------------------------------------------
// function: bimreadh5
// INPUTS:
// OUTPUTS:
//------------------------------------------------------------------------------
int bimreadh5(bim::Image& aImage, std::string& aFormatName, int& aNumPages, double aPixelSize[], bim::TagMap& aMetaDataMap, const std::string aFileName, const int aPage)
{
//-----------------------------------------------------------
// handling of HDF5 containers:
//-----------------------------------------------------------
std::string vExtension = H5AR::GetExtension(aFileName);
if (!vExtension.empty()) {
vExtension = "." + vExtension;
}
// check if the file is inside a container
std::string vTempFileName = aFileName;
const bool vIsContainer = H5AR::IsInsideContainer(aFileName);
// special treatment for containers: extract and later delete
if (vIsContainer) {
vTempFileName = H5AR::TempFileName(vExtension);
if (!H5AR::ExtractFromH5(aFileName, vTempFileName)) {
std::cerr << "Failed to extract image " << aFileName << " from container." << std::endl;
}
}
//else {
// std::vector<char> vDataSet;
// if (!H5AR::ReadFile(aFileName, vDataSet)) {
// std::cerr << "Failed to read image from original filename." << std::endl;
// }
// if (!H5AR::WriteFile(vTempFileName, vDataSet)) {
// remove(vTempFileName.c_str());
// std::cerr << "Failed to write image to temporary filename." << std::endl;
// }
//}
//-----------------------------------------------------------
// read image and metadata
//-----------------------------------------------------------
bim::MetaFormatManager vMetaFormatManager;
if (vMetaFormatManager.sessionStartRead((bim::Filename)vTempFileName.c_str()) != 0) {
std::cerr << "Input format is not supported." << std::endl;
// delete the temporary file after usage:
if (vIsContainer) {
remove(vTempFileName.c_str());
}
return 1;
}
aNumPages = vMetaFormatManager.sessionGetNumberOfPages();
aFormatName = vMetaFormatManager.sessionGetFormatName();
#if defined (DEBUG) || defined (_DEBUG)
std::cerr << "Number of pages: " << aNumPages << std::endl;
std::cerr << "Format name: " << aFormatName << std::endl;
#endif
int vPage = aPage;
if (vPage<0) {
vPage=0;
std::cerr << "Requested page number is invalid, used " << vPage << std::endl;
}
if (vPage>=aNumPages) {
vPage=aNumPages-1;
std::cerr << "Requested page number is invalid, used " << vPage << std::endl;
}
if (vMetaFormatManager.sessionReadImage(aImage.imageBitmap(), vPage) != 0) {
if (vIsContainer) {
remove(vTempFileName.c_str());
}
return 1;
}
// getting metadata fields
vMetaFormatManager.sessionParseMetaData(vPage);
aPixelSize[0] = vMetaFormatManager.getPixelSizeX();
aPixelSize[1] = vMetaFormatManager.getPixelSizeY();
aPixelSize[2] = vMetaFormatManager.getPixelSizeZ();
#if defined (DEBUG) || defined (_DEBUG)
std::cerr << "Pixel resolution: (" << aPixelSize[0] << ", " << aPixelSize[1] << ", " << aPixelSize[2] << ")" << std::endl;
#endif
// get meta text if required
aMetaDataMap = vMetaFormatManager.get_metadata();
// add the filename to aMetaDataMap:
const size_t vPos = aFileName.rfind('/');
std::string fbasename;
if (vPos != std::string::npos) fbasename = aFileName.substr(vPos+1);
else fbasename = aFileName;
aMetaDataMap["Filename"] = fbasename.c_str();
vMetaFormatManager.sessionEnd();
//-----------------------------------------------------------
// pre-poc input image
//-----------------------------------------------------------
// make sure red image is in supported pixel format, e.g. will convert 12 bit to 16 bit
aImage = aImage.ensureTypedDepth();
// delete the temporary file after usage:
if (vIsContainer)
remove(vTempFileName.c_str());
return 0;
}