ColorMap.C

Go to the documentation of this file.
00001 /*!@file Image/ColorMap.C Simple colormaps */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the University of Southern California (USC) and the iLab at USC.  //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file:
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Image/ColorMap.C $
00035 // $Id: ColorMap.C 13690 2010-07-23 19:50:36Z lior $
00036 //
00037 
00038 #ifndef IMAGE_COLORMAP_C_DEFINED
00039 #define IMAGE_COLORMAP_C_DEFINED
00040 
00041 #include "Image/ColorMap.H"
00042 
00043 #include "Image/MatrixOps.H"
00044 #include "Util/log.H"
00045 
00046 // ######################################################################
00047 ColorMap::ColorMap() :
00048   Image< PixRGB<byte> >()
00049 { }
00050 
00051 // ######################################################################
00052 ColorMap::ColorMap(const int n) :
00053   Image< PixRGB<byte> >(n, 1, NO_INIT)
00054 { }
00055 
00056 // ######################################################################
00057 ColorMap::ColorMap(const Image< PixRGB<byte> >& cmap) :
00058   Image< PixRGB<byte> >(cmap.getHeight() != 1 ? transpose(cmap) : cmap)
00059 {
00060   if (this->getHeight() != 1) LFATAL("Invalid colormap image height != 1");
00061 }
00062 
00063 // ######################################################################
00064 ColorMap::~ColorMap()
00065 { }
00066 
00067 // ######################################################################
00068 ColorMap ColorMap::GREY(const int n)
00069 {
00070   Image< PixRGB<byte> > cmap(n, 1, NO_INIT);
00071   Image< PixRGB<byte> >::iterator ptr = cmap.beginw();
00072   for (int i = 0; i < n; i++)
00073     *ptr++ = PixRGB<byte>(byte((i * 256) / n));
00074   return ColorMap(cmap);
00075 }
00076 
00077 // ######################################################################
00078 ColorMap ColorMap::GRADIENT(const PixRGB<byte>& from,
00079                             const PixRGB<byte>& to,
00080                             const int n)
00081 {
00082   Image< PixRGB<byte> > cmap(n, 1, NO_INIT);
00083   Image< PixRGB<byte> >::iterator ptr = cmap.beginw();
00084 
00085   for (int i = 0; i < n; i++)
00086     *ptr++ = PixRGB<byte>(PixRGB<float>(from) +
00087                           (PixRGB<float>(to) - PixRGB<float>(from)) *
00088                           float(i) / float(n));
00089   return ColorMap(cmap);
00090 }
00091 
00092 // ######################################################################
00093 ColorMap ColorMap::JET(const int m)
00094 {
00095   Image<PixRGB<byte> > cmap(m, 1, NO_INIT);
00096   const Image<PixRGB<byte> >::iterator ptr = cmap.beginw();
00097 
00098   const int n = m/4;          // e.g., 64 if cmapsize=256
00099   const int mid1 = n/2;       // e.g., 32 if cmapsize=256
00100   const int mid2 = mid1 + n;  // e.g., 96 if cmapsize=256
00101   const int mid3 = mid2 + n;  // e.g., 160 if cmapsize=256
00102   const int mid4 = mid3 + n;  // e.g., 224 if cmapsize=256
00103 
00104 #define LINMAP(i, a_in, b_in, a_out, b_out)     \
00105   byte(255.0 * ((a_out)                         \
00106                 + double((i) - (a_in))          \
00107                 * ((b_out)-(a_out))             \
00108                 / ((b_in) - (a_in))))
00109 
00110   for (int i = 0; i < mid1; ++i)
00111     ptr[i] = PixRGB<byte>(0,
00112                           0,
00113                           LINMAP(i, 0, mid1, 0.5, 1.0));
00114 
00115   for (int i = mid1; i < mid2; ++i)
00116     ptr[i] = PixRGB<byte>(0,
00117                           LINMAP(i, mid1, mid2, 0.0, 1.0),
00118                           255);
00119 
00120   for (int i = mid2; i < mid3; ++i)
00121     ptr[i] = PixRGB<byte>(LINMAP(i, mid2, mid3, 0.0, 1.0),
00122                           255,
00123                           LINMAP(i, mid2, mid3, 1.0, 0.0));
00124 
00125   for (int i = mid3; i < mid4; ++i)
00126     ptr[i] = PixRGB<byte>(255,
00127                           LINMAP(i, mid3, mid4, 1.0, 0.0),
00128                           0);
00129 
00130   for (int i = mid4; i < m; ++i)
00131     ptr[i] = PixRGB<byte>(LINMAP(i, mid4, m, 1.0, 0.5),
00132                           0,
00133                           0);
00134 
00135 #undef LINMAP
00136 
00137   return ColorMap(cmap);
00138 }
00139 
00140 ColorMap ColorMap::LINES(const int m)
00141 {
00142   Image<PixRGB<byte> > cmap(m, 1, NO_INIT);
00143   const Image<PixRGB<byte> >::iterator ptr = cmap.beginw();
00144 
00145   byte RValues[7] = {0,   0,   255, 0,   191, 191, 64};
00146   byte GValues[7] = {0,   128, 0,   191, 0,   191, 64};
00147   byte BValues[7] = {255, 0,   0,   191, 191, 0,   64};
00148 
00149   if (m > 7*7*7)
00150     LFATAL("Only %i values are allowed for max", 7*7*7);
00151 
00152   int i=0;
00153   for(int b=0; b<7; b++) 
00154     for(int g=0; g<7; g++) 
00155       for(int r=0; r<7; r++) 
00156       {
00157         if (i >= m)
00158           break;
00159         ptr[i] = PixRGB<byte>(RValues[r],GValues[g],BValues[b]);
00160         i++;
00161       }
00162 
00163   return ColorMap(cmap);
00164 }
00165 
00166 // ######################################################################
00167 /* So things look consistent in everyone's emacs... */
00168 /* Local Variables: */
00169 /* mode: c++ */
00170 /* indent-tabs-mode: nil */
00171 /* End: */
00172 
00173 #endif // IMAGE_COLORMAP_C_DEFINED
Generated on Sun May 8 08:40:48 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3