ITK  4.9.0
Insight Segmentation and Registration Toolkit
itkLabelToRGBFunctor.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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:
53 
55 
57  {
58  typedef typename TRGBPixel::ValueType 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 colours for plotting and
70  // overlays.
71 
72  AddColor(255, 0, 0);
73  AddColor(0, 205, 0);
74  AddColor(0, 0, 255);
75  AddColor(0, 255, 255);
76  AddColor(255, 0, 255);
77  AddColor(255, 127, 0);
78  AddColor(0, 100, 0);
79  AddColor(138, 43, 226);
80  AddColor(139, 35, 35);
81  AddColor(0, 0, 128);
82  AddColor(139, 139, 0);
83  AddColor(255, 62, 150);
84  AddColor(139, 76, 57);
85  AddColor(0, 134, 139);
86  AddColor(205, 104, 57);
87  AddColor(191, 62, 255);
88  AddColor(0, 139, 69);
89  AddColor(199, 21, 133);
90  AddColor(205, 55, 0);
91  AddColor(32, 178, 170);
92  AddColor(106, 90, 205);
93  AddColor(255, 20, 147);
94  AddColor(69, 139, 116);
95  AddColor(72, 118, 255);
96  AddColor(205, 79, 57);
97  AddColor(0, 0, 205);
98  AddColor(139, 34, 82);
99  AddColor(139, 0, 139);
100  AddColor(238, 130, 238);
101  AddColor(139, 0, 0);
102 
103  // provide some default value for external use (outside
104  // LabelToRGBImageFilter)
105  // Inside LabelToRGBImageFilter, the values are always initialized
109  }
110 
111  inline TRGBPixel operator()(const TLabel & p) const
112  {
113  // value is background
114  // return a gray pixel with the same intensity than the label pixel
115  if ( p == m_BackgroundValue )
116  {
117  return m_BackgroundColor;
118  }
119 
120  // else, return a colored pixel from the color table
121  return m_Colors[p % m_Colors.size()];
122  }
123 
124  void AddColor(unsigned char r, unsigned char g, unsigned char b)
125  {
126  TRGBPixel rgbPixel;
128 
129  typedef typename TRGBPixel::ValueType ValueType;
130 
131  ValueType m = NumericTraits<ValueType>::max();
132 
133  rgbPixel[0] = static_cast< ValueType >( static_cast< double >( r ) / 255 * m );
134  rgbPixel[1] = static_cast< ValueType >( static_cast< double >( g ) / 255 * m );
135  rgbPixel[2] = static_cast< ValueType >( static_cast< double >( b ) / 255 * m );
136  m_Colors.push_back(rgbPixel);
137  }
138 
139  // Empty the color LUT
140  void ResetColors()
141  {
142  m_Colors.clear();
143  }
144 
145  // Get number of colors in the LUT
146  unsigned int GetNumberOfColors() const
147  {
148  return m_Colors.size();
149  }
150 
151  bool operator!=(const Self & l) const
152  {
155  || m_Colors.size() != l.m_Colors.size() )
156  {
157  return true;
158  }
159 
160  // We need to check each color to see if it's different
161  for ( typename std::vector< TRGBPixel >::size_type i = 0; i < m_Colors.size(); ++i )
162  {
163  if ( m_Colors[i] != l.m_Colors[i] )
164  {
165  return true;
166  }
167  }
168  return false;
169  }
170 
171  bool operator==(const Self & other) const
172  {
173  return !( *this != other );
174  }
175 
176  void SetBackgroundValue(TLabel v)
177  {
178  m_BackgroundValue = v;
179  }
180 
181  void SetBackgroundColor(TRGBPixel rgb)
182  {
183  m_BackgroundColor = rgb;
184  }
185 
187 
188  std::vector< TRGBPixel > m_Colors;
189 
190  TRGBPixel m_BackgroundColor;
191 
193 };
194 } // end namespace functor
195 } // end namespace itk
196 
197 #endif
bool operator==(const Self &other) const
bool operator!=(const Self &l) const
std::vector< TRGBPixel > m_Colors
static T max(const T &)
static void SetLength(T &m, const unsigned int s)
Functor for converting labels into RGB triplets.
unsigned int GetNumberOfColors() const
Define additional traits for native types such as int or float.
TRGBPixel operator()(const TLabel &p) const
void AddColor(unsigned char r, unsigned char g, unsigned char b)