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 SetDcmItem(DcmItem * item);
71 GetElementSQ(
const unsigned short group,
72 const unsigned short entry,
74 const bool throwException =
true)
const;
77 DcmItem * m_DcmItem{
nullptr };
85 SetDcmSequenceOfItems(DcmSequenceOfItems * seq);
89 GetSequence(
unsigned long index,
DCMTKSequence & target,
const bool throwException =
true)
const;
91 GetStack(
const unsigned short group,
92 const unsigned short element,
93 DcmStack & resultStack,
94 const bool throwException =
true)
const;
96 GetElementCS(
const unsigned short group,
97 const unsigned short element,
99 const bool throwException =
true)
const;
102 GetElementOB(
const unsigned short group,
103 const unsigned short element,
104 std::string & target,
105 const bool throwException =
true)
const;
108 GetElementCSorOB(
const unsigned short group,
109 const unsigned short element,
110 std::string & target,
111 const bool throwException =
true)
const;
113 template <
typename TType>
116 const unsigned short element,
118 const bool throwException =
true)
const
120 if (this->GetElementDS<TType>(group, element, 1, &target,
false) == EXIT_SUCCESS)
125 if (this->GetElementOB(group, element, val, throwException) != EXIT_SUCCESS)
128 << element << std::dec);
130 const char * data = val.c_str();
131 const auto * fptr = reinterpret_cast<const TType *>(data);
136 template <
typename TType>
139 const unsigned short element,
142 const bool throwException =
true)
const
144 if (this->GetElementDS<TType>(group, element, count, target,
false) == EXIT_SUCCESS)
149 if (this->GetElementOB(group, element, val, throwException) != EXIT_SUCCESS)
152 << element << std::dec);
154 const char * data = val.c_str();
155 const auto * fptr = reinterpret_cast<const TType *>(data);
156 for (
int i = 0; i < count; ++i)
165 GetElementFD(
const unsigned short group,
166 const unsigned short element,
169 const bool throwException =
true)
const;
171 GetElementFD(
const unsigned short group,
172 const unsigned short element,
174 const bool throwException =
true)
const;
176 GetElementDS(
const unsigned short group,
177 const unsigned short element,
178 std::string & target,
179 const bool throwException =
true)
const;
181 GetElementTM(
const unsigned short group,
182 const unsigned short element,
183 std::string & target,
184 const bool throwException =
true)
const;
188 template <
typename TType>
191 const unsigned short element,
192 unsigned short count,
194 const bool throwException =
true)
const
196 DcmStack resultStack;
197 if (this->GetStack(group, element, resultStack, throwException) != EXIT_SUCCESS)
201 auto * dsItem = dynamic_cast<DcmDecimalString *>(resultStack.top());
202 if (dsItem ==
nullptr)
209 OFVector<Float64> doubleVals;
210 if (dsItem->getFloat64Vector(doubleVals) != EC_Normal)
213 << element << std::dec);
215 if (doubleVals.size() != count)
218 << count <<
"items, but found " << doubleVals.size() << std::dec);
220 for (
unsigned i = 0; i < count; i++)
222 target[i] = static_cast<TType>(doubleVals[i]);
227 GetElementSQ(
const unsigned short group,
228 const unsigned short element,
230 const bool throwException =
true)
const;
232 GetElementItem(
unsigned short itemIndex,
DCMTKItem & target,
const bool throwException =
true)
const;
237 this->m_DcmSequenceOfItems->print(out);
241 DcmSequenceOfItems * m_DcmSequenceOfItems{
nullptr };
253 SetFileName(
const std::string &
fileName);
262 GetElementLO(
const unsigned short group,
263 const unsigned short element,
264 std::string & target,
265 const bool throwException =
true)
const;
267 GetElementLO(
const unsigned short group,
268 const unsigned short element,
269 std::vector<std::string> & target,
270 const bool throwException =
true)
const;
275 template <
typename TType>
278 const unsigned short element,
279 unsigned short count,
281 const bool throwException =
true)
const
283 DcmTagKey tagkey(group, element);
285 if (this->m_Dataset->findAndGetElement(tagkey, el) != EC_Normal)
289 auto * dsItem = dynamic_cast<DcmDecimalString *>(el);
290 if (dsItem ==
nullptr)
293 << element << std::dec);
295 OFVector<Float64> doubleVals;
296 if (dsItem->getFloat64Vector(doubleVals) != EC_Normal)
299 << element << std::dec);
301 if (doubleVals.size() != count)
304 << count <<
"items, but found " << doubleVals.size() << std::dec);
306 for (
unsigned i = 0; i < count; i++)
308 target[i] = static_cast<TType>(doubleVals[i]);
314 template <
typename TType>
317 const unsigned short element,
319 const bool throwException =
true)
const
321 if (this->GetElementDS<TType>(group, element, 1, &target,
false) == EXIT_SUCCESS)
326 if (this->GetElementOB(group, element, val, throwException) != EXIT_SUCCESS)
329 << element << std::dec);
331 const char * data = val.c_str();
332 const auto * fptr = reinterpret_cast<const TType *>(data);
334 switch (this->GetTransferSyntax())
336 case EXS_LittleEndianImplicit:
337 case EXS_LittleEndianExplicit:
340 case EXS_BigEndianImplicit:
341 case EXS_BigEndianExplicit:
352 GetElementDS(
const unsigned short group,
353 const unsigned short element,
354 std::string & target,
355 const bool throwException =
true)
const;
357 GetElementFD(
const unsigned short group,
358 const unsigned short element,
360 const bool throwException =
true)
const;
362 GetElementFD(
const unsigned short group,
363 const unsigned short element,
366 const bool throwException =
true)
const;
368 GetElementFL(
const unsigned short group,
369 const unsigned short element,
371 const bool throwException =
true)
const;
373 GetElementFLorOB(
const unsigned short group,
374 const unsigned short element,
376 const bool throwException =
true)
const;
380 GetElementUS(
const unsigned short group,
381 const unsigned short element,
382 unsigned short & target,
383 const bool throwException =
true)
const;
385 GetElementUS(
const unsigned short group,
386 const unsigned short element,
387 unsigned short *& target,
388 const bool throwException =
true)
const;
392 GetElementCS(
const unsigned short group,
393 const unsigned short element,
394 std::string & target,
395 const bool throwException =
true)
const;
400 GetElementPN(
const unsigned short group,
401 const unsigned short element,
402 std::string & target,
403 const bool throwException =
true)
const;
408 GetElementIS(
const unsigned short group,
409 const unsigned short element,
411 const bool throwException =
true)
const;
414 GetElementSL(
const unsigned short group,
415 const unsigned short element,
417 const bool throwException =
true)
const;
420 GetElementISorOB(
const unsigned short group,
421 const unsigned short element,
423 const bool throwException =
true)
const;
426 GetElementCSorOB(
const unsigned short group,
427 const unsigned short element,
428 std::string & target,
429 const bool throwException =
true)
const;
434 GetElementOB(
const unsigned short group,
435 const unsigned short element,
436 std::string & target,
437 const bool throwException =
true)
const;
440 GetElementSQ(
const unsigned short group,
441 unsigned short entry,
443 const bool throwException =
true)
const;
446 GetElementUI(
const unsigned short group,
447 unsigned short entry,
448 std::string & target,
449 const bool throwException =
true)
const;
452 GetElementDA(
const unsigned short group,
453 const unsigned short element,
454 std::string & target,
455 const bool throwException =
true)
const;
458 GetElementTM(
const unsigned short group,
459 const unsigned short element,
460 std::string & target,
461 const bool throwException =
true)
const;
464 GetDirCosines(vnl_vector<double> & dir1, vnl_vector<double> & dir2, vnl_vector<double> & dir3)
const;
467 GetDirCosArray(
double *
const dircos)
const;
470 GetFrameCount()
const;
473 GetSlopeIntercept(
double & slope,
double & intercept)
const;
476 GetDimensions(
unsigned short & rows,
unsigned short & columns)
const;
479 GetImageDataType()
const;
481 GetImagePixelType()
const;
484 GetSpacing(
double *
const spacing)
const;
486 GetOrigin(
double *
const origin)
const;
489 HasPixelData()
const;
492 GetTransferSyntax()
const;
495 GetFileNumber()
const;
497 AddDictEntry(DcmDictEntry * entry);
500 CanReadFile(
const std::string & filename);
502 IsImageFile(
const std::string & filename);
506 DcmFileFormat * m_DFile{
nullptr };
507 DcmDataset * m_Dataset{
nullptr };
508 E_TransferSyntax m_Xfer{ EXS_Unknown };
509 Sint32 m_FrameCount{ 0 };
510 long m_FileNumber{ -1L };
517 #endif // itkDCMTKFileReader_h