00001 /* 00002 * The information in this file is 00003 * Copyright(c) 2007 Ball Aerospace & Technologies Corporation 00004 * and is subject to the terms and conditions of the 00005 * GNU Lesser General Public License Version 2.1 00006 * The license text is available from 00007 * http://www.gnu.org/licenses/lgpl.html 00008 */ 00009 00010 #ifndef COLORMAP_H 00011 #define COLORMAP_H 00012 00013 #include "ColorType.h" 00014 00015 #include <stdio.h> 00016 #include <string> 00017 #include <vector> 00018 00019 class QIODevice; 00020 00021 /** 00022 * This class manages a color map. 00023 * 00024 * A colormap can be a hold any number of colors, and may contain RGB or RGBA 00025 * color. Each channel must have a value of between 0 and 256. 00026 */ 00027 class ColorMap 00028 { 00029 public: 00030 00031 /** 00032 * This struct defines a color table. 00033 * 00034 * This struct specifies a color lookup table by defining a series of colors and their 00035 * positions in the lookup table. All entries in the lookup table that fall between 00036 * defined colors will be computed by interpolating between the defined color Controls. 00037 */ 00038 struct Gradient 00039 { 00040 /** 00041 * This struct specifies a color and position in a color gradient. 00042 */ 00043 struct Control 00044 { 00045 ColorType mColor; /**< Specifies a color in the Gradient. */ 00046 int mPosition; /**< The index of the color specified by the control. Indices between 00047 two controls will be linearly interpolated between the controls. */ 00048 }; 00049 static const unsigned int MAX_CONTROLS = 20; /**< The maximum number of controls that can 00050 be specified. If more than this number of controls are specified 00051 the Gradient will be rejected. */ 00052 std::vector<Control> mControls; /**< The controls specifying the color lookup table 00053 defined by the Gradient. The positions of the controls must be 00054 monotonically increasing (i.e. the position of Control n must be 00055 greater than or equal to the position of Control n-1). The position 00056 of the first Control must be greater than or equal to mStartPosition. 00057 The position of the last Control must be less than or equal to 00058 mStopPosition. */ 00059 int mStartPosition; /**< The index in the color lookup table that the gradient begins. 00060 All entries in the table before this value will be filled with grayscale values. 00061 All entries in the table after this value but before the first control will be 00062 filled with the color of the first control. If no grayscale is desired, this 00063 should be set to 0. This value must be greater than or equal to 0 and less 00064 than or equal to the position of the first Control. */ 00065 int mStopPosition; /**< The index in the color lookup table that the gradient ends. 00066 All entries in the table after this value will be filled with grayscale values. 00067 All entries in the table before this value but after the last control will be 00068 filled with the color of the last control. If no grayscale is desired, this 00069 should be set to mNumIndices-1. This value must be less than mNumIndices 00070 and greater than or equal to the position of the last Control. */ 00071 int mNumIndices; /**< The number of colors in the color lookup table defined by the Gradient. */ 00072 }; 00073 00074 /** 00075 * Construct a default ColorMap. 00076 */ 00077 ColorMap(); 00078 00079 /** 00080 * Construct a ColorMap by loading it from the file specified. 00081 * The object's name is specified within the file. 00082 * 00083 * @param filename 00084 * The file to load the ColorMap from. 00085 * 00086 * @throw std::runtime_error 00087 * This exception is thrown if the file load fails. 00088 */ 00089 explicit ColorMap(const std::string& filename); 00090 00091 /** 00092 * Construct a new ColorMap from a vector of ColorType. 00093 * 00094 * @param name 00095 * The name of the new ColorMap. 00096 * @param table 00097 * The colors to place in the ColorMap. 00098 * 00099 * @throw std::runtime_error 00100 * This exception is thrown if there is an invalid color specified. 00101 */ 00102 ColorMap(const std::string& name, const std::vector<ColorType>& table); 00103 00104 /** 00105 * Construct a new ColorMap from a Gradient definition. 00106 * 00107 * @param name 00108 * The name of the new ColorMap. 00109 * @param gradient 00110 * The gradient object defining the colormap 00111 * 00112 * @throw std::runtime_error 00113 * This exception is thrown if there is an invalid color specified. 00114 */ 00115 ColorMap(const std::string& name, const Gradient &gradient); 00116 00117 /** 00118 * Copy an existing ColorMap. 00119 * 00120 * The new ColorMap will have the same name as the existing one. 00121 * 00122 * @param colorMap 00123 * The ColorMap to copy. 00124 */ 00125 ColorMap(const ColorMap& colorMap); 00126 00127 /** 00128 * Frees all resources associated with a ColorMap. 00129 */ 00130 ~ColorMap(); 00131 00132 /** 00133 * Assign from an existing ColorMap. 00134 * 00135 * The assigned ColorMap will have the same name as the existing one. 00136 * 00137 * @param colorMap 00138 * The ColorMap to assign from. 00139 */ 00140 ColorMap& operator=(const ColorMap& colorMap); 00141 00142 /** 00143 * Determine if ColorMaps are equal. 00144 * 00145 * @param colorMap 00146 * The map to compare against 00147 * 00148 * @return \c true if the maps are equal, \c false otherwise. 00149 */ 00150 bool operator==(const ColorMap& colorMap) const; 00151 00152 /** 00153 * Get the name of the ColorMap. 00154 * 00155 * @return The name of the object. 00156 */ 00157 const std::string& getName() const; 00158 00159 /** 00160 * Save the ColorMap to a file. 00161 * 00162 * @param filename 00163 * The name of the file to save to. 00164 * 00165 * @return \c true if the operation was a success, \c false otherwise. 00166 */ 00167 bool saveToFile(const std::string& filename) const; 00168 00169 /** 00170 * Save the ColorMap to a string buffer. 00171 * 00172 * @param buffer 00173 * The ColorMap data will be written to this string. The string 00174 * will be resized to accomidate the data. 00175 * 00176 * @return \c true if the operation was a success, \c false otherwise. 00177 */ 00178 bool saveToBuffer(std::string& buffer) const; 00179 00180 /** 00181 * Get the ColorMap's color table. 00182 * 00183 * @return The vector of ColorTypes for the ColorMap. 00184 */ 00185 const std::vector<ColorType>& getTable() const; 00186 00187 /** 00188 * Get a specific color from the map. 00189 * 00190 * @param index 00191 * The index to retrieve. This must be within the bounds of the map. 00192 * 00193 * @return The associated ColorType. 00194 */ 00195 const ColorType &operator[](int index) const; 00196 00197 /** 00198 * Get a specific color from the map as a non-const reference. 00199 * 00200 * This allows for code like map[i] = ColorType(255, 255, 255) 00201 * 00202 * @param index 00203 * The index to retrieve. This must be within the bounds of the map. 00204 * 00205 * @return The associated ColorType. 00206 */ 00207 ColorType &operator[](int index); 00208 00209 /** 00210 * Clear the table and name, replacing it with a default grayscale. 00211 */ 00212 void resetToDefault(); 00213 00214 /** 00215 * Determine if the ColorMap has not been changed from the default. 00216 * 00217 * @return \c true if the object is still default, \c false otherwise. 00218 */ 00219 bool isDefault() const; 00220 00221 /** 00222 * Determine if the ColorMap contains only fully opaque colors. 00223 * 00224 * @return \c true if the object is fully opaque, \c false otherwise. 00225 */ 00226 bool isFullyOpaque() const; 00227 00228 /** 00229 * Returns a pointer to the Gradient object that was used to define the 00230 * ColorMap, if any. 00231 * 00232 * @return A pointer to the Gradient object that was used to define the 00233 * ColorMap, or \c NULL if none was used. 00234 */ 00235 const Gradient *getGradientDefinition() const; 00236 00237 /** 00238 * Load ColorMap data from a file. 00239 * 00240 * @param filename 00241 * The file containing the ColorMap data. 00242 * 00243 * @return \c true if successful, \c false otherwise. 00244 */ 00245 bool loadFromFile(const std::string& filename); 00246 00247 /** 00248 * Load ColorMap data from a string buffer. 00249 * 00250 * @param buffer 00251 * The string containing the ColorMap data. 00252 * 00253 * @return \c true if successful, \c false otherwise. 00254 */ 00255 bool loadFromBuffer(const std::string& buffer); 00256 00257 private: 00258 static const unsigned int VERSION_ONE_TABLE_SIZE = 256; 00259 00260 bool serialize(QIODevice &io) const; 00261 bool deserialize(QIODevice &io); 00262 bool setTable(const std::string& name, const std::vector<ColorType>& table); 00263 static bool tableIsValid(const std::vector<ColorType>& table); 00264 std::vector<ColorType> tableFromGradient(const Gradient &gradient); 00265 bool deserializeGradient(QIODevice &io, int version); 00266 bool serializeGradient(QIODevice &io) const; 00267 00268 std::vector<ColorType> mTable; 00269 std::string mName; 00270 bool mIsDefault; 00271 Gradient* mpGradient; 00272 }; 00273 00274 #endif