Orfeo Toolbox  3.16
itkDICOMImageIO2.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkDICOMImageIO2.cxx,v $
5  Language: C++
6  Date: $Date: 2008-12-29 19:27:37 $
7  Version: $Revision: 1.31 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 
18 #ifdef _MSC_VER
19 #pragma warning(disable:4786)
20 #endif
21 
22 #include "itkArray.h"
23 #include "itkDICOMImageIO2.h"
24 #include "itkExceptionObject.h"
25 #include "itkByteSwapper.h"
26 #include "itkMetaDataDictionary.h"
27 #include "itkMetaDataObject.h"
28 
29 #include <iostream>
30 #include <list>
31 
32 #include <math.h>
33 #include <stdio.h>
34 #include <string.h>
35 
36 #include "DICOMCallback.h"
37 
38 namespace itk
39 {
40 
43 {
44  // We are going to default the dimensions for a DICOM image to 3.
45  // The ImagePositionPatient field in a DICOM image is a 3D position
46  // even though the image is just 2D. So from the ImageIO
47  // standpoint, a single DICOM image will be a 3D image with just one
48  // slice. The ImageFileReader may reduce this down to a 2D image if
49  // the user only asks for a 2D image.
50  this->SetNumberOfDimensions(3);
51 
55  m_Parser = new itkdicomparser::DICOMParser();
56  m_AppHelper = new itkdicomparser::DICOMAppHelper();
57 }
58 
59 
62 {
63  delete m_Parser;
64  delete m_AppHelper;
65 }
66 
67 bool DICOMImageIO2::CanReadFile( const char* filename )
68 {
69  bool open = m_Parser->OpenFile( filename);
70  if (!open)
71  {
72  return false;
73  }
74  bool magic = m_Parser->IsDICOMFile();
75  return magic;
76 }
77 
79  doublebyte,
80  itkdicomparser::DICOMParser::VRTypes,
81  unsigned char* val,
82  quadbyte len)
83 {
84  unsigned int imageBytes = static_cast<unsigned int>( this->GetImageSizeInBytes() );
85  if (len < 0)
86  {
87  len = 0;
88  }
89  if (len < static_cast<int>( imageBytes ) )
90  {
91  imageBytes = len;
92  }
93  memcpy( m_ImageDataBuffer, val, imageBytes );
94 }
95 
96 void DICOMImageIO2::Read(void* buffer)
97 {
98  m_Parser->ClearAllDICOMTagCallbacks();
99  m_AppHelper->RegisterCallbacks(m_Parser);
100  m_AppHelper->RegisterPixelDataCallback(m_Parser);
101 
102  bool open = m_Parser->OpenFile(m_FileName.c_str());
103  if (!open)
104  {
105  return;
106  }
107 
108  // Should ReadHeader() be Read() since more than just a header is read?
109  m_Parser->ReadHeader();
110 
111  Array<float> imagePosition(3);
112  imagePosition[0] = m_AppHelper->GetImagePositionPatient()[0];
113  imagePosition[1] = m_AppHelper->GetImagePositionPatient()[1];
114  imagePosition[2] = m_AppHelper->GetImagePositionPatient()[2];
115 
116  EncapsulateMetaData<Array<float> >(this->GetMetaDataDictionary(),
117  "ITK_ImageOrigin",
118  imagePosition);
119 
120  void* newData;
121  itkdicomparser::DICOMParser::VRTypes newType;
122  unsigned long imageDataLength = 0;
123 
124 
125  m_AppHelper->GetImageData(newData, newType, imageDataLength);
126  memcpy(buffer, newData, imageDataLength);
127 
128  // Clean up
129  m_AppHelper->Clear();
130 }
131 
132 
137 {
138  m_Parser->ClearAllDICOMTagCallbacks();
139  m_AppHelper->RegisterCallbacks(m_Parser);
140 
141  bool open = m_Parser->OpenFile(m_FileName.c_str());
142  if (!open)
143  {
144  return;
145  }
146 
147  m_Parser->ReadHeader();
148 
149  Array<float> imagePosition(3);
150  imagePosition[0] = m_AppHelper->GetImagePositionPatient()[0];
151  imagePosition[1] = m_AppHelper->GetImagePositionPatient()[1];
152  imagePosition[2] = m_AppHelper->GetImagePositionPatient()[2];
153 
154  Array<float> imageSpacing(3);
155  imageSpacing[0] = m_AppHelper->GetPixelSpacing()[0];
156  imageSpacing[1] = m_AppHelper->GetPixelSpacing()[1];
157  imageSpacing[2] = m_AppHelper->GetPixelSpacing()[2];
158 
159  EncapsulateMetaData<Array<float> >(this->GetMetaDataDictionary(),
160  "ITK_ImageOrigin",
161  imagePosition);
162 
163  int* dims = m_AppHelper->GetDimensions();
164 
165  for (int i = 0; i < 3; i++)
166  {
167  this->SetOrigin(i, imagePosition[i]);
168  this->SetSpacing(i, imageSpacing[i]);
169  }
170 
171  for (int i = 0; i < 2; i++)
172  {
173  this->SetDimensions(i, dims[i]);
174  }
175  this->SetDimensions(2, 1); // single slice in a 3D image
176 
177  int numBits = m_AppHelper->GetBitsAllocated();
178  bool sign = m_AppHelper->RescaledImageDataIsSigned();
179 
180  bool isFloat = m_AppHelper->RescaledImageDataIsFloat();
181  int num_comp = m_AppHelper->GetNumberOfComponents();
182 
183  if (isFloat) // Float
184  {
187  }
188  else if (num_comp == 3) //RGB
189  {
190  if (numBits == 8)
191  {
194  }
195  else
196  {
199  }
200  }
201  else // Everything else
202  {
203  if (numBits == 8)
204  {
205  if (sign)
206  {
209  }
210  else
211  {
214  }
215  }
216  else if (numBits == 16)
217  {
218  if (sign)
219  {
222  }
223  else
224  {
227  }
228  }
229  else
230  {
233  }
234  }
235 
236  this->SetNumberOfComponents(num_comp);
237 
238  // Cleanup
239  m_AppHelper->Clear();
240 }
241 
243 void DICOMImageIO2::PrintSelf(std::ostream& os, Indent indent) const
244 {
245  unsigned int i;
246  Superclass::PrintSelf(os, indent);
247  os << indent << "Spacing: ( ";
248  for (i=0; i < m_NumberOfDimensions; i++)
249  {
250  os << m_Spacing[i] << " ";
251  }
252  os << " )\n";
253  os << indent << "Origin: ( ";
254  for (i=0; i < m_Origin.size(); i++)
255  {
256  os << m_Origin[i] << " ";
257  }
258  os << " )" << std::endl;
259 }
260 
261 
262 void
264 ::GetPatientName(char* name)
265 {
266  m_AppHelper->GetPatientName(name);
267 }
268 
269 
270 void
272 ::GetPatientID(char* id)
273 {
274  m_AppHelper->GetPatientID(id);
275 }
276 
277 void
279 ::GetPatientSex(char* sex)
280 {
281  m_AppHelper->GetPatientSex(sex);
282 }
283 
284 void
286 ::GetPatientAge(char* age)
287 {
288  m_AppHelper->GetPatientAge(age);
289 }
290 
291 void
293 ::GetPatientDOB(char* dob)
294 {
295  m_AppHelper->GetPatientDOB(dob);
296 }
297 
298 void
300 ::GetStudyID(char* id)
301 {
302  m_AppHelper->GetStudyID(id);
303 }
304 
305 
306 void
309 {
310  m_AppHelper->GetStudyDescription(desc);
311 }
312 
313 void
315 ::GetBodyPart(char* part)
316 {
317  m_AppHelper->GetBodyPart(part);
318 }
319 
320 void
323 {
324  m_AppHelper->GetNumberOfSeriesInStudy(series);
325 }
326 
327 
328 void
331 {
332  m_AppHelper->GetNumberOfStudyRelatedSeries(series);
333 }
334 
335 void
337 ::GetStudyDate(char* date)
338 {
339  m_AppHelper->GetStudyDate(date);
340 }
341 
342 void
344 ::GetModality(char* modality)
345 {
346  m_AppHelper->GetModality(modality);
347 }
348 
349 void
351 ::GetManufacturer(char* manu)
352 {
353  m_AppHelper->GetManufacturer(manu);
354 }
355 
356 void
359 {
360  m_AppHelper->GetInstitution(ins);
361 }
362 
363 void
365 ::GetModel(char* model)
366 {
367  m_AppHelper->GetModel(model);
368 }
369 
370 } // end namespace itk

Generated at Sat May 18 2013 23:36:20 for Orfeo Toolbox with doxygen 1.8.3.1