ITK  5.2.0
Insight Toolkit
itkLabelToRGBFunctor.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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 itkLabelToRGBFunctor_h
19 #define itkLabelToRGBFunctor_h
20 
21 #include <vector>
22 #include "itkNumericTraits.h"
23 
24 namespace itk
25 {
26 namespace Functor
27 {
49 template <typename TLabel, typename TRGBPixel>
51 {
52 public:
54 
56  {
57  using ValueType = typename TRGBPixel::ValueType;
58 
59  // the following colors are from "R", and named:
60  // "red" "green3" "blue" "cyan"
61  //"magenta" "darkorange1" "darkgreen" "blueviolet"
62  //"brown4" "navy" "yellow4" "violetred1"
63  //"salmon4" "turquoise4" "sienna3" "darkorchid1"
64  //"springgreen4" "mediumvioletred" "orangered3" "lightseagreen"
65  //"slateblue" "deeppink1" "aquamarine4" "royalblue1"
66  //"tomato3" "mediumblue" "violetred4" "darkmagenta"
67  //"violet" "red4"
68  // They are a good selection of distinct colors for plotting and
69  // overlays.
70  constexpr size_t numColors = 30;
71  constexpr unsigned char colors[numColors][3] = {
72  { 255, 0, 0 }, { 0, 205, 0 }, { 0, 0, 255 }, { 0, 255, 255 }, { 255, 0, 255 }, { 255, 127, 0 },
73  { 0, 100, 0 }, { 138, 43, 226 }, { 139, 35, 35 }, { 0, 0, 128 }, { 139, 139, 0 }, { 255, 62, 150 },
74  { 139, 76, 57 }, { 0, 134, 139 }, { 205, 104, 57 }, { 191, 62, 255 }, { 0, 139, 69 }, { 199, 21, 133 },
75  { 205, 55, 0 }, { 32, 178, 170 }, { 106, 90, 205 }, { 255, 20, 147 }, { 69, 139, 116 }, { 72, 118, 255 },
76  { 205, 79, 57 }, { 0, 0, 205 }, { 139, 34, 82 }, { 139, 0, 139 }, { 238, 130, 238 }, { 139, 0, 0 }
77  };
78 
79  for (auto & color : colors)
80  {
81  AddColor(color[0], color[1], color[2]);
82  }
83 
84  // provide some default value for external use (outside
85  // LabelToRGBImageFilter)
86  // Inside LabelToRGBImageFilter, the values are always initialized
90  }
91 
92  inline TRGBPixel
93  operator()(const TLabel & p) const
94  {
95  // value is background
96  // return a gray pixel with the same intensity than the label pixel
97  if (p == m_BackgroundValue)
98  {
99  return m_BackgroundColor;
100  }
101 
102  // else, return a colored pixel from the color table
103  return m_Colors[p % m_Colors.size()];
104  }
105 
106  void
107  AddColor(unsigned char r, unsigned char g, unsigned char b)
108  {
109  TRGBPixel rgbPixel;
111 
112  using ValueType = typename TRGBPixel::ValueType;
113 
114  ValueType m = NumericTraits<ValueType>::max();
115 
116  rgbPixel[0] = static_cast<ValueType>(static_cast<double>(r) / 255 * m);
117  rgbPixel[1] = static_cast<ValueType>(static_cast<double>(g) / 255 * m);
118  rgbPixel[2] = static_cast<ValueType>(static_cast<double>(b) / 255 * m);
119  m_Colors.push_back(rgbPixel);
120  }
121 
122  // Empty the color LUT
123  void
125  {
126  m_Colors.clear();
127  }
128 
129  // Get number of colors in the LUT
130  unsigned int
132  {
133  return static_cast<unsigned int>(m_Colors.size());
134  }
135 
136  bool
137  operator!=(const Self & l) const
138  {
140  m_Colors.size() != l.m_Colors.size())
141  {
142  return true;
143  }
144 
145  // We need to check each color to see if it's different
146  for (typename std::vector<TRGBPixel>::size_type i = 0; i < m_Colors.size(); ++i)
147  {
148  if (m_Colors[i] != l.m_Colors[i])
149  {
150  return true;
151  }
152  }
153  return false;
154  }
155 
156  bool
157  operator==(const Self & other) const
158  {
159  return !(*this != other);
160  }
161 
162  void
164  {
165  m_BackgroundValue = v;
166  }
167 
168  void
169  SetBackgroundColor(TRGBPixel rgb)
170  {
171  m_BackgroundColor = rgb;
172  }
173 
174  ~LabelToRGBFunctor() = default;
175 
176  std::vector<TRGBPixel> m_Colors;
177 
178  TRGBPixel m_BackgroundColor;
179 
181 };
182 } // end namespace Functor
183 } // end namespace itk
184 
185 #endif
itk::Functor::LabelToRGBFunctor::GetNumberOfColors
unsigned int GetNumberOfColors() const
Definition: itkLabelToRGBFunctor.h:131
itk::Functor::LabelToRGBFunctor::m_Colors
std::vector< TRGBPixel > m_Colors
Definition: itkLabelToRGBFunctor.h:176
itk::Functor::LabelToRGBFunctor
Functor for converting labels into RGB triplets.
Definition: itkLabelToRGBFunctor.h:50
itk::Functor::LabelToRGBFunctor::AddColor
void AddColor(unsigned char r, unsigned char g, unsigned char b)
Definition: itkLabelToRGBFunctor.h:107
itk::Functor::LabelToRGBFunctor::operator!=
bool operator!=(const Self &l) const
Definition: itkLabelToRGBFunctor.h:137
itk::NumericTraits::SetLength
static void SetLength(T &m, const unsigned int s)
Definition: itkNumericTraits.h:185
itk::Functor::LabelToRGBFunctor::LabelToRGBFunctor
LabelToRGBFunctor()
Definition: itkLabelToRGBFunctor.h:55
itk::Functor::LabelToRGBFunctor::SetBackgroundColor
void SetBackgroundColor(TRGBPixel rgb)
Definition: itkLabelToRGBFunctor.h:169
itk::Functor::LabelToRGBFunctor::operator()
TRGBPixel operator()(const TLabel &p) const
Definition: itkLabelToRGBFunctor.h:93
itk::Functor::LabelToRGBFunctor::~LabelToRGBFunctor
~LabelToRGBFunctor()=default
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:58
itk::NumericTraits::max
static constexpr T max(const T &)
Definition: itkNumericTraits.h:167
itk::Functor::LabelToRGBFunctor::ResetColors
void ResetColors()
Definition: itkLabelToRGBFunctor.h:124
itk::NumericTraits::ZeroValue
static T ZeroValue()
Definition: itkNumericTraits.h:148
itk::Functor::LabelToRGBFunctor::m_BackgroundColor
TRGBPixel m_BackgroundColor
Definition: itkLabelToRGBFunctor.h:178
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::Functor::LabelToRGBFunctor::SetBackgroundValue
void SetBackgroundValue(TLabel v)
Definition: itkLabelToRGBFunctor.h:163
itkNumericTraits.h
itk::Functor::LabelToRGBFunctor::m_BackgroundValue
TLabel m_BackgroundValue
Definition: itkLabelToRGBFunctor.h:180
itk::Functor::LabelToRGBFunctor::operator==
bool operator==(const Self &other) const
Definition: itkLabelToRGBFunctor.h:157