18 #ifndef itkDCMTKFileReader_h
19 #define itkDCMTKFileReader_h
21 #include "ITKIODCMTKExport.h"
26 #include "vnl/vnl_vector.h"
27 #include "dcmtk/dcmdata/dcdict.h"
28 #include "dcmtk/dcmdata/dcdicent.h"
29 #include "dcmtk/dcmdata/dcxfer.h"
30 #include "dcmtk/dcmdata/dcvrds.h"
31 #include "dcmtk/dcmdata/dcstack.h"
32 #include "dcmtk/dcmdata/dcdatset.h"
33 #include "dcmtk/dcmdata/dcitem.h"
34 #include "dcmtk/dcmdata/dcvrobow.h"
35 #include "dcmtk/dcmdata/dcsequen.h"
39 class DcmSequenceOfItems;
46 #define DCMTKExceptionOrErrorReturn(body) \
50 itkGenericExceptionMacro(body); \
54 return EXIT_FAILURE; \
69 void SetDcmItem(DcmItem *item);
70 int GetElementSQ(
const unsigned short group,
71 const unsigned short entry,
73 const bool throwException =
true)
const;
83 void SetDcmSequenceOfItems(DcmSequenceOfItems *seq);
85 int GetSequence(
unsigned long index,
86 DCMTKSequence &target,
const bool throwException =
true)
const;
87 int GetStack(
const unsigned short group,
88 const unsigned short element,
89 DcmStack &resultStack,
const bool throwException =
true)
const;
90 int GetElementCS(
const unsigned short group,
91 const unsigned short element,
93 const bool throwException =
true)
const;
95 int GetElementOB(
const unsigned short group,
96 const unsigned short element,
98 const bool throwException =
true)
const;
100 int GetElementCSorOB(
const unsigned short group,
101 const unsigned short element,
103 const bool throwException =
true)
const;
105 template <
typename TType>
107 const unsigned short element,
109 const bool throwException =
true)
const
111 if(this->GetElementDS<TType>(group,element,1,&target,
false) == EXIT_SUCCESS)
116 if(this->GetElementOB(group,element,val,throwException) != EXIT_SUCCESS)
119 << group <<
" " << std::hex
120 << element << std::dec);
122 const char *data = val.c_str();
123 const auto * fptr =
reinterpret_cast<const TType *
>(data);
129 template <
typename TType>
131 const unsigned short element,
134 const bool throwException =
true)
const
136 if(this->GetElementDS<TType>(group,element,count,target,
false) == EXIT_SUCCESS)
141 if(this->GetElementOB(group,element,val,throwException) != EXIT_SUCCESS)
144 << group <<
" " << std::hex
145 << element << std::dec);
147 const char *data = val.c_str();
148 const auto * fptr =
reinterpret_cast<const TType *
>(data);
149 for(
int i = 0; i < count; ++i)
157 int GetElementFD(
const unsigned short group,
158 const unsigned short element,
161 const bool throwException =
true)
const;
162 int GetElementFD(
const unsigned short group,
163 const unsigned short element,
165 const bool throwException =
true)
const;
166 int GetElementDS(
const unsigned short group,
167 const unsigned short element,
169 const bool throwException =
true)
const;
170 int GetElementTM(
const unsigned short group,
171 const unsigned short element,
173 const bool throwException =
true)
const;
177 template <
typename TType>
179 const unsigned short element,
180 unsigned short count,
182 const bool throwException =
true)
const
184 DcmStack resultStack;
185 if(this->GetStack(group,element,resultStack,throwException) != EXIT_SUCCESS)
189 auto * dsItem =
dynamic_cast<DcmDecimalString *
>(resultStack.top());
190 if(dsItem ==
nullptr)
193 << std::hex << group <<
" "
194 << element << std::dec);
198 OFVector<Float64> doubleVals;
199 if(dsItem->getFloat64Vector(doubleVals) != EC_Normal)
202 << group <<
" " << std::hex
203 << element << std::dec);
205 if(doubleVals.size() != count)
208 << group <<
" " << std::hex
209 << element <<
" expected "
210 << count <<
"items, but found "
211 << doubleVals.size() << std::dec);
214 for(
unsigned i = 0; i < count; i++)
216 target[i] =
static_cast<TType
>(doubleVals[i]);
220 int GetElementSQ(
const unsigned short group,
221 const unsigned short element,
223 const bool throwException =
true)
const;
224 int GetElementItem(
unsigned short itemIndex,
226 const bool throwException =
true)
const;
230 this->m_DcmSequenceOfItems->print(out);
249 void SetFileName(
const std::string &fileName);
251 const std::string &GetFileName()
const;
255 int GetElementLO(
const unsigned short group,
256 const unsigned short element,
258 const bool throwException =
true)
const;
259 int GetElementLO(
const unsigned short group,
260 const unsigned short element,
261 std::vector<std::string> &target,
262 const bool throwException =
true)
const;
267 template <
typename TType>
269 const unsigned short element,
270 unsigned short count,
272 const bool throwException =
true)
const
274 DcmTagKey tagkey(group,element);
276 if(this->m_Dataset->findAndGetElement(tagkey,el) != EC_Normal)
279 << group <<
" " << std::hex
280 << element << std::dec);
282 auto * dsItem =
dynamic_cast<DcmDecimalString *
>(el);
283 if(dsItem ==
nullptr)
286 << group <<
" " << std::hex
287 << element << std::dec);
289 OFVector<Float64> doubleVals;
290 if(dsItem->getFloat64Vector(doubleVals) != EC_Normal)
293 << group <<
" " << std::hex
294 << element << std::dec);
296 if(doubleVals.size() != count)
299 << group <<
" " << std::hex
300 << element <<
" expected "
301 << count <<
"items, but found "
302 << doubleVals.size() << std::dec);
306 for(
unsigned i = 0; i < count; i++)
308 target[i] =
static_cast<TType
>(doubleVals[i]);
313 template <
typename TType>
315 const unsigned short element,
317 const bool throwException =
true)
const
319 if(this->GetElementDS<TType>(group,element,1,&target,
false) == EXIT_SUCCESS)
324 if(this->GetElementOB(group,element,val,throwException) != EXIT_SUCCESS)
327 << group <<
" " << std::hex
328 << element << std::dec);
330 const char *data = val.c_str();
331 const auto * fptr =
reinterpret_cast<const TType *
>(data);
333 switch(this->GetTransferSyntax())
335 case EXS_LittleEndianImplicit:
336 case EXS_LittleEndianExplicit:
339 case EXS_BigEndianImplicit:
340 case EXS_BigEndianExplicit:
351 int GetElementDS(
const unsigned short group,
352 const unsigned short element,
354 const bool throwException =
true)
const;
355 int GetElementFD(
const unsigned short group,
356 const unsigned short element,
358 const bool throwException =
true)
const;
359 int GetElementFD(
const unsigned short group,
360 const unsigned short element,
363 const bool throwException =
true)
const;
364 int GetElementFL(
const unsigned short group,
365 const unsigned short element,
367 const bool throwException =
true)
const;
368 int GetElementFLorOB(
const unsigned short group,
369 const unsigned short element,
371 const bool throwException =
true)
const;
374 int GetElementUS(
const unsigned short group,
375 const unsigned short element,
376 unsigned short &target,
377 const bool throwException =
true)
const;
378 int GetElementUS(
const unsigned short group,
379 const unsigned short element,
380 unsigned short *&target,
381 const bool throwException =
true)
const;
384 int GetElementCS(
const unsigned short group,
385 const unsigned short element,
387 const bool throwException =
true)
const;
391 int GetElementPN(
const unsigned short group,
392 const unsigned short element,
394 const bool throwException =
true)
const;
398 int GetElementIS(
const unsigned short group,
399 const unsigned short element,
401 const bool throwException =
true)
const;
403 int GetElementSL(
const unsigned short group,
404 const unsigned short element,
406 const bool throwException =
true)
const;
408 int GetElementISorOB(
const unsigned short group,
409 const unsigned short element,
411 const bool throwException =
true)
const;
413 int GetElementCSorOB(
const unsigned short group,
414 const unsigned short element,
416 const bool throwException =
true)
const;
420 int GetElementOB(
const unsigned short group,
421 const unsigned short element,
423 const bool throwException =
true)
const;
425 int GetElementSQ(
const unsigned short group,
426 unsigned short entry,
428 const bool throwException =
true)
const;
430 int GetElementUI(
const unsigned short group,
431 unsigned short entry,
433 const bool throwException =
true)
const;
435 int GetElementDA(
const unsigned short group,
436 const unsigned short element,
438 const bool throwException =
true)
const;
440 int GetElementTM(
const unsigned short group,
441 const unsigned short element,
443 const bool throwException =
true)
const;
445 int GetDirCosines(vnl_vector<double> &dir1,
446 vnl_vector<double> &dir2,
447 vnl_vector<double> &dir3)
const;
449 int GetDirCosArray(
double *
const dircos)
const;
451 int GetFrameCount()
const;
453 int GetSlopeIntercept(
double &slope,
double &intercept)
const;
455 int GetDimensions(
unsigned short &rows,
unsigned short &columns)
const;
460 int GetSpacing(
double *
const spacing)
const;
461 int GetOrigin(
double *
const origin)
const;
463 bool HasPixelData()
const;
465 E_TransferSyntax GetTransferSyntax()
const;
467 long GetFileNumber()
const;
469 AddDictEntry(DcmDictEntry *entry);
471 static bool CanReadFile(
const std::string &filename);
472 static bool IsImageFile(
const std::string &filename);
487 #endif // itkDCMTKFileReader_h
static void SwapFromSystemToBigEndian(T *p)
int GetElementDSorOB(const unsigned short group, const unsigned short element, int count, TType *target, const bool throwException=true) const
int GetElementDSorOB(const unsigned short group, const unsigned short element, TType &target, const bool throwException=true) const
bool CompareDCMTKFileReaders(DCMTKFileReader *a, DCMTKFileReader *b)
int GetElementDS(const unsigned short group, const unsigned short element, unsigned short count, TType *target, const bool throwException=true) const
DcmSequenceOfItems * m_DcmSequenceOfItems
static void SwapFromSystemToLittleEndian(T *p)
int GetElementDSorOB(const unsigned short group, const unsigned short element, TType &target, const bool throwException=true) const
void print(std::ostream &out) const
#define DCMTKExceptionOrErrorReturn(body)
int GetElementDS(const unsigned short group, const unsigned short element, unsigned short count, TType *target, const bool throwException=true) const