casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ColormapDefinition.h
Go to the documentation of this file.
00001 //# ColormapDefinition.h: wrapper for colormap function or look-up table
00002 //# Copyright (C) 1998,1999,2000,2001
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id$
00027 
00028 #ifndef TRIALDISPLAY_COLORMAPDEFINITION_H
00029 #define TRIALDISPLAY_COLORMAPDEFINITION_H
00030 
00031 #include <map>
00032 #include <casa/aips.h>
00033 #include <casa/Arrays/Vector.h>
00034 #include <casa/IO/AipsIO.h>
00035 #include <tables/Tables/Table.h>
00036 
00037 namespace casa { //# NAMESPACE CASA - BEGIN
00038 
00039 // <summary>
00040 // This class defines a wrapper for a color function or look-up table.
00041 // </summary>
00042 
00043 // <use visibility=local>
00044 
00045 // <reviewed reviewer="" date="yyyy/mm/dd" test="" demos="">
00046 // </reviewed>
00047 
00048 // <etymology>
00049 // "ColormapDefinition" is the thing one uses to define a colormap.
00050 // </etymology>
00051 
00052 // <synopsis>
00053 // ColormapDefinition is a class whose principal purpose is, given an
00054 // input value in the range <src>[0.0, 1.0]</src>, to return an RGB
00055 // triplet corresponding to that position in the colormap.  It can
00056 // read definitions from and write definitions to <linkto
00057 // class="Table"> Tables </linkto> on disk, thereby providing a
00058 // mechanism for using custom colormaps for display applications. 
00059 //
00060 
00061 // It supplies one built-in colormap <src>Greyscale 1</src>, which will
00062 // always be available. The other colormaps are stored in AIPS++
00063 // tables. The standard colormaps ar in
00064 // <src>/aips++/data/colormaps/default.tbl</src>. These can be
00065 // deactivated by putting <src>display.colormaps.usedefault: no</src> in
00066 // the <src>.aipsrc</src> file. 
00067 // It also supplies user defined colormaps. It looks for the complete
00068 // path to the user table in <src>.aipsrc</src> under
00069 // <src>display.colormaps.usertable:</src>.
00070 //
00071 // ColormapDefinition is used by the <linkto
00072 // class="Colormap">Colormap</linkto> class to generate color values
00073 // for the <linkto class="ColormapManager"> ColormapManager</linkto>.
00074 // </synopsis>
00075 
00076 // <example>
00077 // A ColormapDefinition corresponding to the system "rainbow" Colormap
00078 // can be obtained and used as follows:
00079 // <srcblock>
00080 // ColormapDefinition rainbowDefinition(String("rainbow"));
00081 // Float red, green, blue;
00082 // for (uInt i = 0; i <= 100; i++) {
00083 //   rainbowDefinition.getValues((Float)i/100.0, red, green, blue);
00084 //   // ... do something with red, green, blue ...
00085 // }
00086 // </srcblock>
00087 // Or a new ColormapDefinition with a red ramp and green and blue fixed
00088 // at 0.5 could be constructed and saved for later use as follows:
00089 // <srcblock>
00090 // Vector<Float> reds(40), greens(40), blues(40);
00091 // greens = 0.5;
00092 // blues = 0.5;
00093 // for (uInt i = 0; i < 40; i++) {
00094 //   reds(i) = (Float)i / 39.0;
00095 // }
00096 // ColormapDefinition simpleRamp("redRamp", reds, greens, blues);
00097 // Vector<String> synonyms(2);
00098 // synonyms(0) = "RedRamp";synonyms(1) = "redramp";
00099 // simpleRamp.save("mytable.tbl,synonyms);
00100 // </srcblock>
00101 // </example>
00102 
00103 // <motivation>
00104 // Needed to separate out ColormapDefinition from Colormap to give the
00105 // programmer a way to over-ride the definition without having to also
00106 // over-ride the shape function.
00107 // </motivation>
00108 
00109 // <thrown>
00110 // <li> AipsError: unrecognized map name 
00111 // <li> AipsError: incompatible colormap definition version 
00112 // </thrown>
00113 
00114 // <todo asof="1998/12/14">
00115 // <li> add checks for red/green/blue componenets to have same number
00116 //      of elements 
00117 // </todo>
00118 
00119 class RegEx;
00120 class String;
00121 
00122 class ColormapDefinition {
00123 
00124  public:
00125 
00126   // Construct a single color (white) Colormap
00127   ColormapDefinition();
00128 
00129   // Construct the known Colormap <src>mapName</src>, first looking for
00130   // a saved Colormap having this name, then resorting to a built-in
00131   // Colormap, and if that doesn't exist, throw an error
00132   explicit ColormapDefinition(const String & mapName);
00133   
00134   // Construct a new Colormap, using the supplied name and component
00135   // vectors
00136   ColormapDefinition(const String & mapName,
00137                      const Vector<Float> & reds, 
00138                      const Vector<Float> & greens,
00139                      const Vector<Float> & blues);
00140 
00141   // Obtain the Colormap value for the "index" value <src>0 <= t <= 1</src>
00142   void getValue(const Float t, Float & red, Float & green, Float & blue) const;
00143 
00144   // Change the Colormap values with the provided component vectors
00145   void setValues(const Vector<Float> & reds, 
00146                  const Vector<Float> & greens,
00147                  const Vector<Float> & blues);
00148 
00149   // Write this ColormapDefinition to the named Table in the named
00150   // directory (default values are obtained from the user's
00151   // <src>.aipsrc</src> file.  If <src>overwrite</src> is
00152   // <src>True</src>, then an existing map of the same name in the
00153   // Table will be over-written.  If the named Table does not exist,
00154   // it will be created.
00155 
00156   // The table format has to conform with following scheme. 
00157   // It must have five columns: 
00158   // <src>CMAP_NAME</src> a String
00159   // <src>RED</src> a Float array of dim n
00160   // <src>GREEN</src> a Float array of dim n
00161   // <src>BLUE</src> a Float array of dim n
00162   // <src>SYNONYMS</src> a String array of dim m
00163   Bool save(const String &fullPathName, 
00164             const Vector<String> &synonyms,
00165             const Bool &overwrite = True) const;
00166 
00167   // Return the names of the built-in colormaps.  If <src>uniqueonly</src>
00168   // is True (default), only the names of the unique colormaps 
00169   // are returned, otherwise all colormap names are returned.
00170   typedef std::map<String,bool> colormapnamemap;
00171   static colormapnamemap builtinColormapNames(Bool uniqueonly = True);
00172 
00173   // Load Colormap definitions for a specified colormap<src>name</src>
00174   Bool loadColormap(const String& name);
00175   Bool loadBuiltinColormap(const String& name);
00176 
00177   // Write a ColormapDefinition to an ostream in a simple text form.
00178   friend ostream & operator << (ostream & os, 
00179                                 const ColormapDefinition& pcreh);
00180 
00181 private:
00182 
00183   // The name of this ColormapDefinition
00184   String itsName;
00185   
00186   // Utility function to look if a colormap name is in a Table
00187   Bool queryColormapTable(const Table& table, const String& name);
00188 
00189   // load ColormapDefinitions from default location 
00190   // aips++/data/colormaps/default.tbl
00191   // and/or location specified in <src>display.colormaps.usertable</src>
00192   static void loadColormapTable();
00193   
00194   //The loaded colormaps (a replacement for the builtins)
00195   //<group>
00196   static String ourDefaultColormap;
00197   static Table ourDefaultColormapTable;
00198   static Table ourUserColormapTable;
00199   static String ourTableVersion;
00200   //</group>
00201 
00202   // The Color component vectors for this ColormapDefinition
00203   Vector<Float> itsReds;
00204   Vector<Float> itsGreens;
00205   Vector<Float> itsBlues;
00206 
00207 };
00208 
00209 
00210 } //# NAMESPACE CASA - END
00211 
00212 #endif