fw4spl
core/fwDicomTools/src/fwDicomTools/Image.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2016.
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 "fwDicomTools/Image.hpp"
8 
9 #include <fwCore/spyLog.hpp>
10 
11 namespace fwDicomTools
12 {
13 
15  unsigned short samplesPerPixel,
16  unsigned short bitsAllocated,
17  unsigned short bitsStored,
18  unsigned short highBit,
19  unsigned short pixelRepresentation,
20  double rescaleSlope,
21  double rescaleIntercept) :
22  m_samplesPerPixel(samplesPerPixel),
23  m_bitsAllocated(bitsAllocated),
24  m_bitsStored(bitsStored),
25  m_highBit(highBit),
26  m_pixelRepresentation(pixelRepresentation),
27  m_rescaleSlope(rescaleSlope),
28  m_rescaleIntercept(rescaleIntercept)
29 {
30 }
31 
32 //-----------------------------------------------------------------------------
33 
35 {
36 }
37 
38 //-----------------------------------------------------------------------------
39 
41 {
42  ::fwTools::Type result = ::fwTools::Type::s_UNSPECIFIED_TYPE;
43 
44  // Bool & Monochrome values
45  if(m_bitsAllocated == 1 && m_pixelRepresentation == 0)
46  {
47  result = ::fwTools::Type::s_INT8;
48  }
49  else
50  {
51  // Double
53  {
54  result = ::fwTools::Type::s_DOUBLE;
55  }
56  else
57  {
58  const double min = m_rescaleSlope * (double)this->getPixelMin() + m_rescaleIntercept;
59  const double max = m_rescaleSlope * (double)this->getPixelMax() + m_rescaleIntercept;
60 
61  SLM_ASSERT("Min must be lower than max.", min <= max );
62  SLM_ASSERT("Min and max values must be of type int64_t.", min == (int64_t)min && max == (int64_t)max );
63 
64  // Unsigned values
65  if(min >= 0)
66  {
67  if( max <= std::numeric_limits<uint8_t>::max() )
68  {
69  result = ::fwTools::Type::s_UINT8;
70  }
71  else if( max <= std::numeric_limits<uint16_t>::max() )
72  {
73  result = ::fwTools::Type::s_UINT16;
74  }
75  else if( max <= std::numeric_limits<uint32_t>::max() )
76  {
77  result = ::fwTools::Type::s_UINT32;
78  }
79  else
80  {
81  SLM_ASSERT("Unable to determine the pixel format.", 0 );
82  }
83  }
84  // Signed values
85  else
86  {
87 
88  if( max <= std::numeric_limits<int8_t>::max() && min >= std::numeric_limits<int8_t>::min() )
89  {
90  result = ::fwTools::Type::s_INT8;
91  }
92  else if( max <= std::numeric_limits<int16_t>::max() && min >= std::numeric_limits<int16_t>::min() )
93  {
94  result = ::fwTools::Type::s_INT16;
95  }
96  else if( max <= std::numeric_limits<int32_t>::max() && min >= std::numeric_limits<int32_t>::min() )
97  {
98  result = ::fwTools::Type::s_INT32;
99  }
100  else
101  {
102  SLM_ASSERT("Unable to determine the pixel format.", 0 );
103  }
104  }
105 
106  }
107 
108  }
109 
110  return result;
111 }
112 
113 //-----------------------------------------------------------------------------
114 
115 int64_t Image::getPixelMin() const
116 {
117  SLM_ASSERT("The number of bits allocated must be known", m_bitsAllocated);
118  SLM_ASSERT("The number of bits stored must be less than 33.", m_bitsStored <= 32);
119 
120  if(m_pixelRepresentation == 1)
121  {
122  return (int64_t)(~(((1ull << m_bitsStored) - 1) >> 1));
123  }
124  else if(m_pixelRepresentation == 0)
125  {
126  return 0;
127  }
128 
129  SLM_ASSERT("Unable to determine minimum value of pixel", 0);
130  return 0;
131 }
132 
133 //-----------------------------------------------------------------------------
134 
135 int64_t Image::getPixelMax() const
136 {
137  SLM_ASSERT("The number of bits allocated must be known", m_bitsAllocated);
138  SLM_ASSERT("The number of bits stored must be less than 33.", m_bitsStored <= 32);
139 
140  if(m_pixelRepresentation == 1)
141  {
142  return (int64_t)(((1ull << m_bitsStored) - 1) >> 1);
143  }
144  else if(m_pixelRepresentation == 0)
145  {
146  return (int64_t)((1ull << m_bitsStored) - 1);
147  }
148 
149  SLM_ASSERT("Unable to determine maximum value of pixel", 0);
150  return 0;
151 }
152 
153 } //fwDicomTools
FWDICOMTOOLS_API::fwTools::Type findImageTypeFromMinMaxValues() const
Find Image Type.
FWDICOMTOOLS_API int64_t getPixelMin() const
Return minimum possible value of the pixel.
virtual FWDICOMTOOLS_API ~Image()
Destructor.
unsigned short m_pixelRepresentation
Pixel representation.
FWDICOMTOOLS_API Image(unsigned short samplesPerPixel, unsigned short bitsAllocated, unsigned short bitsStored, unsigned short highBit, unsigned short pixelRepresentation, double rescaleSlope, double rescaleIntercept)
Constructor.
unsigned short m_bitsAllocated
Bits allocated.
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
Class describing an elementary C++ type aka unsigned char, signed char, .... int, float...
Definition: Type.hpp:32
fwDicomTools contains general tools to handle DICOM format
FWDICOMTOOLS_API int64_t getPixelMax() const
Return maximum possible value of the pixel.
This file defines SpyLog macros. These macros are used to log messages to a file or to the console du...