fw4spl
DicomDataTools.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwGdcmIO/helper/DicomDataTools.hpp"
8 
9 #include "fwGdcmIO/helper/Encoding.hpp"
10 
11 #include <fwCore/base.hpp>
12 
13 #include <fwTools/IntrinsicTypes.hpp>
14 
15 #include <gdcmGlobal.h>
16 #include <gdcmPhotometricInterpretation.h>
17 #include <gdcmPixelFormat.h>
18 
19 namespace fwGdcmIO
20 {
21 namespace helper
22 {
23 
24 //------------------------------------------------------------------------------
25 
26 typedef std::map< ::fwTools::Type, ::gdcm::PixelFormat::ScalarType > PixelTypeConversionMapType;
27 
28 static const PixelTypeConversionMapType s_PIXEL_TYPE_CONVERSION_MAP = {
29  {::fwTools::Type::create("uint8"), ::gdcm::PixelFormat::UINT8},
30  {::fwTools::Type::create("int8"), ::gdcm::PixelFormat::INT8},
31  // {::fwTools::Type::create("XXX") , ::gdcm::PixelFormat::UINT12} , // Unsupported by VTK Render
32  // {::fwTools::Type::create("XXX") , ::gdcm::PixelFormat::INT12} , // Unsupported by VTK Render
33  {::fwTools::Type::create("uint16"), ::gdcm::PixelFormat::UINT16},
34  {::fwTools::Type::create("int16"), ::gdcm::PixelFormat::INT16},
35  {::fwTools::Type::create("uint32"), ::gdcm::PixelFormat::UINT32},
36  {::fwTools::Type::create("int32"), ::gdcm::PixelFormat::INT32},
37  // { ::fwTools::Type::create("XXX") , ::gdcm::PixelFormat::FLOAT16} , // Unsupported by VTK Render
38  {::fwTools::Type::create("float"), ::gdcm::PixelFormat::FLOAT32},
39  {::fwTools::Type::create("double"), ::gdcm::PixelFormat::FLOAT64}
40 };
41 
42 //------------------------------------------------------------------------------
43 
44 const ::gdcm::PixelFormat DicomDataTools::getPixelType(const ::fwData::Image::csptr& image)
45 {
46  auto it = s_PIXEL_TYPE_CONVERSION_MAP.find(image->getType());
47  if(it != s_PIXEL_TYPE_CONVERSION_MAP.end())
48  {
49  return it->second;
50  }
51  return ::gdcm::PixelFormat::UNKNOWN;
52 }
53 
54 //------------------------------------------------------------------------------
55 
56 const ::gdcm::PhotometricInterpretation
57 DicomDataTools::getPhotometricInterpretation(const ::fwData::Image::csptr& image)
58 {
59  ::gdcm::PhotometricInterpretation pi;
60  const size_t components = image->getNumberOfComponents();
61 
62  // Attempt a guess (VTK do the same choice)
63  switch (components)
64  {
65  case 1: // It could well be MONOCHROME1
66  pi = ::gdcm::PhotometricInterpretation::MONOCHROME2;
67  break;
68  case 3: // It could well be YBR
69  pi = ::gdcm::PhotometricInterpretation::RGB;
70  break;
71  case 4: // It could well be CMYK
72  pi = ::gdcm::PhotometricInterpretation::ARGB;
73  break;
74  default:
75  SLM_ERROR("Photometric interpretation not found");
76  pi = ::gdcm::PhotometricInterpretation::UNKNOWN;
77  break;
78  }
79 
80  return pi;
81 }
82 
83 //------------------------------------------------------------------------------
84 
86  ::fwData::Material::RepresentationType representationMode)
87 {
88  switch (representationMode)
89  {
90  case ::fwData::Material::SURFACE:
91  return ::gdcm::Surface::SURFACE;
92  break;
93  case ::fwData::Material::POINT:
94  return ::gdcm::Surface::POINTS;
95  break;
96  case ::fwData::Material::WIREFRAME:
97  return ::gdcm::Surface::WIREFRAME;
98  break;
99  default:
100  SLM_WARN("Representation type not handle (changed to : SURFACE)");
101  return ::gdcm::Surface::SURFACE;
102  }
103 }
104 
105 //------------------------------------------------------------------------------
106 
108  ::gdcm::Surface::VIEWType presentationType)
109 {
110  switch (presentationType)
111  {
112  case ::gdcm::Surface::SURFACE:
113  return ::fwData::Material::SURFACE;
114  break;
115  case ::gdcm::Surface::WIREFRAME:
116  return ::fwData::Material::WIREFRAME;
117  break;
118  case ::gdcm::Surface::POINTS:
119  return ::fwData::Material::POINT;
120  break;
121  default:
122  SLM_WARN("Presentation type not handle (changed to : SURFACE)");
123  return ::fwData::Material::SURFACE;
124  }
125 }
126 
127 //------------------------------------------------------------------------------
128 
129 std::size_t DicomDataTools::convertPointToFrameNumber(const ::fwData::Image::csptr& image,
130  const ::fwData::Point::csptr& point)
132 {
133  // Retrieve Z spacing
134  const double zSpacing = (image->getNumberOfDimensions() > 2) ? (image->getSpacing()[2]) : 1;
135 
136  // Retrieve Z coordinate of image origin
137  const double zOrigin = (image->getNumberOfDimensions() > 2) ? (image->getOrigin()[2]) : 0;
138 
139  // Retrieve Z coordinate
140  const double zCoordinate = static_cast<double>(point->getCoord()[2]);
141 
142  // Compute frame number
143  const std::size_t frameNumber = static_cast<std::size_t>(floor((zCoordinate - zOrigin) / zSpacing + 0.5)) + 1;
144  FW_RAISE_EXCEPTION_IF(::fwGdcmIO::exception::Failed("Coordinates out of image bounds."),
145  frameNumber < 1 || frameNumber > image->getSize()[2]);
146 
147  return frameNumber;
148 }
149 
150 //------------------------------------------------------------------------------
151 
152 double DicomDataTools::convertFrameNumberToZCoordinate(const ::fwData::Image::csptr& image,
153  const std::size_t frameNumber)
155 {
156  // Retrieve Z spacing
157  const double zSpacing = (image->getNumberOfDimensions() > 2) ? (image->getSpacing()[2]) : 1;
158 
159  // Retrieve Z coordinate of image origin
160  const double zOrigin = (image->getNumberOfDimensions() > 2) ? (image->getOrigin()[2]) : 0;
161 
162  // Compute coordinate
163  const std::size_t frameIndex = (frameNumber-1);
164  FW_RAISE_EXCEPTION_IF(::fwGdcmIO::exception::Failed("Coordinates out of image bounds."),
165  frameIndex >= image->getSize()[2]);
166  const double zCoordinate = zOrigin + static_cast<double>(frameIndex) * zSpacing;
167 
168  return zCoordinate;
169 }
170 
171 //------------------------------------------------------------------------------
172 
173 } //namespace helper
174 } //namespace fwGdcmIO
static FWGDCMIO_API::fwData::Material::RepresentationType convertToRepresentationMode(::gdcm::Surface::VIEWType presentationType)
Convert a surface recommended presentation type (DICOM) into representation mode (FW4SPL).
static FWGDCMIO_API double convertFrameNumberToZCoordinate(const ::fwData::Image::csptr &image, const std::size_t frameNumber)
Convert a frame number to a Z coordinate.
The namespace fwGdcmIO contains reader, writer and helper for dicom data.
#define SLM_WARN(message)
Definition: spyLog.hpp:261
static FWGDCMIO_APIconst::gdcm::PhotometricInterpretation getPhotometricInterpretation(const ::fwData::Image::csptr &image)
Return the photometric interpretation of an acquisition.
#define SLM_ERROR(message)
Definition: spyLog.hpp:272
Implements a failed exception class for fwGdcmIO.
static FWGDCMIO_APIconst::gdcm::PixelFormat getPixelType(const ::fwData::Image::csptr &image)
Return the pixel type of a fwData Image.
RepresentationType
Representation models.
static FWGDCMIO_API std::size_t convertPointToFrameNumber(const ::fwData::Image::csptr &image, const ::fwData::Point::csptr &point)
Convert a 3D point to the closest frame number index.
static FWGDCMIO_API::gdcm::Surface::VIEWType convertToPresentationType(::fwData::Material::RepresentationType representationMode)
Convert a surface representation mode (FW4SPL) into recommended presentation type (DICOM)...