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