Slicer  4.8
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
itkDCMTKFileReader.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkDCMTKFileReader_h
19 #define itkDCMTKFileReader_h
20 
21 // XXX # Workaround bug in packaging of DCMTK 3.6.0 on Debian.
22 // # See http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=637687
23 #ifndef HAVE_CONFIG_H
24 #define HAVE_CONFIG_H
25 #endif
26 
27 #include <stack>
28 #include <vector>
29 #include "itkByteSwapper.h"
30 #include "itkIntTypes.h"
31 #include "vnl/vnl_vector.h"
32 #include "dcmtk/dcmdata/dcxfer.h"
33 #include "dcmtk/dcmdata/dcvrds.h"
34 #include "dcmtk/dcmdata/dcstack.h"
35 #include "dcmtk/dcmdata/dcdatset.h"
36 #include "itkMacro.h"
37 #include "itkImageIOBase.h"
38 
39 class DcmSequenceOfItems;
40 class DcmFileFormat;
41 class DcmDictEntry;
42 
43 // Don't print error messages if you're not throwing
44 // an exception
45 // std::cerr body;
46 #define DCMTKException(body) \
47  { \
48  if(throwException) \
49  { \
50  itkGenericExceptionMacro(body); \
51  } \
52  else \
53  { \
54  return EXIT_FAILURE; \
55  } \
56  }
57 
58 namespace itk
59 {
61 {
62 public:
63  DCMTKSequence() : m_DcmSequenceOfItems(0) {}
64  void SetDcmSequenceOfItems(DcmSequenceOfItems *seq);
65  int card();
66  int GetSequence(unsigned long index,
67  DCMTKSequence &target,bool throwException = true);
68  int GetStack(unsigned short group,
69  unsigned short element,
70  DcmStack *resultStack, bool throwException = true);
71  int GetElementCS(unsigned short group,
72  unsigned short element,
73  std::string &target,
74  bool throwException = true);
75  int GetElementFD(unsigned short group,
76  unsigned short element,
77  double * &target,
78  bool throwException = true);
79  int GetElementFD(unsigned short group,
80  unsigned short element,
81  double &target,
82  bool throwException = true);
83  int GetElementDS(unsigned short group,
84  unsigned short element,
85  std::string &target,
86  bool throwException = true);
87  int GetElementTM(unsigned short group,
88  unsigned short element,
89  std::string &target,
90  bool throwException = true);
94  template <typename TType>
95  int GetElementDS(unsigned short group,
96  unsigned short element,
97  unsigned short count,
98  TType *target,
99  bool throwException)
100  {
101  DcmStack resultStack;
102  this->GetStack(group,element,&resultStack);
103  DcmDecimalString *dsItem =
104  dynamic_cast<DcmDecimalString *>(resultStack.top());
105  if(dsItem == 0)
106  {
107  DCMTKException(<< "Can't get DecimalString Element at tag "
108  << std::hex << group << " "
109  << element << std::dec);
110  }
111 
112  OFVector<Float64> doubleVals;
113  if(dsItem->getFloat64Vector(doubleVals) != EC_Normal)
114  {
115  DCMTKException(<< "Cant extract Array from DecimalString " << std::hex
116  << group << " " << std::hex
117  << element << std::dec);
118  }
119  if(doubleVals.size() != count)
120  {
121  DCMTKException(<< "DecimalString " << std::hex
122  << group << " " << std::hex
123  << element << " expected "
124  << count << "items, but found "
125  << doubleVals.size() << std::dec);
126 
127  }
128  for(unsigned i = 0; i < count; i++)
129  {
130  target[i] = static_cast<TType>(doubleVals[i]);
131  }
132  return EXIT_SUCCESS;
133  }
134  int GetElementSQ(unsigned short group,
135  unsigned short element,
136  DCMTKSequence &target,
137  bool throwException = true);
138 private:
139  DcmSequenceOfItems *m_DcmSequenceOfItems;
140 };
141 
143 {
144 public:
146 
147  DCMTKFileReader() : m_DFile(0),
148  m_Dataset(0),
149  m_Xfer(EXS_Unknown),
150  m_FrameCount(0),
151  m_FileNumber(-1L)
152  {
153  }
154  ~DCMTKFileReader();
155 
156  void SetFileName(const std::string &fileName);
157 
158  const std::string &GetFileName() const;
159 
160  void LoadFile();
161 
162  int GetElementLO(unsigned short group,
163  unsigned short element,
164  std::string &target,
165  bool throwException = true);
166  int GetElementLO(unsigned short group,
167  unsigned short element,
168  std::vector<std::string> &target,
169  bool throwException = true);
170 
174  template <typename TType>
175  int GetElementDS(unsigned short group,
176  unsigned short element,
177  unsigned short count,
178  TType *target,
179  bool throwException = true)
180  {
181  DcmTagKey tagkey(group,element);
182  DcmElement *el;
183  if(this->m_Dataset->findAndGetElement(tagkey,el) != EC_Normal)
184  {
185  DCMTKException(<< "Cant find tag " << std::hex
186  << group << " " << std::hex
187  << element << std::dec);
188  }
189  DcmDecimalString *dsItem = dynamic_cast<DcmDecimalString *>(el);
190  if(dsItem == 0)
191  {
192  DCMTKException(<< "Cant find DecimalString element " << std::hex
193  << group << " " << std::hex
194  << element << std::dec);
195  }
196  OFVector<Float64> doubleVals;
197  if(dsItem->getFloat64Vector(doubleVals) != EC_Normal)
198  {
199  DCMTKException(<< "Cant extract Array from DecimalString " << std::hex
200  << group << " " << std::hex
201  << element << std::dec);
202  }
203  if(doubleVals.size() != count)
204  {
205  DCMTKException(<< "DecimalString " << std::hex
206  << group << " " << std::hex
207  << element << " expected "
208  << count << "items, but found "
209  << doubleVals.size() << std::dec);
210 
211  }
212  for(unsigned i = 0; i < count; i++)
213  {
214  target[i] = static_cast<TType>(doubleVals[i]);
215  }
216  return EXIT_SUCCESS;
217  }
218 
219  template <typename TType>
220  int GetElementDSorOB(unsigned short group,
221  unsigned short element,
222  TType &target,
223  bool throwException = true)
224  {
225  if(this->GetElementDS<TType>(group,element,1,&target,false) == EXIT_SUCCESS)
226  {
227  return EXIT_SUCCESS;
228  }
229  std::string val;
230  if(this->GetElementOB(group,element,val) != EXIT_SUCCESS)
231  {
232  DCMTKException(<< "Cant find DecimalString element " << std::hex
233  << group << " " << std::hex
234  << element << std::dec);
235  }
236  const char *data = val.c_str();
237  const TType *fptr = reinterpret_cast<const TType *>(data);
238  target = *fptr;
239  switch(this->GetTransferSyntax())
240  {
241  case EXS_LittleEndianImplicit:
242  case EXS_LittleEndianExplicit:
243  itk::ByteSwapper<TType>::SwapFromSystemToLittleEndian(&target);
244  break;
245  case EXS_BigEndianImplicit:
246  case EXS_BigEndianExplicit:
247  itk::ByteSwapper<TType>::SwapFromSystemToBigEndian(&target);
248  break;
249  default:
250  break;
251  }
252  return EXIT_SUCCESS;
253 
254  }
257  int GetElementDS(unsigned short group,
258  unsigned short element,
259  std::string &target,
260  bool throwException = true);
261  int GetElementFD(unsigned short group,
262  unsigned short element,
263  double &target,
264  bool throwException = true);
265  int GetElementFD(unsigned short group,
266  unsigned short element,
267  double * &target,
268  bool throwException = true);
269  int GetElementFL(unsigned short group,
270  unsigned short element,
271  float &target,
272  bool throwException = true);
273  int GetElementFLorOB(unsigned short group,
274  unsigned short element,
275  float &target,
276  bool throwException = true);
277 
278  int GetElementUS(unsigned short group,
279  unsigned short element,
280  unsigned short &target,
281  bool throwException = true);
282  int GetElementUS(unsigned short group,
283  unsigned short element,
284  unsigned short *&target,
285  bool throwException = true);
288  int GetElementCS(unsigned short group,
289  unsigned short element,
290  std::string &target,
291  bool throwException = true);
292 
295  int GetElementPN(unsigned short group,
296  unsigned short element,
297  std::string &target,
298  bool throwException = true);
299 
302  int GetElementIS(unsigned short group,
303  unsigned short element,
304  ::itk::int32_t &target,
305  bool throwException = true);
306 
307  int GetElementISorOB(unsigned short group,
308  unsigned short element,
309  ::itk::int32_t &target,
310  bool throwException = true);
313  int GetElementOB(unsigned short group,
314  unsigned short element,
315  std::string &target,
316  bool throwException = true);
317 
318  int GetElementSQ(unsigned short group,
319  unsigned short entry,
320  DCMTKSequence &sequence,
321  bool throwException = true);
322 
323  int GetElementUI(unsigned short group,
324  unsigned short entry,
325  std::string &target,
326  bool throwException = true);
327 
328  int GetElementDA(unsigned short group,
329  unsigned short element,
330  std::string &target,
331  bool throwException = true);
332 
333  int GetElementTM(unsigned short group,
334  unsigned short element,
335  std::string &target,
336  bool throwException = true);
337 
338  int GetDirCosines(vnl_vector<double> &dir1,
339  vnl_vector<double> &dir2,
340  vnl_vector<double> &dir3);
341 
342  int GetFrameCount() const;
343 
344  int GetSlopeIntercept(double &slope, double &intercept);
345 
346  int GetDimensions(unsigned short &rows, unsigned short &columns);
347 
348  ImageIOBase::IOComponentType GetImageDataType();
349  ImageIOBase::IOPixelType GetImagePixelType();
350 
351  int GetSpacing(double *spacing);
352  int GetOrigin(double *origin);
353 
354  E_TransferSyntax GetTransferSyntax() const;
355 
356  long GetFileNumber() const;
357 
358  static void
359  AddDictEntry(DcmDictEntry *entry);
360 
361 private:
362  static unsigned ascii2hex(char c);
363 
364  static std::string ConvertFromOB(OFString &toConvert);
365 
366  std::string m_FileName;
367  DcmFileFormat* m_DFile;
368  DcmDataset * m_Dataset;
369  E_TransferSyntax m_Xfer;
370  Sint32 m_FrameCount;
371  long m_FileNumber;
372 };
373 
375 }
376 
377 #endif // itkDCMTKFileReader_h
int GetElementDSorOB(unsigned short group, unsigned short element, TType &target, bool throwException=true)
Simplified inverse ITK transforms.
int GetElementDS(unsigned short group, unsigned short element, unsigned short count, TType *target, bool throwException)
bool CompareDCMTKFileReaders(DCMTKFileReader *a, DCMTKFileReader *b)
#define DCMTKException(body)
int GetElementDS(unsigned short group, unsigned short element, unsigned short count, TType *target, bool throwException=true)
int GetSequence(unsigned long index, DCMTKSequence &target, bool throwException=true)
int GetElementTM(unsigned short group, unsigned short element, std::string &target, bool throwException=true)
int GetElementSQ(unsigned short group, unsigned short element, DCMTKSequence &target, bool throwException=true)
void SetDcmSequenceOfItems(DcmSequenceOfItems *seq)
int GetStack(unsigned short group, unsigned short element, DcmStack *resultStack, bool throwException=true)
int GetElementDS(unsigned short group, unsigned short element, std::string &target, bool throwException=true)
int GetElementCS(unsigned short group, unsigned short element, std::string &target, bool throwException=true)
int GetElementFD(unsigned short group, unsigned short element, double *&target, bool throwException=true)