ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkLabelToRGBFunctor.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkLabelToRGBFunctor_h
00019 #define __itkLabelToRGBFunctor_h
00020 
00021 #include <vector>
00022 #include "itkNumericTraits.h"
00023 
00024 namespace itk
00025 {
00026 namespace Functor
00027 {
00049 template< class TLabel, class TRGBPixel >
00050 class LabelToRGBFunctor
00051 {
00052 public:
00053 
00054   typedef LabelToRGBFunctor Self;
00055 
00056   LabelToRGBFunctor()
00057   {
00058     TRGBPixel rgbPixel;
00059 
00060     typedef typename TRGBPixel::ValueType ValueType;
00061 
00062     // the following colors are from "R", and named:
00063     // "red"             "green3"          "blue"            "cyan"
00064     //"magenta"         "darkorange1"     "darkgreen"       "blueviolet"
00065     //"brown4"          "navy"            "yellow4"         "violetred1"
00066     //"salmon4"         "turquoise4"      "sienna3"         "darkorchid1"
00067     //"springgreen4"    "mediumvioletred" "orangered3"      "lightseagreen"
00068     //"slateblue"       "deeppink1"       "aquamarine4"     "royalblue1"
00069     //"tomato3"         "mediumblue"      "violetred4"      "darkmagenta"
00070     //"violet"          "red4"
00071     // They are a good selection of distinct colours for plotting and
00072     // overlays.
00073 
00074     AddColor(255, 0, 0);
00075     AddColor(0, 205, 0);
00076     AddColor(0, 0, 255);
00077     AddColor(0, 255, 255);
00078     AddColor(255, 0, 255);
00079     AddColor(255, 127, 0);
00080     AddColor(0, 100, 0);
00081     AddColor(138, 43, 226);
00082     AddColor(139, 35, 35);
00083     AddColor(0, 0, 128);
00084     AddColor(139, 139, 0);
00085     AddColor(255, 62, 150);
00086     AddColor(139, 76, 57);
00087     AddColor(0, 134, 139);
00088     AddColor(205, 104, 57);
00089     AddColor(191, 62, 255);
00090     AddColor(0, 139, 69);
00091     AddColor(199, 21, 133);
00092     AddColor(205, 55, 0);
00093     AddColor(32, 178, 170);
00094     AddColor(106, 90, 205);
00095     AddColor(255, 20, 147);
00096     AddColor(69, 139, 116);
00097     AddColor(72, 118, 255);
00098     AddColor(205, 79, 57);
00099     AddColor(0, 0, 205);
00100     AddColor(139, 34, 82);
00101     AddColor(139, 0, 139);
00102     AddColor(238, 130, 238);
00103     AddColor(139, 0, 0);
00104 
00105     // provide some default value for external use (outside
00106     // LabelToRGBImageFilter)
00107     // Inside LabelToRGBImageFilter, the values are always initialized
00108     NumericTraits<TRGBPixel>::SetLength( m_BackgroundColor, 3);
00109     m_BackgroundColor.Fill(NumericTraits< ValueType >::Zero);
00110     m_BackgroundValue = NumericTraits< TLabel >::Zero;
00111   }
00112 
00113   inline TRGBPixel operator()(const TLabel & p) const
00114   {
00115     // value is background
00116     // return a gray pixel with the same intensity than the label pixel
00117     if ( p == m_BackgroundValue )
00118       {
00119       return m_BackgroundColor;
00120       }
00121 
00122     // else, return a colored pixel from the color table
00123     return m_Colors[p % m_Colors.size()];
00124   }
00125 
00126   void AddColor(unsigned char r, unsigned char g, unsigned char b)
00127   {
00128     TRGBPixel rgbPixel;
00129     NumericTraits<TRGBPixel>::SetLength(rgbPixel, 3);
00130 
00131     typedef typename TRGBPixel::ValueType ValueType;
00132 
00133     ValueType m = NumericTraits<ValueType>::max();
00134 
00135     rgbPixel[0] = static_cast< ValueType >( static_cast< double >( r ) / 255 * m );
00136     rgbPixel[1] = static_cast< ValueType >( static_cast< double >( g ) / 255 * m );
00137     rgbPixel[2] = static_cast< ValueType >( static_cast< double >( b ) / 255 * m );
00138     m_Colors.push_back(rgbPixel);
00139   }
00140 
00141   // Empty the color LUT
00142   void ResetColors()
00143   {
00144     m_Colors.clear();
00145   }
00146 
00147   // Get number of colors in the LUT
00148   unsigned int GetNumberOfColors() const
00149   {
00150     return m_Colors.size();
00151   }
00152 
00153   bool operator!=(const Self & l) const
00154   {
00155     const bool areDifferent = m_BackgroundColor != l.m_BackgroundColor
00156                               || m_BackgroundValue != l.m_BackgroundValue;
00157 
00158     return areDifferent;
00159   }
00160 
00161   bool operator==(const Self & other) const
00162   {
00163     return !( *this != other );
00164   }
00165 
00166   void SetBackgroundValue(TLabel v)
00167   {
00168     m_BackgroundValue = v;
00169   }
00170 
00171   void SetBackgroundColor(TRGBPixel rgb)
00172   {
00173     m_BackgroundColor = rgb;
00174   }
00175 
00176   ~LabelToRGBFunctor() {}
00177 
00178   std::vector< TRGBPixel > m_Colors;
00179 
00180   TRGBPixel m_BackgroundColor;
00181 
00182   TLabel m_BackgroundValue;
00183 };
00184 }  // end namespace functor
00185 }  // end namespace itk
00186 
00187 #endif
00188