Repository navigation
Expand file tree
/
Copy pathHdfObjH5ImageDS.cpp
More file actions
executable file
·337 lines (283 loc) · 7.75 KB
/
Copy pathHdfObjH5ImageDS.cpp
File metadata and controls
executable file
·337 lines (283 loc) · 7.75 KB
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
// HdfObjH5ImageDS.cpp: implementation of the CH5ImageDS class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "HDFPlugin.h"
#include "HdfObjH5ImageDS.h"
#include "HdfObjH5AtomicDS.h"
#include "HdfObjH5File.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CH5ImageDS::CH5ImageDS(CFileFormat* fileformat, LPCTSTR name, LPCTSTR path)
:CImageDS(fileformat, name, path)
{
}
CH5ImageDS::~CH5ImageDS()
{
if (m_pData) free ( m_pData );
if (m_pDims) free (m_pDims);
if (m_pCount) free (m_pCount);
if (m_pStart) free (m_pStart);
if (m_pStride) free (m_pStride);
}
CObArray* CH5ImageDS::ReadAttributes()
{
if (m_pAttrList)
return m_pAttrList;
hid_t did, fid;
fid = (hid_t)m_pFile->GetFileID();
did = H5Dopen(fid, m_pFullName);
m_pAttrList = CH5File::GetAttribute(did);
H5Dclose(did);
return m_pAttrList;
}
void CH5ImageDS::Init()
{
hid_t did=-1, sid=-1, tid = -1, aid=-1, atid=-1;
hsize_t* dims = NULL;
// TODO: Add your own code here.
hid_t fid = (hid_t)m_pFile->GetFileID();
did = H5Dopen(fid, m_pFullName);
sid = H5Dget_space(did );
tid = H5Dget_type(did );
m_nNT = CH5AtomicDS::GetNT(tid);
m_nRank = H5Sget_simple_extent_ndims(sid);
if (m_nRank>0)
{
dims = (hsize_t*)malloc(m_nRank*sizeof(hsize_t));
H5Sget_simple_extent_dims(sid, dims,NULL ) ;
m_pDims = (unsigned long *)malloc(m_nRank*sizeof(unsigned long));
m_pCount = (unsigned long *)malloc(m_nRank*sizeof(unsigned long));
m_pStart = (unsigned long *)malloc(m_nRank*sizeof(unsigned long));
m_pStride = (unsigned long *)malloc(m_nRank*sizeof(unsigned long));
for (int i=0; i<m_nRank; i++)
{
m_pDims[i] = (unsigned long)dims[i];
m_pCount[i] = 1;
m_pStart[i] = 0;
m_pStride[i] = 1;
}
if (m_nRank == 1)
{
m_pCount[0] = m_pDims[0];
m_nHeight = m_pCount[0];
m_nHeightIdx = 0;
}
else
{
m_pCount[0] = m_pDims[0];
m_pCount[1] = m_pDims[1];
m_nHeight = m_pCount[0];
m_nHeightIdx = 0;
m_nWidth = m_pCount[1];
m_nWidthIdx = 1;
}
}
// default image settings
m_nInterlaceMode = IMAGE_INTERLACE_PIXEL;
m_nImageSubclass = IMAGE_SUBCLASS_INDEXED;
// get informaton for Attribute name="IMAGE_SUBCLASS"
aid = H5Aopen_name(did, "IMAGE_SUBCLASS");
if (aid > 0)
{
atid = H5Aget_type(aid);
char* subclassValue = (char*)malloc(sizeof(char) * 17);
H5Aread(aid, atid, subclassValue);
H5Tclose(atid);
H5Aclose(aid);
if (strncmp(subclassValue, "IMAGE_TRUECOLOR", 15) == 0)
{
m_nImageSubclass = IMAGE_SUBCLASS_TRUECOLOR;
if (m_nRank>2)
{
m_pCount[2] = m_pDims[2];
m_nComponents = m_pCount[2];
m_nHeight = m_pCount[0];
m_nHeightIdx = 0;
m_nWidth = m_pCount[1];
m_nWidthIdx = 1;
}
}
free(subclassValue);
}
// get informaton for Attribute name="INTERLACE_MODE"
aid = H5Aopen_name(did, "INTERLACE_MODE");
if (aid > 0)
{
atid = H5Aget_type(aid);
char* interlaceValue = (char*)malloc(sizeof(char) * 17);
H5Aread(aid, atid, interlaceValue);
H5Aclose(aid);
H5Tclose(atid);
if (strncmp(interlaceValue, "INTERLACE_PLANE", 15) == 0)
{
m_nInterlaceMode = IMAGE_INTERLACE_PLANE;
if (m_nRank>2 && m_pDims[0]>=3)
{
m_nImageSubclass = IMAGE_SUBCLASS_TRUECOLOR;
// storage layout [pixel components][height][width]
m_pCount[2] = m_pDims[2];
m_nComponents = m_pCount[0];
m_nHeight = m_pCount[1];
m_nHeightIdx = 1;
m_nWidth = m_pCount[2];
m_nWidthIdx = 2;
}
}
else
{
m_nInterlaceMode = IMAGE_INTERLACE_PIXEL;
if (m_nRank>2 && m_pDims[2]>=3)
{
m_nImageSubclass = IMAGE_SUBCLASS_TRUECOLOR;
m_pCount[2] = m_pDims[2];
m_nComponents = m_pCount[2];
m_nHeight = m_pCount[0];
m_nHeightIdx = 0;
m_nWidth = m_pCount[1];
m_nWidthIdx = 1;
}
}
free(interlaceValue);
}
aid = H5Aopen_name(did, "IMAGE_MINMAXRANGE");
if (aid > 0)
{
atid = H5Aget_type(aid);
int tsize = H5Tget_size(atid);
void* attrValue = malloc(2*tsize*sizeof(char));
if (H5Aread(aid, atid, attrValue)>=0)
{
m_pDataRange = (double *)malloc(2*sizeof(double));
unsigned char *vp = (unsigned char *)attrValue;
memcpy(&m_pDataRange, vp, tsize);
memcpy(&m_pDataRange+1, vp+1, tsize);
}
free(attrValue);
H5Aclose(aid);
H5Tclose(atid);
}
m_pPaletteRefs = GetPaletteRef(did);
if (dims) free(dims);
H5Sclose(sid);
H5Tclose(tid);
H5Dclose(did);
}
void* CH5ImageDS::ReadData()
{
hid_t did=-1, msid=-1, fsid=-1, tid=-1, mem_type_id=-1, fid=-1;
if (m_pData)
return m_pData; // data is loaded
if (m_nRank < 0)
Init();
fid = (hid_t)m_pFile->GetFileID();
did = H5Dopen(fid, m_pFullName);
tid = H5Dget_type(did );
msid = fsid = H5S_ALL;
m_nDataPoints = m_nWidth*m_nHeight*m_nComponents;
if (m_nRank > 0)
{
hsize_t mdims[1];
mdims[0] = m_nDataPoints;
msid = H5Screate_simple(1, mdims, NULL);
hssize_t *start;
hsize_t *count;
start = (hssize_t *)malloc(m_nRank*sizeof(hssize_t));
count = (hsize_t *)malloc(m_nRank*sizeof(hsize_t));
for (int i=0; i<m_nRank; i++)
{
start[i] = m_pStart[i];
count[i] = m_pCount[i];
}
fsid = H5Dget_space(did);
H5Sselect_hyperslab(fsid, H5S_SELECT_SET, start, NULL, count, NULL);
free(start);
free(count);
}
m_pData = malloc(m_nDataPoints*H5Tget_size(tid));
if (m_pData)
{
mem_type_id = H5Tget_native_type(tid,H5T_DIR_ASCEND);
herr_t status = H5Dread (did, mem_type_id, msid, fsid, H5P_DEFAULT, m_pData);
if (status < 0)
{
free(m_pData);
m_pData = NULL;
}
}
H5Sclose(msid);
H5Sclose(fsid);
H5Tclose(mem_type_id);
H5Tclose(tid);
H5Dclose(did);
return m_pData;
}
unsigned char* CH5ImageDS::ReadPalette()
{
if (m_pPalette || m_nImageSubclass == IMAGE_SUBCLASS_TRUECOLOR)
return m_pPalette;
if (!m_pPaletteRefs) // no palete attached to the image
return NULL;
hid_t fid=-1, pal_id=-1, tid=-1;
fid = (hid_t)m_pFile->GetFileID();
// use the fist palette
pal_id = H5Rdereference(fid, H5R_OBJECT, m_pPaletteRefs);
tid = H5Dget_type(pal_id);
// support up to 3*256 byte palette data
if (H5Dget_storage_size(pal_id) <= 768)
{
m_pPalette = (unsigned char *)malloc(3*256); // support 256 8-bit colors
herr_t status = H5Dread( pal_id, tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, m_pPalette);
if (status < 0)
{
free (m_pPalette);
m_pPalette = NULL;
}
}
H5Tclose(tid);
H5Dclose(pal_id);
return m_pPalette;
}
/* reads the references of palettes into an array
* Each reference requires eight bytes storage. Therefore, the array length
* is 8*numberOfPalettes.
*/
hobj_ref_t* CH5ImageDS::GetPaletteRef(hid_t did)
{
hid_t aid=-1, sid=-1, atid=-1;
hobj_ref_t *ref_buf;
int rank;
size_t size;
hsize_t *dims;
aid = H5Aopen_name(did, "PALETTE");
if (aid < 0) return NULL;
dims = NULL;
ref_buf = NULL;
sid = H5Aget_space(aid);
rank = H5Sget_simple_extent_ndims(sid);
size = 1;
if (rank > 0)
{
dims = (hsize_t *)malloc(rank * sizeof(hsize_t));
H5Sget_simple_extent_dims(sid, dims, NULL);
for (int i=0; i<rank; i++)
size *= (size_t)dims[i];
}
ref_buf = (hobj_ref_t *)malloc(size*sizeof(hobj_ref_t));
atid = H5Aget_type(aid);
if (H5Aread( aid, atid, ref_buf) < 0)
{
free(ref_buf);
ref_buf = NULL;
}
H5Tclose(atid);
H5Sclose(sid);
H5Aclose(aid);
return ref_buf;
}